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