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