]> git.sesse.net Git - ffmpeg/blob - libavformat/riffenc.c
Merge commit '192ccc5034ad4ac1b5022fc16c1162267add6a0f'
[ffmpeg] / libavformat / riffenc.c
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, 0);
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 */
54 int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
55 {
56     int bps, blkalign, bytespersec, frame_size;
57     int hdrsize = 18;
58     int waveformatextensible;
59     uint8_t temp[256];
60     uint8_t *riff_extradata       = temp;
61     uint8_t *riff_extradata_start = temp;
62
63     if (!enc->codec_tag || enc->codec_tag > 0xffff)
64         return -1;
65
66     /* We use the known constant frame size for the codec if known, otherwise
67      * fall back on using AVCodecContext.frame_size, which is not as reliable
68      * for indicating packet duration. */
69     frame_size = av_get_audio_frame_duration(enc, 0);
70     if (!frame_size)
71         frame_size = enc->frame_size;
72
73     waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
74                            enc->sample_rate > 48000 ||
75                            enc->codec_id == AV_CODEC_ID_EAC3 ||
76                            av_get_bits_per_sample(enc->codec_id) > 16;
77
78     if (waveformatextensible)
79         avio_wl16(pb, 0xfffe);
80     else
81         avio_wl16(pb, enc->codec_tag);
82
83     avio_wl16(pb, enc->channels);
84     avio_wl32(pb, enc->sample_rate);
85     if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
86         enc->codec_id == AV_CODEC_ID_G723_1 ||
87         enc->codec_id == AV_CODEC_ID_MP2    ||
88         enc->codec_id == AV_CODEC_ID_MP3    ||
89         enc->codec_id == AV_CODEC_ID_GSM_MS) {
90         bps = 0;
91     } else {
92         if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
93             if (enc->bits_per_coded_sample)
94                 bps = enc->bits_per_coded_sample;
95             else
96                 bps = 16;  // default to 16
97         }
98     }
99     if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
100         av_log(enc, AV_LOG_WARNING,
101                "requested bits_per_coded_sample (%d) "
102                "and actually stored (%d) differ\n",
103                enc->bits_per_coded_sample, bps);
104     }
105
106     if (enc->codec_id == AV_CODEC_ID_MP2 ||
107         enc->codec_id == AV_CODEC_ID_MP3) {
108         /* This is wrong, but it seems many demuxers do not work if this
109          * is set correctly. */
110         blkalign = frame_size;
111         // blkalign = 144 * enc->bit_rate/enc->sample_rate;
112     } else if (enc->codec_id == AV_CODEC_ID_AC3) {
113         blkalign = 3840;                /* maximum bytes per frame */
114     } else if (enc->codec_id == AV_CODEC_ID_AAC) {
115         blkalign = 768 * enc->channels; /* maximum bytes per frame */
116     } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
117         blkalign = 24;
118     } else if (enc->block_align != 0) { /* specified by the codec */
119         blkalign = enc->block_align;
120     } else
121         blkalign = bps * enc->channels / av_gcd(8, bps);
122     if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
123         enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
124         enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
125         enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
126         enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
127         enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
128         bytespersec = enc->sample_rate * blkalign;
129     } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
130         bytespersec = 800;
131     } else {
132         bytespersec = enc->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 (enc->codec_id == AV_CODEC_ID_MP3) {
138         hdrsize += 12;
139         bytestream_put_le16(&riff_extradata, 1);    /* wID */
140         bytestream_put_le32(&riff_extradata, 2);    /* fdwFlags */
141         bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
142         bytestream_put_le16(&riff_extradata, 1);    /* nFramesPerBlock */
143         bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
144     } else if (enc->codec_id == AV_CODEC_ID_MP2) {
145         hdrsize += 22;
146         /* fwHeadLayer */
147         bytestream_put_le16(&riff_extradata, 2);
148         /* dwHeadBitrate */
149         bytestream_put_le32(&riff_extradata, enc->bit_rate);
150         /* fwHeadMode */
151         bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
152         /* fwHeadModeExt */
153         bytestream_put_le16(&riff_extradata, 0);
154         /* wHeadEmphasis */
155         bytestream_put_le16(&riff_extradata, 1);
156         /* fwHeadFlags */
157         bytestream_put_le16(&riff_extradata, 16);
158         /* dwPTSLow */
159         bytestream_put_le32(&riff_extradata, 0);
160         /* dwPTSHigh */
161         bytestream_put_le32(&riff_extradata, 0);
162     } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
163         hdrsize += 20;
164         bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
165         bytestream_put_le32(&riff_extradata, 0xaea2f732);
166         bytestream_put_le16(&riff_extradata, 0xacde);
167     } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
168                enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
169         hdrsize += 2;
170         /* wSamplesPerBlock */
171         bytestream_put_le16(&riff_extradata, frame_size);
172     } else if (enc->extradata_size) {
173         riff_extradata_start = enc->extradata;
174         riff_extradata       = enc->extradata + enc->extradata_size;
175         hdrsize             += enc->extradata_size;
176     }
177     /* write WAVEFORMATEXTENSIBLE extensions */
178     if (waveformatextensible) {
179         hdrsize += 22;
180         /* 22 is WAVEFORMATEXTENSIBLE size */
181         avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
182         /* ValidBitsPerSample || SamplesPerBlock || Reserved */
183         avio_wl16(pb, bps);
184         /* dwChannelMask */
185         avio_wl32(pb, enc->channel_layout);
186         /* GUID + next 3 */
187         if (enc->codec_id == AV_CODEC_ID_EAC3) {
188             ff_put_guid(pb, get_codec_guid(enc->codec_id, ff_codec_wav_guids));
189         } else {
190         avio_wl32(pb, enc->codec_tag);
191         avio_wl32(pb, 0x00100000);
192         avio_wl32(pb, 0xAA000080);
193         avio_wl32(pb, 0x719B3800);
194         }
195     } else {
196         avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
197     }
198     avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
199     if (hdrsize & 1) {
200         hdrsize++;
201         avio_w8(pb, 0);
202     }
203
204     return hdrsize;
205 }
206
207 /* BITMAPINFOHEADER header */
208 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
209                        const AVCodecTag *tags, int for_asf, int ignore_extradata)
210 {
211     /* size */
212     avio_wl32(pb, 40 + (ignore_extradata ? 0 : enc->extradata_size));
213     avio_wl32(pb, enc->width);
214     //We always store RGB TopDown
215     avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
216     /* planes */
217     avio_wl16(pb, 1);
218     /* depth */
219     avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
220     /* compression type */
221     avio_wl32(pb, enc->codec_tag);
222     avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
223     avio_wl32(pb, 0);
224     avio_wl32(pb, 0);
225     avio_wl32(pb, 0);
226     avio_wl32(pb, 0);
227
228     if (!ignore_extradata) {
229         avio_write(pb, enc->extradata, enc->extradata_size);
230
231         if (!for_asf && enc->extradata_size & 1)
232             avio_w8(pb, 0);
233     }
234 }
235
236 void ff_parse_specific_params(AVCodecContext *stream, int *au_rate,
237                               int *au_ssize, int *au_scale)
238 {
239     int gcd;
240     int audio_frame_size;
241
242     /* We use the known constant frame size for the codec if known, otherwise
243      * fall back on using AVCodecContext.frame_size, which is not as reliable
244      * for indicating packet duration. */
245     audio_frame_size = av_get_audio_frame_duration(stream, 0);
246     if (!audio_frame_size)
247         audio_frame_size = stream->frame_size;
248
249     *au_ssize = stream->block_align;
250     if (audio_frame_size && stream->sample_rate) {
251         *au_scale = audio_frame_size;
252         *au_rate  = stream->sample_rate;
253     } else if (stream->codec_type == AVMEDIA_TYPE_VIDEO ||
254                stream->codec_type == AVMEDIA_TYPE_DATA ||
255                stream->codec_type == AVMEDIA_TYPE_SUBTITLE) {
256         *au_scale = stream->time_base.num;
257         *au_rate  = stream->time_base.den;
258     } else {
259         *au_scale = stream->block_align ? stream->block_align * 8 : 8;
260         *au_rate  = stream->bit_rate ? stream->bit_rate :
261                     8 * stream->sample_rate;
262     }
263     gcd        = av_gcd(*au_scale, *au_rate);
264     *au_scale /= gcd;
265     *au_rate  /= gcd;
266 }
267
268 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
269 {
270     int len = strlen(str);
271     if (len > 0) {
272         len++;
273         ffio_wfourcc(pb, tag);
274         avio_wl32(pb, len);
275         avio_put_str(pb, str);
276         if (len & 1)
277             avio_w8(pb, 0);
278     }
279 }
280
281 static const char riff_tags[][5] = {
282     "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
283     "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
284     "IPRT", "ITRK", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
285     { 0 }
286 };
287
288 static int riff_has_valid_tags(AVFormatContext *s)
289 {
290     int i;
291
292     for (i = 0; *riff_tags[i]; i++)
293         if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
294             return 1;
295
296     return 0;
297 }
298
299 void ff_riff_write_info(AVFormatContext *s)
300 {
301     AVIOContext *pb = s->pb;
302     int i;
303     int64_t list_pos;
304     AVDictionaryEntry *t = NULL;
305
306     ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
307
308     /* writing empty LIST is not nice and may cause problems */
309     if (!riff_has_valid_tags(s))
310         return;
311
312     list_pos = ff_start_tag(pb, "LIST");
313     ffio_wfourcc(pb, "INFO");
314     for (i = 0; *riff_tags[i]; i++)
315         if ((t = av_dict_get(s->metadata, riff_tags[i],
316                              NULL, AV_DICT_MATCH_CASE)))
317             ff_riff_write_info_tag(s->pb, t->key, t->value);
318     ff_end_tag(pb, list_pos);
319 }
320
321 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
322 {
323     av_assert0(sizeof(*g) == 16);
324     avio_write(s, *g, sizeof(*g));
325 }
326
327 const ff_asf_guid *get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid)
328 {
329     int i;
330     for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) {
331         if (id == av_guid[i].id)
332             return &(av_guid[i].guid);
333     }
334     return NULL;
335 }