1 /*
2 * FLV demuxer
3 * Copyright (c) 2003 The FFmpeg Project
4 *
5 * This demuxer will generate a 1 byte extradata for VP6F content.
6 * It is composed of:
7 * - upper 4bits: difference between encoded width and visible width
8 * - lower 4bits: difference between encoded height and visible height
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
39
40 #define VALIDATE_INDEX_TS_THRESH 2500
41
42 #define RESYNC_BUFFER_SIZE (1<<20)
43
45 const AVClass *
class;
///< Class for private options.
52 struct {
59
61
65
67 {
70
71 if (d[0] == 'F' &&
72 d[1] == 'L' &&
73 d[2] == 'V' &&
74 d[3] < 5 && d[5] == 0 &&
76 offset > 8) {
77 int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
78
79 if (live == is_live)
81 }
82 return 0;
83 }
84
86 {
88 }
89
91 {
93 }
94
96 {
98 if (!st)
105
107 return st;
108 }
109
111 {
115
117 return 1;
118
120 return 0;
121
122 switch (flv_codecid) {
123 // no distinction between S16 and S8 PCM codec flags
125 codec_id = bits_per_coded_sample == 8
127 #if HAVE_BIGENDIAN
129 #else
131 #endif
132 return codec_id == acodec->
codec_id;
134 codec_id = bits_per_coded_sample == 8
137 return codec_id == acodec->
codec_id;
156 default:
158 }
159 }
160
163 {
164 switch (flv_codecid) {
165 // no distinction between S16 and S8 PCM codec flags
169 #if HAVE_BIGENDIAN
171 #else
173 #endif
174 break;
179 break;
182 break;
185 break;
189 break;
193 break;
195 // in case metadata does not otherwise declare samplerate
198 break;
202 break;
205 break;
209 break;
213 break;
214 default:
218 }
219 }
220
222 {
224
226 return 1;
227
228 switch (flv_codecid) {
241 default:
243 }
244 }
245
247 int flv_codecid, int read)
248 {
250 switch (flv_codecid) {
253 break;
256 break; // Really mean it this time
259 break;
262 break;
268 if (read) {
271 }
274 else
276 }
277 return 1; // 1 byte body size adjustment for flv_read_packet()
281 return 3; // not 4, reading packet type will consume one byte
284 return 3;
285 default:
288 }
289
290 return 0;
291 }
292
294 {
296 if (length >= buffsize) {
298 return -1;
299 }
300
302
304
306 }
307
310 {
312 unsigned int timeslen = 0, fileposlen = 0, i;
313 char str_val[256];
314 int64_t *times =
NULL;
315 int64_t *filepositions =
NULL;
318
321 return 0;
322 }
323
325 return 0;
326
329 int64_t **current_array;
330 unsigned int arraylen;
331
332 // Expect array object in context
334 break;
335
337 if (arraylen>>28)
338 break;
339
341 current_array = ×
342 timeslen = arraylen;
344 !filepositions) {
345 current_array = &filepositions;
346 fileposlen = arraylen;
347 } else
348 // unexpected metatag inside keyframes, will not use such
349 // metadata for indexing
350 break;
351
352 if (!(*current_array =
av_mallocz(
sizeof(**current_array) * arraylen))) {
355 }
356
357 for (i = 0; i < arraylen &&
avio_tell(ioc) < max_pos - 1; i++) {
359 goto invalid;
361 }
362 if (times && filepositions) {
363 // All done, exiting at a position allowing amf_parse_object
364 // to finish parsing the object
365 ret = 0;
366 break;
367 }
368 }
369
370 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
371 for (i = 0; i < fileposlen; i++) {
374 if (i < 2) {
378 }
379 }
380 } else {
381 invalid:
383 }
384
389 return ret;
390 }
391
395 {
400 char str_val[1024];
401 double num_val;
402
403 num_val = 0;
406
407 switch (amf_type) {
410 break;
413 break;
417 return -1;
418 }
419 break;
421 if ((vstream || astream) && key &&
425 max_pos) < 0)
427
431 depth + 1) < 0)
432 return -1; // if we couldn't skip, bomb out.
435 return -1;
436 }
437 break;
441 break; // these take up no additional space
443 {
444 unsigned v;
445 avio_skip(ioc, 4);
// skip 32-bit max array index
448 // this is the only case in which we would want a nested
449 // parse to not skip over the object
451 depth + 1) < 0)
452 return -1;
455 av_log(s,
AV_LOG_ERROR,
"Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
456 return -1;
457 }
458 break;
459 }
461 {
462 unsigned int arraylen, i;
463
465 for (i = 0; i < arraylen &&
avio_tell(ioc) < max_pos - 1; i++)
467 depth + 1) < 0)
468 return -1; // if we couldn't skip, bomb out.
469 }
470 break;
472 avio_skip(ioc, 8 + 2);
// timestamp (double) and UTC offset (int16)
473 break;
474 default: // unsupported type, we couldn't skip
476 return -1;
477 }
478
479 if (key) {
480 acodec = astream ? astream->
codec :
NULL;
481 vcodec = vstream ? vstream->
codec :
NULL;
482
483 // stream info doesn't live any deeper than the first object
484 if (depth == 1) {
487 if (!strcmp(key, "duration"))
489 else if (!strcmp(key, "videodatarate") && vcodec &&
490 0 <= (int)(num_val * 1024.0))
491 vcodec->
bit_rate = num_val * 1024.0;
492 else if (!strcmp(key, "audiodatarate") && acodec &&
493 0 <= (int)(num_val * 1024.0))
494 acodec->
bit_rate = num_val * 1024.0;
495 else if (!strcmp(key, "datastream")) {
497 if (!st)
501 if (!strcmp(key, "videocodecid") && vcodec) {
503 } else if (!strcmp(key, "audiocodecid") && acodec) {
506 } else if (!strcmp(key, "audiosamplerate") && acodec) {
508 } else if (!strcmp(key, "audiosamplesize") && acodec) {
510 } else if (!strcmp(key, "stereo") && acodec) {
515 } else if (!strcmp(key, "width") && vcodec) {
516 vcodec->
width = num_val;
517 } else if (!strcmp(key, "height") && vcodec) {
519 }
520 }
521 }
523 if (!strcmp(key, "encoder")) {
525 if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
526 if (version > 0 && version <= 655)
528 }
529 } else if (!strcmp(key, "metadatacreator") && !strcmp(str_val, "MEGA")) {
531 }
532 }
533 }
534
536 ((!acodec && !strcmp(key, "audiocodecid")) ||
537 (!vcodec && !strcmp(key, "videocodecid"))))
539
540 if (!strcmp(key, "duration") ||
541 !strcmp(key, "filesize") ||
542 !strcmp(key, "width") ||
543 !strcmp(key, "height") ||
544 !strcmp(key, "videodatarate") ||
545 !strcmp(key, "framerate") ||
546 !strcmp(key, "videocodecid") ||
547 !strcmp(key, "audiodatarate") ||
548 !strcmp(key, "audiosamplerate") ||
549 !strcmp(key, "audiosamplesize") ||
550 !strcmp(key, "stereo") ||
551 !strcmp(key, "audiocodecid") ||
552 !strcmp(key, "datastream"))
553 return 0;
554
557 av_strlcpy(str_val, num_val > 0 ?
"true" :
"false",
558 sizeof(str_val));
561 snprintf(str_val,
sizeof(str_val),
"%.f", num_val);
565 }
566
567 return 0;
568 }
569
570 #define TYPE_ONTEXTDATA 1
571 #define TYPE_ONCAPTION 2
572 #define TYPE_ONCAPTIONINFO 3
573 #define TYPE_UNKNOWN 9
574
576 {
578 AVStream *stream, *astream, *vstream;
581 int i;
582 // only needs to hold the string "onMetaData".
583 // Anything longer is something we don't want.
585
590
591 // first object needs to be "onMetaData" string
596
597 if (!strcmp(buffer, "onTextData"))
599
600 if (!strcmp(buffer, "onCaption"))
602
603 if (!strcmp(buffer, "onCaptionInfo"))
605
606 if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
609 }
610
611 // find the streams now so that amf_parse_object doesn't need to do
612 // the lookup every time it is called.
616 vstream = stream;
618 astream = stream;
620 dstream = stream;
621 }
622
623 // parse the second object (we want a mixed array)
625 return -1;
626
627 return 0;
628 }
629
631 {
634
637
639
646 // Flag doesn't indicate whether or not there is script-data present. Must
647 // create that stream if it's encountered.
648
652
655
656 return 0;
657 }
658
660 {
661 int i;
665 return 0;
666 }
667
669 {
673 return 0;
674 }
675
678 {
686 return 0;
687 }
688
690 {
693 "Found invalid index entries, clearing the index.\n");
696 /* Remove all index entries that point to >= pos */
697 out = 0;
702 }
703 }
704
706 {
707 int nb = -1, ret, parse_name = 1;
708
709 switch (type) {
712 break;
715 break;
718 break;
720 parse_name = 0;
725 if (parse_name) {
727 if (!size) {
729 break;
730 }
732 }
734 return ret;
735 }
736 break;
739 break;
740 default:
742 }
743 return 0;
744 }
745
747 int64_t dts, int64_t next)
748 {
754 int array = 0;
755
758 array = 1;
762 break;
763 default:
764 goto skip;
765 }
766
767 while (array || (ret =
amf_get_string(pb, buf,
sizeof(buf))) > 0) {
772 if (ret < 0)
773 goto skip;
774 else
775 break;
776 } else {
778 goto skip;
779 }
780 }
781
782 if (length < 0) {
784 goto skip;
785 }
786
790 break;
791 }
792
795 if (!st)
798 }
799
803
806
807 skip:
809
810 return ret;
811 }
812
814 {
816 int64_t i;
818
824
825 if (i > 22) {
827 if (lsize2 >= 11 && lsize2 + 8LL <
FFMIN(i, RESYNC_BUFFER_SIZE)) {
830 if (lsize1 >= 11 && lsize1 + 8LL + lsize2 <
FFMIN(i, RESYNC_BUFFER_SIZE)) {
832 if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
833 avio_seek(s->
pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
834 return 1;
835 }
836 }
837 }
838 }
839 }
841 }
842
844 {
848 int stream_type=-1;
849 int64_t next, pos, meta_pos;
854 int last = -1;
855 int orig_size;
856
857 retry:
858 /* pkt size is repeated at end. skip it */
861 orig_size =
866 av_log(s,
AV_LOG_TRACE,
"type:%d, size:%d, last:%d, dts:%"PRId64
" pos:%"PRId64
"\n", type, size, last, dts,
avio_tell(s->
pb));
870 flags = 0;
871
874 if (pos == validate_pos) {
878 } else {
881 }
882 } else if (pos > validate_pos) {
885 }
886 }
887
888 if (size == 0) {
890 goto leave;
891 }
892
894
898 size--;
902 size--;
904 goto skip;
907 if (size > 13 + 1 + 4) { // Header-type metadata stuff
911 if (type == 0 && dts == 0 || type < 0 || type ==
TYPE_UNKNOWN) {
915 ) {
918 }
919 goto skip;
925 }
927 }
928 } else {
930 "Skipping flv packet: type %d, size %d, flags %d.\n",
931 type, size, flags);
932 skip:
935 goto leave;
936 }
937
938 /* skip empty data packets */
939 if (!size) {
941 goto leave;
942 }
943
944 /* now find stream */
950 break;
954 break;
957 break;
958 }
959 }
964 if (!st)
966
967 }
969
974
978 ) {
981 goto leave;
982 }
983
984 // if not streamed and no duration from metadata then seek to end to find
985 // the duration from the timestamps
990 // Read the last 4 bytes of the file, this should be the size of the
991 // previous FLV tag. Use the timestamp of its payload as duration.
993 retry_duration:
996 if (size > 0 && size < fsize) {
997 // Seek to the start of the last FLV tag at position (fsize - 4 - size)
998 // but skip the byte indicating the type.
1003 if (ts)
1005 else if (fsize >= 8 && fsize - 8 >= size) {
1006 fsize -= size+4;
1007 goto retry_duration;
1008 }
1009 }
1010 }
1011
1014 }
1015
1017 int bits_per_coded_sample;
1030 }
1038 } else {
1044 }
1049 }
1050
1055 size--;
1057 // sign extension
1059 pts = dts + cts;
1060 if (cts < 0) { // dts might be wrong
1063 "Negative cts, previous timestamps might be wrong.\n");
1065 }
else if (
FFABS(dts - pts) > 1000*60*15) {
1067 "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1069 }
1070 }
1074
1077 return ret;
1079 goto leave;
1080 }
1082 return ret;
1083
1084 /* Workaround for buggy Omnia A/XE encoder */
1088
1091
1098 else
1102 }
1103 }
1104
1106 goto leave;
1107 }
1108 }
1109
1110 /* skip empty data packets */
1111 if (!size) {
1113 goto leave;
1114 }
1115
1117 if (ret < 0)
1118 return ret;
1125 if (side) {
1130 }
1131 }
1138 }
1139
1144
1145 leave:
1147 if (last != orig_size + 11 &&
1154 if (ret >= 0) {
1155 goto retry;
1156 }
1157 }
1158 return ret;
1159 }
1160
1162 int64_t ts,
int flags)
1163 {
1167 }
1168
1169 #define OFFSET(x) offsetof(FLVContext, x)
1170 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1172 {
"flv_metadata",
"Allocate streams according to the onMetaData array",
OFFSET(trust_metadata),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VD },
1174 };
1175
1181 };
1182
1192 .extensions = "flv",
1193 .priv_class = &flv_class,
1194 };
1195
1201 };
1202
1212 .extensions = "flv",
1213 .priv_class = &live_flv_class,
1215 };
struct FLVContext::@170 validate_index[2]
discard all frames except keyframes
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int64_t avio_size(AVIOContext *s)
Get the filesize.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
static const int32_t max_pos[4]
Size of the MP-MLQ fixed excitation codebooks.
#define LIBAVUTIL_VERSION_INT
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
int index
stream index in AVFormatContext
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
enum AVMediaType codec_type
int event_flags
Flags for the user to detect events happening on the file.
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
#define AV_CH_LAYOUT_STEREO
unsigned int avio_rb16(AVIOContext *s)
int ctx_flags
Flags signalling stream properties.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
unsigned int avio_rb32(AVIOContext *s)
int wrong_dts
wrong dts due to negative cts
enum AVStreamParseType need_parsing
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
int flags
Flags modifying the (de)muxer behaviour.
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
uint64_t avio_rb64(AVIOContext *s)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int trust_metadata
configure streams according onMetaData
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
enum AVCodecID video_codec_id
Forced video codec_id.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
discard all bidirectional frames
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
static const uint8_t offset[127][2]
uint8_t * new_extradata[FLV_STREAM_TYPE_NB]
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
uint8_t resync_buffer[2 *RESYNC_BUFFER_SIZE]
int flags
A combination of AV_PKT_FLAG values.
uint64_t channel_layout
Audio channel layout.
int avio_r8(AVIOContext *s)
AVCodecContext * codec
Codec context associated with this stream.
int buf_size
Size of buf except extra allocated bytes.
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
audio channel layout utility functions
unsigned int avio_rb24(AVIOContext *s)
#define AV_TIME_BASE
Internal time base represented as integer.
enum AVCodecID audio_codec_id
Forced audio codec_id.
int width
picture width / height.
int new_extradata_size[FLV_STREAM_TYPE_NB]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static int read_header(FFV1Context *f)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
enum AVMediaType codec_type
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
int sample_rate
samples per second
AVIOContext * pb
I/O context.
main external API structure.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
This structure contains the data a format has to probe a file.
static int64_t pts
Global timestamp for the audio frames.
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata to retrieve audio configuration.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
int eof_reached
true if eof reached
int channels
number of audio channels
void * priv_data
Format private data.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
#define AV_CH_LAYOUT_MONO
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.