3 * Copyright (c) 2006-2009 Justin Ruggles
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavcodec/flac.h"
26 #include "libavcodec/packet_internal.h"
28 #include "avio_internal.h"
32 #include "vorbiscomment.h"
35 typedef struct FlacMuxerContext {
41 /* audio packets are queued here until we get all the attached pictures */
42 PacketList *queue, *queue_end;
44 /* updated streaminfo sent by the encoder at the end */
45 uint8_t streaminfo[FLAC_STREAMINFO_SIZE];
46 int updated_streaminfo;
48 unsigned attached_types;
51 static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
54 avio_w8(pb, last_block ? 0x81 : 0x01);
55 avio_wb24(pb, n_padding_bytes);
56 ffio_fill(pb, 0, n_padding_bytes);
60 static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
61 int last_block, int bitexact)
63 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
66 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
68 len = ff_vorbiscomment_length(*m, vendor, NULL, 0);
69 if (len >= ((1<<24) - 4))
70 return AVERROR(EINVAL);
72 avio_w8(pb, last_block ? 0x84 : 0x04);
74 ff_vorbiscomment_write(pb, *m, vendor, NULL, 0);
79 static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
81 FlacMuxerContext *c = s->priv_data;
82 AVIOContext *pb = s->pb;
83 const AVPixFmtDescriptor *pixdesc;
84 const CodecMime *mime = ff_id3v2_mime_tags;
86 const char *mimetype = NULL, *desc = "";
87 const AVStream *st = s->streams[pkt->stream_index];
88 int i, mimelen, desclen, type = 0, blocklen;
93 while (mime->id != AV_CODEC_ID_NONE) {
94 if (mime->id == st->codecpar->codec_id) {
101 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
102 "write an attached picture.\n", st->index);
103 return AVERROR(EINVAL);
105 mimelen = strlen(mimetype);
107 /* get the picture type */
108 e = av_dict_get(st->metadata, "comment", NULL, 0);
109 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
110 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
116 if ((c->attached_types & (1 << type)) & 0x6) {
117 av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
118 return AVERROR(EINVAL);
121 if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
122 st->codecpar->width != 32 ||
123 st->codecpar->height != 32)) {
124 av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
125 return AVERROR(EINVAL);
128 c->attached_types |= (1 << type);
130 /* get the description */
131 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
133 desclen = strlen(desc);
135 blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size;
136 if (blocklen >= 1<<24) {
137 av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24);
138 return AVERROR(EINVAL);
142 avio_wb24(pb, blocklen);
146 avio_wb32(pb, mimelen);
147 avio_write(pb, mimetype, mimelen);
149 avio_wb32(pb, desclen);
150 avio_write(pb, desc, desclen);
152 avio_wb32(pb, st->codecpar->width);
153 avio_wb32(pb, st->codecpar->height);
154 if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
155 avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
160 avio_wb32(pb, pkt->size);
161 avio_write(pb, pkt->data, pkt->size);
165 static int flac_finish_header(struct AVFormatContext *s)
167 int i, ret, padding = s->metadata_header_padding;
170 /* The FLAC specification states that 24 bits are used to represent the
171 * size of a metadata block so we must clip this value to 2^24-1. */
172 padding = av_clip_uintp2(padding, 24);
174 for (i = 0; i < s->nb_streams; i++) {
175 AVStream *st = s->streams[i];
176 AVPacket *pkt = st->priv_data;
179 ret = flac_write_picture(s, pkt);
180 av_packet_unref(pkt);
181 if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
185 ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
186 s->flags & AVFMT_FLAG_BITEXACT);
190 /* The command line flac encoder defaults to placing a seekpoint
191 * every 10s. So one might add padding to allow that later
192 * but there seems to be no simple way to get the duration here.
193 * So just add the amount requested by the user. */
195 flac_write_block_padding(s->pb, padding, 1);
200 static int flac_init(struct AVFormatContext *s)
202 AVCodecParameters *par;
203 FlacMuxerContext *c = s->priv_data;
206 c->audio_stream_idx = -1;
207 for (i = 0; i < s->nb_streams; i++) {
208 AVStream *st = s->streams[i];
209 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
210 if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) {
211 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC "
212 "audio stream is required.\n");
213 return AVERROR(EINVAL);
215 par = s->streams[i]->codecpar;
216 c->audio_stream_idx = i;
217 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
218 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
219 av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i);
221 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
222 av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n");
223 return AVERROR_PATCHWELCOME;
224 } else if (!c->write_header) {
225 av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n");
226 return AVERROR(EINVAL);
230 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
231 return AVERROR(EINVAL);
234 if (c->audio_stream_idx < 0) {
235 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
236 return AVERROR(EINVAL);
239 /* add the channel layout tag */
240 if (par->channel_layout &&
241 !(par->channel_layout & ~0x3ffffULL) &&
242 !ff_flac_is_native_layout(par->channel_layout)) {
243 AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
247 av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
248 "already present, this muxer will not overwrite it.\n");
251 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
252 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
259 static int flac_write_header(struct AVFormatContext *s)
261 FlacMuxerContext *c = s->priv_data;
262 AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
265 if (!c->write_header)
268 ret = ff_flac_write_header(s->pb, par->extradata,
269 par->extradata_size, 0);
273 if (!c->waiting_pics)
274 ret = flac_finish_header(s);
279 static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
281 FlacMuxerContext *c = s->priv_data;
283 buffer_size_t streaminfo_size;
285 /* check for updated streaminfo */
286 streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
288 if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
289 memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
290 c->updated_streaminfo = 1;
294 avio_write(s->pb, pkt->data, pkt->size);
298 static int flac_queue_flush(AVFormatContext *s)
300 FlacMuxerContext *c = s->priv_data;
304 ret = flac_finish_header(s);
309 avpriv_packet_list_get(&c->queue, &c->queue_end, &pkt);
310 if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
312 av_packet_unref(&pkt);
317 static int flac_write_trailer(struct AVFormatContext *s)
319 AVIOContext *pb = s->pb;
321 FlacMuxerContext *c = s->priv_data;
323 if (c->waiting_pics) {
324 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
325 "attached pictures.\n");
329 if (!c->write_header || !c->updated_streaminfo)
332 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
333 /* rewrite the STREAMINFO header block data */
334 file_size = avio_tell(pb);
335 avio_seek(pb, 8, SEEK_SET);
336 avio_write(pb, c->streaminfo, FLAC_STREAMINFO_SIZE);
337 avio_seek(pb, file_size, SEEK_SET);
339 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
345 static void flac_deinit(struct AVFormatContext *s)
347 FlacMuxerContext *c = s->priv_data;
349 avpriv_packet_list_free(&c->queue, &c->queue_end);
350 for (unsigned i = 0; i < s->nb_streams; i++)
351 av_packet_free((AVPacket **)&s->streams[i]->priv_data);
354 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
356 FlacMuxerContext *c = s->priv_data;
359 if (pkt->stream_index == c->audio_stream_idx) {
360 if (c->waiting_pics) {
361 /* buffer audio packets until we get all the pictures */
362 ret = avpriv_packet_list_put(&c->queue, &c->queue_end, pkt, av_packet_ref, 0);
364 av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
366 ret = flac_queue_flush(s);
369 return flac_write_audio_packet(s, pkt);
372 return flac_write_audio_packet(s, pkt);
374 AVStream *st = s->streams[pkt->stream_index];
376 if (!c->waiting_pics ||
377 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
380 /* warn only once for each stream */
381 if (st->nb_frames == 1) {
382 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
383 " ignoring.\n", pkt->stream_index);
385 if (st->nb_frames >= 1)
388 st->priv_data = av_packet_clone(pkt);
390 av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
393 /* flush the buffered audio packets */
394 if (!c->waiting_pics &&
395 (ret = flac_queue_flush(s)) < 0)
402 static const AVOption flacenc_options[] = {
403 { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
407 static const AVClass flac_muxer_class = {
408 .class_name = "flac muxer",
409 .item_name = av_default_item_name,
410 .option = flacenc_options,
411 .version = LIBAVUTIL_VERSION_INT,
414 AVOutputFormat ff_flac_muxer = {
416 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
417 .priv_data_size = sizeof(FlacMuxerContext),
418 .mime_type = "audio/x-flac",
419 .extensions = "flac",
420 .audio_codec = AV_CODEC_ID_FLAC,
421 .video_codec = AV_CODEC_ID_PNG,
423 .write_header = flac_write_header,
424 .write_packet = flac_write_packet,
425 .write_trailer = flac_write_trailer,
426 .deinit = flac_deinit,
427 .flags = AVFMT_NOTIMESTAMPS,
428 .priv_class = &flac_muxer_class,