FFmpeg coverage


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