fca46b170f22fabb667a0b7f2ffe1bfa515ce34c
[libav.git] / libavformat / ffm.c
1 /*
2 * FFM (ffserver live feed) encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include <unistd.h>
21
22 /* The FFM file is made of blocks of fixed size */
23 #define FFM_HEADER_SIZE 14
24 #define PACKET_ID 0x666d
25
26 /* each packet contains frames (which can span several packets */
27 #define FRAME_HEADER_SIZE 8
28 #define FLAG_KEY_FRAME 0x01
29
30 typedef struct FFMStream {
31 int64_t pts;
32 } FFMStream;
33
34 enum {
35 READ_HEADER,
36 READ_DATA,
37 };
38
39 typedef struct FFMContext {
40 /* only reading mode */
41 offset_t write_index, file_size;
42 int read_state;
43 uint8_t header[FRAME_HEADER_SIZE];
44
45 /* read and write */
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
47 int packet_size;
48 int frame_offset;
49 int64_t pts;
50 uint8_t *packet_ptr, *packet_end;
51 uint8_t packet[FFM_PACKET_SIZE];
52 } FFMContext;
53
54 static int64_t get_pts(AVFormatContext *s, offset_t pos);
55
56 /* disable pts hack for testing */
57 int ffm_nopts = 0;
58
59 #ifdef CONFIG_ENCODERS
60 static void flush_packet(AVFormatContext *s)
61 {
62 FFMContext *ffm = s->priv_data;
63 int fill_size, h;
64 ByteIOContext *pb = &s->pb;
65
66 fill_size = ffm->packet_end - ffm->packet_ptr;
67 memset(ffm->packet_ptr, 0, fill_size);
68
69 if (url_ftell(pb) % ffm->packet_size)
70 av_abort();
71
72 /* put header */
73 put_be16(pb, PACKET_ID);
74 put_be16(pb, fill_size);
75 put_be64(pb, ffm->pts);
76 h = ffm->frame_offset;
77 if (ffm->first_packet)
78 h |= 0x8000;
79 put_be16(pb, h);
80 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
81
82 /* prepare next packet */
83 ffm->frame_offset = 0; /* no key frame */
84 ffm->pts = 0; /* no pts */
85 ffm->packet_ptr = ffm->packet;
86 ffm->first_packet = 0;
87 }
88
89 /* 'first' is true if first data of a frame */
90 static void ffm_write_data(AVFormatContext *s,
91 const uint8_t *buf, int size,
92 int64_t pts, int first)
93 {
94 FFMContext *ffm = s->priv_data;
95 int len;
96
97 if (first && ffm->frame_offset == 0)
98 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
99 if (first && ffm->pts == 0)
100 ffm->pts = pts;
101
102 /* write as many packets as needed */
103 while (size > 0) {
104 len = ffm->packet_end - ffm->packet_ptr;
105 if (len > size)
106 len = size;
107 memcpy(ffm->packet_ptr, buf, len);
108
109 ffm->packet_ptr += len;
110 buf += len;
111 size -= len;
112 if (ffm->packet_ptr >= ffm->packet_end) {
113 /* special case : no pts in packet : we leave the current one */
114 if (ffm->pts == 0)
115 ffm->pts = pts;
116
117 flush_packet(s);
118 }
119 }
120 }
121
122 static int ffm_write_header(AVFormatContext *s)
123 {
124 FFMContext *ffm = s->priv_data;
125 AVStream *st;
126 FFMStream *fst;
127 ByteIOContext *pb = &s->pb;
128 AVCodecContext *codec;
129 int bit_rate, i;
130
131 ffm->packet_size = FFM_PACKET_SIZE;
132
133 /* header */
134 put_tag(pb, "FFM1");
135 put_be32(pb, ffm->packet_size);
136 /* XXX: store write position in other file ? */
137 put_be64(pb, ffm->packet_size); /* current write position */
138
139 put_be32(pb, s->nb_streams);
140 bit_rate = 0;
141 for(i=0;i<s->nb_streams;i++) {
142 st = s->streams[i];
143 bit_rate += st->codec.bit_rate;
144 }
145 put_be32(pb, bit_rate);
146
147 /* list of streams */
148 for(i=0;i<s->nb_streams;i++) {
149 st = s->streams[i];
150 fst = av_mallocz(sizeof(FFMStream));
151 if (!fst)
152 goto fail;
153 st->priv_data = fst;
154
155 codec = &st->codec;
156 /* generic info */
157 put_be32(pb, codec->codec_id);
158 put_byte(pb, codec->codec_type);
159 put_be32(pb, codec->bit_rate);
160 put_be32(pb, st->quality);
161 put_be32(pb, codec->flags);
162 /* specific info */
163 switch(codec->codec_type) {
164 case CODEC_TYPE_VIDEO:
165 put_be32(pb, codec->frame_rate_base);
166 put_be32(pb, codec->frame_rate);
167 put_be16(pb, codec->width);
168 put_be16(pb, codec->height);
169 put_be16(pb, codec->gop_size);
170 put_byte(pb, codec->qmin);
171 put_byte(pb, codec->qmax);
172 put_byte(pb, codec->max_qdiff);
173 put_be16(pb, (int) (codec->qcompress * 10000.0));
174 put_be16(pb, (int) (codec->qblur * 10000.0));
175 put_be32(pb, codec->bit_rate_tolerance);
176 put_strz(pb, codec->rc_eq);
177 put_be32(pb, codec->rc_max_rate);
178 put_be32(pb, codec->rc_min_rate);
179 put_be32(pb, codec->rc_buffer_size);
180 put_be64_double(pb, codec->i_quant_factor);
181 put_be64_double(pb, codec->b_quant_factor);
182 put_be64_double(pb, codec->i_quant_offset);
183 put_be64_double(pb, codec->b_quant_offset);
184 put_be32(pb, codec->dct_algo);
185 break;
186 case CODEC_TYPE_AUDIO:
187 put_be32(pb, codec->sample_rate);
188 put_le16(pb, codec->channels);
189 put_le16(pb, codec->frame_size);
190 break;
191 default:
192 av_abort();
193 }
194 /* hack to have real time */
195 if (ffm_nopts)
196 fst->pts = 0;
197 else
198 fst->pts = av_gettime();
199 }
200
201 /* flush until end of block reached */
202 while ((url_ftell(pb) % ffm->packet_size) != 0)
203 put_byte(pb, 0);
204
205 put_flush_packet(pb);
206
207 /* init packet mux */
208 ffm->packet_ptr = ffm->packet;
209 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
210 if (ffm->packet_end < ffm->packet)
211 av_abort();
212 ffm->frame_offset = 0;
213 ffm->pts = 0;
214 ffm->first_packet = 1;
215
216 return 0;
217 fail:
218 for(i=0;i<s->nb_streams;i++) {
219 st = s->streams[i];
220 av_freep(&st->priv_data);
221 }
222 return -1;
223 }
224
225 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
226 {
227 AVStream *st = s->streams[pkt->stream_index];
228 FFMStream *fst = st->priv_data;
229 int64_t pts;
230 uint8_t header[FRAME_HEADER_SIZE];
231 int duration;
232 int size= pkt->size;
233
234 //XXX/FIXME use duration from pkt
235 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
236 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
237 } else {
238 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
239 }
240
241 pts = fst->pts;
242 /* packet size & key_frame */
243 header[0] = pkt->stream_index;
244 header[1] = 0;
245 if (pkt->flags & PKT_FLAG_KEY)
246 header[1] |= FLAG_KEY_FRAME;
247 header[2] = (size >> 16) & 0xff;
248 header[3] = (size >> 8) & 0xff;
249 header[4] = size & 0xff;
250 header[5] = (duration >> 16) & 0xff;
251 header[6] = (duration >> 8) & 0xff;
252 header[7] = duration & 0xff;
253 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
254 ffm_write_data(s, pkt->data, size, pts, 0);
255
256 fst->pts += duration;
257 return 0;
258 }
259
260 static int ffm_write_trailer(AVFormatContext *s)
261 {
262 ByteIOContext *pb = &s->pb;
263 FFMContext *ffm = s->priv_data;
264 int i;
265
266 /* flush packets */
267 if (ffm->packet_ptr > ffm->packet)
268 flush_packet(s);
269
270 put_flush_packet(pb);
271
272 if (!url_is_streamed(pb)) {
273 int64_t size;
274 /* update the write offset */
275 size = url_ftell(pb);
276 url_fseek(pb, 8, SEEK_SET);
277 put_be64(pb, size);
278 put_flush_packet(pb);
279 }
280
281 for(i=0;i<s->nb_streams;i++)
282 av_freep(&s->streams[i]->priv_data);
283 return 0;
284 }
285 #endif //CONFIG_ENCODERS
286
287 /* ffm demux */
288
289 static int ffm_is_avail_data(AVFormatContext *s, int size)
290 {
291 FFMContext *ffm = s->priv_data;
292 offset_t pos, avail_size;
293 int len;
294
295 len = ffm->packet_end - ffm->packet_ptr;
296 if (!ffm_nopts) {
297 /* XXX: I don't understand this test, so I disabled it for testing */
298 if (size <= len)
299 return 1;
300 }
301 pos = url_ftell(&s->pb);
302 if (pos == ffm->write_index) {
303 /* exactly at the end of stream */
304 return 0;
305 } else if (pos < ffm->write_index) {
306 avail_size = ffm->write_index - pos;
307 } else {
308 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
309 }
310 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
311 if (size <= avail_size)
312 return 1;
313 else
314 return 0;
315 }
316
317 /* first is true if we read the frame header */
318 static int ffm_read_data(AVFormatContext *s,
319 uint8_t *buf, int size, int first)
320 {
321 FFMContext *ffm = s->priv_data;
322 ByteIOContext *pb = &s->pb;
323 int len, fill_size, size1, frame_offset;
324
325 size1 = size;
326 while (size > 0) {
327 redo:
328 len = ffm->packet_end - ffm->packet_ptr;
329 if (len > size)
330 len = size;
331 if (len == 0) {
332 if (url_ftell(pb) == ffm->file_size)
333 url_fseek(pb, ffm->packet_size, SEEK_SET);
334 retry_read:
335 get_be16(pb); /* PACKET_ID */
336 fill_size = get_be16(pb);
337 ffm->pts = get_be64(pb);
338 frame_offset = get_be16(pb);
339 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
340 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
341 if (ffm->packet_end < ffm->packet)
342 av_abort();
343 /* if first packet or resynchronization packet, we must
344 handle it specifically */
345 if (ffm->first_packet || (frame_offset & 0x8000)) {
346 if (!frame_offset) {
347 /* This packet has no frame headers in it */
348 if (url_ftell(pb) >= ffm->packet_size * 3) {
349 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
350 goto retry_read;
351 }
352 /* This is bad, we cannot find a valid frame header */
353 return 0;
354 }
355 ffm->first_packet = 0;
356 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
357 av_abort();
358 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
359 if (!first)
360 break;
361 } else {
362 ffm->packet_ptr = ffm->packet;
363 }
364 goto redo;
365 }
366 memcpy(buf, ffm->packet_ptr, len);
367 buf += len;
368 ffm->packet_ptr += len;
369 size -= len;
370 first = 0;
371 }
372 return size1 - size;
373 }
374
375
376 static void adjust_write_index(AVFormatContext *s)
377 {
378 FFMContext *ffm = s->priv_data;
379 ByteIOContext *pb = &s->pb;
380 int64_t pts;
381 //offset_t orig_write_index = ffm->write_index;
382 offset_t pos_min, pos_max;
383 int64_t pts_start;
384 offset_t ptr = url_ftell(pb);
385
386
387 pos_min = 0;
388 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
389
390 pts_start = get_pts(s, pos_min);
391
392 pts = get_pts(s, pos_max);
393
394 if (pts - 100000 > pts_start)
395 goto end;
396
397 ffm->write_index = FFM_PACKET_SIZE;
398
399 pts_start = get_pts(s, pos_min);
400
401 pts = get_pts(s, pos_max);
402
403 if (pts - 100000 <= pts_start) {
404 while (1) {
405 offset_t newpos;
406 int64_t newpts;
407
408 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
409
410 if (newpos == pos_min)
411 break;
412
413 newpts = get_pts(s, newpos);
414
415 if (newpts - 100000 <= pts) {
416 pos_max = newpos;
417 pts = newpts;
418 } else {
419 pos_min = newpos;
420 }
421 }
422 ffm->write_index += pos_max;
423 }
424
425 //printf("Adjusted write index from %lld to %lld: pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
426 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
427
428 end:
429 url_fseek(pb, ptr, SEEK_SET);
430 }
431
432
433 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
434 {
435 FFMContext *ffm = s->priv_data;
436 AVStream *st;
437 FFMStream *fst;
438 ByteIOContext *pb = &s->pb;
439 AVCodecContext *codec;
440 int i, nb_streams;
441 uint32_t tag;
442
443 /* header */
444 tag = get_le32(pb);
445 if (tag != MKTAG('F', 'F', 'M', '1'))
446 goto fail;
447 ffm->packet_size = get_be32(pb);
448 if (ffm->packet_size != FFM_PACKET_SIZE)
449 goto fail;
450 ffm->write_index = get_be64(pb);
451 /* get also filesize */
452 if (!url_is_streamed(pb)) {
453 ffm->file_size = url_filesize(url_fileno(pb));
454 adjust_write_index(s);
455 } else {
456 ffm->file_size = (uint64_t_C(1) << 63) - 1;
457 }
458
459 nb_streams = get_be32(pb);
460 get_be32(pb); /* total bitrate */
461 /* read each stream */
462 for(i=0;i<nb_streams;i++) {
463 char rc_eq_buf[128];
464
465 st = av_new_stream(s, 0);
466 if (!st)
467 goto fail;
468 fst = av_mallocz(sizeof(FFMStream));
469 if (!fst)
470 goto fail;
471
472 av_set_pts_info(st, 64, 1, 1000000);
473
474 st->priv_data = fst;
475
476 codec = &st->codec;
477 /* generic info */
478 st->codec.codec_id = get_be32(pb);
479 st->codec.codec_type = get_byte(pb); /* codec_type */
480 codec->bit_rate = get_be32(pb);
481 st->quality = get_be32(pb);
482 codec->flags = get_be32(pb);
483 /* specific info */
484 switch(codec->codec_type) {
485 case CODEC_TYPE_VIDEO:
486 codec->frame_rate_base = get_be32(pb);
487 codec->frame_rate = get_be32(pb);
488 codec->width = get_be16(pb);
489 codec->height = get_be16(pb);
490 codec->gop_size = get_be16(pb);
491 codec->qmin = get_byte(pb);
492 codec->qmax = get_byte(pb);
493 codec->max_qdiff = get_byte(pb);
494 codec->qcompress = get_be16(pb) / 10000.0;
495 codec->qblur = get_be16(pb) / 10000.0;
496 codec->bit_rate_tolerance = get_be32(pb);
497 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
498 codec->rc_max_rate = get_be32(pb);
499 codec->rc_min_rate = get_be32(pb);
500 codec->rc_buffer_size = get_be32(pb);
501 codec->i_quant_factor = get_be64_double(pb);
502 codec->b_quant_factor = get_be64_double(pb);
503 codec->i_quant_offset = get_be64_double(pb);
504 codec->b_quant_offset = get_be64_double(pb);
505 codec->dct_algo = get_be32(pb);
506 break;
507 case CODEC_TYPE_AUDIO:
508 codec->sample_rate = get_be32(pb);
509 codec->channels = get_le16(pb);
510 codec->frame_size = get_le16(pb);
511 break;
512 default:
513 goto fail;
514 }
515
516 }
517
518 /* get until end of block reached */
519 while ((url_ftell(pb) % ffm->packet_size) != 0)
520 get_byte(pb);
521
522 /* init packet demux */
523 ffm->packet_ptr = ffm->packet;
524 ffm->packet_end = ffm->packet;
525 ffm->frame_offset = 0;
526 ffm->pts = 0;
527 ffm->read_state = READ_HEADER;
528 ffm->first_packet = 1;
529 return 0;
530 fail:
531 for(i=0;i<s->nb_streams;i++) {
532 st = s->streams[i];
533 if (st) {
534 av_freep(&st->priv_data);
535 av_free(st);
536 }
537 }
538 return -1;
539 }
540
541 /* return < 0 if eof */
542 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
543 {
544 int size;
545 FFMContext *ffm = s->priv_data;
546 int duration;
547
548 switch(ffm->read_state) {
549 case READ_HEADER:
550 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
551 return -EAGAIN;
552 }
553 #if 0
554 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
555 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
556 #endif
557 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
558 FRAME_HEADER_SIZE)
559 return -EAGAIN;
560 #if 0
561 {
562 int i;
563 for(i=0;i<FRAME_HEADER_SIZE;i++)
564 printf("%02x ", ffm->header[i]);
565 printf("\n");
566 }
567 #endif
568 ffm->read_state = READ_DATA;
569 /* fall thru */
570 case READ_DATA:
571 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
572 if (!ffm_is_avail_data(s, size)) {
573 return -EAGAIN;
574 }
575
576 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
577
578 av_new_packet(pkt, size);
579 pkt->stream_index = ffm->header[0];
580 if (ffm->header[1] & FLAG_KEY_FRAME)
581 pkt->flags |= PKT_FLAG_KEY;
582
583 ffm->read_state = READ_HEADER;
584 if (ffm_read_data(s, pkt->data, size, 0) != size) {
585 /* bad case: desynchronized packet. we cancel all the packet loading */
586 av_free_packet(pkt);
587 return -EAGAIN;
588 }
589 pkt->pts = ffm->pts;
590 pkt->duration = duration;
591 break;
592 }
593 return 0;
594 }
595
596 //#define DEBUG_SEEK
597
598 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
599 by the write position inside this function */
600 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
601 {
602 FFMContext *ffm = s->priv_data;
603 ByteIOContext *pb = &s->pb;
604 offset_t pos;
605
606 pos = pos1 + ffm->write_index;
607 if (pos >= ffm->file_size)
608 pos -= (ffm->file_size - FFM_PACKET_SIZE);
609 #ifdef DEBUG_SEEK
610 printf("seek to %Lx -> %Lx\n", pos1, pos);
611 #endif
612 url_fseek(pb, pos, SEEK_SET);
613 }
614
615 static int64_t get_pts(AVFormatContext *s, offset_t pos)
616 {
617 ByteIOContext *pb = &s->pb;
618 int64_t pts;
619
620 ffm_seek1(s, pos);
621 url_fskip(pb, 4);
622 pts = get_be64(pb);
623 #ifdef DEBUG_SEEK
624 printf("pts=%0.6f\n", pts / 1000000.0);
625 #endif
626 return pts;
627 }
628
629 /* seek to a given time in the file. The file read pointer is
630 positionned at or before pts. XXX: the following code is quite
631 approximative */
632 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
633 {
634 FFMContext *ffm = s->priv_data;
635 offset_t pos_min, pos_max, pos;
636 int64_t pts_min, pts_max, pts;
637 double pos1;
638
639 #ifdef DEBUG_SEEK
640 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
641 #endif
642 /* find the position using linear interpolation (better than
643 dichotomy in typical cases) */
644 pos_min = 0;
645 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
646 while (pos_min <= pos_max) {
647 pts_min = get_pts(s, pos_min);
648 pts_max = get_pts(s, pos_max);
649 /* linear interpolation */
650 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
651 (double)(pts_max - pts_min);
652 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
653 if (pos <= pos_min)
654 pos = pos_min;
655 else if (pos >= pos_max)
656 pos = pos_max;
657 pts = get_pts(s, pos);
658 /* check if we are lucky */
659 if (pts == wanted_pts) {
660 goto found;
661 } else if (pts > wanted_pts) {
662 pos_max = pos - FFM_PACKET_SIZE;
663 } else {
664 pos_min = pos + FFM_PACKET_SIZE;
665 }
666 }
667 pos = pos_min;
668 if (pos > 0)
669 pos -= FFM_PACKET_SIZE;
670 found:
671 ffm_seek1(s, pos);
672 return 0;
673 }
674
675 offset_t ffm_read_write_index(int fd)
676 {
677 uint8_t buf[8];
678 offset_t pos;
679 int i;
680
681 lseek(fd, 8, SEEK_SET);
682 read(fd, buf, 8);
683 pos = 0;
684 for(i=0;i<8;i++)
685 pos |= (int64_t)buf[i] << (56 - i * 8);
686 return pos;
687 }
688
689 void ffm_write_write_index(int fd, offset_t pos)
690 {
691 uint8_t buf[8];
692 int i;
693
694 for(i=0;i<8;i++)
695 buf[i] = (pos >> (56 - i * 8)) & 0xff;
696 lseek(fd, 8, SEEK_SET);
697 write(fd, buf, 8);
698 }
699
700 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
701 {
702 FFMContext *ffm = s->priv_data;
703 ffm->write_index = pos;
704 ffm->file_size = file_size;
705 }
706
707 static int ffm_read_close(AVFormatContext *s)
708 {
709 AVStream *st;
710 int i;
711
712 for(i=0;i<s->nb_streams;i++) {
713 st = s->streams[i];
714 av_freep(&st->priv_data);
715 }
716 return 0;
717 }
718
719 static int ffm_probe(AVProbeData *p)
720 {
721 if (p->buf_size >= 4 &&
722 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
723 p->buf[3] == '1')
724 return AVPROBE_SCORE_MAX + 1;
725 return 0;
726 }
727
728 static AVInputFormat ffm_iformat = {
729 "ffm",
730 "ffm format",
731 sizeof(FFMContext),
732 ffm_probe,
733 ffm_read_header,
734 ffm_read_packet,
735 ffm_read_close,
736 ffm_seek,
737 };
738
739 #ifdef CONFIG_ENCODERS
740 static AVOutputFormat ffm_oformat = {
741 "ffm",
742 "ffm format",
743 "",
744 "ffm",
745 sizeof(FFMContext),
746 /* not really used */
747 CODEC_ID_MP2,
748 CODEC_ID_MPEG1VIDEO,
749 ffm_write_header,
750 ffm_write_packet,
751 ffm_write_trailer,
752 };
753 #endif //CONFIG_ENCODERS
754
755 int ffm_init(void)
756 {
757 av_register_input_format(&ffm_iformat);
758 #ifdef CONFIG_ENCODERS
759 av_register_output_format(&ffm_oformat);
760 #endif //CONFIG_ENCODERS
761 return 0;
762 }