1 /*
2 * FLV muxer
3 * Copyright (c) 2003 The FFmpeg Project
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
37
38
50 };
51
64 };
65
73
79
86 int64_t
delay;
///< first dts delay (needed for AVC & Speex)
87
95
100
107
110
114
119
122
124 int64_t
last_ts;
///< last timestamp for each stream
126
128 {
131
138 "FLV only supports wideband (16kHz) Speex audio\n");
140 }
144 }
146 } else {
148 case 48000:
149 // 48khz mp3 is stored with 44k1 samplerate identifer
152 break;
153 } else {
155 }
156 case 44100:
158 break;
159 case 22050:
161 break;
162 case 11025:
164 break;
165 case 16000: // nellymoser only
166 case 8000: // nellymoser only
167 case 5512: // not MP3
170 break;
171 }
172 default:
175 "FLV does not support sample rate %d, "
176 "choose from (44100, 22050, 11025)\n", par->
sample_rate);
178 }
179 }
180
183
187 break;
190 break;
193 break;
196 break;
199 break;
205 else
207 break;
210 break;
213 break;
214 case 0:
216 break;
217 default:
221 }
222
224 }
225
227 {
228 size_t len = strlen(str);
231 }
232
234 {
237 avio_wb24(pb, ts);
/* lower 24 bits of timestamp in ms */
238 avio_w8(pb, (ts >> 24) & 0x7F);
/* MSB of ts in ms */
240 avio_w8(pb, 23);
/* ub[4] FrameType = 1, ub[4] CodecId = 7 */
241 avio_w8(pb, 2);
/* AVC end of sequence */
242 avio_wb24(pb, 0);
/* Always 0 for AVC EOS. */
244 }
245
247 {
250 }
251
253 {
255 }
256
258 {
261 }
262
264 {
267 }
268
270 {
274 int metadata_count = 0;
275 int64_t metadata_count_pos;
277
278 /* write meta_tag */
281 avio_wb24(pb, 0);
// size of data part (sum of all parts below)
284
285 /* now data of data_size size */
286
287 /* first event name as a string */
290
291 /* mixed array (hash) with size and string/type/data tuples */
297 if (write_duration_filesize) {
298 metadata_count += 2; // +2 for duration and file size
299 }
301
302 if (write_duration_filesize) {
305 // fill in the guessed duration, it'll be corrected later if incorrect
307 }
308
312
315
318
322 metadata_count++;
323 }
324
327 }
328
332
335
338
341
344 }
345
349 }
350
353 if( !strcmp(tag->
key,
"width")
354 ||!strcmp(tag->
key,
"height")
355 ||!strcmp(tag->
key,
"videodatarate")
356 ||!strcmp(tag->
key,
"framerate")
357 ||!strcmp(tag->
key,
"videocodecid")
358 ||!strcmp(tag->
key,
"audiodatarate")
359 ||!strcmp(tag->
key,
"audiosamplerate")
360 ||!strcmp(tag->
key,
"audiosamplesize")
361 ||!strcmp(tag->
key,
"stereo")
362 ||!strcmp(tag->
key,
"audiocodecid")
363 ||!strcmp(tag->
key,
"duration")
364 ||!strcmp(tag->
key,
"onMetaData")
365 ||!strcmp(tag->
key,
"datasize")
366 ||!strcmp(tag->
key,
"lasttimestamp")
367 ||!strcmp(tag->
key,
"totalframes")
368 ||!strcmp(tag->
key,
"hasAudio")
369 ||!strcmp(tag->
key,
"hasVideo")
370 ||!strcmp(tag->
key,
"hasCuePoints")
371 ||!strcmp(tag->
key,
"hasMetadata")
372 ||!strcmp(tag->
key,
"hasKeyframes")
373 ){
375 continue;
376 }
380 metadata_count++;
381 }
382
383 if (write_duration_filesize) {
387 }
388
392
395 metadata_count++;
396
399 metadata_count++;
400
403 metadata_count++;
404
407 metadata_count++;
408
411 metadata_count++;
412
417 metadata_count++;
418
423 metadata_count++;
424
429 metadata_count++;
430
435 metadata_count++;
436
441 metadata_count++;
442
447 metadata_count++;
448
451 metadata_count++;
452
454 }
455
458
459 /* write total size of tag */
461
462 avio_seek(pb, metadata_count_pos, SEEK_SET);
464
470 }
471
474 {
477 "%s codec %s not compatible with flv\n",
478 type,
479 desc ? desc->
name :
"unknown");
481 }
482
484 int64_t data_size;
487
490 int64_t pos;
501 avio_w8(pb, 0);
// AAC sequence header
502
505 int samplerate_index;
509
510 for (samplerate_index = 0; samplerate_index < 16;
511 samplerate_index++)
514 break;
515
518 put_bits(&pbc, 4, samplerate_index);
//sample rate index
520 put_bits(&pbc, 1, 0);
//frame length - 1024 samples
521 put_bits(&pbc, 1, 0);
//does not depend on core coder
522 put_bits(&pbc, 1, 0);
//is not extension
524
527
529 data[0], data[1]);
530 }
532 } else {
534 avio_w8(pb, 0);
// AVC sequence header
537 }
539 avio_seek(pb, -data_size - 10, SEEK_CUR);
542 avio_wb32(pb, data_size + 11);
// previous tag size
543 }
544 }
545
547 {
549
550 if (!position) {
553 }
554
557
562 } else {
566 }
567
569
570 return 0;
571 }
572
574 {
575 int ret = 0;
577 int64_t metadata_size = 0;
581 int read_buf_id = 0;
582 int read_size[2];
584
586 metadata_size += 2 + 13; /* filepositions String */
587 metadata_size += 2 + 5; /* times String */
588 metadata_size += 3; /* Object end */
589
591
592 if (metadata_size < 0)
593 return metadata_size;
594
596 if (!buf) {
598 }
600 read_buf[1] = buf + metadata_size;
601
604
608
609 /* Shift the data: the AVIO context of the output can only be used for
610 * writing, so we re-open the same output, but for reading. It also avoids
611 * a read/seek/write/seek back and forth. */
614 if (ret < 0) {
616 "the second pass (add_keyframe_index)\n", s->
url);
618 }
619
620 /* mark the end of the shift to up to the last data we wrote, and get ready
621 * for writing */
624
625 /* start reading at where the keyframe index information will be placed */
628
629 #define READ_BLOCK do { \
630 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], metadata_size); \
631 read_buf_id ^= 1; \
632 } while (0)
633
634 /* shift data by chunk of at most keyframe *filepositions* and *times* size */
636 do {
638 n = read_size[read_buf_id];
639 if (n < 0)
640 break;
643 } while (pos <= pos_end);
644
646
649 return ret;
650 }
651
652
654 {
655 int i;
658
667 }
670 "at most one video stream is supported in flv\n");
672 }
676
682
683 if (error) {
685 "use vstrict=-1 / -strict -1 to use it anyway.\n");
687 }
690 "Muxing VP6 in flv will produce flipped video on playback.\n");
691 }
692 break;
696 "at most one audio stream is supported in flv\n");
698 }
704 "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
705 break;
710 break;
713 av_log(s,
AV_LOG_ERROR,
"Subtitle codec '%s' for stream %d is not compatible with FLV\n",
716 }
718 break;
719 default:
723 }
725
727 if (!sc)
731 }
732
734
741
744 avio_w8(pb, 8);
// message type
750 }
751
754 } else {
756 }
757
760 }
761
763 return 0;
764 }
765
767 {
768 int64_t file_size;
772 int i, res;
774
775 if (build_keyframes_idx) {
777
780
783
786
789
793
795 if (res < 0) {
797 }
803 }
804
809 }
810
812 while (newflv_posinfo) {
813 p = newflv_posinfo->
next;
814 if (p) {
818 } else {
820 newflv_posinfo =
NULL;
821 }
822 }
823
826
828 }
829
833 } else {
834 /* Add EOS tag */
841 }
842 }
843
845
846 if (build_keyframes_idx) {
850 }
853 /* update information */
856 } else {
858 }
861 } else {
863 }
864 }
865 }
866
867 return 0;
868 }
869
871 {
876 unsigned ts;
879 int flags = -1, flags_size, ret;
881
884 flags_size = 2;
886 flags_size = 5;
887 else
888 flags_size = 1;
889
892 int side_size = 0;
900 }
904 }
905 }
906
909
912 "Packets are not in the proper order with respect to DTS\n");
914 }
915
917
921 }
922
925
929
931
933 break;
936
938
940 break;
944 break;
945 default:
947 }
948
950 /* check if extradata looks like mp4 formatted */
953 return ret;
958 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
959 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
961 }
963 }
964
965 /* check Speex packet duration */
968 "8 frames per packet. Adobe Flash "
969 "Player cannot handle this!\n");
970
973
974 if (size + flags_size >= 1<<24) {
976 size + flags_size, 1<<24);
978 }
979
982 avio_w8(pb, (ts >> 24) & 0x7F);
// timestamps are 32 bits _signed_
984
987 int data_size;
988 int64_t metadata_size_pos =
avio_tell(pb);
990 // legacy FFmpeg magic?
1003 } else {
1004 // just pass the metadata through
1006 }
1007 /* write total size of tag */
1008 data_size =
avio_tell(pb) - metadata_size_pos;
1009 avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
1011 avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
1013 } else {
1021 else
1029 }
1030
1032
1033 avio_wb32(pb, size + flags_size + 11);
// previous tag size
1036 }
1037
1045 int64_t pos = cur_offset;
1046
1050 }
1052 break;
1053
1056 break;
1057
1058 default:
1060 break;
1061 }
1062 }
1063
1065
1067 }
1068
1070 {
"flvflags",
"FLV muxer flags", offsetof(
FLVContext,
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"flvflags" },
1077 };
1078
1084 };
1085
1089 .mime_type = "video/x-flv",
1090 .extensions = "flv",
1099 },
1103 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
void avio_wb64(AVIOContext *s, uint64_t val)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
ptrdiff_t const GLvoid * data
int64_t delay
first dts delay (needed for AVC & Speex)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
const char * av_default_item_name(void *ptr)
Return the context name.
#define AVIO_FLAG_READ
read-only
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
int event_flags
Flags for the user to detect events happening on the file.
int64_t metadata_totalsize
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
double lastkeyframetimestamp
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_WB24 unsigned int_TMPL AV_RB16
int strict_std_compliance
Allow non-standard and experimental extension.
This struct describes the properties of an encoded stream.
int64_t keyframe_index_size
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
FLVFileposition * head_filepositions
Opaque data information usually continuous.
AVCodecParameters * video_par
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
double keyframe_timestamp
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
AVStream ** streams
A list of all streams in the file.
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
AVCodecParameters * audio_par
int64_t keyframes_info_offset
int64_t metadata_totalsize_pos
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
int64_t lastkeyframelocation_offset
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t filepositions_count
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * url
input or output URL.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
enum AVMediaType codec_type
General type of the encoded data.
static const int mpeg4audio_sample_rates[16]
simple assert() macros that are a bit more flexible than ISO C assert().
AVRational avg_frame_rate
Average framerate.
FLVFileposition * filepositions
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
int flags
A combination of AV_PKT_FLAG values.
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
int extradata_size
Size of the extradata content in bytes.
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.
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_TIME_BASE
Internal time base represented as integer.
int64_t lasttimestamp_offset
static int write_trailer(AVFormatContext *s1)
void avio_wb24(AVIOContext *s, unsigned int val)
int64_t lastkeyframelocation
static void error(const char *err)
struct FLVFileposition * next
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
AVIOContext * pb
I/O context.
void avio_w8(AVIOContext *s, int b)
int64_t keyframe_position
Describe the class of an AVClass context structure.
AVCodecParameters * data_par
int64_t lastkeyframetimestamp_offset
const char * name
Name of the codec described by this descriptor.
int error
contains the error code or 0 if no error happened
This struct describes the properties of a single codec described by an AVCodecID. ...
void avio_wb16(AVIOContext *s, unsigned int val)
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.
int sample_rate
Audio only.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int profile
Codec-specific bitstream restrictions that the stream conforms to.
int64_t nb_frames
number of frames in this stream if known or 0
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
A point in the output bytestream where a decoder can start decoding (i.e.
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
int bits_per_coded_sample
The number of bits per sample in the codedwords.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
void avio_wb32(AVIOContext *s, unsigned int val)
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define av_malloc_array(a, b)
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
int64_t metadata_size_pos
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t last_ts
last timestamp for each stream
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
#define AV_NOPTS_VALUE
Undefined timestamp value.