FFmpeg  2.8.15
 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/internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/intfloat.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/pixdesc.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "ffm.h"
34 #include "avio_internal.h"
35 
37 {
38  FFMContext *ffm = s->priv_data;
39  int64_t pos, avail_size;
40  int len;
41 
42  len = ffm->packet_end - ffm->packet_ptr;
43  if (size <= len)
44  return 1;
45  pos = avio_tell(s->pb);
46  if (!ffm->write_index) {
47  if (pos == ffm->file_size)
48  return AVERROR_EOF;
49  avail_size = ffm->file_size - pos;
50  } else {
51  if (pos == ffm->write_index) {
52  /* exactly at the end of stream */
53  return AVERROR(EAGAIN);
54  } else if (pos < ffm->write_index) {
55  avail_size = ffm->write_index - pos;
56  } else {
57  avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
58  }
59  }
60  avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
61  if (size <= avail_size)
62  return 1;
63  else
64  return AVERROR(EAGAIN);
65 }
66 
67 static int ffm_resync(AVFormatContext *s, int state)
68 {
69  av_log(s, AV_LOG_ERROR, "resyncing\n");
70  while (state != PACKET_ID) {
71  if (avio_feof(s->pb)) {
72  av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
73  return -1;
74  }
75  state = (state << 8) | avio_r8(s->pb);
76  }
77  return 0;
78 }
79 
80 /* first is true if we read the frame header */
82  uint8_t *buf, int size, int header)
83 {
84  FFMContext *ffm = s->priv_data;
85  AVIOContext *pb = s->pb;
86  int len, fill_size, size1, frame_offset, id;
87  int64_t last_pos = -1;
88 
89  size1 = size;
90  while (size > 0) {
91  redo:
92  len = ffm->packet_end - ffm->packet_ptr;
93  if (len < 0)
94  return -1;
95  if (len > size)
96  len = size;
97  if (len == 0) {
98  if (avio_tell(pb) == ffm->file_size)
99  avio_seek(pb, ffm->packet_size, SEEK_SET);
100  retry_read:
101  if (pb->buffer_size != ffm->packet_size) {
102  int64_t tell = avio_tell(pb);
103  int ret = ffio_set_buf_size(pb, ffm->packet_size);
104  if (ret < 0)
105  return ret;
106  avio_seek(pb, tell, SEEK_SET);
107  }
108  id = avio_rb16(pb); /* PACKET_ID */
109  if (id != PACKET_ID) {
110  if (ffm_resync(s, id) < 0)
111  return -1;
112  last_pos = avio_tell(pb);
113  }
114  fill_size = avio_rb16(pb);
115  ffm->dts = avio_rb64(pb);
116  frame_offset = avio_rb16(pb);
117  avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
118  if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
119  return -1;
120  }
121  ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
122  /* if first packet or resynchronization packet, we must
123  handle it specifically */
124  if (ffm->first_packet || (frame_offset & 0x8000)) {
125  if (!frame_offset) {
126  /* This packet has no frame headers in it */
127  if (avio_tell(pb) >= ffm->packet_size * 3LL) {
128  int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
129  seekback = FFMAX(seekback, 0);
130  avio_seek(pb, -seekback, SEEK_CUR);
131  goto retry_read;
132  }
133  /* This is bad, we cannot find a valid frame header */
134  return 0;
135  }
136  ffm->first_packet = 0;
137  if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
138  ffm->packet_end = ffm->packet_ptr;
139  return -1;
140  }
141  ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
142  if (!header)
143  break;
144  } else {
145  ffm->packet_ptr = ffm->packet;
146  }
147  goto redo;
148  }
149  memcpy(buf, ffm->packet_ptr, len);
150  buf += len;
151  ffm->packet_ptr += len;
152  size -= len;
153  header = 0;
154  }
155  return size1 - size;
156 }
157 
158 /* ensure that actual seeking happens between FFM_PACKET_SIZE
159  and file_size - FFM_PACKET_SIZE */
160 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
161 {
162  FFMContext *ffm = s->priv_data;
163  AVIOContext *pb = s->pb;
164  int64_t pos;
165 
166  pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
167  pos = FFMAX(pos, FFM_PACKET_SIZE);
168  ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
169  return avio_seek(pb, pos, SEEK_SET);
170 }
171 
172 static int64_t get_dts(AVFormatContext *s, int64_t pos)
173 {
174  AVIOContext *pb = s->pb;
175  int64_t dts;
176 
177  ffm_seek1(s, pos);
178  avio_skip(pb, 4);
179  dts = avio_rb64(pb);
180  ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
181  return dts;
182 }
183 
185 {
186  FFMContext *ffm = s->priv_data;
187  AVIOContext *pb = s->pb;
188  int64_t pts;
189  //int64_t orig_write_index = ffm->write_index;
190  int64_t pos_min, pos_max;
191  int64_t pts_start;
192  int64_t ptr = avio_tell(pb);
193 
194 
195  pos_min = 0;
196  pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
197 
198  pts_start = get_dts(s, pos_min);
199 
200  pts = get_dts(s, pos_max);
201 
202  if (pts - 100000 > pts_start)
203  goto end;
204 
206 
207  pts_start = get_dts(s, pos_min);
208 
209  pts = get_dts(s, pos_max);
210 
211  if (pts - 100000 <= pts_start) {
212  while (1) {
213  int64_t newpos;
214  int64_t newpts;
215 
216  newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
217 
218  if (newpos == pos_min)
219  break;
220 
221  newpts = get_dts(s, newpos);
222 
223  if (newpts - 100000 <= pts) {
224  pos_max = newpos;
225  pts = newpts;
226  } else {
227  pos_min = newpos;
228  }
229  }
230  ffm->write_index += pos_max;
231  }
232 
233  end:
234  avio_seek(pb, ptr, SEEK_SET);
235 }
236 
237 
239 {
240  int i;
241 
242  for (i = 0; i < s->nb_streams; i++)
243  av_freep(&s->streams[i]->codec->rc_eq);
244 
245  return 0;
246 }
247 
248 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
249 {
250  int ret;
251  size_t newsize;
252  av_assert0(conf && st);
253  if (!*conf)
254  return 0;
257  *conf = 0;
258  return 0;
259  }
260  newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
261  if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
262  return ret;
264  av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
265  av_freep(conf);
266  return 0;
267 }
268 
270 {
271  FFMContext *ffm = s->priv_data;
272  AVStream *st;
273  AVIOContext *pb = s->pb;
274  AVCodecContext *codec;
275  const AVCodecDescriptor *codec_desc;
276  int ret;
277  int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
278  AVCodec *enc;
279  char *buffer;
280 
281  ffm->packet_size = avio_rb32(pb);
282  if (ffm->packet_size != FFM_PACKET_SIZE) {
283  av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
285  ret = AVERROR_INVALIDDATA;
286  goto fail;
287  }
288 
289  ffm->write_index = avio_rb64(pb);
290  /* get also filesize */
291  if (pb->seekable) {
292  ffm->file_size = avio_size(pb);
293  if (ffm->write_index && 0)
295  } else {
296  ffm->file_size = (UINT64_C(1) << 63) - 1;
297  }
298 
299  while(!avio_feof(pb)) {
300  unsigned id = avio_rb32(pb);
301  unsigned size = avio_rb32(pb);
302  int64_t next = avio_tell(pb) + size;
303  char rc_eq_buf[128];
304 
305  if(!id)
306  break;
307 
308  switch(id) {
309  case MKBETAG('M', 'A', 'I', 'N'):
310  if (f_main++) {
311  ret = AVERROR(EINVAL);
312  goto fail;
313  }
314  avio_rb32(pb); /* nb_streams */
315  avio_rb32(pb); /* total bitrate */
316  break;
317  case MKBETAG('C', 'O', 'M', 'M'):
318  f_cprv = f_stvi = f_stau = 0;
319  st = avformat_new_stream(s, NULL);
320  if (!st) {
321  ret = AVERROR(ENOMEM);
322  goto fail;
323  }
324 
325  avpriv_set_pts_info(st, 64, 1, 1000000);
326 
327  codec = st->codec;
328  /* generic info */
329  codec->codec_id = avio_rb32(pb);
330  codec_desc = avcodec_descriptor_get(codec->codec_id);
331  if (!codec_desc) {
332  av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
333  codec->codec_id = AV_CODEC_ID_NONE;
334  goto fail;
335  }
336  codec->codec_type = avio_r8(pb);
337  if (codec->codec_type != codec_desc->type) {
338  av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
339  codec_desc->type, codec->codec_type);
340  codec->codec_id = AV_CODEC_ID_NONE;
342  goto fail;
343  }
344  codec->bit_rate = avio_rb32(pb);
345  codec->flags = avio_rb32(pb);
346  codec->flags2 = avio_rb32(pb);
347  codec->debug = avio_rb32(pb);
348  if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
349  if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
350  return AVERROR(ENOMEM);
351  }
352  break;
353  case MKBETAG('S', 'T', 'V', 'I'):
354  if (f_stvi++ || codec->codec_type != AVMEDIA_TYPE_VIDEO) {
355  ret = AVERROR(EINVAL);
356  goto fail;
357  }
358  codec->time_base.num = avio_rb32(pb);
359  codec->time_base.den = avio_rb32(pb);
360  if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
361  av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
362  codec->time_base.num, codec->time_base.den);
363  ret = AVERROR_INVALIDDATA;
364  goto fail;
365  }
366  codec->width = avio_rb16(pb);
367  codec->height = avio_rb16(pb);
368  codec->gop_size = avio_rb16(pb);
369  codec->pix_fmt = avio_rb32(pb);
370  if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
371  av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
372  codec->pix_fmt = AV_PIX_FMT_NONE;
373  goto fail;
374  }
375  codec->qmin = avio_r8(pb);
376  codec->qmax = avio_r8(pb);
377  codec->max_qdiff = avio_r8(pb);
378  codec->qcompress = avio_rb16(pb) / 10000.0;
379  codec->qblur = avio_rb16(pb) / 10000.0;
380  codec->bit_rate_tolerance = avio_rb32(pb);
381  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
382  codec->rc_eq = av_strdup(rc_eq_buf);
383  codec->rc_max_rate = avio_rb32(pb);
384  codec->rc_min_rate = avio_rb32(pb);
385  codec->rc_buffer_size = avio_rb32(pb);
386  codec->i_quant_factor = av_int2double(avio_rb64(pb));
387  codec->b_quant_factor = av_int2double(avio_rb64(pb));
388  codec->i_quant_offset = av_int2double(avio_rb64(pb));
389  codec->b_quant_offset = av_int2double(avio_rb64(pb));
390  codec->dct_algo = avio_rb32(pb);
391  codec->strict_std_compliance = avio_rb32(pb);
392  codec->max_b_frames = avio_rb32(pb);
393  codec->mpeg_quant = avio_rb32(pb);
394  codec->intra_dc_precision = avio_rb32(pb);
395  codec->me_method = avio_rb32(pb);
396  codec->mb_decision = avio_rb32(pb);
397  codec->nsse_weight = avio_rb32(pb);
398  codec->frame_skip_cmp = avio_rb32(pb);
400  codec->codec_tag = avio_rb32(pb);
401  codec->thread_count = avio_r8(pb);
402  codec->coder_type = avio_rb32(pb);
403  codec->me_cmp = avio_rb32(pb);
404  codec->me_subpel_quality = avio_rb32(pb);
405  codec->me_range = avio_rb32(pb);
406  codec->keyint_min = avio_rb32(pb);
407  codec->scenechange_threshold = avio_rb32(pb);
408  codec->b_frame_strategy = avio_rb32(pb);
409  codec->qcompress = av_int2double(avio_rb64(pb));
410  codec->qblur = av_int2double(avio_rb64(pb));
411  codec->max_qdiff = avio_rb32(pb);
412  codec->refs = avio_rb32(pb);
413  break;
414  case MKBETAG('S', 'T', 'A', 'U'):
415  if (f_stau++ || codec->codec_type != AVMEDIA_TYPE_AUDIO) {
416  ret = AVERROR(EINVAL);
417  goto fail;
418  }
419  codec->sample_rate = avio_rb32(pb);
420  codec->channels = avio_rl16(pb);
421  codec->frame_size = avio_rl16(pb);
422  break;
423  case MKBETAG('C', 'P', 'R', 'V'):
424  if (f_cprv++) {
425  ret = AVERROR(EINVAL);
426  goto fail;
427  }
428  enc = avcodec_find_encoder(codec->codec_id);
429  if (enc && enc->priv_data_size && enc->priv_class) {
430  buffer = av_malloc(size + 1);
431  if (!buffer) {
432  ret = AVERROR(ENOMEM);
433  goto fail;
434  }
435  avio_get_str(pb, size, buffer, size + 1);
436  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
437  goto fail;
438  }
439  break;
440  case MKBETAG('S', '2', 'V', 'I'):
441  if (f_stvi++ || !size || codec->codec_type != AVMEDIA_TYPE_VIDEO) {
442  ret = AVERROR(EINVAL);
443  goto fail;
444  }
445  buffer = av_malloc(size);
446  if (!buffer) {
447  ret = AVERROR(ENOMEM);
448  goto fail;
449  }
450  avio_get_str(pb, INT_MAX, buffer, size);
451  av_set_options_string(codec, buffer, "=", ",");
452  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
453  goto fail;
454  break;
455  case MKBETAG('S', '2', 'A', 'U'):
456  if (f_stau++ || !size || codec->codec_type != AVMEDIA_TYPE_AUDIO) {
457  ret = AVERROR(EINVAL);
458  goto fail;
459  }
460  buffer = av_malloc(size);
461  if (!buffer) {
462  ret = AVERROR(ENOMEM);
463  goto fail;
464  }
465  avio_get_str(pb, INT_MAX, buffer, size);
466  av_set_options_string(codec, buffer, "=", ",");
467  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
468  goto fail;
469  break;
470  }
471  avio_seek(pb, next, SEEK_SET);
472  }
473 
474  /* get until end of block reached */
475  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
476  avio_r8(pb);
477 
478  /* init packet demux */
479  ffm->packet_ptr = ffm->packet;
480  ffm->packet_end = ffm->packet;
481  ffm->frame_offset = 0;
482  ffm->dts = 0;
483  ffm->read_state = READ_HEADER;
484  ffm->first_packet = 1;
485  return 0;
486  fail:
487  ffm_close(s);
488  return ret;
489 }
490 
492 {
493  FFMContext *ffm = s->priv_data;
494  AVStream *st;
495  AVIOContext *pb = s->pb;
496  AVCodecContext *codec;
497  const AVCodecDescriptor *codec_desc;
498  int i, nb_streams;
499  uint32_t tag;
500 
501  /* header */
502  tag = avio_rl32(pb);
503  if (tag == MKTAG('F', 'F', 'M', '2'))
504  return ffm2_read_header(s);
505  if (tag != MKTAG('F', 'F', 'M', '1'))
506  goto fail;
507  ffm->packet_size = avio_rb32(pb);
508  if (ffm->packet_size != FFM_PACKET_SIZE)
509  goto fail;
510  ffm->write_index = avio_rb64(pb);
511  /* get also filesize */
512  if (pb->seekable) {
513  ffm->file_size = avio_size(pb);
514  if (ffm->write_index && 0)
516  } else {
517  ffm->file_size = (UINT64_C(1) << 63) - 1;
518  }
519 
520  nb_streams = avio_rb32(pb);
521  avio_rb32(pb); /* total bitrate */
522  /* read each stream */
523  for(i=0;i<nb_streams;i++) {
524  char rc_eq_buf[128];
525 
526  st = avformat_new_stream(s, NULL);
527  if (!st)
528  goto fail;
529 
530  avpriv_set_pts_info(st, 64, 1, 1000000);
531 
532  codec = st->codec;
533  /* generic info */
534  codec->codec_id = avio_rb32(pb);
535  codec_desc = avcodec_descriptor_get(codec->codec_id);
536  if (!codec_desc) {
537  av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
538  codec->codec_id = AV_CODEC_ID_NONE;
539  goto fail;
540  }
541  codec->codec_type = avio_r8(pb); /* codec_type */
542  if (codec->codec_type != codec_desc->type) {
543  av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
544  codec_desc->type, codec->codec_type);
545  codec->codec_id = AV_CODEC_ID_NONE;
547  goto fail;
548  }
549  codec->bit_rate = avio_rb32(pb);
550  codec->flags = avio_rb32(pb);
551  codec->flags2 = avio_rb32(pb);
552  codec->debug = avio_rb32(pb);
553  /* specific info */
554  switch(codec->codec_type) {
555  case AVMEDIA_TYPE_VIDEO:
556  codec->time_base.num = avio_rb32(pb);
557  codec->time_base.den = avio_rb32(pb);
558  if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
559  av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
560  codec->time_base.num, codec->time_base.den);
561  goto fail;
562  }
563  codec->width = avio_rb16(pb);
564  codec->height = avio_rb16(pb);
565  codec->gop_size = avio_rb16(pb);
566  codec->pix_fmt = avio_rb32(pb);
567  if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
568  av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
569  codec->pix_fmt = AV_PIX_FMT_NONE;
570  goto fail;
571  }
572  codec->qmin = avio_r8(pb);
573  codec->qmax = avio_r8(pb);
574  codec->max_qdiff = avio_r8(pb);
575  codec->qcompress = avio_rb16(pb) / 10000.0;
576  codec->qblur = avio_rb16(pb) / 10000.0;
577  codec->bit_rate_tolerance = avio_rb32(pb);
578  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
579  codec->rc_eq = av_strdup(rc_eq_buf);
580  codec->rc_max_rate = avio_rb32(pb);
581  codec->rc_min_rate = avio_rb32(pb);
582  codec->rc_buffer_size = avio_rb32(pb);
583  codec->i_quant_factor = av_int2double(avio_rb64(pb));
584  codec->b_quant_factor = av_int2double(avio_rb64(pb));
585  codec->i_quant_offset = av_int2double(avio_rb64(pb));
586  codec->b_quant_offset = av_int2double(avio_rb64(pb));
587  codec->dct_algo = avio_rb32(pb);
588  codec->strict_std_compliance = avio_rb32(pb);
589  codec->max_b_frames = avio_rb32(pb);
590  codec->mpeg_quant = avio_rb32(pb);
591  codec->intra_dc_precision = avio_rb32(pb);
592  codec->me_method = avio_rb32(pb);
593  codec->mb_decision = avio_rb32(pb);
594  codec->nsse_weight = avio_rb32(pb);
595  codec->frame_skip_cmp = avio_rb32(pb);
597  codec->codec_tag = avio_rb32(pb);
598  codec->thread_count = avio_r8(pb);
599  codec->coder_type = avio_rb32(pb);
600  codec->me_cmp = avio_rb32(pb);
601  codec->me_subpel_quality = avio_rb32(pb);
602  codec->me_range = avio_rb32(pb);
603  codec->keyint_min = avio_rb32(pb);
604  codec->scenechange_threshold = avio_rb32(pb);
605  codec->b_frame_strategy = avio_rb32(pb);
606  codec->qcompress = av_int2double(avio_rb64(pb));
607  codec->qblur = av_int2double(avio_rb64(pb));
608  codec->max_qdiff = avio_rb32(pb);
609  codec->refs = avio_rb32(pb);
610  break;
611  case AVMEDIA_TYPE_AUDIO:
612  codec->sample_rate = avio_rb32(pb);
613  codec->channels = avio_rl16(pb);
614  codec->frame_size = avio_rl16(pb);
615  break;
616  default:
617  goto fail;
618  }
619  if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
620  if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
621  return AVERROR(ENOMEM);
622  }
623  }
624 
625  /* get until end of block reached */
626  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
627  avio_r8(pb);
628 
629  /* init packet demux */
630  ffm->packet_ptr = ffm->packet;
631  ffm->packet_end = ffm->packet;
632  ffm->frame_offset = 0;
633  ffm->dts = 0;
634  ffm->read_state = READ_HEADER;
635  ffm->first_packet = 1;
636  return 0;
637  fail:
638  ffm_close(s);
639  return -1;
640 }
641 
642 /* return < 0 if eof */
644 {
645  int size;
646  FFMContext *ffm = s->priv_data;
647  int duration, ret;
648 
649  switch(ffm->read_state) {
650  case READ_HEADER:
651  if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
652  return ret;
653 
654  ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
655  avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
656  if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
658  return -1;
659  if (ffm->header[1] & FLAG_DTS)
660  if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
661  return -1;
662  ffm->read_state = READ_DATA;
663  /* fall through */
664  case READ_DATA:
665  size = AV_RB24(ffm->header + 2);
666  if ((ret = ffm_is_avail_data(s, size)) < 0)
667  return ret;
668 
669  duration = AV_RB24(ffm->header + 5);
670 
671  if (av_new_packet(pkt, size) < 0) {
672  return AVERROR(ENOMEM);
673  }
674  pkt->stream_index = ffm->header[0];
675  if ((unsigned)pkt->stream_index >= s->nb_streams) {
676  av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
677  av_free_packet(pkt);
678  ffm->read_state = READ_HEADER;
679  return -1;
680  }
681  pkt->pos = avio_tell(s->pb);
682  if (ffm->header[1] & FLAG_KEY_FRAME)
683  pkt->flags |= AV_PKT_FLAG_KEY;
684 
685  ffm->read_state = READ_HEADER;
686  if (ffm_read_data(s, pkt->data, size, 0) != size) {
687  /* bad case: desynchronized packet. we cancel all the packet loading */
688  av_free_packet(pkt);
689  return -1;
690  }
691  pkt->pts = AV_RB64(ffm->header+8);
692  if (ffm->header[1] & FLAG_DTS)
693  pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
694  else
695  pkt->dts = pkt->pts;
696  pkt->duration = duration;
697  break;
698  }
699  return 0;
700 }
701 
702 /* seek to a given time in the file. The file read pointer is
703  positioned at or before pts. XXX: the following code is quite
704  approximative */
705 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
706 {
707  FFMContext *ffm = s->priv_data;
708  int64_t pos_min, pos_max, pos;
709  int64_t pts_min, pts_max, pts;
710  double pos1;
711 
712  ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
713  /* find the position using linear interpolation (better than
714  dichotomy in typical cases) */
715  if (ffm->write_index && ffm->write_index < ffm->file_size) {
716  if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
717  pos_min = FFM_PACKET_SIZE;
718  pos_max = ffm->write_index - FFM_PACKET_SIZE;
719  } else {
720  pos_min = ffm->write_index;
721  pos_max = ffm->file_size - FFM_PACKET_SIZE;
722  }
723  } else {
724  pos_min = FFM_PACKET_SIZE;
725  pos_max = ffm->file_size - FFM_PACKET_SIZE;
726  }
727  while (pos_min <= pos_max) {
728  pts_min = get_dts(s, pos_min);
729  pts_max = get_dts(s, pos_max);
730  if (pts_min > wanted_pts || pts_max <= wanted_pts) {
731  pos = pts_min > wanted_pts ? pos_min : pos_max;
732  goto found;
733  }
734  /* linear interpolation */
735  pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
736  (double)(pts_max - pts_min);
737  pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
738  if (pos <= pos_min)
739  pos = pos_min;
740  else if (pos >= pos_max)
741  pos = pos_max;
742  pts = get_dts(s, pos);
743  /* check if we are lucky */
744  if (pts == wanted_pts) {
745  goto found;
746  } else if (pts > wanted_pts) {
747  pos_max = pos - FFM_PACKET_SIZE;
748  } else {
749  pos_min = pos + FFM_PACKET_SIZE;
750  }
751  }
752  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
753 
754  found:
755  if (ffm_seek1(s, pos) < 0)
756  return -1;
757 
758  /* reset read state */
759  ffm->read_state = READ_HEADER;
760  ffm->packet_ptr = ffm->packet;
761  ffm->packet_end = ffm->packet;
762  ffm->first_packet = 1;
763 
764  return 0;
765 }
766 
767 static int ffm_probe(AVProbeData *p)
768 {
769  if (
770  p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
771  (p->buf[3] == '1' || p->buf[3] == '2'))
772  return AVPROBE_SCORE_MAX + 1;
773  return 0;
774 }
775 
777  .name = "ffm",
778  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
779  .priv_data_size = sizeof(FFMContext),
784  .read_seek = ffm_seek,
785 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2296
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#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:284
int64_t dts
Definition: ffm.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:284
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2129
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
Definition: avformat.h:1185
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:3009
int mpeg_quant
0-> h263 quant 1-> mpeg quant
Definition: avcodec.h:1829
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2958
enum AVCodecID id
Definition: mxfenc.c:102
int frame_offset
Definition: ffm.h:53
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2550
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1458
#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:4093
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2959
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:1790
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:1300
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
Definition: ffmdec.c:248
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:279
static AVPacket pkt
#define FLAG_DTS
Definition: ffm.h:37
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:679
int frame_skip_cmp
frame skip comparison function
Definition: avcodec.h:2716
AVCodec.
Definition: avcodec.h:3482
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmdec.c:643
static int ffm_read_data(AVFormatContext *s, uint8_t *buf, int size, int header)
Definition: ffmdec.c:81
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1845
attribute_deprecated int me_method
This option does nothing.
Definition: avcodec.h:1739
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
Definition: avcodec.h:2089
#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:1641
static int ffm_resync(AVFormatContext *s, int state)
Definition: ffmdec.c:67
Format I/O context.
Definition: avformat.h:1285
Definition: ffm.h:41
#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:1585
attribute_deprecated float rc_buffer_aggressivity
Definition: avcodec.h:2628
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:2606
uint8_t
static int nb_streams
Definition: ffprobe.c:226
#define av_malloc(s)
static int64_t get_dts(AVFormatContext *s, int64_t pos)
Definition: ffmdec.c:172
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:2024
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:694
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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:1799
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:3756
int me_cmp
motion estimation comparison function
Definition: avcodec.h:1919
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
static int ffm_is_avail_data(AVFormatContext *s, int size)
Definition: ffmdec.c:36
int coder_type
coder type
Definition: avcodec.h:2667
uint8_t * data
Definition: avcodec.h:1433
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
uint32_t tag
Definition: movenc.c:1339
#define ff_dlog(a,...)
#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:761
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
static const uint8_t header[24]
Definition: sdr2.c:67
static int64_t duration
Definition: ffplay.c:326
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
static int ffm2_read_header(AVFormatContext *s)
Definition: ffmdec.c:269
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
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:176
static void adjust_write_index(AVFormatContext *s)
Definition: ffmdec.c:184
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:663
#define AVERROR(e)
Definition: error.h:43
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:843
int qmax
maximum quantizer
Definition: avcodec.h:2564
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1607
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2614
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:1838
static int ffm_probe(AVProbeData *p)
Definition: ffmdec.c:767
#define FFMAX(a, b)
Definition: common.h:90
#define fail()
Definition: checkasm.h:57
int first_packet
Definition: ffm.h:51
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2935
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:533
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2591
int intra_dc_precision
precision of the intra DC coefficient - 8
Definition: avcodec.h:2117
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1341
int refs
number of reference frames
Definition: avcodec.h:2188
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1577
#define FFMIN(a, b)
Definition: common.h:92
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1691
int priv_data_size
Definition: avcodec.h:3518
static struct @197 state
int b_frame_strategy
Definition: avcodec.h:1807
uint8_t header[FRAME_HEADER_SIZE+4]
Definition: ffm.h:48
int mb_decision
macroblock decision mode
Definition: avcodec.h:2064
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:192
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2571
int packet_size
Definition: ffm.h:52
int buffer_size
Maximum buffer size.
Definition: avio.h:126
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3043
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
Stream structure.
Definition: avformat.h:854
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:2292
static int ffm_close(AVFormatContext *s)
Definition: ffmdec.c:238
enum AVMediaType codec_type
Definition: avcodec.h:1520
enum AVCodecID codec_id
Definition: avcodec.h:1529
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int sample_rate
samples per second
Definition: avcodec.h:2272
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
int debug
debug
Definition: avcodec.h:2852
Definition: ffm.h:44
main external API structure.
Definition: avcodec.h:1512
int qmin
minimum quantizer
Definition: avcodec.h:2557
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1544
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:578
void * buf
Definition: avisynth_c.h:553
static int ffm_read_header(AVFormatContext *s)
Definition: ffmdec.c:491
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1814
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2549
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:566
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3508
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:784
AVInputFormat ff_ffm_demuxer
Definition: ffmdec.c:776
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
Definition: ffmdec.c:160
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1717
enum AVMediaType type
Definition: avcodec.h:568
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:647
Main libavformat public API header.
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:56
int nsse_weight
noise vs.
Definition: avcodec.h:3118
int64_t pos
position in the file of the current buffer
Definition: avio.h:137
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
Definition: ffmdec.c:705
int den
denominator
Definition: rational.h:45
#define MKBETAG(a, b, c, d)
Definition: common.h:342
int eof_reached
true if eof reached
Definition: avio.h:139
int len
int channels
number of audio channels
Definition: avcodec.h:2273
void * priv_data
Format private data.
Definition: avformat.h:1313
int read_state
Definition: ffm.h:47
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1614
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:1432
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:719
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:303
int stream_index
Definition: avcodec.h:1435
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2621
#define MKTAG(a, b, c, d)
Definition: common.h:341
This structure stores compressed data.
Definition: avcodec.h:1410
int me_subpel_quality
subpel ME quality
Definition: avcodec.h:1995
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2830
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
GLuint buffer
Definition: opengl_enc.c:102
#define FFM_PACKET_SIZE
Definition: ffm.h:31
int keyint_min
minimum GOP size
Definition: avcodec.h:2181