FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvenc.c
Date: 2023-12-07 21:54:23
Exec Total Coverage
Lines: 462 664 69.6%
Functions: 17 19 89.5%
Branches: 237 402 59.0%

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