FFmpeg
riffenc.c
Go to the documentation of this file.
1 /*
2  * RIFF muxing functions
3  * Copyright (c) 2000 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
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"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "riff.h"
30 
31 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
32 {
33  ffio_wfourcc(pb, tag);
34  avio_wl32(pb, -1);
35  return avio_tell(pb);
36 }
37 
38 void ff_end_tag(AVIOContext *pb, int64_t start)
39 {
40  int64_t pos;
41 
42  av_assert0((start&1) == 0);
43 
44  pos = avio_tell(pb);
45  if (pos & 1)
46  avio_w8(pb, 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);
50 }
51 
52 /* WAVEFORMATEX header */
53 /* returns the size or -1 on error */
55  AVCodecParameters *par, int flags)
56 {
57  int bps, blkalign, bytespersec, frame_size;
58  int hdrsize;
59  int64_t hdrstart = avio_tell(pb);
60  int waveformatextensible;
61  uint8_t temp[256];
62  uint8_t *riff_extradata = temp;
63  uint8_t *riff_extradata_start = temp;
64 
65  if (!par->codec_tag || par->codec_tag > 0xffff)
66  return -1;
67 
68  /* We use the known constant frame size for the codec if known, otherwise
69  * fall back on using AVCodecContext.frame_size, which is not as reliable
70  * for indicating packet duration. */
71  frame_size = av_get_audio_frame_duration2(par, par->block_align);
72 
73  waveformatextensible = (par->channels > 2 && par->channel_layout) ||
74  par->channels == 1 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_MONO ||
75  par->channels == 2 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_STEREO ||
76  par->sample_rate > 48000 ||
77  par->codec_id == AV_CODEC_ID_EAC3 ||
79 
80  if (waveformatextensible)
81  avio_wl16(pb, 0xfffe);
82  else
83  avio_wl16(pb, par->codec_tag);
84 
85  avio_wl16(pb, par->channels);
86  avio_wl32(pb, par->sample_rate);
87  if (par->codec_id == AV_CODEC_ID_ATRAC3 ||
88  par->codec_id == AV_CODEC_ID_G723_1 ||
89  par->codec_id == AV_CODEC_ID_MP2 ||
90  par->codec_id == AV_CODEC_ID_MP3 ||
91  par->codec_id == AV_CODEC_ID_GSM_MS) {
92  bps = 0;
93  } else {
94  if (!(bps = av_get_bits_per_sample(par->codec_id))) {
95  if (par->bits_per_coded_sample)
96  bps = par->bits_per_coded_sample;
97  else
98  bps = 16; // default to 16
99  }
100  }
101  if (bps != par->bits_per_coded_sample && par->bits_per_coded_sample) {
103  "requested bits_per_coded_sample (%d) "
104  "and actually stored (%d) differ\n",
105  par->bits_per_coded_sample, bps);
106  }
107 
108  if (par->codec_id == AV_CODEC_ID_MP2) {
109  blkalign = (144 * par->bit_rate - 1)/par->sample_rate + 1;
110  } else if (par->codec_id == AV_CODEC_ID_MP3) {
111  blkalign = 576 * (par->sample_rate <= (24000 + 32000)/2 ? 1 : 2);
112  } else if (par->codec_id == AV_CODEC_ID_AC3) {
113  blkalign = 3840; /* maximum bytes per frame */
114  } else if (par->codec_id == AV_CODEC_ID_AAC) {
115  blkalign = 768 * par->channels; /* maximum bytes per frame */
116  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
117  blkalign = 24;
118  } else if (par->block_align != 0) { /* specified by the codec */
119  blkalign = par->block_align;
120  } else
121  blkalign = bps * par->channels / av_gcd(8, bps);
122  if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
128  bytespersec = par->sample_rate * blkalign;
129  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
130  bytespersec = 800;
131  } else {
132  bytespersec = par->bit_rate / 8;
133  }
134  avio_wl32(pb, bytespersec); /* bytes per second */
135  avio_wl16(pb, blkalign); /* block align */
136  avio_wl16(pb, bps); /* bits per sample */
137  if (par->codec_id == AV_CODEC_ID_MP3) {
138  bytestream_put_le16(&riff_extradata, 1); /* wID */
139  bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
140  bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
141  bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
142  bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
143  } else if (par->codec_id == AV_CODEC_ID_MP2) {
144  /* fwHeadLayer */
145  bytestream_put_le16(&riff_extradata, 2);
146  /* dwHeadBitrate */
147  bytestream_put_le32(&riff_extradata, par->bit_rate);
148  /* fwHeadMode */
149  bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
150  /* fwHeadModeExt */
151  bytestream_put_le16(&riff_extradata, 0);
152  /* wHeadEmphasis */
153  bytestream_put_le16(&riff_extradata, 1);
154  /* fwHeadFlags */
155  bytestream_put_le16(&riff_extradata, 16);
156  /* dwPTSLow */
157  bytestream_put_le32(&riff_extradata, 0);
158  /* dwPTSHigh */
159  bytestream_put_le32(&riff_extradata, 0);
160  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
161  bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
162  bytestream_put_le32(&riff_extradata, 0xaea2f732);
163  bytestream_put_le16(&riff_extradata, 0xacde);
164  } else if (par->codec_id == AV_CODEC_ID_GSM_MS ||
166  /* wSamplesPerBlock */
167  bytestream_put_le16(&riff_extradata, frame_size);
168  } else if (par->extradata_size) {
169  riff_extradata_start = par->extradata;
170  riff_extradata = par->extradata + par->extradata_size;
171  }
172  /* write WAVEFORMATEXTENSIBLE extensions */
173  if (waveformatextensible) {
174  int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
176  par->channel_layout < 0x40000);
177  /* 22 is WAVEFORMATEXTENSIBLE size */
178  avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
179  /* ValidBitsPerSample || SamplesPerBlock || Reserved */
180  avio_wl16(pb, bps);
181  /* dwChannelMask */
182  avio_wl32(pb, write_channel_mask ? par->channel_layout : 0);
183  /* GUID + next 3 */
184  if (par->codec_id == AV_CODEC_ID_EAC3) {
186  } else {
187  avio_wl32(pb, par->codec_tag);
188  avio_wl32(pb, 0x00100000);
189  avio_wl32(pb, 0xAA000080);
190  avio_wl32(pb, 0x719B3800);
191  }
192  } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
193  par->codec_tag != 0x0001 /* PCM */ ||
194  riff_extradata - riff_extradata_start) {
195  /* WAVEFORMATEX */
196  avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
197  } /* else PCMWAVEFORMAT */
198  avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
199  hdrsize = avio_tell(pb) - hdrstart;
200  if (hdrsize & 1) {
201  hdrsize++;
202  avio_w8(pb, 0);
203  }
204 
205  return hdrsize;
206 }
207 
208 /* BITMAPINFOHEADER header */
210  int for_asf, int ignore_extradata, int rgb_frame_is_flipped)
211 {
212  int flipped_extradata = (par->extradata_size >= 9 &&
213  !memcmp(par->extradata + par->extradata_size - 9, "BottomUp", 9));
214  int keep_height = flipped_extradata || rgb_frame_is_flipped;
215  int extradata_size = par->extradata_size - 9*flipped_extradata;
216  enum AVPixelFormat pix_fmt = par->format;
217  int pal_avi;
218 
219  if (pix_fmt == AV_PIX_FMT_NONE && par->bits_per_coded_sample == 1)
220  pix_fmt = AV_PIX_FMT_MONOWHITE;
221  pal_avi = !for_asf &&
222  (pix_fmt == AV_PIX_FMT_PAL8 ||
223  pix_fmt == AV_PIX_FMT_MONOWHITE ||
224  pix_fmt == AV_PIX_FMT_MONOBLACK);
225 
226  /* Size (not including the size of the color table or color masks) */
227  avio_wl32(pb, 40 + (ignore_extradata || pal_avi ? 0 : extradata_size));
228  avio_wl32(pb, par->width);
229  //We always store RGB TopDown
230  avio_wl32(pb, par->codec_tag || keep_height ? par->height : -par->height);
231  /* planes */
232  avio_wl16(pb, 1);
233  /* depth */
235  /* compression type */
236  avio_wl32(pb, par->codec_tag);
237  avio_wl32(pb, (par->width * par->height * (par->bits_per_coded_sample ? par->bits_per_coded_sample : 24)+7) / 8);
238  avio_wl32(pb, 0);
239  avio_wl32(pb, 0);
240  /* Number of color indices in the color table that are used.
241  * A value of 0 means 2^biBitCount indices, but this doesn't work
242  * with Windows Media Player and files containing xxpc chunks. */
243  avio_wl32(pb, pal_avi ? 1 << par->bits_per_coded_sample : 0);
244  avio_wl32(pb, 0);
245 
246  if (!ignore_extradata) {
247  if (par->extradata_size) {
248  avio_write(pb, par->extradata, extradata_size);
249  if (!for_asf && extradata_size & 1)
250  avio_w8(pb, 0);
251  } else if (pal_avi) {
252  int i;
253  for (i = 0; i < 1 << par->bits_per_coded_sample; i++) {
254  /* Initialize 1 bpp palette to black & white */
255  if (i == 0 && pix_fmt == AV_PIX_FMT_MONOWHITE)
256  avio_wl32(pb, 0xffffff);
257  else if (i == 1 && pix_fmt == AV_PIX_FMT_MONOBLACK)
258  avio_wl32(pb, 0xffffff);
259  else
260  avio_wl32(pb, 0);
261  }
262  }
263  }
264 }
265 
266 void ff_parse_specific_params(AVStream *st, int *au_rate,
267  int *au_ssize, int *au_scale)
268 {
269  AVCodecParameters *par = st->codecpar;
270  int gcd;
271  int audio_frame_size;
272 
273  audio_frame_size = av_get_audio_frame_duration2(par, 0);
274  if (!audio_frame_size)
275  audio_frame_size = par->frame_size;
276 
277  *au_ssize = par->block_align;
278  if (audio_frame_size && par->sample_rate) {
279  *au_scale = audio_frame_size;
280  *au_rate = par->sample_rate;
281  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
282  par->codec_type == AVMEDIA_TYPE_DATA ||
284  *au_scale = st->time_base.num;
285  *au_rate = st->time_base.den;
286  } else {
287  *au_scale = par->block_align ? par->block_align * 8 : 8;
288  *au_rate = par->bit_rate ? par->bit_rate :
289  8 * par->sample_rate;
290  }
291  gcd = av_gcd(*au_scale, *au_rate);
292  *au_scale /= gcd;
293  *au_rate /= gcd;
294 }
295 
296 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
297 {
298  size_t len = strlen(str);
299  if (len > 0 && len < UINT32_MAX) {
300  len++;
301  ffio_wfourcc(pb, tag);
302  avio_wl32(pb, len);
303  avio_put_str(pb, str);
304  if (len & 1)
305  avio_w8(pb, 0);
306  }
307 }
308 
309 static const char riff_tags[][5] = {
310  "IARL", "IART", "IAS1", "IAS2", "IAS3", "IAS4", "IAS5", "IAS6", "IAS7",
311  "IAS8", "IAS9", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
312  "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
313  "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
314  { 0 }
315 };
316 
318 {
319  int i;
320 
321  for (i = 0; *riff_tags[i]; i++)
323  return 1;
324 
325  return 0;
326 }
327 
329 {
330  AVIOContext *pb = s->pb;
331  int i;
332  int64_t list_pos;
333  AVDictionaryEntry *t = NULL;
334 
336 
337  /* writing empty LIST is not nice and may cause problems */
338  if (!riff_has_valid_tags(s))
339  return;
340 
341  list_pos = ff_start_tag(pb, "LIST");
342  ffio_wfourcc(pb, "INFO");
343  for (i = 0; *riff_tags[i]; i++)
344  if ((t = av_dict_get(s->metadata, riff_tags[i],
346  ff_riff_write_info_tag(s->pb, t->key, t->value);
347  ff_end_tag(pb, list_pos);
348 }
349 
351 {
352  av_assert0(sizeof(*g) == 16);
353  avio_write(s, *g, sizeof(*g));
354 }
355 
356 const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
357 {
358  int i;
359  for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
360  if (id == av_guid[i].id)
361  return &(av_guid[i].guid);
362  }
363  return NULL;
364 }
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:459
Bytestream IO Context.
Definition: avio.h:161
static enum AVPixelFormat pix_fmt
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
else temp
Definition: vf_mcdeint.c:256
const char * g
Definition: vf_curves.c:115
enum AVCodecID id
Definition: riff.h:95
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
ff_asf_guid guid
Definition: riff.h:96
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define AV_CH_LAYOUT_STEREO
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1659
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
void ff_parse_specific_params(AVStream *st, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:266
int frame_size
Audio only.
Definition: codec_par.h:181
const AVCodecGuid ff_codec_wav_guids[]
Definition: riff.c:619
Format I/O context.
Definition: avformat.h:1351
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
Definition: riffenc.c:350
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:379
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: codec_par.h:126
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint32_t tag
Definition: movenc.c:1596
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
#define FFALIGN(x, a)
Definition: macros.h:48
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
#define av_log(a,...)
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:328
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
const ff_asf_guid * ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
Definition: riffenc.c:356
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:69
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1566
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, int for_asf, int ignore_extradata, int rgb_frame_is_flipped)
Definition: riffenc.c:209
unsigned int pos
Definition: spdifenc.c:410
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:416
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:1783
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int block_align
Audio only.
Definition: codec_par.h:177
void ff_end_tag(AVIOContext *pb, int64_t start)
Definition: riffenc.c:38
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
Write a single RIFF info tag.
Definition: riffenc.c:296
#define s(width, name)
Definition: cbs_vp9.c:257
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:395
uint8_t ff_asf_guid[16]
Definition: riff.h:92
Stream structure.
Definition: avformat.h:876
int frame_size
Definition: mxfenc.c:2166
Libavcodec external API header.
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
Definition: riffenc.c:31
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
static const char riff_tags[][5]
Definition: riffenc.c:309
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:203
static int riff_has_valid_tags(AVFormatContext *s)
Definition: riffenc.c:317
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1592
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:592
#define flags(name, subs,...)
Definition: cbs_av1.c:560
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
int sample_rate
Audio only.
Definition: codec_par.h:170
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
Main libavformat public API header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
unsigned bps
Definition: movenc.c:1597
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
char * value
Definition: dict.h:87
int len
#define FF_PUT_WAV_HEADER_SKIP_CHANNELMASK
Tell ff_put_wav_header() to write an empty channel mask.
Definition: riff.h:59
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int channels
Audio only.
Definition: codec_par.h:166
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
#define AV_CH_LAYOUT_MONO
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
int i
Definition: input.c:407
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:54