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