FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flacenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 163 211 77.3%
Functions: 11 11 100.0%
Branches: 76 130 58.5%

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