]> git.sesse.net Git - ffmpeg/blob - libavformat/flacenc.c
mp3enc: write trailing padding
[ffmpeg] / libavformat / flacenc.c
1 /*
2  * raw FLAC muxer
3  * Copyright (c) 2006-2009 Justin Ruggles
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "libavcodec/flac.h"
25 #include "avformat.h"
26 #include "flacenc.h"
27 #include "vorbiscomment.h"
28 #include "libavcodec/bytestream.h"
29
30
31 typedef struct FlacMuxerContext {
32     const AVClass *class;
33     int write_header;
34
35     /* updated streaminfo sent by the encoder at the end */
36     uint8_t *streaminfo;
37 } FlacMuxerContext;
38
39 static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
40                                     int last_block)
41 {
42     avio_w8(pb, last_block ? 0x81 : 0x01);
43     avio_wb24(pb, n_padding_bytes);
44     while (n_padding_bytes > 0) {
45         avio_w8(pb, 0);
46         n_padding_bytes--;
47     }
48     return 0;
49 }
50
51 static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
52                                     int last_block, int bitexact)
53 {
54     const char *vendor = bitexact ? "Libav" : LIBAVFORMAT_IDENT;
55     unsigned int len;
56     uint8_t *p, *p0;
57
58     ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
59
60     len = ff_vorbiscomment_length(*m, vendor);
61     p0 = av_malloc(len+4);
62     if (!p0)
63         return AVERROR(ENOMEM);
64     p = p0;
65
66     bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
67     bytestream_put_be24(&p, len);
68     ff_vorbiscomment_write(&p, m, vendor);
69
70     avio_write(pb, p0, len+4);
71     av_freep(&p0);
72     p = NULL;
73
74     return 0;
75 }
76
77 static int flac_write_header(struct AVFormatContext *s)
78 {
79     int ret;
80     AVCodecParameters *par = s->streams[0]->codecpar;
81     FlacMuxerContext *c   = s->priv_data;
82
83     if (!c->write_header)
84         return 0;
85
86     ret = ff_flac_write_header(s->pb, par->extradata,
87                                par->extradata_size, 0);
88     if (ret)
89         return ret;
90
91     /* add the channel layout tag */
92     if (par->channel_layout &&
93         !(par->channel_layout & ~0x3ffffULL) &&
94         !ff_flac_is_native_layout(par->channel_layout)) {
95         AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
96                                                 NULL, 0);
97
98         if (chmask) {
99             av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
100                    "already present, this muxer will not overwrite it.\n");
101         } else {
102             uint8_t buf[32];
103             snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
104             av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
105         }
106     }
107
108     ret = flac_write_block_comment(s->pb, &s->metadata, 0,
109                                    s->flags & AVFMT_FLAG_BITEXACT);
110     if (ret)
111         return ret;
112
113     /* The command line flac encoder defaults to placing a seekpoint
114      * every 10s.  So one might add padding to allow that later
115      * but there seems to be no simple way to get the duration here.
116      * So let's try the flac default of 8192 bytes */
117     flac_write_block_padding(s->pb, 8192, 1);
118
119     return ret;
120 }
121
122 static int flac_write_trailer(struct AVFormatContext *s)
123 {
124     AVIOContext *pb = s->pb;
125     int64_t file_size;
126     FlacMuxerContext *c = s->priv_data;
127     uint8_t *streaminfo = c->streaminfo ? c->streaminfo :
128                                           s->streams[0]->codecpar->extradata;
129
130     if (!c->write_header || !streaminfo)
131         return 0;
132
133     if (pb->seekable) {
134         /* rewrite the STREAMINFO header block data */
135         file_size = avio_tell(pb);
136         avio_seek(pb, 8, SEEK_SET);
137         avio_write(pb, streaminfo, FLAC_STREAMINFO_SIZE);
138         avio_seek(pb, file_size, SEEK_SET);
139         avio_flush(pb);
140     } else {
141         av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
142     }
143
144     av_freep(&c->streaminfo);
145
146     return 0;
147 }
148
149 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
150 {
151     FlacMuxerContext *c = s->priv_data;
152     uint8_t *streaminfo;
153     int streaminfo_size;
154
155     /* check for updated streaminfo */
156     streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
157                                          &streaminfo_size);
158     if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
159         av_freep(&c->streaminfo);
160
161         c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
162         if (!c->streaminfo)
163             return AVERROR(ENOMEM);
164         memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
165     }
166
167     if (pkt->size)
168         avio_write(s->pb, pkt->data, pkt->size);
169     return 0;
170 }
171
172 static const AVOption flacenc_options[] = {
173     { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
174     { NULL },
175 };
176
177 static const AVClass flac_muxer_class = {
178     .class_name = "flac muxer",
179     .item_name  = av_default_item_name,
180     .option     = flacenc_options,
181     .version    = LIBAVUTIL_VERSION_INT,
182 };
183
184 AVOutputFormat ff_flac_muxer = {
185     .name              = "flac",
186     .long_name         = NULL_IF_CONFIG_SMALL("raw FLAC"),
187     .priv_data_size    = sizeof(FlacMuxerContext),
188     .mime_type         = "audio/x-flac",
189     .extensions        = "flac",
190     .audio_codec       = AV_CODEC_ID_FLAC,
191     .video_codec       = AV_CODEC_ID_NONE,
192     .write_header      = flac_write_header,
193     .write_packet      = flac_write_packet,
194     .write_trailer     = flac_write_trailer,
195     .flags             = AVFMT_NOTIMESTAMPS,
196     .priv_class        = &flac_muxer_class,
197 };