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 4bits: difference between encoded width and visible width
8  * - lower 4bits: 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 } FLVContext;
65 
66 static int probe(AVProbeData *p, int live)
67 {
68  const uint8_t *d = p->buf;
69  unsigned offset = AV_RB32(d + 5);
70 
71  if (d[0] == 'F' &&
72  d[1] == 'L' &&
73  d[2] == 'V' &&
74  d[3] < 5 && d[5] == 0 &&
75  offset + 100 < p->buf_size &&
76  offset > 8) {
77  int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
78 
79  if (live == is_live)
80  return AVPROBE_SCORE_MAX;
81  }
82  return 0;
83 }
84 
85 static int flv_probe(AVProbeData *p)
86 {
87  return probe(p, 0);
88 }
89 
91 {
92  return probe(p, 1);
93 }
94 
96 {
98  if (!st)
99  return NULL;
100  st->codec->codec_type = codec_type;
101  if (s->nb_streams>=3 ||( s->nb_streams==2
105 
106  avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
107  return st;
108 }
109 
110 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
111 {
112  int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
113  int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
114  int codec_id;
115 
116  if (!acodec->codec_id && !acodec->codec_tag)
117  return 1;
118 
119  if (acodec->bits_per_coded_sample != bits_per_coded_sample)
120  return 0;
121 
122  switch (flv_codecid) {
123  // no distinction between S16 and S8 PCM codec flags
124  case FLV_CODECID_PCM:
125  codec_id = bits_per_coded_sample == 8
127 #if HAVE_BIGENDIAN
129 #else
131 #endif
132  return codec_id == acodec->codec_id;
133  case FLV_CODECID_PCM_LE:
134  codec_id = bits_per_coded_sample == 8
137  return codec_id == acodec->codec_id;
138  case FLV_CODECID_AAC:
139  return acodec->codec_id == AV_CODEC_ID_AAC;
140  case FLV_CODECID_ADPCM:
141  return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
142  case FLV_CODECID_SPEEX:
143  return acodec->codec_id == AV_CODEC_ID_SPEEX;
144  case FLV_CODECID_MP3:
145  return acodec->codec_id == AV_CODEC_ID_MP3;
149  return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
151  return acodec->sample_rate == 8000 &&
152  acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
154  return acodec->sample_rate == 8000 &&
155  acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
156  default:
157  return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
158  }
159 }
160 
162  AVCodecContext *acodec, int flv_codecid)
163 {
164  switch (flv_codecid) {
165  // no distinction between S16 and S8 PCM codec flags
166  case FLV_CODECID_PCM:
167  acodec->codec_id = acodec->bits_per_coded_sample == 8
169 #if HAVE_BIGENDIAN
171 #else
173 #endif
174  break;
175  case FLV_CODECID_PCM_LE:
176  acodec->codec_id = acodec->bits_per_coded_sample == 8
179  break;
180  case FLV_CODECID_AAC:
181  acodec->codec_id = AV_CODEC_ID_AAC;
182  break;
183  case FLV_CODECID_ADPCM:
185  break;
186  case FLV_CODECID_SPEEX:
187  acodec->codec_id = AV_CODEC_ID_SPEEX;
188  acodec->sample_rate = 16000;
189  break;
190  case FLV_CODECID_MP3:
191  acodec->codec_id = AV_CODEC_ID_MP3;
193  break;
195  // in case metadata does not otherwise declare samplerate
196  acodec->sample_rate = 8000;
198  break;
200  acodec->sample_rate = 16000;
202  break;
205  break;
207  acodec->sample_rate = 8000;
209  break;
211  acodec->sample_rate = 8000;
212  acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
213  break;
214  default:
215  avpriv_request_sample(s, "Audio codec (%x)",
216  flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
217  acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
218  }
219 }
220 
221 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
222 {
223  int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
224 
225  if (!vcodec->codec_id && !vcodec->codec_tag)
226  return 1;
227 
228  switch (flv_codecid) {
229  case FLV_CODECID_H263:
230  return vcodec->codec_id == AV_CODEC_ID_FLV1;
231  case FLV_CODECID_SCREEN:
232  return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
233  case FLV_CODECID_SCREEN2:
234  return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
235  case FLV_CODECID_VP6:
236  return vcodec->codec_id == AV_CODEC_ID_VP6F;
237  case FLV_CODECID_VP6A:
238  return vcodec->codec_id == AV_CODEC_ID_VP6A;
239  case FLV_CODECID_H264:
240  return vcodec->codec_id == AV_CODEC_ID_H264;
241  default:
242  return vcodec->codec_tag == flv_codecid;
243  }
244 }
245 
247  int flv_codecid, int read)
248 {
249  AVCodecContext *vcodec = vstream->codec;
250  switch (flv_codecid) {
251  case FLV_CODECID_H263:
252  vcodec->codec_id = AV_CODEC_ID_FLV1;
253  break;
255  vcodec->codec_id = AV_CODEC_ID_H263;
256  break; // Really mean it this time
257  case FLV_CODECID_SCREEN:
258  vcodec->codec_id = AV_CODEC_ID_FLASHSV;
259  break;
260  case FLV_CODECID_SCREEN2:
261  vcodec->codec_id = AV_CODEC_ID_FLASHSV2;
262  break;
263  case FLV_CODECID_VP6:
264  vcodec->codec_id = AV_CODEC_ID_VP6F;
265  case FLV_CODECID_VP6A:
266  if (flv_codecid == FLV_CODECID_VP6A)
267  vcodec->codec_id = AV_CODEC_ID_VP6A;
268  if (read) {
269  if (vcodec->extradata_size != 1) {
270  ff_alloc_extradata(vcodec, 1);
271  }
272  if (vcodec->extradata)
273  vcodec->extradata[0] = avio_r8(s->pb);
274  else
275  avio_skip(s->pb, 1);
276  }
277  return 1; // 1 byte body size adjustment for flv_read_packet()
278  case FLV_CODECID_H264:
279  vcodec->codec_id = AV_CODEC_ID_H264;
281  return 3; // not 4, reading packet type will consume one byte
282  case FLV_CODECID_MPEG4:
283  vcodec->codec_id = AV_CODEC_ID_MPEG4;
284  return 3;
285  default:
286  avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
287  vcodec->codec_tag = flv_codecid;
288  }
289 
290  return 0;
291 }
292 
293 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
294 {
295  int length = avio_rb16(ioc);
296  if (length >= buffsize) {
297  avio_skip(ioc, length);
298  return -1;
299  }
300 
301  avio_read(ioc, buffer, length);
302 
303  buffer[length] = '\0';
304 
305  return length;
306 }
307 
309  AVStream *vstream, int64_t max_pos)
310 {
311  FLVContext *flv = s->priv_data;
312  unsigned int timeslen = 0, fileposlen = 0, i;
313  char str_val[256];
314  int64_t *times = NULL;
315  int64_t *filepositions = NULL;
316  int ret = AVERROR(ENOSYS);
317  int64_t initial_pos = avio_tell(ioc);
318 
319  if (vstream->nb_index_entries>0) {
320  av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
321  return 0;
322  }
323 
324  if (s->flags & AVFMT_FLAG_IGNIDX)
325  return 0;
326 
327  while (avio_tell(ioc) < max_pos - 2 &&
328  amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
329  int64_t **current_array;
330  unsigned int arraylen;
331 
332  // Expect array object in context
333  if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
334  break;
335 
336  arraylen = avio_rb32(ioc);
337  if (arraylen>>28)
338  break;
339 
340  if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
341  current_array = &times;
342  timeslen = arraylen;
343  } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
344  !filepositions) {
345  current_array = &filepositions;
346  fileposlen = arraylen;
347  } else
348  // unexpected metatag inside keyframes, will not use such
349  // metadata for indexing
350  break;
351 
352  if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
353  ret = AVERROR(ENOMEM);
354  goto finish;
355  }
356 
357  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
358  if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
359  goto invalid;
360  current_array[0][i] = av_int2double(avio_rb64(ioc));
361  }
362  if (times && filepositions) {
363  // All done, exiting at a position allowing amf_parse_object
364  // to finish parsing the object
365  ret = 0;
366  break;
367  }
368  }
369 
370  if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
371  for (i = 0; i < fileposlen; i++) {
372  av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
373  0, 0, AVINDEX_KEYFRAME);
374  if (i < 2) {
375  flv->validate_index[i].pos = filepositions[i];
376  flv->validate_index[i].dts = times[i] * 1000;
377  flv->validate_count = i + 1;
378  }
379  }
380  } else {
381 invalid:
382  av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
383  }
384 
385 finish:
386  av_freep(&times);
387  av_freep(&filepositions);
388  avio_seek(ioc, initial_pos, SEEK_SET);
389  return ret;
390 }
391 
393  AVStream *vstream, const char *key,
394  int64_t max_pos, int depth)
395 {
396  AVCodecContext *acodec, *vcodec;
397  FLVContext *flv = s->priv_data;
398  AVIOContext *ioc;
399  AMFDataType amf_type;
400  char str_val[1024];
401  double num_val;
402 
403  num_val = 0;
404  ioc = s->pb;
405  amf_type = avio_r8(ioc);
406 
407  switch (amf_type) {
409  num_val = av_int2double(avio_rb64(ioc));
410  break;
411  case AMF_DATA_TYPE_BOOL:
412  num_val = avio_r8(ioc);
413  break;
415  if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
416  av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
417  return -1;
418  }
419  break;
421  if ((vstream || astream) && key &&
422  ioc->seekable &&
423  !strcmp(KEYFRAMES_TAG, key) && depth == 1)
424  if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
425  max_pos) < 0)
426  av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
427 
428  while (avio_tell(ioc) < max_pos - 2 &&
429  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
430  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
431  depth + 1) < 0)
432  return -1; // if we couldn't skip, bomb out.
433  if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
434  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
435  return -1;
436  }
437  break;
438  case AMF_DATA_TYPE_NULL:
441  break; // these take up no additional space
443  {
444  unsigned v;
445  avio_skip(ioc, 4); // skip 32-bit max array index
446  while (avio_tell(ioc) < max_pos - 2 &&
447  amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
448  // this is the only case in which we would want a nested
449  // parse to not skip over the object
450  if (amf_parse_object(s, astream, vstream, str_val, max_pos,
451  depth + 1) < 0)
452  return -1;
453  v = avio_r8(ioc);
454  if (v != AMF_END_OF_OBJECT) {
455  av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
456  return -1;
457  }
458  break;
459  }
460  case AMF_DATA_TYPE_ARRAY:
461  {
462  unsigned int arraylen, i;
463 
464  arraylen = avio_rb32(ioc);
465  for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
466  if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
467  depth + 1) < 0)
468  return -1; // if we couldn't skip, bomb out.
469  }
470  break;
471  case AMF_DATA_TYPE_DATE:
472  avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
473  break;
474  default: // unsupported type, we couldn't skip
475  av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
476  return -1;
477  }
478 
479  if (key) {
480  acodec = astream ? astream->codec : NULL;
481  vcodec = vstream ? vstream->codec : NULL;
482 
483  // stream info doesn't live any deeper than the first object
484  if (depth == 1) {
485  if (amf_type == AMF_DATA_TYPE_NUMBER ||
486  amf_type == AMF_DATA_TYPE_BOOL) {
487  if (!strcmp(key, "duration"))
488  s->duration = num_val * AV_TIME_BASE;
489  else if (!strcmp(key, "videodatarate") && vcodec &&
490  0 <= (int)(num_val * 1024.0))
491  vcodec->bit_rate = num_val * 1024.0;
492  else if (!strcmp(key, "audiodatarate") && acodec &&
493  0 <= (int)(num_val * 1024.0))
494  acodec->bit_rate = num_val * 1024.0;
495  else if (!strcmp(key, "datastream")) {
497  if (!st)
498  return AVERROR(ENOMEM);
500  } else if (flv->trust_metadata) {
501  if (!strcmp(key, "videocodecid") && vcodec) {
502  flv_set_video_codec(s, vstream, num_val, 0);
503  } else if (!strcmp(key, "audiocodecid") && acodec) {
504  int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
505  flv_set_audio_codec(s, astream, acodec, id);
506  } else if (!strcmp(key, "audiosamplerate") && acodec) {
507  acodec->sample_rate = num_val;
508  } else if (!strcmp(key, "audiosamplesize") && acodec) {
509  acodec->bits_per_coded_sample = num_val;
510  } else if (!strcmp(key, "stereo") && acodec) {
511  acodec->channels = num_val + 1;
512  acodec->channel_layout = acodec->channels == 2 ?
515  } else if (!strcmp(key, "width") && vcodec) {
516  vcodec->width = num_val;
517  } else if (!strcmp(key, "height") && vcodec) {
518  vcodec->height = num_val;
519  }
520  }
521  }
522  if (amf_type == AMF_DATA_TYPE_STRING) {
523  if (!strcmp(key, "encoder")) {
524  int version = -1;
525  if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
526  if (version > 0 && version <= 655)
527  flv->broken_sizes = 1;
528  }
529  } else if (!strcmp(key, "metadatacreator") && !strcmp(str_val, "MEGA")) {
530  flv->broken_sizes = 1;
531  }
532  }
533  }
534 
535  if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
536  ((!acodec && !strcmp(key, "audiocodecid")) ||
537  (!vcodec && !strcmp(key, "videocodecid"))))
538  s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
539 
540  if (!strcmp(key, "duration") ||
541  !strcmp(key, "filesize") ||
542  !strcmp(key, "width") ||
543  !strcmp(key, "height") ||
544  !strcmp(key, "videodatarate") ||
545  !strcmp(key, "framerate") ||
546  !strcmp(key, "videocodecid") ||
547  !strcmp(key, "audiodatarate") ||
548  !strcmp(key, "audiosamplerate") ||
549  !strcmp(key, "audiosamplesize") ||
550  !strcmp(key, "stereo") ||
551  !strcmp(key, "audiocodecid") ||
552  !strcmp(key, "datastream"))
553  return 0;
554 
556  if (amf_type == AMF_DATA_TYPE_BOOL) {
557  av_strlcpy(str_val, num_val > 0 ? "true" : "false",
558  sizeof(str_val));
559  av_dict_set(&s->metadata, key, str_val, 0);
560  } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
561  snprintf(str_val, sizeof(str_val), "%.f", num_val);
562  av_dict_set(&s->metadata, key, str_val, 0);
563  } else if (amf_type == AMF_DATA_TYPE_STRING)
564  av_dict_set(&s->metadata, key, str_val, 0);
565  }
566 
567  return 0;
568 }
569 
570 #define TYPE_ONTEXTDATA 1
571 #define TYPE_ONCAPTION 2
572 #define TYPE_ONCAPTIONINFO 3
573 #define TYPE_UNKNOWN 9
574 
575 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
576 {
578  AVStream *stream, *astream, *vstream;
579  AVStream av_unused *dstream;
580  AVIOContext *ioc;
581  int i;
582  // only needs to hold the string "onMetaData".
583  // Anything longer is something we don't want.
584  char buffer[32];
585 
586  astream = NULL;
587  vstream = NULL;
588  dstream = NULL;
589  ioc = s->pb;
590 
591  // first object needs to be "onMetaData" string
592  type = avio_r8(ioc);
593  if (type != AMF_DATA_TYPE_STRING ||
594  amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
595  return TYPE_UNKNOWN;
596 
597  if (!strcmp(buffer, "onTextData"))
598  return TYPE_ONTEXTDATA;
599 
600  if (!strcmp(buffer, "onCaption"))
601  return TYPE_ONCAPTION;
602 
603  if (!strcmp(buffer, "onCaptionInfo"))
604  return TYPE_ONCAPTIONINFO;
605 
606  if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
607  av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
608  return TYPE_UNKNOWN;
609  }
610 
611  // find the streams now so that amf_parse_object doesn't need to do
612  // the lookup every time it is called.
613  for (i = 0; i < s->nb_streams; i++) {
614  stream = s->streams[i];
615  if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
616  vstream = stream;
617  else if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO)
618  astream = stream;
619  else if (stream->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
620  dstream = stream;
621  }
622 
623  // parse the second object (we want a mixed array)
624  if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
625  return -1;
626 
627  return 0;
628 }
629 
631 {
632  int offset, flags;
633  FLVContext *flv = s->priv_data;
634 
635  avio_skip(s->pb, 4);
636  flags = avio_r8(s->pb);
637 
639 
640  if (flags & FLV_HEADER_FLAG_HASVIDEO)
642  return AVERROR(ENOMEM);
643  if (flags & FLV_HEADER_FLAG_HASAUDIO)
645  return AVERROR(ENOMEM);
646  // Flag doesn't indicate whether or not there is script-data present. Must
647  // create that stream if it's encountered.
648 
649  offset = avio_rb32(s->pb);
650  avio_seek(s->pb, offset, SEEK_SET);
651  avio_skip(s->pb, 4);
652 
653  s->start_time = 0;
654  flv->sum_flv_tag_size = 0;
655 
656  return 0;
657 }
658 
660 {
661  int i;
662  FLVContext *flv = s->priv_data;
663  for (i=0; i<FLV_STREAM_TYPE_NB; i++)
664  av_freep(&flv->new_extradata[i]);
665  return 0;
666 }
667 
669 {
670  av_freep(&st->codec->extradata);
671  if (ff_get_extradata(st->codec, s->pb, size) < 0)
672  return AVERROR(ENOMEM);
673  return 0;
674 }
675 
676 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
677  int size)
678 {
679  av_free(flv->new_extradata[stream]);
680  flv->new_extradata[stream] = av_mallocz(size +
682  if (!flv->new_extradata[stream])
683  return AVERROR(ENOMEM);
684  flv->new_extradata_size[stream] = size;
685  avio_read(pb, flv->new_extradata[stream], size);
686  return 0;
687 }
688 
689 static void clear_index_entries(AVFormatContext *s, int64_t pos)
690 {
691  int i, j, out;
693  "Found invalid index entries, clearing the index.\n");
694  for (i = 0; i < s->nb_streams; i++) {
695  AVStream *st = s->streams[i];
696  /* Remove all index entries that point to >= pos */
697  out = 0;
698  for (j = 0; j < st->nb_index_entries; j++)
699  if (st->index_entries[j].pos < pos)
700  st->index_entries[out++] = st->index_entries[j];
701  st->nb_index_entries = out;
702  }
703 }
704 
706 {
707  int nb = -1, ret, parse_name = 1;
708 
709  switch (type) {
711  avio_skip(pb, 8);
712  break;
713  case AMF_DATA_TYPE_BOOL:
714  avio_skip(pb, 1);
715  break;
717  avio_skip(pb, avio_rb16(pb));
718  break;
719  case AMF_DATA_TYPE_ARRAY:
720  parse_name = 0;
722  nb = avio_rb32(pb);
724  while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
725  if (parse_name) {
726  int size = avio_rb16(pb);
727  if (!size) {
728  avio_skip(pb, 1);
729  break;
730  }
731  avio_skip(pb, size);
732  }
733  if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
734  return ret;
735  }
736  break;
737  case AMF_DATA_TYPE_NULL:
739  break;
740  default:
741  return AVERROR_INVALIDDATA;
742  }
743  return 0;
744 }
745 
747  int64_t dts, int64_t next)
748 {
749  AVIOContext *pb = s->pb;
750  AVStream *st = NULL;
751  char buf[20];
752  int ret = AVERROR_INVALIDDATA;
753  int i, length = -1;
754  int array = 0;
755 
756  switch (avio_r8(pb)) {
757  case AMF_DATA_TYPE_ARRAY:
758  array = 1;
760  avio_seek(pb, 4, SEEK_CUR);
762  break;
763  default:
764  goto skip;
765  }
766 
767  while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
768  AMFDataType type = avio_r8(pb);
769  if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
770  length = avio_rb16(pb);
771  ret = av_get_packet(pb, pkt, length);
772  if (ret < 0)
773  goto skip;
774  else
775  break;
776  } else {
777  if ((ret = amf_skip_tag(pb, type)) < 0)
778  goto skip;
779  }
780  }
781 
782  if (length < 0) {
783  ret = AVERROR_INVALIDDATA;
784  goto skip;
785  }
786 
787  for (i = 0; i < s->nb_streams; i++) {
788  st = s->streams[i];
790  break;
791  }
792 
793  if (i == s->nb_streams) {
795  if (!st)
796  return AVERROR(ENOMEM);
798  }
799 
800  pkt->dts = dts;
801  pkt->pts = dts;
802  pkt->size = ret;
803 
804  pkt->stream_index = st->index;
805  pkt->flags |= AV_PKT_FLAG_KEY;
806 
807 skip:
808  avio_seek(s->pb, next + 4, SEEK_SET);
809 
810  return ret;
811 }
812 
814 {
815  FLVContext *flv = s->priv_data;
816  int64_t i;
817  int64_t pos = avio_tell(s->pb);
818 
819  for (i=0; !avio_feof(s->pb); i++) {
820  int j = i & (RESYNC_BUFFER_SIZE-1);
821  int j1 = j + RESYNC_BUFFER_SIZE;
822  flv->resync_buffer[j ] =
823  flv->resync_buffer[j1] = avio_r8(s->pb);
824 
825  if (i > 22) {
826  unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
827  if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
828  unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
829  unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
830  if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
831  unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
832  if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
833  avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
834  return 1;
835  }
836  }
837  }
838  }
839  }
840  return AVERROR_EOF;
841 }
842 
844 {
845  FLVContext *flv = s->priv_data;
846  int ret, i, size, flags;
847  enum FlvTagType type;
848  int stream_type=-1;
849  int64_t next, pos, meta_pos;
850  int64_t dts, pts = AV_NOPTS_VALUE;
851  int av_uninit(channels);
852  int av_uninit(sample_rate);
853  AVStream *st = NULL;
854  int last = -1;
855  int orig_size;
856 
857 retry:
858  /* pkt size is repeated at end. skip it */
859  pos = avio_tell(s->pb);
860  type = (avio_r8(s->pb) & 0x1F);
861  orig_size =
862  size = avio_rb24(s->pb);
863  flv->sum_flv_tag_size += size + 11;
864  dts = avio_rb24(s->pb);
865  dts |= (unsigned)avio_r8(s->pb) << 24;
866  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));
867  if (avio_feof(s->pb))
868  return AVERROR_EOF;
869  avio_skip(s->pb, 3); /* stream id, always 0 */
870  flags = 0;
871 
872  if (flv->validate_next < flv->validate_count) {
873  int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
874  if (pos == validate_pos) {
875  if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
877  flv->validate_next++;
878  } else {
879  clear_index_entries(s, validate_pos);
880  flv->validate_count = 0;
881  }
882  } else if (pos > validate_pos) {
883  clear_index_entries(s, validate_pos);
884  flv->validate_count = 0;
885  }
886  }
887 
888  if (size == 0) {
889  ret = FFERROR_REDO;
890  goto leave;
891  }
892 
893  next = size + avio_tell(s->pb);
894 
895  if (type == FLV_TAG_TYPE_AUDIO) {
896  stream_type = FLV_STREAM_TYPE_AUDIO;
897  flags = avio_r8(s->pb);
898  size--;
899  } else if (type == FLV_TAG_TYPE_VIDEO) {
900  stream_type = FLV_STREAM_TYPE_VIDEO;
901  flags = avio_r8(s->pb);
902  size--;
904  goto skip;
905  } else if (type == FLV_TAG_TYPE_META) {
906  stream_type=FLV_STREAM_TYPE_DATA;
907  if (size > 13 + 1 + 4) { // Header-type metadata stuff
908  int type;
909  meta_pos = avio_tell(s->pb);
910  type = flv_read_metabody(s, next);
911  if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
912  if (type < 0 && flv->validate_count &&
913  flv->validate_index[0].pos > next &&
914  flv->validate_index[0].pos - 4 < next
915  ) {
916  av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
917  next = flv->validate_index[0].pos - 4;
918  }
919  goto skip;
920  } else if (type == TYPE_ONTEXTDATA) {
921  avpriv_request_sample(s, "OnTextData packet");
922  return flv_data_packet(s, pkt, dts, next);
923  } else if (type == TYPE_ONCAPTION) {
924  return flv_data_packet(s, pkt, dts, next);
925  }
926  avio_seek(s->pb, meta_pos, SEEK_SET);
927  }
928  } else {
929  av_log(s, AV_LOG_DEBUG,
930  "Skipping flv packet: type %d, size %d, flags %d.\n",
931  type, size, flags);
932 skip:
933  avio_seek(s->pb, next, SEEK_SET);
934  ret = FFERROR_REDO;
935  goto leave;
936  }
937 
938  /* skip empty data packets */
939  if (!size) {
940  ret = FFERROR_REDO;
941  goto leave;
942  }
943 
944  /* now find stream */
945  for (i = 0; i < s->nb_streams; i++) {
946  st = s->streams[i];
947  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
948  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
949  (s->audio_codec_id || flv_same_audio_codec(st->codec, flags)))
950  break;
951  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
952  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
953  (s->video_codec_id || flv_same_video_codec(st->codec, flags)))
954  break;
955  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
957  break;
958  }
959  }
960  if (i == s->nb_streams) {
961  static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
962  av_log(s, AV_LOG_WARNING, "%s stream discovered after head already parsed\n", av_get_media_type_string(stream_types[stream_type]));
963  st = create_stream(s, stream_types[stream_type]);
964  if (!st)
965  return AVERROR(ENOMEM);
966 
967  }
968  av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
969 
970  if (s->pb->seekable &&
971  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
972  stream_type == FLV_STREAM_TYPE_AUDIO))
973  av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
974 
975  if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
976  ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
977  || st->discard >= AVDISCARD_ALL
978  ) {
979  avio_seek(s->pb, next, SEEK_SET);
980  ret = FFERROR_REDO;
981  goto leave;
982  }
983 
984  // if not streamed and no duration from metadata then seek to end to find
985  // the duration from the timestamps
986  if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) &&
987  !flv->searched_for_end) {
988  int size;
989  const int64_t pos = avio_tell(s->pb);
990  // Read the last 4 bytes of the file, this should be the size of the
991  // previous FLV tag. Use the timestamp of its payload as duration.
992  int64_t fsize = avio_size(s->pb);
993 retry_duration:
994  avio_seek(s->pb, fsize - 4, SEEK_SET);
995  size = avio_rb32(s->pb);
996  if (size > 0 && size < fsize) {
997  // Seek to the start of the last FLV tag at position (fsize - 4 - size)
998  // but skip the byte indicating the type.
999  avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
1000  if (size == avio_rb24(s->pb) + 11) {
1001  uint32_t ts = avio_rb24(s->pb);
1002  ts |= avio_r8(s->pb) << 24;
1003  if (ts)
1004  s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1005  else if (fsize >= 8 && fsize - 8 >= size) {
1006  fsize -= size+4;
1007  goto retry_duration;
1008  }
1009  }
1010  }
1011 
1012  avio_seek(s->pb, pos, SEEK_SET);
1013  flv->searched_for_end = 1;
1014  }
1015 
1016  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1017  int bits_per_coded_sample;
1018  channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1019  sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1021  bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1022  if (!st->codec->channels || !st->codec->sample_rate ||
1023  !st->codec->bits_per_coded_sample) {
1024  st->codec->channels = channels;
1025  st->codec->channel_layout = channels == 1
1028  st->codec->sample_rate = sample_rate;
1029  st->codec->bits_per_coded_sample = bits_per_coded_sample;
1030  }
1031  if (!st->codec->codec_id) {
1032  flv_set_audio_codec(s, st, st->codec,
1033  flags & FLV_AUDIO_CODECID_MASK);
1034  flv->last_sample_rate =
1035  sample_rate = st->codec->sample_rate;
1036  flv->last_channels =
1037  channels = st->codec->channels;
1038  } else {
1039  AVCodecContext ctx = {0};
1040  ctx.sample_rate = sample_rate;
1041  ctx.bits_per_coded_sample = bits_per_coded_sample;
1042  flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
1043  sample_rate = ctx.sample_rate;
1044  }
1045  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1046  size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1047  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1049  }
1050 
1051  if (st->codec->codec_id == AV_CODEC_ID_AAC ||
1052  st->codec->codec_id == AV_CODEC_ID_H264 ||
1053  st->codec->codec_id == AV_CODEC_ID_MPEG4) {
1054  int type = avio_r8(s->pb);
1055  size--;
1057  // sign extension
1058  int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1059  pts = dts + cts;
1060  if (cts < 0) { // dts might be wrong
1061  if (!flv->wrong_dts)
1063  "Negative cts, previous timestamps might be wrong.\n");
1064  flv->wrong_dts = 1;
1065  } else if (FFABS(dts - pts) > 1000*60*15) {
1067  "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1068  dts = pts = AV_NOPTS_VALUE;
1069  }
1070  }
1071  if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC ||
1072  st->codec->codec_id == AV_CODEC_ID_H264)) {
1073  AVDictionaryEntry *t;
1074 
1075  if (st->codec->extradata) {
1076  if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1077  return ret;
1078  ret = FFERROR_REDO;
1079  goto leave;
1080  }
1081  if ((ret = flv_get_extradata(s, st, size)) < 0)
1082  return ret;
1083 
1084  /* Workaround for buggy Omnia A/XE encoder */
1085  t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1086  if (st->codec->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1087  st->codec->extradata_size = 2;
1088 
1089  if (st->codec->codec_id == AV_CODEC_ID_AAC && 0) {
1090  MPEG4AudioConfig cfg;
1091 
1093  st->codec->extradata_size * 8, 1) >= 0) {
1094  st->codec->channels = cfg.channels;
1095  st->codec->channel_layout = 0;
1096  if (cfg.ext_sample_rate)
1097  st->codec->sample_rate = cfg.ext_sample_rate;
1098  else
1099  st->codec->sample_rate = cfg.sample_rate;
1100  av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
1101  st->codec->channels, st->codec->sample_rate);
1102  }
1103  }
1104 
1105  ret = FFERROR_REDO;
1106  goto leave;
1107  }
1108  }
1109 
1110  /* skip empty data packets */
1111  if (!size) {
1112  ret = FFERROR_REDO;
1113  goto leave;
1114  }
1115 
1116  ret = av_get_packet(s->pb, pkt, size);
1117  if (ret < 0)
1118  return ret;
1119  pkt->dts = dts;
1120  pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1121  pkt->stream_index = st->index;
1122  if (flv->new_extradata[stream_type]) {
1124  flv->new_extradata_size[stream_type]);
1125  if (side) {
1126  memcpy(side, flv->new_extradata[stream_type],
1127  flv->new_extradata_size[stream_type]);
1128  av_freep(&flv->new_extradata[stream_type]);
1129  flv->new_extradata_size[stream_type] = 0;
1130  }
1131  }
1132  if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1133  (sample_rate != flv->last_sample_rate ||
1134  channels != flv->last_channels)) {
1136  flv->last_channels = channels;
1137  ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1138  }
1139 
1140  if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1141  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1142  stream_type == FLV_STREAM_TYPE_DATA)
1143  pkt->flags |= AV_PKT_FLAG_KEY;
1144 
1145 leave:
1146  last = avio_rb32(s->pb);
1147  if (last != orig_size + 11 &&
1148  (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1149  !flv->broken_sizes) {
1150  av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d\n", last, orig_size + 11);
1151  avio_seek(s->pb, pos + 1, SEEK_SET);
1152  ret = resync(s);
1153  av_packet_unref(pkt);
1154  if (ret >= 0) {
1155  goto retry;
1156  }
1157  }
1158  return ret;
1159 }
1160 
1161 static int flv_read_seek(AVFormatContext *s, int stream_index,
1162  int64_t ts, int flags)
1163 {
1164  FLVContext *flv = s->priv_data;
1165  flv->validate_count = 0;
1166  return avio_seek_time(s->pb, stream_index, ts, flags);
1167 }
1168 
1169 #define OFFSET(x) offsetof(FLVContext, x)
1170 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1171 static const AVOption options[] = {
1172  { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1173  { NULL }
1174 };
1175 
1176 static const AVClass flv_class = {
1177  .class_name = "flvdec",
1178  .item_name = av_default_item_name,
1179  .option = options,
1180  .version = LIBAVUTIL_VERSION_INT,
1181 };
1182 
1184  .name = "flv",
1185  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1186  .priv_data_size = sizeof(FLVContext),
1187  .read_probe = flv_probe,
1192  .extensions = "flv",
1193  .priv_class = &flv_class,
1194 };
1195 
1196 static const AVClass live_flv_class = {
1197  .class_name = "live_flvdec",
1198  .item_name = av_default_item_name,
1199  .option = options,
1200  .version = LIBAVUTIL_VERSION_INT,
1201 };
1202 
1204  .name = "live_flv",
1205  .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1206  .priv_data_size = sizeof(FLVContext),
1212  .extensions = "flv",
1213  .priv_class = &live_flv_class,
1215 };
struct FLVContext::@170 validate_index[2]
#define RESYNC_BUFFER_SIZE
Definition: flvdec.c:42
#define NULL
Definition: coverity.c:32
discard all frames except keyframes
Definition: avcodec.h:687
#define TYPE_ONCAPTIONINFO
Definition: flvdec.c:572
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#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:287
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:1778
AVFormatContext * ctx
Definition: movenc-test.c:48
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
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:70
static void clear_index_entries(AVFormatContext *s, int64_t pos)
Definition: flvdec.c:689
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:4149
int64_t pos
Definition: avformat.h:817
int ff_get_extradata(AVCodecContext *avctx, 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:3000
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
#define TYPE_ONCAPTION
Definition: flvdec.c:571
#define KEYFRAMES_TIMESTAMP_TAG
Definition: flv.h:50
int index
stream index in AVFormatContext
Definition: avformat.h:878
int size
Definition: avcodec.h:1468
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1080
static int flv_data_packet(AVFormatContext *s, AVPacket *pkt, int64_t dts, int64_t next)
Definition: flvdec.c:746
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:1603
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
key frame (for AVC, a seekable frame)
Definition: flv.h:115
int version
Definition: avisynth_c.h:629
discard all
Definition: avcodec.h:688
static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos)
Definition: flvdec.c:308
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: flvdec.c:843
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:683
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1363
#define OFFSET(x)
Definition: flvdec.c:1169
static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: flvdec.c:1161
Format I/O context.
Definition: avformat.h:1314
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 AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1427
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:570
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:1170
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1273
AVOptions.
#define FLV_AUDIO_CODECID_MASK
Definition: flv.h:42
Definition: flv.h:74
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:698
int wrong_dts
wrong dts due to negative cts
Definition: flvdec.c:47
enum AVStreamParseType need_parsing
Definition: avformat.h:1069
static const AVOption options[]
Definition: flvdec.c:1171
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data)...
Definition: internal.h:544
#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:1382
#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:39
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1425
#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:252
AMFDataType
Definition: flv.h:122
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:765
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
int trust_metadata
configure streams according onMetaData
Definition: flvdec.c:46
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
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:1474
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
#define AVINDEX_KEYFRAME
Definition: avformat.h:824
#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:1528
#define TYPE_UNKNOWN
Definition: flvdec.c:573
static int live_flv_probe(AVProbeData *p)
Definition: flvdec.c:90
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:487
av_default_item_name
discard all bidirectional frames
Definition: avcodec.h:685
#define AVERROR(e)
Definition: error.h:43
static int flv_read_close(AVFormatContext *s)
Definition: flvdec.c:659
#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:630
#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:420
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:4277
static AVStream * create_stream(AVFormatContext *s, int codec_type)
Definition: flvdec.c:95
GLsizei GLsizei * length
Definition: opengl_enc.c:115
enum AVCodecID codec_id
Definition: mov_chan.c:433
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
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:246
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
int64_t dts
Definition: flvdec.c:53
Only parse headers, do not repack.
Definition: avformat.h:808
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
#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:207
audio channel layout utility functions
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:691
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
#define FFMIN(a, b)
Definition: common.h:96
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1480
static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
Definition: flvdec.c:221
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1711
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:1604
#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:85
static const AVClass flv_class
Definition: flvdec.c:1176
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid)
Definition: flvdec.c:161
#define KEYFRAMES_TAG
Definition: flv.h:49
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
Definition: flvdec.c:110
FILE * out
Definition: movenc-test.c:54
Stream structure.
Definition: avformat.h:877
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:1196
int sum_flv_tag_size
Definition: flvdec.c:63
static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
Definition: flvdec.c:575
sample_rate
FLV common header.
enum AVMediaType codec_type
Definition: avcodec.h:1540
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
enum AVCodecID codec_id
Definition: avcodec.h:1549
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
static void finish(void)
Definition: movenc-test.c:299
static int resync(AVFormatContext *s)
Definition: flvdec.c:813
main external API structure.
Definition: avcodec.h:1532
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:545
static int probe(AVProbeData *p, int live)
Definition: flvdec.c:66
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2979
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1648
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:69
int last_sample_rate
Definition: flvdec.c:50
int nb_index_entries
Definition: avformat.h:1082
Describe the class of an AVClass context structure.
Definition: log.h:67
int last_channels
Definition: flvdec.c:51
static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream, int size)
Definition: flvdec.c:676
AVMediaType
Definition: avutil.h:191
#define snprintf
Definition: snprintf.h:34
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
#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:1399
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
full parsing and repack
Definition: avformat.h:807
Main libavformat public API header.
raw UTF-8 text
Definition: avcodec.h:508
#define FLV_VIDEO_FRAMETYPE_MASK
Definition: flv.h:45
disposable inter frame (H.263 only)
Definition: flv.h:117
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
Definition: flvdec.c:293
AVInputFormat ff_live_flv_demuxer
Definition: flvdec.c:1203
AVInputFormat ff_flv_demuxer
Definition: flvdec.c:1183
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:635
static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
Definition: flvdec.c:705
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:1008
FlvTagType
Definition: flv.h:59
#define av_free(p)
char * value
Definition: dict.h:88
int eof_reached
true if eof reached
Definition: avio.h:186
static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth)
Definition: flvdec.c:392
int channels
number of audio channels
Definition: avcodec.h:2288
void * priv_data
Format private data.
Definition: avformat.h:1342
#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:1466
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
Definition: flvdec.c:668
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1409
video info/command frame
Definition: flv.h:119
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:299
int stream_index
Definition: avcodec.h:1469
#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:942
This structure stores compressed data.
Definition: avcodec.h:1444
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:126