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"
34 typedef struct FlacMuxerContext {
40 /* audio packets are queued here until we get all the attached pictures */
41 AVPacketList *queue, *queue_end;
43 /* updated streaminfo sent by the encoder at the end */
44 uint8_t streaminfo[FLAC_STREAMINFO_SIZE];
45 int updated_streaminfo;
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;
65 ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
67 len = ff_vorbiscomment_length(*m, vendor, NULL, 0);
68 if (len >= ((1<<24) - 4))
69 return AVERROR(EINVAL);
71 avio_w8(pb, last_block ? 0x84 : 0x04);
73 ff_vorbiscomment_write(pb, *m, vendor, NULL, 0);
78 static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt)
80 FlacMuxerContext *c = s->priv_data;
81 AVIOContext *pb = s->pb;
82 const AVPixFmtDescriptor *pixdesc;
83 const CodecMime *mime = ff_id3v2_mime_tags;
85 const char *mimetype = NULL, *desc = "";
86 const AVStream *st = s->streams[pkt->stream_index];
87 int i, mimelen, desclen, type = 0, blocklen;
92 while (mime->id != AV_CODEC_ID_NONE) {
93 if (mime->id == st->codecpar->codec_id) {
100 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
101 "write an attached picture.\n", st->index);
102 return AVERROR(EINVAL);
104 mimelen = strlen(mimetype);
106 /* get the picture type */
107 e = av_dict_get(st->metadata, "comment", NULL, 0);
108 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
109 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
115 if ((c->attached_types & (1 << type)) & 0x6) {
116 av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]);
117 return AVERROR(EINVAL);
120 if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG ||
121 st->codecpar->width != 32 ||
122 st->codecpar->height != 32)) {
123 av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG");
124 return AVERROR(EINVAL);
127 c->attached_types |= (1 << type);
129 /* get the description */
130 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
132 desclen = strlen(desc);
134 blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size;
135 if (blocklen >= 1<<24) {
136 av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24);
137 return AVERROR(EINVAL);
141 avio_wb24(pb, blocklen);
145 avio_wb32(pb, mimelen);
146 avio_write(pb, mimetype, mimelen);
148 avio_wb32(pb, desclen);
149 avio_write(pb, desc, desclen);
151 avio_wb32(pb, st->codecpar->width);
152 avio_wb32(pb, st->codecpar->height);
153 if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
154 avio_wb32(pb, av_get_bits_per_pixel(pixdesc));
159 avio_wb32(pb, pkt->size);
160 avio_write(pb, pkt->data, pkt->size);
164 static int flac_finish_header(struct AVFormatContext *s)
166 int i, ret, padding = s->metadata_header_padding;
169 /* The FLAC specification states that 24 bits are used to represent the
170 * size of a metadata block so we must clip this value to 2^24-1. */
171 padding = av_clip_uintp2(padding, 24);
173 for (i = 0; i < s->nb_streams; i++) {
174 AVStream *st = s->streams[i];
175 AVPacket *pkt = st->priv_data;
178 ret = flac_write_picture(s, pkt);
179 av_packet_unref(pkt);
180 if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE))
184 ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
185 s->flags & AVFMT_FLAG_BITEXACT);
189 /* The command line flac encoder defaults to placing a seekpoint
190 * every 10s. So one might add padding to allow that later
191 * but there seems to be no simple way to get the duration here.
192 * So just add the amount requested by the user. */
194 flac_write_block_padding(s->pb, padding, 1);
199 static int flac_init(struct AVFormatContext *s)
201 AVCodecParameters *par;
202 FlacMuxerContext *c = s->priv_data;
205 c->audio_stream_idx = -1;
206 for (i = 0; i < s->nb_streams; i++) {
207 AVStream *st = s->streams[i];
208 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
209 if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) {
210 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC "
211 "audio stream is required.\n");
212 return AVERROR(EINVAL);
214 par = s->streams[i]->codecpar;
215 c->audio_stream_idx = i;
216 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
217 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
218 av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i);
220 } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
221 av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n");
222 return AVERROR_PATCHWELCOME;
223 } else if (!c->write_header) {
224 av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n");
225 return AVERROR(EINVAL);
229 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n");
230 return AVERROR(EINVAL);
233 if (c->audio_stream_idx < 0) {
234 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
235 return AVERROR(EINVAL);
238 /* add the channel layout tag */
239 if (par->channel_layout &&
240 !(par->channel_layout & ~0x3ffffULL) &&
241 !ff_flac_is_native_layout(par->channel_layout)) {
242 AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
246 av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
247 "already present, this muxer will not overwrite it.\n");
250 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
251 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
258 static int flac_write_header(struct AVFormatContext *s)
260 FlacMuxerContext *c = s->priv_data;
261 AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
264 if (!c->write_header)
267 ret = ff_flac_write_header(s->pb, par->extradata,
268 par->extradata_size, 0);
272 if (!c->waiting_pics)
273 ret = flac_finish_header(s);
278 static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt)
280 FlacMuxerContext *c = s->priv_data;
284 /* check for updated streaminfo */
285 streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
287 if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
288 memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
289 c->updated_streaminfo = 1;
293 avio_write(s->pb, pkt->data, pkt->size);
297 static int flac_queue_flush(AVFormatContext *s)
299 FlacMuxerContext *c = s->priv_data;
303 ret = flac_finish_header(s);
308 ff_packet_list_get(&c->queue, &c->queue_end, &pkt);
309 if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
311 av_packet_unref(&pkt);
316 static int flac_write_trailer(struct AVFormatContext *s)
318 AVIOContext *pb = s->pb;
320 FlacMuxerContext *c = s->priv_data;
322 if (c->waiting_pics) {
323 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
324 "attached pictures.\n");
328 if (!c->write_header || !c->updated_streaminfo)
331 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
332 /* rewrite the STREAMINFO header block data */
333 file_size = avio_tell(pb);
334 avio_seek(pb, 8, SEEK_SET);
335 avio_write(pb, c->streaminfo, FLAC_STREAMINFO_SIZE);
336 avio_seek(pb, file_size, SEEK_SET);
338 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
344 static void flac_deinit(struct AVFormatContext *s)
346 FlacMuxerContext *c = s->priv_data;
348 ff_packet_list_free(&c->queue, &c->queue_end);
351 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
353 FlacMuxerContext *c = s->priv_data;
356 if (pkt->stream_index == c->audio_stream_idx) {
357 if (c->waiting_pics) {
358 /* buffer audio packets until we get all the pictures */
359 ret = ff_packet_list_put(&c->queue, &c->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET);
361 av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
363 ret = flac_queue_flush(s);
366 return flac_write_audio_packet(s, pkt);
369 return flac_write_audio_packet(s, pkt);
371 AVStream *st = s->streams[pkt->stream_index];
373 if (!c->waiting_pics ||
374 !(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
377 /* warn only once for each stream */
378 if (st->nb_frames == 1) {
379 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
380 " ignoring.\n", pkt->stream_index);
382 if (st->nb_frames >= 1)
385 st->priv_data = av_packet_clone(pkt);
387 av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n");
390 /* flush the buffered audio packets */
391 if (!c->waiting_pics &&
392 (ret = flac_queue_flush(s)) < 0)
399 static const AVOption flacenc_options[] = {
400 { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
404 static const AVClass flac_muxer_class = {
405 .class_name = "flac muxer",
406 .item_name = av_default_item_name,
407 .option = flacenc_options,
408 .version = LIBAVUTIL_VERSION_INT,
411 AVOutputFormat ff_flac_muxer = {
413 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
414 .priv_data_size = sizeof(FlacMuxerContext),
415 .mime_type = "audio/x-flac",
416 .extensions = "flac",
417 .audio_codec = AV_CODEC_ID_FLAC,
418 .video_codec = AV_CODEC_ID_PNG,
420 .write_header = flac_write_header,
421 .write_packet = flac_write_packet,
422 .write_trailer = flac_write_trailer,
423 .deinit = flac_deinit,
424 .flags = AVFMT_NOTIMESTAMPS,
425 .priv_class = &flac_muxer_class,