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