ba2785f6bcb796d3e85d7a0529912be76e17696e
[libav.git] / libavformat / avienc.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 typedef struct AVIIentry {
28 unsigned int flags, pos, len;
29 } AVIIentry;
30
31 #define AVI_INDEX_CLUSTER_SIZE 16384
32
33 typedef struct AVIIndex {
34 offset_t indx_start;
35 int entry;
36 int ents_allocated;
37 AVIIentry** cluster;
38 } AVIIndex;
39
40 typedef struct {
41 offset_t riff_start, movi_list, odml_list;
42 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
43 int audio_strm_length[MAX_STREAMS];
44 int riff_id;
45
46 AVIIndex indexes[MAX_STREAMS];
47 } AVIContext;
48
49 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
50 {
51 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
52 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
53 return &idx->cluster[cl][id];
54 }
55
56 offset_t start_tag(ByteIOContext *pb, const char *tag)
57 {
58 put_tag(pb, tag);
59 put_le32(pb, 0);
60 return url_ftell(pb);
61 }
62
63 void end_tag(ByteIOContext *pb, offset_t start)
64 {
65 offset_t pos;
66
67 pos = url_ftell(pb);
68 url_fseek(pb, start - 4, SEEK_SET);
69 put_le32(pb, (uint32_t)(pos - start));
70 url_fseek(pb, pos, SEEK_SET);
71 }
72
73 /* Note: when encoding, the first matching tag is used, so order is
74 important if multiple tags possible for a given codec. */
75 const CodecTag codec_bmp_tags[] = {
76 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
77 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
78 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
79
80 /* added based on MPlayer */
81 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
82 { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
83
84 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
85 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
86 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
87 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
88 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
89 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
90
91 /* added based on MPlayer */
92 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
93 { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
94 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
95 { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
96
97 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
98 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
99
100 /* added based on MPlayer */
101 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
102 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
103 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
104 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
105 { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
106 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
107 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
108
109 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
110
111 /* added based on MPlayer */
112 { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
113
114 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
115
116 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
117
118 /* added based on MPlayer */
119 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
120 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
121 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
122 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
123 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
124 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
125 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
126 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
127 { CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
128 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
129 { CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
130 { CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
131 { CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
132 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
133 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
134 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
135 { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
136 { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
137 { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
138 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
139 { CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
140 { CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
141 { CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
142 { CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
143 { CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
144 { 0, 0 },
145 };
146
147 unsigned int codec_get_tag(const CodecTag *tags, int id)
148 {
149 while (tags->id != 0) {
150 if (tags->id == id)
151 return tags->tag;
152 tags++;
153 }
154 return 0;
155 }
156
157 static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
158 {
159 while (tags->id != 0) {
160 if (!tags->invalid_asf && tags->id == id)
161 return tags->tag;
162 tags++;
163 }
164 return 0;
165 }
166
167 enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag)
168 {
169 while (tags->id != 0) {
170 if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF)
171 && toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF)
172 && toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF)
173 && toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF))
174 return tags->id;
175 tags++;
176 }
177 return CODEC_ID_NONE;
178 }
179
180 unsigned int codec_get_bmp_tag(int id)
181 {
182 return codec_get_tag(codec_bmp_tags, id);
183 }
184
185 unsigned int codec_get_wav_tag(int id)
186 {
187 return codec_get_tag(codec_wav_tags, id);
188 }
189
190 enum CodecID codec_get_bmp_id(unsigned int tag)
191 {
192 return codec_get_id(codec_bmp_tags, tag);
193 }
194
195 enum CodecID codec_get_wav_id(unsigned int tag)
196 {
197 return codec_get_id(codec_wav_tags, tag);
198 }
199
200 /* BITMAPINFOHEADER header */
201 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
202 {
203 put_le32(pb, 40 + enc->extradata_size); /* size */
204 put_le32(pb, enc->width);
205 put_le32(pb, enc->height);
206 put_le16(pb, 1); /* planes */
207
208 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
209 /* compression type */
210 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : enc->codec_tag);
211 put_le32(pb, enc->width * enc->height * 3);
212 put_le32(pb, 0);
213 put_le32(pb, 0);
214 put_le32(pb, 0);
215 put_le32(pb, 0);
216
217 put_buffer(pb, enc->extradata, enc->extradata_size);
218
219 if (enc->extradata_size & 1)
220 put_byte(pb, 0);
221 }
222
223 static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
224 {
225 switch(stream->codec_id) {
226 case CODEC_ID_PCM_S16LE:
227 *au_scale = *au_ssize = 2*stream->channels;
228 *au_byterate = *au_ssize * stream->sample_rate;
229 break;
230 case CODEC_ID_PCM_U8:
231 case CODEC_ID_PCM_ALAW:
232 case CODEC_ID_PCM_MULAW:
233 *au_scale = *au_ssize = stream->channels;
234 *au_byterate = *au_ssize * stream->sample_rate;
235 break;
236 case CODEC_ID_MP2:
237 *au_ssize = 1;
238 *au_scale = 1;
239 *au_byterate = stream->bit_rate / 8;
240 case CODEC_ID_MP3:
241 *au_ssize = 1;
242 *au_scale = 1;
243 *au_byterate = stream->bit_rate / 8;
244 default:
245 *au_ssize = 1;
246 *au_scale = 1;
247 *au_byterate = stream->bit_rate / 8;
248 break;
249 }
250 }
251
252 static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
253 const char* riff_tag, const char* list_tag)
254 {
255 offset_t loff;
256 int i;
257
258 avi->riff_id++;
259 for (i=0; i<MAX_STREAMS; i++)
260 avi->indexes[i].entry = 0;
261
262 avi->riff_start = start_tag(pb, "RIFF");
263 put_tag(pb, riff_tag);
264 loff = start_tag(pb, "LIST");
265 put_tag(pb, list_tag);
266 return loff;
267 }
268
269 static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
270 enum CodecType type)
271 {
272 tag[0] = '0';
273 tag[1] = '0' + index;
274 if (type == CODEC_TYPE_VIDEO) {
275 tag[2] = 'd';
276 tag[3] = 'c';
277 } else {
278 tag[2] = 'w';
279 tag[3] = 'b';
280 }
281 tag[4] = '\0';
282 return tag;
283 }
284
285 static int avi_write_header(AVFormatContext *s)
286 {
287 AVIContext *avi = s->priv_data;
288 ByteIOContext *pb = &s->pb;
289 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
290 AVCodecContext *stream, *video_enc;
291 offset_t list1, list2, strh, strf;
292
293 /* header list */
294 avi->riff_id = 0;
295 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
296
297 /* avi header */
298 put_tag(pb, "avih");
299 put_le32(pb, 14 * 4);
300 bitrate = 0;
301
302 video_enc = NULL;
303 for(n=0;n<s->nb_streams;n++) {
304 stream = &s->streams[n]->codec;
305 bitrate += stream->bit_rate;
306 if (stream->codec_type == CODEC_TYPE_VIDEO)
307 video_enc = stream;
308 }
309
310 nb_frames = 0;
311
312 if(video_enc){
313 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
314 } else {
315 put_le32(pb, 0);
316 }
317 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
318 put_le32(pb, 0); /* padding */
319 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
320 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
321 put_le32(pb, nb_frames); /* nb frames, filled later */
322 put_le32(pb, 0); /* initial frame */
323 put_le32(pb, s->nb_streams); /* nb streams */
324 put_le32(pb, 1024 * 1024); /* suggested buffer size */
325 if(video_enc){
326 put_le32(pb, video_enc->width);
327 put_le32(pb, video_enc->height);
328 } else {
329 put_le32(pb, 0);
330 put_le32(pb, 0);
331 }
332 put_le32(pb, 0); /* reserved */
333 put_le32(pb, 0); /* reserved */
334 put_le32(pb, 0); /* reserved */
335 put_le32(pb, 0); /* reserved */
336
337 /* stream list */
338 for(i=0;i<n;i++) {
339 list2 = start_tag(pb, "LIST");
340 put_tag(pb, "strl");
341
342 stream = &s->streams[i]->codec;
343
344 /* FourCC should really be set by the codec itself */
345 if (! stream->codec_tag) {
346 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
347 }
348
349 /* stream generic header */
350 strh = start_tag(pb, "strh");
351 switch(stream->codec_type) {
352 case CODEC_TYPE_VIDEO:
353 put_tag(pb, "vids");
354 put_le32(pb, stream->codec_tag);
355 put_le32(pb, 0); /* flags */
356 put_le16(pb, 0); /* priority */
357 put_le16(pb, 0); /* language */
358 put_le32(pb, 0); /* initial frame */
359
360 put_le32(pb, stream->frame_rate_base); /* scale */
361 put_le32(pb, stream->frame_rate); /* rate */
362
363 put_le32(pb, 0); /* start */
364 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
365 put_le32(pb, nb_frames); /* length, XXX: fill later */
366 put_le32(pb, 1024 * 1024); /* suggested buffer size */
367 put_le32(pb, -1); /* quality */
368 put_le32(pb, stream->width * stream->height * 3); /* sample size */
369 put_le16(pb, 0);
370 put_le16(pb, 0);
371 put_le16(pb, stream->width);
372 put_le16(pb, stream->height);
373 break;
374 case CODEC_TYPE_AUDIO:
375 put_tag(pb, "auds");
376 put_le32(pb, 1); /* tag */
377 put_le32(pb, 0); /* flags */
378 put_le16(pb, 0); /* priority */
379 put_le16(pb, 0); /* language */
380 put_le32(pb, 0); /* initial frame */
381 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
382 put_le32(pb, au_scale); /* scale */
383 put_le32(pb, au_byterate); /* rate */
384 put_le32(pb, 0); /* start */
385 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
386 put_le32(pb, 0); /* length, XXX: filled later */
387 put_le32(pb, 12 * 1024); /* suggested buffer size */
388 put_le32(pb, -1); /* quality */
389 put_le32(pb, au_ssize); /* sample size */
390 put_le32(pb, 0);
391 put_le32(pb, 0);
392 break;
393 default:
394 av_abort();
395 }
396 end_tag(pb, strh);
397
398 strf = start_tag(pb, "strf");
399 switch(stream->codec_type) {
400 case CODEC_TYPE_VIDEO:
401 put_bmp_header(pb, stream, codec_bmp_tags, 0);
402 break;
403 case CODEC_TYPE_AUDIO:
404 if (put_wav_header(pb, stream) < 0) {
405 av_free(avi);
406 return -1;
407 }
408 break;
409 default:
410 av_abort();
411 }
412 end_tag(pb, strf);
413
414 if (!url_is_streamed(pb)) {
415 unsigned char tag[5];
416 int j;
417
418 /* Starting to lay out AVI OpenDML master index.
419 * We want to make it JUNK entry for now, since we'd
420 * like to get away without making AVI an OpenDML one
421 * for compatibility reasons.
422 */
423 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
424 avi->indexes[i].indx_start = start_tag(pb, "JUNK");
425 put_le16(pb, 4); /* wLongsPerEntry */
426 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
427 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
428 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
429 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
430 /* dwChunkId */
431 put_le64(pb, 0); /* dwReserved[3]
432 put_le32(pb, 0); Must be 0. */
433 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
434 put_le64(pb, 0);
435 end_tag(pb, avi->indexes[i].indx_start);
436 }
437
438 end_tag(pb, list2);
439 }
440
441 if (!url_is_streamed(pb)) {
442 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
443 avi->odml_list = start_tag(pb, "JUNK");
444 put_tag(pb, "odml");
445 put_tag(pb, "dmlh");
446 put_le32(pb, 248);
447 for (i = 0; i < 248; i+= 4)
448 put_le32(pb, 0);
449 end_tag(pb, avi->odml_list);
450 }
451
452 end_tag(pb, list1);
453
454 avi->movi_list = start_tag(pb, "LIST");
455 put_tag(pb, "movi");
456
457 put_flush_packet(pb);
458
459 return 0;
460 }
461
462 static int avi_write_ix(AVFormatContext *s)
463 {
464 ByteIOContext *pb = &s->pb;
465 AVIContext *avi = s->priv_data;
466 unsigned char tag[5];
467 unsigned char ix_tag[] = "ix00";
468 int i, j;
469
470 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
471 return -1;
472
473 for (i=0;i<s->nb_streams;i++) {
474 offset_t ix, pos;
475
476 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec.codec_type);
477 ix_tag[3] = '0' + i;
478
479 /* Writing AVI OpenDML leaf index chunk */
480 ix = url_ftell(pb);
481 put_tag(pb, &ix_tag[0]); /* ix?? */
482 put_le32(pb, avi->indexes[i].entry * 8 + 24);
483 /* chunk size */
484 put_le16(pb, 2); /* wLongsPerEntry */
485 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
486 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
487 put_le32(pb, avi->indexes[i].entry);
488 /* nEntriesInUse */
489 put_tag(pb, &tag[0]); /* dwChunkId */
490 put_le64(pb, avi->movi_list);/* qwBaseOffset */
491 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
492
493 for (j=0; j<avi->indexes[i].entry; j++) {
494 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
495 put_le32(pb, ie->pos + 8);
496 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
497 (ie->flags & 0x10 ? 0 : 0x80000000));
498 }
499 put_flush_packet(pb);
500 pos = url_ftell(pb);
501
502 /* Updating one entry in the AVI OpenDML master index */
503 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
504 put_tag(pb, "indx"); /* enabling this entry */
505 url_fskip(pb, 8);
506 put_le32(pb, avi->riff_id); /* nEntriesInUse */
507 url_fskip(pb, 16*avi->riff_id);
508 put_le64(pb, ix); /* qwOffset */
509 put_le32(pb, pos - ix); /* dwSize */
510 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
511
512 url_fseek(pb, pos, SEEK_SET);
513 }
514 return 0;
515 }
516
517 static int avi_write_idx1(AVFormatContext *s)
518 {
519 ByteIOContext *pb = &s->pb;
520 AVIContext *avi = s->priv_data;
521 offset_t file_size, idx_chunk;
522 int i, n, nb_frames, au_byterate, au_ssize, au_scale;
523 AVCodecContext *stream;
524 unsigned char tag[5];
525
526 if (!url_is_streamed(pb)) {
527 AVIIentry* ie = 0, *tie;
528 int entry[MAX_STREAMS];
529 int empty, stream_id = -1;
530
531 idx_chunk = start_tag(pb, "idx1");
532 memset(&entry[0], 0, sizeof(entry));
533 do {
534 empty = 1;
535 for (i=0; i<s->nb_streams; i++) {
536 if (avi->indexes[i].entry <= entry[i])
537 continue;
538
539 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
540 if (empty || tie->pos < ie->pos) {
541 ie = tie;
542 stream_id = i;
543 }
544 empty = 0;
545 }
546 if (!empty) {
547 avi_stream2fourcc(&tag[0], stream_id,
548 s->streams[stream_id]->codec.codec_type);
549 put_tag(pb, &tag[0]);
550 put_le32(pb, ie->flags);
551 put_le32(pb, ie->pos);
552 put_le32(pb, ie->len);
553 entry[stream_id]++;
554 }
555 } while (!empty);
556 end_tag(pb, idx_chunk);
557
558 /* Fill in frame/sample counters */
559 file_size = url_ftell(pb);
560 nb_frames = 0;
561 for(n=0;n<s->nb_streams;n++) {
562 if (avi->frames_hdr_strm[n] != 0) {
563 stream = &s->streams[n]->codec;
564 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
565 if (stream->codec_type == CODEC_TYPE_VIDEO) {
566 put_le32(pb, stream->frame_number);
567 if (nb_frames < stream->frame_number)
568 nb_frames = stream->frame_number;
569 } else {
570 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
571 put_le32(pb, stream->frame_number);
572 nb_frames += stream->frame_number;
573 } else {
574 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
575 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
576 }
577 }
578 }
579 }
580 if (avi->frames_hdr_all != 0) {
581 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
582 put_le32(pb, nb_frames);
583 }
584 url_fseek(pb, file_size, SEEK_SET);
585 }
586 return 0;
587 }
588
589 static int avi_write_packet(AVFormatContext *s, int stream_index,
590 const uint8_t *buf, int size, int64_t pts)
591 {
592 AVIContext *avi = s->priv_data;
593 ByteIOContext *pb = &s->pb;
594 unsigned char tag[5];
595 unsigned int flags;
596 AVCodecContext *enc;
597
598 if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) {
599 avi_write_ix(s);
600 end_tag(pb, avi->movi_list);
601
602 if (avi->riff_id == 1)
603 avi_write_idx1(s);
604
605 end_tag(pb, avi->riff_start);
606 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
607 }
608
609 enc = &s->streams[stream_index]->codec;
610 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
611 if (enc->codec_type == CODEC_TYPE_AUDIO) {
612 avi->audio_strm_length[stream_index] += size;
613 flags = 0x10;
614 } else
615 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
616
617 if (!url_is_streamed(&s->pb)) {
618 AVIIndex* idx = &avi->indexes[stream_index];
619 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
620 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
621 if (idx->ents_allocated <= idx->entry) {
622 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
623 if (!idx->cluster)
624 return -1;
625 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
626 if (!idx->cluster[cl])
627 return -1;
628 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
629 }
630
631 idx->cluster[cl][id].flags = flags;
632 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
633 idx->cluster[cl][id].len = size;
634 idx->entry++;
635 }
636
637 put_buffer(pb, tag, 4);
638 put_le32(pb, size);
639 put_buffer(pb, buf, size);
640 if (size & 1)
641 put_byte(pb, 0);
642
643 put_flush_packet(pb);
644 return 0;
645 }
646
647 static int avi_write_trailer(AVFormatContext *s)
648 {
649 AVIContext *avi = s->priv_data;
650 ByteIOContext *pb = &s->pb;
651 int res = 0;
652 int i, j, n, nb_frames;
653 offset_t file_size;
654
655 if (avi->riff_id == 1) {
656 end_tag(pb, avi->movi_list);
657 res = avi_write_idx1(s);
658 end_tag(pb, avi->riff_start);
659 } else {
660 avi_write_ix(s);
661 end_tag(pb, avi->movi_list);
662 end_tag(pb, avi->riff_start);
663
664 file_size = url_ftell(pb);
665 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
666 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
667 url_fskip(pb, 16);
668
669 for (n=nb_frames=0;n<s->nb_streams;n++) {
670 AVCodecContext *stream = &s->streams[n]->codec;
671 if (stream->codec_type == CODEC_TYPE_VIDEO) {
672 if (nb_frames < stream->frame_number)
673 nb_frames = stream->frame_number;
674 } else {
675 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
676 nb_frames += stream->frame_number;
677 }
678 }
679 }
680 put_le32(pb, nb_frames);
681 url_fseek(pb, file_size, SEEK_SET);
682 }
683 put_flush_packet(pb);
684
685 for (i=0; i<MAX_STREAMS; i++) {
686 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
687 av_free(avi->indexes[i].cluster[j]);
688 av_free(avi->indexes[i].cluster);
689 avi->indexes[i].cluster = NULL;
690 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
691 }
692
693 return res;
694 }
695
696 static AVOutputFormat avi_oformat = {
697 "avi",
698 "avi format",
699 "video/x-msvideo",
700 "avi",
701 sizeof(AVIContext),
702 CODEC_ID_MP2,
703 CODEC_ID_MPEG4,
704 avi_write_header,
705 avi_write_packet,
706 avi_write_trailer,
707 };
708
709 int avienc_init(void)
710 {
711 av_register_output_format(&avi_oformat);
712 return 0;
713 }