FFmpeg  4.1.11
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 trust_datasize; ///< trust data size of FLVTag
50  int dump_full_metadata; ///< Dump full metadata of the onMetadata
51  int wrong_dts; ///< wrong dts due to negative cts
56  struct {
57  int64_t dts;
58  int64_t pos;
59  } validate_index[2];
63 
65 
68 
71  int64_t video_bit_rate;
72  int64_t audio_bit_rate;
73  int64_t *keyframe_times;
77 } FLVContext;
78 
79 static int probe(AVProbeData *p, int live)
80 {
81  const uint8_t *d = p->buf;
82  unsigned offset = AV_RB32(d + 5);
83 
84  if (d[0] == 'F' &&
85  d[1] == 'L' &&
86  d[2] == 'V' &&
87  d[3] < 5 && d[5] == 0 &&
88  offset + 100 < p->buf_size &&
89  offset > 8) {
90  int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
91 
92  if (live == is_live)
93  return AVPROBE_SCORE_MAX;
94  }
95  return 0;
96 }
97 
98 static int flv_probe(AVProbeData *p)
99 {
100  return probe(p, 0);
101 }
102 
104 {
105  return probe(p, 1);
106 }
107 
109 {
110  FLVContext *flv = s->priv_data;
111  AVStream *stream = NULL;
112  unsigned int i = 0;
113 
114  if (flv->last_keyframe_stream_index < 0) {
115  av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
116  return;
117  }
118 
120  stream = s->streams[flv->last_keyframe_stream_index];
121 
122  if (stream->nb_index_entries == 0) {
123  for (i = 0; i < flv->keyframe_count; i++) {
124  av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
125  flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
127  flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
128  }
129  } else
130  av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
131 
132  if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
133  av_freep(&flv->keyframe_times);
135  flv->keyframe_count = 0;
136  }
137 }
138 
140 {
141  FLVContext *flv = s->priv_data;
143  if (!st)
144  return NULL;
146  if (s->nb_streams>=3 ||( s->nb_streams==2
150  if (codec_type == AVMEDIA_TYPE_AUDIO) {
151  st->codecpar->bit_rate = flv->audio_bit_rate;
153  }
154  if (codec_type == AVMEDIA_TYPE_VIDEO) {
155  st->codecpar->bit_rate = flv->video_bit_rate;
157  st->avg_frame_rate = flv->framerate;
158  }
159 
160 
161  avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
164  return st;
165 }
166 
168 {
169  int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
170  int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
171  int codec_id;
172 
173  if (!apar->codec_id && !apar->codec_tag)
174  return 1;
175 
176  if (apar->bits_per_coded_sample != bits_per_coded_sample)
177  return 0;
178 
179  switch (flv_codecid) {
180  // no distinction between S16 and S8 PCM codec flags
181  case FLV_CODECID_PCM:
182  codec_id = bits_per_coded_sample == 8
184 #if HAVE_BIGENDIAN
186 #else
188 #endif
189  return codec_id == apar->codec_id;
190  case FLV_CODECID_PCM_LE:
191  codec_id = bits_per_coded_sample == 8
194  return codec_id == apar->codec_id;
195  case FLV_CODECID_AAC:
196  return apar->codec_id == AV_CODEC_ID_AAC;
197  case FLV_CODECID_ADPCM:
198  return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
199  case FLV_CODECID_SPEEX:
200  return apar->codec_id == AV_CODEC_ID_SPEEX;
201  case FLV_CODECID_MP3:
202  return apar->codec_id == AV_CODEC_ID_MP3;
206  return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
208  return apar->sample_rate == 8000 &&
211  return apar->sample_rate == 8000 &&
213  default:
214  return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
215  }
216 }
217 
219  AVCodecParameters *apar, int flv_codecid)
220 {
221  switch (flv_codecid) {
222  // no distinction between S16 and S8 PCM codec flags
223  case FLV_CODECID_PCM:
224  apar->codec_id = apar->bits_per_coded_sample == 8
226 #if HAVE_BIGENDIAN
228 #else
230 #endif
231  break;
232  case FLV_CODECID_PCM_LE:
233  apar->codec_id = apar->bits_per_coded_sample == 8
236  break;
237  case FLV_CODECID_AAC:
238  apar->codec_id = AV_CODEC_ID_AAC;
239  break;
240  case FLV_CODECID_ADPCM:
242  break;
243  case FLV_CODECID_SPEEX:
244  apar->codec_id = AV_CODEC_ID_SPEEX;
245  apar->sample_rate = 16000;
246  break;
247  case FLV_CODECID_MP3:
248  apar->codec_id = AV_CODEC_ID_MP3;
250  break;
252  // in case metadata does not otherwise declare samplerate
253  apar->sample_rate = 8000;
255  break;
257  apar->sample_rate = 16000;
259  break;
262  break;
264  apar->sample_rate = 8000;
266  break;
268  apar->sample_rate = 8000;
270  break;
271  default:
272  avpriv_request_sample(s, "Audio codec (%x)",
273  flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
274  apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
275  }
276 }
277 
279 {
280  int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
281 
282  if (!vpar->codec_id && !vpar->codec_tag)
283  return 1;
284 
285  switch (flv_codecid) {
286  case FLV_CODECID_H263:
287  return vpar->codec_id == AV_CODEC_ID_FLV1;
288  case FLV_CODECID_SCREEN:
289  return vpar->codec_id == AV_CODEC_ID_FLASHSV;
290  case FLV_CODECID_SCREEN2:
291  return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
292  case FLV_CODECID_VP6:
293  return vpar->codec_id == AV_CODEC_ID_VP6F;
294  case FLV_CODECID_VP6A:
295  return vpar->codec_id == AV_CODEC_ID_VP6A;
296  case FLV_CODECID_H264:
297  return vpar->codec_id == AV_CODEC_ID_H264;
298  default:
299  return vpar->codec_tag == flv_codecid;
300  }
301 }
302 
304  int flv_codecid, int read)
305 {
306  int ret = 0;
307  AVCodecParameters *par = vstream->codecpar;
308  enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
309  switch (flv_codecid) {
310  case FLV_CODECID_H263:
311  par->codec_id = AV_CODEC_ID_FLV1;
312  break;
314  par->codec_id = AV_CODEC_ID_H263;
315  break; // Really mean it this time
316  case FLV_CODECID_SCREEN:
318  break;
319  case FLV_CODECID_SCREEN2:
321  break;
322  case FLV_CODECID_VP6:
323  par->codec_id = AV_CODEC_ID_VP6F;
324  case FLV_CODECID_VP6A:
325  if (flv_codecid == FLV_CODECID_VP6A)
326  par->codec_id = AV_CODEC_ID_VP6A;
327  if (read) {
328  if (par->extradata_size != 1) {
329  ff_alloc_extradata(par, 1);
330  }
331  if (par->extradata)
332  par->extradata[0] = avio_r8(s->pb);
333  else
334  avio_skip(s->pb, 1);
335  }
336  ret = 1; // 1 byte body size adjustment for flv_read_packet()
337  break;
338  case FLV_CODECID_H264:
339  par->codec_id = AV_CODEC_ID_H264;
341  ret = 3; // not 4, reading packet type will consume one byte
342  break;
343  case FLV_CODECID_MPEG4:
345  ret = 3;
346  break;
347  default:
348  avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
349  par->codec_tag = flv_codecid;
350  }
351 
352  if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
353  avpriv_request_sample(s, "Changing the codec id midstream");
354  return AVERROR_PATCHWELCOME;
355  }
356 
357  return ret;
358 }
359 
360 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
361 {
362  int ret;
363  int length = avio_rb16(ioc);
364  if (length >= buffsize) {
365  avio_skip(ioc, length);
366  return -1;
367  }
368 
369  ret = avio_read(ioc, buffer, length);
370  if (ret < 0)
371  return ret;
372  if (ret < length)
373  return AVERROR_INVALIDDATA;
374 
375  buffer[length] = '\0';
376 
377  return length;
378 }
379 
381 {
382  FLVContext *flv = s->priv_data;
383  unsigned int timeslen = 0, fileposlen = 0, i;
384  char str_val[256];
385  int64_t *times = NULL;
386  int64_t *filepositions = NULL;
387  int ret = AVERROR(ENOSYS);
388  int64_t initial_pos = avio_tell(ioc);
389 
390  if (flv->keyframe_count > 0) {
391  av_log(s, AV_LOG_DEBUG, "keyframes have been paresed\n");
392  return 0;
393  }
394  av_assert0(!flv->keyframe_times);
396 
397  if (s->flags & AVFMT_FLAG_IGNIDX)
398  return 0;
399 
400  while (avio_tell(ioc) < max_pos - 2 &&
401  amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
402  int64_t **current_array;
403  unsigned int arraylen;
404 
405  // Expect array object in context
406  if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
407  break;
408 
409  arraylen = avio_rb32(ioc);
410  if (arraylen>>28)
411  break;
412 
413  if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
414  current_array = &times;
415  timeslen = arraylen;
416  } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
417  !filepositions) {
418  current_array = &filepositions;
419  fileposlen = arraylen;
420  } else
421  // unexpected metatag inside keyframes, will not use such
422  // metadata for indexing
423  break;
424 
425  if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
426  ret = AVERROR(ENOMEM);
427  goto finish;
428  }
429 
430  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
431  double d;
432  if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
433  goto invalid;
434  d = av_int2double(avio_rb64(ioc));
435  if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
436  goto invalid;
437  if (current_array == &times && (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000))
438  goto invalid;
439  if (avio_feof(ioc))
440  goto invalid;
441  current_array[0][i] = d;
442  }
443  if (times && filepositions) {
444  // All done, exiting at a position allowing amf_parse_object
445  // to finish parsing the object
446  ret = 0;
447  break;
448  }
449  }
450 
451  if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
452  for (i = 0; i < FFMIN(2,fileposlen); i++) {
453  flv->validate_index[i].pos = filepositions[i];
454  flv->validate_index[i].dts = times[i] * 1000;
455  flv->validate_count = i + 1;
456  }
457  flv->keyframe_times = times;
459  flv->keyframe_count = timeslen;
460  times = NULL;
461  filepositions = NULL;
462  } else {
463 invalid:
464  av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
465  }
466 
467 finish:
468  av_freep(&times);
469  av_freep(&filepositions);
470  avio_seek(ioc, initial_pos, SEEK_SET);
471  return ret;
472 }
473 
475  AVStream *vstream, const char *key,
476  int64_t max_pos, int depth)
477 {
478  AVCodecParameters *apar, *vpar;
479  FLVContext *flv = s->priv_data;
480  AVIOContext *ioc;
481  AMFDataType amf_type;
482  char str_val[1024];
483  double num_val;
484 
485  if (depth > MAX_DEPTH)
486  return AVERROR_PATCHWELCOME;
487 
488  num_val = 0;
489  ioc = s->pb;
490  if (avio_feof(ioc))
491  return AVERROR_EOF;
492  amf_type = avio_r8(ioc);
493 
494  switch (amf_type) {
496  num_val = av_int2double(avio_rb64(ioc));
497  break;
498  case AMF_DATA_TYPE_BOOL:
499  num_val = avio_r8(ioc);
500  break;
502  if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
503  av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
504  return -1;
505  }
506  break;
508  if (key &&
509  (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
510  !strcmp(KEYFRAMES_TAG, key) && depth == 1)
511  if (parse_keyframes_index(s, ioc,
512  max_pos) < 0)
513  av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
514  else
516  while (avio_tell(ioc) < max_pos - 2 &&
517  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
518  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
519  depth + 1) < 0)
520  return -1; // if we couldn't skip, bomb out.
521  if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
522  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
523  return -1;
524  }
525  break;
526  case AMF_DATA_TYPE_NULL:
529  break; // these take up no additional space
531  {
532  unsigned v;
533  avio_skip(ioc, 4); // skip 32-bit max array index
534  while (avio_tell(ioc) < max_pos - 2 &&
535  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
536  // this is the only case in which we would want a nested
537  // parse to not skip over the object
538  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
539  depth + 1) < 0)
540  return -1;
541  v = avio_r8(ioc);
542  if (v != AMF_END_OF_OBJECT) {
543  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
544  return -1;
545  }
546  break;
547  }
548  case AMF_DATA_TYPE_ARRAY:
549  {
550  unsigned int arraylen, i;
551 
552  arraylen = avio_rb32(ioc);
553  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
554  if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
555  depth + 1) < 0)
556  return -1; // if we couldn't skip, bomb out.
557  }
558  break;
559  case AMF_DATA_TYPE_DATE:
560  avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
561  break;
562  default: // unsupported type, we couldn't skip
563  av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
564  return -1;
565  }
566 
567  if (key) {
568  apar = astream ? astream->codecpar : NULL;
569  vpar = vstream ? vstream->codecpar : NULL;
570 
571  // stream info doesn't live any deeper than the first object
572  if (depth == 1) {
573  if (amf_type == AMF_DATA_TYPE_NUMBER ||
574  amf_type == AMF_DATA_TYPE_BOOL) {
575  if (!strcmp(key, "duration"))
576  s->duration = num_val * AV_TIME_BASE;
577  else if (!strcmp(key, "videodatarate") &&
578  0 <= (int)(num_val * 1024.0))
579  flv->video_bit_rate = num_val * 1024.0;
580  else if (!strcmp(key, "audiodatarate") &&
581  0 <= (int)(num_val * 1024.0))
582  flv->audio_bit_rate = num_val * 1024.0;
583  else if (!strcmp(key, "datastream")) {
585  if (!st)
586  return AVERROR(ENOMEM);
588  } else if (!strcmp(key, "framerate")) {
589  flv->framerate = av_d2q(num_val, 1000);
590  if (vstream)
591  vstream->avg_frame_rate = flv->framerate;
592  } else if (flv->trust_metadata) {
593  if (!strcmp(key, "videocodecid") && vpar) {
594  int ret = flv_set_video_codec(s, vstream, num_val, 0);
595  if (ret < 0)
596  return ret;
597  } else if (!strcmp(key, "audiocodecid") && apar) {
598  int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
599  flv_set_audio_codec(s, astream, apar, id);
600  } else if (!strcmp(key, "audiosamplerate") && apar) {
601  apar->sample_rate = num_val;
602  } else if (!strcmp(key, "audiosamplesize") && apar) {
603  apar->bits_per_coded_sample = num_val;
604  } else if (!strcmp(key, "stereo") && apar) {
605  apar->channels = num_val + 1;
606  apar->channel_layout = apar->channels == 2 ?
609  } else if (!strcmp(key, "width") && vpar) {
610  vpar->width = num_val;
611  } else if (!strcmp(key, "height") && vpar) {
612  vpar->height = num_val;
613  }
614  }
615  }
616  if (amf_type == AMF_DATA_TYPE_STRING) {
617  if (!strcmp(key, "encoder")) {
618  int version = -1;
619  if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
620  if (version > 0 && version <= 655)
621  flv->broken_sizes = 1;
622  }
623  } else if (!strcmp(key, "metadatacreator")) {
624  if ( !strcmp (str_val, "MEGA")
625  || !strncmp(str_val, "FlixEngine", 10))
626  flv->broken_sizes = 1;
627  }
628  }
629  }
630 
631  if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
632  ((!apar && !strcmp(key, "audiocodecid")) ||
633  (!vpar && !strcmp(key, "videocodecid"))))
634  s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
635 
636  if ((!strcmp(key, "duration") ||
637  !strcmp(key, "filesize") ||
638  !strcmp(key, "width") ||
639  !strcmp(key, "height") ||
640  !strcmp(key, "videodatarate") ||
641  !strcmp(key, "framerate") ||
642  !strcmp(key, "videocodecid") ||
643  !strcmp(key, "audiodatarate") ||
644  !strcmp(key, "audiosamplerate") ||
645  !strcmp(key, "audiosamplesize") ||
646  !strcmp(key, "stereo") ||
647  !strcmp(key, "audiocodecid") ||
648  !strcmp(key, "datastream")) && !flv->dump_full_metadata)
649  return 0;
650 
652  if (amf_type == AMF_DATA_TYPE_BOOL) {
653  av_strlcpy(str_val, num_val > 0 ? "true" : "false",
654  sizeof(str_val));
655  av_dict_set(&s->metadata, key, str_val, 0);
656  } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
657  snprintf(str_val, sizeof(str_val), "%.f", num_val);
658  av_dict_set(&s->metadata, key, str_val, 0);
659  } else if (amf_type == AMF_DATA_TYPE_STRING)
660  av_dict_set(&s->metadata, key, str_val, 0);
661  }
662 
663  return 0;
664 }
665 
666 #define TYPE_ONTEXTDATA 1
667 #define TYPE_ONCAPTION 2
668 #define TYPE_ONCAPTIONINFO 3
669 #define TYPE_UNKNOWN 9
670 
671 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
672 {
673  FLVContext *flv = s->priv_data;
675  AVStream *stream, *astream, *vstream;
676  AVStream av_unused *dstream;
677  AVIOContext *ioc;
678  int i;
679  char buffer[32];
680 
681  astream = NULL;
682  vstream = NULL;
683  dstream = NULL;
684  ioc = s->pb;
685 
686  // first object needs to be "onMetaData" string
687  type = avio_r8(ioc);
688  if (type != AMF_DATA_TYPE_STRING ||
689  amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
690  return TYPE_UNKNOWN;
691 
692  if (!strcmp(buffer, "onTextData"))
693  return TYPE_ONTEXTDATA;
694 
695  if (!strcmp(buffer, "onCaption"))
696  return TYPE_ONCAPTION;
697 
698  if (!strcmp(buffer, "onCaptionInfo"))
699  return TYPE_ONCAPTIONINFO;
700 
701  if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
702  av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
703  return TYPE_UNKNOWN;
704  }
705 
706  // find the streams now so that amf_parse_object doesn't need to do
707  // the lookup every time it is called.
708  for (i = 0; i < s->nb_streams; i++) {
709  stream = s->streams[i];
710  if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
711  vstream = stream;
713  } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
714  astream = stream;
715  if (flv->last_keyframe_stream_index == -1)
717  }
718  else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
719  dstream = stream;
720  }
721 
722  // parse the second object (we want a mixed array)
723  if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
724  return -1;
725 
726  return 0;
727 }
728 
730 {
731  int flags;
732  FLVContext *flv = s->priv_data;
733  int offset;
734  int pre_tag_size = 0;
735 
736  avio_skip(s->pb, 4);
737  flags = avio_r8(s->pb);
738 
740 
742 
743  offset = avio_rb32(s->pb);
744  avio_seek(s->pb, offset, SEEK_SET);
745 
746  /* Annex E. The FLV File Format
747  * E.3 TheFLVFileBody
748  * Field Type Comment
749  * PreviousTagSize0 UI32 Always 0
750  * */
751  pre_tag_size = avio_rb32(s->pb);
752  if (pre_tag_size) {
753  av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
754  }
755 
756  s->start_time = 0;
757  flv->sum_flv_tag_size = 0;
758  flv->last_keyframe_stream_index = -1;
759 
760  return 0;
761 }
762 
764 {
765  int i;
766  FLVContext *flv = s->priv_data;
767  for (i=0; i<FLV_STREAM_TYPE_NB; i++)
768  av_freep(&flv->new_extradata[i]);
769  av_freep(&flv->keyframe_times);
771  return 0;
772 }
773 
775 {
776  if (!size)
777  return 0;
778 
779  av_freep(&st->codecpar->extradata);
780  if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
781  return AVERROR(ENOMEM);
782  st->internal->need_context_update = 1;
783  return 0;
784 }
785 
786 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
787  int size)
788 {
789  if (!size)
790  return 0;
791 
792  av_free(flv->new_extradata[stream]);
793  flv->new_extradata[stream] = av_mallocz(size +
795  if (!flv->new_extradata[stream])
796  return AVERROR(ENOMEM);
797  flv->new_extradata_size[stream] = size;
798  avio_read(pb, flv->new_extradata[stream], size);
799  return 0;
800 }
801 
802 static void clear_index_entries(AVFormatContext *s, int64_t pos)
803 {
804  int i, j, out;
806  "Found invalid index entries, clearing the index.\n");
807  for (i = 0; i < s->nb_streams; i++) {
808  AVStream *st = s->streams[i];
809  /* Remove all index entries that point to >= pos */
810  out = 0;
811  for (j = 0; j < st->nb_index_entries; j++)
812  if (st->index_entries[j].pos < pos)
813  st->index_entries[out++] = st->index_entries[j];
814  st->nb_index_entries = out;
815  }
816 }
817 
818 static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
819 {
820  int nb = -1, ret, parse_name = 1;
821 
822  if (depth > MAX_DEPTH)
823  return AVERROR_PATCHWELCOME;
824 
825  if (avio_feof(pb))
826  return AVERROR_EOF;
827 
828  switch (type) {
830  avio_skip(pb, 8);
831  break;
832  case AMF_DATA_TYPE_BOOL:
833  avio_skip(pb, 1);
834  break;
836  avio_skip(pb, avio_rb16(pb));
837  break;
838  case AMF_DATA_TYPE_ARRAY:
839  parse_name = 0;
841  nb = avio_rb32(pb);
842  if (nb < 0)
843  return AVERROR_INVALIDDATA;
845  while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
846  if (parse_name) {
847  int size = avio_rb16(pb);
848  if (!size) {
849  avio_skip(pb, 1);
850  break;
851  }
852  avio_skip(pb, size);
853  }
854  if ((ret = amf_skip_tag(pb, avio_r8(pb), depth + 1)) < 0)
855  return ret;
856  }
857  break;
858  case AMF_DATA_TYPE_NULL:
860  break;
861  default:
862  return AVERROR_INVALIDDATA;
863  }
864  return 0;
865 }
866 
868  int64_t dts, int64_t next)
869 {
870  AVIOContext *pb = s->pb;
871  AVStream *st = NULL;
872  char buf[20];
873  int ret = AVERROR_INVALIDDATA;
874  int i, length = -1;
875  int array = 0;
876 
877  switch (avio_r8(pb)) {
878  case AMF_DATA_TYPE_ARRAY:
879  array = 1;
881  avio_seek(pb, 4, SEEK_CUR);
883  break;
884  default:
885  goto skip;
886  }
887 
888  while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
889  AMFDataType type = avio_r8(pb);
890  if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
891  length = avio_rb16(pb);
892  ret = av_get_packet(pb, pkt, length);
893  if (ret < 0)
894  goto skip;
895  else
896  break;
897  } else {
898  if ((ret = amf_skip_tag(pb, type, 0)) < 0)
899  goto skip;
900  }
901  }
902 
903  if (length < 0) {
904  ret = AVERROR_INVALIDDATA;
905  goto skip;
906  }
907 
908  for (i = 0; i < s->nb_streams; i++) {
909  st = s->streams[i];
911  break;
912  }
913 
914  if (i == s->nb_streams) {
916  if (!st)
917  return AVERROR(ENOMEM);
919  }
920 
921  pkt->dts = dts;
922  pkt->pts = dts;
923  pkt->size = ret;
924 
925  pkt->stream_index = st->index;
926  pkt->flags |= AV_PKT_FLAG_KEY;
927 
928 skip:
929  avio_seek(s->pb, next + 4, SEEK_SET);
930 
931  return ret;
932 }
933 
935 {
936  FLVContext *flv = s->priv_data;
937  int64_t i;
938  int64_t pos = avio_tell(s->pb);
939 
940  for (i=0; !avio_feof(s->pb); i++) {
941  int j = i & (RESYNC_BUFFER_SIZE-1);
942  int j1 = j + RESYNC_BUFFER_SIZE;
943  flv->resync_buffer[j ] =
944  flv->resync_buffer[j1] = avio_r8(s->pb);
945 
946  if (i > 22) {
947  unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
948  if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
949  unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
950  unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
951  if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
952  unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
953  if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
954  avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
955  return 1;
956  }
957  }
958  }
959  }
960  }
961  return AVERROR_EOF;
962 }
963 
965 {
966  FLVContext *flv = s->priv_data;
967  int ret, i, size, flags;
968  enum FlvTagType type;
969  int stream_type=-1;
970  int64_t next, pos, meta_pos;
971  int64_t dts, pts = AV_NOPTS_VALUE;
972  int av_uninit(channels);
973  int av_uninit(sample_rate);
974  AVStream *st = NULL;
975  int last = -1;
976  int orig_size;
977 
978 retry:
979  /* pkt size is repeated at end. skip it */
980  pos = avio_tell(s->pb);
981  type = (avio_r8(s->pb) & 0x1F);
982  orig_size =
983  size = avio_rb24(s->pb);
984  flv->sum_flv_tag_size += size + 11;
985  dts = avio_rb24(s->pb);
986  dts |= (unsigned)avio_r8(s->pb) << 24;
987  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));
988  if (avio_feof(s->pb))
989  return AVERROR_EOF;
990  avio_skip(s->pb, 3); /* stream id, always 0 */
991  flags = 0;
992 
993  if (flv->validate_next < flv->validate_count) {
994  int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
995  if (pos == validate_pos) {
996  if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
998  flv->validate_next++;
999  } else {
1000  clear_index_entries(s, validate_pos);
1001  flv->validate_count = 0;
1002  }
1003  } else if (pos > validate_pos) {
1004  clear_index_entries(s, validate_pos);
1005  flv->validate_count = 0;
1006  }
1007  }
1008 
1009  if (size == 0) {
1010  ret = FFERROR_REDO;
1011  goto leave;
1012  }
1013 
1014  next = size + avio_tell(s->pb);
1015 
1016  if (type == FLV_TAG_TYPE_AUDIO) {
1017  stream_type = FLV_STREAM_TYPE_AUDIO;
1018  flags = avio_r8(s->pb);
1019  size--;
1020  } else if (type == FLV_TAG_TYPE_VIDEO) {
1021  stream_type = FLV_STREAM_TYPE_VIDEO;
1022  flags = avio_r8(s->pb);
1023  size--;
1025  goto skip;
1026  } else if (type == FLV_TAG_TYPE_META) {
1027  stream_type=FLV_STREAM_TYPE_DATA;
1028  if (size > 13 + 1 + 4) { // Header-type metadata stuff
1029  int type;
1030  meta_pos = avio_tell(s->pb);
1031  type = flv_read_metabody(s, next);
1032  if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
1033  if (type < 0 && flv->validate_count &&
1034  flv->validate_index[0].pos > next &&
1035  flv->validate_index[0].pos - 4 < next
1036  ) {
1037  av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
1038  next = flv->validate_index[0].pos - 4;
1039  }
1040  goto skip;
1041  } else if (type == TYPE_ONTEXTDATA) {
1042  avpriv_request_sample(s, "OnTextData packet");
1043  return flv_data_packet(s, pkt, dts, next);
1044  } else if (type == TYPE_ONCAPTION) {
1045  return flv_data_packet(s, pkt, dts, next);
1046  }
1047  avio_seek(s->pb, meta_pos, SEEK_SET);
1048  }
1049  } else {
1050  av_log(s, AV_LOG_DEBUG,
1051  "Skipping flv packet: type %d, size %d, flags %d.\n",
1052  type, size, flags);
1053 skip:
1054  if (avio_seek(s->pb, next, SEEK_SET) != next) {
1055  // This can happen if flv_read_metabody above read past
1056  // next, on a non-seekable input, and the preceding data has
1057  // been flushed out from the IO buffer.
1058  av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
1059  return AVERROR_INVALIDDATA;
1060  }
1061  ret = FFERROR_REDO;
1062  goto leave;
1063  }
1064 
1065  /* skip empty data packets */
1066  if (!size) {
1067  ret = FFERROR_REDO;
1068  goto leave;
1069  }
1070 
1071  /* now find stream */
1072  for (i = 0; i < s->nb_streams; i++) {
1073  st = s->streams[i];
1074  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1075  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1076  (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
1077  break;
1078  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1079  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1080  (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
1081  break;
1082  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1084  break;
1085  }
1086  }
1087  if (i == s->nb_streams) {
1088  static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
1089  st = create_stream(s, stream_types[stream_type]);
1090  if (!st)
1091  return AVERROR(ENOMEM);
1092 
1093  }
1094  av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1095 
1096  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1097  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1098  stream_type == FLV_STREAM_TYPE_AUDIO))
1099  av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
1100 
1101  if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
1102  ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
1103  || st->discard >= AVDISCARD_ALL
1104  ) {
1105  avio_seek(s->pb, next, SEEK_SET);
1106  ret = FFERROR_REDO;
1107  goto leave;
1108  }
1109 
1110  // if not streamed and no duration from metadata then seek to end to find
1111  // the duration from the timestamps
1112  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1113  (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1114  !flv->searched_for_end) {
1115  int size;
1116  const int64_t pos = avio_tell(s->pb);
1117  // Read the last 4 bytes of the file, this should be the size of the
1118  // previous FLV tag. Use the timestamp of its payload as duration.
1119  int64_t fsize = avio_size(s->pb);
1120 retry_duration:
1121  avio_seek(s->pb, fsize - 4, SEEK_SET);
1122  size = avio_rb32(s->pb);
1123  if (size > 0 && size < fsize) {
1124  // Seek to the start of the last FLV tag at position (fsize - 4 - size)
1125  // but skip the byte indicating the type.
1126  avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
1127  if (size == avio_rb24(s->pb) + 11) {
1128  uint32_t ts = avio_rb24(s->pb);
1129  ts |= (unsigned)avio_r8(s->pb) << 24;
1130  if (ts)
1131  s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1132  else if (fsize >= 8 && fsize - 8 >= size) {
1133  fsize -= size+4;
1134  goto retry_duration;
1135  }
1136  }
1137  }
1138 
1139  avio_seek(s->pb, pos, SEEK_SET);
1140  flv->searched_for_end = 1;
1141  }
1142 
1143  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1144  int bits_per_coded_sample;
1145  channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1146  sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1148  bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1149  if (!st->codecpar->channels || !st->codecpar->sample_rate ||
1151  st->codecpar->channels = channels;
1152  st->codecpar->channel_layout = channels == 1
1156  st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1157  }
1158  if (!st->codecpar->codec_id) {
1159  flv_set_audio_codec(s, st, st->codecpar,
1160  flags & FLV_AUDIO_CODECID_MASK);
1161  flv->last_sample_rate =
1163  flv->last_channels =
1164  channels = st->codecpar->channels;
1165  } else {
1167  if (!par) {
1168  ret = AVERROR(ENOMEM);
1169  goto leave;
1170  }
1171  par->sample_rate = sample_rate;
1172  par->bits_per_coded_sample = bits_per_coded_sample;
1173  flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1174  sample_rate = par->sample_rate;
1176  }
1177  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1178  int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1179  if (ret < 0)
1180  return ret;
1181  size -= ret;
1182  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1184  }
1185 
1186  if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1189  int type = avio_r8(s->pb);
1190  size--;
1191 
1192  if (size < 0) {
1193  ret = AVERROR_INVALIDDATA;
1194  goto leave;
1195  }
1196 
1198  // sign extension
1199  int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1200  pts = dts + cts;
1201  if (cts < 0) { // dts might be wrong
1202  if (!flv->wrong_dts)
1204  "Negative cts, previous timestamps might be wrong.\n");
1205  flv->wrong_dts = 1;
1206  } else if (FFABS(dts - pts) > 1000*60*15) {
1208  "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1209  dts = pts = AV_NOPTS_VALUE;
1210  }
1211  }
1212  if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1213  st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1214  AVDictionaryEntry *t;
1215 
1216  if (st->codecpar->extradata) {
1217  if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1218  return ret;
1219  ret = FFERROR_REDO;
1220  goto leave;
1221  }
1222  if ((ret = flv_get_extradata(s, st, size)) < 0)
1223  return ret;
1224 
1225  /* Workaround for buggy Omnia A/XE encoder */
1226  t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1227  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1228  st->codecpar->extradata_size = 2;
1229 
1230  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && 0) {
1231  MPEG4AudioConfig cfg;
1232 
1234  st->codecpar->extradata_size * 8, 1) >= 0) {
1235  st->codecpar->channels = cfg.channels;
1236  st->codecpar->channel_layout = 0;
1237  if (cfg.ext_sample_rate)
1239  else
1240  st->codecpar->sample_rate = cfg.sample_rate;
1241  av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
1242  st->codecpar->channels, st->codecpar->sample_rate);
1243  }
1244  }
1245 
1246  ret = FFERROR_REDO;
1247  goto leave;
1248  }
1249  }
1250 
1251  /* skip empty data packets */
1252  if (!size) {
1253  ret = FFERROR_REDO;
1254  goto leave;
1255  }
1256 
1257  ret = av_get_packet(s->pb, pkt, size);
1258  if (ret < 0)
1259  return ret;
1260  pkt->dts = dts;
1261  pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1262  pkt->stream_index = st->index;
1263  pkt->pos = pos;
1264  if (flv->new_extradata[stream_type]) {
1266  flv->new_extradata_size[stream_type]);
1267  if (side) {
1268  memcpy(side, flv->new_extradata[stream_type],
1269  flv->new_extradata_size[stream_type]);
1270  av_freep(&flv->new_extradata[stream_type]);
1271  flv->new_extradata_size[stream_type] = 0;
1272  }
1273  }
1274  if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1275  (sample_rate != flv->last_sample_rate ||
1276  channels != flv->last_channels)) {
1278  flv->last_channels = channels;
1279  ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1280  }
1281 
1282  if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1283  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1284  stream_type == FLV_STREAM_TYPE_DATA)
1285  pkt->flags |= AV_PKT_FLAG_KEY;
1286 
1287 leave:
1288  last = avio_rb32(s->pb);
1289  if (!flv->trust_datasize) {
1290  if (last != orig_size + 11 && last != orig_size + 10 &&
1291  !avio_feof(s->pb) &&
1292  (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1293  !flv->broken_sizes) {
1294  av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %d\n", last, orig_size + 11, flv->sum_flv_tag_size);
1295  avio_seek(s->pb, pos + 1, SEEK_SET);
1296  ret = resync(s);
1297  av_packet_unref(pkt);
1298  if (ret >= 0) {
1299  goto retry;
1300  }
1301  }
1302  }
1303  return ret;
1304 }
1305 
1306 static int flv_read_seek(AVFormatContext *s, int stream_index,
1307  int64_t ts, int flags)
1308 {
1309  FLVContext *flv = s->priv_data;
1310  flv->validate_count = 0;
1311  return avio_seek_time(s->pb, stream_index, ts, flags);
1312 }
1313 
1314 #define OFFSET(x) offsetof(FLVContext, x)
1315 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1316 static const AVOption options[] = {
1317  { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1318  { "flv_full_metadata", "Dump full metadata of the onMetadata", OFFSET(dump_full_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1319  { "flv_ignore_prevtag", "Ignore the Size of previous tag", OFFSET(trust_datasize), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1320  { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
1321  { NULL }
1322 };
1323 
1324 static const AVClass flv_class = {
1325  .class_name = "flvdec",
1326  .item_name = av_default_item_name,
1327  .option = options,
1328  .version = LIBAVUTIL_VERSION_INT,
1329 };
1330 
1332  .name = "flv",
1333  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1334  .priv_data_size = sizeof(FLVContext),
1335  .read_probe = flv_probe,
1340  .extensions = "flv",
1341  .priv_class = &flv_class,
1342 };
1343 
1344 static const AVClass live_flv_class = {
1345  .class_name = "live_flvdec",
1346  .item_name = av_default_item_name,
1347  .option = options,
1348  .version = LIBAVUTIL_VERSION_INT,
1349 };
1350 
1352  .name = "live_flv",
1353  .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1354  .priv_data_size = sizeof(FLVContext),
1360  .extensions = "flv",
1361  .priv_class = &live_flv_class,
1363 };
int missing_streams
Definition: flvdec.c:75
#define RESYNC_BUFFER_SIZE
Definition: flvdec.c:42
#define NULL
Definition: coverity.c:32
discard all frames except keyframes
Definition: avcodec.h:802
#define TYPE_ONCAPTIONINFO
Definition: flvdec.c:668
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:336
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:284
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:2048
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:85
static void clear_index_entries(AVFormatContext *s, int64_t pos)
Definition: flvdec.c:802
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
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:4899
int64_t pos
Definition: avformat.h:803
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
channels
Definition: aptx.c:30
#define TYPE_ONCAPTION
Definition: flvdec.c:667
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
#define KEYFRAMES_TIMESTAMP_TAG
Definition: flv.h:50
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecParameters *apar, int flv_codecid)
Definition: flvdec.c:218
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1103
static int flv_data_packet(AVFormatContext *s, AVPacket *pkt, int64_t dts, int64_t next)
Definition: flvdec.c:867
int searched_for_end
Definition: flvdec.c:62
int trust_datasize
trust data size of FLVTag
Definition: flvdec.c:49
#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:1666
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
const char * key
int version
Definition: avisynth_c.h:766
discard all
Definition: avcodec.h:803
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: flvdec.c:964
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:785
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1400
#define OFFSET(x)
Definition: flvdec.c:1314
static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: flvdec.c:1306
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3892
int64_t * keyframe_times
Definition: flvdec.c:73
Format I/O context.
Definition: avformat.h:1351
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:1484
Public dictionary API.
#define TYPE_ONTEXTDATA
Definition: flvdec.c:666
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:60
#define VD
Definition: flvdec.c:1315
int width
Video only.
Definition: avcodec.h:3966
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1295
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:2065
int64_t pos
Definition: flvdec.c:58
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:800
int wrong_dts
wrong dts due to negative cts
Definition: flvdec.c:51
#define AV_RB32
Definition: intreadwrite.h:130
enum AVStreamParseType need_parsing
Definition: avformat.h:1092
static const AVOption options[]
Definition: flvdec.c:1316
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4469
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data)...
Definition: internal.h:660
#define FLV_VIDEO_CODECID_MASK
Definition: flv.h:44
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
#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:345
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define FLV_AUDIO_SAMPLERATE_OFFSET
Definition: flv.h:33
disposable inter frame (H.263 only)
Definition: flv.h:117
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:313
AMFDataType
Definition: flv.h:122
static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
Definition: flvdec.c:818
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:921
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int trust_metadata
configure streams according onMetaData
Definition: flvdec.c:48
int validate_count
Definition: flvdec.c:61
#define FLV_AUDIO_SAMPLERATE_MASK
Definition: flv.h:41
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1537
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4002
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
Definition: flvdec.c:380
#define AVINDEX_KEYFRAME
Definition: avformat.h:810
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
static void add_keyframes_index(AVFormatContext *s)
Definition: flvdec.c:108
#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:1591
#define TYPE_UNKNOWN
Definition: flvdec.c:669
static int live_flv_probe(AVProbeData *p)
Definition: flvdec.c:103
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:471
discard all bidirectional frames
Definition: avcodec.h:800
#define AVERROR(e)
Definition: error.h:43
static int flv_read_close(AVFormatContext *s)
Definition: flvdec.c:763
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
static int flv_read_header(AVFormatContext *s)
Definition: flvdec.c:729
int64_t video_bit_rate
Definition: flvdec.c:71
#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:559
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
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:5031
static AVStream * create_stream(AVFormatContext *s, int codec_type)
Definition: flvdec.c:139
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int64_t * keyframe_filepositions
Definition: flvdec.c:74
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
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:2075
uint8_t * new_extradata[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:52
int broken_sizes
Definition: flvdec.c:66
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:64
static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read)
Definition: flvdec.c:303
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
video info/command frame
Definition: flv.h:119
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
int64_t dts
Definition: flvdec.c:57
Only parse headers, do not repack.
Definition: avformat.h:794
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int last_keyframe_stream_index
Definition: flvdec.c:69
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
#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:260
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:3299
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:793
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
int dump_full_metadata
Dump full metadata of the onMetadata.
Definition: flvdec.c:50
#define FFMIN(a, b)
Definition: common.h:96
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1543
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int64_t audio_bit_rate
Definition: flvdec.c:72
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
Definition: flvdec.c:167
int new_extradata_size[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:53
int32_t
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1667
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
static int flv_probe(AVProbeData *p)
Definition: flvdec.c:98
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
static const AVClass flv_class
Definition: flvdec.c:1324
struct FLVContext::@249 validate_index[2]
#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:548
Stream structure.
Definition: avformat.h:874
#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:1344
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
int sum_flv_tag_size
Definition: flvdec.c:67
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
Definition: flvdec.c:671
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1167
sample_rate
FLV common header.
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1231
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
AVRational framerate
Definition: flvdec.c:76
static int resync(AVFormatContext *s)
Definition: flvdec.c:934
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
static int probe(AVProbeData *p, int live)
Definition: flvdec.c:79
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:54
int nb_index_entries
Definition: avformat.h:1105
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:70
int last_channels
Definition: flvdec.c:55
cl_device_type type
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar) ...
Definition: internal.h:192
static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream, int size)
Definition: flvdec.c:786
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:448
#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
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1456
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:4010
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
full parsing and repack
Definition: avformat.h:793
Main libavformat public API header.
int
raw UTF-8 text
Definition: avcodec.h:653
#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:3317
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
Definition: flvdec.c:360
AVInputFormat ff_live_flv_demuxer
Definition: flvdec.c:1351
int flags
Definition: flvenc.c:120
AVInputFormat ff_flv_demuxer
Definition: flvdec.c:1331
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:159
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
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:1241
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:239
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth)
Definition: flvdec.c:474
void * priv_data
Format private data.
Definition: avformat.h:1379
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:289
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3942
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3914
int channels
Audio only.
Definition: avcodec.h:4006
#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:1444
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
Definition: flvdec.c:774
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1466
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:647
key frame (for AVC, a seekable frame)
Definition: flv.h:115
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
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:1447
#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:929
This structure stores compressed data.
Definition: avcodec.h:1422
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
Definition: flvdec.c:278
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define av_unused
Definition: attributes.h:125