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"
27 #include "avio_internal.h"
31 #include "vorbiscomment.h"
32 #include "libavcodec/bytestream.h"
35 typedef struct FlacMuxerContext {
41 /* audio packets are queued here until we get all the attached pictures */
42 AVPacketList *queue, *queue_end;
44 /* updated streaminfo sent by the encoder at the end */
47 unsigned attached_types;
50 static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
53 avio_w8(pb, last_block ? 0x81 : 0x01);
54 avio_wb24(pb, n_padding_bytes);
55 ffio_fill(pb, 0, n_padding_bytes);
59 static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
60 int last_block, int bitexact)
62 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);
71 p0 = av_malloc(len+4);
73 return AVERROR(ENOMEM);
76 bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
77 bytestream_put_be24(&p, len);
78 ff_vorbiscomment_write(&p, m, vendor, NULL, 0);
80 avio_write(pb, p0, len+4);
87 static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
89 FlacMuxerContext *c = s->priv_data;
90 AVIOContext *pb = s->pb;
91 const AVPixFmtDescriptor *pixdesc;
92 const CodecMime *mime = ff_id3v2_mime_tags;
94 const char *mimetype = NULL, *desc = "";
95 const AVStream *st = s->streams[pkt->stream_index];
96 int i, mimelen, desclen, type = 0;
101 while (mime->id != AV_CODEC_ID_NONE) {
102 if (mime->id == st->codecpar->codec_id) {
103 mimetype = mime->str;
109 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
110 "write an attached picture.\n", st->index);
111 return AVERROR(EINVAL);
113 mimelen = strlen(mimetype);
115 /* get the picture type */
116 e = av_dict_get(st->metadata, "comment", NULL, 0);
117 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
118 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
124 if ((c->attached_types & (1 << type)) & 0x6) {
125 av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
126 return AVERROR(EINVAL);
129 if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
130 st->codecpar->width != 32 ||
131 st->codecpar->height != 32)) {
132 av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
133 return AVERROR(EINVAL);
136 c->attached_types |= (1 << type);
138 /* get the description */
139 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
141 desclen = strlen(desc);
144 avio_wb24(pb, 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size);
148 avio_wb32(pb, mimelen);
149 avio_write(pb, mimetype, mimelen);
151 avio_wb32(pb, desclen);
152 avio_write(pb, desc, desclen);
154 avio_wb32(pb, st->codecpar->width);
155 avio_wb32(pb, st->codecpar->height);
156 if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
157 avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
162 avio_wb32(pb, pkt->size);
163 avio_write(pb, pkt->data, pkt->size);
167 static int flac_finish_header(struct AVFormatContext *s)
169 int i, ret, padding = s->metadata_header_padding;
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 padding = av_clip_uintp2(padding, 24);
176 for (i = 0; i < s->nb_streams; i++) {
177 AVStream *st = s->streams[i];
178 AVPacket *pkt = st->priv_data;
181 ret = flac_write_picture(s, pkt);
182 av_packet_unref(pkt);
183 if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
187 ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
188 s->flags & AVFMT_FLAG_BITEXACT);
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. */
197 flac_write_block_padding(s->pb, padding, 1);
202 static int flac_init(struct AVFormatContext *s)
204 AVCodecParameters *par;
205 FlacMuxerContext *c = s->priv_data;
208 c->audio_stream_idx = -1;
209 for (i = 0; i < s->nb_streams; i++) {
210 AVStream *st = s->streams[i];
211 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
212 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);
217 par = s->streams[i]->codecpar;
218 c->audio_stream_idx = i;
219 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
220 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);
223 } 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 } 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);
232 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
233 return AVERROR(EINVAL);
236 if (c->audio_stream_idx < 0) {
237 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
238 return AVERROR(EINVAL);
241 /* add the channel layout tag */
242 if (par->channel_layout &&
243 !(par->channel_layout & ~0x3ffffULL) &&
244 !ff_flac_is_native_layout(par->channel_layout)) {
245 AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
249 av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
250 "already present, this muxer will not overwrite it.\n");
253 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
254 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
261 static int flac_write_header(struct AVFormatContext *s)
263 FlacMuxerContext *c = s->priv_data;
264 AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
267 if (!c->write_header)
270 ret = ff_flac_write_header(s->pb, par->extradata,
271 par->extradata_size, 0);
275 if (!c->waiting_pics)
276 ret = flac_finish_header(s);
281 static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
283 FlacMuxerContext *c = s->priv_data;
287 /* check for updated streaminfo */
288 streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
290 if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
291 av_freep(&c->streaminfo);
293 c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
295 return AVERROR(ENOMEM);
296 memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
300 avio_write(s->pb, pkt->data, pkt->size);
304 static int flac_queue_flush(AVFormatContext *s)
306 FlacMuxerContext *c = s->priv_data;
310 ret = flac_finish_header(s);
315 ff_packet_list_get(&c->queue, &c->queue_end, &pkt);
316 if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
318 av_packet_unref(&pkt);
323 static int flac_write_trailer(struct AVFormatContext *s)
325 AVIOContext *pb = s->pb;
327 FlacMuxerContext *c = s->priv_data;
328 uint8_t *streaminfo = c->streaminfo ? c->streaminfo :
329 s->streams[c->audio_stream_idx]->codecpar->extradata;
331 if (c->waiting_pics) {
332 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
333 "attached pictures.\n");
337 if (!c->write_header || !streaminfo)
340 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
341 /* rewrite the STREAMINFO header block data */
342 file_size = avio_tell(pb);
343 avio_seek(pb, 8, SEEK_SET);
344 avio_write(pb, streaminfo, FLAC_STREAMINFO_SIZE);
345 avio_seek(pb, file_size, SEEK_SET);
348 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
351 av_freep(&c->streaminfo);
356 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
358 FlacMuxerContext *c = s->priv_data;
361 if (pkt->stream_index == c->audio_stream_idx) {
362 if (c->waiting_pics) {
363 /* buffer audio packets until we get all the pictures */
364 ret = ff_packet_list_put(&c->queue, &c->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET);
366 av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
368 ret = flac_queue_flush(s);
371 return flac_write_audio_packet(s, pkt);
374 return flac_write_audio_packet(s, pkt);
376 AVStream *st = s->streams[pkt->stream_index];
378 if (!c->waiting_pics ||
379 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
382 /* warn only once for each stream */
383 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);
387 if (st->nb_frames >= 1)
390 st->priv_data = av_packet_clone(pkt);
392 av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
395 /* flush the buffered audio packets */
396 if (!c->waiting_pics &&
397 (ret = flac_queue_flush(s)) < 0)
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 },
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,
416 AVOutputFormat ff_flac_muxer = {
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,
425 .write_header = flac_write_header,
426 .write_packet = flac_write_packet,
427 .write_trailer = flac_write_trailer,
428 .flags = AVFMT_NOTIMESTAMPS,
429 .priv_class = &flac_muxer_class,