]> git.sesse.net Git - ffmpeg/blob - libavformat/ffmdec.c
Merge commit '16158da9607f2f84232d3dd381406b2f2449ec74'
[ffmpeg] / libavformat / ffmdec.c
1 /*
2  * FFM (ffserver live feed) demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdint.h>
23
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/intfloat.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "ffm.h"
30 #include "avio_internal.h"
31
32 static int ffm_is_avail_data(AVFormatContext *s, int size)
33 {
34     FFMContext *ffm = s->priv_data;
35     int64_t pos, avail_size;
36     int len;
37
38     len = ffm->packet_end - ffm->packet_ptr;
39     if (size <= len)
40         return 1;
41     pos = avio_tell(s->pb);
42     if (!ffm->write_index) {
43         if (pos == ffm->file_size)
44             return AVERROR_EOF;
45         avail_size = ffm->file_size - pos;
46     } else {
47     if (pos == ffm->write_index) {
48         /* exactly at the end of stream */
49         return AVERROR(EAGAIN);
50     } else if (pos < ffm->write_index) {
51         avail_size = ffm->write_index - pos;
52     } else {
53         avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
54     }
55     }
56     avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
57     if (size <= avail_size)
58         return 1;
59     else
60         return AVERROR(EAGAIN);
61 }
62
63 static int ffm_resync(AVFormatContext *s, int state)
64 {
65     av_log(s, AV_LOG_ERROR, "resyncing\n");
66     while (state != PACKET_ID) {
67         if (avio_feof(s->pb)) {
68             av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
69             return -1;
70         }
71         state = (state << 8) | avio_r8(s->pb);
72     }
73     return 0;
74 }
75
76 /* first is true if we read the frame header */
77 static int ffm_read_data(AVFormatContext *s,
78                          uint8_t *buf, int size, int header)
79 {
80     FFMContext *ffm = s->priv_data;
81     AVIOContext *pb = s->pb;
82     int len, fill_size, size1, frame_offset, id;
83
84     size1 = size;
85     while (size > 0) {
86     redo:
87         len = ffm->packet_end - ffm->packet_ptr;
88         if (len < 0)
89             return -1;
90         if (len > size)
91             len = size;
92         if (len == 0) {
93             if (avio_tell(pb) == ffm->file_size)
94                 avio_seek(pb, ffm->packet_size, SEEK_SET);
95     retry_read:
96             if (pb->buffer_size != ffm->packet_size) {
97                 int64_t tell = avio_tell(pb);
98                 ffio_set_buf_size(pb, ffm->packet_size);
99                 avio_seek(pb, tell, SEEK_SET);
100             }
101             id = avio_rb16(pb); /* PACKET_ID */
102             if (id != PACKET_ID)
103                 if (ffm_resync(s, id) < 0)
104                     return -1;
105             fill_size = avio_rb16(pb);
106             ffm->dts = avio_rb64(pb);
107             frame_offset = avio_rb16(pb);
108             avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
109             ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
110             if (ffm->packet_end < ffm->packet || frame_offset < 0)
111                 return -1;
112             /* if first packet or resynchronization packet, we must
113                handle it specifically */
114             if (ffm->first_packet || (frame_offset & 0x8000)) {
115                 if (!frame_offset) {
116                     /* This packet has no frame headers in it */
117                     if (avio_tell(pb) >= ffm->packet_size * 3LL) {
118                         avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR);
119                         goto retry_read;
120                     }
121                     /* This is bad, we cannot find a valid frame header */
122                     return 0;
123                 }
124                 ffm->first_packet = 0;
125                 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
126                     return -1;
127                 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
128                 if (!header)
129                     break;
130             } else {
131                 ffm->packet_ptr = ffm->packet;
132             }
133             goto redo;
134         }
135         memcpy(buf, ffm->packet_ptr, len);
136         buf += len;
137         ffm->packet_ptr += len;
138         size -= len;
139         header = 0;
140     }
141     return size1 - size;
142 }
143
144 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
145    and file_size - FFM_PACKET_SIZE */
146 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
147 {
148     FFMContext *ffm = s->priv_data;
149     AVIOContext *pb = s->pb;
150     int64_t pos;
151
152     pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
153     pos = FFMAX(pos, FFM_PACKET_SIZE);
154     av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
155     return avio_seek(pb, pos, SEEK_SET);
156 }
157
158 static int64_t get_dts(AVFormatContext *s, int64_t pos)
159 {
160     AVIOContext *pb = s->pb;
161     int64_t dts;
162
163     ffm_seek1(s, pos);
164     avio_skip(pb, 4);
165     dts = avio_rb64(pb);
166     av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
167     return dts;
168 }
169
170 static void adjust_write_index(AVFormatContext *s)
171 {
172     FFMContext *ffm = s->priv_data;
173     AVIOContext *pb = s->pb;
174     int64_t pts;
175     //int64_t orig_write_index = ffm->write_index;
176     int64_t pos_min, pos_max;
177     int64_t pts_start;
178     int64_t ptr = avio_tell(pb);
179
180
181     pos_min = 0;
182     pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
183
184     pts_start = get_dts(s, pos_min);
185
186     pts = get_dts(s, pos_max);
187
188     if (pts - 100000 > pts_start)
189         goto end;
190
191     ffm->write_index = FFM_PACKET_SIZE;
192
193     pts_start = get_dts(s, pos_min);
194
195     pts = get_dts(s, pos_max);
196
197     if (pts - 100000 <= pts_start) {
198         while (1) {
199             int64_t newpos;
200             int64_t newpts;
201
202             newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
203
204             if (newpos == pos_min)
205                 break;
206
207             newpts = get_dts(s, newpos);
208
209             if (newpts - 100000 <= pts) {
210                 pos_max = newpos;
211                 pts = newpts;
212             } else {
213                 pos_min = newpos;
214             }
215         }
216         ffm->write_index += pos_max;
217     }
218
219  end:
220     avio_seek(pb, ptr, SEEK_SET);
221 }
222
223
224 static int ffm_close(AVFormatContext *s)
225 {
226     int i;
227
228     for (i = 0; i < s->nb_streams; i++)
229         av_freep(&s->streams[i]->codec->rc_eq);
230
231     return 0;
232 }
233
234 static int ffm2_read_header(AVFormatContext *s)
235 {
236     FFMContext *ffm = s->priv_data;
237     AVStream *st;
238     AVIOContext *pb = s->pb;
239     AVCodecContext *codec;
240     int ret;
241     int f_main = 0, f_cprv, f_stvi, f_stau;
242     AVCodec *enc;
243     char *buffer;
244
245     ffm->packet_size = avio_rb32(pb);
246     if (ffm->packet_size != FFM_PACKET_SIZE) {
247         av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
248                ffm->packet_size, FFM_PACKET_SIZE);
249         ret = AVERROR_INVALIDDATA;
250         goto fail;
251     }
252
253     ffm->write_index = avio_rb64(pb);
254     /* get also filesize */
255     if (pb->seekable) {
256         ffm->file_size = avio_size(pb);
257         if (ffm->write_index && 0)
258             adjust_write_index(s);
259     } else {
260         ffm->file_size = (UINT64_C(1) << 63) - 1;
261     }
262
263     while(!avio_feof(pb)) {
264         unsigned id = avio_rb32(pb);
265         unsigned size = avio_rb32(pb);
266         int64_t next = avio_tell(pb) + size;
267         char rc_eq_buf[128];
268
269         if(!id)
270             break;
271
272         switch(id) {
273         case MKBETAG('M', 'A', 'I', 'N'):
274             if (f_main++) {
275                 ret = AVERROR(EINVAL);
276                 goto fail;
277             }
278             avio_rb32(pb); /* nb_streams */
279             avio_rb32(pb); /* total bitrate */
280             break;
281         case MKBETAG('C', 'O', 'M', 'M'):
282             f_cprv = f_stvi = f_stau = 0;
283             st = avformat_new_stream(s, NULL);
284             if (!st) {
285                 ret = AVERROR(ENOMEM);
286                 goto fail;
287             }
288
289             avpriv_set_pts_info(st, 64, 1, 1000000);
290
291             codec = st->codec;
292             /* generic info */
293             codec->codec_id = avio_rb32(pb);
294             codec->codec_type = avio_r8(pb);
295             codec->bit_rate = avio_rb32(pb);
296             codec->flags = avio_rb32(pb);
297             codec->flags2 = avio_rb32(pb);
298             codec->debug = avio_rb32(pb);
299             if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
300                 if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
301                     return AVERROR(ENOMEM);
302             }
303             break;
304         case MKBETAG('S', 'T', 'V', 'I'):
305             if (f_stvi++) {
306                 ret = AVERROR(EINVAL);
307                 goto fail;
308             }
309             codec->time_base.num = avio_rb32(pb);
310             codec->time_base.den = avio_rb32(pb);
311             codec->width = avio_rb16(pb);
312             codec->height = avio_rb16(pb);
313             codec->gop_size = avio_rb16(pb);
314             codec->pix_fmt = avio_rb32(pb);
315             codec->qmin = avio_r8(pb);
316             codec->qmax = avio_r8(pb);
317             codec->max_qdiff = avio_r8(pb);
318             codec->qcompress = avio_rb16(pb) / 10000.0;
319             codec->qblur = avio_rb16(pb) / 10000.0;
320             codec->bit_rate_tolerance = avio_rb32(pb);
321             avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
322             codec->rc_eq = av_strdup(rc_eq_buf);
323             codec->rc_max_rate = avio_rb32(pb);
324             codec->rc_min_rate = avio_rb32(pb);
325             codec->rc_buffer_size = avio_rb32(pb);
326             codec->i_quant_factor = av_int2double(avio_rb64(pb));
327             codec->b_quant_factor = av_int2double(avio_rb64(pb));
328             codec->i_quant_offset = av_int2double(avio_rb64(pb));
329             codec->b_quant_offset = av_int2double(avio_rb64(pb));
330             codec->dct_algo = avio_rb32(pb);
331             codec->strict_std_compliance = avio_rb32(pb);
332             codec->max_b_frames = avio_rb32(pb);
333             codec->mpeg_quant = avio_rb32(pb);
334             codec->intra_dc_precision = avio_rb32(pb);
335             codec->me_method = avio_rb32(pb);
336             codec->mb_decision = avio_rb32(pb);
337             codec->nsse_weight = avio_rb32(pb);
338             codec->frame_skip_cmp = avio_rb32(pb);
339             codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
340             codec->codec_tag = avio_rb32(pb);
341             codec->thread_count = avio_r8(pb);
342             codec->coder_type = avio_rb32(pb);
343             codec->me_cmp = avio_rb32(pb);
344             codec->me_subpel_quality = avio_rb32(pb);
345             codec->me_range = avio_rb32(pb);
346             codec->keyint_min = avio_rb32(pb);
347             codec->scenechange_threshold = avio_rb32(pb);
348             codec->b_frame_strategy = avio_rb32(pb);
349             codec->qcompress = av_int2double(avio_rb64(pb));
350             codec->qblur = av_int2double(avio_rb64(pb));
351             codec->max_qdiff = avio_rb32(pb);
352             codec->refs = avio_rb32(pb);
353             break;
354         case MKBETAG('S', 'T', 'A', 'U'):
355             if (f_stau++) {
356                 ret = AVERROR(EINVAL);
357                 goto fail;
358             }
359             codec->sample_rate = avio_rb32(pb);
360             codec->channels = avio_rl16(pb);
361             codec->frame_size = avio_rl16(pb);
362             break;
363         case MKBETAG('C', 'P', 'R', 'V'):
364             if (f_cprv++) {
365                 ret = AVERROR(EINVAL);
366                 goto fail;
367             }
368             enc = avcodec_find_encoder(codec->codec_id);
369             if (enc && enc->priv_data_size && enc->priv_class) {
370                 st->recommended_encoder_configuration = av_malloc(size + 1);
371                 if (!st->recommended_encoder_configuration) {
372                     ret = AVERROR(ENOMEM);
373                     goto fail;
374                 }
375                 avio_get_str(pb, size, st->recommended_encoder_configuration, size + 1);
376             }
377             break;
378         case MKBETAG('S', '2', 'V', 'I'):
379             if (f_stvi++) {
380                 ret = AVERROR(EINVAL);
381                 goto fail;
382             }
383             buffer = av_malloc(size);
384             if (!buffer) {
385                 ret = AVERROR(ENOMEM);
386                 goto fail;
387             }
388             avio_get_str(pb, INT_MAX, buffer, size);
389             av_set_options_string(codec, buffer, "=", ",");
390             av_freep(&buffer);
391             break;
392         case MKBETAG('S', '2', 'A', 'U'):
393             if (f_stau++) {
394                 ret = AVERROR(EINVAL);
395                 goto fail;
396             }
397             buffer = av_malloc(size);
398             if (!buffer) {
399                 ret = AVERROR(ENOMEM);
400                 goto fail;
401             }
402             avio_get_str(pb, INT_MAX, buffer, size);
403             av_set_options_string(codec, buffer, "=", ",");
404             av_freep(&buffer);
405             break;
406         }
407         avio_seek(pb, next, SEEK_SET);
408     }
409
410     /* get until end of block reached */
411     while ((avio_tell(pb) % ffm->packet_size) != 0)
412         avio_r8(pb);
413
414     /* init packet demux */
415     ffm->packet_ptr = ffm->packet;
416     ffm->packet_end = ffm->packet;
417     ffm->frame_offset = 0;
418     ffm->dts = 0;
419     ffm->read_state = READ_HEADER;
420     ffm->first_packet = 1;
421     return 0;
422  fail:
423     ffm_close(s);
424     return ret;
425 }
426
427 static int ffm_read_header(AVFormatContext *s)
428 {
429     FFMContext *ffm = s->priv_data;
430     AVStream *st;
431     AVIOContext *pb = s->pb;
432     AVCodecContext *codec;
433     int i, nb_streams;
434     uint32_t tag;
435
436     /* header */
437     tag = avio_rl32(pb);
438     if (tag == MKTAG('F', 'F', 'M', '2'))
439         return ffm2_read_header(s);
440     if (tag != MKTAG('F', 'F', 'M', '1'))
441         goto fail;
442     ffm->packet_size = avio_rb32(pb);
443     if (ffm->packet_size != FFM_PACKET_SIZE)
444         goto fail;
445     ffm->write_index = avio_rb64(pb);
446     /* get also filesize */
447     if (pb->seekable) {
448         ffm->file_size = avio_size(pb);
449         if (ffm->write_index && 0)
450             adjust_write_index(s);
451     } else {
452         ffm->file_size = (UINT64_C(1) << 63) - 1;
453     }
454
455     nb_streams = avio_rb32(pb);
456     avio_rb32(pb); /* total bitrate */
457     /* read each stream */
458     for(i=0;i<nb_streams;i++) {
459         char rc_eq_buf[128];
460
461         st = avformat_new_stream(s, NULL);
462         if (!st)
463             goto fail;
464
465         avpriv_set_pts_info(st, 64, 1, 1000000);
466
467         codec = st->codec;
468         /* generic info */
469         codec->codec_id = avio_rb32(pb);
470         codec->codec_type = avio_r8(pb); /* codec_type */
471         codec->bit_rate = avio_rb32(pb);
472         codec->flags = avio_rb32(pb);
473         codec->flags2 = avio_rb32(pb);
474         codec->debug = avio_rb32(pb);
475         /* specific info */
476         switch(codec->codec_type) {
477         case AVMEDIA_TYPE_VIDEO:
478             codec->time_base.num = avio_rb32(pb);
479             codec->time_base.den = avio_rb32(pb);
480             codec->width = avio_rb16(pb);
481             codec->height = avio_rb16(pb);
482             codec->gop_size = avio_rb16(pb);
483             codec->pix_fmt = avio_rb32(pb);
484             codec->qmin = avio_r8(pb);
485             codec->qmax = avio_r8(pb);
486             codec->max_qdiff = avio_r8(pb);
487             codec->qcompress = avio_rb16(pb) / 10000.0;
488             codec->qblur = avio_rb16(pb) / 10000.0;
489             codec->bit_rate_tolerance = avio_rb32(pb);
490             avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
491             codec->rc_eq = av_strdup(rc_eq_buf);
492             codec->rc_max_rate = avio_rb32(pb);
493             codec->rc_min_rate = avio_rb32(pb);
494             codec->rc_buffer_size = avio_rb32(pb);
495             codec->i_quant_factor = av_int2double(avio_rb64(pb));
496             codec->b_quant_factor = av_int2double(avio_rb64(pb));
497             codec->i_quant_offset = av_int2double(avio_rb64(pb));
498             codec->b_quant_offset = av_int2double(avio_rb64(pb));
499             codec->dct_algo = avio_rb32(pb);
500             codec->strict_std_compliance = avio_rb32(pb);
501             codec->max_b_frames = avio_rb32(pb);
502             codec->mpeg_quant = avio_rb32(pb);
503             codec->intra_dc_precision = avio_rb32(pb);
504             codec->me_method = avio_rb32(pb);
505             codec->mb_decision = avio_rb32(pb);
506             codec->nsse_weight = avio_rb32(pb);
507             codec->frame_skip_cmp = avio_rb32(pb);
508             codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
509             codec->codec_tag = avio_rb32(pb);
510             codec->thread_count = avio_r8(pb);
511             codec->coder_type = avio_rb32(pb);
512             codec->me_cmp = avio_rb32(pb);
513             codec->me_subpel_quality = avio_rb32(pb);
514             codec->me_range = avio_rb32(pb);
515             codec->keyint_min = avio_rb32(pb);
516             codec->scenechange_threshold = avio_rb32(pb);
517             codec->b_frame_strategy = avio_rb32(pb);
518             codec->qcompress = av_int2double(avio_rb64(pb));
519             codec->qblur = av_int2double(avio_rb64(pb));
520             codec->max_qdiff = avio_rb32(pb);
521             codec->refs = avio_rb32(pb);
522             break;
523         case AVMEDIA_TYPE_AUDIO:
524             codec->sample_rate = avio_rb32(pb);
525             codec->channels = avio_rl16(pb);
526             codec->frame_size = avio_rl16(pb);
527             break;
528         default:
529             goto fail;
530         }
531         if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
532             if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
533                 return AVERROR(ENOMEM);
534         }
535     }
536
537     /* get until end of block reached */
538     while ((avio_tell(pb) % ffm->packet_size) != 0)
539         avio_r8(pb);
540
541     /* init packet demux */
542     ffm->packet_ptr = ffm->packet;
543     ffm->packet_end = ffm->packet;
544     ffm->frame_offset = 0;
545     ffm->dts = 0;
546     ffm->read_state = READ_HEADER;
547     ffm->first_packet = 1;
548     return 0;
549  fail:
550     ffm_close(s);
551     return -1;
552 }
553
554 /* return < 0 if eof */
555 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
556 {
557     int size;
558     FFMContext *ffm = s->priv_data;
559     int duration, ret;
560
561     switch(ffm->read_state) {
562     case READ_HEADER:
563         if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
564             return ret;
565
566         av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
567                avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
568         if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
569             FRAME_HEADER_SIZE)
570             return -1;
571         if (ffm->header[1] & FLAG_DTS)
572             if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
573                 return -1;
574         ffm->read_state = READ_DATA;
575         /* fall through */
576     case READ_DATA:
577         size = AV_RB24(ffm->header + 2);
578         if ((ret = ffm_is_avail_data(s, size)) < 0)
579             return ret;
580
581         duration = AV_RB24(ffm->header + 5);
582
583         if (av_new_packet(pkt, size) < 0) {
584             return AVERROR(ENOMEM);
585         }
586         pkt->stream_index = ffm->header[0];
587         if ((unsigned)pkt->stream_index >= s->nb_streams) {
588             av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
589             av_free_packet(pkt);
590             ffm->read_state = READ_HEADER;
591             return -1;
592         }
593         pkt->pos = avio_tell(s->pb);
594         if (ffm->header[1] & FLAG_KEY_FRAME)
595             pkt->flags |= AV_PKT_FLAG_KEY;
596
597         ffm->read_state = READ_HEADER;
598         if (ffm_read_data(s, pkt->data, size, 0) != size) {
599             /* bad case: desynchronized packet. we cancel all the packet loading */
600             av_free_packet(pkt);
601             return -1;
602         }
603         pkt->pts = AV_RB64(ffm->header+8);
604         if (ffm->header[1] & FLAG_DTS)
605             pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
606         else
607             pkt->dts = pkt->pts;
608         pkt->duration = duration;
609         break;
610     }
611     return 0;
612 }
613
614 /* seek to a given time in the file. The file read pointer is
615    positioned at or before pts. XXX: the following code is quite
616    approximative */
617 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
618 {
619     FFMContext *ffm = s->priv_data;
620     int64_t pos_min, pos_max, pos;
621     int64_t pts_min, pts_max, pts;
622     double pos1;
623
624     av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
625     /* find the position using linear interpolation (better than
626        dichotomy in typical cases) */
627     if (ffm->write_index && ffm->write_index < ffm->file_size) {
628         if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
629             pos_min = FFM_PACKET_SIZE;
630             pos_max = ffm->write_index - FFM_PACKET_SIZE;
631         } else {
632             pos_min = ffm->write_index;
633             pos_max = ffm->file_size - FFM_PACKET_SIZE;
634         }
635     } else {
636         pos_min = FFM_PACKET_SIZE;
637         pos_max = ffm->file_size - FFM_PACKET_SIZE;
638     }
639     while (pos_min <= pos_max) {
640         pts_min = get_dts(s, pos_min);
641         pts_max = get_dts(s, pos_max);
642         if (pts_min > wanted_pts || pts_max <= wanted_pts) {
643             pos = pts_min > wanted_pts ? pos_min : pos_max;
644             goto found;
645         }
646         /* linear interpolation */
647         pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
648             (double)(pts_max - pts_min);
649         pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
650         if (pos <= pos_min)
651             pos = pos_min;
652         else if (pos >= pos_max)
653             pos = pos_max;
654         pts = get_dts(s, pos);
655         /* check if we are lucky */
656         if (pts == wanted_pts) {
657             goto found;
658         } else if (pts > wanted_pts) {
659             pos_max = pos - FFM_PACKET_SIZE;
660         } else {
661             pos_min = pos + FFM_PACKET_SIZE;
662         }
663     }
664     pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
665
666  found:
667     if (ffm_seek1(s, pos) < 0)
668         return -1;
669
670     /* reset read state */
671     ffm->read_state = READ_HEADER;
672     ffm->packet_ptr = ffm->packet;
673     ffm->packet_end = ffm->packet;
674     ffm->first_packet = 1;
675
676     return 0;
677 }
678
679 static int ffm_probe(AVProbeData *p)
680 {
681     if (
682         p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
683         (p->buf[3] == '1' || p->buf[3] == '2'))
684         return AVPROBE_SCORE_MAX + 1;
685     return 0;
686 }
687
688 AVInputFormat ff_ffm_demuxer = {
689     .name           = "ffm",
690     .long_name      = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
691     .priv_data_size = sizeof(FFMContext),
692     .read_probe     = ffm_probe,
693     .read_header    = ffm_read_header,
694     .read_packet    = ffm_read_packet,
695     .read_close     = ffm_close,
696     .read_seek      = ffm_seek,
697 };