comparison ffm.c @ 65:a58a8a53eb46 libavformat

* UINTX -> uintx_t INTX -> intx_t
author kabi
date Tue, 11 Feb 2003 16:35:48 +0000
parents f17e285df237
children 8bdaec4e3e4b
comparison
equal deleted inserted replaced
64:b0e0eb595e29 65:a58a8a53eb46
26 /* each packet contains frames (which can span several packets */ 26 /* each packet contains frames (which can span several packets */
27 #define FRAME_HEADER_SIZE 8 27 #define FRAME_HEADER_SIZE 8
28 #define FLAG_KEY_FRAME 0x01 28 #define FLAG_KEY_FRAME 0x01
29 29
30 typedef struct FFMStream { 30 typedef struct FFMStream {
31 INT64 pts; 31 int64_t pts;
32 } FFMStream; 32 } FFMStream;
33 33
34 enum { 34 enum {
35 READ_HEADER, 35 READ_HEADER,
36 READ_DATA, 36 READ_DATA,
38 38
39 typedef struct FFMContext { 39 typedef struct FFMContext {
40 /* only reading mode */ 40 /* only reading mode */
41 offset_t write_index, file_size; 41 offset_t write_index, file_size;
42 int read_state; 42 int read_state;
43 UINT8 header[FRAME_HEADER_SIZE]; 43 uint8_t header[FRAME_HEADER_SIZE];
44 44
45 /* read and write */ 45 /* read and write */
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */ 46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
47 int packet_size; 47 int packet_size;
48 int frame_offset; 48 int frame_offset;
49 INT64 pts; 49 int64_t pts;
50 UINT8 *packet_ptr, *packet_end; 50 uint8_t *packet_ptr, *packet_end;
51 UINT8 packet[FFM_PACKET_SIZE]; 51 uint8_t packet[FFM_PACKET_SIZE];
52 } FFMContext; 52 } FFMContext;
53 53
54 /* disable pts hack for testing */ 54 /* disable pts hack for testing */
55 int ffm_nopts = 0; 55 int ffm_nopts = 0;
56 56
80 ffm->first_packet = 0; 80 ffm->first_packet = 0;
81 } 81 }
82 82
83 /* 'first' is true if first data of a frame */ 83 /* 'first' is true if first data of a frame */
84 static void ffm_write_data(AVFormatContext *s, 84 static void ffm_write_data(AVFormatContext *s,
85 UINT8 *buf, int size, 85 uint8_t *buf, int size,
86 INT64 pts, int first) 86 int64_t pts, int first)
87 { 87 {
88 FFMContext *ffm = s->priv_data; 88 FFMContext *ffm = s->priv_data;
89 int len; 89 int len;
90 90
91 if (first && ffm->frame_offset == 0) 91 if (first && ffm->frame_offset == 0)
212 } 212 }
213 return -1; 213 return -1;
214 } 214 }
215 215
216 static int ffm_write_packet(AVFormatContext *s, int stream_index, 216 static int ffm_write_packet(AVFormatContext *s, int stream_index,
217 UINT8 *buf, int size, int force_pts) 217 uint8_t *buf, int size, int force_pts)
218 { 218 {
219 AVStream *st = s->streams[stream_index]; 219 AVStream *st = s->streams[stream_index];
220 FFMStream *fst = st->priv_data; 220 FFMStream *fst = st->priv_data;
221 INT64 pts; 221 int64_t pts;
222 UINT8 header[FRAME_HEADER_SIZE]; 222 uint8_t header[FRAME_HEADER_SIZE];
223 int duration; 223 int duration;
224 224
225 if (st->codec.codec_type == CODEC_TYPE_AUDIO) { 225 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
226 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0); 226 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
227 } else { 227 } else {
258 flush_packet(s); 258 flush_packet(s);
259 259
260 put_flush_packet(pb); 260 put_flush_packet(pb);
261 261
262 if (!url_is_streamed(pb)) { 262 if (!url_is_streamed(pb)) {
263 INT64 size; 263 int64_t size;
264 /* update the write offset */ 264 /* update the write offset */
265 size = url_ftell(pb); 265 size = url_ftell(pb);
266 url_fseek(pb, 8, SEEK_SET); 266 url_fseek(pb, 8, SEEK_SET);
267 put_be64(pb, size); 267 put_be64(pb, size);
268 put_flush_packet(pb); 268 put_flush_packet(pb);
303 return 0; 303 return 0;
304 } 304 }
305 305
306 /* first is true if we read the frame header */ 306 /* first is true if we read the frame header */
307 static int ffm_read_data(AVFormatContext *s, 307 static int ffm_read_data(AVFormatContext *s,
308 UINT8 *buf, int size, int first) 308 uint8_t *buf, int size, int first)
309 { 309 {
310 FFMContext *ffm = s->priv_data; 310 FFMContext *ffm = s->priv_data;
311 ByteIOContext *pb = &s->pb; 311 ByteIOContext *pb = &s->pb;
312 int len, fill_size, size1, frame_offset; 312 int len, fill_size, size1, frame_offset;
313 313
366 AVStream *st; 366 AVStream *st;
367 FFMStream *fst; 367 FFMStream *fst;
368 ByteIOContext *pb = &s->pb; 368 ByteIOContext *pb = &s->pb;
369 AVCodecContext *codec; 369 AVCodecContext *codec;
370 int i; 370 int i;
371 UINT32 tag; 371 uint32_t tag;
372 372
373 /* header */ 373 /* header */
374 tag = get_le32(pb); 374 tag = get_le32(pb);
375 if (tag != MKTAG('F', 'F', 'M', '1')) 375 if (tag != MKTAG('F', 'F', 'M', '1'))
376 goto fail; 376 goto fail;
380 ffm->write_index = get_be64(pb); 380 ffm->write_index = get_be64(pb);
381 /* get also filesize */ 381 /* get also filesize */
382 if (!url_is_streamed(pb)) { 382 if (!url_is_streamed(pb)) {
383 ffm->file_size = url_filesize(url_fileno(pb)); 383 ffm->file_size = url_filesize(url_fileno(pb));
384 } else { 384 } else {
385 ffm->file_size = (UINT64_C(1) << 63) - 1; 385 ffm->file_size = (uint64_t_C(1) << 63) - 1;
386 } 386 }
387 387
388 s->nb_streams = get_be32(pb); 388 s->nb_streams = get_be32(pb);
389 get_be32(pb); /* total bitrate */ 389 get_be32(pb); /* total bitrate */
390 /* read each stream */ 390 /* read each stream */
409 st->quality = get_be32(pb); 409 st->quality = get_be32(pb);
410 codec->flags = get_be32(pb); 410 codec->flags = get_be32(pb);
411 /* specific info */ 411 /* specific info */
412 switch(codec->codec_type) { 412 switch(codec->codec_type) {
413 case CODEC_TYPE_VIDEO: 413 case CODEC_TYPE_VIDEO:
414 codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000; 414 codec->frame_rate = ((int64_t)get_be32(pb) * FRAME_RATE_BASE) / 1000;
415 codec->width = get_be16(pb); 415 codec->width = get_be16(pb);
416 codec->height = get_be16(pb); 416 codec->height = get_be16(pb);
417 codec->gop_size = get_be16(pb); 417 codec->gop_size = get_be16(pb);
418 codec->qmin = get_byte(pb); 418 codec->qmin = get_byte(pb);
419 codec->qmax = get_byte(pb); 419 codec->qmax = get_byte(pb);
537 printf("seek to %Lx -> %Lx\n", pos1, pos); 537 printf("seek to %Lx -> %Lx\n", pos1, pos);
538 #endif 538 #endif
539 url_fseek(pb, pos, SEEK_SET); 539 url_fseek(pb, pos, SEEK_SET);
540 } 540 }
541 541
542 static INT64 get_pts(AVFormatContext *s, offset_t pos) 542 static int64_t get_pts(AVFormatContext *s, offset_t pos)
543 { 543 {
544 ByteIOContext *pb = &s->pb; 544 ByteIOContext *pb = &s->pb;
545 INT64 pts; 545 int64_t pts;
546 546
547 ffm_seek1(s, pos); 547 ffm_seek1(s, pos);
548 url_fskip(pb, 4); 548 url_fskip(pb, 4);
549 pts = get_be64(pb); 549 pts = get_be64(pb);
550 #ifdef DEBUG_SEEK 550 #ifdef DEBUG_SEEK
554 } 554 }
555 555
556 /* seek to a given time in the file. The file read pointer is 556 /* seek to a given time in the file. The file read pointer is
557 positionned at or before pts. XXX: the following code is quite 557 positionned at or before pts. XXX: the following code is quite
558 approximative */ 558 approximative */
559 static int ffm_seek(AVFormatContext *s, INT64 wanted_pts) 559 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
560 { 560 {
561 FFMContext *ffm = s->priv_data; 561 FFMContext *ffm = s->priv_data;
562 offset_t pos_min, pos_max, pos; 562 offset_t pos_min, pos_max, pos;
563 INT64 pts_min, pts_max, pts; 563 int64_t pts_min, pts_max, pts;
564 double pos1; 564 double pos1;
565 565
566 #ifdef DEBUG_SEEK 566 #ifdef DEBUG_SEEK
567 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0); 567 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
568 #endif 568 #endif
574 pts_min = get_pts(s, pos_min); 574 pts_min = get_pts(s, pos_min);
575 pts_max = get_pts(s, pos_max); 575 pts_max = get_pts(s, pos_max);
576 /* linear interpolation */ 576 /* linear interpolation */
577 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) / 577 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
578 (double)(pts_max - pts_min); 578 (double)(pts_max - pts_min);
579 pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE; 579 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
580 if (pos <= pos_min) 580 if (pos <= pos_min)
581 pos = pos_min; 581 pos = pos_min;
582 else if (pos >= pos_max) 582 else if (pos >= pos_max)
583 pos = pos_max; 583 pos = pos_max;
584 pts = get_pts(s, pos); 584 pts = get_pts(s, pos);
599 return 0; 599 return 0;
600 } 600 }
601 601
602 offset_t ffm_read_write_index(int fd) 602 offset_t ffm_read_write_index(int fd)
603 { 603 {
604 UINT8 buf[8]; 604 uint8_t buf[8];
605 offset_t pos; 605 offset_t pos;
606 int i; 606 int i;
607 607
608 lseek(fd, 8, SEEK_SET); 608 lseek(fd, 8, SEEK_SET);
609 read(fd, buf, 8); 609 read(fd, buf, 8);
613 return pos; 613 return pos;
614 } 614 }
615 615
616 void ffm_write_write_index(int fd, offset_t pos) 616 void ffm_write_write_index(int fd, offset_t pos)
617 { 617 {
618 UINT8 buf[8]; 618 uint8_t buf[8];
619 int i; 619 int i;
620 620
621 for(i=0;i<8;i++) 621 for(i=0;i<8;i++)
622 buf[i] = (pos >> (56 - i * 8)) & 0xff; 622 buf[i] = (pos >> (56 - i * 8)) & 0xff;
623 lseek(fd, 8, SEEK_SET); 623 lseek(fd, 8, SEEK_SET);