ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
[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 av_set_pts_info(st, 64, 1, 1000000);
154 st->priv_data = fst;
155
156 codec = &st->codec;
157 /* generic info */
158 put_be32(pb, codec->codec_id);
159 put_byte(pb, codec->codec_type);
160 put_be32(pb, codec->bit_rate);
161 put_be32(pb, st->quality);
162 put_be32(pb, codec->flags);
163 /* specific info */
164 switch(codec->codec_type) {
165 case CODEC_TYPE_VIDEO:
166 put_be32(pb, codec->frame_rate_base);
167 put_be32(pb, codec->frame_rate);
168 put_be16(pb, codec->width);
169 put_be16(pb, codec->height);
170 put_be16(pb, codec->gop_size);
171 put_byte(pb, codec->qmin);
172 put_byte(pb, codec->qmax);
173 put_byte(pb, codec->max_qdiff);
174 put_be16(pb, (int) (codec->qcompress * 10000.0));
175 put_be16(pb, (int) (codec->qblur * 10000.0));
176 put_be32(pb, codec->bit_rate_tolerance);
177 put_strz(pb, codec->rc_eq);
178 put_be32(pb, codec->rc_max_rate);
179 put_be32(pb, codec->rc_min_rate);
180 put_be32(pb, codec->rc_buffer_size);
181 put_be64_double(pb, codec->i_quant_factor);
182 put_be64_double(pb, codec->b_quant_factor);
183 put_be64_double(pb, codec->i_quant_offset);
184 put_be64_double(pb, codec->b_quant_offset);
185 put_be32(pb, codec->dct_algo);
186 break;
187 case CODEC_TYPE_AUDIO:
188 put_be32(pb, codec->sample_rate);
189 put_le16(pb, codec->channels);
190 put_le16(pb, codec->frame_size);
191 break;
192 default:
193 av_abort();
194 }
195 /* hack to have real time */
196 if (ffm_nopts)
197 fst->pts = 0;
198 else
199 fst->pts = av_gettime();
200 }
201
202 /* flush until end of block reached */
203 while ((url_ftell(pb) % ffm->packet_size) != 0)
204 put_byte(pb, 0);
205
206 put_flush_packet(pb);
207
208 /* init packet mux */
209 ffm->packet_ptr = ffm->packet;
210 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
211 if (ffm->packet_end < ffm->packet)
212 av_abort();
213 ffm->frame_offset = 0;
214 ffm->pts = 0;
215 ffm->first_packet = 1;
216
217 return 0;
218 fail:
219 for(i=0;i<s->nb_streams;i++) {
220 st = s->streams[i];
221 av_freep(&st->priv_data);
222 }
223 return -1;
224 }
225
226 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
227 {
228 AVStream *st = s->streams[pkt->stream_index];
229 FFMStream *fst = st->priv_data;
230 int64_t pts;
231 uint8_t header[FRAME_HEADER_SIZE];
232 int duration;
233 int size= pkt->size;
234
235 //XXX/FIXME use duration from pkt
236 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
237 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
238 } else {
239 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
240 }
241
242 pts = fst->pts;
243 /* packet size & key_frame */
244 header[0] = pkt->stream_index;
245 header[1] = 0;
246 if (pkt->flags & PKT_FLAG_KEY)
247 header[1] |= FLAG_KEY_FRAME;
248 header[2] = (size >> 16) & 0xff;
249 header[3] = (size >> 8) & 0xff;
250 header[4] = size & 0xff;
251 header[5] = (duration >> 16) & 0xff;
252 header[6] = (duration >> 8) & 0xff;
253 header[7] = duration & 0xff;
254 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
255 ffm_write_data(s, pkt->data, size, pts, 0);
256
257 fst->pts += duration;
258 return 0;
259 }
260
261 static int ffm_write_trailer(AVFormatContext *s)
262 {
263 ByteIOContext *pb = &s->pb;
264 FFMContext *ffm = s->priv_data;
265 int i;
266
267 /* flush packets */
268 if (ffm->packet_ptr > ffm->packet)
269 flush_packet(s);
270
271 put_flush_packet(pb);
272
273 if (!url_is_streamed(pb)) {
274 int64_t size;
275 /* update the write offset */
276 size = url_ftell(pb);
277 url_fseek(pb, 8, SEEK_SET);
278 put_be64(pb, size);
279 put_flush_packet(pb);
280 }
281
282 for(i=0;i<s->nb_streams;i++)
283 av_freep(&s->streams[i]->priv_data);
284 return 0;
285 }
286 #endif //CONFIG_ENCODERS
287
288 /* ffm demux */
289
290 static int ffm_is_avail_data(AVFormatContext *s, int size)
291 {
292 FFMContext *ffm = s->priv_data;
293 offset_t pos, avail_size;
294 int len;
295
296 len = ffm->packet_end - ffm->packet_ptr;
297 if (!ffm_nopts) {
298 /* XXX: I don't understand this test, so I disabled it for testing */
299 if (size <= len)
300 return 1;
301 }
302 pos = url_ftell(&s->pb);
303 if (pos == ffm->write_index) {
304 /* exactly at the end of stream */
305 return 0;
306 } else if (pos < ffm->write_index) {
307 avail_size = ffm->write_index - pos;
308 } else {
309 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
310 }
311 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
312 if (size <= avail_size)
313 return 1;
314 else
315 return 0;
316 }
317
318 /* first is true if we read the frame header */
319 static int ffm_read_data(AVFormatContext *s,
320 uint8_t *buf, int size, int first)
321 {
322 FFMContext *ffm = s->priv_data;
323 ByteIOContext *pb = &s->pb;
324 int len, fill_size, size1, frame_offset;
325
326 size1 = size;
327 while (size > 0) {
328 redo:
329 len = ffm->packet_end - ffm->packet_ptr;
330 if (len > size)
331 len = size;
332 if (len == 0) {
333 if (url_ftell(pb) == ffm->file_size)
334 url_fseek(pb, ffm->packet_size, SEEK_SET);
335 retry_read:
336 get_be16(pb); /* PACKET_ID */
337 fill_size = get_be16(pb);
338 ffm->pts = get_be64(pb);
339 frame_offset = get_be16(pb);
340 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
341 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
342 if (ffm->packet_end < ffm->packet)
343 av_abort();
344 /* if first packet or resynchronization packet, we must
345 handle it specifically */
346 if (ffm->first_packet || (frame_offset & 0x8000)) {
347 if (!frame_offset) {
348 /* This packet has no frame headers in it */
349 if (url_ftell(pb) >= ffm->packet_size * 3) {
350 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
351 goto retry_read;
352 }
353 /* This is bad, we cannot find a valid frame header */
354 return 0;
355 }
356 ffm->first_packet = 0;
357 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
358 av_abort();
359 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
360 if (!first)
361 break;
362 } else {
363 ffm->packet_ptr = ffm->packet;
364 }
365 goto redo;
366 }
367 memcpy(buf, ffm->packet_ptr, len);
368 buf += len;
369 ffm->packet_ptr += len;
370 size -= len;
371 first = 0;
372 }
373 return size1 - size;
374 }
375
376
377 static void adjust_write_index(AVFormatContext *s)
378 {
379 FFMContext *ffm = s->priv_data;
380 ByteIOContext *pb = &s->pb;
381 int64_t pts;
382 //offset_t orig_write_index = ffm->write_index;
383 offset_t pos_min, pos_max;
384 int64_t pts_start;
385 offset_t ptr = url_ftell(pb);
386
387
388 pos_min = 0;
389 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
390
391 pts_start = get_pts(s, pos_min);
392
393 pts = get_pts(s, pos_max);
394
395 if (pts - 100000 > pts_start)
396 goto end;
397
398 ffm->write_index = FFM_PACKET_SIZE;
399
400 pts_start = get_pts(s, pos_min);
401
402 pts = get_pts(s, pos_max);
403
404 if (pts - 100000 <= pts_start) {
405 while (1) {
406 offset_t newpos;
407 int64_t newpts;
408
409 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
410
411 if (newpos == pos_min)
412 break;
413
414 newpts = get_pts(s, newpos);
415
416 if (newpts - 100000 <= pts) {
417 pos_max = newpos;
418 pts = newpts;
419 } else {
420 pos_min = newpos;
421 }
422 }
423 ffm->write_index += pos_max;
424 }
425
426 //printf("Adjusted write index from %lld to %lld: pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
427 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
428
429 end:
430 url_fseek(pb, ptr, SEEK_SET);
431 }
432
433
434 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
435 {
436 FFMContext *ffm = s->priv_data;
437 AVStream *st;
438 FFMStream *fst;
439 ByteIOContext *pb = &s->pb;
440 AVCodecContext *codec;
441 int i, nb_streams;
442 uint32_t tag;
443
444 /* header */
445 tag = get_le32(pb);
446 if (tag != MKTAG('F', 'F', 'M', '1'))
447 goto fail;
448 ffm->packet_size = get_be32(pb);
449 if (ffm->packet_size != FFM_PACKET_SIZE)
450 goto fail;
451 ffm->write_index = get_be64(pb);
452 /* get also filesize */
453 if (!url_is_streamed(pb)) {
454 ffm->file_size = url_filesize(url_fileno(pb));
455 adjust_write_index(s);
456 } else {
457 ffm->file_size = (uint64_t_C(1) << 63) - 1;
458 }
459
460 nb_streams = get_be32(pb);
461 get_be32(pb); /* total bitrate */
462 /* read each stream */
463 for(i=0;i<nb_streams;i++) {
464 char rc_eq_buf[128];
465
466 st = av_new_stream(s, 0);
467 if (!st)
468 goto fail;
469 fst = av_mallocz(sizeof(FFMStream));
470 if (!fst)
471 goto fail;
472
473 av_set_pts_info(st, 64, 1, 1000000);
474
475 st->priv_data = fst;
476
477 codec = &st->codec;
478 /* generic info */
479 st->codec.codec_id = get_be32(pb);
480 st->codec.codec_type = get_byte(pb); /* codec_type */
481 codec->bit_rate = get_be32(pb);
482 st->quality = get_be32(pb);
483 codec->flags = get_be32(pb);
484 /* specific info */
485 switch(codec->codec_type) {
486 case CODEC_TYPE_VIDEO:
487 codec->frame_rate_base = get_be32(pb);
488 codec->frame_rate = get_be32(pb);
489 codec->width = get_be16(pb);
490 codec->height = get_be16(pb);
491 codec->gop_size = get_be16(pb);
492 codec->qmin = get_byte(pb);
493 codec->qmax = get_byte(pb);
494 codec->max_qdiff = get_byte(pb);
495 codec->qcompress = get_be16(pb) / 10000.0;
496 codec->qblur = get_be16(pb) / 10000.0;
497 codec->bit_rate_tolerance = get_be32(pb);
498 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
499 codec->rc_max_rate = get_be32(pb);
500 codec->rc_min_rate = get_be32(pb);
501 codec->rc_buffer_size = get_be32(pb);
502 codec->i_quant_factor = get_be64_double(pb);
503 codec->b_quant_factor = get_be64_double(pb);
504 codec->i_quant_offset = get_be64_double(pb);
505 codec->b_quant_offset = get_be64_double(pb);
506 codec->dct_algo = get_be32(pb);
507 break;
508 case CODEC_TYPE_AUDIO:
509 codec->sample_rate = get_be32(pb);
510 codec->channels = get_le16(pb);
511 codec->frame_size = get_le16(pb);
512 break;
513 default:
514 goto fail;
515 }
516
517 }
518
519 /* get until end of block reached */
520 while ((url_ftell(pb) % ffm->packet_size) != 0)
521 get_byte(pb);
522
523 /* init packet demux */
524 ffm->packet_ptr = ffm->packet;
525 ffm->packet_end = ffm->packet;
526 ffm->frame_offset = 0;
527 ffm->pts = 0;
528 ffm->read_state = READ_HEADER;
529 ffm->first_packet = 1;
530 return 0;
531 fail:
532 for(i=0;i<s->nb_streams;i++) {
533 st = s->streams[i];
534 if (st) {
535 av_freep(&st->priv_data);
536 av_free(st);
537 }
538 }
539 return -1;
540 }
541
542 /* return < 0 if eof */
543 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
544 {
545 int size;
546 FFMContext *ffm = s->priv_data;
547 int duration;
548
549 switch(ffm->read_state) {
550 case READ_HEADER:
551 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
552 return -EAGAIN;
553 }
554 #if 0
555 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
556 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
557 #endif
558 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
559 FRAME_HEADER_SIZE)
560 return -EAGAIN;
561 #if 0
562 {
563 int i;
564 for(i=0;i<FRAME_HEADER_SIZE;i++)
565 printf("%02x ", ffm->header[i]);
566 printf("\n");
567 }
568 #endif
569 ffm->read_state = READ_DATA;
570 /* fall thru */
571 case READ_DATA:
572 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
573 if (!ffm_is_avail_data(s, size)) {
574 return -EAGAIN;
575 }
576
577 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
578
579 av_new_packet(pkt, size);
580 pkt->stream_index = ffm->header[0];
581 if (ffm->header[1] & FLAG_KEY_FRAME)
582 pkt->flags |= PKT_FLAG_KEY;
583
584 ffm->read_state = READ_HEADER;
585 if (ffm_read_data(s, pkt->data, size, 0) != size) {
586 /* bad case: desynchronized packet. we cancel all the packet loading */
587 av_free_packet(pkt);
588 return -EAGAIN;
589 }
590 pkt->pts = ffm->pts;
591 pkt->duration = duration;
592 break;
593 }
594 return 0;
595 }
596
597 //#define DEBUG_SEEK
598
599 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
600 by the write position inside this function */
601 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
602 {
603 FFMContext *ffm = s->priv_data;
604 ByteIOContext *pb = &s->pb;
605 offset_t pos;
606
607 pos = pos1 + ffm->write_index;
608 if (pos >= ffm->file_size)
609 pos -= (ffm->file_size - FFM_PACKET_SIZE);
610 #ifdef DEBUG_SEEK
611 printf("seek to %Lx -> %Lx\n", pos1, pos);
612 #endif
613 url_fseek(pb, pos, SEEK_SET);
614 }
615
616 static int64_t get_pts(AVFormatContext *s, offset_t pos)
617 {
618 ByteIOContext *pb = &s->pb;
619 int64_t pts;
620
621 ffm_seek1(s, pos);
622 url_fskip(pb, 4);
623 pts = get_be64(pb);
624 #ifdef DEBUG_SEEK
625 printf("pts=%0.6f\n", pts / 1000000.0);
626 #endif
627 return pts;
628 }
629
630 /* seek to a given time in the file. The file read pointer is
631 positionned at or before pts. XXX: the following code is quite
632 approximative */
633 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
634 {
635 FFMContext *ffm = s->priv_data;
636 offset_t pos_min, pos_max, pos;
637 int64_t pts_min, pts_max, pts;
638 double pos1;
639
640 #ifdef DEBUG_SEEK
641 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
642 #endif
643 /* find the position using linear interpolation (better than
644 dichotomy in typical cases) */
645 pos_min = 0;
646 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
647 while (pos_min <= pos_max) {
648 pts_min = get_pts(s, pos_min);
649 pts_max = get_pts(s, pos_max);
650 /* linear interpolation */
651 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
652 (double)(pts_max - pts_min);
653 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
654 if (pos <= pos_min)
655 pos = pos_min;
656 else if (pos >= pos_max)
657 pos = pos_max;
658 pts = get_pts(s, pos);
659 /* check if we are lucky */
660 if (pts == wanted_pts) {
661 goto found;
662 } else if (pts > wanted_pts) {
663 pos_max = pos - FFM_PACKET_SIZE;
664 } else {
665 pos_min = pos + FFM_PACKET_SIZE;
666 }
667 }
668 pos = pos_min;
669 if (pos > 0)
670 pos -= FFM_PACKET_SIZE;
671 found:
672 ffm_seek1(s, pos);
673 return 0;
674 }
675
676 offset_t ffm_read_write_index(int fd)
677 {
678 uint8_t buf[8];
679 offset_t pos;
680 int i;
681
682 lseek(fd, 8, SEEK_SET);
683 read(fd, buf, 8);
684 pos = 0;
685 for(i=0;i<8;i++)
686 pos |= (int64_t)buf[i] << (56 - i * 8);
687 return pos;
688 }
689
690 void ffm_write_write_index(int fd, offset_t pos)
691 {
692 uint8_t buf[8];
693 int i;
694
695 for(i=0;i<8;i++)
696 buf[i] = (pos >> (56 - i * 8)) & 0xff;
697 lseek(fd, 8, SEEK_SET);
698 write(fd, buf, 8);
699 }
700
701 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
702 {
703 FFMContext *ffm = s->priv_data;
704 ffm->write_index = pos;
705 ffm->file_size = file_size;
706 }
707
708 static int ffm_read_close(AVFormatContext *s)
709 {
710 AVStream *st;
711 int i;
712
713 for(i=0;i<s->nb_streams;i++) {
714 st = s->streams[i];
715 av_freep(&st->priv_data);
716 }
717 return 0;
718 }
719
720 static int ffm_probe(AVProbeData *p)
721 {
722 if (p->buf_size >= 4 &&
723 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
724 p->buf[3] == '1')
725 return AVPROBE_SCORE_MAX + 1;
726 return 0;
727 }
728
729 static AVInputFormat ffm_iformat = {
730 "ffm",
731 "ffm format",
732 sizeof(FFMContext),
733 ffm_probe,
734 ffm_read_header,
735 ffm_read_packet,
736 ffm_read_close,
737 ffm_seek,
738 };
739
740 #ifdef CONFIG_ENCODERS
741 static AVOutputFormat ffm_oformat = {
742 "ffm",
743 "ffm format",
744 "",
745 "ffm",
746 sizeof(FFMContext),
747 /* not really used */
748 CODEC_ID_MP2,
749 CODEC_ID_MPEG1VIDEO,
750 ffm_write_header,
751 ffm_write_packet,
752 ffm_write_trailer,
753 };
754 #endif //CONFIG_ENCODERS
755
756 int ffm_init(void)
757 {
758 av_register_input_format(&ffm_iformat);
759 #ifdef CONFIG_ENCODERS
760 av_register_output_format(&ffm_oformat);
761 #endif //CONFIG_ENCODERS
762 return 0;
763 }