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/dict.h"
29 #include "libavutil/intfloat.h"
30 #include "libavutil/mathematics.h"
31 #include "libavcodec/bytestream.h"
32 #include "libavcodec/mpeg4audio.h"
35 #include "avio_internal.h"
38 #define VALIDATE_INDEX_TS_THRESH 2500
41 int wrong_dts; ///< wrong dts due to negative cts
42 uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
43 int new_extradata_size[FLV_STREAM_TYPE_NB];
54 static int flv_probe(AVProbeData *p)
59 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
60 return AVPROBE_SCORE_MAX;
65 static AVStream *create_stream(AVFormatContext *s, int tag, int codec_type){
66 AVStream *st = avformat_new_stream(s, NULL);
70 st->codec->codec_type = codec_type;
71 if(s->nb_streams>=3 ||( s->nb_streams==2
72 && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_DATA
73 && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_DATA))
74 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
76 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
80 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
82 //no distinction between S16 and S8 PCM codec flags
84 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
91 case FLV_CODECID_PCM_LE:
92 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
93 case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
94 case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
95 case FLV_CODECID_SPEEX:
96 acodec->codec_id = CODEC_ID_SPEEX;
97 acodec->sample_rate = 16000;
99 case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
100 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
101 acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
102 acodec->codec_id = CODEC_ID_NELLYMOSER;
104 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
105 acodec->sample_rate = 16000;
106 acodec->codec_id = CODEC_ID_NELLYMOSER;
108 case FLV_CODECID_NELLYMOSER:
109 acodec->codec_id = CODEC_ID_NELLYMOSER;
112 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
113 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
117 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
118 AVCodecContext *vcodec = vstream->codec;
119 switch(flv_codecid) {
120 case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
121 case FLV_CODECID_REALH263: vcodec->codec_id = CODEC_ID_H263 ; break; // Really mean it this time
122 case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
123 case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
124 case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
125 case FLV_CODECID_VP6A :
126 if(flv_codecid == FLV_CODECID_VP6A)
127 vcodec->codec_id = CODEC_ID_VP6A;
128 if(vcodec->extradata_size != 1) {
129 vcodec->extradata_size = 1;
130 vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
132 vcodec->extradata[0] = avio_r8(s->pb);
133 return 1; // 1 byte body size adjustment for flv_read_packet()
134 case FLV_CODECID_H264:
135 vcodec->codec_id = CODEC_ID_H264;
136 return 3; // not 4, reading packet type will consume one byte
137 case FLV_CODECID_MPEG4:
138 vcodec->codec_id = CODEC_ID_MPEG4;
141 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
142 vcodec->codec_tag = flv_codecid;
148 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
149 int length = avio_rb16(ioc);
150 if(length >= buffsize) {
151 avio_skip(ioc, length);
155 avio_read(ioc, buffer, length);
157 buffer[length] = '\0';
162 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
163 FLVContext *flv = s->priv_data;
164 unsigned int timeslen = 0, fileposlen = 0, i;
166 int64_t *times = NULL;
167 int64_t *filepositions = NULL;
168 int ret = AVERROR(ENOSYS);
169 int64_t initial_pos = avio_tell(ioc);
171 if(vstream->nb_index_entries>0){
172 av_log(s, AV_LOG_WARNING, "Skiping duplicate index\n");
176 if (s->flags & AVFMT_FLAG_IGNIDX)
179 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
180 int64_t** current_array;
181 unsigned int arraylen;
183 // Expect array object in context
184 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
187 arraylen = avio_rb32(ioc);
191 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times){
192 current_array= ×
194 }else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions){
195 current_array= &filepositions;
196 fileposlen= arraylen;
197 }else // unexpected metatag inside keyframes, will not use such metadata for indexing
200 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
201 ret = AVERROR(ENOMEM);
205 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
206 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
208 current_array[0][i] = av_int2double(avio_rb64(ioc));
210 if (times && filepositions) {
211 // All done, exiting at a position allowing amf_parse_object
212 // to finish parsing the object
218 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
219 for (i = 0; i < fileposlen; i++) {
220 av_add_index_entry(vstream, filepositions[i], times[i]*1000,
221 0, 0, AVINDEX_KEYFRAME);
223 flv->validate_index[i].pos = filepositions[i];
224 flv->validate_index[i].dts = times[i] * 1000;
225 flv->validate_count = i + 1;
230 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
235 av_freep(&filepositions);
236 avio_seek(ioc, initial_pos, SEEK_SET);
240 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
241 AVCodecContext *acodec, *vcodec;
243 AMFDataType amf_type;
250 amf_type = avio_r8(ioc);
253 case AMF_DATA_TYPE_NUMBER:
254 num_val = av_int2double(avio_rb64(ioc)); break;
255 case AMF_DATA_TYPE_BOOL:
256 num_val = avio_r8(ioc); break;
257 case AMF_DATA_TYPE_STRING:
258 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
261 case AMF_DATA_TYPE_OBJECT:
262 if ((vstream || astream) && ioc->seekable && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
263 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
265 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
267 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
268 if (amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
269 return -1; //if we couldn't skip, bomb out.
271 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
274 case AMF_DATA_TYPE_NULL:
275 case AMF_DATA_TYPE_UNDEFINED:
276 case AMF_DATA_TYPE_UNSUPPORTED:
277 break; //these take up no additional space
278 case AMF_DATA_TYPE_MIXEDARRAY:
279 avio_skip(ioc, 4); //skip 32-bit max array index
280 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
281 //this is the only case in which we would want a nested parse to not skip over the object
282 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
285 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
288 case AMF_DATA_TYPE_ARRAY: {
289 unsigned int arraylen, i;
291 arraylen = avio_rb32(ioc);
292 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
293 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
294 return -1; //if we couldn't skip, bomb out.
298 case AMF_DATA_TYPE_DATE:
299 avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
301 default: //unsupported type, we couldn't skip
305 if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
306 acodec = astream ? astream->codec : NULL;
307 vcodec = vstream ? vstream->codec : NULL;
309 if (amf_type == AMF_DATA_TYPE_NUMBER) {
310 if (!strcmp(key, "duration"))
311 s->duration = num_val * AV_TIME_BASE;
312 else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
313 vcodec->bit_rate = num_val * 1024.0;
314 else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
315 acodec->bit_rate = num_val * 1024.0;
316 else if (!strcmp(key, "datastream")) {
317 AVStream *st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
319 return AVERROR(ENOMEM);
320 st->codec->codec_id = CODEC_ID_TEXT;
324 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
325 ((!acodec && !strcmp(key, "audiocodecid")) ||
326 (!vcodec && !strcmp(key, "videocodecid"))))
327 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
329 if (!strcmp(key, "duration") ||
330 !strcmp(key, "filesize") ||
331 !strcmp(key, "width") ||
332 !strcmp(key, "height") ||
333 !strcmp(key, "videodatarate") ||
334 !strcmp(key, "framerate") ||
335 !strcmp(key, "videocodecid") ||
336 !strcmp(key, "audiodatarate") ||
337 !strcmp(key, "audiosamplerate") ||
338 !strcmp(key, "audiosamplesize") ||
339 !strcmp(key, "stereo") ||
340 !strcmp(key, "audiocodecid"))
343 if(amf_type == AMF_DATA_TYPE_BOOL) {
344 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
345 av_dict_set(&s->metadata, key, str_val, 0);
346 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
347 snprintf(str_val, sizeof(str_val), "%.f", num_val);
348 av_dict_set(&s->metadata, key, str_val, 0);
349 } else if (amf_type == AMF_DATA_TYPE_STRING)
350 av_dict_set(&s->metadata, key, str_val, 0);
356 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
358 AVStream *stream, *astream, *vstream, *dstream;
361 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
363 vstream = astream = dstream = NULL;
366 //first object needs to be "onMetaData" string
368 if (type != AMF_DATA_TYPE_STRING ||
369 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
372 if (!strcmp(buffer, "onTextData"))
375 if (strcmp(buffer, "onMetaData"))
378 //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
379 for(i = 0; i < s->nb_streams; i++) {
380 stream = s->streams[i];
381 if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
382 else if(stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
383 else if(stream->codec->codec_type == AVMEDIA_TYPE_DATA) dstream = stream;
386 //parse the second object (we want a mixed array)
387 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
393 static int flv_read_header(AVFormatContext *s)
398 flags = avio_r8(s->pb);
399 /* old flvtool cleared this field */
400 /* FIXME: better fix needed */
402 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
403 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
405 s->ctx_flags |= AVFMTCTX_NOHEADER;
407 if(flags & FLV_HEADER_FLAG_HASVIDEO){
408 if(!create_stream(s, 0, AVMEDIA_TYPE_VIDEO))
409 return AVERROR(ENOMEM);
411 if(flags & FLV_HEADER_FLAG_HASAUDIO){
412 if(!create_stream(s, 1, AVMEDIA_TYPE_AUDIO))
413 return AVERROR(ENOMEM);
415 // Flag doesn't indicate whether or not there is script-data present. Must
416 // create that stream if it's encountered.
418 offset = avio_rb32(s->pb);
419 avio_seek(s->pb, offset, SEEK_SET);
427 static int flv_read_close(AVFormatContext *s)
430 FLVContext *flv = s->priv_data;
431 for(i=0; i<FLV_STREAM_TYPE_NB; i++)
432 av_freep(&flv->new_extradata[i]);
436 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
438 av_free(st->codec->extradata);
439 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
440 if (!st->codec->extradata)
441 return AVERROR(ENOMEM);
442 st->codec->extradata_size = size;
443 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
447 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
450 av_free(flv->new_extradata[stream]);
451 flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
452 if (!flv->new_extradata[stream])
453 return AVERROR(ENOMEM);
454 flv->new_extradata_size[stream] = size;
455 avio_read(pb, flv->new_extradata[stream], size);
459 static void clear_index_entries(AVFormatContext *s, int64_t pos)
462 av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
463 for (i = 0; i < s->nb_streams; i++) {
464 AVStream *st = s->streams[i];
465 /* Remove all index entries that point to >= pos */
467 for (j = 0; j < st->nb_index_entries; j++) {
468 if (st->index_entries[j].pos < pos)
469 st->index_entries[out++] = st->index_entries[j];
471 st->nb_index_entries = out;
476 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
477 int64_t dts, int64_t next)
479 int ret = AVERROR_INVALIDDATA, i;
480 AVIOContext *pb = s->pb;
487 if (type == AMF_DATA_TYPE_MIXEDARRAY)
488 avio_seek(pb, 4, SEEK_CUR);
489 else if (type != AMF_DATA_TYPE_OBJECT)
492 amf_get_string(pb, buf, sizeof(buf));
493 if (strcmp(buf, "type") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
496 amf_get_string(pb, buf, sizeof(buf));
497 //FIXME parse it as codec_id
498 amf_get_string(pb, buf, sizeof(buf));
499 if (strcmp(buf, "text") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
502 length = avio_rb16(pb);
503 ret = av_get_packet(s->pb, pkt, length);
509 for (i = 0; i < s->nb_streams; i++) {
515 if (i == s->nb_streams) {
516 st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
519 st->codec->codec_id = CODEC_ID_TEXT;
526 pkt->stream_index = st->index;
527 pkt->flags |= AV_PKT_FLAG_KEY;
529 avio_seek(s->pb, next + 4, SEEK_SET);
534 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
536 FLVContext *flv = s->priv_data;
537 int ret, i, type, size, flags;
540 int64_t dts, pts = AV_NOPTS_VALUE;
541 int av_uninit(channels);
542 int av_uninit(sample_rate);
545 for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
546 pos = avio_tell(s->pb);
547 type = avio_r8(s->pb);
548 size = avio_rb24(s->pb);
549 dts = avio_rb24(s->pb);
550 dts |= avio_r8(s->pb) << 24;
551 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
554 avio_skip(s->pb, 3); /* stream id, always 0 */
557 if (flv->validate_next < flv->validate_count) {
558 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
559 if (pos == validate_pos) {
560 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
561 VALIDATE_INDEX_TS_THRESH) {
562 flv->validate_next++;
564 clear_index_entries(s, validate_pos);
565 flv->validate_count = 0;
567 } else if (pos > validate_pos) {
568 clear_index_entries(s, validate_pos);
569 flv->validate_count = 0;
576 next= size + avio_tell(s->pb);
578 if (type == FLV_TAG_TYPE_AUDIO) {
579 stream_type=FLV_STREAM_TYPE_AUDIO;
580 flags = avio_r8(s->pb);
582 } else if (type == FLV_TAG_TYPE_VIDEO) {
583 stream_type=FLV_STREAM_TYPE_VIDEO;
584 flags = avio_r8(s->pb);
586 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
588 } else if (type == FLV_TAG_TYPE_META) {
589 if (size > 13+1+4 && dts == 0) { // Header-type metadata stuff
590 flv_read_metabody(s, next);
592 } else if (dts != 0) { // Script-data "special" metadata frames - don't skip
593 stream_type=FLV_STREAM_TYPE_DATA;
598 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
600 avio_seek(s->pb, next, SEEK_SET);
604 /* skip empty data packets */
608 /* now find stream */
609 for(i=0;i<s->nb_streams;i++) {
611 if (st->id == stream_type)
614 if(i == s->nb_streams){
615 av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
616 st = create_stream(s, stream_type,
617 (int[]){AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA}[stream_type]);
619 av_dlog(s, "%d %X %d \n", stream_type, flags, st->discard);
620 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
621 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
622 || st->discard >= AVDISCARD_ALL
624 avio_seek(s->pb, next, SEEK_SET);
627 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
628 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
632 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
633 if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
635 const int64_t pos= avio_tell(s->pb);
636 const int64_t fsize= avio_size(s->pb);
637 avio_seek(s->pb, fsize-4, SEEK_SET);
638 size= avio_rb32(s->pb);
639 avio_seek(s->pb, fsize-3-size, SEEK_SET);
640 if(size == avio_rb24(s->pb) + 11){
641 uint32_t ts = avio_rb24(s->pb);
642 ts |= avio_r8(s->pb) << 24;
643 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
645 avio_seek(s->pb, pos, SEEK_SET);
648 if(stream_type == FLV_STREAM_TYPE_AUDIO){
649 int bits_per_coded_sample;
650 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
651 sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
652 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
653 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
654 st->codec->channels = channels;
655 st->codec->sample_rate = sample_rate;
656 st->codec->bits_per_coded_sample = bits_per_coded_sample;
658 if(!st->codec->codec_id){
659 flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
660 flv->last_sample_rate = sample_rate = st->codec->sample_rate;
661 flv->last_channels = channels = st->codec->channels;
664 ctx.sample_rate = sample_rate;
665 flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
666 sample_rate = ctx.sample_rate;
668 } else if(stream_type == FLV_STREAM_TYPE_VIDEO) {
669 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
672 if (st->codec->codec_id == CODEC_ID_AAC ||
673 st->codec->codec_id == CODEC_ID_H264 ||
674 st->codec->codec_id == CODEC_ID_MPEG4) {
675 int type = avio_r8(s->pb);
677 if (st->codec->codec_id == CODEC_ID_H264 || st->codec->codec_id == CODEC_ID_MPEG4) {
678 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
680 if (cts < 0) { // dts are wrong
682 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
685 dts = AV_NOPTS_VALUE;
687 if (type == 0 && (!st->codec->extradata || st->codec->codec_id == CODEC_ID_AAC)) {
688 if (st->codec->extradata) {
689 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
691 ret = AVERROR(EAGAIN);
694 if ((ret = flv_get_extradata(s, st, size)) < 0)
696 if (st->codec->codec_id == CODEC_ID_AAC) {
697 MPEG4AudioConfig cfg;
698 if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
699 st->codec->extradata_size * 8, 1) >= 0) {
700 st->codec->channels = cfg.channels;
701 if (cfg.ext_sample_rate)
702 st->codec->sample_rate = cfg.ext_sample_rate;
704 st->codec->sample_rate = cfg.sample_rate;
705 av_dlog(s, "mp4a config channels %d sample rate %d\n",
706 st->codec->channels, st->codec->sample_rate);
710 ret = AVERROR(EAGAIN);
715 /* skip empty data packets */
717 ret = AVERROR(EAGAIN);
721 ret= av_get_packet(s->pb, pkt, size);
725 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
726 pkt->stream_index = st->index;
727 if (flv->new_extradata[stream_type]) {
728 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
729 flv->new_extradata_size[stream_type]);
731 memcpy(side, flv->new_extradata[stream_type],
732 flv->new_extradata_size[stream_type]);
733 av_freep(&flv->new_extradata[stream_type]);
734 flv->new_extradata_size[stream_type] = 0;
737 if (stream_type == FLV_STREAM_TYPE_AUDIO && (sample_rate != flv->last_sample_rate ||
738 channels != flv->last_channels)) {
739 flv->last_sample_rate = sample_rate;
740 flv->last_channels = channels;
741 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
744 if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
745 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
746 stream_type == FLV_STREAM_TYPE_DATA)
747 pkt->flags |= AV_PKT_FLAG_KEY;
754 static int flv_read_seek(AVFormatContext *s, int stream_index,
755 int64_t ts, int flags)
757 FLVContext *flv = s->priv_data;
758 flv->validate_count = 0;
759 return avio_seek_time(s->pb, stream_index, ts, flags);
762 AVInputFormat ff_flv_demuxer = {
764 .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
765 .priv_data_size = sizeof(FLVContext),
766 .read_probe = flv_probe,
767 .read_header = flv_read_header,
768 .read_packet = flv_read_packet,
769 .read_seek = flv_read_seek,
770 .read_close = flv_read_close,