FFmpeg
 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/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/intfloat.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavcodec/internal.h"
33 #include "avformat.h"
34 #include "internal.h"
35 #include "ffm.h"
36 #include "avio_internal.h"
37 
39 {
40  FFMContext *ffm = s->priv_data;
41  int64_t pos, avail_size;
42  ptrdiff_t len;
43 
44  len = ffm->packet_end - ffm->packet_ptr;
45  if (size <= len)
46  return 1;
47  pos = avio_tell(s->pb);
48  if (!ffm->write_index) {
49  if (pos == ffm->file_size)
50  return AVERROR_EOF;
51  avail_size = ffm->file_size - pos;
52  } else {
53  if (pos == ffm->write_index) {
54  /* exactly at the end of stream */
55  if (ffm->server_attached)
56  return AVERROR(EAGAIN);
57  else
58  return AVERROR_INVALIDDATA;
59  } else if (pos < ffm->write_index) {
60  avail_size = ffm->write_index - pos;
61  } else {
62  avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
63  }
64  }
65  avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
66  if (size <= avail_size)
67  return 1;
68  else if (ffm->server_attached)
69  return AVERROR(EAGAIN);
70  else
71  return AVERROR_INVALIDDATA;
72 }
73 
74 static int ffm_resync(AVFormatContext *s, uint32_t state)
75 {
76  av_log(s, AV_LOG_ERROR, "resyncing\n");
77  while (state != PACKET_ID) {
78  if (avio_feof(s->pb)) {
79  av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
80  return -1;
81  }
82  state = (state << 8) | avio_r8(s->pb);
83  }
84  return 0;
85 }
86 
87 /* first is true if we read the frame header */
89  uint8_t *buf, int size, int header)
90 {
91  FFMContext *ffm = s->priv_data;
92  AVIOContext *pb = s->pb;
93  int fill_size, size1, frame_offset;
94  uint32_t id;
95  ptrdiff_t len;
96  int64_t last_pos = -1;
97 
98  size1 = size;
99  while (size > 0) {
100  redo:
101  len = ffm->packet_end - ffm->packet_ptr;
102  if (len < 0)
103  return -1;
104  if (len > size)
105  len = size;
106  if (len == 0) {
107  if (avio_tell(pb) == ffm->file_size) {
108  if (ffm->server_attached) {
109  avio_seek(pb, ffm->packet_size, SEEK_SET);
110  } else
111  return AVERROR_EOF;
112  }
113  retry_read:
114  if (pb->buffer_size != ffm->packet_size) {
115  int64_t tell = avio_tell(pb);
116  int ret = ffio_set_buf_size(pb, ffm->packet_size);
117  if (ret < 0)
118  return ret;
119  avio_seek(pb, tell, SEEK_SET);
120  }
121  id = avio_rb16(pb); /* PACKET_ID */
122  if (id != PACKET_ID) {
123  if (ffm_resync(s, id) < 0)
124  return -1;
125  last_pos = avio_tell(pb);
126  }
127  fill_size = avio_rb16(pb);
128  ffm->dts = avio_rb64(pb);
129  frame_offset = avio_rb16(pb);
130  avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
131  if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
132  return -1;
133  }
134  ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
135  /* if first packet or resynchronization packet, we must
136  handle it specifically */
137  if (ffm->first_packet || (frame_offset & 0x8000)) {
138  if (!frame_offset) {
139  /* This packet has no frame headers in it */
140  if (avio_tell(pb) >= ffm->packet_size * 3LL) {
141  int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
142  seekback = FFMAX(seekback, 0);
143  avio_seek(pb, -seekback, SEEK_CUR);
144  goto retry_read;
145  }
146  /* This is bad, we cannot find a valid frame header */
147  return 0;
148  }
149  ffm->first_packet = 0;
150  if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
151  ffm->packet_end = ffm->packet_ptr;
152  return -1;
153  }
154  ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
155  if (!header)
156  break;
157  } else {
158  ffm->packet_ptr = ffm->packet;
159  }
160  goto redo;
161  }
162  memcpy(buf, ffm->packet_ptr, len);
163  buf += len;
164  ffm->packet_ptr += len;
165  size -= len;
166  header = 0;
167  }
168  return size1 - size;
169 }
170 
171 /* ensure that actual seeking happens between FFM_PACKET_SIZE
172  and file_size - FFM_PACKET_SIZE */
173 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
174 {
175  FFMContext *ffm = s->priv_data;
176  AVIOContext *pb = s->pb;
177  int64_t pos;
178 
179  pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
180  pos = FFMAX(pos, FFM_PACKET_SIZE);
181  ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
182  return avio_seek(pb, pos, SEEK_SET);
183 }
184 
185 static int64_t get_dts(AVFormatContext *s, int64_t pos)
186 {
187  AVIOContext *pb = s->pb;
188  int64_t dts;
189 
190  ffm_seek1(s, pos);
191  avio_skip(pb, 4);
192  dts = avio_rb64(pb);
193  ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
194  return dts;
195 }
196 
198 {
199  FFMContext *ffm = s->priv_data;
200  AVIOContext *pb = s->pb;
201  int64_t pts;
202  //int64_t orig_write_index = ffm->write_index;
203  int64_t pos_min, pos_max;
204  int64_t pts_start;
205  int64_t ptr = avio_tell(pb);
206 
207 
208  pos_min = 0;
209  pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
210 
211  pts_start = get_dts(s, pos_min);
212 
213  pts = get_dts(s, pos_max);
214 
215  if (pts - 100000 > pts_start)
216  goto end;
217 
219 
220  pts_start = get_dts(s, pos_min);
221 
222  pts = get_dts(s, pos_max);
223 
224  if (pts - 100000 <= pts_start) {
225  while (1) {
226  int64_t newpos;
227  int64_t newpts;
228 
229  newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
230 
231  if (newpos == pos_min)
232  break;
233 
234  newpts = get_dts(s, newpos);
235 
236  if (newpts - 100000 <= pts) {
237  pos_max = newpos;
238  pts = newpts;
239  } else {
240  pos_min = newpos;
241  }
242  }
243  ffm->write_index += pos_max;
244  }
245 
246  end:
247  avio_seek(pb, ptr, SEEK_SET);
248 }
249 
250 
251 static int ffm_append_recommended_configuration(AVStream *st, char **conf)
252 {
253  int ret;
254  size_t newsize;
255  av_assert0(conf && st);
256  if (!*conf)
257  return 0;
260  *conf = 0;
261  return 0;
262  }
263  newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
264  if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
265  return ret;
267  av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
268  av_freep(conf);
269  return 0;
270 }
271 
272 #define VALIDATE_PARAMETER(parameter, name, check) { \
273  if (check) { \
274  av_log(s, AV_LOG_ERROR, "Invalid " name " %d\n", codecpar->parameter); \
275  ret = AVERROR_INVALIDDATA; \
276  goto fail; \
277  } \
278 }
279 
281 {
282  FFMContext *ffm = s->priv_data;
283  AVStream *st = NULL;
284  AVIOContext *pb = s->pb;
285  AVCodecContext *dummy_codec = NULL;
286  AVCodecParameters *codecpar = NULL;
287  const AVCodecDescriptor *codec_desc;
288  int ret;
289  int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
290  AVCodec *enc;
291  char *buffer;
292 
293  ffm->packet_size = avio_rb32(pb);
294  if (ffm->packet_size != FFM_PACKET_SIZE) {
295  av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
297  ret = AVERROR_INVALIDDATA;
298  goto fail;
299  }
300 
301  ffm->write_index = avio_rb64(pb);
302  /* get also filesize */
303  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
304  ffm->file_size = avio_size(pb);
305  if (ffm->write_index && 0)
307  } else {
308  ffm->file_size = (UINT64_C(1) << 63) - 1;
309  }
310  dummy_codec = avcodec_alloc_context3(NULL);
311 
312  while(!avio_feof(pb)) {
313  unsigned id = avio_rb32(pb);
314  unsigned size = avio_rb32(pb);
315  int64_t next = avio_tell(pb) + size;
316  char rc_eq_buf[128];
317  int flags;
318 
319  if(!id)
320  break;
321 
322  switch(id) {
323  case MKBETAG('M', 'A', 'I', 'N'):
324  if (f_main++) {
325  ret = AVERROR(EINVAL);
326  goto fail;
327  }
328  avio_rb32(pb); /* nb_streams */
329  avio_rb32(pb); /* total bitrate */
330  break;
331  case MKBETAG('C', 'O', 'M', 'M'):
332  f_cprv = f_stvi = f_stau = 0;
333  st = avformat_new_stream(s, NULL);
334  if (!st) {
335  ret = AVERROR(ENOMEM);
336  goto fail;
337  }
338 
339  avpriv_set_pts_info(st, 64, 1, 1000000);
340 
341  codecpar = st->codecpar;
342  /* generic info */
343  codecpar->codec_id = avio_rb32(pb);
344  codec_desc = avcodec_descriptor_get(codecpar->codec_id);
345  if (!codec_desc) {
346  av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
347  codecpar->codec_id = AV_CODEC_ID_NONE;
348  ret = AVERROR_INVALIDDATA;
349  goto fail;
350  }
351  codecpar->codec_type = avio_r8(pb);
352  if (codecpar->codec_type != codec_desc->type) {
353  av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
354  codec_desc->type, codecpar->codec_type);
355  codecpar->codec_id = AV_CODEC_ID_NONE;
356  codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
357  ret = AVERROR_INVALIDDATA;
358  goto fail;
359  }
360  codecpar->bit_rate = avio_rb32(pb);
361  if (codecpar->bit_rate < 0) {
362  av_log(s, AV_LOG_ERROR, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
363  ret = AVERROR_INVALIDDATA;
364  goto fail;
365  }
366  flags = avio_rb32(pb);
367 #if FF_API_LAVF_AVCTX
369  st->codec->flags = flags;
371 #endif
372  avio_rb32(pb); // flags2
373  avio_rb32(pb); // debug
374  if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
375  int size = avio_rb32(pb);
377  av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
378  ret = AVERROR_INVALIDDATA;
379  goto fail;
380  }
382  if (!codecpar->extradata) {
383  ret = AVERROR(ENOMEM);
384  goto fail;
385  }
386  codecpar->extradata_size = size;
387  avio_read(pb, codecpar->extradata, size);
388  }
389  break;
390  case MKBETAG('S', 'T', 'V', 'I'):
391  if (f_stvi++ || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
392  ret = AVERROR(EINVAL);
393  goto fail;
394  }
395  avio_rb32(pb); // time_base.num
396  avio_rb32(pb); // time_base.den
397  codecpar->width = avio_rb16(pb);
398  codecpar->height = avio_rb16(pb);
399  ret = av_image_check_size(codecpar->width, codecpar->height, 0, s);
400  if (ret < 0)
401  goto fail;
402  avio_rb16(pb); // gop_size
403  codecpar->format = avio_rb32(pb);
404  if (!av_pix_fmt_desc_get(codecpar->format)) {
405  av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
406  codecpar->format = AV_PIX_FMT_NONE;
407  ret = AVERROR_INVALIDDATA;
408  goto fail;
409  }
410  avio_r8(pb); // qmin
411  avio_r8(pb); // qmax
412  avio_r8(pb); // max_qdiff
413  avio_rb16(pb); // qcompress / 10000.0
414  avio_rb16(pb); // qblur / 10000.0
415  avio_rb32(pb); // bit_rate_tolerance
416  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
417 
418  avio_rb32(pb); // rc_max_rate
419  avio_rb32(pb); // rc_min_rate
420  avio_rb32(pb); // rc_buffer_size
421  avio_rb64(pb); // i_quant_factor
422  avio_rb64(pb); // b_quant_factor
423  avio_rb64(pb); // i_quant_offset
424  avio_rb64(pb); // b_quant_offset
425  avio_rb32(pb); // dct_algo
426  avio_rb32(pb); // strict_std_compliance
427  avio_rb32(pb); // max_b_frames
428  avio_rb32(pb); // mpeg_quant
429  avio_rb32(pb); // intra_dc_precision
430  avio_rb32(pb); // me_method
431  avio_rb32(pb); // mb_decision
432  avio_rb32(pb); // nsse_weight
433  avio_rb32(pb); // frame_skip_cmp
434  avio_rb64(pb); // rc_buffer_aggressivity
435  codecpar->codec_tag = avio_rb32(pb);
436  avio_r8(pb); // thread_count
437  avio_rb32(pb); // coder_type
438  avio_rb32(pb); // me_cmp
439  avio_rb32(pb); // me_subpel_quality
440  avio_rb32(pb); // me_range
441  avio_rb32(pb); // keyint_min
442  avio_rb32(pb); // scenechange_threshold
443  avio_rb32(pb); // b_frame_strategy
444  avio_rb64(pb); // qcompress
445  avio_rb64(pb); // qblur
446  avio_rb32(pb); // max_qdiff
447  avio_rb32(pb); // refs
448  break;
449  case MKBETAG('S', 'T', 'A', 'U'):
450  if (f_stau++ || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
451  ret = AVERROR(EINVAL);
452  goto fail;
453  }
454  codecpar->sample_rate = avio_rb32(pb);
455  VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
456  codecpar->channels = avio_rl16(pb);
457  VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
458  codecpar->frame_size = avio_rl16(pb);
459  VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
460  break;
461  case MKBETAG('C', 'P', 'R', 'V'):
462  if (f_cprv++) {
463  ret = AVERROR(EINVAL);
464  goto fail;
465  }
466  enc = avcodec_find_encoder(codecpar->codec_id);
467  if (enc && enc->priv_data_size && enc->priv_class) {
468  buffer = av_malloc(size + 1);
469  if (!buffer) {
470  ret = AVERROR(ENOMEM);
471  goto fail;
472  }
473  avio_get_str(pb, size, buffer, size + 1);
474  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
475  goto fail;
476  }
477  break;
478  case MKBETAG('S', '2', 'V', 'I'):
479  if (f_stvi++ || !size || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
480  ret = AVERROR(EINVAL);
481  goto fail;
482  }
483  buffer = av_malloc(size);
484  if (!buffer) {
485  ret = AVERROR(ENOMEM);
486  goto fail;
487  }
488  avio_get_str(pb, INT_MAX, buffer, size);
489  // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
490  avcodec_parameters_to_context(dummy_codec, codecpar);
491  av_set_options_string(dummy_codec, buffer, "=", ",");
492  avcodec_parameters_from_context(codecpar, dummy_codec);
493  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
494  goto fail;
495  break;
496  case MKBETAG('S', '2', 'A', 'U'):
497  if (f_stau++ || !size || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
498  ret = AVERROR(EINVAL);
499  goto fail;
500  }
501  buffer = av_malloc(size);
502  if (!buffer) {
503  ret = AVERROR(ENOMEM);
504  goto fail;
505  }
506  avio_get_str(pb, INT_MAX, buffer, size);
507  // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
508  avcodec_parameters_to_context(dummy_codec, codecpar);
509  av_set_options_string(dummy_codec, buffer, "=", ",");
510  avcodec_parameters_from_context(codecpar, dummy_codec);
511  if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
512  goto fail;
513  break;
514  }
515  avio_seek(pb, next, SEEK_SET);
516  }
517 
518  /* get until end of block reached */
519  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
520  avio_r8(pb);
521 
522  /* init packet demux */
523  ffm->packet_ptr = ffm->packet;
524  ffm->packet_end = ffm->packet;
525  ffm->frame_offset = 0;
526  ffm->dts = 0;
527  ffm->read_state = READ_HEADER;
528  ffm->first_packet = 1;
529  avcodec_free_context(&dummy_codec);
530  return 0;
531  fail:
532  avcodec_free_context(&dummy_codec);
533  return ret;
534 }
535 
537 {
538  FFMContext *ffm = s->priv_data;
539  AVStream *st;
540  AVIOContext *pb = s->pb;
541  AVCodecContext *dummy_codec = NULL;
542  AVCodecParameters *codecpar;
543  const AVCodecDescriptor *codec_desc;
544  int i, nb_streams, ret;
545  uint32_t tag;
546 
547  /* header */
548  tag = avio_rl32(pb);
549  if (tag == MKTAG('F', 'F', 'M', '2'))
550  return ffm2_read_header(s);
551  if (tag != MKTAG('F', 'F', 'M', '1')) {
552  ret = AVERROR_INVALIDDATA;
553  goto fail;
554  }
555  ffm->packet_size = avio_rb32(pb);
556  if (ffm->packet_size != FFM_PACKET_SIZE) {
557  ret = AVERROR_INVALIDDATA;
558  goto fail;
559  }
560  ffm->write_index = avio_rb64(pb);
561  /* get also filesize */
562  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
563  ffm->file_size = avio_size(pb);
564  if (ffm->write_index && 0)
566  } else {
567  ffm->file_size = (UINT64_C(1) << 63) - 1;
568  }
569  dummy_codec = avcodec_alloc_context3(NULL);
570 
571  nb_streams = avio_rb32(pb);
572  avio_rb32(pb); /* total bitrate */
573  /* read each stream */
574  for(i=0;i<nb_streams;i++) {
575  char rc_eq_buf[128];
576  int flags;
577 
578  st = avformat_new_stream(s, NULL);
579  if (!st) {
580  ret = AVERROR(ENOMEM);
581  goto fail;
582  }
583 
584  avpriv_set_pts_info(st, 64, 1, 1000000);
585 
586  codecpar = st->codecpar;
587  /* generic info */
588  codecpar->codec_id = avio_rb32(pb);
589  codec_desc = avcodec_descriptor_get(codecpar->codec_id);
590  if (!codec_desc) {
591  av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
592  codecpar->codec_id = AV_CODEC_ID_NONE;
593  ret = AVERROR_INVALIDDATA;
594  goto fail;
595  }
596  codecpar->codec_type = avio_r8(pb); /* codec_type */
597  if (codecpar->codec_type != codec_desc->type) {
598  av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
599  codec_desc->type, codecpar->codec_type);
600  codecpar->codec_id = AV_CODEC_ID_NONE;
601  codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
602  ret = AVERROR_INVALIDDATA;
603  goto fail;
604  }
605  codecpar->bit_rate = avio_rb32(pb);
606  if (codecpar->bit_rate < 0) {
607  av_log(s, AV_LOG_WARNING, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
608  ret = AVERROR_INVALIDDATA;
609  goto fail;
610  }
611  flags = avio_rb32(pb);
612 #if FF_API_LAVF_AVCTX
614  st->codec->flags = flags;
616 #endif
617  avio_rb32(pb); // flags2
618  avio_rb32(pb); // debug
619  /* specific info */
620  switch(codecpar->codec_type) {
621  case AVMEDIA_TYPE_VIDEO:
622  avio_rb32(pb); // time_base.num
623  avio_rb32(pb); // time_base.den
624  codecpar->width = avio_rb16(pb);
625  codecpar->height = avio_rb16(pb);
626  if ((ret = av_image_check_size(codecpar->width, codecpar->height, 0, s)) < 0)
627  goto fail;
628  avio_rb16(pb); // gop_size
629  codecpar->format = avio_rb32(pb);
630  if (!av_pix_fmt_desc_get(codecpar->format)) {
631  av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
632  codecpar->format = AV_PIX_FMT_NONE;
633  ret = AVERROR_INVALIDDATA;
634  goto fail;
635  }
636  avio_r8(pb); // qmin
637  avio_r8(pb); // qmax
638  avio_r8(pb); // max_qdiff
639  avio_rb16(pb); // qcompress / 10000.0
640  avio_rb16(pb); // qblur / 10000.0
641  avio_rb32(pb); // bit_rate_tolerance
642  avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
643 
644  avio_rb32(pb); // rc_max_rate
645  avio_rb32(pb); // rc_min_rate
646  avio_rb32(pb); // rc_buffer_size
647  avio_rb64(pb); // i_quant_factor
648  avio_rb64(pb); // b_quant_factor
649  avio_rb64(pb); // i_quant_offset
650  avio_rb64(pb); // b_quant_offset
651  avio_rb32(pb); // dct_algo
652  avio_rb32(pb); // strict_std_compliance
653  avio_rb32(pb); // max_b_frames
654  avio_rb32(pb); // mpeg_quant
655  avio_rb32(pb); // intra_dc_precision
656  avio_rb32(pb); // me_method
657  avio_rb32(pb); // mb_decision
658  avio_rb32(pb); // nsse_weight
659  avio_rb32(pb); // frame_skip_cmp
660  avio_rb64(pb); // rc_buffer_aggressivity
661  codecpar->codec_tag = avio_rb32(pb);
662  avio_r8(pb); // thread_count
663  avio_rb32(pb); // coder_type
664  avio_rb32(pb); // me_cmp
665  avio_rb32(pb); // me_subpel_quality
666  avio_rb32(pb); // me_range
667  avio_rb32(pb); // keyint_min
668  avio_rb32(pb); // scenechange_threshold
669  avio_rb32(pb); // b_frame_strategy
670  avio_rb64(pb); // qcompress
671  avio_rb64(pb); // qblur
672  avio_rb32(pb); // max_qdiff
673  avio_rb32(pb); // refs
674  break;
675  case AVMEDIA_TYPE_AUDIO:
676  codecpar->sample_rate = avio_rb32(pb);
677  VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
678  codecpar->channels = avio_rl16(pb);
679  VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
680  codecpar->frame_size = avio_rl16(pb);
681  VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
682  break;
683  default:
684  ret = AVERROR_INVALIDDATA;
685  goto fail;
686  }
687  if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
688  int size = avio_rb32(pb);
690  av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
691  ret = AVERROR_INVALIDDATA;
692  goto fail;
693  }
695  if (!codecpar->extradata) {
696  ret = AVERROR(ENOMEM);
697  goto fail;
698  }
699  codecpar->extradata_size = size;
700  avio_read(pb, codecpar->extradata, size);
701  }
702  }
703 
704  /* get until end of block reached */
705  while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
706  avio_r8(pb);
707 
708  /* init packet demux */
709  ffm->packet_ptr = ffm->packet;
710  ffm->packet_end = ffm->packet;
711  ffm->frame_offset = 0;
712  ffm->dts = 0;
713  ffm->read_state = READ_HEADER;
714  ffm->first_packet = 1;
715  avcodec_free_context(&dummy_codec);
716  return 0;
717  fail:
718  avcodec_free_context(&dummy_codec);
719  return ret;
720 }
721 
722 /* return < 0 if eof */
724 {
725  int size;
726  FFMContext *ffm = s->priv_data;
727  int duration, ret;
728 
729  switch(ffm->read_state) {
730  case READ_HEADER:
731  if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
732  return ret;
733 
734  ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
735  avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
736  if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
738  return -1;
739  if (ffm->header[1] & FLAG_DTS)
740  if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
741  return -1;
742  ffm->read_state = READ_DATA;
743  /* fall through */
744  case READ_DATA:
745  size = AV_RB24(ffm->header + 2);
746  if ((ret = ffm_is_avail_data(s, size)) < 0)
747  return ret;
748 
749  duration = AV_RB24(ffm->header + 5);
750 
751  if (av_new_packet(pkt, size) < 0) {
752  return AVERROR(ENOMEM);
753  }
754  pkt->stream_index = ffm->header[0];
755  if ((unsigned)pkt->stream_index >= s->nb_streams) {
756  av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
757  av_packet_unref(pkt);
758  ffm->read_state = READ_HEADER;
759  return -1;
760  }
761  pkt->pos = avio_tell(s->pb);
762  if (ffm->header[1] & FLAG_KEY_FRAME)
763  pkt->flags |= AV_PKT_FLAG_KEY;
764 
765  ffm->read_state = READ_HEADER;
766  if (ffm_read_data(s, pkt->data, size, 0) != size) {
767  /* bad case: desynchronized packet. we cancel all the packet loading */
768  av_packet_unref(pkt);
769  return -1;
770  }
771  pkt->pts = AV_RB64(ffm->header+8);
772  if (ffm->header[1] & FLAG_DTS)
773  pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
774  else
775  pkt->dts = pkt->pts;
776  pkt->duration = duration;
777  break;
778  }
779  return 0;
780 }
781 
782 /* seek to a given time in the file. The file read pointer is
783  positioned at or before pts. XXX: the following code is quite
784  approximative */
785 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
786 {
787  FFMContext *ffm = s->priv_data;
788  int64_t pos_min, pos_max, pos;
789  int64_t pts_min, pts_max, pts;
790  double pos1;
791 
792  ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
793  /* find the position using linear interpolation (better than
794  dichotomy in typical cases) */
795  if (ffm->write_index && ffm->write_index < ffm->file_size) {
796  if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
797  pos_min = FFM_PACKET_SIZE;
798  pos_max = ffm->write_index - FFM_PACKET_SIZE;
799  } else {
800  pos_min = ffm->write_index;
801  pos_max = ffm->file_size - FFM_PACKET_SIZE;
802  }
803  } else {
804  pos_min = FFM_PACKET_SIZE;
805  pos_max = ffm->file_size - FFM_PACKET_SIZE;
806  }
807  while (pos_min <= pos_max) {
808  pts_min = get_dts(s, pos_min);
809  pts_max = get_dts(s, pos_max);
810  if (pts_min > wanted_pts || pts_max <= wanted_pts) {
811  pos = pts_min > wanted_pts ? pos_min : pos_max;
812  goto found;
813  }
814  /* linear interpolation */
815  pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
816  (double)(pts_max - pts_min);
817  pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
818  if (pos <= pos_min)
819  pos = pos_min;
820  else if (pos >= pos_max)
821  pos = pos_max;
822  pts = get_dts(s, pos);
823  /* check if we are lucky */
824  if (pts == wanted_pts) {
825  goto found;
826  } else if (pts > wanted_pts) {
827  pos_max = pos - FFM_PACKET_SIZE;
828  } else {
829  pos_min = pos + FFM_PACKET_SIZE;
830  }
831  }
832  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
833 
834  found:
835  if (ffm_seek1(s, pos) < 0)
836  return -1;
837 
838  /* reset read state */
839  ffm->read_state = READ_HEADER;
840  ffm->packet_ptr = ffm->packet;
841  ffm->packet_end = ffm->packet;
842  ffm->first_packet = 1;
843 
844  return 0;
845 }
846 
847 static int ffm_probe(AVProbeData *p)
848 {
849  if (
850  p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
851  (p->buf[3] == '1' || p->buf[3] == '2'))
852  return AVPROBE_SCORE_MAX + 1;
853  return 0;
854 }
855 
856 static const AVOption options[] = {
857  {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
858  {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
859  {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
860  { NULL },
861 };
862 
863 static const AVClass ffm_class = {
864  .class_name = "ffm demuxer",
865  .item_name = av_default_item_name,
866  .option = options,
867  .version = LIBAVUTIL_VERSION_INT,
868 };
870  .name = "ffm",
871  .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
872  .priv_data_size = sizeof(FFMContext),
876  .read_seek = ffm_seek,
877  .priv_class = &ffm_class,
878 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2423
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#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:334
int64_t dts
Definition: ffm.h:55
static const AVOption options[]
Definition: ffmdec.c:856
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2419
char * recommended_encoder_configuration
String containing paris of key and values describing recommended encoder configuration.
Definition: avformat.h:1226
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:1256
AVOption.
Definition: opt.h:246
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition: opt.h:287
static struct @260 state
int frame_offset
Definition: ffm.h:54
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1699
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:4737
#define FFM_PACKET_SIZE
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
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:1412
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
Definition: ffmdec.c:251
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
#define FLAG_DTS
Definition: ffm.h:37
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:775
static int ffm_resync(AVFormatContext *s, uint32_t state)
Definition: ffmdec.c:74
AVCodec.
Definition: avcodec.h:3739
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmdec.c:723
static int ffm_read_data(AVFormatContext *s, uint8_t *buf, int size, int header)
Definition: ffmdec.c:88
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
#define FFM_HEADER_SIZE
Definition: ffm.h:30
int frame_size
Audio only.
Definition: avcodec.h:4273
Format I/O context.
Definition: avformat.h:1349
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int64_t file_size
Definition: ffm.h:47
uint8_t
static int nb_streams
Definition: ffprobe.c:276
#define av_malloc(s)
static int64_t get_dts(AVFormatContext *s, int64_t pos)
Definition: ffmdec.c:185
int width
Video only.
Definition: avcodec.h:4218
AVOptions.
#define FRAME_HEADER_SIZE
Definition: cpia.c:30
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
uint8_t * packet_end
Definition: ffm.h:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
int64_t duration
Definition: movenc.c:63
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2354
static int ffm_is_avail_data(AVFormatContext *s, int size)
Definition: ffmdec.c:38
uint8_t * data
Definition: avcodec.h:1679
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1409
#define ff_dlog(a,...)
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:857
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
static const uint8_t header[24]
Definition: sdr2.c:67
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4181
static int ffm2_read_header(AVFormatContext *s)
Definition: ffmdec.c:280
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
uint8_t * packet_ptr
Definition: ffm.h:56
#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:197
av_default_item_name
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define AVERROR(e)
Definition: error.h:43
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:1003
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
#define VALIDATE_PARAMETER(parameter, name, check)
Definition: ffmdec.c:272
static const AVClass ffm_class
Definition: ffmdec.c:863
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
simple assert() macros that are a bit more flexible than ISO C assert().
static int ffm_probe(AVProbeData *p)
Definition: ffmdec.c:847
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:109
int first_packet
Definition: ffm.h:52
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3121
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4170
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
common internal API header
#define FF_MAX_EXTRADATA_SIZE
Maximum size in bytes of extradata.
Definition: internal.h:260
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
int server_attached
Definition: ffm.h:59
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:281
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
#define FFMIN(a, b)
Definition: common.h:96
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int priv_data_size
Definition: avcodec.h:3775
uint8_t header[FRAME_HEADER_SIZE+4]
Definition: ffm.h:49
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
Definition: ffm.h:41
int packet_size
Definition: ffm.h:53
int buffer_size
Maximum buffer size.
Definition: avio.h:227
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
sample_rate
int frame_size
Definition: mxfenc.c:1896
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:172
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
Definition: ffm.h:44
main external API structure.
Definition: avcodec.h:1761
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:618
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:569
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2297
static int ffm_read_header(AVFormatContext *s)
Definition: ffmdec.c:536
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
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:703
static int64_t pts
Global timestamp for the audio frames.
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3765
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:925
AVInputFormat ff_ffm_demuxer
Definition: ffmdec.c:869
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
Definition: ffmdec.c:173
int sample_rate
Audio only.
Definition: avcodec.h:4262
enum AVMediaType type
Definition: avcodec.h:705
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
Main libavformat public API header.
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
uint8_t packet[FFM_PACKET_SIZE]
Definition: ffm.h:57
common internal api header.
int64_t pos
position in the file of the current buffer
Definition: avio.h:238
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
Definition: ffmdec.c:785
#define MKBETAG(a, b, c, d)
Definition: common.h:343
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:777
int eof_reached
true if eof reached
Definition: avio.h:240
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
int len
void * priv_data
Format private data.
Definition: avformat.h:1377
int read_state
Definition: ffm.h:48
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4166
int channels
Audio only.
Definition: avcodec.h:4258
int64_t write_index
Definition: ffm.h:47
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1678
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:815
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
int stream_index
Definition: avcodec.h:1681
#define MKTAG(a, b, c, d)
Definition: common.h:342
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1656
#define FLAG_KEY_FRAME
Definition: ffm.h:36
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
GLuint buffer
Definition: opengl_enc.c:102