FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
riff.c
Go to the documentation of this file.
1 /*
2  * RIFF common 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/mathematics.h"
23 #include "libavcodec/avcodec.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "riff.h"
27 #include "libavcodec/bytestream.h"
28 #include "libavutil/avassert.h"
29 
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.
34  */
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') },
88  /* flipped video */
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') },
93  /* Ephv MPEG-4 */
94  { AV_CODEC_ID_MPEG4, MKTAG('E', 'P', 'H', 'V') },
95  { AV_CODEC_ID_MPEG4, MKTAG('E', 'M', '4', 'A') },
96  /* Divio MPEG-4 */
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') },
132  /* Canopus DV */
133  { AV_CODEC_ID_DVVIDEO, MKTAG('c', 'd', 'v', 'c') },
134  /* Canopus DV */
135  { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', 'H') },
136  /* Canopus DV */
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') },
183  /* SL M-JPEG */
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_AMV, MKTAG('A', 'M', 'V', 'F') },
329  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'A') },
330  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'G') },
331  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '0') },
332  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '2') },
333  /* Ut Video version 13.0.1 BT.709 codecs */
334  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'H', '0') },
335  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'H', '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_MSS1, MKTAG('M', 'S', 'S', '1') },
343  { AV_CODEC_ID_MSA1, MKTAG('M', 'S', 'A', '1') },
344  { AV_CODEC_ID_TSCC2, MKTAG('T', 'S', 'C', '2') },
345  { AV_CODEC_ID_MTS2, MKTAG('M', 'T', 'S', '2') },
346  { AV_CODEC_ID_CLLC, MKTAG('C', 'L', 'L', 'C') },
347  { AV_CODEC_ID_MSS2, MKTAG('M', 'S', 'S', '2') },
348  { AV_CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
349  { AV_CODEC_ID_012V, MKTAG('0', '1', '2', 'v') },
350  { AV_CODEC_ID_012V, MKTAG('a', '1', '2', 'v') },
351  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '2') },
352  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '3') },
353  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '4') },
354  { AV_CODEC_ID_NONE, 0 }
355 };
356 
358  { AV_CODEC_ID_PCM_S16LE, 0x0001 },
359  /* must come after s16le in this list */
360  { AV_CODEC_ID_PCM_U8, 0x0001 },
361  { AV_CODEC_ID_PCM_S24LE, 0x0001 },
362  { AV_CODEC_ID_PCM_S32LE, 0x0001 },
363  { AV_CODEC_ID_ADPCM_MS, 0x0002 },
364  { AV_CODEC_ID_PCM_F32LE, 0x0003 },
365  /* must come after f32le in this list */
366  { AV_CODEC_ID_PCM_F64LE, 0x0003 },
367  { AV_CODEC_ID_PCM_ALAW, 0x0006 },
368  { AV_CODEC_ID_PCM_MULAW, 0x0007 },
369  { AV_CODEC_ID_WMAVOICE, 0x000A },
370  { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0010 },
371  { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0011 },
372  /* must come after adpcm_ima_wav in this list */
373  { AV_CODEC_ID_PCM_ZORK, 0x0011 },
374  { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0017 },
375  { AV_CODEC_ID_ADPCM_YAMAHA, 0x0020 },
376  { AV_CODEC_ID_TRUESPEECH, 0x0022 },
377  { AV_CODEC_ID_GSM_MS, 0x0031 },
378  { AV_CODEC_ID_AMR_NB, 0x0038 }, /* rogue format number */
379  { AV_CODEC_ID_G723_1, 0x0042 },
380  { AV_CODEC_ID_ADPCM_G726, 0x0045 },
381  { AV_CODEC_ID_MP2, 0x0050 },
382  { AV_CODEC_ID_MP3, 0x0055 },
383  { AV_CODEC_ID_AMR_NB, 0x0057 },
384  { AV_CODEC_ID_AMR_WB, 0x0058 },
385  /* rogue format number */
386  { AV_CODEC_ID_ADPCM_IMA_DK4, 0x0061 },
387  /* rogue format number */
388  { AV_CODEC_ID_ADPCM_IMA_DK3, 0x0062 },
389  { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0069 },
390  { AV_CODEC_ID_VOXWARE, 0x0075 },
391  { AV_CODEC_ID_AAC, 0x00ff },
392  { AV_CODEC_ID_SIPR, 0x0130 },
393  { AV_CODEC_ID_WMAV1, 0x0160 },
394  { AV_CODEC_ID_WMAV2, 0x0161 },
395  { AV_CODEC_ID_WMAPRO, 0x0162 },
396  { AV_CODEC_ID_WMALOSSLESS, 0x0163 },
397  { AV_CODEC_ID_ADPCM_CT, 0x0200 },
398  { AV_CODEC_ID_ATRAC3, 0x0270 },
399  { AV_CODEC_ID_ADPCM_G722, 0x028F },
400  { AV_CODEC_ID_IMC, 0x0401 },
401  { AV_CODEC_ID_IAC, 0x0402 },
402  { AV_CODEC_ID_GSM_MS, 0x1500 },
403  { AV_CODEC_ID_TRUESPEECH, 0x1501 },
404  /* ADTS AAC */
405  { AV_CODEC_ID_AAC, 0x1600 },
406  { AV_CODEC_ID_AAC_LATM, 0x1602 },
407  { AV_CODEC_ID_AC3, 0x2000 },
408  { AV_CODEC_ID_DTS, 0x2001 },
409  { AV_CODEC_ID_SONIC, 0x2048 },
410  { AV_CODEC_ID_SONIC_LS, 0x2048 },
411  { AV_CODEC_ID_PCM_MULAW, 0x6c75 },
412  { AV_CODEC_ID_AAC, 0x706d },
413  { AV_CODEC_ID_AAC, 0x4143 },
414  { AV_CODEC_ID_G723_1, 0xA100 },
415  { AV_CODEC_ID_AAC, 0xA106 },
416  { AV_CODEC_ID_SPEEX, 0xA109 },
417  { AV_CODEC_ID_FLAC, 0xF1AC },
418  { AV_CODEC_ID_ADPCM_SWF, ('S' << 8) + 'F' },
419  /* HACK/FIXME: Does Vorbis in WAV/AVI have an (in)official ID? */
420  { AV_CODEC_ID_VORBIS, ('V' << 8) + 'o' },
421  { AV_CODEC_ID_NONE, 0 },
422 };
423 
425  { AV_CODEC_ID_AC3, { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
426  { AV_CODEC_ID_ATRAC3P, { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } },
427  { AV_CODEC_ID_EAC3, { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
428  { AV_CODEC_ID_MP2, { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
429  { AV_CODEC_ID_NONE }
430 };
431 
433  { "IART", "artist" },
434  { "ICMT", "comment" },
435  { "ICOP", "copyright" },
436  { "ICRD", "date" },
437  { "IGNR", "genre" },
438  { "ILNG", "language" },
439  { "INAM", "title" },
440  { "IPRD", "album" },
441  { "IPRT", "track" },
442  { "ISFT", "encoder" },
443  { "ISMP", "timecode" },
444  { "ITCH", "encoded_by" },
445  { 0 },
446 };
447 
449 {
450  av_assert0(sizeof(*g) == 16); //compiler will optimize this out
451  if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
452  memset(*g, 0, sizeof(*g));
453 }
454 
456 {
457  int i;
458  for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
459  if (!ff_guidcmp(guids[i].guid, guid))
460  return guids[i].id;
461  return AV_CODEC_ID_NONE;
462 }
463 
465 {
466  return ff_codec_bmp_tags;
467 }
468 
470 {
471  return ff_codec_wav_tags;
472 }
473 
474 #if CONFIG_MUXERS
475 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
476 {
477  ffio_wfourcc(pb, tag);
478  avio_wl32(pb, 0);
479  return avio_tell(pb);
480 }
481 
482 void ff_end_tag(AVIOContext *pb, int64_t start)
483 {
484  int64_t pos;
485 
486  av_assert0((start&1) == 0);
487 
488  pos = avio_tell(pb);
489  if (pos & 1)
490  avio_w8(pb, 0);
491  avio_seek(pb, start - 4, SEEK_SET);
492  avio_wl32(pb, (uint32_t)(pos - start));
493  avio_seek(pb, FFALIGN(pos, 2), SEEK_SET);
494 }
495 
496 /* WAVEFORMATEX header */
497 /* returns the size or -1 on error */
499 {
500  int bps, blkalign, bytespersec, frame_size;
501  int hdrsize = 18;
502  int waveformatextensible;
503  uint8_t temp[256];
504  uint8_t *riff_extradata = temp;
505  uint8_t *riff_extradata_start = temp;
506 
507  if (!enc->codec_tag || enc->codec_tag > 0xffff)
508  return -1;
509 
510  /* We use the known constant frame size for the codec if known, otherwise
511  * fall back on using AVCodecContext.frame_size, which is not as reliable
512  * for indicating packet duration. */
513  frame_size = av_get_audio_frame_duration(enc, 0);
514  if (!frame_size)
515  frame_size = enc->frame_size;
516 
517  waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
518  enc->sample_rate > 48000 ||
519  av_get_bits_per_sample(enc->codec_id) > 16;
520 
521  if (waveformatextensible)
522  avio_wl16(pb, 0xfffe);
523  else
524  avio_wl16(pb, enc->codec_tag);
525 
526  avio_wl16(pb, enc->channels);
527  avio_wl32(pb, enc->sample_rate);
528  if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
529  enc->codec_id == AV_CODEC_ID_G723_1 ||
530  enc->codec_id == AV_CODEC_ID_MP2 ||
531  enc->codec_id == AV_CODEC_ID_MP3 ||
532  enc->codec_id == AV_CODEC_ID_GSM_MS) {
533  bps = 0;
534  } else {
535  if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
536  if (enc->bits_per_coded_sample)
537  bps = enc->bits_per_coded_sample;
538  else
539  bps = 16; // default to 16
540  }
541  }
542  if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
543  av_log(enc, AV_LOG_WARNING,
544  "requested bits_per_coded_sample (%d) "
545  "and actually stored (%d) differ\n",
546  enc->bits_per_coded_sample, bps);
547  }
548 
549  if (enc->codec_id == AV_CODEC_ID_MP2 ||
550  enc->codec_id == AV_CODEC_ID_MP3) {
551  /* This is wrong, but it seems many demuxers do not work if this
552  * is set correctly. */
553  blkalign = frame_size;
554  // blkalign = 144 * enc->bit_rate/enc->sample_rate;
555  } else if (enc->codec_id == AV_CODEC_ID_AC3) {
556  blkalign = 3840; /* maximum bytes per frame */
557  } else if (enc->codec_id == AV_CODEC_ID_AAC) {
558  blkalign = 768 * enc->channels; /* maximum bytes per frame */
559  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
560  blkalign = 24;
561  } else if (enc->block_align != 0) { /* specified by the codec */
562  blkalign = enc->block_align;
563  } else
564  blkalign = bps * enc->channels / av_gcd(8, bps);
565  if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
571  bytespersec = enc->sample_rate * blkalign;
572  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
573  bytespersec = 800;
574  } else {
575  bytespersec = enc->bit_rate / 8;
576  }
577  avio_wl32(pb, bytespersec); /* bytes per second */
578  avio_wl16(pb, blkalign); /* block align */
579  avio_wl16(pb, bps); /* bits per sample */
580  if (enc->codec_id == AV_CODEC_ID_MP3) {
581  hdrsize += 12;
582  bytestream_put_le16(&riff_extradata, 1); /* wID */
583  bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
584  bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
585  bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
586  bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
587  } else if (enc->codec_id == AV_CODEC_ID_MP2) {
588  hdrsize += 22;
589  /* fwHeadLayer */
590  bytestream_put_le16(&riff_extradata, 2);
591  /* dwHeadBitrate */
592  bytestream_put_le32(&riff_extradata, enc->bit_rate);
593  /* fwHeadMode */
594  bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
595  /* fwHeadModeExt */
596  bytestream_put_le16(&riff_extradata, 0);
597  /* wHeadEmphasis */
598  bytestream_put_le16(&riff_extradata, 1);
599  /* fwHeadFlags */
600  bytestream_put_le16(&riff_extradata, 16);
601  /* dwPTSLow */
602  bytestream_put_le32(&riff_extradata, 0);
603  /* dwPTSHigh */
604  bytestream_put_le32(&riff_extradata, 0);
605  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
606  hdrsize += 20;
607  bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
608  bytestream_put_le32(&riff_extradata, 0xaea2f732);
609  bytestream_put_le16(&riff_extradata, 0xacde);
610  } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
612  hdrsize += 2;
613  /* wSamplesPerBlock */
614  bytestream_put_le16(&riff_extradata, frame_size);
615  } else if (enc->extradata_size) {
616  riff_extradata_start = enc->extradata;
617  riff_extradata = enc->extradata + enc->extradata_size;
618  hdrsize += enc->extradata_size;
619  }
620  /* write WAVEFORMATEXTENSIBLE extensions */
621  if (waveformatextensible) {
622  hdrsize += 22;
623  /* 22 is WAVEFORMATEXTENSIBLE size */
624  avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
625  /* ValidBitsPerSample || SamplesPerBlock || Reserved */
626  avio_wl16(pb, bps);
627  /* dwChannelMask */
628  avio_wl32(pb, enc->channel_layout);
629  /* GUID + next 3 */
630  avio_wl32(pb, enc->codec_tag);
631  avio_wl32(pb, 0x00100000);
632  avio_wl32(pb, 0xAA000080);
633  avio_wl32(pb, 0x719B3800);
634  } else {
635  avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
636  }
637  avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
638  if (hdrsize & 1) {
639  hdrsize++;
640  avio_w8(pb, 0);
641  }
642 
643  return hdrsize;
644 }
645 
646 /* BITMAPINFOHEADER header */
648  const AVCodecTag *tags, int for_asf)
649 {
650  /* size */
651  avio_wl32(pb, 40 + enc->extradata_size);
652  avio_wl32(pb, enc->width);
653  //We always store RGB TopDown
654  avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
655  /* planes */
656  avio_wl16(pb, 1);
657  /* depth */
659  /* compression type */
660  avio_wl32(pb, enc->codec_tag);
661  avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
662  avio_wl32(pb, 0);
663  avio_wl32(pb, 0);
664  avio_wl32(pb, 0);
665  avio_wl32(pb, 0);
666 
667  avio_write(pb, enc->extradata, enc->extradata_size);
668 
669  if (!for_asf && enc->extradata_size & 1)
670  avio_w8(pb, 0);
671 }
672 
673 void ff_parse_specific_params(AVCodecContext *stream, int *au_rate,
674  int *au_ssize, int *au_scale)
675 {
676  int gcd;
677  int audio_frame_size;
678 
679  /* We use the known constant frame size for the codec if known, otherwise
680  * fall back on using AVCodecContext.frame_size, which is not as reliable
681  * for indicating packet duration. */
682  audio_frame_size = av_get_audio_frame_duration(stream, 0);
683  if (!audio_frame_size)
684  audio_frame_size = stream->frame_size;
685 
686  *au_ssize = stream->block_align;
687  if (audio_frame_size && stream->sample_rate) {
688  *au_scale = audio_frame_size;
689  *au_rate = stream->sample_rate;
690  } else if (stream->codec_type == AVMEDIA_TYPE_VIDEO ||
691  stream->codec_type == AVMEDIA_TYPE_DATA ||
692  stream->codec_type == AVMEDIA_TYPE_SUBTITLE) {
693  *au_scale = stream->time_base.num;
694  *au_rate = stream->time_base.den;
695  } else {
696  *au_scale = stream->block_align ? stream->block_align * 8 : 8;
697  *au_rate = stream->bit_rate ? stream->bit_rate :
698  8 * stream->sample_rate;
699  }
700  gcd = av_gcd(*au_scale, *au_rate);
701  *au_scale /= gcd;
702  *au_rate /= gcd;
703 }
704 
705 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
706 {
707  int len = strlen(str);
708  if (len > 0) {
709  len++;
710  ffio_wfourcc(pb, tag);
711  avio_wl32(pb, len);
712  avio_put_str(pb, str);
713  if (len & 1)
714  avio_w8(pb, 0);
715  }
716 }
717 
718 static const char riff_tags[][5] = {
719  "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
720  "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
721  "IPRT", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
722  { 0 }
723 };
724 
725 static int riff_has_valid_tags(AVFormatContext *s)
726 {
727  int i;
728 
729  for (i = 0; *riff_tags[i]; i++)
730  if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
731  return 1;
732 
733  return 0;
734 }
735 
737 {
738  AVIOContext *pb = s->pb;
739  int i;
740  int64_t list_pos;
741  AVDictionaryEntry *t = NULL;
742 
743  ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
744 
745  /* writing empty LIST is not nice and may cause problems */
746  if (!riff_has_valid_tags(s))
747  return;
748 
749  list_pos = ff_start_tag(pb, "LIST");
750  ffio_wfourcc(pb, "INFO");
751  for (i = 0; *riff_tags[i]; i++)
752  if ((t = av_dict_get(s->metadata, riff_tags[i],
753  NULL, AV_DICT_MATCH_CASE)))
754  ff_riff_write_info_tag(s->pb, t->key, t->value);
755  ff_end_tag(pb, list_pos);
756 }
757 #endif /* CONFIG_MUXERS */
758 
759 #if CONFIG_DEMUXERS
760 /* We could be given one of the three possible structures here:
761  * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
762  * is an expansion of the previous one with the fields added
763  * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
764  * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
765  * an openended structure.
766  */
767 
768 static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
769 {
770  ff_asf_guid subformat;
771  int bps = avio_rl16(pb);
772  if (bps)
774 
775  c->channel_layout = avio_rl32(pb); /* dwChannelMask */
776 
777  ff_get_guid(pb, &subformat);
778  if (!memcmp(subformat + 4,
779  (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) {
780  c->codec_tag = AV_RL32(subformat);
783  } else {
784  c->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
785  if (!c->codec_id)
787  "unknown subformat:"FF_PRI_GUID"\n",
788  FF_ARG_GUID(subformat));
789  }
790 }
791 
792 int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
793 {
794  int id;
795 
796  id = avio_rl16(pb);
798  codec->channels = avio_rl16(pb);
799  codec->sample_rate = avio_rl32(pb);
800  codec->bit_rate = avio_rl32(pb) * 8;
801  codec->block_align = avio_rl16(pb);
802  if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
803  codec->bits_per_coded_sample = 8;
804  } else
805  codec->bits_per_coded_sample = avio_rl16(pb);
806  if (id == 0xFFFE) {
807  codec->codec_tag = 0;
808  } else {
809  codec->codec_tag = id;
810  codec->codec_id = ff_wav_codec_get_id(id,
811  codec->bits_per_coded_sample);
812  }
813  if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
814  int cbSize = avio_rl16(pb); /* cbSize */
815  size -= 18;
816  cbSize = FFMIN(size, cbSize);
817  if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
818  parse_waveformatex(pb, codec);
819  cbSize -= 22;
820  size -= 22;
821  }
822  codec->extradata_size = cbSize;
823  if (cbSize > 0) {
824  av_free(codec->extradata);
825  codec->extradata = av_mallocz(codec->extradata_size +
827  if (!codec->extradata)
828  return AVERROR(ENOMEM);
829  avio_read(pb, codec->extradata, codec->extradata_size);
830  size -= cbSize;
831  }
832 
833  /* It is possible for the chunk to contain garbage at the end */
834  if (size > 0)
835  avio_skip(pb, size);
836  }
837  if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
838  /* Channels and sample_rate values are those prior to applying SBR
839  * and/or PS. */
840  codec->channels = 0;
841  codec->sample_rate = 0;
842  }
843  /* override bits_per_coded_sample for G.726 */
844  if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
845  codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
846 
847  return 0;
848 }
849 
850 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
851 {
852  enum AVCodecID id;
853  id = ff_codec_get_id(ff_codec_wav_tags, tag);
854  if (id <= 0)
855  return id;
856 
857  if (id == AV_CODEC_ID_PCM_S16LE)
858  id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
859  else if (id == AV_CODEC_ID_PCM_F32LE)
860  id = ff_get_pcm_codec_id(bps, 1, 0, 0);
861 
862  if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
864  return id;
865 }
866 
867 int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
868 {
869  int tag1;
870  if(esize) *esize = avio_rl32(pb);
871  else avio_rl32(pb);
872  st->codec->width = avio_rl32(pb);
873  st->codec->height = (int32_t)avio_rl32(pb);
874  avio_rl16(pb); /* planes */
875  st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
876  tag1 = avio_rl32(pb);
877  avio_rl32(pb); /* ImageSize */
878  avio_rl32(pb); /* XPelsPerMeter */
879  avio_rl32(pb); /* YPelsPerMeter */
880  avio_rl32(pb); /* ClrUsed */
881  avio_rl32(pb); /* ClrImportant */
882  return tag1;
883 }
884 
885 int ff_read_riff_info(AVFormatContext *s, int64_t size)
886 {
887  int64_t start, end, cur;
888  AVIOContext *pb = s->pb;
889 
890  start = avio_tell(pb);
891  end = start + size;
892 
893  while ((cur = avio_tell(pb)) >= 0 &&
894  cur <= end - 8 /* = tag + size */) {
895  uint32_t chunk_code;
896  int64_t chunk_size;
897  char key[5] = { 0 };
898  char *value;
899 
900  chunk_code = avio_rl32(pb);
901  chunk_size = avio_rl32(pb);
902  if (url_feof(pb)) {
903  if (chunk_code || chunk_size) {
904  av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
905  return AVERROR_INVALIDDATA;
906  }
907  return AVERROR_EOF;
908  }
909  if (chunk_size > end ||
910  end - chunk_size < cur ||
911  chunk_size == UINT_MAX) {
912  avio_seek(pb, -9, SEEK_CUR);
913  chunk_code = avio_rl32(pb);
914  chunk_size = avio_rl32(pb);
915  if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
916  av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
917  return AVERROR_INVALIDDATA;
918  }
919  }
920 
921  chunk_size += (chunk_size & 1);
922 
923  if (!chunk_code) {
924  if (chunk_size)
925  avio_skip(pb, chunk_size);
926  else if (pb->eof_reached) {
927  av_log(s, AV_LOG_WARNING, "truncated file\n");
928  return AVERROR_EOF;
929  }
930  continue;
931  }
932 
933  value = av_mallocz(chunk_size + 1);
934  if (!value) {
935  av_log(s, AV_LOG_ERROR,
936  "out of memory, unable to read INFO tag\n");
937  return AVERROR(ENOMEM);
938  }
939 
940  AV_WL32(key, chunk_code);
941 
942  if (avio_read(pb, value, chunk_size) != chunk_size) {
944  "premature end of file while reading INFO tag\n");
945  }
946 
947  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
948  }
949 
950  return 0;
951 }
952 #endif /* CONFIG_DEMUXERS */