3 * Copyright (c) 2003 The Libav Project
5 * This demuxer will generate a 1 byte extradata for VP6F content.
7 * - upper 4 bits: difference between encoded width and visible width
8 * - lower 4 bits: 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];
64 static int flv_probe(AVProbeData
*p
)
72 d
[3] < 5 && d
[5] == 0 &&
74 return AVPROBE_SCORE_MAX
;
79 static AVStream
*create_stream(AVFormatContext
*s
, int codec_type
)
81 FLVContext
*flv
= s
->priv_data
;
82 AVStream
*st
= avformat_new_stream(s
, NULL
);
85 st
->codecpar
->codec_type
= codec_type
;
86 if (codec_type
== AVMEDIA_TYPE_VIDEO
)
87 st
->avg_frame_rate
= flv
->framerate
;
89 avpriv_set_pts_info(st
, 32, 1, 1000); /* 32 bit pts in ms */
93 static int flv_same_audio_codec(AVCodecParameters
*apar
, int flags
)
95 int bits_per_coded_sample
= (flags
& FLV_AUDIO_SAMPLESIZE_MASK
) ?
16 : 8;
96 int flv_codecid
= flags
& FLV_AUDIO_CODECID_MASK
;
99 if (!apar
->codec_id
&& !apar
->codec_tag
)
102 if (apar
->bits_per_coded_sample
!= bits_per_coded_sample
)
105 switch (flv_codecid
) {
106 // no distinction between S16 and S8 PCM codec flags
107 case FLV_CODECID_PCM
:
108 codec_id
= bits_per_coded_sample
== 8
111 : AV_CODEC_ID_PCM_S16BE
;
113 : AV_CODEC_ID_PCM_S16LE
;
115 return codec_id
== apar
->codec_id
;
116 case FLV_CODECID_PCM_LE
:
117 codec_id
= bits_per_coded_sample
== 8
119 : AV_CODEC_ID_PCM_S16LE
;
120 return codec_id
== apar
->codec_id
;
121 case FLV_CODECID_AAC
:
122 return apar
->codec_id
== AV_CODEC_ID_AAC
;
123 case FLV_CODECID_ADPCM
:
124 return apar
->codec_id
== AV_CODEC_ID_ADPCM_SWF
;
125 case FLV_CODECID_SPEEX
:
126 return apar
->codec_id
== AV_CODEC_ID_SPEEX
;
127 case FLV_CODECID_MP3
:
128 return apar
->codec_id
== AV_CODEC_ID_MP3
;
129 case FLV_CODECID_NELLYMOSER_8KHZ_MONO
:
130 case FLV_CODECID_NELLYMOSER_16KHZ_MONO
:
131 case FLV_CODECID_NELLYMOSER
:
132 return apar
->codec_id
== AV_CODEC_ID_NELLYMOSER
;
133 case FLV_CODECID_PCM_MULAW
:
134 return apar
->sample_rate
== 8000 &&
135 apar
->codec_id
== AV_CODEC_ID_PCM_MULAW
;
136 case FLV_CODECID_PCM_ALAW
:
137 return apar
->sample_rate
== 8000 &&
138 apar
->codec_id
== AV_CODEC_ID_PCM_ALAW
;
140 return apar
->codec_tag
== (flv_codecid
>> FLV_AUDIO_CODECID_OFFSET
);
144 static void flv_set_audio_codec(AVFormatContext
*s
, AVStream
*astream
,
145 AVCodecParameters
*apar
, int flv_codecid
)
147 switch (flv_codecid
) {
148 // no distinction between S16 and S8 PCM codec flags
149 case FLV_CODECID_PCM
:
150 apar
->codec_id
= apar
->bits_per_coded_sample
== 8
153 : AV_CODEC_ID_PCM_S16BE
;
155 : AV_CODEC_ID_PCM_S16LE
;
158 case FLV_CODECID_PCM_LE
:
159 apar
->codec_id
= apar
->bits_per_coded_sample
== 8
161 : AV_CODEC_ID_PCM_S16LE
;
163 case FLV_CODECID_AAC
:
164 apar
->codec_id
= AV_CODEC_ID_AAC
;
166 case FLV_CODECID_ADPCM
:
167 apar
->codec_id
= AV_CODEC_ID_ADPCM_SWF
;
169 case FLV_CODECID_SPEEX
:
170 apar
->codec_id
= AV_CODEC_ID_SPEEX
;
171 apar
->sample_rate
= 16000;
173 case FLV_CODECID_MP3
:
174 apar
->codec_id
= AV_CODEC_ID_MP3
;
175 astream
->need_parsing
= AVSTREAM_PARSE_FULL
;
177 case FLV_CODECID_NELLYMOSER_8KHZ_MONO
:
178 // in case metadata does not otherwise declare samplerate
179 apar
->sample_rate
= 8000;
180 apar
->codec_id
= AV_CODEC_ID_NELLYMOSER
;
182 case FLV_CODECID_NELLYMOSER_16KHZ_MONO
:
183 apar
->sample_rate
= 16000;
184 apar
->codec_id
= AV_CODEC_ID_NELLYMOSER
;
186 case FLV_CODECID_NELLYMOSER
:
187 apar
->codec_id
= AV_CODEC_ID_NELLYMOSER
;
189 case FLV_CODECID_PCM_MULAW
:
190 apar
->sample_rate
= 8000;
191 apar
->codec_id
= AV_CODEC_ID_PCM_MULAW
;
193 case FLV_CODECID_PCM_ALAW
:
194 apar
->sample_rate
= 8000;
195 apar
->codec_id
= AV_CODEC_ID_PCM_ALAW
;
198 av_log(s
, AV_LOG_INFO
, "Unsupported audio codec (%x)\n",
199 flv_codecid
>> FLV_AUDIO_CODECID_OFFSET
);
200 apar
->codec_tag
= flv_codecid
>> FLV_AUDIO_CODECID_OFFSET
;
204 static int flv_same_video_codec(AVCodecParameters
*vpar
, int flags
)
206 int flv_codecid
= flags
& FLV_VIDEO_CODECID_MASK
;
208 if (!vpar
->codec_id
&& !vpar
->codec_tag
)
211 switch (flv_codecid
) {
212 case FLV_CODECID_H263
:
213 return vpar
->codec_id
== AV_CODEC_ID_FLV1
;
214 case FLV_CODECID_SCREEN
:
215 return vpar
->codec_id
== AV_CODEC_ID_FLASHSV
;
216 case FLV_CODECID_SCREEN2
:
217 return vpar
->codec_id
== AV_CODEC_ID_FLASHSV2
;
218 case FLV_CODECID_VP6
:
219 return vpar
->codec_id
== AV_CODEC_ID_VP6F
;
220 case FLV_CODECID_VP6A
:
221 return vpar
->codec_id
== AV_CODEC_ID_VP6A
;
222 case FLV_CODECID_H264
:
223 return vpar
->codec_id
== AV_CODEC_ID_H264
;
225 return vpar
->codec_tag
== flv_codecid
;
229 static int flv_set_video_codec(AVFormatContext
*s
, AVStream
*vstream
,
230 int flv_codecid
, int read
)
232 AVCodecParameters
*par
= vstream
->codecpar
;
233 switch (flv_codecid
) {
234 case FLV_CODECID_H263
:
235 par
->codec_id
= AV_CODEC_ID_FLV1
;
237 case FLV_CODECID_SCREEN
:
238 par
->codec_id
= AV_CODEC_ID_FLASHSV
;
240 case FLV_CODECID_SCREEN2
:
241 par
->codec_id
= AV_CODEC_ID_FLASHSV2
;
243 case FLV_CODECID_VP6
:
244 par
->codec_id
= AV_CODEC_ID_VP6F
;
245 case FLV_CODECID_VP6A
:
246 if (flv_codecid
== FLV_CODECID_VP6A
)
247 par
->codec_id
= AV_CODEC_ID_VP6A
;
249 if (par
->extradata_size
!= 1) {
250 par
->extradata
= av_malloc(1);
252 par
->extradata_size
= 1;
255 par
->extradata
[0] = avio_r8(s
->pb
);
259 return 1; // 1 byte body size adjustment for flv_read_packet()
260 case FLV_CODECID_H264
:
261 par
->codec_id
= AV_CODEC_ID_H264
;
262 return 3; // not 4, reading packet type will consume one byte
264 av_log(s
, AV_LOG_INFO
, "Unsupported video codec (%x)\n", flv_codecid
);
265 par
->codec_tag
= flv_codecid
;
271 static int amf_get_string(AVIOContext
*ioc
, char *buffer
, int buffsize
)
273 int length
= avio_rb16(ioc
);
274 if (length
>= buffsize
) {
275 avio_skip(ioc
, length
);
279 avio_read(ioc
, buffer
, length
);
281 buffer
[length
] = '\0';
286 static int parse_keyframes_index(AVFormatContext
*s
, AVIOContext
*ioc
,
287 AVStream
*vstream
, int64_t max_pos
)
289 FLVContext
*flv
= s
->priv_data
;
290 unsigned int arraylen
= 0, timeslen
= 0, fileposlen
= 0, i
;
293 int64_t *times
= NULL
;
294 int64_t *filepositions
= NULL
;
295 int ret
= AVERROR(ENOSYS
);
296 int64_t initial_pos
= avio_tell(ioc
);
298 if (s
->flags
& AVFMT_FLAG_IGNIDX
)
301 while (avio_tell(ioc
) < max_pos
- 2 &&
302 amf_get_string(ioc
, str_val
, sizeof(str_val
)) > 0) {
303 int64_t *current_array
;
305 // Expect array object in context
306 if (avio_r8(ioc
) != AMF_DATA_TYPE_ARRAY
)
309 arraylen
= avio_rb32(ioc
);
313 /* Expect only 'times' or 'filepositions' sub-arrays in other
314 * case refuse to use such metadata for indexing. */
315 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG
, str_val
) && !times
) {
316 if (!(times
= av_mallocz(sizeof(*times
) * arraylen
))) {
317 ret
= AVERROR(ENOMEM
);
321 current_array
= times
;
322 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG
, str_val
) &&
324 if (!(filepositions
= av_mallocz(sizeof(*filepositions
) * arraylen
))) {
325 ret
= AVERROR(ENOMEM
);
328 fileposlen
= arraylen
;
329 current_array
= filepositions
;
331 // unexpected metatag inside keyframes, will not use such
332 // metadata for indexing
335 for (i
= 0; i
< arraylen
&& avio_tell(ioc
) < max_pos
- 1; i
++) {
336 if (avio_r8(ioc
) != AMF_DATA_TYPE_NUMBER
)
338 num_val
= av_int2double(avio_rb64(ioc
));
339 current_array
[i
] = num_val
;
341 if (times
&& filepositions
) {
342 // All done, exiting at a position allowing amf_parse_object
343 // to finish parsing the object
349 if (!ret
&& timeslen
== fileposlen
) {
350 for (i
= 0; i
< fileposlen
; i
++) {
351 av_add_index_entry(vstream
, filepositions
[i
], times
[i
] * 1000,
352 0, 0, AVINDEX_KEYFRAME
);
354 flv
->validate_index
[i
].pos
= filepositions
[i
];
355 flv
->validate_index
[i
].dts
= times
[i
] * 1000;
356 flv
->validate_count
= i
+ 1;
360 av_log(s
, AV_LOG_WARNING
, "Invalid keyframes object, skipping.\n");
364 av_freep(&filepositions
);
365 // If we got unexpected data, but successfully reset back to
366 // the start pos, the caller can continue parsing
367 if (ret
< 0 && avio_seek(ioc
, initial_pos
, SEEK_SET
) > 0)
372 static int amf_parse_object(AVFormatContext
*s
, AVStream
*astream
,
373 AVStream
*vstream
, const char *key
,
374 int64_t max_pos
, int depth
)
376 AVCodecParameters
*apar
, *vpar
;
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
&&
400 !strcmp(KEYFRAMES_TAG
, key
) && depth
== 1)
401 if (parse_keyframes_index(s
, ioc
, vstream ? vstream
: astream
,
405 while (avio_tell(ioc
) < max_pos
- 2 &&
406 amf_get_string(ioc
, str_val
, sizeof(str_val
)) > 0)
407 if (amf_parse_object(s
, astream
, vstream
, str_val
, max_pos
,
409 return -1; // if we couldn't skip, bomb out.
410 if (avio_r8(ioc
) != AMF_END_OF_OBJECT
)
413 case AMF_DATA_TYPE_NULL
:
414 case AMF_DATA_TYPE_UNDEFINED
:
415 case AMF_DATA_TYPE_UNSUPPORTED
:
416 break; // these take up no additional space
417 case AMF_DATA_TYPE_MIXEDARRAY
:
418 avio_skip(ioc
, 4); // skip 32-bit max array index
419 while (avio_tell(ioc
) < max_pos
- 2 &&
420 amf_get_string(ioc
, str_val
, sizeof(str_val
)) > 0)
421 // this is the only case in which we would want a nested
422 // parse to not skip over the object
423 if (amf_parse_object(s
, astream
, vstream
, str_val
, max_pos
,
426 if (avio_r8(ioc
) != AMF_END_OF_OBJECT
)
429 case AMF_DATA_TYPE_ARRAY
:
431 unsigned int arraylen
, i
;
433 arraylen
= avio_rb32(ioc
);
434 for (i
= 0; i
< arraylen
&& avio_tell(ioc
) < max_pos
- 1; i
++)
435 if (amf_parse_object(s
, NULL
, NULL
, NULL
, max_pos
,
437 return -1; // if we couldn't skip, bomb out.
440 case AMF_DATA_TYPE_DATE
:
441 avio_skip(ioc
, 8 + 2); // timestamp (double) and UTC offset (int16)
443 default: // unsupported type, we couldn't skip
448 // stream info doesn't live any deeper than the first object
450 apar
= astream ? astream
->codecpar
: NULL
;
451 vpar
= vstream ? vstream
->codecpar
: 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") && vpar
&&
458 0 <= (int)(num_val
* 1024.0))
459 vpar
->bit_rate
= num_val
* 1024.0;
460 else if (!strcmp(key
, "audiodatarate") && apar
&&
461 0 <= (int)(num_val
* 1024.0))
462 apar
->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
->codecpar
->codec_id
= AV_CODEC_ID_TEXT
;
468 } else if (!strcmp(key
, "framerate")) {
469 flv
->framerate
= av_d2q(num_val
, 1000);
471 vstream
->avg_frame_rate
= flv
->framerate
;
472 } else if (flv
->trust_metadata
) {
473 if (!strcmp(key
, "videocodecid") && vpar
) {
474 flv_set_video_codec(s
, vstream
, num_val
, 0);
475 } else if (!strcmp(key
, "audiocodecid") && apar
) {
476 int id
= ((int)num_val
) << FLV_AUDIO_CODECID_OFFSET
;
477 flv_set_audio_codec(s
, astream
, apar
, id
);
478 } else if (!strcmp(key
, "audiosamplerate") && apar
) {
479 apar
->sample_rate
= num_val
;
480 } else if (!strcmp(key
, "audiosamplesize") && apar
) {
481 apar
->bits_per_coded_sample
= num_val
;
482 } else if (!strcmp(key
, "stereo") && apar
) {
483 apar
->channels
= num_val
+ 1;
484 apar
->channel_layout
= apar
->channels
== 2 ?
485 AV_CH_LAYOUT_STEREO
:
487 } else if (!strcmp(key
, "width") && vpar
) {
488 vpar
->width
= num_val
;
489 } else if (!strcmp(key
, "height") && vpar
) {
490 vpar
->height
= num_val
;
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 s
->event_flags
|= AVFMT_EVENT_FLAG_METADATA_UPDATED
;
512 if (amf_type
== AMF_DATA_TYPE_BOOL
) {
513 av_strlcpy(str_val
, num_val
> 0 ?
"true" : "false",
515 av_dict_set(&s
->metadata
, key
, str_val
, 0);
516 } else if (amf_type
== AMF_DATA_TYPE_NUMBER
) {
517 snprintf(str_val
, sizeof(str_val
), "%.f", num_val
);
518 av_dict_set(&s
->metadata
, key
, str_val
, 0);
519 } else if (amf_type
== AMF_DATA_TYPE_STRING
)
520 av_dict_set(&s
->metadata
, key
, str_val
, 0);
526 static int flv_read_metabody(AVFormatContext
*s
, int64_t next_pos
)
529 AVStream
*stream
, *astream
, *vstream
;
532 // only needs to hold the string "onMetaData".
533 // Anything longer is something we don't want.
540 // first object needs to be "onMetaData" string
542 if (type
!= AMF_DATA_TYPE_STRING
||
543 amf_get_string(ioc
, buffer
, sizeof(buffer
)) < 0)
546 if (!strcmp(buffer
, "onTextData"))
549 if (strcmp(buffer
, "onMetaData") && strcmp(buffer
, "onCuePoint"))
552 // find the streams now so that amf_parse_object doesn't need to do
553 // the lookup every time it is called.
554 for (i
= 0; i
< s
->nb_streams
; i
++) {
555 stream
= s
->streams
[i
];
556 if (stream
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
)
558 else if (stream
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
)
562 // parse the second object (we want a mixed array)
563 if (amf_parse_object(s
, astream
, vstream
, buffer
, next_pos
, 0) < 0)
569 static int flv_read_header(AVFormatContext
*s
)
574 avio_r8(s
->pb
); // flags
576 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
578 offset
= avio_rb32(s
->pb
);
579 avio_seek(s
->pb
, offset
, SEEK_SET
);
587 static int flv_read_close(AVFormatContext
*s
)
589 FLVContext
*flv
= s
->priv_data
;
590 av_freep(&flv
->new_extradata
[0]);
591 av_freep(&flv
->new_extradata
[1]);
595 static int flv_get_extradata(AVFormatContext
*s
, AVStream
*st
, int size
)
597 av_free(st
->codecpar
->extradata
);
598 st
->codecpar
->extradata
= av_mallocz(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
599 if (!st
->codecpar
->extradata
)
600 return AVERROR(ENOMEM
);
601 st
->codecpar
->extradata_size
= size
;
602 avio_read(s
->pb
, st
->codecpar
->extradata
, st
->codecpar
->extradata_size
);
606 static int flv_queue_extradata(FLVContext
*flv
, AVIOContext
*pb
, int stream
,
609 av_free(flv
->new_extradata
[stream
]);
610 flv
->new_extradata
[stream
] = av_mallocz(size
+
611 AV_INPUT_BUFFER_PADDING_SIZE
);
612 if (!flv
->new_extradata
[stream
])
613 return AVERROR(ENOMEM
);
614 flv
->new_extradata_size
[stream
] = size
;
615 avio_read(pb
, flv
->new_extradata
[stream
], size
);
619 static void clear_index_entries(AVFormatContext
*s
, int64_t pos
)
622 av_log(s
, AV_LOG_WARNING
,
623 "Found invalid index entries, clearing the index.\n");
624 for (i
= 0; i
< s
->nb_streams
; i
++) {
625 AVStream
*st
= s
->streams
[i
];
626 /* Remove all index entries that point to >= pos */
628 for (j
= 0; j
< st
->nb_index_entries
; j
++)
629 if (st
->index_entries
[j
].pos
< pos
)
630 st
->index_entries
[out
++] = st
->index_entries
[j
];
631 st
->nb_index_entries
= out
;
635 static int amf_skip_tag(AVIOContext
*pb
, AMFDataType type
)
637 int nb
= -1, ret
, parse_name
= 1;
640 case AMF_DATA_TYPE_NUMBER
:
643 case AMF_DATA_TYPE_BOOL
:
646 case AMF_DATA_TYPE_STRING
:
647 avio_skip(pb
, avio_rb16(pb
));
649 case AMF_DATA_TYPE_ARRAY
:
651 case AMF_DATA_TYPE_MIXEDARRAY
:
653 case AMF_DATA_TYPE_OBJECT
:
654 while(!pb
->eof_reached
&& (nb
-- > 0 || type
!= AMF_DATA_TYPE_ARRAY
)) {
656 int size
= avio_rb16(pb
);
663 if ((ret
= amf_skip_tag(pb
, avio_r8(pb
))) < 0)
667 case AMF_DATA_TYPE_NULL
:
668 case AMF_DATA_TYPE_OBJECT_END
:
671 return AVERROR_INVALIDDATA
;
676 static int flv_data_packet(AVFormatContext
*s
, AVPacket
*pkt
,
677 int64_t dts
, int64_t next
)
679 AVIOContext
*pb
= s
->pb
;
682 int ret
= AVERROR_INVALIDDATA
;
685 switch (avio_r8(pb
)) {
686 case AMF_DATA_TYPE_MIXEDARRAY
:
687 avio_seek(pb
, 4, SEEK_CUR
);
688 case AMF_DATA_TYPE_OBJECT
:
694 while ((ret
= amf_get_string(pb
, buf
, sizeof(buf
))) > 0) {
695 AMFDataType type
= avio_r8(pb
);
696 if (type
== AMF_DATA_TYPE_STRING
&& !strcmp(buf
, "text")) {
697 length
= avio_rb16(pb
);
698 ret
= av_get_packet(pb
, pkt
, length
);
704 if ((ret
= amf_skip_tag(pb
, type
)) < 0)
710 ret
= AVERROR_INVALIDDATA
;
714 for (i
= 0; i
< s
->nb_streams
; i
++) {
716 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_DATA
)
720 if (i
== s
->nb_streams
) {
721 st
= create_stream(s
, AVMEDIA_TYPE_DATA
);
723 return AVERROR(ENOMEM
);
724 st
->codecpar
->codec_id
= AV_CODEC_ID_TEXT
;
731 pkt
->stream_index
= st
->index
;
732 pkt
->flags
|= AV_PKT_FLAG_KEY
;
735 avio_seek(s
->pb
, next
+ 4, SEEK_SET
);
740 static int flv_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
742 FLVContext
*flv
= s
->priv_data
;
743 int ret
, i
, size
, flags
, is_audio
;
744 enum FlvTagType type
;
746 int64_t dts
, pts
= AV_NOPTS_VALUE
;
747 int sample_rate
= 0, channels
= 0;
750 /* pkt size is repeated at end. skip it */
751 for (;; avio_skip(s
->pb
, 4)) {
752 pos
= avio_tell(s
->pb
);
753 type
= avio_r8(s
->pb
);
754 size
= avio_rb24(s
->pb
);
755 dts
= avio_rb24(s
->pb
);
756 dts
|= avio_r8(s
->pb
) << 24;
757 av_log(s
, AV_LOG_TRACE
, "type:%d, size:%d, dts:%"PRId64
"\n", type
, size
, dts
);
758 if (s
->pb
->eof_reached
)
760 avio_skip(s
->pb
, 3); /* stream id, always 0 */
763 if (flv
->validate_next
< flv
->validate_count
) {
764 int64_t validate_pos
= flv
->validate_index
[flv
->validate_next
].pos
;
765 if (pos
== validate_pos
) {
766 if (FFABS(dts
- flv
->validate_index
[flv
->validate_next
].dts
) <=
767 VALIDATE_INDEX_TS_THRESH
) {
768 flv
->validate_next
++;
770 clear_index_entries(s
, validate_pos
);
771 flv
->validate_count
= 0;
773 } else if (pos
> validate_pos
) {
774 clear_index_entries(s
, validate_pos
);
775 flv
->validate_count
= 0;
782 next
= size
+ avio_tell(s
->pb
);
784 if (type
== FLV_TAG_TYPE_AUDIO
) {
786 flags
= avio_r8(s
->pb
);
788 } else if (type
== FLV_TAG_TYPE_VIDEO
) {
790 flags
= avio_r8(s
->pb
);
792 if ((flags
& 0xf0) == 0x50) /* video info / command frame */
795 if (type
== FLV_TAG_TYPE_META
&& size
> 13 + 1 + 4)
796 if (flv_read_metabody(s
, next
) > 0) {
797 return flv_data_packet(s
, pkt
, dts
, next
);
798 } else /* skip packet */
799 av_log(s
, AV_LOG_DEBUG
,
800 "Skipping flv packet: type %d, size %d, flags %d.\n",
804 if (avio_seek(s
->pb
, next
, SEEK_SET
) != next
) {
805 // This can happen if flv_read_metabody above read past
806 // next, on a non-seekable input, and the preceding data has
807 // been flushed out from the IO buffer.
808 av_log(s
, AV_LOG_ERROR
, "Unable to seek to the next packet\n");
809 return AVERROR_INVALIDDATA
;
814 /* skip empty data packets */
818 /* now find stream */
819 for (i
= 0; i
< s
->nb_streams
; i
++) {
821 if (is_audio
&& st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
822 if (flv_same_audio_codec(st
->codecpar
, flags
))
824 } else if (!is_audio
&&
825 st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
826 if (flv_same_video_codec(st
->codecpar
, flags
))
830 if (i
== s
->nb_streams
) {
831 st
= create_stream(s
, is_audio ? AVMEDIA_TYPE_AUDIO
832 : AVMEDIA_TYPE_VIDEO
);
834 return AVERROR(ENOMEM
);
836 av_log(s
, AV_LOG_TRACE
, "%d %X %d \n", is_audio
, flags
, st
->discard
);
838 if ((flags
& FLV_VIDEO_FRAMETYPE_MASK
) == FLV_FRAME_KEY
||
840 av_add_index_entry(st
, pos
, dts
, size
, 0, AVINDEX_KEYFRAME
);
842 if ((st
->discard
>= AVDISCARD_NONKEY
&&
843 !((flags
& FLV_VIDEO_FRAMETYPE_MASK
) == FLV_FRAME_KEY
|| is_audio
)) ||
844 (st
->discard
>= AVDISCARD_BIDIR
&&
845 ((flags
& FLV_VIDEO_FRAMETYPE_MASK
) == FLV_FRAME_DISP_INTER
&& !is_audio
)) ||
846 st
->discard
>= AVDISCARD_ALL
) {
847 avio_seek(s
->pb
, next
, SEEK_SET
);
853 // if not streamed and no duration from metadata then seek to end to find
854 // the duration from the timestamps
855 if ((s
->pb
->seekable
& AVIO_SEEKABLE_NORMAL
) &&
856 (!s
->duration
|| s
->duration
== AV_NOPTS_VALUE
) &&
857 !flv
->searched_for_end
) {
859 const int64_t pos
= avio_tell(s
->pb
);
860 // Read the last 4 bytes of the file, this should be the size of the
861 // previous FLV tag. Use the timestamp of its payload as duration.
862 const int64_t fsize
= avio_size(s
->pb
);
863 avio_seek(s
->pb
, fsize
- 4, SEEK_SET
);
864 size
= avio_rb32(s
->pb
);
865 if (size
> 0 && size
< fsize
) {
866 // Seek to the start of the last FLV tag at position (fsize - 4 - size)
867 // but skip the byte indicating the type.
868 avio_seek(s
->pb
, fsize
- 3 - size
, SEEK_SET
);
869 if (size
== avio_rb24(s
->pb
) + 11) {
870 uint32_t ts
= avio_rb24(s
->pb
);
871 ts
|= avio_r8(s
->pb
) << 24;
872 s
->duration
= ts
* (int64_t)AV_TIME_BASE
/ 1000;
875 avio_seek(s
->pb
, pos
, SEEK_SET
);
876 flv
->searched_for_end
= 1;
880 int bits_per_coded_sample
;
881 channels
= (flags
& FLV_AUDIO_CHANNEL_MASK
) == FLV_STEREO ?
2 : 1;
882 sample_rate
= 44100 << ((flags
& FLV_AUDIO_SAMPLERATE_MASK
) >>
883 FLV_AUDIO_SAMPLERATE_OFFSET
) >> 3;
884 bits_per_coded_sample
= (flags
& FLV_AUDIO_SAMPLESIZE_MASK
) ?
16 : 8;
885 if (!st
->codecpar
->channels
|| !st
->codecpar
->sample_rate
||
886 !st
->codecpar
->bits_per_coded_sample
) {
887 st
->codecpar
->channels
= channels
;
888 st
->codecpar
->channel_layout
= channels
== 1
890 : AV_CH_LAYOUT_STEREO
;
891 st
->codecpar
->sample_rate
= sample_rate
;
892 st
->codecpar
->bits_per_coded_sample
= bits_per_coded_sample
;
894 if (!st
->codecpar
->codec_id
) {
895 flv_set_audio_codec(s
, st
, st
->codecpar
,
896 flags
& FLV_AUDIO_CODECID_MASK
);
897 flv
->last_sample_rate
=
898 sample_rate
= st
->codecpar
->sample_rate
;
900 channels
= st
->codecpar
->channels
;
902 AVCodecParameters
*par
= avcodec_parameters_alloc();
904 ret
= AVERROR(ENOMEM
);
907 par
->sample_rate
= sample_rate
;
908 par
->bits_per_coded_sample
= bits_per_coded_sample
;
909 flv_set_audio_codec(s
, st
, par
, flags
& FLV_AUDIO_CODECID_MASK
);
910 sample_rate
= par
->sample_rate
;
911 avcodec_parameters_free(&par
);
914 size
-= flv_set_video_codec(s
, st
, flags
& FLV_VIDEO_CODECID_MASK
, 1);
917 if (st
->codecpar
->codec_id
== AV_CODEC_ID_AAC
||
918 st
->codecpar
->codec_id
== AV_CODEC_ID_H264
) {
919 int type
= avio_r8(s
->pb
);
923 ret
= AVERROR_INVALIDDATA
;
927 if (st
->codecpar
->codec_id
== AV_CODEC_ID_H264
) {
929 int32_t cts
= (avio_rb24(s
->pb
) + 0xff800000) ^ 0xff800000;
931 if (cts
< 0 && !flv
->wrong_dts
) { // dts might be wrong
933 av_log(s
, AV_LOG_WARNING
,
934 "Negative cts, previous timestamps might be wrong.\n");
938 if (st
->codecpar
->extradata
) {
939 if ((ret
= flv_queue_extradata(flv
, s
->pb
, is_audio
, size
)) < 0)
941 ret
= AVERROR(EAGAIN
);
944 if ((ret
= flv_get_extradata(s
, st
, size
)) < 0)
946 if (st
->codecpar
->codec_id
== AV_CODEC_ID_AAC
) {
947 MPEG4AudioConfig cfg
;
949 /* Workaround for buggy Omnia A/XE encoder */
950 AVDictionaryEntry
*t
= av_dict_get(s
->metadata
, "Encoder", NULL
, 0);
951 if (t
&& !strcmp(t
->value
, "Omnia A/XE"))
952 st
->codecpar
->extradata_size
= 2;
954 avpriv_mpeg4audio_get_config(&cfg
, st
->codecpar
->extradata
,
955 st
->codecpar
->extradata_size
* 8, 1);
956 st
->codecpar
->channels
= cfg
.channels
;
957 st
->codecpar
->channel_layout
= 0;
958 if (cfg
.ext_sample_rate
)
959 st
->codecpar
->sample_rate
= cfg
.ext_sample_rate
;
961 st
->codecpar
->sample_rate
= cfg
.sample_rate
;
962 av_log(s
, AV_LOG_TRACE
, "mp4a config channels %d sample rate %d\n",
963 st
->codecpar
->channels
, st
->codecpar
->sample_rate
);
966 ret
= AVERROR(EAGAIN
);
971 /* skip empty data packets */
973 ret
= AVERROR(EAGAIN
);
977 ret
= av_get_packet(s
->pb
, pkt
, size
);
980 /* note: we need to modify the packet size here to handle the last
984 pkt
->pts
= pts
== AV_NOPTS_VALUE ? dts
: pts
;
985 pkt
->stream_index
= st
->index
;
986 if (flv
->new_extradata
[is_audio
]) {
987 uint8_t *side
= av_packet_new_side_data(pkt
, AV_PKT_DATA_NEW_EXTRADATA
,
988 flv
->new_extradata_size
[is_audio
]);
990 memcpy(side
, flv
->new_extradata
[is_audio
],
991 flv
->new_extradata_size
[is_audio
]);
992 av_freep(&flv
->new_extradata
[is_audio
]);
993 flv
->new_extradata_size
[is_audio
] = 0;
996 if (is_audio
&& (sample_rate
!= flv
->last_sample_rate
||
997 channels
!= flv
->last_channels
)) {
998 flv
->last_sample_rate
= sample_rate
;
999 flv
->last_channels
= channels
;
1000 ff_add_param_change(pkt
, channels
, 0, sample_rate
, 0, 0);
1003 if (is_audio
|| ((flags
& FLV_VIDEO_FRAMETYPE_MASK
) == FLV_FRAME_KEY
))
1004 pkt
->flags
|= AV_PKT_FLAG_KEY
;
1007 avio_skip(s
->pb
, 4);
1011 static int flv_read_seek(AVFormatContext
*s
, int stream_index
,
1012 int64_t ts
, int flags
)
1014 FLVContext
*flv
= s
->priv_data
;
1015 flv
->validate_count
= 0;
1016 return avio_seek_time(s
->pb
, stream_index
, ts
, flags
);
1019 #define OFFSET(x) offsetof(FLVContext, x)
1020 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1021 static const AVOption options
[] = {
1022 { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, 1, VD
},
1026 static const AVClass
class = {
1027 .class_name
= "flvdec",
1028 .item_name
= av_default_item_name
,
1030 .version
= LIBAVUTIL_VERSION_INT
,
1033 AVInputFormat ff_flv_demuxer
= {
1035 .long_name
= NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1036 .priv_data_size
= sizeof(FLVContext
),
1037 .read_probe
= flv_probe
,
1038 .read_header
= flv_read_header
,
1039 .read_packet
= flv_read_packet
,
1040 .read_seek
= flv_read_seek
,
1041 .read_close
= flv_read_close
,
1042 .extensions
= "flv",
1043 .priv_class
= &class,