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 "libavcodec/flac.h"
26 #include "vorbiscomment.h"
27 #include "libavcodec/bytestream.h"
29 int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec,
33 0x66, 0x4C, 0x61, 0x43, 0x00, 0x00, 0x00, 0x22
36 enum FLACExtradataFormat format;
38 header[4] = last_block ? 0x80 : 0x00;
39 if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo))
42 /* write "fLaC" stream marker and first metadata block header if needed */
43 if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) {
44 put_buffer(pb, header, 8);
47 /* write STREAMINFO or full header */
48 put_buffer(pb, codec->extradata, codec->extradata_size);
53 static int flac_write_block_padding(ByteIOContext *pb, unsigned int n_padding_bytes,
56 put_byte(pb, last_block ? 0x81 : 0x01);
57 put_be24(pb, n_padding_bytes);
58 while (n_padding_bytes > 0) {
65 static int flac_write_block_comment(ByteIOContext *pb, AVMetadata *m,
66 int last_block, int bitexact)
68 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
69 unsigned int len, count;
72 len = ff_vorbiscomment_length(m, vendor, &count);
73 p0 = av_malloc(len+4);
75 return AVERROR(ENOMEM);
78 bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
79 bytestream_put_be24(&p, len);
80 ff_vorbiscomment_write(&p, m, vendor, count);
82 put_buffer(pb, p0, len+4);
89 static int flac_write_header(struct AVFormatContext *s)
92 AVCodecContext *codec = s->streams[0]->codec;
94 ret = ff_flac_write_header(s->pb, codec, 0);
98 ret = flac_write_block_comment(s->pb, s->metadata, 0,
99 codec->flags & CODEC_FLAG_BITEXACT);
103 /* The command line flac encoder defaults to placing a seekpoint
104 * every 10s. So one might add padding to allow that later
105 * but there seems to be no simple way to get the duration here.
106 * So let's try the flac default of 8192 bytes */
107 flac_write_block_padding(s->pb, 8192, 1);
112 static int flac_write_trailer(struct AVFormatContext *s)
114 ByteIOContext *pb = s->pb;
116 enum FLACExtradataFormat format;
119 if (!ff_flac_is_extradata_valid(s->streams[0]->codec, &format, &streaminfo))
122 if (!url_is_streamed(pb)) {
123 /* rewrite the STREAMINFO header block data */
124 file_size = url_ftell(pb);
125 url_fseek(pb, 8, SEEK_SET);
126 put_buffer(pb, streaminfo, FLAC_STREAMINFO_SIZE);
127 url_fseek(pb, file_size, SEEK_SET);
128 put_flush_packet(pb);
130 av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
135 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
137 put_buffer(s->pb, pkt->data, pkt->size);
138 put_flush_packet(s->pb);
142 AVOutputFormat flac_muxer = {
144 NULL_IF_CONFIG_SMALL("raw FLAC"),
153 .flags= AVFMT_NOTIMESTAMPS,
154 .metadata_conv = ff_vorbiscomment_metadata_conv,