FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvenc.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 750 968 77.5%
Functions: 24 25 96.0%
Branches: 452 669 67.6%

Line Branch Exec Source
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
22 #include "libavutil/attributes.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intfloat.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/mastering_display_metadata.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/mem.h"
30 #include "libavcodec/codec_desc.h"
31 #include "libavcodec/mpeg4audio.h"
32 #include "avio.h"
33 #include "avc.h"
34 #include "av1.h"
35 #include "vpcc.h"
36 #include "hevc.h"
37 #include "vvc.h"
38 #include "avformat.h"
39 #include "flv.h"
40 #include "internal.h"
41 #include "nal.h"
42 #include "mux.h"
43 #include "libavutil/opt.h"
44 #include "libavcodec/put_bits.h"
45
46
47 static const AVCodecTag flv_video_codec_ids[] = {
48 { AV_CODEC_ID_FLV1, FLV_CODECID_H263 },
49 { AV_CODEC_ID_H263, FLV_CODECID_REALH263 },
50 { AV_CODEC_ID_MPEG4, FLV_CODECID_MPEG4 },
51 { AV_CODEC_ID_FLASHSV, FLV_CODECID_SCREEN },
52 { AV_CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2 },
53 { AV_CODEC_ID_VP6F, FLV_CODECID_VP6 },
54 { AV_CODEC_ID_VP6, FLV_CODECID_VP6 },
55 { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
56 { AV_CODEC_ID_H264, FLV_CODECID_H264 },
57 { AV_CODEC_ID_HEVC, MKBETAG('h', 'v', 'c', '1') },
58 { AV_CODEC_ID_VVC, MKBETAG('v', 'v', 'c', '1') },
59 { AV_CODEC_ID_AV1, MKBETAG('a', 'v', '0', '1') },
60 { AV_CODEC_ID_VP9, MKBETAG('v', 'p', '0', '9') },
61 { AV_CODEC_ID_NONE, 0 }
62 };
63
64 static const AVCodecTag flv_audio_codec_ids[] = {
65 { AV_CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET },
66 { AV_CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
67 { AV_CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
68 { AV_CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET },
69 { AV_CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET },
70 { AV_CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET },
71 { AV_CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET },
72 { AV_CODEC_ID_PCM_MULAW, FLV_CODECID_PCM_MULAW >> FLV_AUDIO_CODECID_OFFSET },
73 { AV_CODEC_ID_PCM_ALAW, FLV_CODECID_PCM_ALAW >> FLV_AUDIO_CODECID_OFFSET },
74 { AV_CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET },
75 { AV_CODEC_ID_OPUS, MKBETAG('O', 'p', 'u', 's') },
76 { AV_CODEC_ID_FLAC, MKBETAG('f', 'L', 'a', 'C') },
77 { AV_CODEC_ID_AC3, MKBETAG('a', 'c', '-', '3') },
78 { AV_CODEC_ID_EAC3, MKBETAG('e', 'c', '-', '3') },
79 { AV_CODEC_ID_NONE, 0 }
80 };
81
82 typedef enum {
83 FLV_AAC_SEQ_HEADER_DETECT = (1 << 0),
84 FLV_NO_SEQUENCE_END = (1 << 1),
85 FLV_ADD_KEYFRAME_INDEX = (1 << 2),
86 FLV_NO_METADATA = (1 << 3),
87 FLV_NO_DURATION_FILESIZE = (1 << 4),
88 } FLVFlags;
89
90 typedef struct FLVFileposition {
91 int64_t keyframe_position;
92 double keyframe_timestamp;
93 struct FLVFileposition *next;
94 } FLVFileposition;
95
96 typedef struct FLVContext {
97 AVClass *av_class;
98 int reserved;
99 int64_t duration_offset;
100 int64_t filesize_offset;
101 int64_t duration;
102 int64_t delay; ///< first dts delay (needed for AVC & Speex)
103
104 int64_t datastart_offset;
105 int64_t datasize_offset;
106 int64_t datasize;
107 int64_t videosize_offset;
108 int64_t videosize;
109 int64_t audiosize_offset;
110 int64_t audiosize;
111
112 int64_t metadata_size_pos;
113 int64_t metadata_totalsize_pos;
114 int64_t metadata_totalsize;
115 int64_t keyframe_index_size;
116
117 int64_t lasttimestamp_offset;
118 double lasttimestamp;
119 int64_t lastkeyframetimestamp_offset;
120 double lastkeyframetimestamp;
121 int64_t lastkeyframelocation_offset;
122 int64_t lastkeyframelocation;
123
124 int64_t keyframes_info_offset;
125
126 int64_t filepositions_count;
127 FLVFileposition *filepositions;
128 FLVFileposition *head_filepositions;
129
130 AVCodecParameters *audio_par;
131 AVCodecParameters *video_par;
132 double framerate;
133 AVCodecParameters *data_par;
134
135 int flags;
136 int64_t *last_ts;
137 int *metadata_pkt_written;
138 int *track_idx_map;
139 } FLVContext;
140
141 944 static int get_audio_flags(AVFormatContext *s, AVCodecParameters *par)
142 {
143 1888 int flags = (par->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT
144
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 724 times.
944 : FLV_SAMPLESSIZE_8BIT;
145
146
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 651 times.
944 if (par->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
147 293 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ |
148 FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
149
4/4
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 557 times.
✓ Branch 3 taken 17 times.
651 if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC
150
3/4
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 509 times.
557 || par->codec_id == AV_CODEC_ID_AC3 || par->codec_id == AV_CODEC_ID_EAC3)
151 142 return FLV_CODECID_EX_HEADER; // only needed for codec support check
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 509 times.
509 else if (par->codec_id == AV_CODEC_ID_SPEEX) {
153 if (par->sample_rate != 16000) {
154 av_log(s, AV_LOG_ERROR,
155 "FLV only supports wideband (16kHz) Speex audio\n");
156 return AVERROR(EINVAL);
157 }
158 if (par->ch_layout.nb_channels != 1) {
159 av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
160 return AVERROR(EINVAL);
161 }
162 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
163 } else {
164
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 377 times.
✗ Branch 5 not taken.
509 switch (par->sample_rate) {
165 case 48000:
166 // 48khz mp3 is stored with 44k1 samplerate identifier
167 if (par->codec_id == AV_CODEC_ID_MP3) {
168 flags |= FLV_SAMPLERATE_44100HZ;
169 break;
170 } else {
171 goto error;
172 }
173 132 case 44100:
174 132 flags |= FLV_SAMPLERATE_44100HZ;
175 132 break;
176 case 22050:
177 flags |= FLV_SAMPLERATE_22050HZ;
178 break;
179 case 11025:
180 flags |= FLV_SAMPLERATE_11025HZ;
181 break;
182 377 case 16000: // nellymoser only
183 case 8000: // nellymoser only
184 case 5512: // not MP3
185
1/2
✓ Branch 0 taken 377 times.
✗ Branch 1 not taken.
377 if (par->codec_id != AV_CODEC_ID_MP3) {
186 377 flags |= FLV_SAMPLERATE_SPECIAL;
187 377 break;
188 }
189 av_fallthrough;
190 default:
191 error:
192 av_log(s, AV_LOG_ERROR,
193 "FLV does not support sample rate %d, "
194 "choose from (44100, 22050, 11025)\n", par->sample_rate);
195 return AVERROR(EINVAL);
196 }
197 }
198
199
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 377 times.
509 if (par->ch_layout.nb_channels > 1)
200 132 flags |= FLV_STEREO;
201
202
2/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 132 times.
✓ Branch 5 taken 377 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
509 switch (par->codec_id) {
203 case AV_CODEC_ID_MP3:
204 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
205 break;
206 case AV_CODEC_ID_PCM_U8:
207 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
208 break;
209 case AV_CODEC_ID_PCM_S16BE:
210 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
211 break;
212 case AV_CODEC_ID_PCM_S16LE:
213 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
214 break;
215 132 case AV_CODEC_ID_ADPCM_SWF:
216 132 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
217 132 break;
218 377 case AV_CODEC_ID_NELLYMOSER:
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 377 times.
377 if (par->sample_rate == 8000)
220 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
221
1/2
✓ Branch 0 taken 377 times.
✗ Branch 1 not taken.
377 else if (par->sample_rate == 16000)
222 377 flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
223 else
224 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
225 377 break;
226 case AV_CODEC_ID_PCM_MULAW:
227 flags |= FLV_CODECID_PCM_MULAW | FLV_SAMPLESSIZE_16BIT;
228 break;
229 case AV_CODEC_ID_PCM_ALAW:
230 flags |= FLV_CODECID_PCM_ALAW | FLV_SAMPLESSIZE_16BIT;
231 break;
232 case 0:
233 flags |= par->codec_tag << 4;
234 break;
235 default:
236 av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
237 avcodec_get_name(par->codec_id));
238 return AVERROR(EINVAL);
239 }
240
241 509 return flags;
242 }
243
244 331 static void put_amf_string(AVIOContext *pb, const char *str)
245 {
246 331 size_t len = strlen(str);
247 331 avio_wb16(pb, len);
248 // Avoid avio_write() if put_amf_string(pb, "") is inlined.
249 if (av_builtin_constant_p(len == 0) && len == 0)
250 return;
251 331 avio_write(pb, str, len);
252 }
253
254 // FLV timestamps are 32 bits signed, RTMP timestamps should be 32-bit unsigned
255 2141 static void put_timestamp(AVIOContext *pb, int64_t ts) {
256 2141 avio_wb24(pb, ts & 0xFFFFFF);
257 2141 avio_w8(pb, (ts >> 24) & 0x7F);
258 2141 }
259
260 2 static void put_eos_tag(AVIOContext *pb, unsigned ts, enum AVCodecID codec_id)
261 {
262 2 uint32_t tag = ff_codec_get_tag(flv_video_codec_ids, codec_id);
263 /* ub[4] FrameType = 1, ub[4] CodecId */
264 2 tag |= 1 << 4;
265 2 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
266 2 avio_wb24(pb, 5); /* Tag Data Size */
267 2 put_timestamp(pb, ts);
268 2 avio_wb24(pb, 0); /* StreamId = 0 */
269 2 avio_w8(pb, tag);
270 2 avio_w8(pb, 2); /* AVC end of sequence */
271 2 avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
272 2 avio_wb32(pb, 16); /* Size of FLV tag */
273 2 }
274
275 295 static void put_amf_double(AVIOContext *pb, double d)
276 {
277 295 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
278 295 avio_wb64(pb, av_double2int(d));
279 295 }
280
281 1 static void put_amf_byte(AVIOContext *pb, unsigned char abyte)
282 {
283 1 avio_w8(pb, abyte);
284 1 }
285
286 2 static void put_amf_dword_array(AVIOContext *pb, uint32_t dw)
287 {
288 2 avio_w8(pb, AMF_DATA_TYPE_ARRAY);
289 2 avio_wb32(pb, dw);
290 2 }
291
292 10 static void put_amf_bool(AVIOContext *pb, int b)
293 {
294 10 avio_w8(pb, AMF_DATA_TYPE_BOOL);
295 10 avio_w8(pb, !!b);
296 10 }
297
298 24 static void write_metadata(AVFormatContext *s, unsigned int ts)
299 {
300 24 AVIOContext *pb = s->pb;
301 24 FLVContext *flv = s->priv_data;
302 24 int write_duration_filesize = !(flv->flags & FLV_NO_DURATION_FILESIZE);
303 24 int metadata_count = 0;
304 int64_t metadata_count_pos;
305 24 const AVDictionaryEntry *tag = NULL;
306
307 /* write meta_tag */
308 24 avio_w8(pb, FLV_TAG_TYPE_META); // tag type META
309 24 flv->metadata_size_pos = avio_tell(pb);
310 24 avio_wb24(pb, 0); // size of data part (sum of all parts below)
311 24 put_timestamp(pb, ts); // timestamp
312 24 avio_wb24(pb, 0); // reserved
313
314 /* now data of data_size size */
315
316 /* first event name as a string */
317 24 avio_w8(pb, AMF_DATA_TYPE_STRING);
318 24 put_amf_string(pb, "onMetaData"); // 12 bytes
319
320 /* mixed array (hash) with size and string/type/data tuples */
321 24 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
322 24 metadata_count_pos = avio_tell(pb);
323
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 metadata_count = 4 * !!flv->video_par +
324
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 5 * !!flv->audio_par +
325 24 1 * !!flv->data_par;
326
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (write_duration_filesize) {
327 24 metadata_count += 2; // +2 for duration and file size
328 }
329 24 avio_wb32(pb, metadata_count);
330
331
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (write_duration_filesize) {
332 24 put_amf_string(pb, "duration");
333 24 flv->duration_offset = avio_tell(pb);
334 // fill in the guessed duration, it'll be corrected later if incorrect
335 24 put_amf_double(pb, s->duration / AV_TIME_BASE);
336 }
337
338
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 if (flv->video_par) {
339 21 put_amf_string(pb, "width");
340 21 put_amf_double(pb, flv->video_par->width);
341
342 21 put_amf_string(pb, "height");
343 21 put_amf_double(pb, flv->video_par->height);
344
345 21 put_amf_string(pb, "videodatarate");
346 21 put_amf_double(pb, flv->video_par->bit_rate / 1024.0);
347
348
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 if (flv->framerate != 0.0) {
349 19 put_amf_string(pb, "framerate");
350 19 put_amf_double(pb, flv->framerate);
351 19 metadata_count++;
352 }
353
354 21 put_amf_string(pb, "videocodecid");
355 21 put_amf_double(pb, flv->video_par->codec_tag);
356 }
357
358
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 if (flv->audio_par) {
359 5 put_amf_string(pb, "audiodatarate");
360 5 put_amf_double(pb, flv->audio_par->bit_rate / 1024.0);
361
362 5 put_amf_string(pb, "audiosamplerate");
363 5 put_amf_double(pb, flv->audio_par->sample_rate);
364
365 5 put_amf_string(pb, "audiosamplesize");
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 put_amf_double(pb, flv->audio_par->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
367
368 5 put_amf_string(pb, "stereo");
369 5 put_amf_bool(pb, flv->audio_par->ch_layout.nb_channels == 2);
370
371 5 put_amf_string(pb, "audiocodecid");
372 5 put_amf_double(pb, flv->audio_par->codec_tag);
373 }
374
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (flv->data_par) {
376 put_amf_string(pb, "datastream");
377 put_amf_double(pb, 0.0);
378 }
379
380 24 ff_standardize_creation_time(s);
381
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 24 times.
38 while ((tag = av_dict_iterate(s->metadata, tag))) {
382
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if( !strcmp(tag->key, "width")
383
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "height")
384
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "videodatarate")
385
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "framerate")
386
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "videocodecid")
387
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "audiodatarate")
388
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "audiosamplerate")
389
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "audiosamplesize")
390
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "stereo")
391
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "audiocodecid")
392
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "duration")
393
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "onMetaData")
394
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "datasize")
395
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "lasttimestamp")
396
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "totalframes")
397
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "hasAudio")
398
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "hasVideo")
399
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "hasCuePoints")
400
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ||!strcmp(tag->key, "hasMetadata")
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 ||!strcmp(tag->key, "hasKeyframes")
402 ){
403 av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
404 continue;
405 }
406 14 put_amf_string(pb, tag->key);
407 14 avio_w8(pb, AMF_DATA_TYPE_STRING);
408 14 put_amf_string(pb, tag->value);
409 14 metadata_count++;
410 }
411
412
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (write_duration_filesize) {
413 24 put_amf_string(pb, "filesize");
414 24 flv->filesize_offset = avio_tell(pb);
415 24 put_amf_double(pb, 0); // delayed write
416 }
417
418
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 if (flv->flags & FLV_ADD_KEYFRAME_INDEX) {
419 1 flv->keyframe_index_size = 0;
420
421 1 put_amf_string(pb, "hasVideo");
422 1 put_amf_bool(pb, !!flv->video_par);
423 1 metadata_count++;
424
425 1 put_amf_string(pb, "hasKeyframes");
426 1 put_amf_bool(pb, 1);
427 1 metadata_count++;
428
429 1 put_amf_string(pb, "hasAudio");
430 1 put_amf_bool(pb, !!flv->audio_par);
431 1 metadata_count++;
432
433 1 put_amf_string(pb, "hasMetadata");
434 1 put_amf_bool(pb, 1);
435 1 metadata_count++;
436
437 1 put_amf_string(pb, "canSeekToEnd");
438 1 put_amf_bool(pb, 1);
439 1 metadata_count++;
440
441 1 put_amf_string(pb, "datasize");
442 1 flv->datasize_offset = avio_tell(pb);
443 1 flv->datasize = 0;
444 1 put_amf_double(pb, flv->datasize);
445 1 metadata_count++;
446
447 1 put_amf_string(pb, "videosize");
448 1 flv->videosize_offset = avio_tell(pb);
449 1 flv->videosize = 0;
450 1 put_amf_double(pb, flv->videosize);
451 1 metadata_count++;
452
453 1 put_amf_string(pb, "audiosize");
454 1 flv->audiosize_offset = avio_tell(pb);
455 1 flv->audiosize = 0;
456 1 put_amf_double(pb, flv->audiosize);
457 1 metadata_count++;
458
459 1 put_amf_string(pb, "lasttimestamp");
460 1 flv->lasttimestamp_offset = avio_tell(pb);
461 1 flv->lasttimestamp = 0;
462 1 put_amf_double(pb, 0);
463 1 metadata_count++;
464
465 1 put_amf_string(pb, "lastkeyframetimestamp");
466 1 flv->lastkeyframetimestamp_offset = avio_tell(pb);
467 1 flv->lastkeyframetimestamp = 0;
468 1 put_amf_double(pb, 0);
469 1 metadata_count++;
470
471 1 put_amf_string(pb, "lastkeyframelocation");
472 1 flv->lastkeyframelocation_offset = avio_tell(pb);
473 1 flv->lastkeyframelocation = 0;
474 1 put_amf_double(pb, 0);
475 1 metadata_count++;
476
477 1 put_amf_string(pb, "keyframes");
478 1 put_amf_byte(pb, AMF_DATA_TYPE_OBJECT);
479 1 metadata_count++;
480
481 1 flv->keyframes_info_offset = avio_tell(pb);
482 }
483
484 24 put_amf_string(pb, "");
485 24 avio_w8(pb, AMF_END_OF_OBJECT);
486
487 /* write total size of tag */
488 24 flv->metadata_totalsize = avio_tell(pb) - flv->metadata_size_pos - 10;
489
490 24 avio_seek(pb, metadata_count_pos, SEEK_SET);
491 24 avio_wb32(pb, metadata_count);
492
493 24 avio_seek(pb, flv->metadata_size_pos, SEEK_SET);
494 24 avio_wb24(pb, flv->metadata_totalsize);
495 24 avio_skip(pb, flv->metadata_totalsize + 10 - 3);
496 24 flv->metadata_totalsize_pos = avio_tell(pb);
497 24 avio_wb32(pb, flv->metadata_totalsize + 11);
498 24 }
499
500 587 static void write_codec_fourcc(AVIOContext *pb, enum AVCodecID codec_id)
501 {
502
9/12
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 39 times.
✓ Branch 7 taken 155 times.
✓ Branch 8 taken 19 times.
✓ Branch 9 taken 104 times.
✓ Branch 10 taken 50 times.
✗ Branch 11 not taken.
587 switch (codec_id) {
503 75 case AV_CODEC_ID_AAC:
504 75 avio_write(pb, "mp4a", 4);
505 75 return;
506 78 case AV_CODEC_ID_OPUS:
507 78 avio_write(pb, "Opus", 4);
508 78 return;
509 18 case AV_CODEC_ID_FLAC:
510 18 avio_write(pb, "fLaC", 4);
511 18 return;
512 case AV_CODEC_ID_MP3:
513 avio_write(pb, ".mp3", 4);
514 return;
515 49 case AV_CODEC_ID_AC3:
516 49 avio_write(pb, "ac-3", 4);
517 49 return;
518 case AV_CODEC_ID_EAC3:
519 avio_write(pb, "ec-3", 4);
520 return;
521 39 case AV_CODEC_ID_H264:
522 39 avio_write(pb, "avc1", 4);
523 39 return;
524 155 case AV_CODEC_ID_HEVC:
525 155 avio_write(pb, "hvc1", 4);
526 155 return;
527 19 case AV_CODEC_ID_VVC:
528 19 avio_write(pb, "vvc1", 4);
529 19 return;
530 104 case AV_CODEC_ID_AV1:
531 104 avio_write(pb, "av01", 4);
532 104 return;
533 50 case AV_CODEC_ID_VP9:
534 50 avio_write(pb, "vp09", 4);
535 50 return;
536 default:
537 av_log(NULL, AV_LOG_ERROR, "Invalid codec FourCC write requested.\n");
538 av_assert0(0);
539 }
540 }
541
542 766 static void flv_write_metadata_packet(AVFormatContext *s, AVCodecParameters *par, unsigned int ts, int stream_idx)
543 {
544 766 AVIOContext *pb = s->pb;
545 766 FLVContext *flv = s->priv_data;
546 766 AVContentLightMetadata *lightMetadata = NULL;
547 766 AVMasteringDisplayMetadata *displayMetadata = NULL;
548 766 int64_t metadata_size_pos = 0;
549 766 int64_t total_size = 0;
550 766 const AVPacketSideData *side_data = NULL;
551
552
2/2
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 465 times.
766 if (flv->metadata_pkt_written[stream_idx])
553 301 return;
554
555
4/4
✓ Branch 0 taken 461 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 459 times.
✓ Branch 3 taken 2 times.
465 if (par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 ||
556
4/4
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 456 times.
459 par->codec_id == AV_CODEC_ID_VP9 || par->codec_id == AV_CODEC_ID_VVC) {
557 9 int flags_size = 5;
558 9 side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
559 AV_PKT_DATA_CONTENT_LIGHT_LEVEL);
560
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (side_data)
561 1 lightMetadata = (AVContentLightMetadata *)side_data->data;
562
563 9 side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
564 AV_PKT_DATA_MASTERING_DISPLAY_METADATA);
565
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (side_data)
566 1 displayMetadata = (AVMasteringDisplayMetadata *)side_data->data;
567
568 /*
569 * Reference Enhancing FLV
570 * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp.pdf
571 * */
572 9 avio_w8(pb, FLV_TAG_TYPE_VIDEO); //write video tag type
573 9 metadata_size_pos = avio_tell(pb);
574 9 avio_wb24(pb, 0 + flags_size);
575 9 put_timestamp(pb, ts); //ts = pkt->dts, gen
576 9 avio_wb24(pb, flv->reserved);
577
578 9 avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata | FLV_FRAME_VIDEO_INFO_CMD); // ExVideoTagHeader mode with PacketTypeMetadata
579 9 write_codec_fourcc(pb, par->codec_id);
580
581 9 avio_w8(pb, AMF_DATA_TYPE_STRING);
582 9 put_amf_string(pb, "colorInfo");
583
584 9 avio_w8(pb, AMF_DATA_TYPE_OBJECT);
585
586 9 put_amf_string(pb, "colorConfig"); // colorConfig
587
588 9 avio_w8(pb, AMF_DATA_TYPE_OBJECT);
589
590
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
591
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 par->color_trc < AVCOL_TRC_NB) {
592 3 put_amf_string(pb, "transferCharacteristics"); // color_trc
593 3 put_amf_double(pb, par->color_trc);
594 }
595
596
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
597
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 par->color_space < AVCOL_SPC_NB) {
598 6 put_amf_string(pb, "matrixCoefficients"); // colorspace
599 6 put_amf_double(pb, par->color_space);
600 }
601
602
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
603
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 par->color_primaries < AVCOL_PRI_NB) {
604 3 put_amf_string(pb, "colorPrimaries"); // color_primaries
605 3 put_amf_double(pb, par->color_primaries);
606 }
607
608 9 put_amf_string(pb, "");
609 9 avio_w8(pb, AMF_END_OF_OBJECT);
610
611
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (lightMetadata) {
612 1 put_amf_string(pb, "hdrCll");
613 1 avio_w8(pb, AMF_DATA_TYPE_OBJECT);
614
615 1 put_amf_string(pb, "maxFall");
616 1 put_amf_double(pb, lightMetadata->MaxFALL);
617
618 1 put_amf_string(pb, "maxCLL");
619 1 put_amf_double(pb, lightMetadata->MaxCLL);
620
621 1 put_amf_string(pb, "");
622 1 avio_w8(pb, AMF_END_OF_OBJECT);
623 }
624
625
3/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
9 if (displayMetadata && (displayMetadata->has_primaries || displayMetadata->has_luminance)) {
626 1 put_amf_string(pb, "hdrMdcv");
627 1 avio_w8(pb, AMF_DATA_TYPE_OBJECT);
628
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (displayMetadata->has_primaries) {
629 1 put_amf_string(pb, "redX");
630 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[0][0]));
631
632 1 put_amf_string(pb, "redY");
633 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[0][1]));
634
635 1 put_amf_string(pb, "greenX");
636 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[1][0]));
637
638 1 put_amf_string(pb, "greenY");
639 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[1][1]));
640
641 1 put_amf_string(pb, "blueX");
642 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[2][0]));
643
644 1 put_amf_string(pb, "blueY");
645 1 put_amf_double(pb, av_q2d(displayMetadata->display_primaries[2][1]));
646
647 1 put_amf_string(pb, "whitePointX");
648 1 put_amf_double(pb, av_q2d(displayMetadata->white_point[0]));
649
650 1 put_amf_string(pb, "whitePointY");
651 1 put_amf_double(pb, av_q2d(displayMetadata->white_point[1]));
652 }
653
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (displayMetadata->has_luminance) {
654 1 put_amf_string(pb, "maxLuminance");
655 1 put_amf_double(pb, av_q2d(displayMetadata->max_luminance));
656
657 1 put_amf_string(pb, "minLuminance");
658 1 put_amf_double(pb, av_q2d(displayMetadata->min_luminance));
659 }
660 1 put_amf_string(pb, "");
661 1 avio_w8(pb, AMF_END_OF_OBJECT);
662 }
663 9 put_amf_string(pb, "");
664 9 avio_w8(pb, AMF_END_OF_OBJECT);
665
666 9 total_size = avio_tell(pb) - metadata_size_pos - 10;
667 9 avio_seek(pb, metadata_size_pos, SEEK_SET);
668 9 avio_wb24(pb, total_size);
669 9 avio_skip(pb, total_size + 10 - 3);
670 9 avio_wb32(pb, total_size + 11); // previous tag size
671 9 flv->metadata_pkt_written[stream_idx] = 1;
672 }
673 }
674
675 static int unsupported_codec(AVFormatContext *s,
676 const char* type, int codec_id)
677 {
678 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
679 av_log(s, AV_LOG_ERROR,
680 "%s codec %s not compatible with flv\n",
681 type,
682 desc ? desc->name : "unknown");
683 return AVERROR(ENOSYS);
684 }
685
686 3 static void flv_write_aac_header(AVFormatContext* s, AVCodecParameters* par)
687 {
688 3 AVIOContext *pb = s->pb;
689 3 FLVContext *flv = s->priv_data;
690
691
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (!par->extradata_size && (flv->flags & FLV_AAC_SEQ_HEADER_DETECT)) {
692 PutBitContext pbc;
693 int samplerate_index;
694 int channels = par->ch_layout.nb_channels
695 - (par->ch_layout.nb_channels == 8 ? 1 : 0);
696 uint8_t data[2];
697
698 for (samplerate_index = 0; samplerate_index < 16;
699 samplerate_index++)
700 if (par->sample_rate
701 == ff_mpeg4audio_sample_rates[samplerate_index])
702 break;
703
704 init_put_bits(&pbc, data, sizeof(data));
705 put_bits(&pbc, 5, par->profile + 1); //profile
706 put_bits(&pbc, 4, samplerate_index); //sample rate index
707 put_bits(&pbc, 4, channels);
708 put_bits(&pbc, 1, 0); //frame length - 1024 samples
709 put_bits(&pbc, 1, 0); //does not depend on core coder
710 put_bits(&pbc, 1, 0); //is not extension
711 flush_put_bits(&pbc);
712
713 avio_w8(pb, data[0]);
714 avio_w8(pb, data[1]);
715
716 av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n",
717 data[0], data[1]);
718 }
719 3 avio_write(pb, par->extradata, par->extradata_size);
720 3 }
721
722 5 static void flv_write_multichannel_body(AVFormatContext* s, AVCodecParameters* par)
723 {
724 5 AVIOContext *pb = s->pb;
725
726
1/3
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
5 switch (par->ch_layout.order) {
727 5 case AV_CHANNEL_ORDER_NATIVE:
728 5 avio_w8(pb, AudioChannelOrderNative);
729 5 break;
730 case AV_CHANNEL_ORDER_CUSTOM:
731 avio_w8(pb, AudioChannelOrderCustom);
732 break;
733 default:
734 avio_w8(pb, AudioChannelOrderUnspecified);
735 break;
736 }
737
738 5 avio_w8(pb, par->ch_layout.nb_channels);
739
740
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) {
741 // The first 18 entries are identical between FFmpeg and flv
742 5 uint32_t mask = par->ch_layout.u.mask & 0x03FFFF;
743 // The remaining 6 flv entries are in the right order, but start at AV_CHAN_LOW_FREQUENCY_2
744 5 mask |= (par->ch_layout.u.mask >> (AV_CHAN_LOW_FREQUENCY_2 - 18)) & 0xFC0000;
745
746 5 avio_wb32(pb, mask);
747 } else if (par->ch_layout.order == AV_CHANNEL_ORDER_CUSTOM) {
748 for (int i = 0; i < par->ch_layout.nb_channels; i++) {
749 enum AVChannel id = par->ch_layout.u.map[i].id;
750 if (id >= AV_CHAN_FRONT_LEFT && id <= AV_CHAN_TOP_BACK_RIGHT) {
751 avio_w8(pb, id - AV_CHAN_FRONT_LEFT + 0);
752 } else if (id >= AV_CHAN_LOW_FREQUENCY_2 && id <= AV_CHAN_BOTTOM_FRONT_RIGHT) {
753 avio_w8(pb, id - AV_CHAN_LOW_FREQUENCY_2 + 18);
754 } else if (id == AV_CHAN_UNUSED) {
755 avio_w8(pb, 0xFE);
756 } else {
757 avio_w8(pb, 0xFF); // unknown
758 }
759 }
760 }
761 5 }
762
763 5 static int flv_get_multichannel_body_size(AVCodecParameters* par)
764 {
765 5 int res = 2;
766
767
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE)
768 5 res += 4;
769 else if (par->ch_layout.order == AV_CHANNEL_ORDER_CUSTOM)
770 res += par->ch_layout.nb_channels;
771
772 5 return res;
773 }
774
775 5 static void flv_write_multichannel_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts, int stream_index)
776 {
777 5 AVIOContext *pb = s->pb;
778 5 FLVContext *flv = s->priv_data;
779
780 5 int track_idx = flv->track_idx_map[stream_index];
781 5 int data_size = flv_get_multichannel_body_size(par);
782
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (track_idx)
783 4 data_size += 2;
784
785 5 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
786 5 avio_wb24(pb, data_size + 5); // size
787 5 put_timestamp(pb, ts);
788 5 avio_wb24(pb, 0); // streamid
789
790
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (track_idx) {
791 4 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack);
792 4 avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeMultichannelConfig);
793 } else {
794 1 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultichannelConfig);
795 }
796
797 5 write_codec_fourcc(pb, par->codec_id);
798
799
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (track_idx)
800 4 avio_w8(pb, track_idx);
801
802 5 flv_write_multichannel_body(s, par);
803
804 5 avio_wb32(pb, data_size + 5 + 11); // previous tag size
805 5 }
806
807 34 static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts, int stream_index) {
808 int64_t data_size;
809 34 AVIOContext *pb = s->pb;
810 34 FLVContext *flv = s->priv_data;
811 34 int track_idx = flv->track_idx_map[stream_index];
812 34 int extended_flv = 0;
813
814
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 2 times.
34 if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
815
3/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 4 times.
29 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC
816
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 2 times.
25 || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id == AV_CODEC_ID_VP9
817
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 || par->codec_id == AV_CODEC_ID_VVC
818
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 || (par->codec_id == AV_CODEC_ID_MP3 && track_idx)
819
4/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1 times.
20 || par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC
820
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
18 || par->codec_id == AV_CODEC_ID_AC3 || par->codec_id == AV_CODEC_ID_EAC3) {
821 int64_t pos;
822 17 avio_w8(pb,
823
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
17 par->codec_type == AVMEDIA_TYPE_VIDEO ?
824 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
825 17 avio_wb24(pb, 0); // size patched later
826 17 put_timestamp(pb, ts);
827 17 avio_wb24(pb, 0); // streamid
828 17 pos = avio_tell(pb);
829
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 11 times.
17 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
830
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 extended_flv = (par->codec_id == AV_CODEC_ID_AAC && track_idx)
831
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 || (par->codec_id == AV_CODEC_ID_MP3 && track_idx)
832
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 || par->codec_id == AV_CODEC_ID_OPUS
833
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 || par->codec_id == AV_CODEC_ID_FLAC
834
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 || par->codec_id == AV_CODEC_ID_AC3
835
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
12 || par->codec_id == AV_CODEC_ID_EAC3;
836
837
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (extended_flv) {
838
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (track_idx) {
839 4 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack);
840 4 avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeSequenceStart);
841 } else {
842 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeSequenceStart);
843 }
844
845 4 write_codec_fourcc(pb, par->codec_id);
846
847
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (track_idx)
848 4 avio_w8(pb, track_idx);
849
850
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (par->codec_id == AV_CODEC_ID_AAC) {
851 1 flv_write_aac_header(s, par);
852
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 } else if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) {
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(par->extradata_size);
854 2 avio_write(pb, par->extradata, par->extradata_size);
855 }
856
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (par->codec_id == AV_CODEC_ID_AAC) {
857 2 avio_w8(pb, get_audio_flags(s, par));
858 2 avio_w8(pb, 0); // AAC sequence header
859
860 2 flv_write_aac_header(s, par);
861 }
862 } else {
863 // If video stream has track_idx > 0 we need to send H.264 as extended video packet
864
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 extended_flv = (par->codec_id == AV_CODEC_ID_H264 && track_idx) ||
865
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 par->codec_id == AV_CODEC_ID_HEVC ||
866
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 par->codec_id == AV_CODEC_ID_VVC ||
867
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
25 par->codec_id == AV_CODEC_ID_AV1 ||
868
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 par->codec_id == AV_CODEC_ID_VP9;
869
870
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 if (extended_flv) {
871
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (track_idx) {
872 4 avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMultitrack | FLV_FRAME_KEY);
873 4 avio_w8(pb, MultitrackTypeOneTrack | PacketTypeSequenceStart);
874 } else {
875 6 avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart | FLV_FRAME_KEY);
876 }
877
878 10 write_codec_fourcc(pb, par->codec_id);
879
880
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (track_idx)
881 4 avio_w8(pb, track_idx);
882 } else {
883 1 avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
884 1 avio_w8(pb, 0); // AVC sequence header
885 1 avio_wb24(pb, 0); // composition time
886 }
887
888
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if (par->codec_id == AV_CODEC_ID_HEVC)
889 4 ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0, s);
890
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 else if (par->codec_id == AV_CODEC_ID_VVC)
891 1 ff_isom_write_vvcc(pb, par->extradata, par->extradata_size, 1);
892
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 else if (par->codec_id == AV_CODEC_ID_AV1)
893 2 ff_isom_write_av1c(pb, par->extradata, par->extradata_size, 1);
894
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 else if (par->codec_id == AV_CODEC_ID_VP9)
895 2 ff_isom_write_vpcc(s, pb, par->extradata, par->extradata_size, par);
896
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 else if (par->codec_id == AV_CODEC_ID_H264)
897 2 ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
898 else if (par->codec_id == AV_CODEC_ID_MPEG4)
899 avio_write(pb, par->extradata, par->extradata_size);
900 else
901 av_assert0(0);
902 }
903 17 data_size = avio_tell(pb) - pos;
904 17 avio_seek(pb, -data_size - 10, SEEK_CUR);
905 17 avio_wb24(pb, data_size);
906 17 avio_skip(pb, data_size + 10 - 3);
907 17 avio_wb32(pb, data_size + 11); // previous tag size
908 }
909
910
6/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
39 if (par->codec_type == AVMEDIA_TYPE_AUDIO && (extended_flv ||
911
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
7 (av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) == 1 &&
912 2 av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) == 1)))
913 5 flv_write_multichannel_header(s, par, ts, stream_index);
914 34 }
915
916 20 static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos)
917 {
918 20 FLVFileposition *position = av_malloc(sizeof(FLVFileposition));
919
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!position) {
921 av_log(s, AV_LOG_WARNING, "no mem for add keyframe index!\n");
922 return AVERROR(ENOMEM);
923 }
924
925 20 position->keyframe_timestamp = ts;
926 20 position->keyframe_position = pos;
927
928
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
20 if (!flv->filepositions_count) {
929 1 flv->filepositions = position;
930 1 flv->head_filepositions = flv->filepositions;
931 1 position->next = NULL;
932 } else {
933 19 flv->filepositions->next = position;
934 19 position->next = NULL;
935 19 flv->filepositions = flv->filepositions->next;
936 }
937
938 20 flv->filepositions_count++;
939
940 20 return 0;
941 }
942
943 1 static int shift_data(AVFormatContext *s)
944 {
945 int ret;
946 1 int64_t metadata_size = 0;
947 1 FLVContext *flv = s->priv_data;
948
949 1 metadata_size = flv->filepositions_count * 9 * 2 + 10; /* filepositions and times value */
950 1 metadata_size += 2 + 13; /* filepositions String */
951 1 metadata_size += 2 + 5; /* times String */
952 1 metadata_size += 3; /* Object end */
953
954 1 flv->keyframe_index_size = metadata_size;
955
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (metadata_size < 0)
957 return metadata_size;
958
959 1 ret = ff_format_shift_data(s, flv->keyframes_info_offset, metadata_size);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
961 return ret;
962
963 1 avio_seek(s->pb, flv->metadata_size_pos, SEEK_SET);
964 1 avio_wb24(s->pb, flv->metadata_totalsize + metadata_size);
965
966 1 avio_seek(s->pb, flv->metadata_totalsize_pos + metadata_size, SEEK_SET);
967 1 avio_wb32(s->pb, flv->metadata_totalsize + 11 + metadata_size);
968
969 1 return 0;
970 }
971
972 24 static int flv_init(struct AVFormatContext *s)
973 {
974 int i;
975 24 int video_ctr = 0, audio_ctr = 0;
976 24 FLVContext *flv = s->priv_data;
977
978 24 flv->last_ts = av_calloc(s->nb_streams, sizeof(*flv->last_ts));
979 24 flv->metadata_pkt_written = av_calloc(s->nb_streams, sizeof(*flv->metadata_pkt_written));
980 24 flv->track_idx_map = av_calloc(s->nb_streams, sizeof(*flv->track_idx_map));
981
3/6
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 if (!flv->last_ts || !flv->metadata_pkt_written || !flv->track_idx_map)
982 return AVERROR(ENOMEM);
983
984
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24 times.
58 for (i = 0; i < s->nb_streams; i++) {
985 34 AVCodecParameters *par = s->streams[i]->codecpar;
986
987
2/5
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
34 switch (par->codec_type) {
988 25 case AVMEDIA_TYPE_VIDEO:
989
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 21 times.
25 if (video_ctr &&
990
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 par->codec_id != AV_CODEC_ID_VP8 &&
991
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 par->codec_id != AV_CODEC_ID_VP9 &&
992
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 par->codec_id != AV_CODEC_ID_AV1 &&
993
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 par->codec_id != AV_CODEC_ID_H264 &&
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 par->codec_id != AV_CODEC_ID_HEVC &&
995 par->codec_id != AV_CODEC_ID_VVC) {
996 av_log(s, AV_LOG_ERROR, "Unsupported multi-track video codec.\n");
997 return AVERROR(EINVAL);
998 }
999
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
25 if (s->streams[i]->avg_frame_rate.den &&
1000
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 s->streams[i]->avg_frame_rate.num) {
1001 23 flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
1002 }
1003 25 flv->track_idx_map[i] = video_ctr++;
1004
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
25 if (flv->video_par && flv->flags & FLV_ADD_KEYFRAME_INDEX) {
1005 av_log(s, AV_LOG_ERROR,
1006 "at most one video stream is supported in flv with keyframe index\n");
1007 return AVERROR(EINVAL);
1008
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 21 times.
25 } else if (flv->video_par) {
1009 4 av_log(s, AV_LOG_WARNING,
1010 "more than one video stream is not supported by most flv demuxers.\n");
1011 }
1012
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 4 times.
25 if (!flv->video_par)
1013 21 flv->video_par = par;
1014
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (!ff_codec_get_tag(flv_video_codec_ids, par->codec_id))
1015 return unsupported_codec(s, "Video", par->codec_id);
1016
1017
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (par->codec_id == AV_CODEC_ID_MPEG4 ||
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 par->codec_id == AV_CODEC_ID_H263) {
1019 int error = s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL;
1020 av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
1021 "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id));
1022
1023 if (error) {
1024 av_log(s, AV_LOG_ERROR,
1025 "use vstrict=-1 / -strict -1 to use it anyway.\n");
1026 return AVERROR(EINVAL);
1027 }
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 } else if (par->codec_id == AV_CODEC_ID_VP6) {
1029 av_log(s, AV_LOG_WARNING,
1030 "Muxing VP6 in flv will produce flipped video on playback.\n");
1031 }
1032 25 break;
1033 9 case AVMEDIA_TYPE_AUDIO:
1034
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (audio_ctr &&
1035
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 par->codec_id != AV_CODEC_ID_AAC &&
1036
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 par->codec_id != AV_CODEC_ID_MP3 &&
1037
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 par->codec_id != AV_CODEC_ID_OPUS &&
1038
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 par->codec_id != AV_CODEC_ID_FLAC &&
1039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 par->codec_id != AV_CODEC_ID_AC3 &&
1040 par->codec_id != AV_CODEC_ID_EAC3) {
1041 av_log(s, AV_LOG_ERROR, "Unsupported multi-track audio codec.\n");
1042 return AVERROR(EINVAL);
1043 }
1044 9 flv->track_idx_map[i] = audio_ctr++;
1045
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (flv->audio_par)
1046 4 av_log(s, AV_LOG_WARNING,
1047 "more than one audio stream is not supported by most flv demuxers.\n");
1048 else
1049 5 flv->audio_par = par;
1050
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_audio_flags(s, par) < 0)
1051 return unsupported_codec(s, "Audio", par->codec_id);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (par->codec_id == AV_CODEC_ID_PCM_S16BE)
1053 av_log(s, AV_LOG_WARNING,
1054 "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
1055 9 break;
1056 case AVMEDIA_TYPE_DATA:
1057 if (par->codec_id != AV_CODEC_ID_TEXT && par->codec_id != AV_CODEC_ID_NONE)
1058 return unsupported_codec(s, "Data", par->codec_id);
1059 flv->data_par = par;
1060 break;
1061 case AVMEDIA_TYPE_SUBTITLE:
1062 if (par->codec_id != AV_CODEC_ID_TEXT) {
1063 av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
1064 avcodec_get_name(par->codec_id), i);
1065 return AVERROR_INVALIDDATA;
1066 }
1067 flv->data_par = par;
1068 break;
1069 default:
1070 av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
1071 av_get_media_type_string(par->codec_type), i);
1072 return AVERROR(EINVAL);
1073 }
1074 34 avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
1075 34 flv->last_ts[i] = -1;
1076 }
1077
1078 24 flv->delay = AV_NOPTS_VALUE;
1079
1080 24 return 0;
1081 }
1082
1083 24 static int flv_write_header(AVFormatContext *s)
1084 {
1085 int i;
1086 24 AVIOContext *pb = s->pb;
1087 24 FLVContext *flv = s->priv_data;
1088
1089 24 avio_write(pb, "FLV", 3);
1090 24 avio_w8(pb, 1);
1091
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!flv->audio_par +
1092 24 FLV_HEADER_FLAG_HASVIDEO * !!flv->video_par);
1093 24 avio_wb32(pb, 9);
1094 24 avio_wb32(pb, 0);
1095
1096
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24 times.
58 for (i = 0; i < s->nb_streams; i++)
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (s->streams[i]->codecpar->codec_tag == 5) {
1098 avio_w8(pb, 8); // message type
1099 avio_wb24(pb, 0); // include flags
1100 avio_wb24(pb, 0); // time stamp
1101 avio_wb32(pb, 0); // reserved
1102 avio_wb32(pb, 11); // size
1103 flv->reserved = 5;
1104 }
1105
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (flv->flags & FLV_NO_METADATA) {
1107 pb->seekable = 0;
1108 } else {
1109 24 write_metadata(s, 0);
1110 }
1111
1112
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24 times.
58 for (i = 0; i < s->nb_streams; i++) {
1113 34 flv_write_codec_header(s, s->streams[i]->codecpar, 0, i);
1114 }
1115
1116 24 flv->datastart_offset = avio_tell(pb);
1117 24 return 0;
1118 }
1119
1120 24 static int flv_write_trailer(AVFormatContext *s)
1121 {
1122 int64_t file_size;
1123 24 AVIOContext *pb = s->pb;
1124 24 FLVContext *flv = s->priv_data;
1125 24 int build_keyframes_idx = flv->flags & FLV_ADD_KEYFRAME_INDEX;
1126 int i, res;
1127 24 int64_t cur_pos = avio_tell(s->pb);
1128
1129
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
24 if (build_keyframes_idx) {
1130 const FLVFileposition *newflv_posinfo;
1131
1132 1 avio_seek(pb, flv->videosize_offset, SEEK_SET);
1133 1 put_amf_double(pb, flv->videosize);
1134
1135 1 avio_seek(pb, flv->audiosize_offset, SEEK_SET);
1136 1 put_amf_double(pb, flv->audiosize);
1137
1138 1 avio_seek(pb, flv->lasttimestamp_offset, SEEK_SET);
1139 1 put_amf_double(pb, flv->lasttimestamp);
1140
1141 1 avio_seek(pb, flv->lastkeyframetimestamp_offset, SEEK_SET);
1142 1 put_amf_double(pb, flv->lastkeyframetimestamp);
1143
1144 1 avio_seek(pb, flv->lastkeyframelocation_offset, SEEK_SET);
1145 1 put_amf_double(pb, flv->lastkeyframelocation + flv->keyframe_index_size);
1146 1 avio_seek(pb, cur_pos, SEEK_SET);
1147
1148 1 res = shift_data(s);
1149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (res < 0) {
1150 goto end;
1151 }
1152 1 avio_seek(pb, flv->keyframes_info_offset, SEEK_SET);
1153 1 put_amf_string(pb, "filepositions");
1154 1 put_amf_dword_array(pb, flv->filepositions_count);
1155
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) {
1156 20 put_amf_double(pb, newflv_posinfo->keyframe_position + flv->keyframe_index_size);
1157 }
1158
1159 1 put_amf_string(pb, "times");
1160 1 put_amf_dword_array(pb, flv->filepositions_count);
1161
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) {
1162 20 put_amf_double(pb, newflv_posinfo->keyframe_timestamp);
1163 }
1164
1165 1 put_amf_string(pb, "");
1166 1 avio_w8(pb, AMF_END_OF_OBJECT);
1167
1168 1 avio_seek(pb, cur_pos + flv->keyframe_index_size, SEEK_SET);
1169 }
1170
1171 23 end:
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (flv->flags & FLV_NO_SEQUENCE_END) {
1173 av_log(s, AV_LOG_DEBUG, "FLV no sequence end mode open\n");
1174 } else {
1175 /* Add EOS tag */
1176
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24 times.
58 for (i = 0; i < s->nb_streams; i++) {
1177 34 AVCodecParameters *par = s->streams[i]->codecpar;
1178
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 9 times.
34 if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
1179
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
25 (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4))
1180 2 put_eos_tag(pb, flv->last_ts[i], par->codec_id);
1181 }
1182 }
1183
1184 24 file_size = avio_tell(pb);
1185
1186
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 if (build_keyframes_idx) {
1187 1 flv->datasize = file_size - flv->datastart_offset;
1188 1 avio_seek(pb, flv->datasize_offset, SEEK_SET);
1189 1 put_amf_double(pb, flv->datasize);
1190 }
1191
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!(flv->flags & FLV_NO_METADATA)) {
1192
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!(flv->flags & FLV_NO_DURATION_FILESIZE)) {
1193 /* update information */
1194
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0) {
1195 av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
1196 } else {
1197 24 put_amf_double(pb, flv->duration / (double)1000);
1198 }
1199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0) {
1200 av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
1201 } else {
1202 24 put_amf_double(pb, file_size);
1203 }
1204 }
1205 }
1206
1207 24 return 0;
1208 }
1209
1210 2084 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
1211 {
1212 2084 AVIOContext *pb = s->pb;
1213 2084 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
1214 2084 FLVContext *flv = s->priv_data;
1215 unsigned ts;
1216 2084 int size = pkt->size;
1217 2084 uint8_t *data = NULL;
1218
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 870 times.
2084 uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
1219 2084 int flags = -1, flags_size, ret = 0;
1220 2084 int64_t cur_offset = avio_tell(pb);
1221 2084 int track_idx = flv->track_idx_map[pkt->stream_index];
1222
1223
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 72 times.
288 int extended_audio = (par->codec_id == AV_CODEC_ID_AAC && track_idx)
1224
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2012 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2012 || (par->codec_id == AV_CODEC_ID_MP3 && track_idx)
1225
2/2
✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 76 times.
2012 || par->codec_id == AV_CODEC_ID_OPUS
1226
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 16 times.
1936 || par->codec_id == AV_CODEC_ID_FLAC
1227
2/2
✓ Branch 0 taken 1873 times.
✓ Branch 1 taken 47 times.
1920 || par->codec_id == AV_CODEC_ID_AC3
1228
3/4
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 1796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1873 times.
4168 || par->codec_id == AV_CODEC_ID_EAC3;
1229
1230
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 1873 times.
2084 if (extended_audio)
1231 211 flags_size = 5;
1232
2/4
✓ Branch 0 taken 1873 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1873 times.
✗ Branch 3 not taken.
1873 else if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
1233
3/4
✓ Branch 0 taken 1873 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 1657 times.
1873 par->codec_id == AV_CODEC_ID_VP6 || par->codec_id == AV_CODEC_ID_AAC)
1234 216 flags_size = 2;
1235
3/4
✓ Branch 0 taken 1581 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 1581 times.
✗ Branch 3 not taken.
1657 else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 ||
1236
4/4
✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 1334 times.
✓ Branch 3 taken 100 times.
1581 par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 ||
1237
4/4
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1271 times.
1334 par->codec_id == AV_CODEC_ID_VP9 || par->codec_id == AV_CODEC_ID_VVC)
1238 386 flags_size = 5;
1239 else
1240 1271 flags_size = 1;
1241
1242
5/6
✓ Branch 0 taken 933 times.
✓ Branch 1 taken 1151 times.
✓ Branch 2 taken 933 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 357 times.
✓ Branch 5 taken 1727 times.
2084 if ((par->codec_type == AVMEDIA_TYPE_VIDEO || par->codec_type == AVMEDIA_TYPE_AUDIO) && track_idx)
1243 357 flags_size += 2; // additional header bytes for multi-track flv
1244
1245
4/4
✓ Branch 0 taken 1937 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 1920 times.
✓ Branch 3 taken 17 times.
2084 if ((par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_VVC ||
1246
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1844 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 38 times.
1920 (par->codec_id == AV_CODEC_ID_H264 && track_idx))
1247
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 39 times.
202 && pkt->pts != pkt->dts)
1248 163 flags_size += 3;
1249
1250
4/4
✓ Branch 0 taken 1796 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 1720 times.
✓ Branch 3 taken 76 times.
2084 if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
1251
3/4
✓ Branch 0 taken 1720 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1573 times.
✓ Branch 3 taken 147 times.
1720 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC
1252
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 17 times.
1573 || par->codec_id == AV_CODEC_ID_VVC
1253
4/4
✓ Branch 0 taken 1456 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 1410 times.
✓ Branch 3 taken 46 times.
1556 || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id == AV_CODEC_ID_VP9
1254
4/4
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 1318 times.
1410 || par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) {
1255 size_t side_size;
1256 766 uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1257
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
766 if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
1258 ret = ff_alloc_extradata(par, side_size);
1259 if (ret < 0)
1260 return ret;
1261 memcpy(par->extradata, side, side_size);
1262 flv_write_codec_header(s, par, pkt->dts, pkt->stream_index);
1263 }
1264 766 flv_write_metadata_packet(s, par, pkt->dts, pkt->stream_index);
1265 }
1266
1267
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2060 times.
2084 if (flv->delay == AV_NOPTS_VALUE)
1268 24 flv->delay = -pkt->dts;
1269
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2084 times.
2084 if (pkt->dts < -flv->delay) {
1271 av_log(s, AV_LOG_WARNING,
1272 "Packets are not in the proper order with respect to DTS\n");
1273 return AVERROR(EINVAL);
1274 }
1275
3/4
✓ Branch 0 taken 2008 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 2008 times.
✗ Branch 3 not taken.
2084 if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 ||
1276
4/4
✓ Branch 0 taken 1861 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 1761 times.
✓ Branch 3 taken 100 times.
2008 par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 ||
1277
4/4
✓ Branch 0 taken 1715 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1698 times.
1761 par->codec_id == AV_CODEC_ID_VP9 || par->codec_id == AV_CODEC_ID_VVC) {
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386 times.
386 if (pkt->pts == AV_NOPTS_VALUE) {
1279 av_log(s, AV_LOG_ERROR, "Packet is missing PTS\n");
1280 return AVERROR(EINVAL);
1281 }
1282 }
1283
1284 2084 ts = pkt->dts;
1285
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2084 times.
2084 if (s->event_flags & AVFMT_EVENT_FLAG_METADATA_UPDATED) {
1287 write_metadata(s, ts);
1288 s->event_flags &= ~AVFMT_EVENT_FLAG_METADATA_UPDATED;
1289 }
1290
1291 2084 avio_write_marker(pb, av_rescale(ts, AV_TIME_BASE, 1000),
1292
6/6
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 870 times.
✓ Branch 2 taken 708 times.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 281 times.
✓ Branch 5 taken 427 times.
2084 pkt->flags & AV_PKT_FLAG_KEY && (flv->video_par ? par->codec_type == AVMEDIA_TYPE_VIDEO : 1) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT);
1293
1294
2/4
✓ Branch 0 taken 1151 times.
✓ Branch 1 taken 933 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2084 switch (par->codec_type) {
1295 1151 case AVMEDIA_TYPE_VIDEO:
1296 1151 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
1297
1298 1151 flags = ff_codec_get_tag(flv_video_codec_ids, par->codec_id);
1299
1300 1151 flags |= frametype;
1301 1151 break;
1302 933 case AVMEDIA_TYPE_AUDIO:
1303 933 flags = get_audio_flags(s, par);
1304
1305 933 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
1306 933 break;
1307 case AVMEDIA_TYPE_SUBTITLE:
1308 case AVMEDIA_TYPE_DATA:
1309 avio_w8(pb, FLV_TAG_TYPE_META);
1310 break;
1311 default:
1312 return AVERROR(EINVAL);
1313 }
1314
1315
3/4
✓ Branch 0 taken 2008 times.
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2008 times.
2084 if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
1316 /* check if extradata looks like mp4 formatted */
1317
2/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
76 if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
1318 if ((ret = ff_nal_parse_units_buf(pkt->data, &data, &size)) < 0)
1319 return ret;
1320
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 1861 times.
2008 } else if (par->codec_id == AV_CODEC_ID_HEVC) {
1321
3/4
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 146 times.
147 if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
1322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0)
1323 return ret;
1324
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1844 times.
1861 } else if (par->codec_id == AV_CODEC_ID_VVC) {
1325
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 if (par->extradata_size > 0 && (*(uint8_t*)par->extradata & 0xF8) != 0xF8)
1326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if ((ret = ff_vvc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0)
1327 return ret;
1328
3/4
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 1556 times.
✓ Branch 2 taken 288 times.
✗ Branch 3 not taken.
1844 } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
1329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
1330 if (!s->streams[pkt->stream_index]->nb_frames) {
1331 av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
1332 "use the audio bitstream filter 'aac_adtstoasc' to fix it "
1333 "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
1334 return AVERROR_INVALIDDATA;
1335 }
1336 av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
1337 }
1338
1339 /* check Speex packet duration */
1340
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2084 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2084 if (par->codec_id == AV_CODEC_ID_SPEEX && ts - flv->last_ts[pkt->stream_index] > 160)
1341 av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
1342 "8 frames per packet. Adobe Flash "
1343 "Player cannot handle this!\n");
1344
1345
1/2
✓ Branch 0 taken 2084 times.
✗ Branch 1 not taken.
2084 if (flv->last_ts[pkt->stream_index] < ts)
1346 2084 flv->last_ts[pkt->stream_index] = ts;
1347
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2084 times.
2084 if (size + flags_size >= 1<<24) {
1349 av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n",
1350 size + flags_size, 1<<24);
1351 ret = AVERROR(EINVAL);
1352 goto fail;
1353 }
1354
1355 2084 avio_wb24(pb, size + flags_size);
1356 2084 put_timestamp(pb, ts);
1357 2084 avio_wb24(pb, flv->reserved);
1358
1359
1/2
✓ Branch 0 taken 2084 times.
✗ Branch 1 not taken.
2084 if (par->codec_type == AVMEDIA_TYPE_DATA ||
1360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2084 times.
2084 par->codec_type == AVMEDIA_TYPE_SUBTITLE ) {
1361 int data_size;
1362 int64_t metadata_size_pos = avio_tell(pb);
1363 if (par->codec_id == AV_CODEC_ID_TEXT) {
1364 // legacy FFmpeg magic?
1365 avio_w8(pb, AMF_DATA_TYPE_STRING);
1366 put_amf_string(pb, "onTextData");
1367 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
1368 avio_wb32(pb, 2);
1369 put_amf_string(pb, "type");
1370 avio_w8(pb, AMF_DATA_TYPE_STRING);
1371 put_amf_string(pb, "Text");
1372 put_amf_string(pb, "text");
1373 avio_w8(pb, AMF_DATA_TYPE_STRING);
1374 put_amf_string(pb, pkt->data);
1375 put_amf_string(pb, "");
1376 avio_w8(pb, AMF_END_OF_OBJECT);
1377 } else {
1378 // just pass the metadata through
1379 avio_write(pb, data ? data : pkt->data, size);
1380 }
1381 /* write total size of tag */
1382 data_size = avio_tell(pb) - metadata_size_pos;
1383 avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
1384 avio_wb24(pb, data_size);
1385 avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
1386 avio_wb32(pb, data_size + 11);
1387 } else {
1388
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 38 times.
76 int extended_video = (par->codec_id == AV_CODEC_ID_H264 && track_idx) ||
1389
2/2
✓ Branch 0 taken 1899 times.
✓ Branch 1 taken 147 times.
2046 par->codec_id == AV_CODEC_ID_HEVC ||
1390
2/2
✓ Branch 0 taken 1882 times.
✓ Branch 1 taken 17 times.
1899 par->codec_id == AV_CODEC_ID_VVC ||
1391
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2008 times.
✓ Branch 2 taken 1782 times.
✓ Branch 3 taken 100 times.
5950 par->codec_id == AV_CODEC_ID_AV1 ||
1392
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1736 times.
1782 par->codec_id == AV_CODEC_ID_VP9;
1393
1394
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 1736 times.
2084 if (extended_video) {
1395 1006 int h26456 = par->codec_id == AV_CODEC_ID_H264 ||
1396
4/4
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 293 times.
✓ Branch 3 taken 17 times.
641 par->codec_id == AV_CODEC_ID_VVC ||
1397
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 146 times.
293 par->codec_id == AV_CODEC_ID_HEVC;
1398 348 int pkttype = PacketTypeCodedFrames;
1399 // Optimisation for VVC/HEVC/H264: Do not send composition time if DTS == PTS
1400
4/4
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 163 times.
348 if (h26456 && pkt->pts == pkt->dts)
1401 39 pkttype = PacketTypeCodedFramesX;
1402
1403
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 202 times.
348 if (track_idx) {
1404 146 avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMultitrack | frametype);
1405 146 avio_w8(pb, MultitrackTypeOneTrack | pkttype);
1406 } else {
1407 202 avio_w8(pb, FLV_IS_EX_HEADER | pkttype | frametype);
1408 }
1409
1410 348 write_codec_fourcc(pb, par->codec_id);
1411
1412
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 202 times.
348 if (track_idx)
1413 146 avio_w8(pb, track_idx);
1414
4/4
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 163 times.
✓ Branch 3 taken 39 times.
348 if (h26456 && pkttype == PacketTypeCodedFrames)
1415 163 avio_wb24(pb, pkt->pts - pkt->dts);
1416
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 1525 times.
1736 } else if (extended_audio) {
1417
1/2
✓ Branch 0 taken 211 times.
✗ Branch 1 not taken.
211 if (track_idx) {
1418 211 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack);
1419 211 avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeCodedFrames);
1420 } else {
1421 avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeCodedFrames);
1422 }
1423 211 write_codec_fourcc(pb, par->codec_id);
1424
1/2
✓ Branch 0 taken 211 times.
✗ Branch 1 not taken.
211 if (track_idx)
1425 211 avio_w8(pb, track_idx);
1426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
1525 } else if (track_idx) {
1427 av_log(s, AV_LOG_ERROR, "Attempted to write legacy codec into extended flv track.\n");
1428 ret = AVERROR(EINVAL);
1429 goto fail;
1430 } else {
1431 av_assert1(flags >= 0);
1432 1525 avio_w8(pb, flags);
1433
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
1525 if (par->codec_id == AV_CODEC_ID_VP6) {
1435 avio_w8(pb,0);
1436
1/2
✓ Branch 0 taken 1525 times.
✗ Branch 1 not taken.
1525 } else if (par->codec_id == AV_CODEC_ID_VP6F ||
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
1525 par->codec_id == AV_CODEC_ID_VP6A) {
1438 if (par->extradata_size)
1439 avio_w8(pb, par->extradata[0]);
1440 else
1441 avio_w8(pb, ((FFALIGN(par->width, 16) - par->width) << 4) |
1442 (FFALIGN(par->height, 16) - par->height));
1443
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1309 times.
1525 } else if (par->codec_id == AV_CODEC_ID_AAC) {
1444 216 avio_w8(pb, 1); // AAC raw
1445
2/2
✓ Branch 0 taken 1271 times.
✓ Branch 1 taken 38 times.
1309 } else if (par->codec_id == AV_CODEC_ID_H264 ||
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271 times.
1271 par->codec_id == AV_CODEC_ID_MPEG4) {
1447 38 avio_w8(pb, 1); // AVC NALU
1448 38 avio_wb24(pb, pkt->pts - pkt->dts);
1449 }
1450 }
1451
1452
2/2
✓ Branch 0 taken 2066 times.
✓ Branch 1 taken 18 times.
2084 avio_write(pb, data ? data : pkt->data, size);
1453
1454 2084 avio_wb32(pb, size + flags_size + 11); // previous tag size
1455 2084 flv->duration = FFMAX(flv->duration,
1456 pkt->pts + flv->delay + pkt->duration);
1457 }
1458
1459
2/2
✓ Branch 0 taken 1944 times.
✓ Branch 1 taken 140 times.
2084 if (flv->flags & FLV_ADD_KEYFRAME_INDEX) {
1460
1/3
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
140 switch (par->codec_type) {
1461 140 case AVMEDIA_TYPE_VIDEO:
1462 140 flv->videosize += (avio_tell(pb) - cur_offset);
1463 140 flv->lasttimestamp = pkt->dts / 1000.0;
1464
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 120 times.
140 if (pkt->flags & AV_PKT_FLAG_KEY) {
1465 20 flv->lastkeyframetimestamp = flv->lasttimestamp;
1466 20 flv->lastkeyframelocation = cur_offset;
1467 20 ret = flv_append_keyframe_info(s, flv, flv->lasttimestamp, cur_offset);
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
1469 goto fail;
1470 }
1471 140 break;
1472
1473 case AVMEDIA_TYPE_AUDIO:
1474 flv->audiosize += (avio_tell(pb) - cur_offset);
1475 break;
1476
1477 default:
1478 av_log(s, AV_LOG_WARNING, "par->codec_type is type = [%d]\n", par->codec_type);
1479 break;
1480 }
1481 }
1482 1944 fail:
1483 2084 av_free(data);
1484
1485 2084 return ret;
1486 }
1487
1488 34 static int flv_check_bitstream(AVFormatContext *s, AVStream *st,
1489 const AVPacket *pkt)
1490 {
1491
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
34 if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1492
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
1493 return ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
1494 }
1495
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 15 times.
34 if (!st->codecpar->extradata_size &&
1496
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 (st->codecpar->codec_id == AV_CODEC_ID_H264 ||
1497
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 st->codecpar->codec_id == AV_CODEC_ID_HEVC ||
1498
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 st->codecpar->codec_id == AV_CODEC_ID_VVC ||
1499
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 st->codecpar->codec_id == AV_CODEC_ID_AV1 ||
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 st->codecpar->codec_id == AV_CODEC_ID_MPEG4))
1501 return ff_stream_add_bitstream_filter(st, "extract_extradata", NULL);
1502 34 return 1;
1503 }
1504
1505 24 static void flv_deinit(AVFormatContext *s)
1506 {
1507 24 FLVContext *flv = s->priv_data;
1508 24 FLVFileposition *filepos = flv->head_filepositions;
1509
1510
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 24 times.
44 while (filepos) {
1511 20 FLVFileposition *next = filepos->next;
1512 20 av_free(filepos);
1513 20 filepos = next;
1514 }
1515 24 flv->filepositions = flv->head_filepositions = NULL;
1516 24 flv->filepositions_count = 0;
1517
1518 24 av_freep(&flv->last_ts);
1519 24 av_freep(&flv->metadata_pkt_written);
1520 24 av_freep(&flv->track_idx_map);
1521 24 }
1522
1523 static const AVOption options[] = {
1524 { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1525 { "aac_seq_header_detect", "Put AAC sequence header based on stream data", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_AAC_SEQ_HEADER_DETECT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1526 { "no_sequence_end", "disable sequence end for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_SEQUENCE_END}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1527 { "no_metadata", "disable metadata for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_METADATA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1528 { "no_duration_filesize", "disable duration and filesize zero value metadata for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_DURATION_FILESIZE}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1529 { "add_keyframe_index", "Add keyframe index metadata", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_ADD_KEYFRAME_INDEX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" },
1530 { NULL },
1531 };
1532
1533 static const AVClass flv_muxer_class = {
1534 .class_name = "flv muxer",
1535 .item_name = av_default_item_name,
1536 .option = options,
1537 .version = LIBAVUTIL_VERSION_INT,
1538 };
1539
1540 const FFOutputFormat ff_flv_muxer = {
1541 .p.name = "flv",
1542 .p.long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1543 .p.mime_type = "video/x-flv",
1544 .p.extensions = "flv",
1545 .priv_data_size = sizeof(FLVContext),
1546 .p.audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
1547 .p.video_codec = AV_CODEC_ID_FLV1,
1548 .init = flv_init,
1549 .write_header = flv_write_header,
1550 .write_packet = flv_write_packet,
1551 .write_trailer = flv_write_trailer,
1552 .deinit = flv_deinit,
1553 .check_bitstream= flv_check_bitstream,
1554 .p.codec_tag = (const AVCodecTag* const []) {
1555 flv_video_codec_ids, flv_audio_codec_ids, 0
1556 },
1557 .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
1558 AVFMT_TS_NONSTRICT,
1559 .p.priv_class = &flv_muxer_class,
1560 };
1561