FFmpeg  3.4.9
flvdec.c
Go to the documentation of this file.
1 /*
2  * FLV demuxer
3  * Copyright (c) 2003 The FFmpeg Project
4  *
5  * This demuxer will generate a 1 byte extradata for VP6F content.
6  * It is composed of:
7  * - upper 4 bits: difference between encoded width and visible width
8  * - lower 4 bits: difference between encoded height and visible height
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * FFmpeg is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with FFmpeg; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 #include "libavutil/avstring.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intfloat.h"
32 #include "libavutil/mathematics.h"
33 #include "libavcodec/bytestream.h"
34 #include "libavcodec/mpeg4audio.h"
35 #include "avformat.h"
36 #include "internal.h"
37 #include "avio_internal.h"
38 #include "flv.h"
39 
40 #define VALIDATE_INDEX_TS_THRESH 2500
41 
42 #define RESYNC_BUFFER_SIZE (1<<20)
43 
44 #define MAX_DEPTH 16 ///< arbitrary limit to prevent unbounded recursion
45 
46 typedef struct FLVContext {
47  const AVClass *class; ///< Class for private options.
48  int trust_metadata; ///< configure streams according onMetaData
49  int wrong_dts; ///< wrong dts due to negative cts
54  struct {
55  int64_t dts;
56  int64_t pos;
57  } validate_index[2];
61 
63 
66 
69  int64_t video_bit_rate;
70  int64_t audio_bit_rate;
71  int64_t *keyframe_times;
75 } FLVContext;
76 
77 static int probe(AVProbeData *p, int live)
78 {
79  const uint8_t *d = p->buf;
80  unsigned offset = AV_RB32(d + 5);
81 
82  if (d[0] == 'F' &&
83  d[1] == 'L' &&
84  d[2] == 'V' &&
85  d[3] < 5 && d[5] == 0 &&
86  offset + 100 < p->buf_size &&
87  offset > 8) {
88  int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
89 
90  if (live == is_live)
91  return AVPROBE_SCORE_MAX;
92  }
93  return 0;
94 }
95 
96 static int flv_probe(AVProbeData *p)
97 {
98  return probe(p, 0);
99 }
100 
102 {
103  return probe(p, 1);
104 }
105 
107 {
108  FLVContext *flv = s->priv_data;
109  AVStream *stream = NULL;
110  unsigned int i = 0;
111 
112  if (flv->last_keyframe_stream_index < 0) {
113  av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
114  return;
115  }
116 
118  stream = s->streams[flv->last_keyframe_stream_index];
119 
120  if (stream->nb_index_entries == 0) {
121  for (i = 0; i < flv->keyframe_count; i++) {
122  av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
123  flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
125  flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
126  }
127  } else
128  av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
129 
130  if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
131  av_freep(&flv->keyframe_times);
133  flv->keyframe_count = 0;
134  }
135 }
136 
138 {
139  FLVContext *flv = s->priv_data;
141  if (!st)
142  return NULL;
144  if (s->nb_streams>=3 ||( s->nb_streams==2
148  if (codec_type == AVMEDIA_TYPE_AUDIO) {
149  st->codecpar->bit_rate = flv->audio_bit_rate;
151  }
152  if (codec_type == AVMEDIA_TYPE_VIDEO) {
153  st->codecpar->bit_rate = flv->video_bit_rate;
155  st->avg_frame_rate = flv->framerate;
156  }
157 
158 
159  avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
162  return st;
163 }
164 
166 {
167  int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
168  int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
169  int codec_id;
170 
171  if (!apar->codec_id && !apar->codec_tag)
172  return 1;
173 
174  if (apar->bits_per_coded_sample != bits_per_coded_sample)
175  return 0;
176 
177  switch (flv_codecid) {
178  // no distinction between S16 and S8 PCM codec flags
179  case FLV_CODECID_PCM:
180  codec_id = bits_per_coded_sample == 8
182 #if HAVE_BIGENDIAN
184 #else
186 #endif
187  return codec_id == apar->codec_id;
188  case FLV_CODECID_PCM_LE:
189  codec_id = bits_per_coded_sample == 8
192  return codec_id == apar->codec_id;
193  case FLV_CODECID_AAC:
194  return apar->codec_id == AV_CODEC_ID_AAC;
195  case FLV_CODECID_ADPCM:
196  return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
197  case FLV_CODECID_SPEEX:
198  return apar->codec_id == AV_CODEC_ID_SPEEX;
199  case FLV_CODECID_MP3:
200  return apar->codec_id == AV_CODEC_ID_MP3;
204  return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
206  return apar->sample_rate == 8000 &&
209  return apar->sample_rate == 8000 &&
211  default:
212  return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
213  }
214 }
215 
217  AVCodecParameters *apar, int flv_codecid)
218 {
219  switch (flv_codecid) {
220  // no distinction between S16 and S8 PCM codec flags
221  case FLV_CODECID_PCM:
222  apar->codec_id = apar->bits_per_coded_sample == 8
224 #if HAVE_BIGENDIAN
226 #else
228 #endif
229  break;
230  case FLV_CODECID_PCM_LE:
231  apar->codec_id = apar->bits_per_coded_sample == 8
234  break;
235  case FLV_CODECID_AAC:
236  apar->codec_id = AV_CODEC_ID_AAC;
237  break;
238  case FLV_CODECID_ADPCM:
240  break;
241  case FLV_CODECID_SPEEX:
242  apar->codec_id = AV_CODEC_ID_SPEEX;
243  apar->sample_rate = 16000;
244  break;
245  case FLV_CODECID_MP3:
246  apar->codec_id = AV_CODEC_ID_MP3;
248  break;
250  // in case metadata does not otherwise declare samplerate
251  apar->sample_rate = 8000;
253  break;
255  apar->sample_rate = 16000;
257  break;
260  break;
262  apar->sample_rate = 8000;
264  break;
266  apar->sample_rate = 8000;
268  break;
269  default:
270  avpriv_request_sample(s, "Audio codec (%x)",
271  flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
272  apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
273  }
274 }
275 
277 {
278  int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
279 
280  if (!vpar->codec_id && !vpar->codec_tag)
281  return 1;
282 
283  switch (flv_codecid) {
284  case FLV_CODECID_H263:
285  return vpar->codec_id == AV_CODEC_ID_FLV1;
286  case FLV_CODECID_SCREEN:
287  return vpar->codec_id == AV_CODEC_ID_FLASHSV;
288  case FLV_CODECID_SCREEN2:
289  return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
290  case FLV_CODECID_VP6:
291  return vpar->codec_id == AV_CODEC_ID_VP6F;
292  case FLV_CODECID_VP6A:
293  return vpar->codec_id == AV_CODEC_ID_VP6A;
294  case FLV_CODECID_H264:
295  return vpar->codec_id == AV_CODEC_ID_H264;
296  default:
297  return vpar->codec_tag == flv_codecid;
298  }
299 }
300 
302  int flv_codecid, int read)
303 {
304  int ret = 0;
305  AVCodecParameters *par = vstream->codecpar;
306  enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
307  switch (flv_codecid) {
308  case FLV_CODECID_H263:
309  par->codec_id = AV_CODEC_ID_FLV1;
310  break;
312  par->codec_id = AV_CODEC_ID_H263;
313  break; // Really mean it this time
314  case FLV_CODECID_SCREEN:
316  break;
317  case FLV_CODECID_SCREEN2:
319  break;
320  case FLV_CODECID_VP6:
321  par->codec_id = AV_CODEC_ID_VP6F;
322  case FLV_CODECID_VP6A:
323  if (flv_codecid == FLV_CODECID_VP6A)
324  par->codec_id = AV_CODEC_ID_VP6A;
325  if (read) {
326  if (par->extradata_size != 1) {
327  ff_alloc_extradata(par, 1);
328  }
329  if (par->extradata)
330  par->extradata[0] = avio_r8(s->pb);
331  else
332  avio_skip(s->pb, 1);
333  }
334  ret = 1; // 1 byte body size adjustment for flv_read_packet()
335  break;
336  case FLV_CODECID_H264:
337  par->codec_id = AV_CODEC_ID_H264;
339  ret = 3; // not 4, reading packet type will consume one byte
340  break;
341  case FLV_CODECID_MPEG4:
343  ret = 3;
344  break;
345  default:
346  avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
347  par->codec_tag = flv_codecid;
348  }
349 
350  if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
351  avpriv_request_sample(s, "Changing the codec id midstream");
352  return AVERROR_PATCHWELCOME;
353  }
354 
355  return ret;
356 }
357 
358 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
359 {
360  int ret;
361  int length = avio_rb16(ioc);
362  if (length >= buffsize) {
363  avio_skip(ioc, length);
364  return -1;
365  }
366 
367  ret = avio_read(ioc, buffer, length);
368  if (ret < 0)
369  return ret;
370  if (ret < length)
371  return AVERROR_INVALIDDATA;
372 
373  buffer[length] = '\0';
374 
375  return length;
376 }
377 
379 {
380  FLVContext *flv = s->priv_data;
381  unsigned int timeslen = 0, fileposlen = 0, i;
382  char str_val[256];
383  int64_t *times = NULL;
384  int64_t *filepositions = NULL;
385  int ret = AVERROR(ENOSYS);
386  int64_t initial_pos = avio_tell(ioc);
387 
388  if (flv->keyframe_count > 0) {
389  av_log(s, AV_LOG_DEBUG, "keyframes have been paresed\n");
390  return 0;
391  }
392  av_assert0(!flv->keyframe_times);
394 
395  if (s->flags & AVFMT_FLAG_IGNIDX)
396  return 0;
397 
398  while (avio_tell(ioc) < max_pos - 2 &&
399  amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
400  int64_t **current_array;
401  unsigned int arraylen;
402 
403  // Expect array object in context
404  if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
405  break;
406 
407  arraylen = avio_rb32(ioc);
408  if (arraylen>>28)
409  break;
410 
411  if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
412  current_array = &times;
413  timeslen = arraylen;
414  } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
415  !filepositions) {
416  current_array = &filepositions;
417  fileposlen = arraylen;
418  } else
419  // unexpected metatag inside keyframes, will not use such
420  // metadata for indexing
421  break;
422 
423  if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
424  ret = AVERROR(ENOMEM);
425  goto finish;
426  }
427 
428  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
429  double d;
430  if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
431  goto invalid;
432  d = av_int2double(avio_rb64(ioc));
433  if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
434  goto invalid;
435  current_array[0][i] = d;
436  }
437  if (times && filepositions) {
438  // All done, exiting at a position allowing amf_parse_object
439  // to finish parsing the object
440  ret = 0;
441  break;
442  }
443  }
444 
445  if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
446  for (i = 0; i < FFMIN(2,fileposlen); i++) {
447  flv->validate_index[i].pos = filepositions[i];
448  flv->validate_index[i].dts = times[i] * 1000;
449  flv->validate_count = i + 1;
450  }
451  flv->keyframe_times = times;
453  flv->keyframe_count = timeslen;
454  times = NULL;
455  filepositions = NULL;
456  } else {
457 invalid:
458  av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
459  }
460 
461 finish:
462  av_freep(&times);
463  av_freep(&filepositions);
464  avio_seek(ioc, initial_pos, SEEK_SET);
465  return ret;
466 }
467 
469  AVStream *vstream, const char *key,
470  int64_t max_pos, int depth)
471 {
472  AVCodecParameters *apar, *vpar;
473  FLVContext *flv = s->priv_data;
474  AVIOContext *ioc;
475  AMFDataType amf_type;
476  char str_val[1024];
477  double num_val;
478 
479  if (depth > MAX_DEPTH)
480  return AVERROR_PATCHWELCOME;
481 
482  num_val = 0;
483  ioc = s->pb;
484  if (avio_feof(ioc))
485  return AVERROR_EOF;
486  amf_type = avio_r8(ioc);
487 
488  switch (amf_type) {
490  num_val = av_int2double(avio_rb64(ioc));
491  break;
492  case AMF_DATA_TYPE_BOOL:
493  num_val = avio_r8(ioc);
494  break;
496  if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
497  av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
498  return -1;
499  }
500  break;
502  if (key &&
503  (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
504  !strcmp(KEYFRAMES_TAG, key) && depth == 1)
505  if (parse_keyframes_index(s, ioc,
506  max_pos) < 0)
507  av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
508  else
510  while (avio_tell(ioc) < max_pos - 2 &&
511  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
512  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
513  depth + 1) < 0)
514  return -1; // if we couldn't skip, bomb out.
515  if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
516  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
517  return -1;
518  }
519  break;
520  case AMF_DATA_TYPE_NULL:
523  break; // these take up no additional space
525  {
526  unsigned v;
527  avio_skip(ioc, 4); // skip 32-bit max array index
528  while (avio_tell(ioc) < max_pos - 2 &&
529  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
530  // this is the only case in which we would want a nested
531  // parse to not skip over the object
532  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
533  depth + 1) < 0)
534  return -1;
535  v = avio_r8(ioc);
536  if (v != AMF_END_OF_OBJECT) {
537  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
538  return -1;
539  }
540  break;
541  }
542  case AMF_DATA_TYPE_ARRAY:
543  {
544  unsigned int arraylen, i;
545 
546  arraylen = avio_rb32(ioc);
547  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
548  if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
549  depth + 1) < 0)
550  return -1; // if we couldn't skip, bomb out.
551  }
552  break;
553  case AMF_DATA_TYPE_DATE:
554  avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
555  break;
556  default: // unsupported type, we couldn't skip
557  av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
558  return -1;
559  }
560 
561  if (key) {
562  apar = astream ? astream->codecpar : NULL;
563  vpar = vstream ? vstream->codecpar : NULL;
564 
565  // stream info doesn't live any deeper than the first object
566  if (depth == 1) {
567  if (amf_type == AMF_DATA_TYPE_NUMBER ||
568  amf_type == AMF_DATA_TYPE_BOOL) {
569  if (!strcmp(key, "duration"))
570  s->duration = num_val * AV_TIME_BASE;
571  else if (!strcmp(key, "videodatarate") &&
572  0 <= (int)(num_val * 1024.0))
573  flv->video_bit_rate = num_val * 1024.0;
574  else if (!strcmp(key, "audiodatarate") &&
575  0 <= (int)(num_val * 1024.0))
576  flv->audio_bit_rate = num_val * 1024.0;
577  else if (!strcmp(key, "datastream")) {
579  if (!st)
580  return AVERROR(ENOMEM);
582  } else if (!strcmp(key, "framerate")) {
583  flv->framerate = av_d2q(num_val, 1000);
584  if (vstream)
585  vstream->avg_frame_rate = flv->framerate;
586  } else if (flv->trust_metadata) {
587  if (!strcmp(key, "videocodecid") && vpar) {
588  int ret = flv_set_video_codec(s, vstream, num_val, 0);
589  if (ret < 0)
590  return ret;
591  } else if (!strcmp(key, "audiocodecid") && apar) {
592  int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
593  flv_set_audio_codec(s, astream, apar, id);
594  } else if (!strcmp(key, "audiosamplerate") && apar) {
595  apar->sample_rate = num_val;
596  } else if (!strcmp(key, "audiosamplesize") && apar) {
597  apar->bits_per_coded_sample = num_val;
598  } else if (!strcmp(key, "stereo") && apar) {
599  apar->channels = num_val + 1;
600  apar->channel_layout = apar->channels == 2 ?
603  } else if (!strcmp(key, "width") && vpar) {
604  vpar->width = num_val;
605  } else if (!strcmp(key, "height") && vpar) {
606  vpar->height = num_val;
607  }
608  }
609  }
610  if (amf_type == AMF_DATA_TYPE_STRING) {
611  if (!strcmp(key, "encoder")) {
612  int version = -1;
613  if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
614  if (version > 0 && version <= 655)
615  flv->broken_sizes = 1;
616  }
617  } else if (!strcmp(key, "metadatacreator") && !strcmp(str_val, "MEGA")) {
618  flv->broken_sizes = 1;
619  }
620  }
621  }
622 
623  if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
624  ((!apar && !strcmp(key, "audiocodecid")) ||
625  (!vpar && !strcmp(key, "videocodecid"))))
626  s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
627 
628  if (!strcmp(key, "duration") ||
629  !strcmp(key, "filesize") ||
630  !strcmp(key, "width") ||
631  !strcmp(key, "height") ||
632  !strcmp(key, "videodatarate") ||
633  !strcmp(key, "framerate") ||
634  !strcmp(key, "videocodecid") ||
635  !strcmp(key, "audiodatarate") ||
636  !strcmp(key, "audiosamplerate") ||
637  !strcmp(key, "audiosamplesize") ||
638  !strcmp(key, "stereo") ||
639  !strcmp(key, "audiocodecid") ||
640  !strcmp(key, "datastream"))
641  return 0;
642 
644  if (amf_type == AMF_DATA_TYPE_BOOL) {
645  av_strlcpy(str_val, num_val > 0 ? "true" : "false",
646  sizeof(str_val));
647  av_dict_set(&s->metadata, key, str_val, 0);
648  } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
649  snprintf(str_val, sizeof(str_val), "%.f", num_val);
650  av_dict_set(&s->metadata, key, str_val, 0);
651  } else if (amf_type == AMF_DATA_TYPE_STRING)
652  av_dict_set(&s->metadata, key, str_val, 0);
653  }
654 
655  return 0;
656 }
657 
658 #define TYPE_ONTEXTDATA 1
659 #define TYPE_ONCAPTION 2
660 #define TYPE_ONCAPTIONINFO 3
661 #define TYPE_UNKNOWN 9
662 
663 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
664 {
665  FLVContext *flv = s->priv_data;
666  AMFDataType type;
667  AVStream *stream, *astream, *vstream;
668  AVStream av_unused *dstream;
669  AVIOContext *ioc;
670  int i;
671  // only needs to hold the string "onMetaData".
672  // Anything longer is something we don't want.
673  char buffer[32];
674 
675  astream = NULL;
676  vstream = NULL;
677  dstream = NULL;
678  ioc = s->pb;
679 
680  // first object needs to be "onMetaData" string
681  type = avio_r8(ioc);
682  if (type != AMF_DATA_TYPE_STRING ||
683  amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
684  return TYPE_UNKNOWN;
685 
686  if (!strcmp(buffer, "onTextData"))
687  return TYPE_ONTEXTDATA;
688 
689  if (!strcmp(buffer, "onCaption"))
690  return TYPE_ONCAPTION;
691 
692  if (!strcmp(buffer, "onCaptionInfo"))
693  return TYPE_ONCAPTIONINFO;
694 
695  if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
696  av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
697  return TYPE_UNKNOWN;
698  }
699 
700  // find the streams now so that amf_parse_object doesn't need to do
701  // the lookup every time it is called.
702  for (i = 0; i < s->nb_streams; i++) {
703  stream = s->streams[i];
704  if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
705  vstream = stream;
707  } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
708  astream = stream;
709  if (flv->last_keyframe_stream_index == -1)
711  }
712  else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
713  dstream = stream;
714  }
715 
716  // parse the second object (we want a mixed array)
717  if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
718  return -1;
719 
720  return 0;
721 }
722 
724 {
725  int flags;
726  FLVContext *flv = s->priv_data;
727  int offset;
728  int pre_tag_size = 0;
729 
730  avio_skip(s->pb, 4);
731  flags = avio_r8(s->pb);
732 
734 
736 
737  offset = avio_rb32(s->pb);
738  avio_seek(s->pb, offset, SEEK_SET);
739 
740  /* Annex E. The FLV File Format
741  * E.3 TheFLVFileBody
742  * Field Type Comment
743  * PreviousTagSize0 UI32 Always 0
744  * */
745  pre_tag_size = avio_rb32(s->pb);
746  if (pre_tag_size) {
747  av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
748  }
749 
750  s->start_time = 0;
751  flv->sum_flv_tag_size = 0;
752  flv->last_keyframe_stream_index = -1;
753 
754  return 0;
755 }
756 
758 {
759  int i;
760  FLVContext *flv = s->priv_data;
761  for (i=0; i<FLV_STREAM_TYPE_NB; i++)
762  av_freep(&flv->new_extradata[i]);
763  av_freep(&flv->keyframe_times);
765  return 0;
766 }
767 
769 {
770  av_freep(&st->codecpar->extradata);
771  if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
772  return AVERROR(ENOMEM);
773  st->internal->need_context_update = 1;
774  return 0;
775 }
776 
777 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
778  int size)
779 {
780  av_free(flv->new_extradata[stream]);
781  flv->new_extradata[stream] = av_mallocz(size +
783  if (!flv->new_extradata[stream])
784  return AVERROR(ENOMEM);
785  flv->new_extradata_size[stream] = size;
786  avio_read(pb, flv->new_extradata[stream], size);
787  return 0;
788 }
789 
790 static void clear_index_entries(AVFormatContext *s, int64_t pos)
791 {
792  int i, j, out;
794  "Found invalid index entries, clearing the index.\n");
795  for (i = 0; i < s->nb_streams; i++) {
796  AVStream *st = s->streams[i];
797  /* Remove all index entries that point to >= pos */
798  out = 0;
799  for (j = 0; j < st->nb_index_entries; j++)
800  if (st->index_entries[j].pos < pos)
801  st->index_entries[out++] = st->index_entries[j];
802  st->nb_index_entries = out;
803  }
804 }
805 
806 static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
807 {
808  int nb = -1, ret, parse_name = 1;
809 
810  if (depth > MAX_DEPTH)
811  return AVERROR_PATCHWELCOME;
812 
813  if (avio_feof(pb))
814  return AVERROR_EOF;
815 
816  switch (type) {
818  avio_skip(pb, 8);
819  break;
820  case AMF_DATA_TYPE_BOOL:
821  avio_skip(pb, 1);
822  break;
824  avio_skip(pb, avio_rb16(pb));
825  break;
826  case AMF_DATA_TYPE_ARRAY:
827  parse_name = 0;
829  nb = avio_rb32(pb);
830  if (nb < 0)
831  return AVERROR_INVALIDDATA;
833  while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
834  if (parse_name) {
835  int size = avio_rb16(pb);
836  if (!size) {
837  avio_skip(pb, 1);
838  break;
839  }
840  avio_skip(pb, size);
841  }
842  if ((ret = amf_skip_tag(pb, avio_r8(pb), depth + 1)) < 0)
843  return ret;
844  }
845  break;
846  case AMF_DATA_TYPE_NULL:
848  break;
849  default:
850  return AVERROR_INVALIDDATA;
851  }
852  return 0;
853 }
854 
856  int64_t dts, int64_t next)
857 {
858  AVIOContext *pb = s->pb;
859  AVStream *st = NULL;
860  char buf[20];
861  int ret = AVERROR_INVALIDDATA;
862  int i, length = -1;
863  int array = 0;
864 
865  switch (avio_r8(pb)) {
866  case AMF_DATA_TYPE_ARRAY:
867  array = 1;
869  avio_seek(pb, 4, SEEK_CUR);
871  break;
872  default:
873  goto skip;
874  }
875 
876  while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
877  AMFDataType type = avio_r8(pb);
878  if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
879  length = avio_rb16(pb);
880  ret = av_get_packet(pb, pkt, length);
881  if (ret < 0)
882  goto skip;
883  else
884  break;
885  } else {
886  if ((ret = amf_skip_tag(pb, type, 0)) < 0)
887  goto skip;
888  }
889  }
890 
891  if (length < 0) {
892  ret = AVERROR_INVALIDDATA;
893  goto skip;
894  }
895 
896  for (i = 0; i < s->nb_streams; i++) {
897  st = s->streams[i];
899  break;
900  }
901 
902  if (i == s->nb_streams) {
904  if (!st)
905  return AVERROR(ENOMEM);
907  }
908 
909  pkt->dts = dts;
910  pkt->pts = dts;
911  pkt->size = ret;
912 
913  pkt->stream_index = st->index;
914  pkt->flags |= AV_PKT_FLAG_KEY;
915 
916 skip:
917  avio_seek(s->pb, next + 4, SEEK_SET);
918 
919  return ret;
920 }
921 
923 {
924  FLVContext *flv = s->priv_data;
925  int64_t i;
926  int64_t pos = avio_tell(s->pb);
927 
928  for (i=0; !avio_feof(s->pb); i++) {
929  int j = i & (RESYNC_BUFFER_SIZE-1);
930  int j1 = j + RESYNC_BUFFER_SIZE;
931  flv->resync_buffer[j ] =
932  flv->resync_buffer[j1] = avio_r8(s->pb);
933 
934  if (i > 22) {
935  unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
936  if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
937  unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
938  unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
939  if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
940  unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
941  if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
942  avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
943  return 1;
944  }
945  }
946  }
947  }
948  }
949  return AVERROR_EOF;
950 }
951 
953 {
954  FLVContext *flv = s->priv_data;
955  int ret, i, size, flags;
956  enum FlvTagType type;
957  int stream_type=-1;
958  int64_t next, pos, meta_pos;
959  int64_t dts, pts = AV_NOPTS_VALUE;
960  int av_uninit(channels);
961  int av_uninit(sample_rate);
962  AVStream *st = NULL;
963  int last = -1;
964  int orig_size;
965 
966 retry:
967  /* pkt size is repeated at end. skip it */
968  pos = avio_tell(s->pb);
969  type = (avio_r8(s->pb) & 0x1F);
970  orig_size =
971  size = avio_rb24(s->pb);
972  flv->sum_flv_tag_size += size + 11;
973  dts = avio_rb24(s->pb);
974  dts |= (unsigned)avio_r8(s->pb) << 24;
975  av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
976  if (avio_feof(s->pb))
977  return AVERROR_EOF;
978  avio_skip(s->pb, 3); /* stream id, always 0 */
979  flags = 0;
980 
981  if (flv->validate_next < flv->validate_count) {
982  int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
983  if (pos == validate_pos) {
984  if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
986  flv->validate_next++;
987  } else {
988  clear_index_entries(s, validate_pos);
989  flv->validate_count = 0;
990  }
991  } else if (pos > validate_pos) {
992  clear_index_entries(s, validate_pos);
993  flv->validate_count = 0;
994  }
995  }
996 
997  if (size == 0) {
998  ret = FFERROR_REDO;
999  goto leave;
1000  }
1001 
1002  next = size + avio_tell(s->pb);
1003 
1004  if (type == FLV_TAG_TYPE_AUDIO) {
1005  stream_type = FLV_STREAM_TYPE_AUDIO;
1006  flags = avio_r8(s->pb);
1007  size--;
1008  } else if (type == FLV_TAG_TYPE_VIDEO) {
1009  stream_type = FLV_STREAM_TYPE_VIDEO;
1010  flags = avio_r8(s->pb);
1011  size--;
1013  goto skip;
1014  } else if (type == FLV_TAG_TYPE_META) {
1015  stream_type=FLV_STREAM_TYPE_DATA;
1016  if (size > 13 + 1 + 4) { // Header-type metadata stuff
1017  int type;
1018  meta_pos = avio_tell(s->pb);
1019  type = flv_read_metabody(s, next);
1020  if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
1021  if (type < 0 && flv->validate_count &&
1022  flv->validate_index[0].pos > next &&
1023  flv->validate_index[0].pos - 4 < next
1024  ) {
1025  av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
1026  next = flv->validate_index[0].pos - 4;
1027  }
1028  goto skip;
1029  } else if (type == TYPE_ONTEXTDATA) {
1030  avpriv_request_sample(s, "OnTextData packet");
1031  return flv_data_packet(s, pkt, dts, next);
1032  } else if (type == TYPE_ONCAPTION) {
1033  return flv_data_packet(s, pkt, dts, next);
1034  }
1035  avio_seek(s->pb, meta_pos, SEEK_SET);
1036  }
1037  } else {
1038  av_log(s, AV_LOG_DEBUG,
1039  "Skipping flv packet: type %d, size %d, flags %d.\n",
1040  type, size, flags);
1041 skip:
1042  if (avio_seek(s->pb, next, SEEK_SET) != next) {
1043  // This can happen if flv_read_metabody above read past
1044  // next, on a non-seekable input, and the preceding data has
1045  // been flushed out from the IO buffer.
1046  av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
1047  return AVERROR_INVALIDDATA;
1048  }
1049  ret = FFERROR_REDO;
1050  goto leave;
1051  }
1052 
1053  /* skip empty data packets */
1054  if (!size) {
1055  ret = FFERROR_REDO;
1056  goto leave;
1057  }
1058 
1059  /* now find stream */
1060  for (i = 0; i < s->nb_streams; i++) {
1061  st = s->streams[i];
1062  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1063  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1064  (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
1065  break;
1066  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1067  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1068  (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
1069  break;
1070  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1072  break;
1073  }
1074  }
1075  if (i == s->nb_streams) {
1076  static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
1077  st = create_stream(s, stream_types[stream_type]);
1078  if (!st)
1079  return AVERROR(ENOMEM);
1080 
1081  }
1082  av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1083 
1084  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1085  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1086  stream_type == FLV_STREAM_TYPE_AUDIO))
1087  av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
1088 
1089  if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
1090  ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
1091  || st->discard >= AVDISCARD_ALL
1092  ) {
1093  avio_seek(s->pb, next, SEEK_SET);
1094  ret = FFERROR_REDO;
1095  goto leave;
1096  }
1097 
1098  // if not streamed and no duration from metadata then seek to end to find
1099  // the duration from the timestamps
1100  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1101  (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1102  !flv->searched_for_end) {
1103  int size;
1104  const int64_t pos = avio_tell(s->pb);
1105  // Read the last 4 bytes of the file, this should be the size of the
1106  // previous FLV tag. Use the timestamp of its payload as duration.
1107  int64_t fsize = avio_size(s->pb);
1108 retry_duration:
1109  avio_seek(s->pb, fsize - 4, SEEK_SET);
1110  size = avio_rb32(s->pb);
1111  if (size > 0 && size < fsize) {
1112  // Seek to the start of the last FLV tag at position (fsize - 4 - size)
1113  // but skip the byte indicating the type.
1114  avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
1115  if (size == avio_rb24(s->pb) + 11) {
1116  uint32_t ts = avio_rb24(s->pb);
1117  ts |= (unsigned)avio_r8(s->pb) << 24;
1118  if (ts)
1119  s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1120  else if (fsize >= 8 && fsize - 8 >= size) {
1121  fsize -= size+4;
1122  goto retry_duration;
1123  }
1124  }
1125  }
1126 
1127  avio_seek(s->pb, pos, SEEK_SET);
1128  flv->searched_for_end = 1;
1129  }
1130 
1131  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1132  int bits_per_coded_sample;
1133  channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1134  sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1136  bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1137  if (!st->codecpar->channels || !st->codecpar->sample_rate ||
1139  st->codecpar->channels = channels;
1140  st->codecpar->channel_layout = channels == 1
1144  st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1145  }
1146  if (!st->codecpar->codec_id) {
1147  flv_set_audio_codec(s, st, st->codecpar,
1148  flags & FLV_AUDIO_CODECID_MASK);
1149  flv->last_sample_rate =
1151  flv->last_channels =
1152  channels = st->codecpar->channels;
1153  } else {
1155  if (!par) {
1156  ret = AVERROR(ENOMEM);
1157  goto leave;
1158  }
1159  par->sample_rate = sample_rate;
1160  par->bits_per_coded_sample = bits_per_coded_sample;
1161  flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1162  sample_rate = par->sample_rate;
1164  }
1165  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1166  int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1167  if (ret < 0)
1168  return ret;
1169  size -= ret;
1170  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1172  }
1173 
1174  if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1177  int type = avio_r8(s->pb);
1178  size--;
1180  // sign extension
1181  int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1182  pts = dts + cts;
1183  if (cts < 0) { // dts might be wrong
1184  if (!flv->wrong_dts)
1186  "Negative cts, previous timestamps might be wrong.\n");
1187  flv->wrong_dts = 1;
1188  } else if (FFABS(dts - pts) > 1000*60*15) {
1190  "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1191  dts = pts = AV_NOPTS_VALUE;
1192  }
1193  }
1194  if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1195  st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1196  AVDictionaryEntry *t;
1197 
1198  if (st->codecpar->extradata) {
1199  if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1200  return ret;
1201  ret = FFERROR_REDO;
1202  goto leave;
1203  }
1204  if ((ret = flv_get_extradata(s, st, size)) < 0)
1205  return ret;
1206 
1207  /* Workaround for buggy Omnia A/XE encoder */
1208  t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1209  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1210  st->codecpar->extradata_size = 2;
1211 
1212  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && 0) {
1213  MPEG4AudioConfig cfg;
1214 
1216  st->codecpar->extradata_size * 8, 1) >= 0) {
1217  st->codecpar->channels = cfg.channels;
1218  st->codecpar->channel_layout = 0;
1219  if (cfg.ext_sample_rate)
1221  else
1222  st->codecpar->sample_rate = cfg.sample_rate;
1223  av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
1224  st->codecpar->channels, st->codecpar->sample_rate);
1225  }
1226  }
1227 
1228  ret = FFERROR_REDO;
1229  goto leave;
1230  }
1231  }
1232 
1233  /* skip empty data packets */
1234  if (!size) {
1235  ret = FFERROR_REDO;
1236  goto leave;
1237  }
1238 
1239  ret = av_get_packet(s->pb, pkt, size);
1240  if (ret < 0)
1241  return ret;
1242  pkt->dts = dts;
1243  pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1244  pkt->stream_index = st->index;
1245  pkt->pos = pos;
1246  if (flv->new_extradata[stream_type]) {
1248  flv->new_extradata_size[stream_type]);
1249  if (side) {
1250  memcpy(side, flv->new_extradata[stream_type],
1251  flv->new_extradata_size[stream_type]);
1252  av_freep(&flv->new_extradata[stream_type]);
1253  flv->new_extradata_size[stream_type] = 0;
1254  }
1255  }
1256  if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1257  (sample_rate != flv->last_sample_rate ||
1258  channels != flv->last_channels)) {
1260  flv->last_channels = channels;
1261  ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1262  }
1263 
1264  if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1265  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1266  stream_type == FLV_STREAM_TYPE_DATA)
1267  pkt->flags |= AV_PKT_FLAG_KEY;
1268 
1269 leave:
1270  last = avio_rb32(s->pb);
1271  if (last != orig_size + 11 && last != orig_size + 10 &&
1272  !avio_feof(s->pb) &&
1273  (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1274  !flv->broken_sizes) {
1275  av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %d\n", last, orig_size + 11, flv->sum_flv_tag_size);
1276  avio_seek(s->pb, pos + 1, SEEK_SET);
1277  ret = resync(s);
1278  av_packet_unref(pkt);
1279  if (ret >= 0) {
1280  goto retry;
1281  }
1282  }
1283  return ret;
1284 }
1285 
1286 static int flv_read_seek(AVFormatContext *s, int stream_index,
1287  int64_t ts, int flags)
1288 {
1289  FLVContext *flv = s->priv_data;
1290  flv->validate_count = 0;
1291  return avio_seek_time(s->pb, stream_index, ts, flags);
1292 }
1293 
1294 #define OFFSET(x) offsetof(FLVContext, x)
1295 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1296 static const AVOption options[] = {
1297  { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1298  { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
1299  { NULL }
1300 };
1301 
1302 static const AVClass flv_class = {
1303  .class_name = "flvdec",
1304  .item_name = av_default_item_name,
1305  .option = options,
1306  .version = LIBAVUTIL_VERSION_INT,
1307 };
1308 
1310  .name = "flv",
1311  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1312  .priv_data_size = sizeof(FLVContext),
1313  .read_probe = flv_probe,
1318  .extensions = "flv",
1319  .priv_class = &flv_class,
1320 };
1321 
1322 static const AVClass live_flv_class = {
1323  .class_name = "live_flvdec",
1324  .item_name = av_default_item_name,
1325  .option = options,
1326  .version = LIBAVUTIL_VERSION_INT,
1327 };
1328 
1330  .name = "live_flv",
1331  .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1332  .priv_data_size = sizeof(FLVContext),
1338  .extensions = "flv",
1339  .priv_class = &live_flv_class,
1341 };
int missing_streams
Definition: flvdec.c:73
#define RESYNC_BUFFER_SIZE
Definition: flvdec.c:42
#define NULL
Definition: coverity.c:32
discard all frames except keyframes
Definition: avcodec.h:829
#define TYPE_ONCAPTIONINFO
Definition: flvdec.c:660
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
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:334
int size
AVOption.
Definition: opt.h:246
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition: opt.h:287
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1994
Definition: flv.h:74
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
Definition: g723_1.h:725
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
static void clear_index_entries(AVFormatContext *s, int64_t pos)
Definition: flvdec.c:790
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:4756
int64_t pos
Definition: avformat.h:820
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
#define TYPE_ONCAPTION
Definition: flvdec.c:659
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
#define KEYFRAMES_TIMESTAMP_TAG
Definition: flv.h:50
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1680
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecParameters *apar, int flv_codecid)
Definition: flvdec.c:216
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1092
static int flv_data_packet(AVFormatContext *s, AVPacket *pkt, int64_t dts, int64_t next)
Definition: flvdec.c:855
int searched_for_end
Definition: flvdec.c:60
#define AV_RB24
Definition: intreadwrite.h:64
enum AVMediaType codec_type
Definition: rtp.c:37
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1642
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
int version
Definition: avisynth_c.h:766
discard all
Definition: avcodec.h:830
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: flvdec.c:952
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:774
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1398
struct FLVContext::@209 validate_index[2]
#define OFFSET(x)
Definition: flvdec.c:1294
static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: flvdec.c:1286
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
int64_t * keyframe_times
Definition: flvdec.c:71
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
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1462
Public dictionary API.
#define TYPE_ONTEXTDATA
Definition: flvdec.c:658
static char buffer[20]
Definition: seek.c:32
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
uint8_t
int validate_next
Definition: flvdec.c:58
#define VD
Definition: flvdec.c:1295
int width
Video only.
Definition: avcodec.h:4218
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1302
AVOptions.
#define FLV_AUDIO_CODECID_MASK
Definition: flv.h:42
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2325
int64_t pos
Definition: flvdec.c:56
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:789
int wrong_dts
wrong dts due to negative cts
Definition: flvdec.c:49
#define AV_RB32
Definition: intreadwrite.h:130
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
static const AVOption options[]
Definition: flvdec.c:1296
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4383
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data)...
Definition: internal.h:607
#define FLV_VIDEO_CODECID_MASK
Definition: flv.h:44
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
#define VALIDATE_INDEX_TS_THRESH
Definition: flvdec.c:40
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
static void finish(void)
Definition: movenc.c:344
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1460
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define FLV_AUDIO_SAMPLERATE_OFFSET
Definition: flv.h:33
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:292
AMFDataType
Definition: flv.h:122
static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
Definition: flvdec.c:806
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:856
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
int trust_metadata
configure streams according onMetaData
Definition: flvdec.c:48
int validate_count
Definition: flvdec.c:59
#define FLV_AUDIO_SAMPLERATE_MASK
Definition: flv.h:41
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1513
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4254
#define av_log(a,...)
disposable inter frame (H.263 only)
Definition: flv.h:117
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:636
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4181
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
Definition: flvdec.c:378
#define AVINDEX_KEYFRAME
Definition: avformat.h:827
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
static void add_keyframes_index(AVFormatContext *s)
Definition: flvdec.c:106
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
#define TYPE_UNKNOWN
Definition: flvdec.c:661
static int live_flv_probe(AVProbeData *p)
Definition: flvdec.c:101
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:488
av_default_item_name
discard all bidirectional frames
Definition: avcodec.h:827
#define AVERROR(e)
Definition: error.h:43
static int flv_read_close(AVFormatContext *s)
Definition: flvdec.c:757
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
static int flv_read_header(AVFormatContext *s)
Definition: flvdec.c:723
int64_t video_bit_rate
Definition: flvdec.c:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:557
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
Definition: utils.c:4890
static AVStream * create_stream(AVFormatContext *s, int codec_type)
Definition: flvdec.c:137
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
int64_t * keyframe_filepositions
Definition: flvdec.c:72
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:970
FLVFileposition * filepositions
Definition: flvenc.c:112
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:2335
uint8_t * new_extradata[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:50
int broken_sizes
Definition: flvdec.c:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
uint8_t resync_buffer[2 *RESYNC_BUFFER_SIZE]
Definition: flvdec.c:62
static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read)
Definition: flvdec.c:301
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4170
int64_t dts
Definition: flvdec.c:55
Only parse headers, do not repack.
Definition: avformat.h:811
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:627
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int last_keyframe_stream_index
Definition: flvdec.c:67
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
#define FLV_AUDIO_CODECID_OFFSET
Definition: flv.h:34
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
audio channel layout utility functions
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3230
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:782
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1519
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int64_t audio_bit_rate
Definition: flvdec.c:70
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
Definition: flvdec.c:165
int new_extradata_size[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:51
int32_t
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1643
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static int flv_probe(AVProbeData *p)
Definition: flvdec.c:96
enum AVCodecID codec_id
Definition: vaapi_decode.c:235
static const AVClass flv_class
Definition: flvdec.c:1302
#define MAX_DEPTH
arbitrary limit to prevent unbounded recursion
Definition: flvdec.c:44
#define KEYFRAMES_TAG
Definition: flv.h:49
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static const AVClass live_flv_class
Definition: flvdec.c:1322
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
int sum_flv_tag_size
Definition: flvdec.c:65
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
Definition: flvdec.c:663
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1420
sample_rate
FLV common header.
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1241
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
AVRational framerate
Definition: flvdec.c:74
static int resync(AVFormatContext *s)
Definition: flvdec.c:922
video info/command frame
Definition: flv.h:119
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:618
static int probe(AVProbeData *p, int live)
Definition: flvdec.c:77
void * buf
Definition: avisynth_c.h:690
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
int last_sample_rate
Definition: flvdec.c:52
int nb_index_entries
Definition: avformat.h:1094
Describe the class of an AVClass context structure.
Definition: log.h:67
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define isnan(x)
Definition: libm.h:340
int keyframe_count
Definition: flvdec.c:68
int last_channels
Definition: flvdec.c:53
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar) ...
Definition: internal.h:198
static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream, int size)
Definition: flvdec.c:777
AVMediaType
Definition: avutil.h:199
#define snprintf
Definition: snprintf.h:34
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
#define FLV_AUDIO_CHANNEL_MASK
Definition: flv.h:39
#define FLV_AUDIO_SAMPLESIZE_MASK
Definition: flv.h:40
#define AMF_END_OF_OBJECT
Definition: flv.h:47
#define KEYFRAMES_BYTEOFFSET_TAG
Definition: flv.h:51
static int64_t pts
Global timestamp for the audio frames.
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1434
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
full parsing and repack
Definition: avformat.h:810
Main libavformat public API header.
int
raw UTF-8 text
Definition: avcodec.h:649
#define FLV_VIDEO_FRAMETYPE_MASK
Definition: flv.h:45
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, 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:3251
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
Definition: flvdec.c:358
AVInputFormat ff_live_flv_demuxer
Definition: flvdec.c:1329
int flags
Definition: flvenc.c:120
AVInputFormat ff_flv_demuxer
Definition: flvdec.c:1309
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration. ...
Definition: mpeg4audio.c:155
#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
key frame (for AVC, a seekable frame)
Definition: flv.h:115
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition: aviobuf.c:1163
FlvTagType
Definition: flv.h:59
#define av_free(p)
char * value
Definition: dict.h:87
int eof_reached
true if eof reached
Definition: avio.h:240
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth)
Definition: flvdec.c:468
void * priv_data
Format private data.
Definition: avformat.h:1377
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:292
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
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
#define av_uninit(x)
Definition: attributes.h:148
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1678
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
Definition: flvdec.c:768
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1444
FILE * out
Definition: movenc.c:54
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
AVCodecParameters * codecpar
Definition: avformat.h:1252
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
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
const char int length
Definition: avisynth_c.h:768
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:329
int stream_index
Definition: avcodec.h:1681
#define AV_CH_LAYOUT_MONO
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:952
This structure stores compressed data.
Definition: avcodec.h:1656
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
Definition: flvdec.c:276
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define av_unused
Definition: attributes.h:125