FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flacenc.c
Date: 2026-04-17 20:31:08
Exec Total Coverage
Lines: 164 213 77.0%
Functions: 11 11 100.0%
Branches: 77 132 58.3%

Line Branch Exec Source
1 /*
2 * raw FLAC muxer
3 * Copyright (c) 2006-2009 Justin Ruggles
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/avstring.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavcodec/flac.h"
27 #include "libavcodec/packet_internal.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "flacenc.h"
31 #include "id3v2.h"
32 #include "internal.h"
33 #include "mux.h"
34 #include "version.h"
35 #include "vorbiscomment.h"
36
37
38 typedef struct FlacMuxerContext {
39 const AVClass *class;
40 int write_header;
41
42 int audio_stream_idx;
43 int waiting_pics;
44 /* audio packets are queued here until we get all the attached pictures */
45 PacketList queue;
46
47 /* updated streaminfo sent by the encoder at the end */
48 uint8_t streaminfo[FLAC_STREAMINFO_SIZE];
49 int updated_streaminfo;
50
51 unsigned attached_types;
52 } FlacMuxerContext;
53
54 13 static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
55 int last_block)
56 {
57
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 avio_w8(pb, last_block ? 0x81 : 0x01);
58 13 avio_wb24(pb, n_padding_bytes);
59 13 ffio_fill(pb, 0, n_padding_bytes);
60 13 return 0;
61 }
62
63 13 static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
64 int last_block, int bitexact)
65 {
66
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
13 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
67 int len;
68
69 13 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
70
71 13 len = ff_vorbiscomment_length(*m, vendor, NULL, 0);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (len < 0)
73 return len;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (len >= ((1<<24) - 4))
75 return AVERROR(EINVAL);
76
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 avio_w8(pb, last_block ? 0x84 : 0x04);
78 13 avio_wb24(pb, len);
79 13 ff_vorbiscomment_write(pb, *m, vendor, NULL, 0);
80
81 13 return 0;
82 }
83
84 5 static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
85 {
86 5 FlacMuxerContext *c = s->priv_data;
87 5 AVIOContext *pb = s->pb;
88 const AVPixFmtDescriptor *pixdesc;
89 5 const CodecMime *mime = ff_id3v2_mime_tags;
90 AVDictionaryEntry *e;
91 5 const char *mimetype = NULL, *desc = "";
92 5 const AVStream *st = s->streams[pkt->stream_index];
93 5 int i, mimelen, desclen, type = 0, blocklen;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!pkt->data)
96 return 0;
97
98
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 while (mime->id != AV_CODEC_ID_NONE) {
99
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13 times.
18 if (mime->id == st->codecpar->codec_id) {
100 5 mimetype = mime->str;
101 5 break;
102 }
103 13 mime++;
104 }
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!mimetype) {
106 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
107 "write an attached picture.\n", st->index);
108 return AVERROR(EINVAL);
109 }
110 5 mimelen = strlen(mimetype);
111
112 /* get the picture type */
113 5 e = av_dict_get(st->metadata, "comment", NULL, 0);
114
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
75 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
115
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 70 times.
75 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
116 5 type = i;
117 5 break;
118 }
119 }
120
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if ((c->attached_types & (1 << type)) & 0x6) {
122 av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
123 return AVERROR(EINVAL);
124 }
125
126
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
127 st->codecpar->width != 32 ||
128 st->codecpar->height != 32)) {
129 av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
130 return AVERROR(EINVAL);
131 }
132
133 5 c->attached_types |= (1 << type);
134
135 /* get the description */
136
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
137 5 desc = e->value;
138 5 desclen = strlen(desc);
139
140 5 blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (blocklen >= 1<<24) {
142 av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24);
143 return AVERROR(EINVAL);
144 }
145
146 5 avio_w8(pb, 0x06);
147 5 avio_wb24(pb, blocklen);
148
149 5 avio_wb32(pb, type);
150
151 5 avio_wb32(pb, mimelen);
152 5 avio_write(pb, mimetype, mimelen);
153
154 5 avio_wb32(pb, desclen);
155 5 avio_write(pb, desc, desclen);
156
157 5 avio_wb32(pb, st->codecpar->width);
158 5 avio_wb32(pb, st->codecpar->height);
159
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
160 5 avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
161 else
162 avio_wb32(pb, 0);
163 5 avio_wb32(pb, 0);
164
165 5 avio_wb32(pb, pkt->size);
166 5 avio_write(pb, pkt->data, pkt->size);
167 5 return 0;
168 }
169
170 13 static int flac_finish_header(struct AVFormatContext *s)
171 {
172 13 int i, ret, padding = s->metadata_header_padding;
173
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (padding < 0)
174 13 padding = 8192;
175 /* The FLAC specification states that 24 bits are used to represent the
176 * size of a metadata block so we must clip this value to 2^24-1. */
177 13 padding = av_clip_uintp2(padding, 24);
178
179
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 for (i = 0; i < s->nb_streams; i++) {
180 18 AVStream *st = s->streams[i];
181 18 AVPacket *pkt = st->priv_data;
182
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 if (!pkt)
183 13 continue;
184 5 ret = flac_write_picture(s, pkt);
185 5 av_packet_unref(pkt);
186
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
187 return ret;
188 }
189
190 13 ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
191 13 s->flags & AVFMT_FLAG_BITEXACT);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret)
193 return ret;
194
195 /* The command line flac encoder defaults to placing a seekpoint
196 * every 10s. So one might add padding to allow that later
197 * but there seems to be no simple way to get the duration here.
198 * So just add the amount requested by the user. */
199
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (padding)
200 13 flac_write_block_padding(s->pb, padding, 1);
201
202 13 return 0;
203 }
204
205 13 static int flac_init(struct AVFormatContext *s)
206 {
207 AVCodecParameters *par;
208 13 FlacMuxerContext *c = s->priv_data;
209 int i;
210
211 13 c->audio_stream_idx = -1;
212
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 for (i = 0; i < s->nb_streams; i++) {
213 18 AVStream *st = s->streams[i];
214
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
215
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) {
216 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC "
217 "audio stream is required.\n");
218 return AVERROR(EINVAL);
219 }
220 13 par = s->streams[i]->codecpar;
221 13 c->audio_stream_idx = i;
222
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
224 av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i);
225 continue;
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
227 av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n");
228 return AVERROR_PATCHWELCOME;
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } else if (!c->write_header) {
230 av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n");
231 return AVERROR(EINVAL);
232 }
233 5 c->waiting_pics++;
234 } else {
235 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
236 return AVERROR(EINVAL);
237 }
238 }
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (c->audio_stream_idx < 0) {
240 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
241 return AVERROR(EINVAL);
242 }
243
244 /* add the channel layout tag */
245
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
246
3/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
26 !(par->ch_layout.u.mask & ~0x3ffffULL) &&
247 13 !ff_flac_is_native_layout(par->ch_layout.u.mask)) {
248 1 AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
249 NULL, 0);
250
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (chmask) {
252 av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
253 "already present, this muxer will not overwrite it.\n");
254 } else {
255 uint8_t buf[32];
256 1 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
257 1 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
258 }
259 }
260
261 13 return 0;
262 }
263
264 13 static int flac_write_header(struct AVFormatContext *s)
265 {
266 13 FlacMuxerContext *c = s->priv_data;
267 13 AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
268 int ret;
269
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!c->write_header)
271 return 0;
272
273 13 ret = ff_flac_write_header(s->pb, par->extradata,
274 par->extradata_size, 0);
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
276 return ret;
277
278
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 if (!c->waiting_pics)
279 12 ret = flac_finish_header(s);
280
281 13 return ret;
282 }
283
284 1263 static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
285 {
286 1263 FlacMuxerContext *c = s->priv_data;
287 uint8_t *streaminfo;
288 size_t streaminfo_size;
289
290 /* check for updated streaminfo */
291 1263 streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
292 &streaminfo_size);
293
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1250 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
1263 if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
294 13 memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
295 13 c->updated_streaminfo = 1;
296 }
297
298
2/2
✓ Branch 0 taken 1250 times.
✓ Branch 1 taken 13 times.
1263 if (pkt->size)
299 1250 avio_write(s->pb, pkt->data, pkt->size);
300 1263 return 0;
301 }
302
303 1 static int flac_queue_flush(AVFormatContext *s)
304 {
305 1 FlacMuxerContext *c = s->priv_data;
306 1 AVPacket *const pkt = ffformatcontext(s)->pkt;
307 1 int ret, write = 1;
308
309 1 ret = flac_finish_header(s);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
311 write = 0;
312
313
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 while (c->queue.head) {
314 1 avpriv_packet_list_get(&c->queue, pkt);
315
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (write && (ret = flac_write_audio_packet(s, pkt)) < 0)
316 write = 0;
317 1 av_packet_unref(pkt);
318 }
319 1 return ret;
320 }
321
322 13 static int flac_write_trailer(struct AVFormatContext *s)
323 {
324 13 AVIOContext *pb = s->pb;
325 int64_t file_size;
326 13 FlacMuxerContext *c = s->priv_data;
327
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (c->waiting_pics) {
329 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
330 "attached pictures.\n");
331 flac_queue_flush(s);
332 }
333
334
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (!c->write_header || !c->updated_streaminfo)
335 return 0;
336
337
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
338 /* rewrite the STREAMINFO header block data */
339 13 file_size = avio_tell(pb);
340 13 avio_seek(pb, 8, SEEK_SET);
341 13 avio_write(pb, c->streaminfo, FLAC_STREAMINFO_SIZE);
342 13 avio_seek(pb, file_size, SEEK_SET);
343 } else {
344 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
345 }
346
347 13 return 0;
348 }
349
350 13 static void flac_deinit(struct AVFormatContext *s)
351 {
352 13 FlacMuxerContext *c = s->priv_data;
353
354 13 avpriv_packet_list_free(&c->queue);
355
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 for (unsigned i = 0; i < s->nb_streams; i++)
356 18 av_packet_free((AVPacket **)&s->streams[i]->priv_data);
357 13 }
358
359 1268 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
360 {
361 1268 FlacMuxerContext *c = s->priv_data;
362 int ret;
363
364
2/2
✓ Branch 0 taken 1263 times.
✓ Branch 1 taken 5 times.
1268 if (pkt->stream_index == c->audio_stream_idx) {
365
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1262 times.
1263 if (c->waiting_pics) {
366 /* buffer audio packets until we get all the pictures */
367 1 ret = avpriv_packet_list_put(&c->queue, pkt, NULL, 0);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
369 av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
370 c->waiting_pics = 0;
371 ret = flac_queue_flush(s);
372 if (ret < 0)
373 return ret;
374 return flac_write_audio_packet(s, pkt);
375 }
376 } else
377 1262 return flac_write_audio_packet(s, pkt);
378 } else {
379 5 AVStream *st = s->streams[pkt->stream_index];
380
381
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!c->waiting_pics ||
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
383 return 0;
384
385 /* warn only once for each stream */
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (st->nb_frames == 1) {
387 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
388 " ignoring.\n", pkt->stream_index);
389 }
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (st->nb_frames >= 1)
391 return 0;
392
393 5 st->priv_data = av_packet_clone(pkt);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!st->priv_data)
395 av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
396 5 c->waiting_pics--;
397
398 /* flush the buffered audio packets */
399
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
6 if (!c->waiting_pics &&
400 1 (ret = flac_queue_flush(s)) < 0)
401 return ret;
402 }
403
404 6 return 0;
405 }
406
407 static const AVOption flacenc_options[] = {
408 { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
409 { NULL },
410 };
411
412 static const AVClass flac_muxer_class = {
413 .class_name = "flac muxer",
414 .item_name = av_default_item_name,
415 .option = flacenc_options,
416 .version = LIBAVUTIL_VERSION_INT,
417 };
418
419 const FFOutputFormat ff_flac_muxer = {
420 .p.name = "flac",
421 .p.long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
422 .priv_data_size = sizeof(FlacMuxerContext),
423 .p.mime_type = "audio/x-flac",
424 .p.extensions = "flac",
425 .p.audio_codec = AV_CODEC_ID_FLAC,
426 .p.video_codec = AV_CODEC_ID_PNG,
427 .init = flac_init,
428 .write_header = flac_write_header,
429 .write_packet = flac_write_packet,
430 .write_trailer = flac_write_trailer,
431 .deinit = flac_deinit,
432 .p.flags = AVFMT_NOTIMESTAMPS,
433 .p.priv_class = &flac_muxer_class,
434 };
435