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;
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 
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 (!apar->codec_id && !apar->codec_tag)
117  return 1;
118 
119  if (apar->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 == apar->codec_id;
133  case FLV_CODECID_PCM_LE:
134  codec_id = bits_per_coded_sample == 8
137  return codec_id == apar->codec_id;
138  case FLV_CODECID_AAC:
139  return apar->codec_id == AV_CODEC_ID_AAC;
140  case FLV_CODECID_ADPCM:
141  return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
142  case FLV_CODECID_SPEEX:
143  return apar->codec_id == AV_CODEC_ID_SPEEX;
144  case FLV_CODECID_MP3:
145  return apar->codec_id == AV_CODEC_ID_MP3;
149  return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
151  return apar->sample_rate == 8000 &&
154  return apar->sample_rate == 8000 &&
156  default:
157  return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
158  }
159 }
160 
162  AVCodecParameters *apar, int flv_codecid)
163 {
164  switch (flv_codecid) {
165  // no distinction between S16 and S8 PCM codec flags
166  case FLV_CODECID_PCM:
167  apar->codec_id = apar->bits_per_coded_sample == 8
169 #if HAVE_BIGENDIAN
171 #else
173 #endif
174  break;
175  case FLV_CODECID_PCM_LE:
176  apar->codec_id = apar->bits_per_coded_sample == 8
179  break;
180  case FLV_CODECID_AAC:
181  apar->codec_id = AV_CODEC_ID_AAC;
182  break;
183  case FLV_CODECID_ADPCM:
185  break;
186  case FLV_CODECID_SPEEX:
187  apar->codec_id = AV_CODEC_ID_SPEEX;
188  apar->sample_rate = 16000;
189  break;
190  case FLV_CODECID_MP3:
191  apar->codec_id = AV_CODEC_ID_MP3;
193  break;
195  // in case metadata does not otherwise declare samplerate
196  apar->sample_rate = 8000;
198  break;
200  apar->sample_rate = 16000;
202  break;
205  break;
207  apar->sample_rate = 8000;
209  break;
211  apar->sample_rate = 8000;
213  break;
214  default:
215  avpriv_request_sample(s, "Audio codec (%x)",
216  flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
217  apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
218  }
219 }
220 
222 {
223  int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
224 
225  if (!vpar->codec_id && !vpar->codec_tag)
226  return 1;
227 
228  switch (flv_codecid) {
229  case FLV_CODECID_H263:
230  return vpar->codec_id == AV_CODEC_ID_FLV1;
231  case FLV_CODECID_SCREEN:
232  return vpar->codec_id == AV_CODEC_ID_FLASHSV;
233  case FLV_CODECID_SCREEN2:
234  return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
235  case FLV_CODECID_VP6:
236  return vpar->codec_id == AV_CODEC_ID_VP6F;
237  case FLV_CODECID_VP6A:
238  return vpar->codec_id == AV_CODEC_ID_VP6A;
239  case FLV_CODECID_H264:
240  return vpar->codec_id == AV_CODEC_ID_H264;
241  default:
242  return vpar->codec_tag == flv_codecid;
243  }
244 }
245 
247  int flv_codecid, int read)
248 {
249  AVCodecParameters *par = vstream->codecpar;
250  switch (flv_codecid) {
251  case FLV_CODECID_H263:
252  par->codec_id = AV_CODEC_ID_FLV1;
253  break;
255  par->codec_id = AV_CODEC_ID_H263;
256  break; // Really mean it this time
257  case FLV_CODECID_SCREEN:
259  break;
260  case FLV_CODECID_SCREEN2:
262  break;
263  case FLV_CODECID_VP6:
264  par->codec_id = AV_CODEC_ID_VP6F;
265  case FLV_CODECID_VP6A:
266  if (flv_codecid == FLV_CODECID_VP6A)
267  par->codec_id = AV_CODEC_ID_VP6A;
268  if (read) {
269  if (par->extradata_size != 1) {
270  ff_alloc_extradata(par, 1);
271  }
272  if (par->extradata)
273  par->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  par->codec_id = AV_CODEC_ID_H264;
281  return 3; // not 4, reading packet type will consume one byte
282  case FLV_CODECID_MPEG4:
284  return 3;
285  default:
286  avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
287  par->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  AVCodecParameters *apar, *vpar;
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  apar = astream ? astream->codecpar : NULL;
481  vpar = vstream ? vstream->codecpar : 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") && vpar &&
490  0 <= (int)(num_val * 1024.0))
491  vpar->bit_rate = num_val * 1024.0;
492  else if (!strcmp(key, "audiodatarate") && apar &&
493  0 <= (int)(num_val * 1024.0))
494  apar->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") && vpar) {
502  flv_set_video_codec(s, vstream, num_val, 0);
503  } else if (!strcmp(key, "audiocodecid") && apar) {
504  int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
505  flv_set_audio_codec(s, astream, apar, id);
506  } else if (!strcmp(key, "audiosamplerate") && apar) {
507  apar->sample_rate = num_val;
508  } else if (!strcmp(key, "audiosamplesize") && apar) {
509  apar->bits_per_coded_sample = num_val;
510  } else if (!strcmp(key, "stereo") && apar) {
511  apar->channels = num_val + 1;
512  apar->channel_layout = apar->channels == 2 ?
515  } else if (!strcmp(key, "width") && vpar) {
516  vpar->width = num_val;
517  } else if (!strcmp(key, "height") && vpar) {
518  vpar->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  ((!apar && !strcmp(key, "audiocodecid")) ||
537  (!vpar && !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->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
616  vstream = stream;
617  else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
618  astream = stream;
619  else if (stream->codecpar->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  FLVContext *flv = s->priv_data;
633  int offset;
634 
635  avio_skip(s->pb, 4);
636  avio_r8(s->pb); // flags
637 
639 
640  offset = avio_rb32(s->pb);
641  avio_seek(s->pb, offset, SEEK_SET);
642  avio_skip(s->pb, 4);
643 
644  s->start_time = 0;
645  flv->sum_flv_tag_size = 0;
646 
647  return 0;
648 }
649 
651 {
652  int i;
653  FLVContext *flv = s->priv_data;
654  for (i=0; i<FLV_STREAM_TYPE_NB; i++)
655  av_freep(&flv->new_extradata[i]);
656  return 0;
657 }
658 
660 {
661  av_freep(&st->codecpar->extradata);
662  if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
663  return AVERROR(ENOMEM);
664  return 0;
665 }
666 
667 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
668  int size)
669 {
670  av_free(flv->new_extradata[stream]);
671  flv->new_extradata[stream] = av_mallocz(size +
673  if (!flv->new_extradata[stream])
674  return AVERROR(ENOMEM);
675  flv->new_extradata_size[stream] = size;
676  avio_read(pb, flv->new_extradata[stream], size);
677  return 0;
678 }
679 
680 static void clear_index_entries(AVFormatContext *s, int64_t pos)
681 {
682  int i, j, out;
684  "Found invalid index entries, clearing the index.\n");
685  for (i = 0; i < s->nb_streams; i++) {
686  AVStream *st = s->streams[i];
687  /* Remove all index entries that point to >= pos */
688  out = 0;
689  for (j = 0; j < st->nb_index_entries; j++)
690  if (st->index_entries[j].pos < pos)
691  st->index_entries[out++] = st->index_entries[j];
692  st->nb_index_entries = out;
693  }
694 }
695 
697 {
698  int nb = -1, ret, parse_name = 1;
699 
700  switch (type) {
702  avio_skip(pb, 8);
703  break;
704  case AMF_DATA_TYPE_BOOL:
705  avio_skip(pb, 1);
706  break;
708  avio_skip(pb, avio_rb16(pb));
709  break;
710  case AMF_DATA_TYPE_ARRAY:
711  parse_name = 0;
713  nb = avio_rb32(pb);
715  while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
716  if (parse_name) {
717  int size = avio_rb16(pb);
718  if (!size) {
719  avio_skip(pb, 1);
720  break;
721  }
722  avio_skip(pb, size);
723  }
724  if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
725  return ret;
726  }
727  break;
728  case AMF_DATA_TYPE_NULL:
730  break;
731  default:
732  return AVERROR_INVALIDDATA;
733  }
734  return 0;
735 }
736 
738  int64_t dts, int64_t next)
739 {
740  AVIOContext *pb = s->pb;
741  AVStream *st = NULL;
742  char buf[20];
743  int ret = AVERROR_INVALIDDATA;
744  int i, length = -1;
745  int array = 0;
746 
747  switch (avio_r8(pb)) {
748  case AMF_DATA_TYPE_ARRAY:
749  array = 1;
751  avio_seek(pb, 4, SEEK_CUR);
753  break;
754  default:
755  goto skip;
756  }
757 
758  while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
759  AMFDataType type = avio_r8(pb);
760  if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
761  length = avio_rb16(pb);
762  ret = av_get_packet(pb, pkt, length);
763  if (ret < 0)
764  goto skip;
765  else
766  break;
767  } else {
768  if ((ret = amf_skip_tag(pb, type)) < 0)
769  goto skip;
770  }
771  }
772 
773  if (length < 0) {
774  ret = AVERROR_INVALIDDATA;
775  goto skip;
776  }
777 
778  for (i = 0; i < s->nb_streams; i++) {
779  st = s->streams[i];
781  break;
782  }
783 
784  if (i == s->nb_streams) {
786  if (!st)
787  return AVERROR(ENOMEM);
789  }
790 
791  pkt->dts = dts;
792  pkt->pts = dts;
793  pkt->size = ret;
794 
795  pkt->stream_index = st->index;
796  pkt->flags |= AV_PKT_FLAG_KEY;
797 
798 skip:
799  avio_seek(s->pb, next + 4, SEEK_SET);
800 
801  return ret;
802 }
803 
805 {
806  FLVContext *flv = s->priv_data;
807  int64_t i;
808  int64_t pos = avio_tell(s->pb);
809 
810  for (i=0; !avio_feof(s->pb); i++) {
811  int j = i & (RESYNC_BUFFER_SIZE-1);
812  int j1 = j + RESYNC_BUFFER_SIZE;
813  flv->resync_buffer[j ] =
814  flv->resync_buffer[j1] = avio_r8(s->pb);
815 
816  if (i > 22) {
817  unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
818  if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
819  unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
820  unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
821  if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
822  unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
823  if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
824  avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
825  return 1;
826  }
827  }
828  }
829  }
830  }
831  return AVERROR_EOF;
832 }
833 
835 {
836  FLVContext *flv = s->priv_data;
837  int ret, i, size, flags;
838  enum FlvTagType type;
839  int stream_type=-1;
840  int64_t next, pos, meta_pos;
841  int64_t dts, pts = AV_NOPTS_VALUE;
842  int av_uninit(channels);
843  int av_uninit(sample_rate);
844  AVStream *st = NULL;
845  int last = -1;
846  int orig_size;
847 
848 retry:
849  /* pkt size is repeated at end. skip it */
850  pos = avio_tell(s->pb);
851  type = (avio_r8(s->pb) & 0x1F);
852  orig_size =
853  size = avio_rb24(s->pb);
854  flv->sum_flv_tag_size += size + 11;
855  dts = avio_rb24(s->pb);
856  dts |= (unsigned)avio_r8(s->pb) << 24;
857  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));
858  if (avio_feof(s->pb))
859  return AVERROR_EOF;
860  avio_skip(s->pb, 3); /* stream id, always 0 */
861  flags = 0;
862 
863  if (flv->validate_next < flv->validate_count) {
864  int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
865  if (pos == validate_pos) {
866  if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
868  flv->validate_next++;
869  } else {
870  clear_index_entries(s, validate_pos);
871  flv->validate_count = 0;
872  }
873  } else if (pos > validate_pos) {
874  clear_index_entries(s, validate_pos);
875  flv->validate_count = 0;
876  }
877  }
878 
879  if (size == 0) {
880  ret = FFERROR_REDO;
881  goto leave;
882  }
883 
884  next = size + avio_tell(s->pb);
885 
886  if (type == FLV_TAG_TYPE_AUDIO) {
887  stream_type = FLV_STREAM_TYPE_AUDIO;
888  flags = avio_r8(s->pb);
889  size--;
890  } else if (type == FLV_TAG_TYPE_VIDEO) {
891  stream_type = FLV_STREAM_TYPE_VIDEO;
892  flags = avio_r8(s->pb);
893  size--;
895  goto skip;
896  } else if (type == FLV_TAG_TYPE_META) {
897  stream_type=FLV_STREAM_TYPE_DATA;
898  if (size > 13 + 1 + 4) { // Header-type metadata stuff
899  int type;
900  meta_pos = avio_tell(s->pb);
901  type = flv_read_metabody(s, next);
902  if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
903  if (type < 0 && flv->validate_count &&
904  flv->validate_index[0].pos > next &&
905  flv->validate_index[0].pos - 4 < next
906  ) {
907  av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
908  next = flv->validate_index[0].pos - 4;
909  }
910  goto skip;
911  } else if (type == TYPE_ONTEXTDATA) {
912  avpriv_request_sample(s, "OnTextData packet");
913  return flv_data_packet(s, pkt, dts, next);
914  } else if (type == TYPE_ONCAPTION) {
915  return flv_data_packet(s, pkt, dts, next);
916  }
917  avio_seek(s->pb, meta_pos, SEEK_SET);
918  }
919  } else {
920  av_log(s, AV_LOG_DEBUG,
921  "Skipping flv packet: type %d, size %d, flags %d.\n",
922  type, size, flags);
923 skip:
924  avio_seek(s->pb, next, SEEK_SET);
925  ret = FFERROR_REDO;
926  goto leave;
927  }
928 
929  /* skip empty data packets */
930  if (!size) {
931  ret = FFERROR_REDO;
932  goto leave;
933  }
934 
935  /* now find stream */
936  for (i = 0; i < s->nb_streams; i++) {
937  st = s->streams[i];
938  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
939  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
940  (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
941  break;
942  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
943  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
944  (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
945  break;
946  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
948  break;
949  }
950  }
951  if (i == s->nb_streams) {
952  static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
953  av_log(s, AV_LOG_WARNING, "%s stream discovered after head already parsed\n", av_get_media_type_string(stream_types[stream_type]));
954  st = create_stream(s, stream_types[stream_type]);
955  if (!st)
956  return AVERROR(ENOMEM);
957 
958  }
959  av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
960 
961  if (s->pb->seekable &&
962  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
963  stream_type == FLV_STREAM_TYPE_AUDIO))
964  av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
965 
966  if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
967  ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
968  || st->discard >= AVDISCARD_ALL
969  ) {
970  avio_seek(s->pb, next, SEEK_SET);
971  ret = FFERROR_REDO;
972  goto leave;
973  }
974 
975  // if not streamed and no duration from metadata then seek to end to find
976  // the duration from the timestamps
977  if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) &&
978  !flv->searched_for_end) {
979  int size;
980  const int64_t pos = avio_tell(s->pb);
981  // Read the last 4 bytes of the file, this should be the size of the
982  // previous FLV tag. Use the timestamp of its payload as duration.
983  int64_t fsize = avio_size(s->pb);
984 retry_duration:
985  avio_seek(s->pb, fsize - 4, SEEK_SET);
986  size = avio_rb32(s->pb);
987  if (size > 0 && size < fsize) {
988  // Seek to the start of the last FLV tag at position (fsize - 4 - size)
989  // but skip the byte indicating the type.
990  avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
991  if (size == avio_rb24(s->pb) + 11) {
992  uint32_t ts = avio_rb24(s->pb);
993  ts |= avio_r8(s->pb) << 24;
994  if (ts)
995  s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
996  else if (fsize >= 8 && fsize - 8 >= size) {
997  fsize -= size+4;
998  goto retry_duration;
999  }
1000  }
1001  }
1002 
1003  avio_seek(s->pb, pos, SEEK_SET);
1004  flv->searched_for_end = 1;
1005  }
1006 
1007  if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1008  int bits_per_coded_sample;
1009  channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1010  sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1012  bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1013  if (!st->codecpar->channels || !st->codecpar->sample_rate ||
1015  st->codecpar->channels = channels;
1016  st->codecpar->channel_layout = channels == 1
1020  st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1021  }
1022  if (!st->codecpar->codec_id) {
1023  flv_set_audio_codec(s, st, st->codecpar,
1024  flags & FLV_AUDIO_CODECID_MASK);
1025  flv->last_sample_rate =
1027  flv->last_channels =
1028  channels = st->codecpar->channels;
1029  } else {
1031  if (!par) {
1032  ret = AVERROR(ENOMEM);
1033  goto leave;
1034  }
1035  par->sample_rate = sample_rate;
1036  par->bits_per_coded_sample = bits_per_coded_sample;
1037  flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1038  sample_rate = par->sample_rate;
1040  }
1041  } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1042  size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1043  } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1045  }
1046 
1047  if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1050  int type = avio_r8(s->pb);
1051  size--;
1053  // sign extension
1054  int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1055  pts = dts + cts;
1056  if (cts < 0) { // dts might be wrong
1057  if (!flv->wrong_dts)
1059  "Negative cts, previous timestamps might be wrong.\n");
1060  flv->wrong_dts = 1;
1061  } else if (FFABS(dts - pts) > 1000*60*15) {
1063  "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1064  dts = pts = AV_NOPTS_VALUE;
1065  }
1066  }
1067  if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1068  st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1069  AVDictionaryEntry *t;
1070 
1071  if (st->codecpar->extradata) {
1072  if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1073  return ret;
1074  ret = FFERROR_REDO;
1075  goto leave;
1076  }
1077  if ((ret = flv_get_extradata(s, st, size)) < 0)
1078  return ret;
1079 
1080  /* Workaround for buggy Omnia A/XE encoder */
1081  t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1082  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1083  st->codecpar->extradata_size = 2;
1084 
1085  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && 0) {
1086  MPEG4AudioConfig cfg;
1087 
1089  st->codecpar->extradata_size * 8, 1) >= 0) {
1090  st->codecpar->channels = cfg.channels;
1091  st->codecpar->channel_layout = 0;
1092  if (cfg.ext_sample_rate)
1094  else
1095  st->codecpar->sample_rate = cfg.sample_rate;
1096  av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
1097  st->codecpar->channels, st->codecpar->sample_rate);
1098  }
1099  }
1100 
1101  ret = FFERROR_REDO;
1102  goto leave;
1103  }
1104  }
1105 
1106  /* skip empty data packets */
1107  if (!size) {
1108  ret = FFERROR_REDO;
1109  goto leave;
1110  }
1111 
1112  ret = av_get_packet(s->pb, pkt, size);
1113  if (ret < 0)
1114  return ret;
1115  pkt->dts = dts;
1116  pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1117  pkt->stream_index = st->index;
1118  if (flv->new_extradata[stream_type]) {
1120  flv->new_extradata_size[stream_type]);
1121  if (side) {
1122  memcpy(side, flv->new_extradata[stream_type],
1123  flv->new_extradata_size[stream_type]);
1124  av_freep(&flv->new_extradata[stream_type]);
1125  flv->new_extradata_size[stream_type] = 0;
1126  }
1127  }
1128  if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1129  (sample_rate != flv->last_sample_rate ||
1130  channels != flv->last_channels)) {
1132  flv->last_channels = channels;
1133  ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1134  }
1135 
1136  if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1137  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1138  stream_type == FLV_STREAM_TYPE_DATA)
1139  pkt->flags |= AV_PKT_FLAG_KEY;
1140 
1141 leave:
1142  last = avio_rb32(s->pb);
1143  if (last != orig_size + 11 &&
1144  (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1145  !flv->broken_sizes) {
1146  av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d\n", last, orig_size + 11);
1147  avio_seek(s->pb, pos + 1, SEEK_SET);
1148  ret = resync(s);
1149  av_packet_unref(pkt);
1150  if (ret >= 0) {
1151  goto retry;
1152  }
1153  }
1154  return ret;
1155 }
1156 
1157 static int flv_read_seek(AVFormatContext *s, int stream_index,
1158  int64_t ts, int flags)
1159 {
1160  FLVContext *flv = s->priv_data;
1161  flv->validate_count = 0;
1162  return avio_seek_time(s->pb, stream_index, ts, flags);
1163 }
1164 
1165 #define OFFSET(x) offsetof(FLVContext, x)
1166 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1167 static const AVOption options[] = {
1168  { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1169  { NULL }
1170 };
1171 
1172 static const AVClass flv_class = {
1173  .class_name = "flvdec",
1174  .item_name = av_default_item_name,
1175  .option = options,
1176  .version = LIBAVUTIL_VERSION_INT,
1177 };
1178 
1180  .name = "flv",
1181  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1182  .priv_data_size = sizeof(FLVContext),
1183  .read_probe = flv_probe,
1188  .extensions = "flv",
1189  .priv_class = &flv_class,
1190 };
1191 
1192 static const AVClass live_flv_class = {
1193  .class_name = "live_flvdec",
1194  .item_name = av_default_item_name,
1195  .option = options,
1196  .version = LIBAVUTIL_VERSION_INT,
1197 };
1198 
1200  .name = "live_flv",
1201  .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1202  .priv_data_size = sizeof(FLVContext),
1208  .extensions = "flv",
1209  .priv_class = &live_flv_class,
1211 };
#define RESYNC_BUFFER_SIZE
Definition: flvdec.c:42
#define NULL
Definition: coverity.c:32
discard all frames except keyframes
Definition: avcodec.h:780
#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
enum AVCodecID codec_id
Definition: ffmpeg_vaapi.c:149
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:292
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:1900
video info/command frame
Definition: flv.h:119
#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:70
static void clear_index_entries(AVFormatContext *s, int64_t pos)
Definition: flvdec.c:680
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:4404
int64_t pos
Definition: avformat.h:816
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
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3915
#define KEYFRAMES_TIMESTAMP_TAG
Definition: flv.h:50
int index
stream index in AVFormatContext
Definition: avformat.h:877
int size
Definition: avcodec.h:1578
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecParameters *apar, int flv_codecid)
Definition: flvdec.c:161
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:213
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1085
static int flv_data_packet(AVFormatContext *s, AVPacket *pkt, int64_t dts, int64_t next)
Definition: flvdec.c:737
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:1608
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:287
int version
Definition: avisynth_c.h:629
discard all
Definition: avcodec.h:781
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:834
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:688
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1368
#define OFFSET(x)
Definition: flvdec.c:1165
static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: flvdec.c:1157
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3907
Format I/O context.
Definition: avformat.h:1319
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:1432
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 int array[MAX_W *MAX_W]
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:1166
int width
Video only.
Definition: avcodec.h:3981
#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:4031
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:703
int wrong_dts
wrong dts due to negative cts
Definition: flvdec.c:47
enum AVStreamParseType need_parsing
Definition: avformat.h:1074
static const AVOption options[]
Definition: flvdec.c:1167
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4042
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data)...
Definition: internal.h:562
#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:1387
#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:1430
#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:260
AMFDataType
Definition: flv.h:122
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:770
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:447
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:1479
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4017
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:550
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3944
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1609
struct FLVContext::@185 validate_index[2]
#define AVINDEX_KEYFRAME
Definition: avformat.h:823
#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:1533
#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:486
av_default_item_name
discard all bidirectional frames
Definition: avcodec.h:778
#define AVERROR(e)
Definition: error.h:43
static int flv_read_close(AVFormatContext *s)
Definition: flvdec.c:650
#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:512
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3911
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:4537
static AVStream * create_stream(AVFormatContext *s, int codec_type)
Definition: flvdec.c:95
GLsizei GLsizei * length
Definition: opengl_enc.c:115
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:4041
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:1583
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3933
int64_t dts
Definition: flvdec.c:53
Only parse headers, do not repack.
Definition: avformat.h:807
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:541
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:462
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:461
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1375
#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
disposable inter frame (H.263 only)
Definition: flv.h:117
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:3105
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:696
#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:1485
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
Definition: flvdec.c:110
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:1609
#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:1172
#define KEYFRAMES_TAG
Definition: flv.h:49
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
FILE * out
Definition: movenc-test.c:54
Stream structure.
Definition: avformat.h:876
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:1192
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.
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:1361
static void finish(void)
Definition: movenc-test.c:299
static int resync(AVFormatContext *s)
Definition: flvdec.c:804
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:549
static int probe(AVProbeData *p, int live)
Definition: flvdec.c:66
void * buf
Definition: avisynth_c.h:553
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:69
int last_sample_rate
Definition: flvdec.c:50
int nb_index_entries
Definition: avformat.h:1087
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:667
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:459
#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:1404
int sample_rate
Audio only.
Definition: avcodec.h:4025
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:471
full parsing and repack
Definition: avformat.h:806
Main libavformat public API header.
Definition: flv.h:74
raw UTF-8 text
Definition: avcodec.h:601
#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:3126
static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
Definition: flvdec.c:293
AVInputFormat ff_live_flv_demuxer
Definition: flvdec.c:1199
AVInputFormat ff_flv_demuxer
Definition: flvdec.c:1179
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:728
static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
Definition: flvdec.c:696
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:1065
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: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
void * priv_data
Format private data.
Definition: avformat.h:1347
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3957
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3929
int channels
Audio only.
Definition: avcodec.h:4021
#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:1576
static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
Definition: flvdec.c:659
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1414
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
AVCodecParameters * codecpar
Definition: avformat.h:1006
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:311
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3919
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:299
int stream_index
Definition: avcodec.h:1579
key frame (for AVC, a seekable frame)
Definition: flv.h:115
#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:936
This structure stores compressed data.
Definition: avcodec.h:1554
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:1570
static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
Definition: flvdec.c:221
#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