FFmpeg coverage


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