FFmpeg coverage


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