FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvenc.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 405 625 64.8%
Branches: 153 330 46.4%

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