FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 554 756 73.3%
Functions: 18 20 90.0%
Branches: 271 446 60.8%

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