FFmpeg: libavformat/flvenc.c Source File
Go to the documentation of this file. 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
43
44
59 };
60
73 };
74
82
88
96
104
109
116
118
122
127
132
134 {
137
144 "FLV only supports wideband (16kHz) Speex audio\n");
146 }
150 }
152 } else {
154 case 48000:
155 // 48khz mp3 is stored with 44k1 samplerate identifer
158 break;
159 } else {
161 }
162 case 44100:
164 break;
165 case 22050:
167 break;
168 case 11025:
170 break;
171 case 16000: // nellymoser only
172 case 8000: // nellymoser only
173 case 5512: // not MP3
176 break;
177 }
178 default:
181 "FLV does not support sample rate %d, "
182 "choose from (44100, 22050, 11025)\n", par->
sample_rate);
184 }
185 }
186
189
193 break;
196 break;
199 break;
202 break;
205 break;
211 else
213 break;
216 break;
219 break;
220 case 0:
222 break;
223 default:
227 }
228
230 }
231
233 {
234 size_t len = strlen(str);
236 // Avoid avio_write() if put_amf_string(pb, "") is inlined.
238 return;
240 }
241
242 // FLV timestamps are 32 bits signed, RTMP timestamps should be 32-bit unsigned
245 avio_w8(pb, (ts >> 24) & 0x7F);
246 }
247
249 {
251 /* ub[4] FrameType = 1, ub[4] CodecId */
258 avio_w8(pb, 2);
/* AVC end of sequence */
259 avio_wb24(pb, 0);
/* Always 0 for AVC EOS. */
261 }
262
264 {
267 }
268
270 {
272 }
273
275 {
278 }
279
281 {
284 }
285
287 {
291 int metadata_count = 0;
294
295 /* write meta_tag */
298 avio_wb24(pb, 0);
// size of data part (sum of all parts below)
301
302 /* now data of data_size size */
303
304 /* first event name as a string */
307
308 /* mixed array (hash) with size and string/type/data tuples */
314 if (write_duration_filesize) {
315 metadata_count += 2; // +2 for duration and file size
316 }
318
319 if (write_duration_filesize) {
322 // fill in the guessed duration, it'll be corrected later if incorrect
324 }
325
329
332
335
339 metadata_count++;
340 }
341
344 }
345
349
352
355
358
361 }
362
366 }
367
370 if( !strcmp(
tag->key,
"width")
371 ||!strcmp(
tag->key,
"height")
372 ||!strcmp(
tag->key,
"videodatarate")
373 ||!strcmp(
tag->key,
"framerate")
374 ||!strcmp(
tag->key,
"videocodecid")
375 ||!strcmp(
tag->key,
"audiodatarate")
376 ||!strcmp(
tag->key,
"audiosamplerate")
377 ||!strcmp(
tag->key,
"audiosamplesize")
378 ||!strcmp(
tag->key,
"stereo")
379 ||!strcmp(
tag->key,
"audiocodecid")
380 ||!strcmp(
tag->key,
"duration")
381 ||!strcmp(
tag->key,
"onMetaData")
382 ||!strcmp(
tag->key,
"datasize")
383 ||!strcmp(
tag->key,
"lasttimestamp")
384 ||!strcmp(
tag->key,
"totalframes")
385 ||!strcmp(
tag->key,
"hasAudio")
386 ||!strcmp(
tag->key,
"hasVideo")
387 ||!strcmp(
tag->key,
"hasCuePoints")
388 ||!strcmp(
tag->key,
"hasMetadata")
389 ||!strcmp(
tag->key,
"hasKeyframes")
390 ){
392 continue;
393 }
397 metadata_count++;
398 }
399
400 if (write_duration_filesize) {
404 }
405
408
411 metadata_count++;
412
415 metadata_count++;
416
419 metadata_count++;
420
423 metadata_count++;
424
427 metadata_count++;
428
433 metadata_count++;
434
439 metadata_count++;
440
445 metadata_count++;
446
451 metadata_count++;
452
457 metadata_count++;
458
463 metadata_count++;
464
467 metadata_count++;
468
470 }
471
474
475 /* write total size of tag */
477
478 avio_seek(pb, metadata_count_pos, SEEK_SET);
480
486 }
487
489 {
497
501 int flags_size = 5;
504 if (side_data)
506
509 if (side_data)
511
512 /*
513 * Reference Enhancing FLV
514 * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp.pdf
515 * */
521
528 }
529
532
534
536
538
543 }
544
549 }
550
555 }
556
559
560 if (lightMetadata) {
563
566
569
572 }
573
580
583
586
589
592
595
598
601 }
605
608 }
611 }
614
615 total_size =
avio_tell(pb) - metadata_size_pos - 10;
616 avio_seek(pb, metadata_size_pos, SEEK_SET);
619 avio_wb32(pb, total_size + 11);
// previous tag size
621 }
622 }
623
626 {
629 "%s codec %s not compatible with flv\n",
633 }
634
639
653 avio_w8(pb, 0);
// AAC sequence header
654
657 int samplerate_index;
661
662 for (samplerate_index = 0; samplerate_index < 16;
663 samplerate_index++)
666 break;
667
670 put_bits(&pbc, 4, samplerate_index);
//sample rate index
672 put_bits(&pbc, 1, 0);
//frame length - 1024 samples
673 put_bits(&pbc, 1, 0);
//does not depend on core coder
674 put_bits(&pbc, 1, 0);
//is not extension
676
679
682 }
684 } else {
691 } else {
693 avio_w8(pb, 0);
// AVC sequence header
695 }
696
703 else
705 }
707 avio_seek(pb, -data_size - 10, SEEK_CUR);
710 avio_wb32(pb, data_size + 11);
// previous tag size
711 }
712 }
713
715 {
717
718 if (!position) {
721 }
722
725
730 } else {
734 }
735
737
738 return 0;
739 }
740
742 {
746
748 metadata_size += 2 + 13; /* filepositions String */
749 metadata_size += 2 + 5; /* times String */
750 metadata_size += 3; /* Object end */
751
753
754 if (metadata_size < 0)
755 return metadata_size;
756
760
763
766
767 return 0;
768 }
769
771 {
774
779 }
780
781 for (
i = 0;
i <
s->nb_streams;
i++) {
783
786 if (
s->streams[
i]->avg_frame_rate.den &&
787 s->streams[
i]->avg_frame_rate.num) {
789 }
792 "at most one video stream is supported in flv\n");
794 }
798
804
807 "use vstrict=-1 / -strict -1 to use it anyway.\n");
809 }
812 "Muxing VP6 in flv will produce flipped video on playback.\n");
813 }
814 break;
818 "at most one audio stream is supported in flv\n");
820 }
826 "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
827 break;
832 break;
838 }
840 break;
841 default:
845 }
848 }
849
851
852 return 0;
853 }
854
856 {
860
867
868 for (
i = 0;
i <
s->nb_streams;
i++)
869 if (
s->streams[
i]->codecpar->codec_tag == 5) {
870 avio_w8(pb, 8);
// message type
876 }
877
880 } else {
882 }
883
884 for (
i = 0;
i <
s->nb_streams;
i++) {
886 }
887
889 return 0;
890 }
891
893 {
900
901 if (build_keyframes_idx) {
903
906
909
912
915
919
921 if (res < 0) {
922 goto end;
923 }
929 }
930
935 }
936
939
941 }
942
943 end:
946 } else {
947 /* Add EOS tag */
948 for (
i = 0;
i <
s->nb_streams;
i++) {
953 }
954 }
955
957
958 if (build_keyframes_idx) {
962 }
965 /* update information */
968 } else {
970 }
973 } else {
975 }
976 }
977 }
978
979 return 0;
980 }
981
983 {
987 unsigned ts;
991 int flags = -1, flags_size,
ret = 0;
993
997 }
998
1001 flags_size = 2;
1005 flags_size = 5;
1006 else
1007 flags_size = 1;
1008
1010 flags_size += 3;
1011
1015 size_t side_size;
1021 memcpy(par->
extradata, side, side_size);
1023 }
1025 }
1026
1029
1032 "Packets are not in the proper order with respect to DTS\n");
1034 }
1041 }
1042 }
1043
1045
1049 }
1050
1053
1057
1059
1061 break;
1064
1066
1068 break;
1072 break;
1073 default:
1075 }
1076
1078 /* check if extradata looks like mp4 formatted */
1090 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
1091 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
1093 }
1095 }
1096
1097 /* check Speex packet duration */
1100 "8 frames per packet. Adobe Flash "
1101 "Player cannot handle this!\n");
1102
1105
1106 if (
size + flags_size >= 1<<24) {
1108 size + flags_size, 1<<24);
1111 }
1112
1116
1119 int data_size;
1122 // legacy FFmpeg magic?
1135 } else {
1136 // just pass the metadata through
1138 }
1139 /* write total size of tag */
1140 data_size =
avio_tell(pb) - metadata_size_pos;
1141 avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
1143 avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
1145 } else {
1156 } else {
1158 }
1164 else
1172 }
1173
1175
1179 }
1180
1192 }
1193 break;
1194
1197 break;
1198
1199 default:
1201 break;
1202 }
1203 }
1206
1208 }
1209
1212 {
1216 }
1223 return 1;
1224 }
1225
1227 {
1230
1231 while (filepos) {
1234 filepos = next;
1235 }
1238 }
1239
1241 {
"flvflags",
"FLV muxer flags", offsetof(
FLVContext,
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM, .unit =
"flvflags" },
1248 };
1249
1255 };
1256
1260 .p.mime_type = "video/x-flv",
1261 .p.extensions = "flv",
1271 .p.codec_tag = (
const AVCodecTag*
const []) {
1273 },
1277 };
static void error(const char *err)
@ FLV_HEADER_FLAG_HASAUDIO
int64_t metadata_totalsize
AVCodecParameters * audio_par
#define AMF_END_OF_OBJECT
#define AV_LOG_WARNING
Something somehow does not look correct.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
@ AVIO_DATA_MARKER_BOUNDARY_POINT
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVMediaType codec_type
General type of the encoded data.
FLVFileposition * filepositions
This struct describes the properties of an encoded stream.
enum AVColorSpace color_space
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
int64_t delay
first dts delay (needed for AVC & Speex)
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
unsigned MaxCLL
Max content light level (cd/m^2).
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
@ AVCOL_TRC_NB
Not part of ABI.
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
int64_t metadata_totalsize_pos
@ AVCOL_SPC_NB
Not part of ABI.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
int nb_channels
Number of channels in this layout.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t lasttimestamp_offset
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
enum AVColorPrimaries color_primaries
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
int64_t lastkeyframelocation_offset
double lastkeyframetimestamp
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
@ FLV_CODECID_NELLYMOSER_8KHZ_MONO
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
#define FLV_AUDIO_CODECID_OFFSET
@ FLV_HEADER_FLAG_HASVIDEO
enum AVColorTransferCharacteristic color_trc
@ FLV_FRAME_VIDEO_INFO_CMD
video info/command frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int64_t keyframe_position
int64_t lastkeyframelocation
AVCodecParameters * video_par
int64_t filepositions_count
This struct describes the properties of a single codec described by an AVCodecID.
@ AVCOL_PRI_NB
Not part of ABI.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int nb_coded_side_data
Amount of entries in coded_side_data.
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
FLVFileposition * head_filepositions
int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
Make shift_size amount of space at read_start by shifting data in the output at read_start until the ...
int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb, const uint8_t *data, int len, AVCodecParameters *par)
Writes VP codec configuration to the provided AVIOContext.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
const char * av_default_item_name(void *ptr)
Return the context name.
@ FLV_FRAME_KEY
key frame (for AVC, a seekable frame)
void avio_w8(AVIOContext *s, int b)
AVChannelLayout ch_layout
Audio only.
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
int sample_rate
Audio only.
double keyframe_timestamp
AVCodecID
Identify the syntax and semantics of the bitstream.
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
int64_t keyframes_info_offset
int extradata_size
Size of the extradata content in bytes.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define MKBETAG(a, b, c, d)
int profile
Codec-specific bitstream restrictions that the stream conforms to.
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size, int write_seq_header)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_wb32(AVIOContext *s, unsigned int val)
AVCodecParameters * data_par
int flags
A combination of AV_PKT_FLAG values.
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
int64_t keyframe_index_size
#define av_builtin_constant_p
#define AV_TIME_BASE
Internal time base represented as integer.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int64_t metadata_size_pos
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
@ AMF_DATA_TYPE_MIXEDARRAY
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
@ AV_CODEC_ID_TEXT
raw UTF-8 text
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.
@ FLV_CODECID_NELLYMOSER_16KHZ_MONO
int64_t lastkeyframetimestamp_offset
@ FLV_SAMPLERATE_SPECIAL
signifies 5512Hz and 8000Hz in the case of NELLYMOSER
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
void avio_wb64(AVIOContext *s, uint64_t val)
const int ff_mpeg4audio_sample_rates[16]
int bits_per_coded_sample
The number of bits per sample in the codedwords.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
void avio_wb24(AVIOContext *s, unsigned int val)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
This structure stores compressed data.
unsigned MaxFALL
Max average light level per frame (cd/m^2).
@ FLV_FRAME_INTER
inter frame (for AVC, a non-seekable frame)
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
void avio_wb16(AVIOContext *s, unsigned int val)
#define flags(name, subs,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
struct FLVFileposition * next
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ PacketTypeSequenceStart
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
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
Generated on Fri Aug 22 2025 13:58:20 for FFmpeg by
doxygen
1.8.17