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