2 * RIFF common functions and data
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/mathematics.h"
23 #include "libavcodec/avcodec.h"
25 #include "avio_internal.h"
27 #include "libavcodec/bytestream.h"
28 #include "libavutil/avassert.h"
30 /* Note: When encoding, the first matching tag is used, so order is
31 * important if multiple tags are possible for a given codec.
32 * Note also that this list is used for more than just riff, other
33 * files use it as well.
35 const AVCodecTag ff_codec_bmp_tags[] = {
36 { AV_CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
37 { AV_CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
38 { AV_CODEC_ID_H264, MKTAG('X', '2', '6', '4') },
39 { AV_CODEC_ID_H264, MKTAG('x', '2', '6', '4') },
40 { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
41 { AV_CODEC_ID_H264, MKTAG('D', 'A', 'V', 'C') },
42 { AV_CODEC_ID_H264, MKTAG('S', 'M', 'V', '2') },
43 { AV_CODEC_ID_H264, MKTAG('V', 'S', 'S', 'H') },
44 { AV_CODEC_ID_H264, MKTAG('Q', '2', '6', '4') }, /* QNAP surveillance system */
45 { AV_CODEC_ID_H264, MKTAG('V', '2', '6', '4') },
46 { AV_CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
47 { AV_CODEC_ID_H263, MKTAG('X', '2', '6', '3') },
48 { AV_CODEC_ID_H263, MKTAG('T', '2', '6', '3') },
49 { AV_CODEC_ID_H263, MKTAG('L', '2', '6', '3') },
50 { AV_CODEC_ID_H263, MKTAG('V', 'X', '1', 'K') },
51 { AV_CODEC_ID_H263, MKTAG('Z', 'y', 'G', 'o') },
52 { AV_CODEC_ID_H263, MKTAG('M', '2', '6', '3') },
53 { AV_CODEC_ID_H263, MKTAG('l', 's', 'v', 'm') },
54 { AV_CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
55 { AV_CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* Intel H.263 */
56 { AV_CODEC_ID_H261, MKTAG('H', '2', '6', '1') },
57 { AV_CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
58 { AV_CODEC_ID_MPEG4, MKTAG('F', 'M', 'P', '4') },
59 { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
60 { AV_CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0') },
61 { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
62 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
63 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
64 /* some broken AVIs use this */
65 { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 0 ) },
66 /* some broken AVIs use this */
67 { AV_CODEC_ID_MPEG4, MKTAG('Z', 'M', 'P', '4') },
68 { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
69 { AV_CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
70 { AV_CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
71 { AV_CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
72 { AV_CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') },
73 { AV_CODEC_ID_MPEG4, MKTAG('S', 'E', 'D', 'G') },
74 { AV_CODEC_ID_MPEG4, MKTAG('R', 'M', 'P', '4') },
75 { AV_CODEC_ID_MPEG4, MKTAG('3', 'I', 'V', '2') },
76 /* WaWv MPEG-4 Video Codec */
77 { AV_CODEC_ID_MPEG4, MKTAG('W', 'A', 'W', 'V') },
78 { AV_CODEC_ID_MPEG4, MKTAG('F', 'F', 'D', 'S') },
79 { AV_CODEC_ID_MPEG4, MKTAG('F', 'V', 'F', 'W') },
80 { AV_CODEC_ID_MPEG4, MKTAG('D', 'C', 'O', 'D') },
81 { AV_CODEC_ID_MPEG4, MKTAG('M', 'V', 'X', 'M') },
82 { AV_CODEC_ID_MPEG4, MKTAG('P', 'M', '4', 'V') },
83 { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', 'P', '4') },
84 { AV_CODEC_ID_MPEG4, MKTAG('D', 'X', 'G', 'M') },
85 { AV_CODEC_ID_MPEG4, MKTAG('V', 'I', 'D', 'M') },
86 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'T', '3') },
87 { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'X') },
89 { AV_CODEC_ID_MPEG4, MKTAG('H', 'D', 'X', '4') },
90 { AV_CODEC_ID_MPEG4, MKTAG('D', 'M', 'K', '2') },
91 { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'G', 'I') },
92 { AV_CODEC_ID_MPEG4, MKTAG('I', 'N', 'M', 'C') },
94 { AV_CODEC_ID_MPEG4, MKTAG('E', 'P', 'H', 'V') },
95 { AV_CODEC_ID_MPEG4, MKTAG('E', 'M', '4', 'A') },
97 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'C', 'C') },
98 { AV_CODEC_ID_MPEG4, MKTAG('S', 'N', '4', '0') },
99 { AV_CODEC_ID_MPEG4, MKTAG('V', 'S', 'P', 'X') },
100 { AV_CODEC_ID_MPEG4, MKTAG('U', 'L', 'D', 'X') },
101 { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'V') },
102 /* Samsung SHR-6040 */
103 { AV_CODEC_ID_MPEG4, MKTAG('S', 'I', 'P', 'P') },
104 { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', '4', 'V') },
105 { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'X') },
106 { AV_CODEC_ID_MPEG4, MKTAG('D', 'r', 'e', 'X') },
107 { AV_CODEC_ID_MPEG4, MKTAG('Q', 'M', 'P', '4') }, /* QNAP Systems */
108 { AV_CODEC_ID_MPEG4, MKTAG('P', 'L', 'V', '1') }, /* Pelco DVR MPEG-4 */
109 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
110 { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') },
111 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
112 { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
113 { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
114 { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
115 { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'V', 'X', '3') },
116 { AV_CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
117 { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
118 { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
119 { AV_CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
120 { AV_CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
121 { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
122 { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', '4', '1') },
123 { AV_CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
124 { AV_CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
125 { AV_CODEC_ID_WMV2, MKTAG('G', 'X', 'V', 'E') },
126 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
127 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
128 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
129 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
130 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
131 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', '0') },
133 { AV_CODEC_ID_DVVIDEO, MKTAG('c', 'd', 'v', 'c') },
135 { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', 'H') },
137 { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', '5') },
138 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
139 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 's') },
140 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
141 { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'i', 's') },
142 { AV_CODEC_ID_DVVIDEO, MKTAG('p', 'd', 'v', 'c') },
143 { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', '2', '5') },
144 { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', 'D', 'V') },
145 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
146 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
147 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') },
148 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'P', 'E', 'G') },
149 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
150 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('P', 'I', 'M', '2') },
151 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
152 { AV_CODEC_ID_MPEG1VIDEO, MKTAG( 1 , 0 , 0 , 16) },
153 { AV_CODEC_ID_MPEG2VIDEO, MKTAG( 2 , 0 , 0 , 16) },
154 { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 16) },
155 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') },
156 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'M', 'E', 'S') },
157 /* Lead MPEG-2 in AVI */
158 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('L', 'M', 'P', '2') },
159 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('s', 'l', 'i', 'f') },
160 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('E', 'M', '2', 'V') },
161 /* Matrox MPEG-2 intra-only */
162 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', '7', '0', '1') },
163 { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', 'v') },
164 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('B', 'W', '1', '0') },
165 { AV_CODEC_ID_MPEG1VIDEO, MKTAG('X', 'M', 'P', 'G') }, /* Xing MPEG intra only */
166 { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
167 { AV_CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
168 { AV_CODEC_ID_MJPEG, MKTAG('d', 'm', 'b', '1') },
169 { AV_CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') },
170 { AV_CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
171 /* Pegasus lossless JPEG */
172 { AV_CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') },
173 /* JPEG-LS custom FOURCC for AVI - encoder */
174 { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'L', 'S') },
175 { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'P', 'G') },
176 /* JPEG-LS custom FOURCC for AVI - decoder */
177 { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'L', 'S') },
178 { AV_CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') },
179 { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'P', 'G') },
180 { AV_CODEC_ID_AVRN, MKTAG('A', 'V', 'R', 'n') },
181 { AV_CODEC_ID_MJPEG, MKTAG('A', 'C', 'D', 'V') },
182 { AV_CODEC_ID_MJPEG, MKTAG('Q', 'I', 'V', 'G') },
184 { AV_CODEC_ID_MJPEG, MKTAG('S', 'L', 'M', 'J') },
185 /* Creative Webcam JPEG */
186 { AV_CODEC_ID_MJPEG, MKTAG('C', 'J', 'P', 'G') },
187 /* Intel JPEG Library Video Codec */
188 { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'L', 'V') },
189 /* Midvid JPEG Video Codec */
190 { AV_CODEC_ID_MJPEG, MKTAG('M', 'V', 'J', 'P') },
191 { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '1') },
192 { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '2') },
193 { AV_CODEC_ID_MJPEG, MKTAG('M', 'T', 'S', 'J') },
194 /* Paradigm Matrix M-JPEG Codec */
195 { AV_CODEC_ID_MJPEG, MKTAG('Z', 'J', 'P', 'G') },
196 { AV_CODEC_ID_MJPEG, MKTAG('M', 'M', 'J', 'P') },
197 { AV_CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
198 { AV_CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') },
199 { AV_CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
200 { AV_CODEC_ID_RAWVIDEO, MKTAG( 0 , 0 , 0 , 0 ) },
201 { AV_CODEC_ID_RAWVIDEO, MKTAG( 3 , 0 , 0 , 0 ) },
202 { AV_CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
203 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') },
204 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
205 { AV_CODEC_ID_RAWVIDEO, MKTAG('V', '4', '2', '2') },
206 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'N', 'V') },
207 { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'V') },
208 { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'Y') },
209 { AV_CODEC_ID_RAWVIDEO, MKTAG('u', 'y', 'v', '1') },
210 { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'V', 'u', '1') },
211 { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') },
212 { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', 's') },
213 { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', '2') },
214 { AV_CODEC_ID_RAWVIDEO, MKTAG('P', '4', '2', '2') },
215 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '2') },
216 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '6') },
217 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '2', '4') },
218 { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') },
219 { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'Y', 'U', 'Y') },
220 { AV_CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') },
221 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', '0', '0') },
222 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', ' ', ' ') },
223 { AV_CODEC_ID_RAWVIDEO, MKTAG('H', 'D', 'Y', 'C') },
224 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
225 /* SoftLab-NSK VideoTizer */
226 { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'D', 'T', 'Z') },
227 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', '1') },
228 { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '1', '2') },
229 { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '2', '1') },
230 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', 'B') },
231 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', 'B') },
232 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'V', '9') },
233 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
234 { AV_CODEC_ID_RAWVIDEO, MKTAG('a', 'u', 'v', '2') },
235 { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'Y', 'U') },
236 { AV_CODEC_ID_FRWU, MKTAG('F', 'R', 'W', 'U') },
237 { AV_CODEC_ID_R10K, MKTAG('R', '1', '0', 'k') },
238 { AV_CODEC_ID_R210, MKTAG('r', '2', '1', '0') },
239 { AV_CODEC_ID_V210, MKTAG('v', '2', '1', '0') },
240 { AV_CODEC_ID_V308, MKTAG('v', '3', '0', '8') },
241 { AV_CODEC_ID_V408, MKTAG('v', '4', '0', '8') },
242 { AV_CODEC_ID_AYUV, MKTAG('A', 'Y', 'U', 'V') },
243 { AV_CODEC_ID_V410, MKTAG('v', '4', '1', '0') },
244 { AV_CODEC_ID_YUV4, MKTAG('y', 'u', 'v', '4') },
245 { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
246 { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
247 { AV_CODEC_ID_INDEO4, MKTAG('I', 'V', '4', '1') },
248 { AV_CODEC_ID_INDEO5, MKTAG('I', 'V', '5', '0') },
249 { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
250 { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
251 { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
252 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
253 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
254 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
255 { AV_CODEC_ID_VP6F, MKTAG('V', 'P', '6', 'F') },
256 { AV_CODEC_ID_VP6F, MKTAG('F', 'L', 'V', '4') },
257 { AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
258 { AV_CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
259 { AV_CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
260 { AV_CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
261 { AV_CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
262 { AV_CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
263 { AV_CODEC_ID_MIMIC, MKTAG('L', 'M', '2', '0') },
264 { AV_CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') },
265 { AV_CODEC_ID_MSRLE, MKTAG( 1 , 0 , 0 , 0 ) },
266 { AV_CODEC_ID_MSRLE, MKTAG( 2 , 0 , 0 , 0 ) },
267 { AV_CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') },
268 { AV_CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') },
269 { AV_CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') },
270 { AV_CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') },
271 { AV_CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') },
272 { AV_CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') },
273 { AV_CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') },
274 { AV_CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') },
275 { AV_CODEC_ID_TRUEMOTION1, MKTAG('P', 'V', 'E', 'Z') },
276 { AV_CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') },
277 { AV_CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') },
278 { AV_CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
279 { AV_CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
280 { AV_CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
281 { AV_CODEC_ID_FLV1, MKTAG('S', '2', '6', '3') },
282 { AV_CODEC_ID_FLASHSV, MKTAG('F', 'S', 'V', '1') },
283 { AV_CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
284 { AV_CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
285 { AV_CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
286 { AV_CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') },
287 { AV_CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') },
288 { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
289 { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
290 { AV_CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
291 { AV_CODEC_ID_WMV3IMAGE, MKTAG('W', 'M', 'V', 'P') },
292 { AV_CODEC_ID_VC1, MKTAG('W', 'V', 'C', '1') },
293 { AV_CODEC_ID_VC1, MKTAG('W', 'M', 'V', 'A') },
294 { AV_CODEC_ID_VC1IMAGE, MKTAG('W', 'V', 'P', '2') },
295 { AV_CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
296 { AV_CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') },
297 { AV_CODEC_ID_WNV1, MKTAG('Y', 'U', 'V', '8') },
298 { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', '4') },
299 { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') },
300 { AV_CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') },
301 { AV_CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') },
302 { AV_CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') },
303 { AV_CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') },
304 { AV_CODEC_ID_CSCD, MKTAG('C', 'S', 'C', 'D') },
305 { AV_CODEC_ID_ZMBV, MKTAG('Z', 'M', 'B', 'V') },
306 { AV_CODEC_ID_KMVC, MKTAG('K', 'M', 'V', 'C') },
307 { AV_CODEC_ID_CAVS, MKTAG('C', 'A', 'V', 'S') },
308 { AV_CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') },
309 { AV_CODEC_ID_JPEG2000, MKTAG('M', 'J', '2', 'C') },
310 { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'C') },
311 { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'K') },
312 { AV_CODEC_ID_JPEG2000, MKTAG('I', 'P', 'J', '2') },
313 { AV_CODEC_ID_VMNC, MKTAG('V', 'M', 'n', 'c') },
314 { AV_CODEC_ID_TARGA, MKTAG('t', 'g', 'a', ' ') },
315 { AV_CODEC_ID_PNG, MKTAG('M', 'P', 'N', 'G') },
316 { AV_CODEC_ID_PNG, MKTAG('P', 'N', 'G', '1') },
317 { AV_CODEC_ID_CLJR, MKTAG('C', 'L', 'J', 'R') },
318 { AV_CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') },
319 { AV_CODEC_ID_RPZA, MKTAG('a', 'z', 'p', 'r') },
320 { AV_CODEC_ID_RPZA, MKTAG('R', 'P', 'Z', 'A') },
321 { AV_CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') },
322 { AV_CODEC_ID_SP5X, MKTAG('S', 'P', '5', '4') },
323 { AV_CODEC_ID_AURA, MKTAG('A', 'U', 'R', 'A') },
324 { AV_CODEC_ID_AURA2, MKTAG('A', 'U', 'R', '2') },
325 { AV_CODEC_ID_DPX, MKTAG('d', 'p', 'x', ' ') },
326 { AV_CODEC_ID_KGV1, MKTAG('K', 'G', 'V', '1') },
327 { AV_CODEC_ID_LAGARITH, MKTAG('L', 'A', 'G', 'S') },
328 { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '2') },
329 { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '3') },
330 { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '4') },
331 { AV_CODEC_ID_AMV, MKTAG('A', 'M', 'V', 'F') },
332 { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'A') },
333 { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'G') },
334 { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '0') },
335 { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '2') },
336 { AV_CODEC_ID_VBLE, MKTAG('V', 'B', 'L', 'E') },
337 { AV_CODEC_ID_ESCAPE130, MKTAG('E', '1', '3', '0') },
338 { AV_CODEC_ID_DXTORY, MKTAG('x', 't', 'o', 'r') },
339 { AV_CODEC_ID_ZEROCODEC, MKTAG('Z', 'E', 'C', 'O') },
340 { AV_CODEC_ID_Y41P, MKTAG('Y', '4', '1', 'P') },
341 { AV_CODEC_ID_FLIC, MKTAG('A', 'F', 'L', 'C') },
342 { AV_CODEC_ID_EXR, MKTAG('e', 'x', 'r', ' ') },
343 { AV_CODEC_ID_MSS1, MKTAG('M', 'S', 'S', '1') },
344 { AV_CODEC_ID_MSA1, MKTAG('M', 'S', 'A', '1') },
345 { AV_CODEC_ID_TSCC2, MKTAG('T', 'S', 'C', '2') },
346 { AV_CODEC_ID_MTS2, MKTAG('M', 'T', 'S', '2') },
347 { AV_CODEC_ID_CLLC, MKTAG('C', 'L', 'L', 'C') },
348 { AV_CODEC_ID_MSS2, MKTAG('M', 'S', 'S', '2') },
349 { AV_CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
350 { AV_CODEC_ID_012V, MKTAG('0', '1', '2', 'v') },
351 { AV_CODEC_ID_012V, MKTAG('a', '1', '2', 'v') },
352 { AV_CODEC_ID_NONE, 0 }
355 const AVCodecTag ff_codec_wav_tags[] = {
356 { AV_CODEC_ID_PCM_S16LE, 0x0001 },
357 /* must come after s16le in this list */
358 { AV_CODEC_ID_PCM_U8, 0x0001 },
359 { AV_CODEC_ID_PCM_S24LE, 0x0001 },
360 { AV_CODEC_ID_PCM_S32LE, 0x0001 },
361 { AV_CODEC_ID_ADPCM_MS, 0x0002 },
362 { AV_CODEC_ID_PCM_F32LE, 0x0003 },
363 /* must come after f32le in this list */
364 { AV_CODEC_ID_PCM_F64LE, 0x0003 },
365 { AV_CODEC_ID_PCM_ALAW, 0x0006 },
366 { AV_CODEC_ID_PCM_MULAW, 0x0007 },
367 { AV_CODEC_ID_WMAVOICE, 0x000A },
368 { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0010 },
369 { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0011 },
370 /* must come after adpcm_ima_wav in this list */
371 { AV_CODEC_ID_PCM_ZORK, 0x0011 },
372 { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0017 },
373 { AV_CODEC_ID_ADPCM_YAMAHA, 0x0020 },
374 { AV_CODEC_ID_TRUESPEECH, 0x0022 },
375 { AV_CODEC_ID_GSM_MS, 0x0031 },
376 { AV_CODEC_ID_AMR_NB, 0x0038 }, /* rogue format number */
377 { AV_CODEC_ID_G723_1, 0x0042 },
378 { AV_CODEC_ID_ADPCM_G726, 0x0045 },
379 { AV_CODEC_ID_MP2, 0x0050 },
380 { AV_CODEC_ID_MP3, 0x0055 },
381 { AV_CODEC_ID_AMR_NB, 0x0057 },
382 { AV_CODEC_ID_AMR_WB, 0x0058 },
383 /* rogue format number */
384 { AV_CODEC_ID_ADPCM_IMA_DK4, 0x0061 },
385 /* rogue format number */
386 { AV_CODEC_ID_ADPCM_IMA_DK3, 0x0062 },
387 { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0069 },
388 { AV_CODEC_ID_VOXWARE, 0x0075 },
389 { AV_CODEC_ID_AAC, 0x00ff },
390 { AV_CODEC_ID_SIPR, 0x0130 },
391 { AV_CODEC_ID_WMAV1, 0x0160 },
392 { AV_CODEC_ID_WMAV2, 0x0161 },
393 { AV_CODEC_ID_WMAPRO, 0x0162 },
394 { AV_CODEC_ID_WMALOSSLESS, 0x0163 },
395 { AV_CODEC_ID_ADPCM_CT, 0x0200 },
396 { AV_CODEC_ID_ATRAC3, 0x0270 },
397 { AV_CODEC_ID_ADPCM_G722, 0x028F },
398 { AV_CODEC_ID_IMC, 0x0401 },
399 { AV_CODEC_ID_IAC, 0x0402 },
400 { AV_CODEC_ID_GSM_MS, 0x1500 },
401 { AV_CODEC_ID_TRUESPEECH, 0x1501 },
403 { AV_CODEC_ID_AAC, 0x1600 },
404 { AV_CODEC_ID_AAC_LATM, 0x1602 },
405 { AV_CODEC_ID_AC3, 0x2000 },
406 { AV_CODEC_ID_DTS, 0x2001 },
407 { AV_CODEC_ID_SONIC, 0x2048 },
408 { AV_CODEC_ID_SONIC_LS, 0x2048 },
409 { AV_CODEC_ID_PCM_MULAW, 0x6c75 },
410 { AV_CODEC_ID_AAC, 0x706d },
411 { AV_CODEC_ID_AAC, 0x4143 },
412 { AV_CODEC_ID_G723_1, 0xA100 },
413 { AV_CODEC_ID_AAC, 0xA106 },
414 { AV_CODEC_ID_SPEEX, 0xA109 },
415 { AV_CODEC_ID_FLAC, 0xF1AC },
416 { AV_CODEC_ID_ADPCM_SWF, ('S' << 8) + 'F' },
417 /* HACK/FIXME: Does Vorbis in WAV/AVI have an (in)official ID? */
418 { AV_CODEC_ID_VORBIS, ('V' << 8) + 'o' },
419 { AV_CODEC_ID_NONE, 0 },
422 const AVCodecGuid ff_codec_wav_guids[] = {
423 { AV_CODEC_ID_AC3, { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
424 { AV_CODEC_ID_ATRAC3P, { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } },
425 { AV_CODEC_ID_EAC3, { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
426 { AV_CODEC_ID_MP2, { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
430 const AVMetadataConv ff_riff_info_conv[] = {
431 { "IART", "artist" },
432 { "ICMT", "comment" },
433 { "ICOP", "copyright" },
436 { "ILNG", "language" },
440 { "ISFT", "encoder" },
441 { "ISMP", "timecode" },
442 { "ITCH", "encoded_by" },
446 void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
448 av_assert0(sizeof(*g) == 16); //compiler will optimize this out
449 if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
450 memset(*g, 0, sizeof(*g));
453 enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
456 for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
457 if (!ff_guidcmp(guids[i].guid, guid))
459 return AV_CODEC_ID_NONE;
463 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
465 ffio_wfourcc(pb, tag);
467 return avio_tell(pb);
470 void ff_end_tag(AVIOContext *pb, int64_t start)
474 av_assert0((start&1) == 0);
479 avio_seek(pb, start - 4, SEEK_SET);
480 avio_wl32(pb, (uint32_t)(pos - start));
481 avio_seek(pb, FFALIGN(pos, 2), SEEK_SET);
484 /* WAVEFORMATEX header */
485 /* returns the size or -1 on error */
486 int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
488 int bps, blkalign, bytespersec, frame_size;
490 int waveformatextensible;
492 uint8_t *riff_extradata = temp;
493 uint8_t *riff_extradata_start = temp;
495 if (!enc->codec_tag || enc->codec_tag > 0xffff)
498 /* We use the known constant frame size for the codec if known, otherwise
499 * fall back on using AVCodecContext.frame_size, which is not as reliable
500 * for indicating packet duration. */
501 frame_size = av_get_audio_frame_duration(enc, 0);
503 frame_size = enc->frame_size;
505 waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
506 enc->sample_rate > 48000 ||
507 av_get_bits_per_sample(enc->codec_id) > 16;
509 if (waveformatextensible)
510 avio_wl16(pb, 0xfffe);
512 avio_wl16(pb, enc->codec_tag);
514 avio_wl16(pb, enc->channels);
515 avio_wl32(pb, enc->sample_rate);
516 if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
517 enc->codec_id == AV_CODEC_ID_G723_1 ||
518 enc->codec_id == AV_CODEC_ID_MP2 ||
519 enc->codec_id == AV_CODEC_ID_MP3 ||
520 enc->codec_id == AV_CODEC_ID_GSM_MS) {
523 if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
524 if (enc->bits_per_coded_sample)
525 bps = enc->bits_per_coded_sample;
527 bps = 16; // default to 16
530 if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
531 av_log(enc, AV_LOG_WARNING,
532 "requested bits_per_coded_sample (%d) "
533 "and actually stored (%d) differ\n",
534 enc->bits_per_coded_sample, bps);
537 if (enc->codec_id == AV_CODEC_ID_MP2 ||
538 enc->codec_id == AV_CODEC_ID_MP3) {
539 /* This is wrong, but it seems many demuxers do not work if this
540 * is set correctly. */
541 blkalign = frame_size;
542 // blkalign = 144 * enc->bit_rate/enc->sample_rate;
543 } else if (enc->codec_id == AV_CODEC_ID_AC3) {
544 blkalign = 3840; /* maximum bytes per frame */
545 } else if (enc->codec_id == AV_CODEC_ID_AAC) {
546 blkalign = 768 * enc->channels; /* maximum bytes per frame */
547 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
549 } else if (enc->block_align != 0) { /* specified by the codec */
550 blkalign = enc->block_align;
552 blkalign = bps * enc->channels / av_gcd(8, bps);
553 if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
554 enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
555 enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
556 enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
557 enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
558 enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
559 bytespersec = enc->sample_rate * blkalign;
560 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
563 bytespersec = enc->bit_rate / 8;
565 avio_wl32(pb, bytespersec); /* bytes per second */
566 avio_wl16(pb, blkalign); /* block align */
567 avio_wl16(pb, bps); /* bits per sample */
568 if (enc->codec_id == AV_CODEC_ID_MP3) {
570 bytestream_put_le16(&riff_extradata, 1); /* wID */
571 bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
572 bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
573 bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
574 bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
575 } else if (enc->codec_id == AV_CODEC_ID_MP2) {
578 bytestream_put_le16(&riff_extradata, 2);
580 bytestream_put_le32(&riff_extradata, enc->bit_rate);
582 bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
584 bytestream_put_le16(&riff_extradata, 0);
586 bytestream_put_le16(&riff_extradata, 1);
588 bytestream_put_le16(&riff_extradata, 16);
590 bytestream_put_le32(&riff_extradata, 0);
592 bytestream_put_le32(&riff_extradata, 0);
593 } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
595 bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
596 bytestream_put_le32(&riff_extradata, 0xaea2f732);
597 bytestream_put_le16(&riff_extradata, 0xacde);
598 } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
599 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
601 /* wSamplesPerBlock */
602 bytestream_put_le16(&riff_extradata, frame_size);
603 } else if (enc->extradata_size) {
604 riff_extradata_start = enc->extradata;
605 riff_extradata = enc->extradata + enc->extradata_size;
606 hdrsize += enc->extradata_size;
608 /* write WAVEFORMATEXTENSIBLE extensions */
609 if (waveformatextensible) {
611 /* 22 is WAVEFORMATEXTENSIBLE size */
612 avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
613 /* ValidBitsPerSample || SamplesPerBlock || Reserved */
616 avio_wl32(pb, enc->channel_layout);
618 avio_wl32(pb, enc->codec_tag);
619 avio_wl32(pb, 0x00100000);
620 avio_wl32(pb, 0xAA000080);
621 avio_wl32(pb, 0x719B3800);
623 avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
625 avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
634 /* BITMAPINFOHEADER header */
635 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
636 const AVCodecTag *tags, int for_asf)
639 avio_wl32(pb, 40 + enc->extradata_size);
640 avio_wl32(pb, enc->width);
641 //We always store RGB TopDown
642 avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
646 avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
647 /* compression type */
648 avio_wl32(pb, enc->codec_tag);
649 avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
655 avio_write(pb, enc->extradata, enc->extradata_size);
657 if (!for_asf && enc->extradata_size & 1)
661 void ff_parse_specific_params(AVCodecContext *stream, int *au_rate,
662 int *au_ssize, int *au_scale)
665 int audio_frame_size;
667 /* We use the known constant frame size for the codec if known, otherwise
668 * fall back on using AVCodecContext.frame_size, which is not as reliable
669 * for indicating packet duration. */
670 audio_frame_size = av_get_audio_frame_duration(stream, 0);
671 if (!audio_frame_size)
672 audio_frame_size = stream->frame_size;
674 *au_ssize = stream->block_align;
675 if (audio_frame_size && stream->sample_rate) {
676 *au_scale = audio_frame_size;
677 *au_rate = stream->sample_rate;
678 } else if (stream->codec_type == AVMEDIA_TYPE_VIDEO ||
679 stream->codec_type == AVMEDIA_TYPE_DATA ||
680 stream->codec_type == AVMEDIA_TYPE_SUBTITLE) {
681 *au_scale = stream->time_base.num;
682 *au_rate = stream->time_base.den;
684 *au_scale = stream->block_align ? stream->block_align * 8 : 8;
685 *au_rate = stream->bit_rate ? stream->bit_rate :
686 8 * stream->sample_rate;
688 gcd = av_gcd(*au_scale, *au_rate);
693 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
695 int len = strlen(str);
698 ffio_wfourcc(pb, tag);
700 avio_put_str(pb, str);
706 static const char riff_tags[][5] = {
707 "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
708 "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
709 "IPRT", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
713 static int riff_has_valid_tags(AVFormatContext *s)
717 for (i = 0; *riff_tags[i]; i++)
718 if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
724 void ff_riff_write_info(AVFormatContext *s)
726 AVIOContext *pb = s->pb;
729 AVDictionaryEntry *t = NULL;
731 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
733 /* writing empty LIST is not nice and may cause problems */
734 if (!riff_has_valid_tags(s))
737 list_pos = ff_start_tag(pb, "LIST");
738 ffio_wfourcc(pb, "INFO");
739 for (i = 0; *riff_tags[i]; i++)
740 if ((t = av_dict_get(s->metadata, riff_tags[i],
741 NULL, AV_DICT_MATCH_CASE)))
742 ff_riff_write_info_tag(s->pb, t->key, t->value);
743 ff_end_tag(pb, list_pos);
745 #endif /* CONFIG_MUXERS */
748 /* We could be given one of the three possible structures here:
749 * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
750 * is an expansion of the previous one with the fields added
751 * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
752 * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
753 * an openended structure.
756 static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
758 ff_asf_guid subformat;
759 int bps = avio_rl16(pb);
761 c->bits_per_coded_sample = bps;
763 c->channel_layout = avio_rl32(pb); /* dwChannelMask */
765 ff_get_guid(pb, &subformat);
766 if (!memcmp(subformat + 4,
767 (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) {
768 c->codec_tag = AV_RL32(subformat);
769 c->codec_id = ff_wav_codec_get_id(c->codec_tag,
770 c->bits_per_coded_sample);
772 c->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
774 av_log(c, AV_LOG_WARNING,
775 "unknown subformat:"FF_PRI_GUID"\n",
776 FF_ARG_GUID(subformat));
780 int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
785 codec->codec_type = AVMEDIA_TYPE_AUDIO;
786 codec->channels = avio_rl16(pb);
787 codec->sample_rate = avio_rl32(pb);
788 codec->bit_rate = avio_rl32(pb) * 8;
789 codec->block_align = avio_rl16(pb);
790 if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
791 codec->bits_per_coded_sample = 8;
793 codec->bits_per_coded_sample = avio_rl16(pb);
795 codec->codec_tag = 0;
797 codec->codec_tag = id;
798 codec->codec_id = ff_wav_codec_get_id(id,
799 codec->bits_per_coded_sample);
801 if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
802 int cbSize = avio_rl16(pb); /* cbSize */
804 cbSize = FFMIN(size, cbSize);
805 if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
806 parse_waveformatex(pb, codec);
810 codec->extradata_size = cbSize;
812 av_free(codec->extradata);
813 codec->extradata = av_mallocz(codec->extradata_size +
814 FF_INPUT_BUFFER_PADDING_SIZE);
815 if (!codec->extradata)
816 return AVERROR(ENOMEM);
817 avio_read(pb, codec->extradata, codec->extradata_size);
821 /* It is possible for the chunk to contain garbage at the end */
825 if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
826 /* Channels and sample_rate values are those prior to applying SBR
829 codec->sample_rate = 0;
831 /* override bits_per_coded_sample for G.726 */
832 if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
833 codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
838 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
841 id = ff_codec_get_id(ff_codec_wav_tags, tag);
845 if (id == AV_CODEC_ID_PCM_S16LE)
846 id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
847 else if (id == AV_CODEC_ID_PCM_F32LE)
848 id = ff_get_pcm_codec_id(bps, 1, 0, 0);
850 if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
851 id = AV_CODEC_ID_PCM_ZORK;
855 int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
858 if(esize) *esize = avio_rl32(pb);
860 st->codec->width = avio_rl32(pb);
861 st->codec->height = (int32_t)avio_rl32(pb);
862 avio_rl16(pb); /* planes */
863 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
864 tag1 = avio_rl32(pb);
865 avio_rl32(pb); /* ImageSize */
866 avio_rl32(pb); /* XPelsPerMeter */
867 avio_rl32(pb); /* YPelsPerMeter */
868 avio_rl32(pb); /* ClrUsed */
869 avio_rl32(pb); /* ClrImportant */
873 int ff_read_riff_info(AVFormatContext *s, int64_t size)
875 int64_t start, end, cur;
876 AVIOContext *pb = s->pb;
878 start = avio_tell(pb);
881 while ((cur = avio_tell(pb)) >= 0 &&
882 cur <= end - 8 /* = tag + size */) {
888 chunk_code = avio_rl32(pb);
889 chunk_size = avio_rl32(pb);
891 if (chunk_code || chunk_size) {
892 av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
893 return AVERROR_INVALIDDATA;
897 if (chunk_size > end ||
898 end - chunk_size < cur ||
899 chunk_size == UINT_MAX) {
900 avio_seek(pb, -9, SEEK_CUR);
901 chunk_code = avio_rl32(pb);
902 chunk_size = avio_rl32(pb);
903 if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
904 av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
905 return AVERROR_INVALIDDATA;
909 chunk_size += (chunk_size & 1);
913 avio_skip(pb, chunk_size);
914 else if (pb->eof_reached) {
915 av_log(s, AV_LOG_WARNING, "truncated file\n");
921 value = av_mallocz(chunk_size + 1);
923 av_log(s, AV_LOG_ERROR,
924 "out of memory, unable to read INFO tag\n");
925 return AVERROR(ENOMEM);
928 AV_WL32(key, chunk_code);
930 if (avio_read(pb, value, chunk_size) != chunk_size) {
931 av_log(s, AV_LOG_WARNING,
932 "premature end of file while reading INFO tag\n");
935 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
940 #endif /* CONFIG_DEMUXERS */