FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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->sample_rate > 48000 ||
75  par->codec_id == AV_CODEC_ID_EAC3 ||
77 
78  if (waveformatextensible)
79  avio_wl16(pb, 0xfffe);
80  else
81  avio_wl16(pb, par->codec_tag);
82 
83  avio_wl16(pb, par->channels);
84  avio_wl32(pb, par->sample_rate);
85  if (par->codec_id == AV_CODEC_ID_ATRAC3 ||
86  par->codec_id == AV_CODEC_ID_G723_1 ||
87  par->codec_id == AV_CODEC_ID_MP2 ||
88  par->codec_id == AV_CODEC_ID_MP3 ||
89  par->codec_id == AV_CODEC_ID_GSM_MS) {
90  bps = 0;
91  } else {
92  if (!(bps = av_get_bits_per_sample(par->codec_id))) {
93  if (par->bits_per_coded_sample)
94  bps = par->bits_per_coded_sample;
95  else
96  bps = 16; // default to 16
97  }
98  }
99  if (bps != par->bits_per_coded_sample && par->bits_per_coded_sample) {
101  "requested bits_per_coded_sample (%d) "
102  "and actually stored (%d) differ\n",
103  par->bits_per_coded_sample, bps);
104  }
105 
106  if (par->codec_id == AV_CODEC_ID_MP2) {
107  blkalign = (144 * par->bit_rate - 1)/par->sample_rate + 1;
108  } else if (par->codec_id == AV_CODEC_ID_MP3) {
109  blkalign = 576 * (par->sample_rate <= (24000 + 32000)/2 ? 1 : 2);
110  } else if (par->codec_id == AV_CODEC_ID_AC3) {
111  blkalign = 3840; /* maximum bytes per frame */
112  } else if (par->codec_id == AV_CODEC_ID_AAC) {
113  blkalign = 768 * par->channels; /* maximum bytes per frame */
114  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
115  blkalign = 24;
116  } else if (par->block_align != 0) { /* specified by the codec */
117  blkalign = par->block_align;
118  } else
119  blkalign = bps * par->channels / av_gcd(8, bps);
120  if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
126  bytespersec = par->sample_rate * blkalign;
127  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
128  bytespersec = 800;
129  } else {
130  bytespersec = par->bit_rate / 8;
131  }
132  avio_wl32(pb, bytespersec); /* bytes per second */
133  avio_wl16(pb, blkalign); /* block align */
134  avio_wl16(pb, bps); /* bits per sample */
135  if (par->codec_id == AV_CODEC_ID_MP3) {
136  bytestream_put_le16(&riff_extradata, 1); /* wID */
137  bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
138  bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
139  bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
140  bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
141  } else if (par->codec_id == AV_CODEC_ID_MP2) {
142  /* fwHeadLayer */
143  bytestream_put_le16(&riff_extradata, 2);
144  /* dwHeadBitrate */
145  bytestream_put_le32(&riff_extradata, par->bit_rate);
146  /* fwHeadMode */
147  bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
148  /* fwHeadModeExt */
149  bytestream_put_le16(&riff_extradata, 0);
150  /* wHeadEmphasis */
151  bytestream_put_le16(&riff_extradata, 1);
152  /* fwHeadFlags */
153  bytestream_put_le16(&riff_extradata, 16);
154  /* dwPTSLow */
155  bytestream_put_le32(&riff_extradata, 0);
156  /* dwPTSHigh */
157  bytestream_put_le32(&riff_extradata, 0);
158  } else if (par->codec_id == AV_CODEC_ID_G723_1) {
159  bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
160  bytestream_put_le32(&riff_extradata, 0xaea2f732);
161  bytestream_put_le16(&riff_extradata, 0xacde);
162  } else if (par->codec_id == AV_CODEC_ID_GSM_MS ||
164  /* wSamplesPerBlock */
165  bytestream_put_le16(&riff_extradata, frame_size);
166  } else if (par->extradata_size) {
167  riff_extradata_start = par->extradata;
168  riff_extradata = par->extradata + par->extradata_size;
169  }
170  /* write WAVEFORMATEXTENSIBLE extensions */
171  if (waveformatextensible) {
172  int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
174  par->channel_layout < 0x40000);
175  /* 22 is WAVEFORMATEXTENSIBLE size */
176  avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
177  /* ValidBitsPerSample || SamplesPerBlock || Reserved */
178  avio_wl16(pb, bps);
179  /* dwChannelMask */
180  avio_wl32(pb, write_channel_mask ? par->channel_layout : 0);
181  /* GUID + next 3 */
182  if (par->codec_id == AV_CODEC_ID_EAC3) {
184  } else {
185  avio_wl32(pb, par->codec_tag);
186  avio_wl32(pb, 0x00100000);
187  avio_wl32(pb, 0xAA000080);
188  avio_wl32(pb, 0x719B3800);
189  }
190  } else if ((flags & FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX) ||
191  par->codec_tag != 0x0001 /* PCM */ ||
192  riff_extradata - riff_extradata_start) {
193  /* WAVEFORMATEX */
194  avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
195  } /* else PCMWAVEFORMAT */
196  avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
197  hdrsize = avio_tell(pb) - hdrstart;
198  if (hdrsize & 1) {
199  hdrsize++;
200  avio_w8(pb, 0);
201  }
202 
203  return hdrsize;
204 }
205 
206 /* BITMAPINFOHEADER header */
208  const AVCodecTag *tags, int for_asf, int ignore_extradata)
209 {
210  int keep_height = par->extradata_size >= 9 &&
211  !memcmp(par->extradata + par->extradata_size - 9, "BottomUp", 9);
212  int extradata_size = par->extradata_size - 9*keep_height;
213  enum AVPixelFormat pix_fmt = par->format;
214  int pal_avi;
215 
216  if (pix_fmt == AV_PIX_FMT_NONE && par->bits_per_coded_sample == 1)
217  pix_fmt = AV_PIX_FMT_MONOWHITE;
218  pal_avi = !for_asf &&
219  (pix_fmt == AV_PIX_FMT_PAL8 ||
220  pix_fmt == AV_PIX_FMT_MONOWHITE ||
221  pix_fmt == AV_PIX_FMT_MONOBLACK);
222 
223  /* Size (not including the size of the color table or color masks) */
224  avio_wl32(pb, 40 + (ignore_extradata || pal_avi ? 0 : extradata_size));
225  avio_wl32(pb, par->width);
226  //We always store RGB TopDown
227  avio_wl32(pb, par->codec_tag || keep_height ? par->height : -par->height);
228  /* planes */
229  avio_wl16(pb, 1);
230  /* depth */
232  /* compression type */
233  avio_wl32(pb, par->codec_tag);
234  avio_wl32(pb, (par->width * par->height * (par->bits_per_coded_sample ? par->bits_per_coded_sample : 24)+7) / 8);
235  avio_wl32(pb, 0);
236  avio_wl32(pb, 0);
237  /* Number of color indices in the color table that are used.
238  * A value of 0 means 2^biBitCount indices, but this doesn't work
239  * with Windows Media Player and files containing xxpc chunks. */
240  avio_wl32(pb, pal_avi ? 1 << par->bits_per_coded_sample : 0);
241  avio_wl32(pb, 0);
242 
243  if (!ignore_extradata) {
244  if (par->extradata_size) {
245  avio_write(pb, par->extradata, extradata_size);
246  if (!for_asf && extradata_size & 1)
247  avio_w8(pb, 0);
248  } else if (pal_avi) {
249  int i;
250  for (i = 0; i < 1 << par->bits_per_coded_sample; i++) {
251  /* Initialize 1 bpp palette to black & white */
252  if (i == 0 && pix_fmt == AV_PIX_FMT_MONOWHITE)
253  avio_wl32(pb, 0xffffff);
254  else if (i == 1 && pix_fmt == AV_PIX_FMT_MONOBLACK)
255  avio_wl32(pb, 0xffffff);
256  else
257  avio_wl32(pb, 0);
258  }
259  }
260  }
261 }
262 
263 void ff_parse_specific_params(AVStream *st, int *au_rate,
264  int *au_ssize, int *au_scale)
265 {
266  AVCodecParameters *par = st->codecpar;
267  int gcd;
268  int audio_frame_size;
269 
270  audio_frame_size = av_get_audio_frame_duration2(par, 0);
271  if (!audio_frame_size)
272  audio_frame_size = par->frame_size;
273 
274  *au_ssize = par->block_align;
275  if (audio_frame_size && par->sample_rate) {
276  *au_scale = audio_frame_size;
277  *au_rate = par->sample_rate;
278  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
279  par->codec_type == AVMEDIA_TYPE_DATA ||
281  *au_scale = st->time_base.num;
282  *au_rate = st->time_base.den;
283  } else {
284  *au_scale = par->block_align ? par->block_align * 8 : 8;
285  *au_rate = par->bit_rate ? par->bit_rate :
286  8 * par->sample_rate;
287  }
288  gcd = av_gcd(*au_scale, *au_rate);
289  *au_scale /= gcd;
290  *au_rate /= gcd;
291 }
292 
293 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
294 {
295  size_t len = strlen(str);
296  if (len > 0 && len < UINT32_MAX) {
297  len++;
298  ffio_wfourcc(pb, tag);
299  avio_wl32(pb, len);
300  avio_put_str(pb, str);
301  if (len & 1)
302  avio_w8(pb, 0);
303  }
304 }
305 
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",
310  { 0 }
311 };
312 
314 {
315  int i;
316 
317  for (i = 0; *riff_tags[i]; i++)
319  return 1;
320 
321  return 0;
322 }
323 
325 {
326  AVIOContext *pb = s->pb;
327  int i;
328  int64_t list_pos;
329  AVDictionaryEntry *t = NULL;
330 
332 
333  /* writing empty LIST is not nice and may cause problems */
334  if (!riff_has_valid_tags(s))
335  return;
336 
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],
342  ff_riff_write_info_tag(s->pb, t->key, t->value);
343  ff_end_tag(pb, list_pos);
344 }
345 
347 {
348  av_assert0(sizeof(*g) == 16);
349  avio_write(s, *g, sizeof(*g));
350 }
351 
352 const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
353 {
354  int i;
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);
358  }
359  return NULL;
360 }
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:446
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
static enum AVPixelFormat pix_fmt
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
else temp
Definition: vf_mcdeint.c:259
const char * g
Definition: vf_curves.c:108
enum AVCodecID id
Definition: riff.h:92
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3922
ff_asf_guid guid
Definition: riff.h:93
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1607
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3914
void ff_parse_specific_params(AVStream *st, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:263
int frame_size
Audio only.
Definition: avcodec.h:4043
const AVCodecGuid ff_codec_wav_guids[]
Definition: riff.c:542
Format I/O context.
Definition: avformat.h:1325
#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:346
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:346
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:195
int width
Video only.
Definition: avcodec.h:3988
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
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:39
uint32_t tag
Definition: movenc.c:1367
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
#define FFALIGN(x, a)
Definition: macros.h:48
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4024
#define av_log(a,...)
void ff_riff_write_info(AVFormatContext *s)
Write all recognized RIFF tags from s->metadata.
Definition: riffenc.c:324
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3951
const ff_asf_guid * ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
Definition: riffenc.c:352
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
#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:1539
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3446
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:515
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3918
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of a and b.
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:3657
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3940
int block_align
Audio only.
Definition: avcodec.h:4039
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, const AVCodecTag *tags, int for_asf, int ignore_extradata)
Definition: riffenc.c:207
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:293
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:362
uint8_t ff_asf_guid[16]
Definition: riff.h:89
Stream structure.
Definition: avformat.h:876
int frame_size
Definition: mxfenc.c:1821
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:1367
static const char riff_tags[][5]
Definition: riffenc.c:306
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
static int riff_has_valid_tags(AVFormatContext *s)
Definition: riffenc.c:313
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2869
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:515
static int flags
Definition: cpu.c:47
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: avcodec.h:4032
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:72
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:71
char * key
Definition: dict.h:86
int den
denominator
Definition: rational.h:45
unsigned bps
Definition: movenc.c:1368
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:58
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3964
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3936
int channels
Audio only.
Definition: avcodec.h:4028
void INT64 start
Definition: avisynth_c.h:553
AVCodecParameters * codecpar
Definition: avformat.h:1006
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3926
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:913
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:53