/src/ffmpeg/libavformat/riffdec.c
Line | Count | Source |
1 | | /* |
2 | | * RIFF demuxing functions and data |
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/avassert.h" |
23 | | #include "libavutil/dict.h" |
24 | | #include "libavutil/error.h" |
25 | | #include "libavutil/intreadwrite.h" |
26 | | #include "libavutil/log.h" |
27 | | #include "libavutil/mem.h" |
28 | | #include "avformat.h" |
29 | | #include "avio_internal.h" |
30 | | #include "demux.h" |
31 | | #include "riff.h" |
32 | | |
33 | | int ff_get_guid(AVIOContext *s, ff_asf_guid *g) |
34 | 2.69M | { |
35 | 2.69M | int ret; |
36 | 2.69M | av_assert0(sizeof(*g) == 16); //compiler will optimize this out |
37 | 2.69M | ret = ffio_read_size(s, *g, sizeof(*g)); |
38 | 2.69M | if (ret < 0) { |
39 | 1.02M | memset(*g, 0, sizeof(*g)); |
40 | 1.02M | return ret; |
41 | 1.02M | } |
42 | 1.66M | return 0; |
43 | 2.69M | } |
44 | | |
45 | | enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid) |
46 | 18.0k | { |
47 | 18.0k | int i; |
48 | 117k | for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++) |
49 | 105k | if (!ff_guidcmp(guids[i].guid, guid)) |
50 | 5.50k | return guids[i].id; |
51 | 12.4k | return AV_CODEC_ID_NONE; |
52 | 18.0k | } |
53 | | |
54 | | /* We could be given one of the three possible structures here: |
55 | | * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure |
56 | | * is an expansion of the previous one with the fields added |
57 | | * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and |
58 | | * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself |
59 | | * an openended structure. |
60 | | */ |
61 | | |
62 | | static void parse_waveformatex(void *logctx, AVIOContext *pb, AVCodecParameters *par) |
63 | 27.6k | { |
64 | 27.6k | ff_asf_guid subformat; |
65 | 27.6k | int bps; |
66 | 27.6k | uint64_t mask; |
67 | | |
68 | 27.6k | bps = avio_rl16(pb); |
69 | 27.6k | if (bps) |
70 | 12.7k | par->bits_per_coded_sample = bps; |
71 | | |
72 | 27.6k | mask = avio_rl32(pb); /* dwChannelMask */ |
73 | 27.6k | av_channel_layout_from_mask(&par->ch_layout, mask); |
74 | | |
75 | 27.6k | ff_get_guid(pb, &subformat); |
76 | 27.6k | if (!memcmp(subformat + 4, |
77 | 27.6k | (const uint8_t[]){ FF_AMBISONIC_BASE_GUID }, 12) || |
78 | 25.1k | !memcmp(subformat + 4, |
79 | 25.1k | (const uint8_t[]){ FF_BROKEN_BASE_GUID }, 12) || |
80 | 22.2k | !memcmp(subformat + 4, |
81 | 22.2k | (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) { |
82 | 10.6k | par->codec_tag = AV_RL32(subformat); |
83 | 10.6k | par->codec_id = ff_wav_codec_get_id(par->codec_tag, |
84 | 10.6k | par->bits_per_coded_sample); |
85 | 17.0k | } else { |
86 | 17.0k | par->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat); |
87 | 17.0k | if (!par->codec_id) |
88 | 12.1k | av_log(logctx, AV_LOG_WARNING, |
89 | 12.1k | "unknown subformat:"FF_PRI_GUID"\n", |
90 | 12.1k | FF_ARG_GUID(subformat)); |
91 | 17.0k | } |
92 | 27.6k | } |
93 | | |
94 | | /* "big_endian" values are needed for RIFX file format */ |
95 | | int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, |
96 | | AVCodecParameters *par, int size, int big_endian) |
97 | 202k | { |
98 | 202k | int id, channels = 0, ret; |
99 | 202k | uint64_t bitrate = 0; |
100 | | |
101 | 202k | if (size < 14) { |
102 | 255 | avpriv_request_sample(s, "wav header size < 14"); |
103 | 255 | return AVERROR_INVALIDDATA; |
104 | 255 | } |
105 | | |
106 | 201k | av_channel_layout_uninit(&par->ch_layout); |
107 | | |
108 | 201k | par->codec_type = AVMEDIA_TYPE_AUDIO; |
109 | 201k | if (!big_endian) { |
110 | 201k | id = avio_rl16(pb); |
111 | 201k | if (id != 0x0165) { |
112 | 198k | channels = avio_rl16(pb); |
113 | 198k | par->sample_rate = avio_rl32(pb); |
114 | 198k | bitrate = avio_rl32(pb) * 8LL; |
115 | 198k | par->block_align = avio_rl16(pb); |
116 | 198k | } |
117 | 201k | } else { |
118 | 91 | id = avio_rb16(pb); |
119 | 91 | channels = avio_rb16(pb); |
120 | 91 | par->sample_rate = avio_rb32(pb); |
121 | 91 | bitrate = avio_rb32(pb) * 8LL; |
122 | 91 | par->block_align = avio_rb16(pb); |
123 | 91 | } |
124 | 201k | if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */ |
125 | 7.32k | par->bits_per_coded_sample = 8; |
126 | 194k | } else { |
127 | 194k | if (!big_endian) { |
128 | 194k | par->bits_per_coded_sample = avio_rl16(pb); |
129 | 194k | } else { |
130 | 91 | par->bits_per_coded_sample = avio_rb16(pb); |
131 | 91 | } |
132 | 194k | } |
133 | 201k | if (id == 0xFFFE) { |
134 | 28.5k | par->codec_tag = 0; |
135 | 173k | } else { |
136 | 173k | par->codec_tag = id; |
137 | 173k | par->codec_id = ff_wav_codec_get_id(id, |
138 | 173k | par->bits_per_coded_sample); |
139 | 173k | } |
140 | 201k | if (size >= 18 && id != 0x0165) { /* We're obviously dealing with WAVEFORMATEX */ |
141 | 159k | int cbSize = avio_rl16(pb); /* cbSize */ |
142 | 159k | if (big_endian) { |
143 | 1 | avpriv_report_missing_feature(s, "WAVEFORMATEX support for RIFX files"); |
144 | 1 | return AVERROR_PATCHWELCOME; |
145 | 1 | } |
146 | 159k | size -= 18; |
147 | 159k | cbSize = FFMIN(size, cbSize); |
148 | 159k | if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */ |
149 | 27.6k | parse_waveformatex(s, pb, par); |
150 | 27.6k | cbSize -= 22; |
151 | 27.6k | size -= 22; |
152 | 132k | } else if (cbSize >= 12 && id == 0x1610) { /* HEAACWAVEFORMAT */ |
153 | 295 | int wPayloadType = avio_rl16(pb); |
154 | 295 | if (wPayloadType == 3) |
155 | 73 | par->codec_id = AV_CODEC_ID_AAC_LATM; |
156 | 295 | avio_skip(pb, 2); // wAudioProfileLevelIndication |
157 | 295 | int wStructType = avio_rl16(pb); |
158 | 295 | if (wStructType) { |
159 | 63 | avpriv_report_missing_feature(s, "HEAACWAVEINFO wStructType \"%d\"", wStructType); |
160 | 63 | return AVERROR_PATCHWELCOME; |
161 | 63 | } |
162 | 232 | avio_skip(pb, 2); // wReserved1 |
163 | 232 | avio_skip(pb, 4); // dwReserved2 |
164 | 232 | cbSize -= 12; |
165 | 232 | size -= 12; |
166 | 232 | } |
167 | 159k | if (cbSize > 0) { |
168 | 141k | ret = ff_get_extradata(s, par, pb, cbSize); |
169 | 141k | if (ret < 0) |
170 | 1.24k | return ret; |
171 | 140k | size -= cbSize; |
172 | 140k | } |
173 | | |
174 | | /* It is possible for the chunk to contain garbage at the end */ |
175 | 158k | if (size > 0) |
176 | 28.0k | avio_skip(pb, size); |
177 | 158k | } else if (id == 0x0165 && size >= 32) { |
178 | 3.02k | int nb_streams, i; |
179 | | |
180 | 3.02k | size -= 4; |
181 | 3.02k | ret = ff_get_extradata(s, par, pb, size); |
182 | 3.02k | if (ret < 0) |
183 | 330 | return ret; |
184 | 2.69k | nb_streams = AV_RL16(par->extradata + 4); |
185 | 2.69k | par->sample_rate = AV_RL32(par->extradata + 12); |
186 | 2.69k | channels = 0; |
187 | 2.69k | bitrate = 0; |
188 | 2.69k | if (size < 8 + nb_streams * 20) |
189 | 54 | return AVERROR_INVALIDDATA; |
190 | 53.1k | for (i = 0; i < nb_streams; i++) |
191 | 50.4k | channels += par->extradata[8 + i * 20 + 17]; |
192 | 2.64k | } |
193 | | |
194 | 200k | par->bit_rate = bitrate; |
195 | | |
196 | 200k | if (par->sample_rate <= 0) { |
197 | 1.40k | av_log(s, AV_LOG_ERROR, |
198 | 1.40k | "Invalid sample rate: %d\n", par->sample_rate); |
199 | 1.40k | return AVERROR_INVALIDDATA; |
200 | 1.40k | } |
201 | 198k | if (par->codec_id == AV_CODEC_ID_AAC_LATM) { |
202 | | /* Channels and sample_rate values are those prior to applying SBR |
203 | | * and/or PS. */ |
204 | 3.48k | channels = 0; |
205 | 3.48k | par->sample_rate = 0; |
206 | 3.48k | } |
207 | | /* override bits_per_coded_sample for G.726 */ |
208 | 198k | if (par->codec_id == AV_CODEC_ID_ADPCM_G726 && par->sample_rate) |
209 | 3.51k | par->bits_per_coded_sample = par->bit_rate / par->sample_rate; |
210 | | |
211 | | /* ignore WAVEFORMATEXTENSIBLE layout if different from channel count */ |
212 | 198k | if (channels != par->ch_layout.nb_channels) { |
213 | 141k | av_channel_layout_uninit(&par->ch_layout); |
214 | 141k | par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; |
215 | 141k | par->ch_layout.nb_channels = channels; |
216 | 141k | } |
217 | | |
218 | 198k | return 0; |
219 | 200k | } |
220 | | |
221 | | enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps) |
222 | 298k | { |
223 | 298k | enum AVCodecID id; |
224 | 298k | id = ff_codec_get_id(ff_codec_wav_tags, tag); |
225 | 298k | if (id <= 0) |
226 | 133k | return id; |
227 | | |
228 | 164k | if (id == AV_CODEC_ID_PCM_S16LE) |
229 | 79.7k | id = ff_get_pcm_codec_id(bps, 0, 0, ~1); |
230 | 84.9k | else if (id == AV_CODEC_ID_PCM_F32LE) |
231 | 11.9k | id = ff_get_pcm_codec_id(bps, 1, 0, 0); |
232 | | |
233 | 164k | if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8) |
234 | 775 | id = AV_CODEC_ID_ADPCM_ZORK; |
235 | 164k | return id; |
236 | 298k | } |
237 | | |
238 | | int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size) |
239 | 6.00k | { |
240 | 6.00k | int tag1; |
241 | 6.00k | uint32_t size_ = avio_rl32(pb); |
242 | 6.00k | if (size) |
243 | 5.79k | *size = size_; |
244 | 6.00k | st->codecpar->width = avio_rl32(pb); |
245 | 6.00k | st->codecpar->height = (int32_t)avio_rl32(pb); |
246 | 6.00k | avio_rl16(pb); /* planes */ |
247 | 6.00k | st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */ |
248 | 6.00k | tag1 = avio_rl32(pb); |
249 | 6.00k | avio_rl32(pb); /* ImageSize */ |
250 | 6.00k | avio_rl32(pb); /* XPelsPerMeter */ |
251 | 6.00k | avio_rl32(pb); /* YPelsPerMeter */ |
252 | 6.00k | avio_rl32(pb); /* ClrUsed */ |
253 | 6.00k | avio_rl32(pb); /* ClrImportant */ |
254 | 6.00k | return tag1; |
255 | 6.00k | } |
256 | | |
257 | | int ff_read_riff_info(AVFormatContext *s, int64_t size) |
258 | 4.91k | { |
259 | 4.91k | int64_t start, end, cur; |
260 | 4.91k | AVIOContext *pb = s->pb; |
261 | | |
262 | 4.91k | start = avio_tell(pb); |
263 | 4.91k | end = start + size; |
264 | | |
265 | 35.2k | while ((cur = avio_tell(pb)) >= 0 && |
266 | 35.2k | cur <= end - 8 /* = tag + size */) { |
267 | 31.0k | uint32_t chunk_code; |
268 | 31.0k | int64_t chunk_size; |
269 | 31.0k | char key[5] = { 0 }; |
270 | 31.0k | char *value; |
271 | | |
272 | 31.0k | chunk_code = avio_rl32(pb); |
273 | 31.0k | chunk_size = avio_rl32(pb); |
274 | 31.0k | if (avio_feof(pb)) { |
275 | 156 | if (chunk_code || chunk_size) { |
276 | 42 | av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n"); |
277 | 42 | return AVERROR_INVALIDDATA; |
278 | 42 | } |
279 | 114 | return AVERROR_EOF; |
280 | 156 | } |
281 | 30.8k | if (chunk_size > end || |
282 | 30.3k | end - chunk_size < cur || |
283 | 29.8k | chunk_size == UINT_MAX) { |
284 | 1.02k | avio_seek(pb, -9, SEEK_CUR); |
285 | 1.02k | chunk_code = avio_rl32(pb); |
286 | 1.02k | chunk_size = avio_rl32(pb); |
287 | 1.02k | if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) { |
288 | 519 | av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n"); |
289 | 519 | return AVERROR_INVALIDDATA; |
290 | 519 | } |
291 | 1.02k | } |
292 | | |
293 | 30.3k | chunk_size += (chunk_size & 1); |
294 | | |
295 | 30.3k | if (!chunk_code) { |
296 | 7.15k | if (chunk_size) |
297 | 272 | avio_skip(pb, chunk_size); |
298 | 6.88k | else if (pb->eof_reached) { |
299 | 6 | av_log(s, AV_LOG_WARNING, "truncated file\n"); |
300 | 6 | return AVERROR_EOF; |
301 | 6 | } |
302 | 7.14k | continue; |
303 | 7.15k | } |
304 | | |
305 | 23.1k | value = av_mallocz(chunk_size + 1); |
306 | 23.1k | if (!value) { |
307 | 16 | av_log(s, AV_LOG_ERROR, |
308 | 16 | "out of memory, unable to read INFO tag\n"); |
309 | 16 | return AVERROR(ENOMEM); |
310 | 16 | } |
311 | | |
312 | 23.1k | AV_WL32(key, chunk_code); |
313 | | // Work around VC++ 2015 Update 1 code-gen bug: |
314 | | // https://connect.microsoft.com/VisualStudio/feedback/details/2291638 |
315 | 23.1k | key[4] = 0; |
316 | | |
317 | 23.1k | if (avio_read(pb, value, chunk_size) != chunk_size) { |
318 | 85 | av_log(s, AV_LOG_WARNING, |
319 | 85 | "premature end of file while reading INFO tag\n"); |
320 | 85 | } |
321 | | |
322 | 23.1k | av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL); |
323 | 23.1k | } |
324 | | |
325 | 4.21k | return 0; |
326 | 4.91k | } |