3 * Copyright (c) 2003 The FFmpeg 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 FFmpeg.
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.
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.
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
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 VALIDATE_INDEX_TS_THRESH 2500
43 const AVClass *class; ///< Class for private options.
44 int trust_metadata; ///< configure streams according onMetaData
45 int wrong_dts; ///< wrong dts due to negative cts
46 uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
47 int new_extradata_size[FLV_STREAM_TYPE_NB];
59 static int flv_probe(AVProbeData *p)
67 d[3] < 5 && d[5] == 0 &&
69 return AVPROBE_SCORE_MAX;
74 static AVStream *create_stream(AVFormatContext *s, int codec_type)
76 AVStream *st = avformat_new_stream(s, NULL);
79 st->codec->codec_type = codec_type;
80 if (s->nb_streams>=3 ||( s->nb_streams==2
81 && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_DATA
82 && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_DATA))
83 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
85 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
89 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
91 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
92 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
95 if (!acodec->codec_id && !acodec->codec_tag)
98 if (acodec->bits_per_coded_sample != bits_per_coded_sample)
101 switch (flv_codecid) {
102 // no distinction between S16 and S8 PCM codec flags
103 case FLV_CODECID_PCM:
104 codec_id = bits_per_coded_sample == 8
107 : AV_CODEC_ID_PCM_S16BE;
109 : AV_CODEC_ID_PCM_S16LE;
111 return codec_id == acodec->codec_id;
112 case FLV_CODECID_PCM_LE:
113 codec_id = bits_per_coded_sample == 8
115 : AV_CODEC_ID_PCM_S16LE;
116 return codec_id == acodec->codec_id;
117 case FLV_CODECID_AAC:
118 return acodec->codec_id == AV_CODEC_ID_AAC;
119 case FLV_CODECID_ADPCM:
120 return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
121 case FLV_CODECID_SPEEX:
122 return acodec->codec_id == AV_CODEC_ID_SPEEX;
123 case FLV_CODECID_MP3:
124 return acodec->codec_id == AV_CODEC_ID_MP3;
125 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
126 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
127 case FLV_CODECID_NELLYMOSER:
128 return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
129 case FLV_CODECID_PCM_MULAW:
130 return acodec->sample_rate == 8000 &&
131 acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
132 case FLV_CODECID_PCM_ALAW:
133 return acodec->sample_rate == 8000 &&
134 acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
136 return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
140 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
141 AVCodecContext *acodec, int flv_codecid)
143 switch (flv_codecid) {
144 // no distinction between S16 and S8 PCM codec flags
145 case FLV_CODECID_PCM:
146 acodec->codec_id = acodec->bits_per_coded_sample == 8
149 : AV_CODEC_ID_PCM_S16BE;
151 : AV_CODEC_ID_PCM_S16LE;
154 case FLV_CODECID_PCM_LE:
155 acodec->codec_id = acodec->bits_per_coded_sample == 8
157 : AV_CODEC_ID_PCM_S16LE;
159 case FLV_CODECID_AAC:
160 acodec->codec_id = AV_CODEC_ID_AAC;
162 case FLV_CODECID_ADPCM:
163 acodec->codec_id = AV_CODEC_ID_ADPCM_SWF;
165 case FLV_CODECID_SPEEX:
166 acodec->codec_id = AV_CODEC_ID_SPEEX;
167 acodec->sample_rate = 16000;
169 case FLV_CODECID_MP3:
170 acodec->codec_id = AV_CODEC_ID_MP3;
171 astream->need_parsing = AVSTREAM_PARSE_FULL;
173 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
174 // in case metadata does not otherwise declare samplerate
175 acodec->sample_rate = 8000;
176 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
178 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
179 acodec->sample_rate = 16000;
180 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
182 case FLV_CODECID_NELLYMOSER:
183 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
185 case FLV_CODECID_PCM_MULAW:
186 acodec->sample_rate = 8000;
187 acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
189 case FLV_CODECID_PCM_ALAW:
190 acodec->sample_rate = 8000;
191 acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
194 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n",
195 flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
196 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
200 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
202 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
204 if (!vcodec->codec_id && !vcodec->codec_tag)
207 switch (flv_codecid) {
208 case FLV_CODECID_H263:
209 return vcodec->codec_id == AV_CODEC_ID_FLV1;
210 case FLV_CODECID_SCREEN:
211 return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
212 case FLV_CODECID_SCREEN2:
213 return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
214 case FLV_CODECID_VP6:
215 return vcodec->codec_id == AV_CODEC_ID_VP6F;
216 case FLV_CODECID_VP6A:
217 return vcodec->codec_id == AV_CODEC_ID_VP6A;
218 case FLV_CODECID_H264:
219 return vcodec->codec_id == AV_CODEC_ID_H264;
221 return vcodec->codec_tag == flv_codecid;
225 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
226 int flv_codecid, int read)
228 AVCodecContext *vcodec = vstream->codec;
229 switch (flv_codecid) {
230 case FLV_CODECID_H263:
231 vcodec->codec_id = AV_CODEC_ID_FLV1;
233 case FLV_CODECID_REALH263:
234 vcodec->codec_id = AV_CODEC_ID_H263;
235 break; // Really mean it this time
236 case FLV_CODECID_SCREEN:
237 vcodec->codec_id = AV_CODEC_ID_FLASHSV;
239 case FLV_CODECID_SCREEN2:
240 vcodec->codec_id = AV_CODEC_ID_FLASHSV2;
242 case FLV_CODECID_VP6:
243 vcodec->codec_id = AV_CODEC_ID_VP6F;
244 case FLV_CODECID_VP6A:
245 if (flv_codecid == FLV_CODECID_VP6A)
246 vcodec->codec_id = AV_CODEC_ID_VP6A;
248 if (vcodec->extradata_size != 1) {
249 vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
250 if (vcodec->extradata)
251 vcodec->extradata_size = 1;
253 if (vcodec->extradata)
254 vcodec->extradata[0] = avio_r8(s->pb);
258 return 1; // 1 byte body size adjustment for flv_read_packet()
259 case FLV_CODECID_H264:
260 vcodec->codec_id = AV_CODEC_ID_H264;
261 return 3; // not 4, reading packet type will consume one byte
262 case FLV_CODECID_MPEG4:
263 vcodec->codec_id = AV_CODEC_ID_MPEG4;
266 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
267 vcodec->codec_tag = flv_codecid;
273 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
275 int length = avio_rb16(ioc);
276 if (length >= buffsize) {
277 avio_skip(ioc, length);
281 avio_read(ioc, buffer, length);
283 buffer[length] = '\0';
288 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
289 AVStream *vstream, int64_t max_pos)
291 FLVContext *flv = s->priv_data;
292 unsigned int timeslen = 0, fileposlen = 0, i;
294 int64_t *times = NULL;
295 int64_t *filepositions = NULL;
296 int ret = AVERROR(ENOSYS);
297 int64_t initial_pos = avio_tell(ioc);
299 if (vstream->nb_index_entries>0) {
300 av_log(s, AV_LOG_WARNING, "Skiping duplicate index\n");
304 if (s->flags & AVFMT_FLAG_IGNIDX)
307 while (avio_tell(ioc) < max_pos - 2 &&
308 amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
309 int64_t **current_array;
310 unsigned int arraylen;
312 // Expect array object in context
313 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
316 arraylen = avio_rb32(ioc);
320 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
321 current_array = ×
323 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
325 current_array = &filepositions;
326 fileposlen = arraylen;
328 // unexpected metatag inside keyframes, will not use such
329 // metadata for indexing
332 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
333 ret = AVERROR(ENOMEM);
337 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
338 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
340 current_array[0][i] = av_int2double(avio_rb64(ioc));
342 if (times && filepositions) {
343 // All done, exiting at a position allowing amf_parse_object
344 // to finish parsing the object
350 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
351 for (i = 0; i < fileposlen; i++) {
352 av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
353 0, 0, AVINDEX_KEYFRAME);
355 flv->validate_index[i].pos = filepositions[i];
356 flv->validate_index[i].dts = times[i] * 1000;
357 flv->validate_count = i + 1;
362 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
367 av_freep(&filepositions);
368 avio_seek(ioc, initial_pos, SEEK_SET);
372 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
373 AVStream *vstream, const char *key,
374 int64_t max_pos, int depth)
376 AVCodecContext *acodec, *vcodec;
377 FLVContext *flv = s->priv_data;
379 AMFDataType amf_type;
385 amf_type = avio_r8(ioc);
388 case AMF_DATA_TYPE_NUMBER:
389 num_val = av_int2double(avio_rb64(ioc));
391 case AMF_DATA_TYPE_BOOL:
392 num_val = avio_r8(ioc);
394 case AMF_DATA_TYPE_STRING:
395 if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
398 case AMF_DATA_TYPE_OBJECT:
399 if ((vstream || astream) && key &&
401 !strcmp(KEYFRAMES_TAG, key) && depth == 1)
402 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
404 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
406 while (avio_tell(ioc) < max_pos - 2 &&
407 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
408 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
410 return -1; // if we couldn't skip, bomb out.
411 if (avio_r8(ioc) != AMF_END_OF_OBJECT)
414 case AMF_DATA_TYPE_NULL:
415 case AMF_DATA_TYPE_UNDEFINED:
416 case AMF_DATA_TYPE_UNSUPPORTED:
417 break; // these take up no additional space
418 case AMF_DATA_TYPE_MIXEDARRAY:
419 avio_skip(ioc, 4); // skip 32-bit max array index
420 while (avio_tell(ioc) < max_pos - 2 &&
421 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
422 // this is the only case in which we would want a nested
423 // parse to not skip over the object
424 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
427 if (avio_r8(ioc) != AMF_END_OF_OBJECT)
430 case AMF_DATA_TYPE_ARRAY:
432 unsigned int arraylen, i;
434 arraylen = avio_rb32(ioc);
435 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
436 if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
438 return -1; // if we couldn't skip, bomb out.
441 case AMF_DATA_TYPE_DATE:
442 avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
444 default: // unsupported type, we couldn't skip
448 // only look for metadata values when we are not nested and key != NULL
449 if (depth == 1 && key) {
450 acodec = astream ? astream->codec : NULL;
451 vcodec = vstream ? vstream->codec : NULL;
453 if (amf_type == AMF_DATA_TYPE_NUMBER ||
454 amf_type == AMF_DATA_TYPE_BOOL) {
455 if (!strcmp(key, "duration"))
456 s->duration = num_val * AV_TIME_BASE;
457 else if (!strcmp(key, "videodatarate") && vcodec &&
458 0 <= (int)(num_val * 1024.0))
459 vcodec->bit_rate = num_val * 1024.0;
460 else if (!strcmp(key, "audiodatarate") && acodec &&
461 0 <= (int)(num_val * 1024.0))
462 acodec->bit_rate = num_val * 1024.0;
463 else if (!strcmp(key, "datastream")) {
464 AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
466 return AVERROR(ENOMEM);
467 st->codec->codec_id = AV_CODEC_ID_TEXT;
468 } else if (flv->trust_metadata) {
469 if (!strcmp(key, "videocodecid") && vcodec) {
470 flv_set_video_codec(s, vstream, num_val, 0);
471 } else if (!strcmp(key, "audiocodecid") && acodec) {
472 int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
473 flv_set_audio_codec(s, astream, acodec, id);
474 } else if (!strcmp(key, "audiosamplerate") && acodec) {
475 acodec->sample_rate = num_val;
476 } else if (!strcmp(key, "audiosamplesize") && acodec) {
477 acodec->bits_per_coded_sample = num_val;
478 } else if (!strcmp(key, "stereo") && acodec) {
479 acodec->channels = num_val + 1;
480 acodec->channel_layout = acodec->channels == 2 ?
481 AV_CH_LAYOUT_STEREO :
483 } else if (!strcmp(key, "width") && vcodec) {
484 vcodec->width = num_val;
485 } else if (!strcmp(key, "height") && vcodec) {
486 vcodec->height = num_val;
491 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
492 ((!acodec && !strcmp(key, "audiocodecid")) ||
493 (!vcodec && !strcmp(key, "videocodecid"))))
494 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
496 if (!strcmp(key, "duration") ||
497 !strcmp(key, "filesize") ||
498 !strcmp(key, "width") ||
499 !strcmp(key, "height") ||
500 !strcmp(key, "videodatarate") ||
501 !strcmp(key, "framerate") ||
502 !strcmp(key, "videocodecid") ||
503 !strcmp(key, "audiodatarate") ||
504 !strcmp(key, "audiosamplerate") ||
505 !strcmp(key, "audiosamplesize") ||
506 !strcmp(key, "stereo") ||
507 !strcmp(key, "audiocodecid") ||
508 !strcmp(key, "datastream"))
511 if (amf_type == AMF_DATA_TYPE_BOOL) {
512 av_strlcpy(str_val, num_val > 0 ? "true" : "false",
514 av_dict_set(&s->metadata, key, str_val, 0);
515 } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
516 snprintf(str_val, sizeof(str_val), "%.f", num_val);
517 av_dict_set(&s->metadata, key, str_val, 0);
518 } else if (amf_type == AMF_DATA_TYPE_STRING)
519 av_dict_set(&s->metadata, key, str_val, 0);
525 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
528 AVStream *stream, *astream, *vstream;
529 AVStream av_unused *dstream;
532 // only needs to hold the string "onMetaData".
533 // Anything longer is something we don't want.
541 // first object needs to be "onMetaData" string
543 if (type != AMF_DATA_TYPE_STRING ||
544 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
547 if (!strcmp(buffer, "onTextData"))
550 if (strcmp(buffer, "onMetaData"))
553 // find the streams now so that amf_parse_object doesn't need to do
554 // the lookup every time it is called.
555 for (i = 0; i < s->nb_streams; i++) {
556 stream = s->streams[i];
557 if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
559 else if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO)
561 else if (stream->codec->codec_type == AVMEDIA_TYPE_DATA)
565 // parse the second object (we want a mixed array)
566 if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
572 static int flv_read_header(AVFormatContext *s)
577 flags = avio_r8(s->pb);
578 /* old flvtool cleared this field */
579 /* FIXME: better fix needed */
581 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
582 av_log(s, AV_LOG_WARNING,
583 "Broken FLV file, which says no streams present, "
584 "this might fail\n");
587 s->ctx_flags |= AVFMTCTX_NOHEADER;
589 if (flags & FLV_HEADER_FLAG_HASVIDEO)
590 if (!create_stream(s, AVMEDIA_TYPE_VIDEO))
591 return AVERROR(ENOMEM);
592 if (flags & FLV_HEADER_FLAG_HASAUDIO)
593 if (!create_stream(s, AVMEDIA_TYPE_AUDIO))
594 return AVERROR(ENOMEM);
595 // Flag doesn't indicate whether or not there is script-data present. Must
596 // create that stream if it's encountered.
598 offset = avio_rb32(s->pb);
599 avio_seek(s->pb, offset, SEEK_SET);
607 static int flv_read_close(AVFormatContext *s)
610 FLVContext *flv = s->priv_data;
611 for (i=0; i<FLV_STREAM_TYPE_NB; i++)
612 av_freep(&flv->new_extradata[i]);
616 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
618 av_free(st->codec->extradata);
619 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
620 if (!st->codec->extradata)
621 return AVERROR(ENOMEM);
622 st->codec->extradata_size = size;
623 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
627 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
630 av_free(flv->new_extradata[stream]);
631 flv->new_extradata[stream] = av_mallocz(size +
632 FF_INPUT_BUFFER_PADDING_SIZE);
633 if (!flv->new_extradata[stream])
634 return AVERROR(ENOMEM);
635 flv->new_extradata_size[stream] = size;
636 avio_read(pb, flv->new_extradata[stream], size);
640 static void clear_index_entries(AVFormatContext *s, int64_t pos)
643 av_log(s, AV_LOG_WARNING,
644 "Found invalid index entries, clearing the index.\n");
645 for (i = 0; i < s->nb_streams; i++) {
646 AVStream *st = s->streams[i];
647 /* Remove all index entries that point to >= pos */
649 for (j = 0; j < st->nb_index_entries; j++)
650 if (st->index_entries[j].pos < pos)
651 st->index_entries[out++] = st->index_entries[j];
652 st->nb_index_entries = out;
656 static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
658 int nb = -1, ret, parse_name = 1;
661 case AMF_DATA_TYPE_NUMBER:
664 case AMF_DATA_TYPE_BOOL:
667 case AMF_DATA_TYPE_STRING:
668 avio_skip(pb, avio_rb16(pb));
670 case AMF_DATA_TYPE_ARRAY:
672 case AMF_DATA_TYPE_MIXEDARRAY:
674 case AMF_DATA_TYPE_OBJECT:
675 while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
677 int size = avio_rb16(pb);
684 if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
688 case AMF_DATA_TYPE_NULL:
689 case AMF_DATA_TYPE_OBJECT_END:
692 return AVERROR_INVALIDDATA;
697 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
698 int64_t dts, int64_t next)
700 AVIOContext *pb = s->pb;
703 int ret = AVERROR_INVALIDDATA;
706 switch (avio_r8(pb)) {
707 case AMF_DATA_TYPE_MIXEDARRAY:
708 avio_seek(pb, 4, SEEK_CUR);
709 case AMF_DATA_TYPE_OBJECT:
715 while ((ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
716 AMFDataType type = avio_r8(pb);
717 if (type == AMF_DATA_TYPE_STRING && !strcmp(buf, "text")) {
718 length = avio_rb16(pb);
719 ret = av_get_packet(pb, pkt, length);
725 if ((ret = amf_skip_tag(pb, type)) < 0)
731 ret = AVERROR_INVALIDDATA;
735 for (i = 0; i < s->nb_streams; i++) {
737 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
741 if (i == s->nb_streams) {
742 st = create_stream(s, AVMEDIA_TYPE_DATA);
744 return AVERROR_INVALIDDATA;
745 st->codec->codec_id = AV_CODEC_ID_TEXT;
752 pkt->stream_index = st->index;
753 pkt->flags |= AV_PKT_FLAG_KEY;
756 avio_seek(s->pb, next + 4, SEEK_SET);
761 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
763 FLVContext *flv = s->priv_data;
764 int ret, i, type, size, flags;
766 int64_t next, pos, meta_pos;
767 int64_t dts, pts = AV_NOPTS_VALUE;
768 int av_uninit(channels);
769 int av_uninit(sample_rate);
772 /* pkt size is repeated at end. skip it */
773 for (;; avio_skip(s->pb, 4)) {
774 pos = avio_tell(s->pb);
775 type = avio_r8(s->pb);
776 size = avio_rb24(s->pb);
777 dts = avio_rb24(s->pb);
778 dts |= avio_r8(s->pb) << 24;
779 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
782 avio_skip(s->pb, 3); /* stream id, always 0 */
785 if (flv->validate_next < flv->validate_count) {
786 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
787 if (pos == validate_pos) {
788 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
789 VALIDATE_INDEX_TS_THRESH) {
790 flv->validate_next++;
792 clear_index_entries(s, validate_pos);
793 flv->validate_count = 0;
795 } else if (pos > validate_pos) {
796 clear_index_entries(s, validate_pos);
797 flv->validate_count = 0;
804 next = size + avio_tell(s->pb);
806 if (type == FLV_TAG_TYPE_AUDIO) {
807 stream_type = FLV_STREAM_TYPE_AUDIO;
808 flags = avio_r8(s->pb);
810 } else if (type == FLV_TAG_TYPE_VIDEO) {
811 stream_type = FLV_STREAM_TYPE_VIDEO;
812 flags = avio_r8(s->pb);
814 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
816 } else if (type == FLV_TAG_TYPE_META) {
817 stream_type=FLV_STREAM_TYPE_DATA;
818 if (size > 13 + 1 + 4 && dts == 0) { // Header-type metadata stuff
819 meta_pos = avio_tell(s->pb);
820 if (flv_read_metabody(s, next) == 0) {
823 avio_seek(s->pb, meta_pos, SEEK_SET);
826 av_log(s, AV_LOG_DEBUG,
827 "skipping flv packet: type %d, size %d, flags %d\n",
830 avio_seek(s->pb, next, SEEK_SET);
834 /* skip empty data packets */
838 /* now find stream */
839 for (i = 0; i < s->nb_streams; i++) {
841 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
842 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
843 (s->audio_codec_id || flv_same_audio_codec(st->codec, flags)))
845 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
846 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
847 (s->video_codec_id || flv_same_video_codec(st->codec, flags)))
849 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
850 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
854 if (i == s->nb_streams) {
855 static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA};
856 av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
857 st = create_stream(s, stream_types[stream_type]);
859 return AVERROR(ENOMEM);
862 av_dlog(s, "%d %X %d \n", stream_type, flags, st->discard);
863 if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
864 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
865 || st->discard >= AVDISCARD_ALL
867 avio_seek(s->pb, next, SEEK_SET);
870 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || stream_type == FLV_STREAM_TYPE_AUDIO)
871 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
875 // if not streamed and no duration from metadata then seek to end to find
876 // the duration from the timestamps
877 if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) && !flv->searched_for_end) {
879 const int64_t pos = avio_tell(s->pb);
880 int64_t fsize = avio_size(s->pb);
882 avio_seek(s->pb, fsize - 4, SEEK_SET);
883 size = avio_rb32(s->pb);
884 avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
885 if (size == avio_rb24(s->pb) + 11) {
886 uint32_t ts = avio_rb24(s->pb);
887 ts |= avio_r8(s->pb) << 24;
889 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
890 else if (fsize >= 8 && fsize - 8 >= size) {
896 avio_seek(s->pb, pos, SEEK_SET);
897 flv->searched_for_end = 1;
900 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
901 int bits_per_coded_sample;
902 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
903 sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
904 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
905 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
906 if (!st->codec->channels || !st->codec->sample_rate ||
907 !st->codec->bits_per_coded_sample) {
908 st->codec->channels = channels;
909 st->codec->channel_layout = channels == 1
911 : AV_CH_LAYOUT_STEREO;
912 st->codec->sample_rate = sample_rate;
913 st->codec->bits_per_coded_sample = bits_per_coded_sample;
915 if (!st->codec->codec_id) {
916 flv_set_audio_codec(s, st, st->codec,
917 flags & FLV_AUDIO_CODECID_MASK);
918 flv->last_sample_rate =
919 sample_rate = st->codec->sample_rate;
921 channels = st->codec->channels;
924 ctx.sample_rate = sample_rate;
925 flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
926 sample_rate = ctx.sample_rate;
928 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
929 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
932 if (st->codec->codec_id == AV_CODEC_ID_AAC ||
933 st->codec->codec_id == AV_CODEC_ID_H264 ||
934 st->codec->codec_id == AV_CODEC_ID_MPEG4) {
935 int type = avio_r8(s->pb);
937 if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
939 int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
941 if (cts < 0) { // dts are wrong
943 av_log(s, AV_LOG_WARNING,
944 "negative cts, previous timestamps might be wrong\n");
947 dts = AV_NOPTS_VALUE;
949 if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC)) {
950 if (st->codec->extradata) {
951 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
953 ret = AVERROR(EAGAIN);
956 if ((ret = flv_get_extradata(s, st, size)) < 0)
958 if (st->codec->codec_id == AV_CODEC_ID_AAC && 0) {
959 MPEG4AudioConfig cfg;
960 if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
961 st->codec->extradata_size * 8, 1) >= 0) {
962 st->codec->channels = cfg.channels;
963 st->codec->channel_layout = 0;
964 if (cfg.ext_sample_rate)
965 st->codec->sample_rate = cfg.ext_sample_rate;
967 st->codec->sample_rate = cfg.sample_rate;
968 av_dlog(s, "mp4a config channels %d sample rate %d\n",
969 st->codec->channels, st->codec->sample_rate);
973 ret = AVERROR(EAGAIN);
978 /* skip empty data packets */
980 ret = AVERROR(EAGAIN);
984 ret = av_get_packet(s->pb, pkt, size);
988 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
989 pkt->stream_index = st->index;
990 if (flv->new_extradata[stream_type]) {
991 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
992 flv->new_extradata_size[stream_type]);
994 memcpy(side, flv->new_extradata[stream_type],
995 flv->new_extradata_size[stream_type]);
996 av_freep(&flv->new_extradata[stream_type]);
997 flv->new_extradata_size[stream_type] = 0;
1000 if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1001 (sample_rate != flv->last_sample_rate ||
1002 channels != flv->last_channels)) {
1003 flv->last_sample_rate = sample_rate;
1004 flv->last_channels = channels;
1005 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1008 if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1009 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1010 stream_type == FLV_STREAM_TYPE_DATA)
1011 pkt->flags |= AV_PKT_FLAG_KEY;
1014 avio_skip(s->pb, 4);
1018 static int flv_read_seek(AVFormatContext *s, int stream_index,
1019 int64_t ts, int flags)
1021 FLVContext *flv = s->priv_data;
1022 flv->validate_count = 0;
1023 return avio_seek_time(s->pb, stream_index, ts, flags);
1026 #define OFFSET(x) offsetof(FLVContext, x)
1027 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1028 static const AVOption options[] = {
1029 { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1033 static const AVClass flv_class = {
1034 .class_name = "flvdec",
1035 .item_name = av_default_item_name,
1037 .version = LIBAVUTIL_VERSION_INT,
1040 AVInputFormat ff_flv_demuxer = {
1042 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1043 .priv_data_size = sizeof(FLVContext),
1044 .read_probe = flv_probe,
1045 .read_header = flv_read_header,
1046 .read_packet = flv_read_packet,
1047 .read_seek = flv_read_seek,
1048 .read_close = flv_read_close,
1049 .extensions = "flv",
1050 .priv_class = &flv_class,