3 * Copyright (c) 2003 The Libav Project
5 * This demuxer will generate a 1 byte extradata for VP6F content.
7 * - upper 4bits: difference between encoded width and visible width
8 * - lower 4bits: difference between encoded height and visible height
10 * This file is part of Libav.
12 * Libav 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.
17 * Libav 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.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with Libav; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.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"
37 #include "avio_internal.h"
40 #define KEYFRAMES_TAG "keyframes"
41 #define KEYFRAMES_TIMESTAMP_TAG "times"
42 #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
44 #define VALIDATE_INDEX_TS_THRESH 2500
46 typedef struct FLVContext {
47 const AVClass *class; ///< Class for private options.
48 int trust_metadata; ///< configure streams according onMetaData
49 int wrong_dts; ///< wrong dts due to negative cts
50 uint8_t *new_extradata[2];
51 int new_extradata_size[2];
63 static int flv_probe(AVProbeData *p)
71 d[3] < 5 && d[5] == 0 &&
73 return AVPROBE_SCORE_MAX;
78 static AVStream *create_stream(AVFormatContext *s, int codec_type)
80 AVStream *st = avformat_new_stream(s, NULL);
83 st->codecpar->codec_type = codec_type;
84 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
88 static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
90 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
91 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
94 if (!apar->codec_id && !apar->codec_tag)
97 if (apar->bits_per_coded_sample != bits_per_coded_sample)
100 switch (flv_codecid) {
101 // no distinction between S16 and S8 PCM codec flags
102 case FLV_CODECID_PCM:
103 codec_id = bits_per_coded_sample == 8
106 : AV_CODEC_ID_PCM_S16BE;
108 : AV_CODEC_ID_PCM_S16LE;
110 return codec_id == apar->codec_id;
111 case FLV_CODECID_PCM_LE:
112 codec_id = bits_per_coded_sample == 8
114 : AV_CODEC_ID_PCM_S16LE;
115 return codec_id == apar->codec_id;
116 case FLV_CODECID_AAC:
117 return apar->codec_id == AV_CODEC_ID_AAC;
118 case FLV_CODECID_ADPCM:
119 return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
120 case FLV_CODECID_SPEEX:
121 return apar->codec_id == AV_CODEC_ID_SPEEX;
122 case FLV_CODECID_MP3:
123 return apar->codec_id == AV_CODEC_ID_MP3;
124 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
125 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
126 case FLV_CODECID_NELLYMOSER:
127 return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
128 case FLV_CODECID_PCM_MULAW:
129 return apar->sample_rate == 8000 &&
130 apar->codec_id == AV_CODEC_ID_PCM_MULAW;
131 case FLV_CODECID_PCM_ALAW:
132 return apar->sample_rate == 8000 &&
133 apar->codec_id == AV_CODEC_ID_PCM_ALAW;
135 return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
139 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
140 AVCodecParameters *apar, int flv_codecid)
142 switch (flv_codecid) {
143 // no distinction between S16 and S8 PCM codec flags
144 case FLV_CODECID_PCM:
145 apar->codec_id = apar->bits_per_coded_sample == 8
148 : AV_CODEC_ID_PCM_S16BE;
150 : AV_CODEC_ID_PCM_S16LE;
153 case FLV_CODECID_PCM_LE:
154 apar->codec_id = apar->bits_per_coded_sample == 8
156 : AV_CODEC_ID_PCM_S16LE;
158 case FLV_CODECID_AAC:
159 apar->codec_id = AV_CODEC_ID_AAC;
161 case FLV_CODECID_ADPCM:
162 apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
164 case FLV_CODECID_SPEEX:
165 apar->codec_id = AV_CODEC_ID_SPEEX;
166 apar->sample_rate = 16000;
168 case FLV_CODECID_MP3:
169 apar->codec_id = AV_CODEC_ID_MP3;
170 astream->need_parsing = AVSTREAM_PARSE_FULL;
172 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
173 // in case metadata does not otherwise declare samplerate
174 apar->sample_rate = 8000;
175 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
177 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
178 apar->sample_rate = 16000;
179 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
181 case FLV_CODECID_NELLYMOSER:
182 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
184 case FLV_CODECID_PCM_MULAW:
185 apar->sample_rate = 8000;
186 apar->codec_id = AV_CODEC_ID_PCM_MULAW;
188 case FLV_CODECID_PCM_ALAW:
189 apar->sample_rate = 8000;
190 apar->codec_id = AV_CODEC_ID_PCM_ALAW;
193 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n",
194 flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
195 apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
199 static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
201 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
203 if (!vpar->codec_id && !vpar->codec_tag)
206 switch (flv_codecid) {
207 case FLV_CODECID_H263:
208 return vpar->codec_id == AV_CODEC_ID_FLV1;
209 case FLV_CODECID_SCREEN:
210 return vpar->codec_id == AV_CODEC_ID_FLASHSV;
211 case FLV_CODECID_SCREEN2:
212 return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
213 case FLV_CODECID_VP6:
214 return vpar->codec_id == AV_CODEC_ID_VP6F;
215 case FLV_CODECID_VP6A:
216 return vpar->codec_id == AV_CODEC_ID_VP6A;
217 case FLV_CODECID_H264:
218 return vpar->codec_id == AV_CODEC_ID_H264;
220 return vpar->codec_tag == flv_codecid;
224 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
225 int flv_codecid, int read)
227 AVCodecParameters *par = vstream->codecpar;
228 switch (flv_codecid) {
229 case FLV_CODECID_H263:
230 par->codec_id = AV_CODEC_ID_FLV1;
232 case FLV_CODECID_SCREEN:
233 par->codec_id = AV_CODEC_ID_FLASHSV;
235 case FLV_CODECID_SCREEN2:
236 par->codec_id = AV_CODEC_ID_FLASHSV2;
238 case FLV_CODECID_VP6:
239 par->codec_id = AV_CODEC_ID_VP6F;
240 case FLV_CODECID_VP6A:
241 if (flv_codecid == FLV_CODECID_VP6A)
242 par->codec_id = AV_CODEC_ID_VP6A;
244 if (par->extradata_size != 1) {
245 par->extradata = av_malloc(1);
247 par->extradata_size = 1;
250 par->extradata[0] = avio_r8(s->pb);
254 return 1; // 1 byte body size adjustment for flv_read_packet()
255 case FLV_CODECID_H264:
256 par->codec_id = AV_CODEC_ID_H264;
257 return 3; // not 4, reading packet type will consume one byte
259 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
260 par->codec_tag = flv_codecid;
266 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
268 int length = avio_rb16(ioc);
269 if (length >= buffsize) {
270 avio_skip(ioc, length);
274 avio_read(ioc, buffer, length);
276 buffer[length] = '\0';
281 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
282 AVStream *vstream, int64_t max_pos)
284 FLVContext *flv = s->priv_data;
285 unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
288 int64_t *times = NULL;
289 int64_t *filepositions = NULL;
290 int ret = AVERROR(ENOSYS);
291 int64_t initial_pos = avio_tell(ioc);
293 if (s->flags & AVFMT_FLAG_IGNIDX)
296 while (avio_tell(ioc) < max_pos - 2 &&
297 amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
298 int64_t *current_array;
300 // Expect array object in context
301 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
304 arraylen = avio_rb32(ioc);
308 /* Expect only 'times' or 'filepositions' sub-arrays in other
309 * case refuse to use such metadata for indexing. */
310 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
311 if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
312 ret = AVERROR(ENOMEM);
316 current_array = times;
317 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
319 if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
320 ret = AVERROR(ENOMEM);
323 fileposlen = arraylen;
324 current_array = filepositions;
326 // unexpected metatag inside keyframes, will not use such
327 // metadata for indexing
330 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
331 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
333 num_val = av_int2double(avio_rb64(ioc));
334 current_array[i] = num_val;
336 if (times && filepositions) {
337 // All done, exiting at a position allowing amf_parse_object
338 // to finish parsing the object
344 if (!ret && timeslen == fileposlen) {
345 for (i = 0; i < fileposlen; i++) {
346 av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
347 0, 0, AVINDEX_KEYFRAME);
349 flv->validate_index[i].pos = filepositions[i];
350 flv->validate_index[i].dts = times[i] * 1000;
351 flv->validate_count = i + 1;
355 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
359 av_freep(&filepositions);
360 // If we got unexpected data, but successfully reset back to
361 // the start pos, the caller can continue parsing
362 if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
367 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
368 AVStream *vstream, const char *key,
369 int64_t max_pos, int depth)
371 AVCodecParameters *apar, *vpar;
372 FLVContext *flv = s->priv_data;
374 AMFDataType amf_type;
380 amf_type = avio_r8(ioc);
383 case AMF_DATA_TYPE_NUMBER:
384 num_val = av_int2double(avio_rb64(ioc));
386 case AMF_DATA_TYPE_BOOL:
387 num_val = avio_r8(ioc);
389 case AMF_DATA_TYPE_STRING:
390 if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
393 case AMF_DATA_TYPE_OBJECT:
394 if ((vstream || astream) && key &&
395 !strcmp(KEYFRAMES_TAG, key) && depth == 1)
396 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
400 while (avio_tell(ioc) < max_pos - 2 &&
401 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
402 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
404 return -1; // if we couldn't skip, bomb out.
405 if (avio_r8(ioc) != AMF_END_OF_OBJECT)
408 case AMF_DATA_TYPE_NULL:
409 case AMF_DATA_TYPE_UNDEFINED:
410 case AMF_DATA_TYPE_UNSUPPORTED:
411 break; // these take up no additional space
412 case AMF_DATA_TYPE_MIXEDARRAY:
413 avio_skip(ioc, 4); // skip 32-bit max array index
414 while (avio_tell(ioc) < max_pos - 2 &&
415 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
416 // this is the only case in which we would want a nested
417 // parse to not skip over the object
418 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
421 if (avio_r8(ioc) != AMF_END_OF_OBJECT)
424 case AMF_DATA_TYPE_ARRAY:
426 unsigned int arraylen, i;
428 arraylen = avio_rb32(ioc);
429 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
430 if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
432 return -1; // if we couldn't skip, bomb out.
435 case AMF_DATA_TYPE_DATE:
436 avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
438 default: // unsupported type, we couldn't skip
443 // stream info doesn't live any deeper than the first object
445 apar = astream ? astream->codecpar : NULL;
446 vpar = vstream ? vstream->codecpar : NULL;
448 if (amf_type == AMF_DATA_TYPE_NUMBER ||
449 amf_type == AMF_DATA_TYPE_BOOL) {
450 if (!strcmp(key, "duration"))
451 s->duration = num_val * AV_TIME_BASE;
452 else if (!strcmp(key, "videodatarate") && vpar &&
453 0 <= (int)(num_val * 1024.0))
454 vpar->bit_rate = num_val * 1024.0;
455 else if (!strcmp(key, "audiodatarate") && apar &&
456 0 <= (int)(num_val * 1024.0))
457 apar->bit_rate = num_val * 1024.0;
458 else if (!strcmp(key, "datastream")) {
459 AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
461 return AVERROR(ENOMEM);
462 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
463 } else if (flv->trust_metadata) {
464 if (!strcmp(key, "videocodecid") && vpar) {
465 flv_set_video_codec(s, vstream, num_val, 0);
466 } else if (!strcmp(key, "audiocodecid") && apar) {
467 int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
468 flv_set_audio_codec(s, astream, apar, id);
469 } else if (!strcmp(key, "audiosamplerate") && apar) {
470 apar->sample_rate = num_val;
471 } else if (!strcmp(key, "audiosamplesize") && apar) {
472 apar->bits_per_coded_sample = num_val;
473 } else if (!strcmp(key, "stereo") && apar) {
474 apar->channels = num_val + 1;
475 apar->channel_layout = apar->channels == 2 ?
476 AV_CH_LAYOUT_STEREO :
478 } else if (!strcmp(key, "width") && vpar) {
479 vpar->width = num_val;
480 } else if (!strcmp(key, "height") && vpar) {
481 vpar->height = num_val;
487 if (!strcmp(key, "duration") ||
488 !strcmp(key, "filesize") ||
489 !strcmp(key, "width") ||
490 !strcmp(key, "height") ||
491 !strcmp(key, "videodatarate") ||
492 !strcmp(key, "framerate") ||
493 !strcmp(key, "videocodecid") ||
494 !strcmp(key, "audiodatarate") ||
495 !strcmp(key, "audiosamplerate") ||
496 !strcmp(key, "audiosamplesize") ||
497 !strcmp(key, "stereo") ||
498 !strcmp(key, "audiocodecid") ||
499 !strcmp(key, "datastream"))
502 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
503 if (amf_type == AMF_DATA_TYPE_BOOL) {
504 av_strlcpy(str_val, num_val > 0 ? "true" : "false",
506 av_dict_set(&s->metadata, key, str_val, 0);
507 } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
508 snprintf(str_val, sizeof(str_val), "%.f", num_val);
509 av_dict_set(&s->metadata, key, str_val, 0);
510 } else if (amf_type == AMF_DATA_TYPE_STRING)
511 av_dict_set(&s->metadata, key, str_val, 0);
517 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
520 AVStream *stream, *astream, *vstream;
523 // only needs to hold the string "onMetaData".
524 // Anything longer is something we don't want.
531 // first object needs to be "onMetaData" string
533 if (type != AMF_DATA_TYPE_STRING ||
534 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
537 if (!strcmp(buffer, "onTextData"))
540 if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint"))
543 // find the streams now so that amf_parse_object doesn't need to do
544 // the lookup every time it is called.
545 for (i = 0; i < s->nb_streams; i++) {
546 stream = s->streams[i];
547 if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
549 else if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
553 // parse the second object (we want a mixed array)
554 if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
560 static int flv_read_header(AVFormatContext *s)
565 avio_r8(s->pb); // flags
567 s->ctx_flags |= AVFMTCTX_NOHEADER;
569 offset = avio_rb32(s->pb);
570 avio_seek(s->pb, offset, SEEK_SET);
578 static int flv_read_close(AVFormatContext *s)
580 FLVContext *flv = s->priv_data;
581 av_freep(&flv->new_extradata[0]);
582 av_freep(&flv->new_extradata[1]);
586 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
588 av_free(st->codecpar->extradata);
589 st->codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
590 if (!st->codecpar->extradata)
591 return AVERROR(ENOMEM);
592 st->codecpar->extradata_size = size;
593 avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
597 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
600 av_free(flv->new_extradata[stream]);
601 flv->new_extradata[stream] = av_mallocz(size +
602 AV_INPUT_BUFFER_PADDING_SIZE);
603 if (!flv->new_extradata[stream])
604 return AVERROR(ENOMEM);
605 flv->new_extradata_size[stream] = size;
606 avio_read(pb, flv->new_extradata[stream], size);
610 static void clear_index_entries(AVFormatContext *s, int64_t pos)
613 av_log(s, AV_LOG_WARNING,
614 "Found invalid index entries, clearing the index.\n");
615 for (i = 0; i < s->nb_streams; i++) {
616 AVStream *st = s->streams[i];
617 /* Remove all index entries that point to >= pos */
619 for (j = 0; j < st->nb_index_entries; j++)
620 if (st->index_entries[j].pos < pos)
621 st->index_entries[out++] = st->index_entries[j];
622 st->nb_index_entries = out;
626 static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
628 int nb = -1, ret, parse_name = 1;
631 case AMF_DATA_TYPE_NUMBER:
634 case AMF_DATA_TYPE_BOOL:
637 case AMF_DATA_TYPE_STRING:
638 avio_skip(pb, avio_rb16(pb));
640 case AMF_DATA_TYPE_ARRAY:
642 case AMF_DATA_TYPE_MIXEDARRAY:
644 case AMF_DATA_TYPE_OBJECT:
645 while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
647 int size = avio_rb16(pb);
654 if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
658 case AMF_DATA_TYPE_NULL:
659 case AMF_DATA_TYPE_OBJECT_END:
662 return AVERROR_INVALIDDATA;
667 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
668 int64_t dts, int64_t next)
670 AVIOContext *pb = s->pb;
673 int ret = AVERROR_INVALIDDATA;
676 switch (avio_r8(pb)) {
677 case AMF_DATA_TYPE_MIXEDARRAY:
678 avio_seek(pb, 4, SEEK_CUR);
679 case AMF_DATA_TYPE_OBJECT:
685 while ((ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
686 AMFDataType type = avio_r8(pb);
687 if (type == AMF_DATA_TYPE_STRING && !strcmp(buf, "text")) {
688 length = avio_rb16(pb);
689 ret = av_get_packet(pb, pkt, length);
695 if ((ret = amf_skip_tag(pb, type)) < 0)
701 ret = AVERROR_INVALIDDATA;
705 for (i = 0; i < s->nb_streams; i++) {
707 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
711 if (i == s->nb_streams) {
712 st = create_stream(s, AVMEDIA_TYPE_DATA);
714 return AVERROR(ENOMEM);
715 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
722 pkt->stream_index = st->index;
723 pkt->flags |= AV_PKT_FLAG_KEY;
726 avio_seek(s->pb, next + 4, SEEK_SET);
731 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
733 FLVContext *flv = s->priv_data;
734 int ret, i, size, flags, is_audio;
735 enum FlvTagType type;
737 int64_t dts, pts = AV_NOPTS_VALUE;
738 int sample_rate = 0, channels = 0;
741 /* pkt size is repeated at end. skip it */
742 for (;; avio_skip(s->pb, 4)) {
743 pos = avio_tell(s->pb);
744 type = avio_r8(s->pb);
745 size = avio_rb24(s->pb);
746 dts = avio_rb24(s->pb);
747 dts |= avio_r8(s->pb) << 24;
748 av_log(s, AV_LOG_TRACE, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
749 if (s->pb->eof_reached)
751 avio_skip(s->pb, 3); /* stream id, always 0 */
754 if (flv->validate_next < flv->validate_count) {
755 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
756 if (pos == validate_pos) {
757 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
758 VALIDATE_INDEX_TS_THRESH) {
759 flv->validate_next++;
761 clear_index_entries(s, validate_pos);
762 flv->validate_count = 0;
764 } else if (pos > validate_pos) {
765 clear_index_entries(s, validate_pos);
766 flv->validate_count = 0;
773 next = size + avio_tell(s->pb);
775 if (type == FLV_TAG_TYPE_AUDIO) {
777 flags = avio_r8(s->pb);
779 } else if (type == FLV_TAG_TYPE_VIDEO) {
781 flags = avio_r8(s->pb);
783 if ((flags & 0xf0) == 0x50) /* video info / command frame */
786 if (type == FLV_TAG_TYPE_META && size > 13 + 1 + 4)
787 if (flv_read_metabody(s, next) > 0) {
788 return flv_data_packet(s, pkt, dts, next);
789 } else /* skip packet */
790 av_log(s, AV_LOG_DEBUG,
791 "Skipping flv packet: type %d, size %d, flags %d.\n",
795 avio_seek(s->pb, next, SEEK_SET);
799 /* skip empty data packets */
803 /* now find stream */
804 for (i = 0; i < s->nb_streams; i++) {
806 if (is_audio && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
807 if (flv_same_audio_codec(st->codecpar, flags))
809 } else if (!is_audio &&
810 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
811 if (flv_same_video_codec(st->codecpar, flags))
815 if (i == s->nb_streams) {
816 st = create_stream(s, is_audio ? AVMEDIA_TYPE_AUDIO
817 : AVMEDIA_TYPE_VIDEO);
819 return AVERROR(ENOMEM);
821 av_log(s, AV_LOG_TRACE, "%d %X %d \n", is_audio, flags, st->discard);
823 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
825 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
827 if ((st->discard >= AVDISCARD_NONKEY &&
828 !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) ||
829 (st->discard >= AVDISCARD_BIDIR &&
830 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) ||
831 st->discard >= AVDISCARD_ALL) {
832 avio_seek(s->pb, next, SEEK_SET);
838 // if not streamed and no duration from metadata then seek to end to find
839 // the duration from the timestamps
840 if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) &&
841 !flv->searched_for_end) {
843 const int64_t pos = avio_tell(s->pb);
844 // Read the last 4 bytes of the file, this should be the size of the
845 // previous FLV tag. Use the timestamp of its payload as duration.
846 const int64_t fsize = avio_size(s->pb);
847 avio_seek(s->pb, fsize - 4, SEEK_SET);
848 size = avio_rb32(s->pb);
849 if (size > 0 && size < fsize) {
850 // Seek to the start of the last FLV tag at position (fsize - 4 - size)
851 // but skip the byte indicating the type.
852 avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
853 if (size == avio_rb24(s->pb) + 11) {
854 uint32_t ts = avio_rb24(s->pb);
855 ts |= avio_r8(s->pb) << 24;
856 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
859 avio_seek(s->pb, pos, SEEK_SET);
860 flv->searched_for_end = 1;
864 int bits_per_coded_sample;
865 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
866 sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
867 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
868 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
869 if (!st->codecpar->channels || !st->codecpar->sample_rate ||
870 !st->codecpar->bits_per_coded_sample) {
871 st->codecpar->channels = channels;
872 st->codecpar->channel_layout = channels == 1
874 : AV_CH_LAYOUT_STEREO;
875 st->codecpar->sample_rate = sample_rate;
876 st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
878 if (!st->codecpar->codec_id) {
879 flv_set_audio_codec(s, st, st->codecpar,
880 flags & FLV_AUDIO_CODECID_MASK);
881 flv->last_sample_rate =
882 sample_rate = st->codecpar->sample_rate;
884 channels = st->codecpar->channels;
886 AVCodecParameters *par = avcodec_parameters_alloc();
888 ret = AVERROR(ENOMEM);
891 par->sample_rate = sample_rate;
892 par->bits_per_coded_sample = bits_per_coded_sample;
893 flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
894 sample_rate = par->sample_rate;
895 avcodec_parameters_free(&par);
898 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
901 if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
902 st->codecpar->codec_id == AV_CODEC_ID_H264) {
903 int type = avio_r8(s->pb);
905 if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
907 int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
909 if (cts < 0 && !flv->wrong_dts) { // dts might be wrong
911 av_log(s, AV_LOG_WARNING,
912 "Negative cts, previous timestamps might be wrong.\n");
916 if (st->codecpar->extradata) {
917 if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
919 ret = AVERROR(EAGAIN);
922 if ((ret = flv_get_extradata(s, st, size)) < 0)
924 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
925 MPEG4AudioConfig cfg;
927 /* Workaround for buggy Omnia A/XE encoder */
928 AVDictionaryEntry *t = av_dict_get(s->metadata, "Encoder", NULL, 0);
929 if (t && !strcmp(t->value, "Omnia A/XE"))
930 st->codecpar->extradata_size = 2;
932 avpriv_mpeg4audio_get_config(&cfg, st->codecpar->extradata,
933 st->codecpar->extradata_size * 8, 1);
934 st->codecpar->channels = cfg.channels;
935 st->codecpar->channel_layout = 0;
936 if (cfg.ext_sample_rate)
937 st->codecpar->sample_rate = cfg.ext_sample_rate;
939 st->codecpar->sample_rate = cfg.sample_rate;
940 av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
941 st->codecpar->channels, st->codecpar->sample_rate);
944 ret = AVERROR(EAGAIN);
949 /* skip empty data packets */
951 ret = AVERROR(EAGAIN);
955 ret = av_get_packet(s->pb, pkt, size);
958 /* note: we need to modify the packet size here to handle the last
962 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
963 pkt->stream_index = st->index;
964 if (flv->new_extradata[is_audio]) {
965 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
966 flv->new_extradata_size[is_audio]);
968 memcpy(side, flv->new_extradata[is_audio],
969 flv->new_extradata_size[is_audio]);
970 av_freep(&flv->new_extradata[is_audio]);
971 flv->new_extradata_size[is_audio] = 0;
974 if (is_audio && (sample_rate != flv->last_sample_rate ||
975 channels != flv->last_channels)) {
976 flv->last_sample_rate = sample_rate;
977 flv->last_channels = channels;
978 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
981 if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
982 pkt->flags |= AV_PKT_FLAG_KEY;
989 static int flv_read_seek(AVFormatContext *s, int stream_index,
990 int64_t ts, int flags)
992 FLVContext *flv = s->priv_data;
993 flv->validate_count = 0;
994 return avio_seek_time(s->pb, stream_index, ts, flags);
997 #define OFFSET(x) offsetof(FLVContext, x)
998 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
999 static const AVOption options[] = {
1000 { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1004 static const AVClass class = {
1005 .class_name = "flvdec",
1006 .item_name = av_default_item_name,
1008 .version = LIBAVUTIL_VERSION_INT,
1011 AVInputFormat ff_flv_demuxer = {
1013 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1014 .priv_data_size = sizeof(FLVContext),
1015 .read_probe = flv_probe,
1016 .read_header = flv_read_header,
1017 .read_packet = flv_read_packet,
1018 .read_seek = flv_read_seek,
1019 .read_close = flv_read_close,
1020 .extensions = "flv",
1021 .priv_class = &class,