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 */
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;
67 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
69 len = ff_vorbiscomment_length(*m, vendor, NULL, 0);
70 if (len >= ((1<<24) - 4))
71 return AVERROR(EINVAL);
72 p0 = av_malloc(len+4);
74 return AVERROR(ENOMEM);
77 bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
78 bytestream_put_be24(&p, len);
79 ff_vorbiscomment_write(&p, m, vendor, NULL, 0);
81 avio_write(pb, p0, len+4);
88 static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
90 FlacMuxerContext *c = s->priv_data;
91 AVIOContext *pb = s->pb;
92 const AVPixFmtDescriptor *pixdesc;
93 const CodecMime *mime = ff_id3v2_mime_tags;
95 const char *mimetype = NULL, *desc = "";
96 const AVStream *st = s->streams[pkt->stream_index];
97 int i, mimelen, desclen, type = 0, blocklen;
102 while (mime->id != AV_CODEC_ID_NONE) {
103 if (mime->id == st->codecpar->codec_id) {
104 mimetype = mime->str;
110 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
111 "write an attached picture.\n", st->index);
112 return AVERROR(EINVAL);
114 mimelen = strlen(mimetype);
116 /* get the picture type */
117 e = av_dict_get(st->metadata, "comment", NULL, 0);
118 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
119 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
125 if ((c->attached_types & (1 << type)) & 0x6) {
126 av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
127 return AVERROR(EINVAL);
130 if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
131 st->codecpar->width != 32 ||
132 st->codecpar->height != 32)) {
133 av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
134 return AVERROR(EINVAL);
137 c->attached_types |= (1 << type);
139 /* get the description */
140 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
142 desclen = strlen(desc);
144 blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size;
145 if (blocklen >= 1<<24) {
146 av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24);
147 return AVERROR(EINVAL);
151 avio_wb24(pb, blocklen);
155 avio_wb32(pb, mimelen);
156 avio_write(pb, mimetype, mimelen);
158 avio_wb32(pb, desclen);
159 avio_write(pb, desc, desclen);
161 avio_wb32(pb, st->codecpar->width);
162 avio_wb32(pb, st->codecpar->height);
163 if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
164 avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
169 avio_wb32(pb, pkt->size);
170 avio_write(pb, pkt->data, pkt->size);
174 static int flac_finish_header(struct AVFormatContext *s)
176 int i, ret, padding = s->metadata_header_padding;
179 /* The FLAC specification states that 24 bits are used to represent the
180 * size of a metadata block so we must clip this value to 2^24-1. */
181 padding = av_clip_uintp2(padding, 24);
183 for (i = 0; i < s->nb_streams; i++) {
184 AVStream *st = s->streams[i];
185 AVPacket *pkt = st->priv_data;
188 ret = flac_write_picture(s, pkt);
189 av_packet_unref(pkt);
190 if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
194 ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
195 s->flags & AVFMT_FLAG_BITEXACT);
199 /* The command line flac encoder defaults to placing a seekpoint
200 * every 10s. So one might add padding to allow that later
201 * but there seems to be no simple way to get the duration here.
202 * So just add the amount requested by the user. */
204 flac_write_block_padding(s->pb, padding, 1);
209 static int flac_init(struct AVFormatContext *s)
211 AVCodecParameters *par;
212 FlacMuxerContext *c = s->priv_data;
215 c->audio_stream_idx = -1;
216 for (i = 0; i < s->nb_streams; i++) {
217 AVStream *st = s->streams[i];
218 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
219 if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) {
220 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC "
221 "audio stream is required.\n");
222 return AVERROR(EINVAL);
224 par = s->streams[i]->codecpar;
225 c->audio_stream_idx = i;
226 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
227 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
228 av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i);
230 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
231 av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n");
232 return AVERROR_PATCHWELCOME;
233 } else if (!c->write_header) {
234 av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n");
235 return AVERROR(EINVAL);
239 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
240 return AVERROR(EINVAL);
243 if (c->audio_stream_idx < 0) {
244 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
245 return AVERROR(EINVAL);
248 /* add the channel layout tag */
249 if (par->channel_layout &&
250 !(par->channel_layout & ~0x3ffffULL) &&
251 !ff_flac_is_native_layout(par->channel_layout)) {
252 AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
256 av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
257 "already present, this muxer will not overwrite it.\n");
260 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
261 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
268 static int flac_write_header(struct AVFormatContext *s)
270 FlacMuxerContext *c = s->priv_data;
271 AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
274 if (!c->write_header)
277 ret = ff_flac_write_header(s->pb, par->extradata,
278 par->extradata_size, 0);
282 if (!c->waiting_pics)
283 ret = flac_finish_header(s);
288 static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
290 FlacMuxerContext *c = s->priv_data;
294 /* check for updated streaminfo */
295 streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
297 if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
298 memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
299 c->updated_streaminfo = 1;
303 avio_write(s->pb, pkt->data, pkt->size);
307 static int flac_queue_flush(AVFormatContext *s)
309 FlacMuxerContext *c = s->priv_data;
313 ret = flac_finish_header(s);
318 ff_packet_list_get(&c->queue, &c->queue_end, &pkt);
319 if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
321 av_packet_unref(&pkt);
326 static int flac_write_trailer(struct AVFormatContext *s)
328 AVIOContext *pb = s->pb;
330 FlacMuxerContext *c = s->priv_data;
332 if (c->waiting_pics) {
333 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
334 "attached pictures.\n");
338 if (!c->write_header || !c->updated_streaminfo)
341 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
342 /* rewrite the STREAMINFO header block data */
343 file_size = avio_tell(pb);
344 avio_seek(pb, 8, SEEK_SET);
345 avio_write(pb, c->streaminfo, FLAC_STREAMINFO_SIZE);
346 avio_seek(pb, file_size, SEEK_SET);
348 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
354 static void flac_deinit(struct AVFormatContext *s)
356 FlacMuxerContext *c = s->priv_data;
358 ff_packet_list_free(&c->queue, &c->queue_end);
361 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
363 FlacMuxerContext *c = s->priv_data;
366 if (pkt->stream_index == c->audio_stream_idx) {
367 if (c->waiting_pics) {
368 /* buffer audio packets until we get all the pictures */
369 ret = ff_packet_list_put(&c->queue, &c->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET);
371 av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
373 ret = flac_queue_flush(s);
376 return flac_write_audio_packet(s, pkt);
379 return flac_write_audio_packet(s, pkt);
381 AVStream *st = s->streams[pkt->stream_index];
383 if (!c->waiting_pics ||
384 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
387 /* warn only once for each stream */
388 if (st->nb_frames == 1) {
389 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
390 " ignoring.\n", pkt->stream_index);
392 if (st->nb_frames >= 1)
395 st->priv_data = av_packet_clone(pkt);
397 av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
400 /* flush the buffered audio packets */
401 if (!c->waiting_pics &&
402 (ret = flac_queue_flush(s)) < 0)
409 static const AVOption flacenc_options[] = {
410 { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
414 static const AVClass flac_muxer_class = {
415 .class_name = "flac muxer",
416 .item_name = av_default_item_name,
417 .option = flacenc_options,
418 .version = LIBAVUTIL_VERSION_INT,
421 AVOutputFormat ff_flac_muxer = {
423 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
424 .priv_data_size = sizeof(FlacMuxerContext),
425 .mime_type = "audio/x-flac",
426 .extensions = "flac",
427 .audio_codec = AV_CODEC_ID_FLAC,
428 .video_codec = AV_CODEC_ID_PNG,
430 .write_header = flac_write_header,
431 .write_packet = flac_write_packet,
432 .write_trailer = flac_write_trailer,
433 .deinit = flac_deinit,
434 .flags = AVFMT_NOTIMESTAMPS,
435 .priv_class = &flac_muxer_class,