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(AVFormatContext *s, AVIOContext *pb,
55 AVCodecParameters *par, int flags)
57 int bps, blkalign, bytespersec, frame_size;
59 int64_t hdrstart = avio_tell(pb);
60 int waveformatextensible;
62 uint8_t *riff_extradata = temp;
63 uint8_t *riff_extradata_start = temp;
65 if (!par->codec_tag || par->codec_tag > 0xffff)
68 if (par->codec_id == AV_CODEC_ID_ADPCM_SWF && par->block_align == 0) {
69 av_log(s, AV_LOG_ERROR, "%s can only be written to WAVE with a constant frame size\n",
70 avcodec_get_name(par->codec_id));
71 return AVERROR(EINVAL);
74 /* We use the known constant frame size for the codec if known, otherwise
75 * fall back on using AVCodecContext.frame_size, which is not as reliable
76 * for indicating packet duration. */
77 frame_size = av_get_audio_frame_duration2(par, par->block_align);
79 waveformatextensible = (par->channels > 2 && par->channel_layout) ||
80 par->channels == 1 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_MONO ||
81 par->channels == 2 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_STEREO ||
82 par->sample_rate > 48000 ||
83 par->codec_id == AV_CODEC_ID_EAC3 ||
84 av_get_bits_per_sample(par->codec_id) > 16;
86 if (waveformatextensible)
87 avio_wl16(pb, 0xfffe);
89 avio_wl16(pb, par->codec_tag);
91 avio_wl16(pb, par->channels);
92 avio_wl32(pb, par->sample_rate);
93 if (par->codec_id == AV_CODEC_ID_ATRAC3 ||
94 par->codec_id == AV_CODEC_ID_G723_1 ||
95 par->codec_id == AV_CODEC_ID_MP2 ||
96 par->codec_id == AV_CODEC_ID_MP3 ||
97 par->codec_id == AV_CODEC_ID_GSM_MS) {
100 if (!(bps = av_get_bits_per_sample(par->codec_id))) {
101 if (par->bits_per_coded_sample)
102 bps = par->bits_per_coded_sample;
104 bps = 16; // default to 16
107 if (bps != par->bits_per_coded_sample && par->bits_per_coded_sample) {
108 av_log(s, AV_LOG_WARNING,
109 "requested bits_per_coded_sample (%d) "
110 "and actually stored (%d) differ\n",
111 par->bits_per_coded_sample, bps);
114 if (par->codec_id == AV_CODEC_ID_MP2) {
115 blkalign = (144 * par->bit_rate - 1)/par->sample_rate + 1;
116 } else if (par->codec_id == AV_CODEC_ID_MP3) {
117 blkalign = 576 * (par->sample_rate <= (24000 + 32000)/2 ? 1 : 2);
118 } else if (par->codec_id == AV_CODEC_ID_AC3) {
119 blkalign = 3840; /* maximum bytes per frame */
120 } else if (par->codec_id == AV_CODEC_ID_AAC) {
121 blkalign = 768 * par->channels; /* maximum bytes per frame */
122 } else if (par->codec_id == AV_CODEC_ID_G723_1) {
124 } else if (par->block_align != 0) { /* specified by the codec */
125 blkalign = par->block_align;
127 blkalign = bps * par->channels / av_gcd(8, bps);
128 if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
129 par->codec_id == AV_CODEC_ID_PCM_S24LE ||
130 par->codec_id == AV_CODEC_ID_PCM_S32LE ||
131 par->codec_id == AV_CODEC_ID_PCM_F32LE ||
132 par->codec_id == AV_CODEC_ID_PCM_F64LE ||
133 par->codec_id == AV_CODEC_ID_PCM_S16LE) {
134 bytespersec = par->sample_rate * blkalign;
135 } else if (par->codec_id == AV_CODEC_ID_G723_1) {
138 bytespersec = par->bit_rate / 8;
140 avio_wl32(pb, bytespersec); /* bytes per second */
141 avio_wl16(pb, blkalign); /* block align */
142 avio_wl16(pb, bps); /* bits per sample */
143 if (par->codec_id == AV_CODEC_ID_MP3) {
144 bytestream_put_le16(&riff_extradata, 1); /* wID */
145 bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
146 bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
147 bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
148 bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
149 } else if (par->codec_id == AV_CODEC_ID_MP2) {
151 bytestream_put_le16(&riff_extradata, 2);
153 bytestream_put_le32(&riff_extradata, par->bit_rate);
155 bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
157 bytestream_put_le16(&riff_extradata, 0);
159 bytestream_put_le16(&riff_extradata, 1);
161 bytestream_put_le16(&riff_extradata, 16);
163 bytestream_put_le32(&riff_extradata, 0);
165 bytestream_put_le32(&riff_extradata, 0);
166 } else if (par->codec_id == AV_CODEC_ID_G723_1) {
167 bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
168 bytestream_put_le32(&riff_extradata, 0xaea2f732);
169 bytestream_put_le16(&riff_extradata, 0xacde);
170 } else if (par->codec_id == AV_CODEC_ID_GSM_MS ||
171 par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
172 /* wSamplesPerBlock */
173 bytestream_put_le16(&riff_extradata, frame_size);
174 } else if (par->extradata_size) {
175 riff_extradata_start = par->extradata;
176 riff_extradata = par->extradata + par->extradata_size;
178 /* write WAVEFORMATEXTENSIBLE extensions */
179 if (waveformatextensible) {
180 int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
181 (s->strict_std_compliance < FF_COMPLIANCE_NORMAL ||
182 par->channel_layout < 0x40000);
183 /* 22 is WAVEFORMATEXTENSIBLE size */
184 avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
185 /* ValidBitsPerSample || SamplesPerBlock || Reserved */
188 avio_wl32(pb, write_channel_mask ? par->channel_layout : 0);
190 if (par->codec_id == AV_CODEC_ID_EAC3) {
191 ff_put_guid(pb, ff_get_codec_guid(par->codec_id, ff_codec_wav_guids));
193 avio_wl32(pb, par->codec_tag);
194 avio_wl32(pb, 0x00100000);
195 avio_wl32(pb, 0xAA000080);
196 avio_wl32(pb, 0x719B3800);
198 } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
199 par->codec_tag != 0x0001 /* PCM */ ||
200 riff_extradata - riff_extradata_start) {
202 avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
203 } /* else PCMWAVEFORMAT */
204 avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
205 hdrsize = avio_tell(pb) - hdrstart;
214 /* BITMAPINFOHEADER header */
215 void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par,
216 int for_asf, int ignore_extradata, int rgb_frame_is_flipped)
218 int flipped_extradata = (par->extradata_size >= 9 &&
219 !memcmp(par->extradata + par->extradata_size - 9, "BottomUp", 9));
220 int keep_height = flipped_extradata || rgb_frame_is_flipped;
221 int extradata_size = par->extradata_size - 9*flipped_extradata;
222 enum AVPixelFormat pix_fmt = par->format;
225 if (pix_fmt == AV_PIX_FMT_NONE && par->bits_per_coded_sample == 1)
226 pix_fmt = AV_PIX_FMT_MONOWHITE;
227 pal_avi = !for_asf &&
228 (pix_fmt == AV_PIX_FMT_PAL8 ||
229 pix_fmt == AV_PIX_FMT_MONOWHITE ||
230 pix_fmt == AV_PIX_FMT_MONOBLACK);
232 /* Size (not including the size of the color table or color masks) */
233 avio_wl32(pb, 40 + (ignore_extradata || pal_avi ? 0 : extradata_size));
234 avio_wl32(pb, par->width);
235 //We always store RGB TopDown
236 avio_wl32(pb, par->codec_tag || keep_height ? par->height : -par->height);
240 avio_wl16(pb, par->bits_per_coded_sample ? par->bits_per_coded_sample : 24);
241 /* compression type */
242 avio_wl32(pb, par->codec_tag);
243 avio_wl32(pb, (par->width * par->height * (par->bits_per_coded_sample ? par->bits_per_coded_sample : 24)+7) / 8);
246 /* Number of color indices in the color table that are used.
247 * A value of 0 means 2^biBitCount indices, but this doesn't work
248 * with Windows Media Player and files containing xxpc chunks. */
249 avio_wl32(pb, pal_avi ? 1 << par->bits_per_coded_sample : 0);
252 if (!ignore_extradata) {
253 if (par->extradata_size) {
254 avio_write(pb, par->extradata, extradata_size);
255 if (!for_asf && extradata_size & 1)
257 } else if (pal_avi) {
259 for (i = 0; i < 1 << par->bits_per_coded_sample; i++) {
260 /* Initialize 1 bpp palette to black & white */
261 if (i == 0 && pix_fmt == AV_PIX_FMT_MONOWHITE)
262 avio_wl32(pb, 0xffffff);
263 else if (i == 1 && pix_fmt == AV_PIX_FMT_MONOBLACK)
264 avio_wl32(pb, 0xffffff);
272 void ff_parse_specific_params(AVStream *st, int *au_rate,
273 int *au_ssize, int *au_scale)
275 AVCodecParameters *par = st->codecpar;
277 int audio_frame_size;
279 audio_frame_size = av_get_audio_frame_duration2(par, 0);
280 if (!audio_frame_size)
281 audio_frame_size = par->frame_size;
283 *au_ssize = par->block_align;
284 if (audio_frame_size && par->sample_rate) {
285 *au_scale = audio_frame_size;
286 *au_rate = par->sample_rate;
287 } else if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
288 par->codec_type == AVMEDIA_TYPE_DATA ||
289 par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
290 *au_scale = st->time_base.num;
291 *au_rate = st->time_base.den;
293 *au_scale = par->block_align ? par->block_align * 8 : 8;
294 *au_rate = par->bit_rate ? par->bit_rate :
295 8 * par->sample_rate;
297 gcd = av_gcd(*au_scale, *au_rate);
302 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
304 size_t len = strlen(str);
305 if (len > 0 && len < UINT32_MAX) {
307 ffio_wfourcc(pb, tag);
309 avio_put_str(pb, str);
315 static const char riff_tags[][5] = {
316 "IARL", "IART", "IAS1", "IAS2", "IAS3", "IAS4", "IAS5", "IAS6", "IAS7",
317 "IAS8", "IAS9", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
318 "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
319 "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
323 static int riff_has_valid_tags(AVFormatContext *s)
327 for (i = 0; *riff_tags[i]; i++)
328 if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
334 void ff_riff_write_info(AVFormatContext *s)
336 AVIOContext *pb = s->pb;
339 AVDictionaryEntry *t = NULL;
341 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
343 /* writing empty LIST is not nice and may cause problems */
344 if (!riff_has_valid_tags(s))
347 list_pos = ff_start_tag(pb, "LIST");
348 ffio_wfourcc(pb, "INFO");
349 for (i = 0; *riff_tags[i]; i++)
350 if ((t = av_dict_get(s->metadata, riff_tags[i],
351 NULL, AV_DICT_MATCH_CASE)))
352 ff_riff_write_info_tag(s->pb, t->key, t->value);
353 ff_end_tag(pb, list_pos);
356 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
358 av_assert0(sizeof(*g) == 16);
359 avio_write(s, *g, sizeof(*g));
362 const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
365 for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
366 if (id == av_guid[i].id)
367 return &(av_guid[i].guid);