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