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 typedef struct FLVContext {
45  const AVClass *class; ///< Class for private options.
46  int trust_metadata; ///< configure streams according onMetaData
47  int trust_datasize; ///< trust data size of FLVTag
48  int dump_full_metadata; ///< Dump full metadata of the onMetadata
49  int wrong_dts; ///< wrong dts due to negative cts
54  struct {
55  int64_t dts;
56  int64_t pos;
57  } validate_index[2];
61 
63 
66 
69  int64_t video_bit_rate;
70  int64_t audio_bit_rate;
71  int64_t *keyframe_times;
75  int64_t last_ts;
76  int64_t time_offset;
77  int64_t time_pos;
78 } FLVContext;
79 
80 /* AMF date type */
81 typedef struct amf_date {
82  double milliseconds;
83  int16_t timezone;
84 } amf_date;
85 
86 static int probe(const AVProbeData *p, int live)
87 {
88  const uint8_t *d = p->buf;
89  unsigned offset = AV_RB32(d + 5);
90 
91  if (d[0] == 'F' &&
92  d[1] == 'L' &&
93  d[2] == 'V' &&
94  d[3] < 5 && d[5] == 0 &&
95  offset + 100 < p->buf_size &&
96  offset > 8) {
97  int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
98 
99  if (live == is_live)
100  return AVPROBE_SCORE_MAX;
101  }
102  return 0;
103 }
104 
105 static int flv_probe(const AVProbeData *p)
106 {
107  return probe(p, 0);
108 }
109 
110 static int live_flv_probe(const AVProbeData *p)
111 {
112  return probe(p, 1);
113 }
114 
115 static int kux_probe(const AVProbeData *p)
116 {
117  const uint8_t *d = p->buf;
118 
119  if (d[0] == 'K' &&
120  d[1] == 'D' &&
121  d[2] == 'K' &&
122  d[3] == 0 &&
123  d[4] == 0) {
124  return AVPROBE_SCORE_EXTENSION + 1;
125  }
126  return 0;
127 }
128 
130 {
131  FLVContext *flv = s->priv_data;
132  AVStream *stream = NULL;
133  unsigned int i = 0;
134 
135  if (flv->last_keyframe_stream_index < 0) {
136  av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
137  return;
138  }
139 
141  stream = s->streams[flv->last_keyframe_stream_index];
142 
143  if (stream->internal->nb_index_entries == 0) {
144  for (i = 0; i < flv->keyframe_count; i++) {
145  av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
146  flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
148  flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
149  }
150  } else
151  av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
152 
153  if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
154  av_freep(&flv->keyframe_times);
156  flv->keyframe_count = 0;
157  }
158 }
159 
161 {
162  FLVContext *flv = s->priv_data;
164  if (!st)
165  return NULL;
167  if (s->nb_streams>=3 ||( s->nb_streams==2
173  if (codec_type == AVMEDIA_TYPE_AUDIO) {
174  st->codecpar->bit_rate = flv->audio_bit_rate;
176  }
177  if (codec_type == AVMEDIA_TYPE_VIDEO) {
178  st->codecpar->bit_rate = flv->video_bit_rate;
180  st->avg_frame_rate = flv->framerate;
181  }
182 
183 
184  avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
187  return st;
188 }
189 
191 {
192  int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
193  int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
194  int codec_id;
195 
196  if (!apar->codec_id && !apar->codec_tag)
197  return 1;
198 
199  if (apar->bits_per_coded_sample != bits_per_coded_sample)
200  return 0;
201 
202  switch (flv_codecid) {
203  // no distinction between S16 and S8 PCM codec flags
204  case FLV_CODECID_PCM:
205  codec_id = bits_per_coded_sample == 8
207 #if HAVE_BIGENDIAN
209 #else
211 #endif
212  return codec_id == apar->codec_id;
213  case FLV_CODECID_PCM_LE:
214  codec_id = bits_per_coded_sample == 8
217  return codec_id == apar->codec_id;
218  case FLV_CODECID_AAC:
219  return apar->codec_id == AV_CODEC_ID_AAC;
220  case FLV_CODECID_ADPCM:
221  return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
222  case FLV_CODECID_SPEEX:
223  return apar->codec_id == AV_CODEC_ID_SPEEX;
224  case FLV_CODECID_MP3:
225  return apar->codec_id == AV_CODEC_ID_MP3;
229  return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
231  return apar->sample_rate == 8000 &&
234  return apar->sample_rate == 8000 &&
236  default:
237  return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
238  }
239 }
240 
242  AVCodecParameters *apar, int flv_codecid)
243 {
244  switch (flv_codecid) {
245  // no distinction between S16 and S8 PCM codec flags
246  case FLV_CODECID_PCM:
247  apar->codec_id = apar->bits_per_coded_sample == 8
249 #if HAVE_BIGENDIAN
251 #else
253 #endif
254  break;
255  case FLV_CODECID_PCM_LE:
256  apar->codec_id = apar->bits_per_coded_sample == 8
259  break;
260  case FLV_CODECID_AAC:
261  apar->codec_id = AV_CODEC_ID_AAC;
262  break;
263  case FLV_CODECID_ADPCM:
265  break;
266  case FLV_CODECID_SPEEX:
267  apar->codec_id = AV_CODEC_ID_SPEEX;
268  apar->sample_rate = 16000;
269  break;
270  case FLV_CODECID_MP3:
271  apar->codec_id = AV_CODEC_ID_MP3;
273  break;
275  // in case metadata does not otherwise declare samplerate
276  apar->sample_rate = 8000;
278  break;
280  apar->sample_rate = 16000;
282  break;
285  break;
287  apar->sample_rate = 8000;
289  break;
291  apar->sample_rate = 8000;
293  break;
294  default:
295  avpriv_request_sample(s, "Audio codec (%x)",
296  flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
297  apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
298  }
299 }
300 
302 {
303  int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
304 
305  if (!vpar->codec_id && !vpar->codec_tag)
306  return 1;
307 
308  switch (flv_codecid) {
309  case FLV_CODECID_H263:
310  return vpar->codec_id == AV_CODEC_ID_FLV1;
311  case FLV_CODECID_SCREEN:
312  return vpar->codec_id == AV_CODEC_ID_FLASHSV;
313  case FLV_CODECID_SCREEN2:
314  return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
315  case FLV_CODECID_VP6:
316  return vpar->codec_id == AV_CODEC_ID_VP6F;
317  case FLV_CODECID_VP6A:
318  return vpar->codec_id == AV_CODEC_ID_VP6A;
319  case FLV_CODECID_H264:
320  return vpar->codec_id == AV_CODEC_ID_H264;
321  default:
322  return vpar->codec_tag == flv_codecid;
323  }
324 }
325 
327  int flv_codecid, int read)
328 {
329  int ret = 0;
330  AVCodecParameters *par = vstream->codecpar;
331  enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
332  switch (flv_codecid) {
333  case FLV_CODECID_H263:
334  par->codec_id = AV_CODEC_ID_FLV1;
335  break;
337  par->codec_id = AV_CODEC_ID_H263;
338  break; // Really mean it this time
339  case FLV_CODECID_SCREEN:
341  break;
342  case FLV_CODECID_SCREEN2:
344  break;
345  case FLV_CODECID_VP6:
346  par->codec_id = AV_CODEC_ID_VP6F;
347  case FLV_CODECID_VP6A:
348  if (flv_codecid == FLV_CODECID_VP6A)
349  par->codec_id = AV_CODEC_ID_VP6A;
350  if (read) {
351  if (par->extradata_size != 1) {
352  ff_alloc_extradata(par, 1);
353  }
354  if (par->extradata)
355  par->extradata[0] = avio_r8(s->pb);
356  else
357  avio_skip(s->pb, 1);
358  }
359  ret = 1; // 1 byte body size adjustment for flv_read_packet()
360  break;
361  case FLV_CODECID_H264:
362  par->codec_id = AV_CODEC_ID_H264;
364  ret = 3; // not 4, reading packet type will consume one byte
365  break;
366  case FLV_CODECID_MPEG4:
368  ret = 3;
369  break;
370  default:
371  avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
372  par->codec_tag = flv_codecid;
373  }
374 
375  if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
376  avpriv_request_sample(s, "Changing the codec id midstream");
377  return AVERROR_PATCHWELCOME;
378  }
379 
380  return ret;
381 }
382 
383 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
384 {
385  int length = avio_rb16(ioc);
386  if (length >= buffsize) {
387  avio_skip(ioc, length);
388  return -1;
389  }
390 
391  avio_read(ioc, buffer, length);
392 
393  buffer[length] = '\0';
394 
395  return length;
396 }
397 
399 {
400  FLVContext *flv = s->priv_data;
401  unsigned int timeslen = 0, fileposlen = 0, i;
402  char str_val[256];
403  int64_t *times = NULL;
404  int64_t *filepositions = NULL;
405  int ret = AVERROR(ENOSYS);
406  int64_t initial_pos = avio_tell(ioc);
407 
408  if (flv->keyframe_count > 0) {
409  av_log(s, AV_LOG_DEBUG, "keyframes have been parsed\n");
410  return 0;
411  }
412  av_assert0(!flv->keyframe_times);
414 
415  if (s->flags & AVFMT_FLAG_IGNIDX)
416  return 0;
417 
418  while (avio_tell(ioc) < max_pos - 2 &&
419  amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
420  int64_t **current_array;
421  unsigned int arraylen;
422 
423  // Expect array object in context
424  if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
425  break;
426 
427  arraylen = avio_rb32(ioc);
428  if (arraylen>>28)
429  break;
430 
431  if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
432  current_array = &times;
433  timeslen = arraylen;
434  } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
435  !filepositions) {
436  current_array = &filepositions;
437  fileposlen = arraylen;
438  } else
439  // unexpected metatag inside keyframes, will not use such
440  // metadata for indexing
441  break;
442 
443  if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
444  ret = AVERROR(ENOMEM);
445  goto finish;
446  }
447 
448  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
449  if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
450  goto invalid;
451  current_array[0][i] = av_int2double(avio_rb64(ioc));
452  }
453  if (times && filepositions) {
454  // All done, exiting at a position allowing amf_parse_object
455  // to finish parsing the object
456  ret = 0;
457  break;
458  }
459  }
460 
461  if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
462  for (i = 0; i < FFMIN(2,fileposlen); i++) {
463  flv->validate_index[i].pos = filepositions[i];
464  flv->validate_index[i].dts = times[i] * 1000;
465  flv->validate_count = i + 1;
466  }
467  flv->keyframe_times = times;
469  flv->keyframe_count = timeslen;
470  times = NULL;
471  filepositions = NULL;
472  } else {
473 invalid:
474  av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
475  }
476 
477 finish:
478  av_freep(&times);
479  av_freep(&filepositions);
480  avio_seek(ioc, initial_pos, SEEK_SET);
481  return ret;
482 }
483 
485  AVStream *vstream, const char *key,
486  int64_t max_pos, int depth)
487 {
488  AVCodecParameters *apar, *vpar;
489  FLVContext *flv = s->priv_data;
490  AVIOContext *ioc;
491  AMFDataType amf_type;
492  char str_val[1024];
493  double num_val;
494  amf_date date;
495 
496  num_val = 0;
497  ioc = s->pb;
498  if (avio_feof(ioc))
499  return AVERROR_EOF;
500  amf_type = avio_r8(ioc);
501 
502  switch (amf_type) {
504  num_val = av_int2double(avio_rb64(ioc));
505  break;
506  case AMF_DATA_TYPE_BOOL:
507  num_val = avio_r8(ioc);
508  break;
510  if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
511  av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
512  return -1;
513  }
514  break;
516  if (key &&
517  (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
518  !strcmp(KEYFRAMES_TAG, key) && depth == 1)
519  if (parse_keyframes_index(s, ioc, 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") && strcmp(buffer, "|RtmpSampleAccess")) {
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  } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
737  dstream = stream;
738  }
739 
740  // parse the second object (we want a mixed array)
741  if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
742  return -1;
743 
744  return 0;
745 }
746 
748 {
749  int flags;
750  FLVContext *flv = s->priv_data;
751  int offset;
752  int pre_tag_size = 0;
753 
754  /* Actual FLV data at 0xe40000 in KUX file */
755  if(!strcmp(s->iformat->name, "kux"))
756  avio_skip(s->pb, 0xe40000);
757 
758  avio_skip(s->pb, 4);
759  flags = avio_r8(s->pb);
760 
762 
764 
765  offset = avio_rb32(s->pb);
766  avio_seek(s->pb, offset, SEEK_SET);
767 
768  /* Annex E. The FLV File Format
769  * E.3 TheFLVFileBody
770  * Field Type Comment
771  * PreviousTagSize0 UI32 Always 0
772  * */
773  pre_tag_size = avio_rb32(s->pb);
774  if (pre_tag_size) {
775  av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
776  }
777 
778  s->start_time = 0;
779  flv->sum_flv_tag_size = 0;
780  flv->last_keyframe_stream_index = -1;
781 
782  return 0;
783 }
784 
786 {
787  int i;
788  FLVContext *flv = s->priv_data;
789  for (i=0; i<FLV_STREAM_TYPE_NB; i++)
790  av_freep(&flv->new_extradata[i]);
791  av_freep(&flv->keyframe_times);
793  return 0;
794 }
795 
797 {
798  int ret;
799  if (!size)
800  return 0;
801 
802  if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
803  return ret;
804  st->internal->need_context_update = 1;
805  return 0;
806 }
807 
808 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
809  int size)
810 {
811  if (!size)
812  return 0;
813 
814  av_free(flv->new_extradata[stream]);
815  flv->new_extradata[stream] = av_mallocz(size +
817  if (!flv->new_extradata[stream])
818  return AVERROR(ENOMEM);
819  flv->new_extradata_size[stream] = size;
820  avio_read(pb, flv->new_extradata[stream], size);
821  return 0;
822 }
823 
824 static void clear_index_entries(AVFormatContext *s, int64_t pos)
825 {
826  int i, j, out;
828  "Found invalid index entries, clearing the index.\n");
829  for (i = 0; i < s->nb_streams; i++) {
830  AVStream *st = s->streams[i];
831  /* Remove all index entries that point to >= pos */
832  out = 0;
833  for (j = 0; j < st->internal->nb_index_entries; j++)
834  if (st->internal->index_entries[j].pos < pos)
835  st->internal->index_entries[out++] = st->internal->index_entries[j];
837  }
838 }
839 
841 {
842  int nb = -1, ret, parse_name = 1;
843 
844  switch (type) {
846  avio_skip(pb, 8);
847  break;
848  case AMF_DATA_TYPE_BOOL:
849  avio_skip(pb, 1);
850  break;
852  avio_skip(pb, avio_rb16(pb));
853  break;
854  case AMF_DATA_TYPE_ARRAY:
855  parse_name = 0;
857  nb = avio_rb32(pb);
859  while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
860  if (parse_name) {
861  int size = avio_rb16(pb);
862  if (!size) {
863  avio_skip(pb, 1);
864  break;
865  }
866  avio_skip(pb, size);
867  }
868  if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
869  return ret;
870  }
871  break;
872  case AMF_DATA_TYPE_NULL:
874  break;
875  default:
876  return AVERROR_INVALIDDATA;
877  }
878  return 0;
879 }
880 
882  int64_t dts, int64_t next)
883 {
884  AVIOContext *pb = s->pb;
885  AVStream *st = NULL;
886  char buf[20];
887  int ret = AVERROR_INVALIDDATA;
888  int i, length = -1;
889  int array = 0;
890 
891  switch (avio_r8(pb)) {
892  case AMF_DATA_TYPE_ARRAY:
893  array = 1;
895  avio_seek(pb, 4, SEEK_CUR);
897  break;
898  default:
899  goto skip;
900  }
901 
902  while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
903  AMFDataType type = avio_r8(pb);
904  if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
905  length = avio_rb16(pb);
906  ret = av_get_packet(pb, pkt, length);
907  if (ret < 0)
908  goto skip;
909  else
910  break;
911  } else {
912  if ((ret = amf_skip_tag(pb, type)) < 0)
913  goto skip;
914  }
915  }
916 
917  if (length < 0) {
918  ret = AVERROR_INVALIDDATA;
919  goto skip;
920  }
921 
922  for (i = 0; i < s->nb_streams; i++) {
923  st = s->streams[i];
925  break;
926  }
927 
928  if (i == s->nb_streams) {
930  if (!st)
931  return AVERROR(ENOMEM);
933  }
934 
935  pkt->dts = dts;
936  pkt->pts = dts;
937  pkt->size = ret;
938 
939  pkt->stream_index = st->index;
940  pkt->flags |= AV_PKT_FLAG_KEY;
941 
942 skip:
943  avio_seek(s->pb, next + 4, SEEK_SET);
944 
945  return ret;
946 }
947 
949 {
950  FLVContext *flv = s->priv_data;
951  int64_t i;
952  int64_t pos = avio_tell(s->pb);
953 
954  for (i=0; !avio_feof(s->pb); i++) {
955  int j = i & (RESYNC_BUFFER_SIZE-1);
956  int j1 = j + RESYNC_BUFFER_SIZE;
957  flv->resync_buffer[j ] =
958  flv->resync_buffer[j1] = avio_r8(s->pb);
959 
960  if (i >= 8 && pos) {
961  uint8_t *d = flv->resync_buffer + j1 - 8;
962  if (d[0] == 'F' &&
963  d[1] == 'L' &&
964  d[2] == 'V' &&
965  d[3] < 5 && d[5] == 0) {
966  av_log(s, AV_LOG_WARNING, "Concatenated FLV detected, might fail to demux, decode and seek %"PRId64"\n", flv->last_ts);
967  flv->time_offset = flv->last_ts + 1;
968  flv->time_pos = avio_tell(s->pb);
969  }
970  }
971 
972  if (i > 22) {
973  unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
974  if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
975  unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
976  unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
977  if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
978  unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
979  if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
980  avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
981  return 1;
982  }
983  }
984  }
985  }
986  }
987  return AVERROR_EOF;
988 }
989 
991 {
992  FLVContext *flv = s->priv_data;
993  int ret, i, size, flags;
994  enum FlvTagType type;
995  int stream_type=-1;
996  int64_t next, pos, meta_pos;
997  int64_t dts, pts = AV_NOPTS_VALUE;
998  int av_uninit(channels);
999  int av_uninit(sample_rate);
1000  AVStream *st = NULL;
1001  int last = -1;
1002  int orig_size;
1003 
1004 retry:
1005  /* pkt size is repeated at end. skip it */
1006  pos = avio_tell(s->pb);
1007  type = (avio_r8(s->pb) & 0x1F);
1008  orig_size =
1009  size = avio_rb24(s->pb);
1010  flv->sum_flv_tag_size += size + 11;
1011  dts = avio_rb24(s->pb);
1012  dts |= (unsigned)avio_r8(s->pb) << 24;
1013  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));
1014  if (avio_feof(s->pb))
1015  return AVERROR_EOF;
1016  avio_skip(s->pb, 3); /* stream id, always 0 */
1017  flags = 0;
1018 
1019  if (flv->validate_next < flv->validate_count) {
1020  int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
1021  if (pos == validate_pos) {
1022  if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
1024  flv->validate_next++;
1025  } else {
1026  clear_index_entries(s, validate_pos);
1027  flv->validate_count = 0;
1028  }
1029  } else if (pos > validate_pos) {
1030  clear_index_entries(s, validate_pos);
1031  flv->validate_count = 0;
1032  }
1033  }
1034 
1035  if (size == 0) {
1036  ret = FFERROR_REDO;
1037  goto leave;
1038  }
1039 
1040  next = size + avio_tell(s->pb);
1041 
1042  if (type == FLV_TAG_TYPE_AUDIO) {
1043  stream_type = FLV_STREAM_TYPE_AUDIO;
1044  flags = avio_r8(s->pb);
1045  size--;
1046  } else if (type == FLV_TAG_TYPE_VIDEO) {
1047  stream_type = FLV_STREAM_TYPE_VIDEO;
1048  flags = avio_r8(s->pb);
1049  size--;
1051  goto skip;
1052  } else if (type == FLV_TAG_TYPE_META) {
1053  stream_type=FLV_STREAM_TYPE_SUBTITLE;
1054  if (size > 13 + 1 + 4) { // Header-type metadata stuff
1055  int type;
1056  meta_pos = avio_tell(s->pb);
1057  type = flv_read_metabody(s, next);
1058  if (type == 0 && dts == 0 || type < 0) {
1060  flv->validate_index[0].pos > next &&
1061  flv->validate_index[0].pos - 4 < next) {
1062  av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
1063  next = flv->validate_index[0].pos - 4;
1064  }
1065  goto skip;
1066  } else if (type == TYPE_ONTEXTDATA) {
1067  avpriv_request_sample(s, "OnTextData packet");
1068  return flv_data_packet(s, pkt, dts, next);
1069  } else if (type == TYPE_ONCAPTION) {
1070  return flv_data_packet(s, pkt, dts, next);
1071  } else if (type == TYPE_UNKNOWN) {
1072  stream_type = FLV_STREAM_TYPE_DATA;
1073  }
1074  avio_seek(s->pb, meta_pos, SEEK_SET);
1075  }
1076  } else {
1077  av_log(s, AV_LOG_DEBUG,
1078  "Skipping flv packet: type %d, size %d, flags %d.\n",
1079  type, size, flags);
1080 skip:
1081  if (avio_seek(s->pb, next, SEEK_SET) != next) {
1082  // This can happen if flv_read_metabody above read past
1083  // next, on a non-seekable input, and the preceding data has
1084  // been flushed out from the IO buffer.
1085  av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
1086  return AVERROR_INVALIDDATA;
1087  }
1088  ret = FFERROR_REDO;
1089  goto leave;
1090  }
1091 
1092  /* skip empty data packets */
1093  if (!size) {
1094  ret = FFERROR_REDO;
1095  goto leave;
1096  }
1097 
1098  /* now find stream */
1099  for (i = 0; i < s->nb_streams; i++) {
1100  st = s->streams[i];
1101  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1102  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1103  (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
1104  break;
1105  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1106  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1107  (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
1108  break;
1109  } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1111  break;
1112  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1114  break;
1115  }
1116  }
1117  if (i == s->nb_streams) {
1119  st = create_stream(s, stream_types[stream_type]);
1120  if (!st)
1121  return AVERROR(ENOMEM);
1122  }
1123  av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1124 
1125  if (flv->time_pos <= pos) {
1126  dts += flv->time_offset;
1127  }
1128 
1129  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1130  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1131  stream_type == FLV_STREAM_TYPE_AUDIO))
1132  av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
1133 
1134  if ((st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || stream_type == FLV_STREAM_TYPE_AUDIO)) ||
1135  (st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && stream_type == FLV_STREAM_TYPE_VIDEO)) ||
1136  st->discard >= AVDISCARD_ALL) {
1137  avio_seek(s->pb, next, SEEK_SET);
1138  ret = FFERROR_REDO;
1139  goto leave;
1140  }
1141 
1142  // if not streamed and no duration from metadata then seek to end to find
1143  // the duration from the timestamps
1144  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1145  (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1146  !flv->searched_for_end) {
1147  int size;
1148  const int64_t pos = avio_tell(s->pb);
1149  // Read the last 4 bytes of the file, this should be the size of the
1150  // previous FLV tag. Use the timestamp of its payload as duration.
1151  int64_t fsize = avio_size(s->pb);
1152 retry_duration:
1153  avio_seek(s->pb, fsize - 4, SEEK_SET);
1154  size = avio_rb32(s->pb);
1155  if (size > 0 && size < fsize) {
1156  // Seek to the start of the last FLV tag at position (fsize - 4 - size)
1157  // but skip the byte indicating the type.
1158  avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
1159  if (size == avio_rb24(s->pb) + 11) {
1160  uint32_t ts = avio_rb24(s->pb);
1161  ts |= avio_r8(s->pb) << 24;
1162  if (ts)
1163  s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1164  else if (fsize >= 8 && fsize - 8 >= size) {
1165  fsize -= size+4;
1166  goto retry_duration;
1167  }
1168  }
1169  }
1170 
1171  avio_seek(s->pb, pos, SEEK_SET);
1172  flv->searched_for_end = 1;
1173  }
1174 
1175  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1176  int bits_per_coded_sample;
1177  channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1178  sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1180  bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1181  if (!st->codecpar->channels || !st->codecpar->sample_rate ||
1183  st->codecpar->channels = channels;
1184  st->codecpar->channel_layout = channels == 1
1188  st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1189  }
1190  if (!st->codecpar->codec_id) {
1191  flv_set_audio_codec(s, st, st->codecpar,
1192  flags & FLV_AUDIO_CODECID_MASK);
1193  flv->last_sample_rate =
1195  flv->last_channels =
1196  channels = st->codecpar->channels;
1197  } else {
1199  if (!par) {
1200  ret = AVERROR(ENOMEM);
1201  goto leave;
1202  }
1203  par->sample_rate = sample_rate;
1204  par->bits_per_coded_sample = bits_per_coded_sample;
1205  flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1206  sample_rate = par->sample_rate;
1208  }
1209  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1210  int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1211  if (ret < 0)
1212  return ret;
1213  size -= ret;
1214  } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1216  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1217  st->codecpar->codec_id = AV_CODEC_ID_NONE; // Opaque AMF data
1218  }
1219 
1220  if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1223  int type = avio_r8(s->pb);
1224  size--;
1225 
1226  if (size < 0) {
1227  ret = AVERROR_INVALIDDATA;
1228  goto leave;
1229  }
1230 
1232  // sign extension
1233  int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1234  pts = dts + cts;
1235  if (cts < 0) { // dts might be wrong
1236  if (!flv->wrong_dts)
1238  "Negative cts, previous timestamps might be wrong.\n");
1239  flv->wrong_dts = 1;
1240  } else if (FFABS(dts - pts) > 1000*60*15) {
1242  "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1243  dts = pts = AV_NOPTS_VALUE;
1244  }
1245  }
1246  if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1247  st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1248  AVDictionaryEntry *t;
1249 
1250  if (st->codecpar->extradata) {
1251  if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1252  return ret;
1253  ret = FFERROR_REDO;
1254  goto leave;
1255  }
1256  if ((ret = flv_get_extradata(s, st, size)) < 0)
1257  return ret;
1258 
1259  /* Workaround for buggy Omnia A/XE encoder */
1260  t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1261  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1262  st->codecpar->extradata_size = 2;
1263 
1264  ret = FFERROR_REDO;
1265  goto leave;
1266  }
1267  }
1268 
1269  /* skip empty data packets */
1270  if (!size) {
1271  ret = FFERROR_REDO;
1272  goto leave;
1273  }
1274 
1275  ret = av_get_packet(s->pb, pkt, size);
1276  if (ret < 0)
1277  return ret;
1278  pkt->dts = dts;
1279  pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1280  pkt->stream_index = st->index;
1281  pkt->pos = pos;
1282  if (flv->new_extradata[stream_type]) {
1284  flv->new_extradata[stream_type],
1285  flv->new_extradata_size[stream_type]);
1286  if (ret >= 0) {
1287  flv->new_extradata[stream_type] = NULL;
1288  flv->new_extradata_size[stream_type] = 0;
1289  }
1290  }
1291  if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1292  (sample_rate != flv->last_sample_rate ||
1293  channels != flv->last_channels)) {
1295  flv->last_channels = channels;
1296  ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1297  }
1298 
1299  if (stream_type == FLV_STREAM_TYPE_AUDIO ||
1300  (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1301  stream_type == FLV_STREAM_TYPE_SUBTITLE ||
1302  stream_type == FLV_STREAM_TYPE_DATA)
1303  pkt->flags |= AV_PKT_FLAG_KEY;
1304 
1305 leave:
1306  last = avio_rb32(s->pb);
1307  if (!flv->trust_datasize) {
1308  if (last != orig_size + 11 && last != orig_size + 10 &&
1309  !avio_feof(s->pb) &&
1310  (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1311  !flv->broken_sizes) {
1312  av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %d\n", last, orig_size + 11, flv->sum_flv_tag_size);
1313  avio_seek(s->pb, pos + 1, SEEK_SET);
1314  ret = resync(s);
1315  av_packet_unref(pkt);
1316  if (ret >= 0) {
1317  goto retry;
1318  }
1319  }
1320  }
1321 
1322  if (ret >= 0)
1323  flv->last_ts = pkt->dts;
1324 
1325  return ret;
1326 }
1327 
1328 static int flv_read_seek(AVFormatContext *s, int stream_index,
1329  int64_t ts, int flags)
1330 {
1331  FLVContext *flv = s->priv_data;
1332  flv->validate_count = 0;
1333  return avio_seek_time(s->pb, stream_index, ts, flags);
1334 }
1335 
1336 #define OFFSET(x) offsetof(FLVContext, x)
1337 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1338 static const AVOption options[] = {
1339  { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1340  { "flv_full_metadata", "Dump full metadata of the onMetadata", OFFSET(dump_full_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1341  { "flv_ignore_prevtag", "Ignore the Size of previous tag", OFFSET(trust_datasize), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1342  { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
1343  { NULL }
1344 };
1345 
1346 static const AVClass flv_class = {
1347  .class_name = "flvdec",
1348  .item_name = av_default_item_name,
1349  .option = options,
1350  .version = LIBAVUTIL_VERSION_INT,
1351 };
1352 
1354  .name = "flv",
1355  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1356  .priv_data_size = sizeof(FLVContext),
1357  .read_probe = flv_probe,
1362  .extensions = "flv",
1363  .priv_class = &flv_class,
1364 };
1365 
1366 static const AVClass live_flv_class = {
1367  .class_name = "live_flvdec",
1368  .item_name = av_default_item_name,
1369  .option = options,
1370  .version = LIBAVUTIL_VERSION_INT,
1371 };
1372 
1374  .name = "live_flv",
1375  .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1376  .priv_data_size = sizeof(FLVContext),
1382  .extensions = "flv",
1383  .priv_class = &live_flv_class,
1385 };
1386 
1387 static const AVClass kux_class = {
1388  .class_name = "kuxdec",
1389  .item_name = av_default_item_name,
1390  .option = options,
1391  .version = LIBAVUTIL_VERSION_INT,
1392 };
1393 
1395  .name = "kux",
1396  .long_name = NULL_IF_CONFIG_SMALL("KUX (YouKu)"),
1397  .priv_data_size = sizeof(FLVContext),
1398  .read_probe = kux_probe,
1403  .extensions = "kux",
1404  .priv_class = &kux_class,
1405 };
int missing_streams
Definition: flvdec.c:73
#define RESYNC_BUFFER_SIZE
Definition: flvdec.c:42
#define NULL
Definition: coverity.c:32
discard all frames except keyframes
Definition: avcodec.h:235
#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:346
version
Definition: libkvazaar.c:317
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:1984
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
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:824
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:4871
int64_t pos
Definition: avformat.h:809
#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
#define TYPE_ONCAPTION
Definition: flvdec.c:686
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:881
int size
Definition: packet.h:364
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecParameters *apar, int flv_codecid)
Definition: flvdec.c:241
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:881
int searched_for_end
Definition: flvdec.c:60
int trust_datasize
trust data size of FLVTag
Definition: flvdec.c:47
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:1560
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:990
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:770
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1288
#define OFFSET(x)
Definition: flvdec.c:1336
static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: flvdec.c:1328
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int64_t * keyframe_times
Definition: flvdec.c:71
Format I/O context.
Definition: avformat.h:1239
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:1394
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1372
Public dictionary API.
int64_t time_offset
Definition: flvdec.c:76
#define TYPE_ONTEXTDATA
Definition: flvdec.c:685
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
uint8_t
int validate_next
Definition: flvdec.c:58
#define VD
Definition: flvdec.c:1337
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:1183
AVOptions.
#define FLV_AUDIO_CODECID_MASK
Definition: flv.h:42
int64_t pos
Definition: flvdec.c:56
#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:49
enum AVStreamParseType need_parsing
Definition: avformat.h:1088
static const AVOption options[]
Definition: flvdec.c:1338
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4453
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data)...
Definition: internal.h:785
#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:1307
#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:1370
#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:305
AMFDataType
Definition: flv.h:123
ptrdiff_t size
Definition: opengl_enc.c:100
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:46
int validate_count
Definition: flvdec.c:59
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:1425
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:75
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:2088
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
Definition: flvdec.c:398
#define AVINDEX_KEYFRAME
Definition: avformat.h:816
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
static void add_keyframes_index(AVFormatContext *s)
Definition: flvdec.c:129
#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:1479
#define TYPE_UNKNOWN
Definition: flvdec.c:688
#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:785
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
static int flv_read_header(AVFormatContext *s)
Definition: flvdec.c:747
int64_t video_bit_rate
Definition: flvdec.c:69
ff_const59 struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1251
#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:419
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:5003
static const AVClass kux_class
Definition: flvdec.c:1387
static AVStream * create_stream(AVFormatContext *s, int codec_type)
Definition: flvdec.c:160
GLsizei GLsizei * length
Definition: opengl_enc.c:114
int64_t * keyframe_filepositions
Definition: flvdec.c:72
static int live_flv_probe(const AVProbeData *p)
Definition: flvdec.c:110
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:953
FLVFileposition * filepositions
Definition: flvenc.c:112
uint8_t * new_extradata[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:50
int broken_sizes
Definition: flvdec.c:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
uint8_t resync_buffer[2 *RESYNC_BUFFER_SIZE]
Definition: flvdec.c:62
static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read)
Definition: flvdec.c:326
struct FLVContext::@258 validate_index[2]
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:55
Only parse headers, do not repack.
Definition: avformat.h:800
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:67
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:228
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1295
int64_t time_pos
Definition: flvdec.c:77
#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:3260
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:48
#define FFMIN(a, b)
Definition: common.h:96
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1431
int64_t audio_bit_rate
Definition: flvdec.c:70
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
Definition: flvdec.c:190
int new_extradata_size[FLV_STREAM_TYPE_NB]
Definition: flvdec.c:51
int32_t
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1567
#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:1346
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:527
Stream structure.
Definition: avformat.h:880
#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:1366
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
int sum_flv_tag_size
Definition: flvdec.c:65
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
Definition: flvdec.c:690
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:1119
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:1281
AVRational framerate
Definition: flvdec.c:74
static int resync(AVFormatContext *s)
Definition: flvdec.c:948
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:52
static int probe(const AVProbeData *p, int live)
Definition: flvdec.c:86
Describe the class of an AVClass context structure.
Definition: log.h:67
static int kux_probe(const AVProbeData *p)
Definition: flvdec.c:115
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:68
int last_channels
Definition: flvdec.c:53
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar) ...
Definition: internal.h:189
static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream, int size)
Definition: flvdec.c:808
AVMediaType
Definition: avutil.h:199
#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:83
static int flv_probe(const AVProbeData *p)
Definition: flvdec.c:105
#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:230
#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:1344
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
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2078
full parsing and repack
Definition: avformat.h:799
Main libavformat public API header.
int
raw UTF-8 text
Definition: codec_id.h:519
#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:3278
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
Definition: flvdec.c:383
#define localtime_r
Definition: time_internal.h:46
AVInputFormat ff_live_flv_demuxer
Definition: flvdec.c:1373
int flags
Definition: flvenc.c:120
AVInputFormat ff_flv_demuxer
Definition: flvdec.c:1353
#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
static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
Definition: flvdec.c:840
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:484
void * priv_data
Format private data.
Definition: avformat.h:1267
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:291
video info/command frame
Definition: flv.h:120
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:82
#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:796
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1354
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:652
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
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:935
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:301
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