FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2200
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:669
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PACKET_ID
Definition: ffm.h:32
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:281
int64_t dts
Definition: ffm.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:281
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2029
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
Definition: avformat.h:1130
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2919
int mpeg_quant
0-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1538
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2668
enum AVCodecID id
Definition: mxfenc.c:95
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2261
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1185
#define AV_RB64
Definition: intreadwrite.h:164
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3997
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2880
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1501
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int num
numerator
Definition: rational.h:44
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:1302
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:203
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
Definition: ffmdec.c:247
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:276
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static AVPacket pkt
#define FLAG_DTS
Definition: ffm.h:37
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:666
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2427
AVCodec.
Definition: avcodec.h:3173
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmdec.c:641
static int ffm_read_data(AVFormatContext *s, uint8_t *buf, int size, int header)
Definition: ffmdec.c:80
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1554
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:1800
#define FFM_HEADER_SIZE
Definition: ffm.h:30
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1367
static int ffm_resync(AVFormatContext *s, int state)
Definition: ffmdec.c:66
Format I/O context.
Definition: avformat.h:1226
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int64_t file_size
Definition: ffm.h:46
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1311
attribute_deprecated float rc_buffer_aggressivity
Definition: avcodec.h:2339
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
attribute_deprecated const char * rc_eq
Definition: avcodec.h:2317
uint8_t
static int nb_streams
Definition: ffprobe.c:216
#define av_malloc(s)
static int64_t get_dts(AVFormatContext *s, int64_t pos)
Definition: ffmdec.c:171
AVOptions.
#define FRAME_HEADER_SIZE
Definition: cpia.c:30
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:1733
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:681
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:756
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:1510
uint8_t * packet_end
Definition: ffm.h:55
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3663
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1628
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1294
static int ffm_is_avail_data(AVFormatContext *s, int size)
Definition: ffmdec.c:35
int coder_type
coder type
Definition: avcodec.h:2378
uint8_t * data
Definition: avcodec.h:1160
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:185
uint32_t tag
Definition: movenc.c:1332
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:748
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:273
static const uint8_t header[24]
Definition: sdr2.c:67
static int64_t duration
Definition: ffplay.c:320
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1178
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:537
static int ffm2_read_header(AVFormatContext *s)
Definition: ffmdec.c:268
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1206
Definition: ffm.h:41
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
uint8_t * packet_ptr
Definition: ffm.h:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
static void adjust_write_index(AVFormatContext *s)
Definition: ffmdec.c:183
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:650
#define AVERROR(e)
Definition: error.h:43
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:828
int qmax
maximum quantizer
Definition: avcodec.h:2275
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1333
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2325
simple assert() macros that are a bit more flexible than ISO C assert().
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
Definition: avcodec.h:1547
static int ffm_probe(AVProbeData *p)
Definition: ffmdec.c:765
#define FFMAX(a, b)
Definition: common.h:79
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1166
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2877
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:528
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:826
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2302
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:1828
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:415
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1282
int refs
number of reference frames
Definition: avcodec.h:1899
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1303
#define FFMIN(a, b)
Definition: common.h:81
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
int priv_data_size
Definition: avcodec.h:3211
int b_frame_strategy
Definition: avcodec.h:1516
uint8_t header[FRAME_HEADER_SIZE+4]
Definition: ffm.h:48
int mb_decision
macroblock decision mode
Definition: avcodec.h:1775
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:193
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2282
int packet_size
Definition: ffm.h:52
int buffer_size
Maximum buffer size.
Definition: avio.h:83
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2751
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:629
Stream structure.
Definition: avformat.h:807
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2003
static int ffm_close(AVFormatContext *s)
Definition: ffmdec.c:237
enum AVMediaType codec_type
Definition: avcodec.h:1247
enum AVCodecID codec_id
Definition: avcodec.h:1256
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
int sample_rate
samples per second
Definition: avcodec.h:1983
AVIOContext * pb
I/O context.
Definition: avformat.h:1268
int debug
debug
Definition: avcodec.h:2563
Definition: ffm.h:44
main external API structure.
Definition: avcodec.h:1239
int qmin
minimum quantizer
Definition: avcodec.h:2268
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1271
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:578
void * buf
Definition: avisynth_c.h:595
static int ffm_read_header(AVFormatContext *s)
Definition: ffmdec.c:489
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1523
This structure contains the data a format has to probe a file.
Definition: avformat.h:413
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2260
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:558
static int64_t pts
Global timestamp for the audio frames.
static uint32_t state
Definition: trasher.c:27
static int flags
Definition: cpu.c:47
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3201
AVInputFormat ff_ffm_demuxer
Definition: ffmdec.c:774
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
Definition: ffmdec.c:159
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1433
enum AVMediaType type
Definition: avcodec.h:560
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:425
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:634
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:2826
int64_t pos
position in the file of the current buffer
Definition: avio.h:94
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
Definition: ffmdec.c:703
int den
denominator
Definition: rational.h:45
#define MKBETAG(a, b, c, d)
Definition: common.h:320
int eof_reached
true if eof reached
Definition: avio.h:96
int len
int channels
number of audio channels
Definition: avcodec.h:1984
void * priv_data
Format private data.
Definition: avformat.h:1254
int read_state
Definition: ffm.h:47
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1340
int64_t write_index
Definition: ffm.h:46
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1159
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:593
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:706
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:300
int me_method
Motion estimation algorithm used for video coding.
Definition: avcodec.h:1451
int stream_index
Definition: avcodec.h:1162
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2332
#define MKTAG(a, b, c, d)
Definition: common.h:319
This structure stores compressed data.
Definition: avcodec.h:1137
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:1704
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2541
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1153
GLuint buffer
Definition: opengl_enc.c:102
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size
Definition: avcodec.h:1892