ffmpeg / libavformat / avienc.c @ c4052af0
History | View | Annotate | Download (26.1 KB)
1 |
/*
|
---|---|
2 |
* AVI encoder.
|
3 |
* Copyright (c) 2000 Fabrice Bellard.
|
4 |
*
|
5 |
* This library is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
7 |
* License as published by the Free Software Foundation; either
|
8 |
* version 2 of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This library is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
* Lesser General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
16 |
* License along with this library; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 |
*/
|
19 |
#include "avformat.h" |
20 |
#include "avi.h" |
21 |
|
22 |
/*
|
23 |
* TODO:
|
24 |
* - fill all fields if non streamed (nb_frames for example)
|
25 |
*/
|
26 |
|
27 |
#ifdef CONFIG_MUXERS
|
28 |
typedef struct AVIIentry { |
29 |
unsigned int flags, pos, len; |
30 |
} AVIIentry; |
31 |
|
32 |
#define AVI_INDEX_CLUSTER_SIZE 16384 |
33 |
|
34 |
typedef struct AVIIndex { |
35 |
offset_t indx_start; |
36 |
int entry;
|
37 |
int ents_allocated;
|
38 |
AVIIentry** cluster; |
39 |
} AVIIndex; |
40 |
|
41 |
typedef struct { |
42 |
offset_t riff_start, movi_list, odml_list; |
43 |
offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS]; |
44 |
int audio_strm_length[MAX_STREAMS];
|
45 |
int riff_id;
|
46 |
int packet_count[MAX_STREAMS];
|
47 |
|
48 |
AVIIndex indexes[MAX_STREAMS]; |
49 |
} AVIContext; |
50 |
|
51 |
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id) |
52 |
{ |
53 |
int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
|
54 |
int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
|
55 |
return &idx->cluster[cl][id];
|
56 |
} |
57 |
|
58 |
offset_t start_tag(ByteIOContext *pb, const char *tag) |
59 |
{ |
60 |
put_tag(pb, tag); |
61 |
put_le32(pb, 0);
|
62 |
return url_ftell(pb);
|
63 |
} |
64 |
|
65 |
void end_tag(ByteIOContext *pb, offset_t start)
|
66 |
{ |
67 |
offset_t pos; |
68 |
|
69 |
pos = url_ftell(pb); |
70 |
url_fseek(pb, start - 4, SEEK_SET);
|
71 |
put_le32(pb, (uint32_t)(pos - start)); |
72 |
url_fseek(pb, pos, SEEK_SET); |
73 |
} |
74 |
#endif //CONFIG_MUXERS |
75 |
|
76 |
/* Note: when encoding, the first matching tag is used, so order is
|
77 |
important if multiple tags possible for a given codec. */
|
78 |
const CodecTag codec_bmp_tags[] = {
|
79 |
{ CODEC_ID_H264, MKTAG('H', '2', '6', '4') }, |
80 |
{ CODEC_ID_H264, MKTAG('h', '2', '6', '4') }, |
81 |
{ CODEC_ID_H264, MKTAG('X', '2', '6', '4') }, |
82 |
{ CODEC_ID_H264, MKTAG('x', '2', '6', '4') }, |
83 |
{ CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, |
84 |
{ CODEC_ID_H264, MKTAG('V', 'S', 'S', 'H') }, |
85 |
|
86 |
{ CODEC_ID_H263, MKTAG('H', '2', '6', '3') }, |
87 |
{ CODEC_ID_H263P, MKTAG('H', '2', '6', '3') }, |
88 |
{ CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */ |
89 |
{ CODEC_ID_H261, MKTAG('H', '2', '6', '1') }, |
90 |
|
91 |
/* added based on MPlayer */
|
92 |
{ CODEC_ID_H263P, MKTAG('U', '2', '6', '3') }, |
93 |
{ CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') }, |
94 |
|
95 |
{ CODEC_ID_MPEG4, MKTAG('F', 'M', 'P', '4')}, |
96 |
{ CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 }, |
97 |
{ CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 }, |
98 |
{ CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 }, |
99 |
{ CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') }, |
100 |
{ CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') }, |
101 |
{ CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */ |
102 |
|
103 |
/* added based on MPlayer */
|
104 |
{ CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') }, |
105 |
{ CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') }, |
106 |
{ CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, |
107 |
{ CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') }, |
108 |
{ CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') }, |
109 |
{ CODEC_ID_MPEG4, MKTAG('S', 'E', 'D', 'G') }, |
110 |
|
111 |
{ CODEC_ID_MPEG4, MKTAG('R', 'M', 'P', '4') }, |
112 |
|
113 |
{ CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */ |
114 |
{ CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, |
115 |
|
116 |
/* added based on MPlayer */
|
117 |
{ CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') }, |
118 |
{ CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') }, |
119 |
{ CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') }, |
120 |
{ CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') }, |
121 |
{ CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') }, |
122 |
{ CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') }, |
123 |
{ CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') }, |
124 |
|
125 |
{ CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') }, |
126 |
|
127 |
/* added based on MPlayer */
|
128 |
{ CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') }, |
129 |
|
130 |
{ CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') }, |
131 |
|
132 |
{ CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') }, |
133 |
|
134 |
/* added based on MPlayer */
|
135 |
{ CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') }, |
136 |
{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') }, |
137 |
{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') }, |
138 |
{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') }, |
139 |
{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') }, |
140 |
{ CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') }, |
141 |
{ CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') }, |
142 |
{ CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') }, |
143 |
{ CODEC_ID_MPEG2VIDEO, MKTAG('M', 'P', 'E', 'G') }, |
144 |
{ CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') }, |
145 |
{ CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') }, |
146 |
{ CODEC_ID_MPEG1VIDEO, 0x10000001 },
|
147 |
{ CODEC_ID_MPEG2VIDEO, 0x10000002 },
|
148 |
{ CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') }, |
149 |
{ CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') }, |
150 |
{ CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') }, |
151 |
{ CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') }, |
152 |
{ CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */ |
153 |
{ CODEC_ID_MJPEG, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - decoder */ |
154 |
{ CODEC_ID_JPEGLS, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - encoder */ |
155 |
{ CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') }, |
156 |
{ CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') }, |
157 |
{ CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') }, |
158 |
{ CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') }, |
159 |
{ CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') }, |
160 |
{ CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') }, |
161 |
{ CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') }, |
162 |
{ CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') }, |
163 |
{ CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') }, |
164 |
{ CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') }, |
165 |
{ CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, |
166 |
{ CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') }, |
167 |
{ CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') }, |
168 |
{ CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') }, |
169 |
{ CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') }, |
170 |
{ CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') }, |
171 |
{ CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') }, |
172 |
{ CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') }, |
173 |
{ CODEC_ID_MSRLE, MKTAG(0x1, 0x0, 0x0, 0x0) }, |
174 |
{ CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') }, |
175 |
{ CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') }, |
176 |
{ CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') }, |
177 |
{ CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') }, |
178 |
{ CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') }, |
179 |
{ CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') }, |
180 |
{ CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, |
181 |
{ CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') }, |
182 |
{ CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') }, |
183 |
{ CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') }, |
184 |
{ CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') }, |
185 |
{ CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') }, |
186 |
{ CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') }, |
187 |
{ CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, |
188 |
{ CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') }, |
189 |
{ CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') }, |
190 |
{ CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') }, |
191 |
{ CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') }, |
192 |
{ CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') }, |
193 |
{ CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') }, |
194 |
{ CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') }, |
195 |
{ CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') }, |
196 |
{ CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') }, |
197 |
{ CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') }, |
198 |
{ CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') }, |
199 |
{ CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') }, |
200 |
{ CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') }, |
201 |
{ CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') }, |
202 |
{ CODEC_ID_CSCD, MKTAG('C', 'S', 'C', 'D') }, |
203 |
{ CODEC_ID_ZMBV, MKTAG('Z', 'M', 'B', 'V') }, |
204 |
{ CODEC_ID_KMVC, MKTAG('K', 'M', 'V', 'C') }, |
205 |
{ CODEC_ID_RAWVIDEO, 0 },
|
206 |
{ CODEC_ID_NONE, 0 },
|
207 |
}; |
208 |
|
209 |
unsigned int codec_get_tag(const CodecTag *tags, int id) |
210 |
{ |
211 |
while (tags->id != CODEC_ID_NONE) {
|
212 |
if (tags->id == id)
|
213 |
return tags->tag;
|
214 |
tags++; |
215 |
} |
216 |
return 0; |
217 |
} |
218 |
|
219 |
static unsigned int codec_get_asf_tag(const CodecTag *tags, unsigned int id) |
220 |
{ |
221 |
while (tags->id != CODEC_ID_NONE) {
|
222 |
if (!tags->invalid_asf && tags->id == id)
|
223 |
return tags->tag;
|
224 |
tags++; |
225 |
} |
226 |
return 0; |
227 |
} |
228 |
|
229 |
enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag) |
230 |
{ |
231 |
while (tags->id != CODEC_ID_NONE) {
|
232 |
if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF) |
233 |
&& toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF) |
234 |
&& toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF) |
235 |
&& toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF)) |
236 |
return tags->id;
|
237 |
tags++; |
238 |
} |
239 |
return CODEC_ID_NONE;
|
240 |
} |
241 |
|
242 |
unsigned int codec_get_bmp_tag(int id) |
243 |
{ |
244 |
return codec_get_tag(codec_bmp_tags, id);
|
245 |
} |
246 |
|
247 |
unsigned int codec_get_wav_tag(int id) |
248 |
{ |
249 |
return codec_get_tag(codec_wav_tags, id);
|
250 |
} |
251 |
|
252 |
enum CodecID codec_get_bmp_id(unsigned int tag) |
253 |
{ |
254 |
return codec_get_id(codec_bmp_tags, tag);
|
255 |
} |
256 |
|
257 |
enum CodecID codec_get_wav_id(unsigned int tag) |
258 |
{ |
259 |
return codec_get_id(codec_wav_tags, tag);
|
260 |
} |
261 |
|
262 |
#ifdef CONFIG_MUXERS
|
263 |
/* BITMAPINFOHEADER header */
|
264 |
void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf) |
265 |
{ |
266 |
put_le32(pb, 40 + enc->extradata_size); /* size */ |
267 |
put_le32(pb, enc->width); |
268 |
put_le32(pb, enc->height); |
269 |
put_le16(pb, 1); /* planes */ |
270 |
|
271 |
put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */ |
272 |
/* compression type */
|
273 |
put_le32(pb, for_asf ? (enc->codec_tag ? enc->codec_tag : codec_get_asf_tag(tags, enc->codec_id)) : enc->codec_tag); //
|
274 |
put_le32(pb, enc->width * enc->height * 3);
|
275 |
put_le32(pb, 0);
|
276 |
put_le32(pb, 0);
|
277 |
put_le32(pb, 0);
|
278 |
put_le32(pb, 0);
|
279 |
|
280 |
put_buffer(pb, enc->extradata, enc->extradata_size); |
281 |
|
282 |
if (enc->extradata_size & 1) |
283 |
put_byte(pb, 0);
|
284 |
} |
285 |
|
286 |
void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale) |
287 |
{ |
288 |
int gcd;
|
289 |
|
290 |
*au_ssize= stream->block_align; |
291 |
if(stream->frame_size && stream->sample_rate){
|
292 |
*au_scale=stream->frame_size; |
293 |
*au_rate= stream->sample_rate; |
294 |
}else if(stream->codec_type == CODEC_TYPE_VIDEO){ |
295 |
*au_scale= stream->time_base.num; |
296 |
*au_rate = stream->time_base.den; |
297 |
}else{
|
298 |
*au_scale= stream->block_align ? stream->block_align*8 : 8; |
299 |
*au_rate = stream->bit_rate; |
300 |
} |
301 |
gcd= ff_gcd(*au_scale, *au_rate); |
302 |
*au_scale /= gcd; |
303 |
*au_rate /= gcd; |
304 |
} |
305 |
|
306 |
static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
|
307 |
const char* riff_tag, const char* list_tag) |
308 |
{ |
309 |
offset_t loff; |
310 |
int i;
|
311 |
|
312 |
avi->riff_id++; |
313 |
for (i=0; i<MAX_STREAMS; i++) |
314 |
avi->indexes[i].entry = 0;
|
315 |
|
316 |
avi->riff_start = start_tag(pb, "RIFF");
|
317 |
put_tag(pb, riff_tag); |
318 |
loff = start_tag(pb, "LIST");
|
319 |
put_tag(pb, list_tag); |
320 |
return loff;
|
321 |
} |
322 |
|
323 |
static unsigned char* avi_stream2fourcc(unsigned char* tag, int index, |
324 |
enum CodecType type)
|
325 |
{ |
326 |
tag[0] = '0'; |
327 |
tag[1] = '0' + index; |
328 |
if (type == CODEC_TYPE_VIDEO) {
|
329 |
tag[2] = 'd'; |
330 |
tag[3] = 'c'; |
331 |
} else {
|
332 |
tag[2] = 'w'; |
333 |
tag[3] = 'b'; |
334 |
} |
335 |
tag[4] = '\0'; |
336 |
return tag;
|
337 |
} |
338 |
|
339 |
static int avi_write_header(AVFormatContext *s) |
340 |
{ |
341 |
AVIContext *avi = s->priv_data; |
342 |
ByteIOContext *pb = &s->pb; |
343 |
int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
|
344 |
AVCodecContext *stream, *video_enc; |
345 |
offset_t list1, list2, strh, strf; |
346 |
|
347 |
/* header list */
|
348 |
avi->riff_id = 0;
|
349 |
list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl"); |
350 |
|
351 |
/* avi header */
|
352 |
put_tag(pb, "avih");
|
353 |
put_le32(pb, 14 * 4); |
354 |
bitrate = 0;
|
355 |
|
356 |
video_enc = NULL;
|
357 |
for(n=0;n<s->nb_streams;n++) { |
358 |
stream = s->streams[n]->codec; |
359 |
bitrate += stream->bit_rate; |
360 |
if (stream->codec_type == CODEC_TYPE_VIDEO)
|
361 |
video_enc = stream; |
362 |
} |
363 |
|
364 |
nb_frames = 0;
|
365 |
|
366 |
if(video_enc){
|
367 |
put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
|
368 |
} else {
|
369 |
put_le32(pb, 0);
|
370 |
} |
371 |
put_le32(pb, bitrate / 8); /* XXX: not quite exact */ |
372 |
put_le32(pb, 0); /* padding */ |
373 |
if (url_is_streamed(pb))
|
374 |
put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
|
375 |
else
|
376 |
put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
|
377 |
avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
|
378 |
put_le32(pb, nb_frames); /* nb frames, filled later */
|
379 |
put_le32(pb, 0); /* initial frame */ |
380 |
put_le32(pb, s->nb_streams); /* nb streams */
|
381 |
put_le32(pb, 1024 * 1024); /* suggested buffer size */ |
382 |
if(video_enc){
|
383 |
put_le32(pb, video_enc->width); |
384 |
put_le32(pb, video_enc->height); |
385 |
} else {
|
386 |
put_le32(pb, 0);
|
387 |
put_le32(pb, 0);
|
388 |
} |
389 |
put_le32(pb, 0); /* reserved */ |
390 |
put_le32(pb, 0); /* reserved */ |
391 |
put_le32(pb, 0); /* reserved */ |
392 |
put_le32(pb, 0); /* reserved */ |
393 |
|
394 |
/* stream list */
|
395 |
for(i=0;i<n;i++) { |
396 |
list2 = start_tag(pb, "LIST");
|
397 |
put_tag(pb, "strl");
|
398 |
|
399 |
stream = s->streams[i]->codec; |
400 |
|
401 |
/* FourCC should really be set by the codec itself */
|
402 |
if (! stream->codec_tag) {
|
403 |
stream->codec_tag = codec_get_bmp_tag(stream->codec_id); |
404 |
} |
405 |
|
406 |
/* stream generic header */
|
407 |
strh = start_tag(pb, "strh");
|
408 |
switch(stream->codec_type) {
|
409 |
case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break; |
410 |
case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break; |
411 |
// case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
|
412 |
case CODEC_TYPE_DATA : put_tag(pb, "dats"); break; |
413 |
} |
414 |
if(stream->codec_type == CODEC_TYPE_VIDEO)
|
415 |
put_le32(pb, stream->codec_tag); |
416 |
else
|
417 |
put_le32(pb, 1);
|
418 |
put_le32(pb, 0); /* flags */ |
419 |
put_le16(pb, 0); /* priority */ |
420 |
put_le16(pb, 0); /* language */ |
421 |
put_le32(pb, 0); /* initial frame */ |
422 |
|
423 |
ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); |
424 |
|
425 |
put_le32(pb, au_scale); /* scale */
|
426 |
put_le32(pb, au_byterate); /* rate */
|
427 |
av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
|
428 |
|
429 |
put_le32(pb, 0); /* start */ |
430 |
avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
|
431 |
if (url_is_streamed(pb))
|
432 |
put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
|
433 |
else
|
434 |
put_le32(pb, 0); /* length, XXX: filled later */ |
435 |
|
436 |
/* suggested buffer size */ //FIXME set at the end to largest chunk |
437 |
if(stream->codec_type == CODEC_TYPE_VIDEO)
|
438 |
put_le32(pb, 1024 * 1024); |
439 |
else if(stream->codec_type == CODEC_TYPE_AUDIO) |
440 |
put_le32(pb, 12 * 1024); |
441 |
else
|
442 |
put_le32(pb, 0);
|
443 |
put_le32(pb, -1); /* quality */ |
444 |
put_le32(pb, au_ssize); /* sample size */
|
445 |
put_le32(pb, 0);
|
446 |
put_le16(pb, stream->width); |
447 |
put_le16(pb, stream->height); |
448 |
end_tag(pb, strh); |
449 |
|
450 |
if(stream->codec_type != CODEC_TYPE_DATA){
|
451 |
strf = start_tag(pb, "strf");
|
452 |
switch(stream->codec_type) {
|
453 |
case CODEC_TYPE_VIDEO:
|
454 |
put_bmp_header(pb, stream, codec_bmp_tags, 0);
|
455 |
break;
|
456 |
case CODEC_TYPE_AUDIO:
|
457 |
if (put_wav_header(pb, stream) < 0) { |
458 |
av_free(avi); |
459 |
return -1; |
460 |
} |
461 |
break;
|
462 |
default:
|
463 |
return -1; |
464 |
} |
465 |
end_tag(pb, strf); |
466 |
} |
467 |
|
468 |
if (!url_is_streamed(pb)) {
|
469 |
unsigned char tag[5]; |
470 |
int j;
|
471 |
|
472 |
/* Starting to lay out AVI OpenDML master index.
|
473 |
* We want to make it JUNK entry for now, since we'd
|
474 |
* like to get away without making AVI an OpenDML one
|
475 |
* for compatibility reasons.
|
476 |
*/
|
477 |
avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
|
478 |
avi->indexes[i].indx_start = start_tag(pb, "JUNK");
|
479 |
put_le16(pb, 4); /* wLongsPerEntry */ |
480 |
put_byte(pb, 0); /* bIndexSubType (0 == frame index) */ |
481 |
put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */ |
482 |
put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */ |
483 |
put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
|
484 |
/* dwChunkId */
|
485 |
put_le64(pb, 0); /* dwReserved[3] |
486 |
put_le32(pb, 0); Must be 0. */
|
487 |
for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++) |
488 |
put_le64(pb, 0);
|
489 |
end_tag(pb, avi->indexes[i].indx_start); |
490 |
} |
491 |
|
492 |
end_tag(pb, list2); |
493 |
} |
494 |
|
495 |
if (!url_is_streamed(pb)) {
|
496 |
/* AVI could become an OpenDML one, if it grows beyond 2Gb range */
|
497 |
avi->odml_list = start_tag(pb, "JUNK");
|
498 |
put_tag(pb, "odml");
|
499 |
put_tag(pb, "dmlh");
|
500 |
put_le32(pb, 248);
|
501 |
for (i = 0; i < 248; i+= 4) |
502 |
put_le32(pb, 0);
|
503 |
end_tag(pb, avi->odml_list); |
504 |
} |
505 |
|
506 |
end_tag(pb, list1); |
507 |
|
508 |
avi->movi_list = start_tag(pb, "LIST");
|
509 |
put_tag(pb, "movi");
|
510 |
|
511 |
put_flush_packet(pb); |
512 |
|
513 |
return 0; |
514 |
} |
515 |
|
516 |
static int avi_write_ix(AVFormatContext *s) |
517 |
{ |
518 |
ByteIOContext *pb = &s->pb; |
519 |
AVIContext *avi = s->priv_data; |
520 |
unsigned char tag[5]; |
521 |
unsigned char ix_tag[] = "ix00"; |
522 |
int i, j;
|
523 |
|
524 |
assert(!url_is_streamed(pb)); |
525 |
|
526 |
if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
|
527 |
return -1; |
528 |
|
529 |
for (i=0;i<s->nb_streams;i++) { |
530 |
offset_t ix, pos; |
531 |
|
532 |
avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
|
533 |
ix_tag[3] = '0' + i; |
534 |
|
535 |
/* Writing AVI OpenDML leaf index chunk */
|
536 |
ix = url_ftell(pb); |
537 |
put_tag(pb, &ix_tag[0]); /* ix?? */ |
538 |
put_le32(pb, avi->indexes[i].entry * 8 + 24); |
539 |
/* chunk size */
|
540 |
put_le16(pb, 2); /* wLongsPerEntry */ |
541 |
put_byte(pb, 0); /* bIndexSubType (0 == frame index) */ |
542 |
put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */ |
543 |
put_le32(pb, avi->indexes[i].entry); |
544 |
/* nEntriesInUse */
|
545 |
put_tag(pb, &tag[0]); /* dwChunkId */ |
546 |
put_le64(pb, avi->movi_list);/* qwBaseOffset */
|
547 |
put_le32(pb, 0); /* dwReserved_3 (must be 0) */ |
548 |
|
549 |
for (j=0; j<avi->indexes[i].entry; j++) { |
550 |
AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j); |
551 |
put_le32(pb, ie->pos + 8);
|
552 |
put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
|
553 |
(ie->flags & 0x10 ? 0 : 0x80000000)); |
554 |
} |
555 |
put_flush_packet(pb); |
556 |
pos = url_ftell(pb); |
557 |
|
558 |
/* Updating one entry in the AVI OpenDML master index */
|
559 |
url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
|
560 |
put_tag(pb, "indx"); /* enabling this entry */ |
561 |
url_fskip(pb, 8);
|
562 |
put_le32(pb, avi->riff_id); /* nEntriesInUse */
|
563 |
url_fskip(pb, 16*avi->riff_id);
|
564 |
put_le64(pb, ix); /* qwOffset */
|
565 |
put_le32(pb, pos - ix); /* dwSize */
|
566 |
put_le32(pb, avi->indexes[i].entry); /* dwDuration */
|
567 |
|
568 |
url_fseek(pb, pos, SEEK_SET); |
569 |
} |
570 |
return 0; |
571 |
} |
572 |
|
573 |
static int avi_write_idx1(AVFormatContext *s) |
574 |
{ |
575 |
ByteIOContext *pb = &s->pb; |
576 |
AVIContext *avi = s->priv_data; |
577 |
offset_t file_size, idx_chunk; |
578 |
int i, n, nb_frames, au_byterate, au_ssize, au_scale;
|
579 |
AVCodecContext *stream; |
580 |
unsigned char tag[5]; |
581 |
|
582 |
if (!url_is_streamed(pb)) {
|
583 |
AVIIentry* ie = 0, *tie;
|
584 |
int entry[MAX_STREAMS];
|
585 |
int empty, stream_id = -1; |
586 |
|
587 |
idx_chunk = start_tag(pb, "idx1");
|
588 |
memset(&entry[0], 0, sizeof(entry)); |
589 |
do {
|
590 |
empty = 1;
|
591 |
for (i=0; i<s->nb_streams; i++) { |
592 |
if (avi->indexes[i].entry <= entry[i])
|
593 |
continue;
|
594 |
|
595 |
tie = avi_get_ientry(&avi->indexes[i], entry[i]); |
596 |
if (empty || tie->pos < ie->pos) {
|
597 |
ie = tie; |
598 |
stream_id = i; |
599 |
} |
600 |
empty = 0;
|
601 |
} |
602 |
if (!empty) {
|
603 |
avi_stream2fourcc(&tag[0], stream_id,
|
604 |
s->streams[stream_id]->codec->codec_type); |
605 |
put_tag(pb, &tag[0]);
|
606 |
put_le32(pb, ie->flags); |
607 |
put_le32(pb, ie->pos); |
608 |
put_le32(pb, ie->len); |
609 |
entry[stream_id]++; |
610 |
} |
611 |
} while (!empty);
|
612 |
end_tag(pb, idx_chunk); |
613 |
|
614 |
/* Fill in frame/sample counters */
|
615 |
file_size = url_ftell(pb); |
616 |
nb_frames = 0;
|
617 |
for(n=0;n<s->nb_streams;n++) { |
618 |
assert(avi->frames_hdr_strm[n]); |
619 |
stream = s->streams[n]->codec; |
620 |
url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET); |
621 |
ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale); |
622 |
if (au_ssize == 0) { |
623 |
put_le32(pb, avi->packet_count[n]); |
624 |
} else {
|
625 |
put_le32(pb, avi->audio_strm_length[n] / au_ssize); |
626 |
} |
627 |
if(stream->codec_type == CODEC_TYPE_VIDEO)
|
628 |
nb_frames = FFMAX(nb_frames, avi->packet_count[n]); |
629 |
} |
630 |
assert(avi->frames_hdr_all); |
631 |
url_fseek(pb, avi->frames_hdr_all, SEEK_SET); |
632 |
put_le32(pb, nb_frames); |
633 |
url_fseek(pb, file_size, SEEK_SET); |
634 |
} |
635 |
return 0; |
636 |
} |
637 |
|
638 |
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) |
639 |
{ |
640 |
AVIContext *avi = s->priv_data; |
641 |
ByteIOContext *pb = &s->pb; |
642 |
unsigned char tag[5]; |
643 |
unsigned int flags=0; |
644 |
const int stream_index= pkt->stream_index; |
645 |
AVCodecContext *enc= s->streams[stream_index]->codec; |
646 |
int size= pkt->size;
|
647 |
|
648 |
// av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
|
649 |
while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){ |
650 |
AVPacket empty_packet; |
651 |
|
652 |
av_init_packet(&empty_packet); |
653 |
empty_packet.size= 0;
|
654 |
empty_packet.data= NULL;
|
655 |
empty_packet.stream_index= stream_index; |
656 |
avi_write_packet(s, &empty_packet); |
657 |
// av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
|
658 |
} |
659 |
avi->packet_count[stream_index]++; |
660 |
|
661 |
// Make sure to put an OpenDML chunk when the file size exceeds the limits
|
662 |
if (!url_is_streamed(pb) &&
|
663 |
(url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { |
664 |
|
665 |
avi_write_ix(s); |
666 |
end_tag(pb, avi->movi_list); |
667 |
|
668 |
if (avi->riff_id == 1) |
669 |
avi_write_idx1(s); |
670 |
|
671 |
end_tag(pb, avi->riff_start); |
672 |
avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi"); |
673 |
} |
674 |
|
675 |
avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
|
676 |
if(pkt->flags&PKT_FLAG_KEY)
|
677 |
flags = 0x10;
|
678 |
if (enc->codec_type == CODEC_TYPE_AUDIO) {
|
679 |
avi->audio_strm_length[stream_index] += size; |
680 |
} |
681 |
|
682 |
if (!url_is_streamed(&s->pb)) {
|
683 |
AVIIndex* idx = &avi->indexes[stream_index]; |
684 |
int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
|
685 |
int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
|
686 |
if (idx->ents_allocated <= idx->entry) {
|
687 |
idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); |
688 |
if (!idx->cluster)
|
689 |
return -1; |
690 |
idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
|
691 |
if (!idx->cluster[cl])
|
692 |
return -1; |
693 |
idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE; |
694 |
} |
695 |
|
696 |
idx->cluster[cl][id].flags = flags; |
697 |
idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list; |
698 |
idx->cluster[cl][id].len = size; |
699 |
idx->entry++; |
700 |
} |
701 |
|
702 |
put_buffer(pb, tag, 4);
|
703 |
put_le32(pb, size); |
704 |
put_buffer(pb, pkt->data, size); |
705 |
if (size & 1) |
706 |
put_byte(pb, 0);
|
707 |
|
708 |
put_flush_packet(pb); |
709 |
return 0; |
710 |
} |
711 |
|
712 |
static int avi_write_trailer(AVFormatContext *s) |
713 |
{ |
714 |
AVIContext *avi = s->priv_data; |
715 |
ByteIOContext *pb = &s->pb; |
716 |
int res = 0; |
717 |
int i, j, n, nb_frames;
|
718 |
offset_t file_size; |
719 |
|
720 |
if (!url_is_streamed(pb))
|
721 |
{ |
722 |
if (avi->riff_id == 1) { |
723 |
end_tag(pb, avi->movi_list); |
724 |
res = avi_write_idx1(s); |
725 |
end_tag(pb, avi->riff_start); |
726 |
} else {
|
727 |
avi_write_ix(s); |
728 |
end_tag(pb, avi->movi_list); |
729 |
end_tag(pb, avi->riff_start); |
730 |
|
731 |
file_size = url_ftell(pb); |
732 |
url_fseek(pb, avi->odml_list - 8, SEEK_SET);
|
733 |
put_tag(pb, "LIST"); /* Making this AVI OpenDML one */ |
734 |
url_fskip(pb, 16);
|
735 |
|
736 |
for (n=nb_frames=0;n<s->nb_streams;n++) { |
737 |
AVCodecContext *stream = s->streams[n]->codec; |
738 |
if (stream->codec_type == CODEC_TYPE_VIDEO) {
|
739 |
if (nb_frames < avi->packet_count[n])
|
740 |
nb_frames = avi->packet_count[n]; |
741 |
} else {
|
742 |
if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
|
743 |
nb_frames += avi->packet_count[n]; |
744 |
} |
745 |
} |
746 |
} |
747 |
put_le32(pb, nb_frames); |
748 |
url_fseek(pb, file_size, SEEK_SET); |
749 |
} |
750 |
} |
751 |
put_flush_packet(pb); |
752 |
|
753 |
for (i=0; i<MAX_STREAMS; i++) { |
754 |
for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++) |
755 |
av_free(avi->indexes[i].cluster[j]); |
756 |
av_free(avi->indexes[i].cluster); |
757 |
avi->indexes[i].cluster = NULL;
|
758 |
avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
|
759 |
} |
760 |
|
761 |
return res;
|
762 |
} |
763 |
|
764 |
static AVOutputFormat avi_oformat = {
|
765 |
"avi",
|
766 |
"avi format",
|
767 |
"video/x-msvideo",
|
768 |
"avi",
|
769 |
sizeof(AVIContext),
|
770 |
CODEC_ID_MP2, |
771 |
CODEC_ID_MPEG4, |
772 |
avi_write_header, |
773 |
avi_write_packet, |
774 |
avi_write_trailer, |
775 |
}; |
776 |
|
777 |
int avienc_init(void) |
778 |
{ |
779 |
av_register_output_format(&avi_oformat); |
780 |
return 0; |
781 |
} |
782 |
#endif //CONFIG_MUXERS |