2 * RIFF muxing functions
3 * Copyright (c) 2000 Fabrice Bellard
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/dict.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mathematics.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/bytestream.h"
28 #include "avio_internal.h"
31 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
33 ffio_wfourcc(pb, tag);
38 void ff_end_tag(AVIOContext *pb, int64_t start)
42 av_assert0((start&1) == 0);
47 avio_seek(pb, start - 4, SEEK_SET);
48 avio_wl32(pb, (uint32_t)(pos - start));
49 avio_seek(pb, FFALIGN(pos, 2), SEEK_SET);
52 /* WAVEFORMATEX header */
53 /* returns the size or -1 on error */
54 int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc, int flags)
56 int bps, blkalign, bytespersec, frame_size;
58 int64_t hdrstart = avio_tell(pb);
59 int waveformatextensible;
61 uint8_t *riff_extradata = temp;
62 uint8_t *riff_extradata_start = temp;
64 if (!enc->codec_tag || enc->codec_tag > 0xffff)
67 /* We use the known constant frame size for the codec if known, otherwise
68 * fall back on using AVCodecContext.frame_size, which is not as reliable
69 * for indicating packet duration. */
70 frame_size = av_get_audio_frame_duration(enc, enc->block_align);
72 waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
73 enc->sample_rate > 48000 ||
74 enc->codec_id == AV_CODEC_ID_EAC3 ||
75 av_get_bits_per_sample(enc->codec_id) > 16;
77 if (waveformatextensible)
78 avio_wl16(pb, 0xfffe);
80 avio_wl16(pb, enc->codec_tag);
82 avio_wl16(pb, enc->channels);
83 avio_wl32(pb, enc->sample_rate);
84 if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
85 enc->codec_id == AV_CODEC_ID_G723_1 ||
86 enc->codec_id == AV_CODEC_ID_MP2 ||
87 enc->codec_id == AV_CODEC_ID_MP3 ||
88 enc->codec_id == AV_CODEC_ID_GSM_MS) {
91 if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
92 if (enc->bits_per_coded_sample)
93 bps = enc->bits_per_coded_sample;
95 bps = 16; // default to 16
98 if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
99 av_log(enc, AV_LOG_WARNING,
100 "requested bits_per_coded_sample (%d) "
101 "and actually stored (%d) differ\n",
102 enc->bits_per_coded_sample, bps);
105 if (enc->codec_id == AV_CODEC_ID_MP2) {
106 blkalign = (144 * enc->bit_rate - 1)/enc->sample_rate + 1;
107 } else if (enc->codec_id == AV_CODEC_ID_MP3) {
108 blkalign = 576 * (enc->sample_rate <= (24000 + 32000)/2 ? 1 : 2);
109 } else if (enc->codec_id == AV_CODEC_ID_AC3) {
110 blkalign = 3840; /* maximum bytes per frame */
111 } else if (enc->codec_id == AV_CODEC_ID_AAC) {
112 blkalign = 768 * enc->channels; /* maximum bytes per frame */
113 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
115 } else if (enc->block_align != 0) { /* specified by the codec */
116 blkalign = enc->block_align;
118 blkalign = bps * enc->channels / av_gcd(8, bps);
119 if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
120 enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
121 enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
122 enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
123 enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
124 enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
125 bytespersec = enc->sample_rate * blkalign;
126 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
129 bytespersec = enc->bit_rate / 8;
131 avio_wl32(pb, bytespersec); /* bytes per second */
132 avio_wl16(pb, blkalign); /* block align */
133 avio_wl16(pb, bps); /* bits per sample */
134 if (enc->codec_id == AV_CODEC_ID_MP3) {
135 bytestream_put_le16(&riff_extradata, 1); /* wID */
136 bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
137 bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
138 bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
139 bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
140 } else if (enc->codec_id == AV_CODEC_ID_MP2) {
142 bytestream_put_le16(&riff_extradata, 2);
144 bytestream_put_le32(&riff_extradata, enc->bit_rate);
146 bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
148 bytestream_put_le16(&riff_extradata, 0);
150 bytestream_put_le16(&riff_extradata, 1);
152 bytestream_put_le16(&riff_extradata, 16);
154 bytestream_put_le32(&riff_extradata, 0);
156 bytestream_put_le32(&riff_extradata, 0);
157 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
158 bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
159 bytestream_put_le32(&riff_extradata, 0xaea2f732);
160 bytestream_put_le16(&riff_extradata, 0xacde);
161 } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
162 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
163 /* wSamplesPerBlock */
164 bytestream_put_le16(&riff_extradata, frame_size);
165 } else if (enc->extradata_size) {
166 riff_extradata_start = enc->extradata;
167 riff_extradata = enc->extradata + enc->extradata_size;
169 /* write WAVEFORMATEXTENSIBLE extensions */
170 if (waveformatextensible) {
171 int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
172 (enc->strict_std_compliance < FF_COMPLIANCE_NORMAL ||
173 enc->channel_layout < 0x40000);
174 /* 22 is WAVEFORMATEXTENSIBLE size */
175 avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
176 /* ValidBitsPerSample || SamplesPerBlock || Reserved */
179 avio_wl32(pb, write_channel_mask ? enc->channel_layout : 0);
181 if (enc->codec_id == AV_CODEC_ID_EAC3) {
182 ff_put_guid(pb, ff_get_codec_guid(enc->codec_id, ff_codec_wav_guids));
184 avio_wl32(pb, enc->codec_tag);
185 avio_wl32(pb, 0x00100000);
186 avio_wl32(pb, 0xAA000080);
187 avio_wl32(pb, 0x719B3800);
189 } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
190 enc->codec_tag != 0x0001 /* PCM */ ||
191 riff_extradata - riff_extradata_start) {
193 avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
194 } /* else PCMWAVEFORMAT */
195 avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
196 hdrsize = avio_tell(pb) - hdrstart;
205 /* BITMAPINFOHEADER header */
206 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
207 const AVCodecTag *tags, int for_asf, int ignore_extradata)
209 int keep_height = enc->extradata_size >= 9 &&
210 !memcmp(enc->extradata + enc->extradata_size - 9, "BottomUp", 9);
211 int extradata_size = enc->extradata_size - 9*keep_height;
214 raw_pal_avi = !for_asf && enc->codec_id == AV_CODEC_ID_RAWVIDEO &&
216 enc->bits_per_coded_sample >= 1 && enc->bits_per_coded_sample <= 8;
217 if (!enc->extradata_size && raw_pal_avi)
218 extradata_size = 4 * (1 << enc->bits_per_coded_sample);
221 avio_wl32(pb, 40 + (ignore_extradata ? 0 :extradata_size));
222 avio_wl32(pb, enc->width);
223 //We always store RGB TopDown
224 avio_wl32(pb, enc->codec_tag || keep_height ? enc->height : -enc->height);
228 avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
229 /* compression type */
230 avio_wl32(pb, enc->codec_tag);
231 avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
237 if (!ignore_extradata) {
238 if (enc->extradata_size) {
239 avio_write(pb, enc->extradata, extradata_size);
240 if (!for_asf && extradata_size & 1)
242 } else if (raw_pal_avi) {
244 enum AVPixelFormat pix_fmt = enc->pix_fmt;
245 if (pix_fmt == AV_PIX_FMT_NONE && enc->bits_per_coded_sample == 1)
246 pix_fmt = AV_PIX_FMT_MONOWHITE;
247 for (i = 0; i < 1 << enc->bits_per_coded_sample; i++) {
248 /* Initialize 1 bpp palette to black & white */
249 if (i == 0 && pix_fmt == AV_PIX_FMT_MONOWHITE)
250 avio_wl32(pb, 0xffffff);
251 else if (i == 1 && pix_fmt == AV_PIX_FMT_MONOBLACK)
252 avio_wl32(pb, 0xffffff);
260 void ff_parse_specific_params(AVStream *st, int *au_rate,
261 int *au_ssize, int *au_scale)
263 AVCodecContext *codec = st->codec;
265 int audio_frame_size;
267 /* We use the known constant frame size for the codec if known, otherwise
268 * fall back on using AVCodecContext.frame_size, which is not as reliable
269 * for indicating packet duration. */
270 audio_frame_size = av_get_audio_frame_duration(codec, 0);
271 if (!audio_frame_size)
272 audio_frame_size = codec->frame_size;
274 *au_ssize = codec->block_align;
275 if (audio_frame_size && codec->sample_rate) {
276 *au_scale = audio_frame_size;
277 *au_rate = codec->sample_rate;
278 } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
279 codec->codec_type == AVMEDIA_TYPE_DATA ||
280 codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
281 *au_scale = st->time_base.num;
282 *au_rate = st->time_base.den;
284 *au_scale = codec->block_align ? codec->block_align * 8 : 8;
285 *au_rate = codec->bit_rate ? codec->bit_rate :
286 8 * codec->sample_rate;
288 gcd = av_gcd(*au_scale, *au_rate);
293 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
295 size_t len = strlen(str);
296 if (len > 0 && len < UINT32_MAX) {
298 ffio_wfourcc(pb, tag);
300 avio_put_str(pb, str);
306 static const char riff_tags[][5] = {
307 "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
308 "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
309 "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
313 static int riff_has_valid_tags(AVFormatContext *s)
317 for (i = 0; *riff_tags[i]; i++)
318 if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
324 void ff_riff_write_info(AVFormatContext *s)
326 AVIOContext *pb = s->pb;
329 AVDictionaryEntry *t = NULL;
331 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
333 /* writing empty LIST is not nice and may cause problems */
334 if (!riff_has_valid_tags(s))
337 list_pos = ff_start_tag(pb, "LIST");
338 ffio_wfourcc(pb, "INFO");
339 for (i = 0; *riff_tags[i]; i++)
340 if ((t = av_dict_get(s->metadata, riff_tags[i],
341 NULL, AV_DICT_MATCH_CASE)))
342 ff_riff_write_info_tag(s->pb, t->key, t->value);
343 ff_end_tag(pb, list_pos);
346 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
348 av_assert0(sizeof(*g) == 16);
349 avio_write(s, *g, sizeof(*g));
352 const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
355 for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
356 if (id == av_guid[i].id)
357 return &(av_guid[i].guid);