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