FFmpeg
 All Data Structures 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 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 h263 */
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  { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 0 ) }, /* some broken avi use this */
65  { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
66  { AV_CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
67  { AV_CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
68  { AV_CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
69  { AV_CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') },
70  { AV_CODEC_ID_MPEG4, MKTAG('S', 'E', 'D', 'G') },
71  { AV_CODEC_ID_MPEG4, MKTAG('R', 'M', 'P', '4') },
72  { AV_CODEC_ID_MPEG4, MKTAG('3', 'I', 'V', '2') },
73  { AV_CODEC_ID_MPEG4, MKTAG('W', 'A', 'W', 'V') }, /* WaWv MPEG-4 Video Codec */
74  { AV_CODEC_ID_MPEG4, MKTAG('F', 'F', 'D', 'S') },
75  { AV_CODEC_ID_MPEG4, MKTAG('F', 'V', 'F', 'W') },
76  { AV_CODEC_ID_MPEG4, MKTAG('D', 'C', 'O', 'D') },
77  { AV_CODEC_ID_MPEG4, MKTAG('M', 'V', 'X', 'M') },
78  { AV_CODEC_ID_MPEG4, MKTAG('P', 'M', '4', 'V') },
79  { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', 'P', '4') },
80  { AV_CODEC_ID_MPEG4, MKTAG('D', 'X', 'G', 'M') },
81  { AV_CODEC_ID_MPEG4, MKTAG('V', 'I', 'D', 'M') },
82  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'T', '3') },
83  { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'X') },
84  { AV_CODEC_ID_MPEG4, MKTAG('H', 'D', 'X', '4') }, /* flipped video */
85  { AV_CODEC_ID_MPEG4, MKTAG('D', 'M', 'K', '2') },
86  { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'G', 'I') },
87  { AV_CODEC_ID_MPEG4, MKTAG('I', 'N', 'M', 'C') },
88  { AV_CODEC_ID_MPEG4, MKTAG('E', 'P', 'H', 'V') }, /* Ephv MPEG-4 */
89  { AV_CODEC_ID_MPEG4, MKTAG('E', 'M', '4', 'A') },
90  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'C', 'C') }, /* Divio MPEG-4 */
91  { AV_CODEC_ID_MPEG4, MKTAG('S', 'N', '4', '0') },
92  { AV_CODEC_ID_MPEG4, MKTAG('V', 'S', 'P', 'X') },
93  { AV_CODEC_ID_MPEG4, MKTAG('U', 'L', 'D', 'X') },
94  { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'V') },
95  { AV_CODEC_ID_MPEG4, MKTAG('S', 'I', 'P', 'P') }, /* Samsung SHR-6040 */
96  { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', '4', 'V') },
97  { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'X') },
98  { AV_CODEC_ID_MPEG4, MKTAG('D', 'r', 'e', 'X') },
99  { AV_CODEC_ID_MPEG4, MKTAG('Q', 'M', 'P', '4') }, /* QNAP Systems */
100  { AV_CODEC_ID_MPEG4, MKTAG('P', 'L', 'V', '1') }, /* Pelco DVR MPEG-4 */
101  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
102  { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') },
103  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
104  { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
105  { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
106  { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
107  { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'V', 'X', '3') },
108  { AV_CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
109  { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
110  { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
111  { AV_CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
112  { AV_CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
113  { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
114  { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', '4', '1') },
115  { AV_CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
116  { AV_CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
117  { AV_CODEC_ID_WMV2, MKTAG('G', 'X', 'V', 'E') },
118  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
119  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
120  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
121  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
122  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
123  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', '0') },
124  { AV_CODEC_ID_DVVIDEO, MKTAG('c', 'd', 'v', 'c') }, /* Canopus DV */
125  { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', 'H') }, /* Canopus DV */
126  { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', '5') }, /* Canopus DV */
127  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
128  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 's') },
129  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
130  { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'i', 's') },
131  { AV_CODEC_ID_DVVIDEO, MKTAG('p', 'd', 'v', 'c') },
132  { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', '2', '5') },
133  { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', 'D', 'V') },
134  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
135  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
136  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') },
137  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'P', 'E', 'G') },
138  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
139  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('P', 'I', 'M', '2') },
140  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
141  { AV_CODEC_ID_MPEG1VIDEO, MKTAG( 1 , 0 , 0 , 16) },
142  { AV_CODEC_ID_MPEG2VIDEO, MKTAG( 2 , 0 , 0 , 16) },
143  { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 16) },
144  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') },
145  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'M', 'E', 'S') },
146  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('L', 'M', 'P', '2') }, /* Lead MPEG2 in avi */
147  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('s', 'l', 'i', 'f') },
148  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('E', 'M', '2', 'V') },
149  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', '7', '0', '1') }, /* Matrox MPEG2 intra-only */
150  { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', 'v') },
151  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('B', 'W', '1', '0') },
152  { AV_CODEC_ID_MPEG1VIDEO, MKTAG('X', 'M', 'P', 'G') }, /* Xing MPEG intra only */
153  { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
154  { AV_CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
155  { AV_CODEC_ID_MJPEG, MKTAG('d', 'm', 'b', '1') },
156  { AV_CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') },
157  { AV_CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
158  { AV_CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
159  { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - encoder */
160  { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'P', 'G') },
161  { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - decoder */
162  { AV_CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') },
163  { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'P', 'G') },
164  { AV_CODEC_ID_AVRN, MKTAG('A', 'V', 'R', 'n') },
165  { AV_CODEC_ID_MJPEG, MKTAG('A', 'C', 'D', 'V') },
166  { AV_CODEC_ID_MJPEG, MKTAG('Q', 'I', 'V', 'G') },
167  { AV_CODEC_ID_MJPEG, MKTAG('S', 'L', 'M', 'J') }, /* SL M-JPEG */
168  { AV_CODEC_ID_MJPEG, MKTAG('C', 'J', 'P', 'G') }, /* Creative Webcam JPEG */
169  { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'L', 'V') }, /* Intel JPEG Library Video Codec */
170  { AV_CODEC_ID_MJPEG, MKTAG('M', 'V', 'J', 'P') }, /* Midvid JPEG Video Codec */
171  { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '1') },
172  { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '2') },
173  { AV_CODEC_ID_MJPEG, MKTAG('M', 'T', 'S', 'J') },
174  { AV_CODEC_ID_MJPEG, MKTAG('Z', 'J', 'P', 'G') }, /* Paradigm Matrix M-JPEG Codec */
175  { AV_CODEC_ID_MJPEG, MKTAG('M', 'M', 'J', 'P') },
176  { AV_CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
177  { AV_CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') },
178  { AV_CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
179  { AV_CODEC_ID_RAWVIDEO, MKTAG( 0 , 0 , 0 , 0 ) },
180  { AV_CODEC_ID_RAWVIDEO, MKTAG( 3 , 0 , 0 , 0 ) },
181  { AV_CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
182  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') },
183  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
184  { AV_CODEC_ID_RAWVIDEO, MKTAG('V', '4', '2', '2') },
185  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'N', 'V') },
186  { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'V') },
187  { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'Y') },
188  { AV_CODEC_ID_RAWVIDEO, MKTAG('u', 'y', 'v', '1') },
189  { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'V', 'u', '1') },
190  { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') },
191  { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', 's') },
192  { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', '2') },
193  { AV_CODEC_ID_RAWVIDEO, MKTAG('P', '4', '2', '2') },
194  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '2') },
195  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '6') },
196  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '2', '4') },
197  { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') },
198  { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'Y', 'U', 'Y') },
199  { AV_CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') },
200  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', '0', '0') },
201  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', ' ', ' ') },
202  { AV_CODEC_ID_RAWVIDEO, MKTAG('H', 'D', 'Y', 'C') },
203  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
204  { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'D', 'T', 'Z') }, /* SoftLab-NSK VideoTizer */
205  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', '1') },
206  { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '1', '2') },
207  { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '2', '1') },
208  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', 'B') },
209  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', 'B') },
210  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'V', '9') },
211  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
212  { AV_CODEC_ID_RAWVIDEO, MKTAG('a', 'u', 'v', '2') },
213  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'Y', 'U') },
214  { AV_CODEC_ID_FRWU, MKTAG('F', 'R', 'W', 'U') },
215  { AV_CODEC_ID_R10K, MKTAG('R', '1', '0', 'k') },
216  { AV_CODEC_ID_R210, MKTAG('r', '2', '1', '0') },
217  { AV_CODEC_ID_V210, MKTAG('v', '2', '1', '0') },
218  { AV_CODEC_ID_V308, MKTAG('v', '3', '0', '8') },
219  { AV_CODEC_ID_V408, MKTAG('v', '4', '0', '8') },
220  { AV_CODEC_ID_AYUV, MKTAG('A', 'Y', 'U', 'V') },
221  { AV_CODEC_ID_V410, MKTAG('v', '4', '1', '0') },
222  { AV_CODEC_ID_YUV4, MKTAG('y', 'u', 'v', '4') },
223  { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
224  { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
225  { AV_CODEC_ID_INDEO4, MKTAG('I', 'V', '4', '1') },
226  { AV_CODEC_ID_INDEO5, MKTAG('I', 'V', '5', '0') },
227  { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
228  { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
229  { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
230  { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
231  { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
232  { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
233  { AV_CODEC_ID_VP6F, MKTAG('V', 'P', '6', 'F') },
234  { AV_CODEC_ID_VP6F, MKTAG('F', 'L', 'V', '4') },
235  { AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
236  { AV_CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
237  { AV_CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
238  { AV_CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
239  { AV_CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
240  { AV_CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
241  { AV_CODEC_ID_MIMIC, MKTAG('L', 'M', '2', '0') },
242  { AV_CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') },
243  { AV_CODEC_ID_MSRLE, MKTAG( 1 , 0 , 0 , 0 ) },
244  { AV_CODEC_ID_MSRLE, MKTAG( 2 , 0 , 0 , 0 ) },
245  { AV_CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') },
246  { AV_CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') },
247  { AV_CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') },
248  { AV_CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') },
249  { AV_CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') },
250  { AV_CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') },
251  { AV_CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') },
252  { AV_CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') },
253  { AV_CODEC_ID_TRUEMOTION1, MKTAG('P', 'V', 'E', 'Z') },
254  { AV_CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') },
255  { AV_CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') },
256  { AV_CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
257  { AV_CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
258  { AV_CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
259  { AV_CODEC_ID_FLV1, MKTAG('S', '2', '6', '3') },
260  { AV_CODEC_ID_FLASHSV, MKTAG('F', 'S', 'V', '1') },
261  { AV_CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
262  { AV_CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
263  { AV_CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
264  { AV_CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') },
265  { AV_CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') },
266  { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
267  { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
268  { AV_CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
269  { AV_CODEC_ID_WMV3IMAGE, MKTAG('W', 'M', 'V', 'P') },
270  { AV_CODEC_ID_VC1, MKTAG('W', 'V', 'C', '1') },
271  { AV_CODEC_ID_VC1, MKTAG('W', 'M', 'V', 'A') },
272  { AV_CODEC_ID_VC1IMAGE, MKTAG('W', 'V', 'P', '2') },
273  { AV_CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
274  { AV_CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') },
275  { AV_CODEC_ID_WNV1, MKTAG('Y', 'U', 'V', '8') },
276  { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', '4') },
277  { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') },
278  { AV_CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') },
279  { AV_CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') },
280  { AV_CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') },
281  { AV_CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') },
282  { AV_CODEC_ID_CSCD, MKTAG('C', 'S', 'C', 'D') },
283  { AV_CODEC_ID_ZMBV, MKTAG('Z', 'M', 'B', 'V') },
284  { AV_CODEC_ID_KMVC, MKTAG('K', 'M', 'V', 'C') },
285  { AV_CODEC_ID_CAVS, MKTAG('C', 'A', 'V', 'S') },
286  { AV_CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') },
287  { AV_CODEC_ID_JPEG2000, MKTAG('M', 'J', '2', 'C') },
288  { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'C') },
289  { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'K') },
290  { AV_CODEC_ID_JPEG2000, MKTAG('I', 'P', 'J', '2') },
291  { AV_CODEC_ID_VMNC, MKTAG('V', 'M', 'n', 'c') },
292  { AV_CODEC_ID_TARGA, MKTAG('t', 'g', 'a', ' ') },
293  { AV_CODEC_ID_PNG, MKTAG('M', 'P', 'N', 'G') },
294  { AV_CODEC_ID_PNG, MKTAG('P', 'N', 'G', '1') },
295  { AV_CODEC_ID_CLJR, MKTAG('C', 'L', 'J', 'R') },
296  { AV_CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') },
297  { AV_CODEC_ID_RPZA, MKTAG('a', 'z', 'p', 'r') },
298  { AV_CODEC_ID_RPZA, MKTAG('R', 'P', 'Z', 'A') },
299  { AV_CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') },
300  { AV_CODEC_ID_SP5X, MKTAG('S', 'P', '5', '4') },
301  { AV_CODEC_ID_AURA, MKTAG('A', 'U', 'R', 'A') },
302  { AV_CODEC_ID_AURA2, MKTAG('A', 'U', 'R', '2') },
303  { AV_CODEC_ID_DPX, MKTAG('d', 'p', 'x', ' ') },
304  { AV_CODEC_ID_KGV1, MKTAG('K', 'G', 'V', '1') },
305  { AV_CODEC_ID_LAGARITH, MKTAG('L', 'A', 'G', 'S') },
306  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '2') },
307  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '3') },
308  { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '4') },
309  { AV_CODEC_ID_AMV, MKTAG('A', 'M', 'V', 'F') },
310  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'A') },
311  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'G') },
312  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '0') },
313  { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '2') },
314  { AV_CODEC_ID_VBLE, MKTAG('V', 'B', 'L', 'E') },
315  { AV_CODEC_ID_ESCAPE130, MKTAG('E', '1', '3', '0') },
316  { AV_CODEC_ID_DXTORY, MKTAG('x', 't', 'o', 'r') },
317  { AV_CODEC_ID_ZEROCODEC, MKTAG('Z', 'E', 'C', 'O') },
318  { AV_CODEC_ID_Y41P, MKTAG('Y', '4', '1', 'P') },
319  { AV_CODEC_ID_FLIC, MKTAG('A', 'F', 'L', 'C') },
320  { AV_CODEC_ID_EXR, MKTAG('e', 'x', 'r', ' ') },
321  { AV_CODEC_ID_MSS1, MKTAG('M', 'S', 'S', '1') },
322  { AV_CODEC_ID_MSA1, MKTAG('M', 'S', 'A', '1') },
323  { AV_CODEC_ID_TSCC2, MKTAG('T', 'S', 'C', '2') },
324  { AV_CODEC_ID_MTS2, MKTAG('M', 'T', 'S', '2') },
325  { AV_CODEC_ID_CLLC, MKTAG('C', 'L', 'L', 'C') },
326  { AV_CODEC_ID_MSS2, MKTAG('M', 'S', 'S', '2') },
327  { AV_CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
328  { AV_CODEC_ID_012V, MKTAG('0', '1', '2', 'v') },
329  { AV_CODEC_ID_012V, MKTAG('a', '1', '2', 'v') },
330  { AV_CODEC_ID_NONE, 0 }
331 };
332 
334  { AV_CODEC_ID_PCM_S16LE, 0x0001 },
335  { AV_CODEC_ID_PCM_U8, 0x0001 }, /* must come after s16le in this list */
336  { AV_CODEC_ID_PCM_S24LE, 0x0001 },
337  { AV_CODEC_ID_PCM_S32LE, 0x0001 },
338  { AV_CODEC_ID_ADPCM_MS, 0x0002 },
339  { AV_CODEC_ID_PCM_F32LE, 0x0003 },
340  { AV_CODEC_ID_PCM_F64LE, 0x0003 }, /* must come after f32le in this list */
341  { AV_CODEC_ID_PCM_ALAW, 0x0006 },
342  { AV_CODEC_ID_PCM_MULAW, 0x0007 },
343  { AV_CODEC_ID_WMAVOICE, 0x000A },
344  { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0010 },
345  { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0011 },
346  { AV_CODEC_ID_PCM_ZORK, 0x0011 }, /* must come after adpcm_ima_wav in this list */
347  { AV_CODEC_ID_ADPCM_IMA_OKI, 0x0017 },
348  { AV_CODEC_ID_ADPCM_YAMAHA, 0x0020 },
349  { AV_CODEC_ID_TRUESPEECH, 0x0022 },
350  { AV_CODEC_ID_GSM_MS, 0x0031 },
351  { AV_CODEC_ID_AMR_NB, 0x0038 }, /* rogue format number */
352  { AV_CODEC_ID_G723_1, 0x0042 },
353  { AV_CODEC_ID_ADPCM_G726, 0x0045 },
354  { AV_CODEC_ID_MP2, 0x0050 },
355  { AV_CODEC_ID_MP3, 0x0055 },
356  { AV_CODEC_ID_AMR_NB, 0x0057 },
357  { AV_CODEC_ID_AMR_WB, 0x0058 },
358  { AV_CODEC_ID_ADPCM_IMA_DK4, 0x0061 }, /* rogue format number */
359  { AV_CODEC_ID_ADPCM_IMA_DK3, 0x0062 }, /* rogue format number */
360  { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0069 },
361  { AV_CODEC_ID_VOXWARE, 0x0075 },
362  { AV_CODEC_ID_AAC, 0x00ff },
363  { AV_CODEC_ID_SIPR, 0x0130 },
364  { AV_CODEC_ID_WMAV1, 0x0160 },
365  { AV_CODEC_ID_WMAV2, 0x0161 },
366  { AV_CODEC_ID_WMAPRO, 0x0162 },
367  { AV_CODEC_ID_WMALOSSLESS, 0x0163 },
368  { AV_CODEC_ID_ADPCM_CT, 0x0200 },
369  { AV_CODEC_ID_ATRAC3, 0x0270 },
370  { AV_CODEC_ID_ADPCM_G722, 0x028F },
371  { AV_CODEC_ID_IMC, 0x0401 },
372  { AV_CODEC_ID_IAC, 0x0402 },
373  { AV_CODEC_ID_GSM_MS, 0x1500 },
374  { AV_CODEC_ID_TRUESPEECH, 0x1501 },
375  { AV_CODEC_ID_AAC, 0x1600 }, /* ADTS AAC */
376  { AV_CODEC_ID_AAC_LATM, 0x1602 },
377  { AV_CODEC_ID_AC3, 0x2000 },
378  { AV_CODEC_ID_DTS, 0x2001 },
379  { AV_CODEC_ID_SONIC, 0x2048 },
380  { AV_CODEC_ID_SONIC_LS, 0x2048 },
381  { AV_CODEC_ID_PCM_MULAW, 0x6c75 },
382  { AV_CODEC_ID_AAC, 0x706d },
383  { AV_CODEC_ID_AAC, 0x4143 },
384  { AV_CODEC_ID_G723_1, 0xA100 },
385  { AV_CODEC_ID_AAC, 0xA106 },
386  { AV_CODEC_ID_SPEEX, 0xA109 },
387  { AV_CODEC_ID_FLAC, 0xF1AC },
388  { AV_CODEC_ID_ADPCM_SWF, ('S'<<8)+'F' },
389  { AV_CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
390  { AV_CODEC_ID_NONE, 0 },
391 };
392 
394  {AV_CODEC_ID_AC3, {0x2C,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
395  {AV_CODEC_ID_ATRAC3P, {0xBF,0xAA,0x23,0xE9,0x58,0xCB,0x71,0x44,0xA1,0x19,0xFF,0xFA,0x01,0xE4,0xCE,0x62}},
396  {AV_CODEC_ID_EAC3, {0xAF,0x87,0xFB,0xA7,0x02,0x2D,0xFB,0x42,0xA4,0xD4,0x05,0xCD,0x93,0x84,0x3B,0xDD}},
397  {AV_CODEC_ID_MP2, {0x2B,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
399 };
400 
402  { "IART", "artist" },
403  { "ICMT", "comment" },
404  { "ICOP", "copyright" },
405  { "ICRD", "date" },
406  { "IGNR", "genre" },
407  { "ILNG", "language" },
408  { "INAM", "title" },
409  { "IPRD", "album" },
410  { "IPRT", "track" },
411  { "ISFT", "encoder" },
412  { "ISMP", "timecode" },
413  { "ITCH", "encoded_by"},
414  { 0 },
415 };
416 
417 #if CONFIG_MUXERS
418 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
419 {
420  ffio_wfourcc(pb, tag);
421  avio_wl32(pb, 0);
422  return avio_tell(pb);
423 }
424 
425 void ff_end_tag(AVIOContext *pb, int64_t start)
426 {
427  int64_t pos;
428 
429  pos = avio_tell(pb);
430  avio_seek(pb, start - 4, SEEK_SET);
431  avio_wl32(pb, (uint32_t)(pos - start));
432  avio_seek(pb, pos, SEEK_SET);
433 }
434 
435 /* WAVEFORMATEX header */
436 /* returns the size or -1 on error */
438 {
439  int bps, blkalign, bytespersec, frame_size;
440  int hdrsize = 18;
441  int waveformatextensible;
442  uint8_t temp[256];
443  uint8_t *riff_extradata= temp;
444  uint8_t *riff_extradata_start= temp;
445 
446  if(!enc->codec_tag || enc->codec_tag > 0xffff)
447  return -1;
448 
449  /* We use the known constant frame size for the codec if known, otherwise
450  fallback to using AVCodecContext.frame_size, which is not as reliable
451  for indicating packet duration */
452  frame_size = av_get_audio_frame_duration(enc, 0);
453  if (!frame_size)
454  frame_size = enc->frame_size;
455 
456  waveformatextensible = (enc->channels > 2 && enc->channel_layout)
457  || enc->sample_rate > 48000
458  || av_get_bits_per_sample(enc->codec_id) > 16;
459 
460  if (waveformatextensible) {
461  avio_wl16(pb, 0xfffe);
462  } else {
463  avio_wl16(pb, enc->codec_tag);
464  }
465  avio_wl16(pb, enc->channels);
466  avio_wl32(pb, enc->sample_rate);
467  if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
468  enc->codec_id == AV_CODEC_ID_G723_1 ||
469  enc->codec_id == AV_CODEC_ID_GSM_MS ||
470  enc->codec_id == AV_CODEC_ID_MP2 ||
471  enc->codec_id == AV_CODEC_ID_MP3) {
472  bps = 0;
473  } else {
474  if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
475  if (enc->bits_per_coded_sample)
476  bps = enc->bits_per_coded_sample;
477  else
478  bps = 16; // default to 16
479  }
480  }
481  if(bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample){
482  av_log(enc, AV_LOG_WARNING, "requested bits_per_coded_sample (%d) and actually stored (%d) differ\n", enc->bits_per_coded_sample, bps);
483  }
484 
485  if (enc->codec_id == AV_CODEC_ID_MP2 || enc->codec_id == AV_CODEC_ID_MP3) {
486  /* this is wrong, but it seems many demuxers do not work if this is set
487  correctly */
488  blkalign = frame_size;
489  //blkalign = 144 * enc->bit_rate/enc->sample_rate;
490  } else if (enc->codec_id == AV_CODEC_ID_AC3) {
491  blkalign = 3840; //maximum bytes per frame
492  } else if (enc->codec_id == AV_CODEC_ID_AAC) {
493  blkalign = 768 * enc->channels; //maximum bytes per frame
494  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
495  blkalign = 24;
496  } else if (enc->block_align != 0) { /* specified by the codec */
497  blkalign = enc->block_align;
498  } else
499  blkalign = bps * enc->channels / av_gcd(8, bps);
500  if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
506  bytespersec = enc->sample_rate * blkalign;
507  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
508  bytespersec = 800;
509  } else {
510  bytespersec = enc->bit_rate / 8;
511  }
512  avio_wl32(pb, bytespersec); /* bytes per second */
513  avio_wl16(pb, blkalign); /* block align */
514  avio_wl16(pb, bps); /* bits per sample */
515  if (enc->codec_id == AV_CODEC_ID_MP3) {
516  hdrsize += 12;
517  bytestream_put_le16(&riff_extradata, 1); /* wID */
518  bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
519  bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
520  bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
521  bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
522  } else if (enc->codec_id == AV_CODEC_ID_MP2) {
523  hdrsize += 22;
524  bytestream_put_le16(&riff_extradata, 2); /* fwHeadLayer */
525  bytestream_put_le32(&riff_extradata, enc->bit_rate); /* dwHeadBitrate */
526  bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
527  bytestream_put_le16(&riff_extradata, 0); /* fwHeadModeExt */
528  bytestream_put_le16(&riff_extradata, 1); /* wHeadEmphasis */
529  bytestream_put_le16(&riff_extradata, 16); /* fwHeadFlags */
530  bytestream_put_le32(&riff_extradata, 0); /* dwPTSLow */
531  bytestream_put_le32(&riff_extradata, 0); /* dwPTSHigh */
532  } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
533  hdrsize += 20;
534  bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
535  bytestream_put_le32(&riff_extradata, 0xaea2f732);
536  bytestream_put_le16(&riff_extradata, 0xacde);
537  } else if (enc->codec_id == AV_CODEC_ID_GSM_MS || enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
538  hdrsize += 2;
539  bytestream_put_le16(&riff_extradata, frame_size); /* wSamplesPerBlock */
540  } else if(enc->extradata_size){
541  riff_extradata_start= enc->extradata;
542  riff_extradata= enc->extradata + enc->extradata_size;
543  hdrsize += enc->extradata_size;
544  }
545  if(waveformatextensible) { /* write WAVEFORMATEXTENSIBLE extensions */
546  hdrsize += 22;
547  avio_wl16(pb, riff_extradata - riff_extradata_start + 22); /* 22 is WAVEFORMATEXTENSIBLE size */
548  avio_wl16(pb, bps); /* ValidBitsPerSample || SamplesPerBlock || Reserved */
549  avio_wl32(pb, enc->channel_layout); /* dwChannelMask */
550  avio_wl32(pb, enc->codec_tag); /* GUID + next 3 */
551  avio_wl32(pb, 0x00100000);
552  avio_wl32(pb, 0xAA000080);
553  avio_wl32(pb, 0x719B3800);
554  } else {
555  avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
556  }
557  avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
558  if(hdrsize&1){
559  hdrsize++;
560  avio_w8(pb, 0);
561  }
562 
563  return hdrsize;
564 }
565 
566 /* BITMAPINFOHEADER header */
567 void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
568 {
569  avio_wl32(pb, 40 + enc->extradata_size); /* size */
570  avio_wl32(pb, enc->width);
571  //We always store RGB TopDown
572  avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
573  avio_wl16(pb, 1); /* planes */
574 
575  avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24); /* depth */
576  /* compression type */
577  avio_wl32(pb, enc->codec_tag);
578  avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
579  avio_wl32(pb, 0);
580  avio_wl32(pb, 0);
581  avio_wl32(pb, 0);
582  avio_wl32(pb, 0);
583 
584  avio_write(pb, enc->extradata, enc->extradata_size);
585 
586  if (!for_asf && enc->extradata_size & 1)
587  avio_w8(pb, 0);
588 }
589 
590 void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
591 {
592  int gcd;
593  int audio_frame_size;
594 
595  /* We use the known constant frame size for the codec if known, otherwise
596  fallback to using AVCodecContext.frame_size, which is not as reliable
597  for indicating packet duration */
598  audio_frame_size = av_get_audio_frame_duration(stream, 0);
599  if (!audio_frame_size)
600  audio_frame_size = stream->frame_size;
601 
602  *au_ssize= stream->block_align;
603  if (audio_frame_size && stream->sample_rate) {
604  *au_scale = audio_frame_size;
605  *au_rate= stream->sample_rate;
606  }else if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
607  stream->codec_type == AVMEDIA_TYPE_DATA ||
608  stream->codec_type == AVMEDIA_TYPE_SUBTITLE){
609  *au_scale= stream->time_base.num;
610  *au_rate = stream->time_base.den;
611  }else{
612  *au_scale= stream->block_align ? stream->block_align*8 : 8;
613  *au_rate = stream->bit_rate ? stream->bit_rate : 8*stream->sample_rate;
614  }
615  gcd= av_gcd(*au_scale, *au_rate);
616  *au_scale /= gcd;
617  *au_rate /= gcd;
618 }
619 
620 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
621 {
622  int len = strlen(str);
623  if (len > 0) {
624  len++;
625  ffio_wfourcc(pb, tag);
626  avio_wl32(pb, len);
627  avio_put_str(pb, str);
628  if (len & 1)
629  avio_w8(pb, 0);
630  }
631 }
632 
633 static const char riff_tags[][5] = {
634  "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
635  "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
636  "IPRT", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
637  {0}
638 };
639 
640 static int riff_has_valid_tags(AVFormatContext *s)
641 {
642  int i;
643 
644  for (i = 0; *riff_tags[i]; i++) {
645  if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
646  return 1;
647  }
648 
649  return 0;
650 }
651 
653 {
654  AVIOContext *pb = s->pb;
655  int i;
656  int64_t list_pos;
658 
659  ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
660 
661  /* writing empty LIST is not nice and may cause problems */
662  if (!riff_has_valid_tags(s))
663  return;
664 
665  list_pos = ff_start_tag(pb, "LIST");
666  ffio_wfourcc(pb, "INFO");
667  for (i = 0; *riff_tags[i]; i++) {
668  if ((t = av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
669  ff_riff_write_info_tag(s->pb, t->key, t->value);
670  }
671  ff_end_tag(pb, list_pos);
672 }
673 #endif //CONFIG_MUXERS
674 
675 #if CONFIG_DEMUXERS
676 /* We could be given one of the three possible structures here:
677  * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
678  * is an expansion of the previous one with the fields added
679  * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
680  * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
681  * an openended structure.
682  */
683 int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
684 {
685  int id;
686 
687  id = avio_rl16(pb);
689  codec->channels = avio_rl16(pb);
690  codec->sample_rate = avio_rl32(pb);
691  codec->bit_rate = avio_rl32(pb) * 8;
692  codec->block_align = avio_rl16(pb);
693  if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
694  codec->bits_per_coded_sample = 8;
695  }else
696  codec->bits_per_coded_sample = avio_rl16(pb);
697  if (id == 0xFFFE) {
698  codec->codec_tag = 0;
699  } else {
700  codec->codec_tag = id;
702  }
703  if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
704  int cbSize = avio_rl16(pb); /* cbSize */
705  size -= 18;
706  cbSize = FFMIN(size, cbSize);
707  if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
708  ff_asf_guid subformat;
709  int bps = avio_rl16(pb);
710  if (bps)
711  codec->bits_per_coded_sample = bps;
712  codec->channel_layout = avio_rl32(pb); /* dwChannelMask */
713  ff_get_guid(pb, &subformat);
714  if (!memcmp(subformat + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
715  codec->codec_tag = AV_RL32(subformat);
717  } else {
718  codec->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
719  if (!codec->codec_id)
720  av_log(codec, AV_LOG_WARNING, "unknown subformat:"FF_PRI_GUID"\n", FF_ARG_GUID(subformat));
721  }
722  cbSize -= 22;
723  size -= 22;
724  }
725  codec->extradata_size = cbSize;
726  if (cbSize > 0) {
727  av_free(codec->extradata);
729  if (!codec->extradata)
730  return AVERROR(ENOMEM);
731  avio_read(pb, codec->extradata, codec->extradata_size);
732  size -= cbSize;
733  }
734 
735  /* It is possible for the chunk to contain garbage at the end */
736  if (size > 0)
737  avio_skip(pb, size);
738  }
739  if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
740  /* channels and sample_rate values are those prior to applying SBR and/or PS */
741  codec->channels = 0;
742  codec->sample_rate = 0;
743  }
744  /* override bits_per_coded_sample for G.726 */
745  if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
746  codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
747 
748  return 0;
749 }
750 
751 
752 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
753 {
754  enum AVCodecID id;
755  id = ff_codec_get_id(ff_codec_wav_tags, tag);
756  if (id <= 0)
757  return id;
758 
759  if (id == AV_CODEC_ID_PCM_S16LE)
760  id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
761  else if (id == AV_CODEC_ID_PCM_F32LE)
762  id = ff_get_pcm_codec_id(bps, 1, 0, 0);
763 
764  if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
766  return id;
767 }
768 
769 int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
770 {
771  int tag1;
772  if(esize) *esize = avio_rl32(pb);
773  else avio_rl32(pb);
774  st->codec->width = avio_rl32(pb);
775  st->codec->height = (int32_t)avio_rl32(pb);
776  avio_rl16(pb); /* planes */
777  st->codec->bits_per_coded_sample= avio_rl16(pb); /* depth */
778  tag1 = avio_rl32(pb);
779  avio_rl32(pb); /* ImageSize */
780  avio_rl32(pb); /* XPelsPerMeter */
781  avio_rl32(pb); /* YPelsPerMeter */
782  avio_rl32(pb); /* ClrUsed */
783  avio_rl32(pb); /* ClrImportant */
784  return tag1;
785 }
786 
788 {
789  av_assert0(sizeof(*g) == 16); //compiler will optimize this out
790  if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
791  memset(*g, 0, sizeof(*g));
792 }
793 
794 enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
795 {
796  int i;
797  for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++) {
798  if (!ff_guidcmp(guids[i].guid, guid))
799  return guids[i].id;
800  }
801  return AV_CODEC_ID_NONE;
802 }
803 
804 int ff_read_riff_info(AVFormatContext *s, int64_t size)
805 {
806  int64_t start, end, cur;
807  AVIOContext *pb = s->pb;
808 
809  start = avio_tell(pb);
810  end = start + size;
811 
812  while ((cur = avio_tell(pb)) >= 0 && cur <= end - 8 /* = tag + size */) {
813  uint32_t chunk_code;
814  int64_t chunk_size;
815  char key[5] = {0};
816  char *value;
817 
818  chunk_code = avio_rl32(pb);
819  chunk_size = avio_rl32(pb);
820  if (url_feof(pb)) {
821  if (chunk_code || chunk_size) {
822  av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
823  return AVERROR_INVALIDDATA;
824  }
825  return AVERROR_EOF;
826  }
827  if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
828  avio_seek(pb, -9, SEEK_CUR);
829  chunk_code = avio_rl32(pb);
830  chunk_size = avio_rl32(pb);
831  if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
832  av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
833  return AVERROR_INVALIDDATA;
834  }
835  }
836 
837  chunk_size += (chunk_size & 1);
838 
839  if (!chunk_code) {
840  if (chunk_size)
841  avio_skip(pb, chunk_size);
842  else if (pb->eof_reached) {
843  av_log(s, AV_LOG_WARNING, "truncated file\n");
844  return AVERROR_EOF;
845  }
846  continue;
847  }
848 
849  value = av_mallocz(chunk_size + 1);
850  if (!value) {
851  av_log(s, AV_LOG_ERROR, "out of memory, unable to read INFO tag\n");
852  return AVERROR(ENOMEM);
853  }
854 
855  AV_WL32(key, chunk_code);
856 
857  if (avio_read(pb, value, chunk_size) != chunk_size) {
858  av_log(s, AV_LOG_WARNING, "premature end of file while reading INFO tag\n");
859  }
860 
861  av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
862  }
863 
864  return 0;
865 }
866 #endif // CONFIG_DEMUXERS