Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavformat / avienc.c
CommitLineData
2ba45a60
DM
1/*
2 * AVI muxer
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//#define DEBUG
23
24#include "avformat.h"
25#include "internal.h"
26#include "avi.h"
27#include "avio_internal.h"
28#include "riff.h"
29#include "mpegts.h"
30#include "libavformat/avlanguage.h"
31#include "libavutil/avstring.h"
32#include "libavutil/intreadwrite.h"
33#include "libavutil/dict.h"
34#include "libavutil/avassert.h"
35#include "libavutil/timestamp.h"
36#include "libavutil/pixdesc.h"
37#include "libavcodec/raw.h"
38
39/*
40 * TODO:
41 * - fill all fields if non streamed (nb_frames for example)
42 */
43
44typedef struct AVIIentry {
45 unsigned int flags, pos, len;
46} AVIIentry;
47
48#define AVI_INDEX_CLUSTER_SIZE 16384
49
50typedef struct AVIIndex {
51 int64_t indx_start;
52 int entry;
53 int ents_allocated;
54 AVIIentry** cluster;
55} AVIIndex;
56
57typedef struct {
58 int64_t riff_start, movi_list, odml_list;
59 int64_t frames_hdr_all;
60 int riff_id;
61} AVIContext;
62
63typedef struct {
64 int64_t frames_hdr_strm;
65 int64_t audio_strm_length;
66 int packet_count;
67 int entry;
68 int max_size;
69
f6fa7814
DM
70 int64_t last_dts;
71
2ba45a60
DM
72 AVIIndex indexes;
73} AVIStream;
74
f6fa7814
DM
75static int avi_write_packet(AVFormatContext *s, AVPacket *pkt);
76
77static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
2ba45a60
DM
78{
79 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
80 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
81 return &idx->cluster[cl][id];
82}
83
84static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
85 const char *riff_tag, const char *list_tag)
86{
87 AVIContext *avi = s->priv_data;
88 int64_t loff;
89 int i;
90
91 avi->riff_id++;
92 for (i = 0; i < s->nb_streams; i++) {
93 AVIStream *avist = s->streams[i]->priv_data;
94 avist->indexes.entry = 0;
95 }
96
97 avi->riff_start = ff_start_tag(pb, "RIFF");
98 ffio_wfourcc(pb, riff_tag);
99 loff = ff_start_tag(pb, "LIST");
100 ffio_wfourcc(pb, list_tag);
101 return loff;
102}
103
104static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
105{
106 tag[0] = '0' + index / 10;
107 tag[1] = '0' + index % 10;
108 if (type == AVMEDIA_TYPE_VIDEO) {
109 tag[2] = 'd';
110 tag[3] = 'c';
111 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
112 // note: this is not an official code
113 tag[2] = 's';
114 tag[3] = 'b';
115 } else {
116 tag[2] = 'w';
117 tag[3] = 'b';
118 }
119 tag[4] = '\0';
120 return tag;
121}
122
123static int avi_write_counters(AVFormatContext *s, int riff_id)
124{
125 AVIOContext *pb = s->pb;
126 AVIContext *avi = s->priv_data;
127 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
128 int64_t file_size;
129 AVCodecContext *stream;
130
131 file_size = avio_tell(pb);
132 for (n = 0; n < s->nb_streams; n++) {
133 AVIStream *avist = s->streams[n]->priv_data;
134
135 av_assert0(avist->frames_hdr_strm);
136 stream = s->streams[n]->codec;
137 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
138 ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
139 if (au_ssize == 0)
140 avio_wl32(pb, avist->packet_count);
141 else
142 avio_wl32(pb, avist->audio_strm_length / au_ssize);
143 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
144 nb_frames = FFMAX(nb_frames, avist->packet_count);
145 }
146 if (riff_id == 1) {
147 av_assert0(avi->frames_hdr_all);
148 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
149 avio_wl32(pb, nb_frames);
150 }
151 avio_seek(pb, file_size, SEEK_SET);
152
153 return 0;
154}
155
156static int avi_write_header(AVFormatContext *s)
157{
158 AVIContext *avi = s->priv_data;
159 AVIOContext *pb = s->pb;
160 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
161 AVCodecContext *video_enc;
162 AVStream *video_st = NULL;
163 int64_t list1, list2, strh, strf;
164 AVDictionaryEntry *t = NULL;
165 int padding;
166
167 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
168 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
169 AVI_MAX_STREAM_COUNT);
170 return AVERROR(EINVAL);
171 }
172
173 for (n = 0; n < s->nb_streams; n++) {
174 s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
175 if (!s->streams[n]->priv_data)
176 return AVERROR(ENOMEM);
177 }
178
179 /* header list */
180 avi->riff_id = 0;
181 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
182
183 /* avi header */
184 ffio_wfourcc(pb, "avih");
185 avio_wl32(pb, 14 * 4);
186 bitrate = 0;
187
188 video_enc = NULL;
189 for (n = 0; n < s->nb_streams; n++) {
190 AVCodecContext *codec = s->streams[n]->codec;
191 bitrate += codec->bit_rate;
192 if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
193 video_enc = codec;
194 video_st = s->streams[n];
195 }
196 }
197
198 nb_frames = 0;
199
200 // TODO: should be avg_frame_rate
201 if (video_st)
202 avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
203 video_st->time_base.den));
204 else
205 avio_wl32(pb, 0);
206 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
207 avio_wl32(pb, 0); /* padding */
208 if (!pb->seekable)
209 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
210 else
211 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
212 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
213 avio_wl32(pb, nb_frames); /* nb frames, filled later */
214 avio_wl32(pb, 0); /* initial frame */
215 avio_wl32(pb, s->nb_streams); /* nb streams */
216 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
217 if (video_enc) {
218 avio_wl32(pb, video_enc->width);
219 avio_wl32(pb, video_enc->height);
220 } else {
221 avio_wl32(pb, 0);
222 avio_wl32(pb, 0);
223 }
224 avio_wl32(pb, 0); /* reserved */
225 avio_wl32(pb, 0); /* reserved */
226 avio_wl32(pb, 0); /* reserved */
227 avio_wl32(pb, 0); /* reserved */
228
229 /* stream list */
230 for (i = 0; i < n; i++) {
231 AVStream *st = s->streams[i];
232 AVCodecContext *enc = st->codec;
233 AVIStream *avist = st->priv_data;
234 list2 = ff_start_tag(pb, "LIST");
235 ffio_wfourcc(pb, "strl");
236
237 /* stream generic header */
238 strh = ff_start_tag(pb, "strh");
239 switch (enc->codec_type) {
240 case AVMEDIA_TYPE_SUBTITLE:
241 // XSUB subtitles behave like video tracks, other subtitles
242 // are not (yet) supported.
243 if (enc->codec_id != AV_CODEC_ID_XSUB) {
244 av_log(s, AV_LOG_ERROR,
245 "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
246 return AVERROR_PATCHWELCOME;
247 }
248 case AVMEDIA_TYPE_VIDEO:
249 ffio_wfourcc(pb, "vids");
250 break;
251 case AVMEDIA_TYPE_AUDIO:
252 ffio_wfourcc(pb, "auds");
253 break;
254// case AVMEDIA_TYPE_TEXT:
255// ffio_wfourcc(pb, "txts");
256// break;
257 case AVMEDIA_TYPE_DATA:
258 ffio_wfourcc(pb, "dats");
259 break;
260 }
261 if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
262 enc->codec_id == AV_CODEC_ID_XSUB)
263 avio_wl32(pb, enc->codec_tag);
264 else
265 avio_wl32(pb, 1);
266 avio_wl32(pb, 0); /* flags */
267 avio_wl16(pb, 0); /* priority */
268 avio_wl16(pb, 0); /* language */
269 avio_wl32(pb, 0); /* initial frame */
270
271 ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
272
273 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
274 && enc->codec_id != AV_CODEC_ID_XSUB
275 && au_byterate > 1000LL*au_scale) {
276 au_byterate = 600;
277 au_scale = 1;
278 }
279 avpriv_set_pts_info(st, 64, au_scale, au_byterate);
280 if (enc->codec_id == AV_CODEC_ID_XSUB)
281 au_scale = au_byterate = 0;
282
283 avio_wl32(pb, au_scale); /* scale */
284 avio_wl32(pb, au_byterate); /* rate */
285
286 avio_wl32(pb, 0); /* start */
287 /* remember this offset to fill later */
288 avist->frames_hdr_strm = avio_tell(pb);
289 if (!pb->seekable)
290 /* FIXME: this may be broken, but who cares */
291 avio_wl32(pb, AVI_MAX_RIFF_SIZE);
292 else
293 avio_wl32(pb, 0); /* length, XXX: filled later */
294
295 /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
296 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
297 avio_wl32(pb, 1024 * 1024);
298 else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
299 avio_wl32(pb, 12 * 1024);
300 else
301 avio_wl32(pb, 0);
302 avio_wl32(pb, -1); /* quality */
303 avio_wl32(pb, au_ssize); /* sample size */
304 avio_wl32(pb, 0);
305 avio_wl16(pb, enc->width);
306 avio_wl16(pb, enc->height);
307 ff_end_tag(pb, strh);
308
309 if (enc->codec_type != AVMEDIA_TYPE_DATA) {
310 int ret;
311 enum AVPixelFormat pix_fmt;
312
313 strf = ff_start_tag(pb, "strf");
314 switch (enc->codec_type) {
315 case AVMEDIA_TYPE_SUBTITLE:
316 /* XSUB subtitles behave like video tracks, other subtitles
317 * are not (yet) supported. */
318 if (enc->codec_id != AV_CODEC_ID_XSUB)
319 break;
320 case AVMEDIA_TYPE_VIDEO:
321 /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
322 if ( !enc->codec_tag
323 && enc->codec_id == AV_CODEC_ID_RAWVIDEO
324 && enc->pix_fmt == AV_PIX_FMT_RGB555LE
325 && enc->bits_per_coded_sample == 15)
326 enc->bits_per_coded_sample = 16;
327 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0, 0);
328 pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
329 enc->bits_per_coded_sample);
330 if ( !enc->codec_tag
331 && enc->codec_id == AV_CODEC_ID_RAWVIDEO
332 && enc->pix_fmt != pix_fmt
333 && enc->pix_fmt != AV_PIX_FMT_NONE)
334 av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
335 av_get_pix_fmt_name(enc->pix_fmt));
336 break;
337 case AVMEDIA_TYPE_AUDIO:
338 if ((ret = ff_put_wav_header(pb, enc, 0)) < 0)
339 return ret;
340 break;
341 default:
342 av_log(s, AV_LOG_ERROR,
343 "Invalid or not supported codec type '%s' found in the input\n",
344 (char *)av_x_if_null(av_get_media_type_string(enc->codec_type), "?"));
345 return AVERROR(EINVAL);
346 }
347 ff_end_tag(pb, strf);
348 if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
349 ff_riff_write_info_tag(s->pb, "strn", t->value);
350 t = NULL;
351 }
352 if (enc->codec_id == AV_CODEC_ID_XSUB
353 && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
354 const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
355 t = NULL;
356 if (langstr) {
357 char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
358 ff_riff_write_info_tag(s->pb, "strn", str);
359 av_free(str);
360 }
361 }
362 }
363
364 if (pb->seekable) {
365 unsigned char tag[5];
366 int j;
367
368 /* Starting to lay out AVI OpenDML master index.
369 * We want to make it JUNK entry for now, since we'd
370 * like to get away without making AVI an OpenDML one
371 * for compatibility reasons. */
372 avist->indexes.entry = avist->indexes.ents_allocated = 0;
373 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
374 avio_wl16(pb, 4); /* wLongsPerEntry */
375 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
376 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
377 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
378 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, enc->codec_type));
379 /* dwChunkId */
380 avio_wl64(pb, 0); /* dwReserved[3] */
381 // avio_wl32(pb, 0); /* Must be 0. */
382 for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
383 avio_wl64(pb, 0);
384 ff_end_tag(pb, avist->indexes.indx_start);
385 }
386
387 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
388 st->sample_aspect_ratio.num > 0 &&
389 st->sample_aspect_ratio.den > 0) {
390 int vprp = ff_start_tag(pb, "vprp");
391 AVRational dar = av_mul_q(st->sample_aspect_ratio,
392 (AVRational) { enc->width,
393 enc->height });
394 int num, den;
395 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
396
397 avio_wl32(pb, 0); // video format = unknown
398 avio_wl32(pb, 0); // video standard = unknown
399 // TODO: should be avg_frame_rate
400 avio_wl32(pb, lrintf(1.0 / av_q2d(st->time_base)));
401 avio_wl32(pb, enc->width);
402 avio_wl32(pb, enc->height);
403 avio_wl16(pb, den);
404 avio_wl16(pb, num);
405 avio_wl32(pb, enc->width);
406 avio_wl32(pb, enc->height);
407 avio_wl32(pb, 1); // progressive FIXME
408
409 avio_wl32(pb, enc->height);
410 avio_wl32(pb, enc->width);
411 avio_wl32(pb, enc->height);
412 avio_wl32(pb, enc->width);
413 avio_wl32(pb, 0);
414 avio_wl32(pb, 0);
415
416 avio_wl32(pb, 0);
417 avio_wl32(pb, 0);
418 ff_end_tag(pb, vprp);
419 }
420
421 ff_end_tag(pb, list2);
422 }
423
424 if (pb->seekable) {
425 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
426 avi->odml_list = ff_start_tag(pb, "JUNK");
427 ffio_wfourcc(pb, "odml");
428 ffio_wfourcc(pb, "dmlh");
429 avio_wl32(pb, 248);
430 for (i = 0; i < 248; i += 4)
431 avio_wl32(pb, 0);
432 ff_end_tag(pb, avi->odml_list);
433 }
434
435 ff_end_tag(pb, list1);
436
437 ff_riff_write_info(s);
438
439
440 padding = s->metadata_header_padding;
441 if (padding < 0)
442 padding = 1016;
443
444 /* some padding for easier tag editing */
445 if (padding) {
446 list2 = ff_start_tag(pb, "JUNK");
447 for (i = padding; i > 0; i -= 4)
448 avio_wl32(pb, 0);
449 ff_end_tag(pb, list2);
450 }
451
452 avi->movi_list = ff_start_tag(pb, "LIST");
453 ffio_wfourcc(pb, "movi");
454
455 avio_flush(pb);
456
457 return 0;
458}
459
460static int avi_write_ix(AVFormatContext *s)
461{
462 AVIOContext *pb = s->pb;
463 AVIContext *avi = s->priv_data;
464 char tag[5];
465 char ix_tag[] = "ix00";
466 int i, j;
467
468 av_assert0(pb->seekable);
469
470 if (avi->riff_id > AVI_MASTER_INDEX_SIZE) {
471 av_log(s, AV_LOG_ERROR, "Invalid riff index %d > %d\n",
472 avi->riff_id, AVI_MASTER_INDEX_SIZE);
473 return AVERROR(EINVAL);
474 }
475
476 for (i = 0; i < s->nb_streams; i++) {
477 AVIStream *avist = s->streams[i]->priv_data;
478 int64_t ix, pos;
479
480 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
481 ix_tag[3] = '0' + i;
482
483 /* Writing AVI OpenDML leaf index chunk */
484 ix = avio_tell(pb);
485 ffio_wfourcc(pb, ix_tag); /* ix?? */
486 avio_wl32(pb, avist->indexes.entry * 8 + 24);
487 /* chunk size */
488 avio_wl16(pb, 2); /* wLongsPerEntry */
489 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
490 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
491 avio_wl32(pb, avist->indexes.entry);
492 /* nEntriesInUse */
493 ffio_wfourcc(pb, tag); /* dwChunkId */
494 avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
495 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
496
497 for (j = 0; j < avist->indexes.entry; j++) {
498 AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
499 avio_wl32(pb, ie->pos + 8);
500 avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
501 (ie->flags & 0x10 ? 0 : 0x80000000));
502 }
503 avio_flush(pb);
504 pos = avio_tell(pb);
505
506 /* Updating one entry in the AVI OpenDML master index */
507 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
508 ffio_wfourcc(pb, "indx"); /* enabling this entry */
509 avio_skip(pb, 8);
510 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
511 avio_skip(pb, 16 * avi->riff_id);
512 avio_wl64(pb, ix); /* qwOffset */
513 avio_wl32(pb, pos - ix); /* dwSize */
514 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
515
516 avio_seek(pb, pos, SEEK_SET);
517 }
518 return 0;
519}
520
521static int avi_write_idx1(AVFormatContext *s)
522{
523 AVIOContext *pb = s->pb;
524 AVIContext *avi = s->priv_data;
525 int64_t idx_chunk;
526 int i;
527 char tag[5];
528
529 if (pb->seekable) {
530 AVIStream *avist;
531 AVIIentry *ie = 0, *tie;
532 int empty, stream_id = -1;
533
534 idx_chunk = ff_start_tag(pb, "idx1");
535 for (i = 0; i < s->nb_streams; i++) {
536 avist = s->streams[i]->priv_data;
537 avist->entry = 0;
538 }
539
540 do {
541 empty = 1;
542 for (i = 0; i < s->nb_streams; i++) {
543 avist = s->streams[i]->priv_data;
544 if (avist->indexes.entry <= avist->entry)
545 continue;
546
547 tie = avi_get_ientry(&avist->indexes, avist->entry);
548 if (empty || tie->pos < ie->pos) {
549 ie = tie;
550 stream_id = i;
551 }
552 empty = 0;
553 }
554 if (!empty) {
555 avist = s->streams[stream_id]->priv_data;
556 avi_stream2fourcc(tag, stream_id,
557 s->streams[stream_id]->codec->codec_type);
558 ffio_wfourcc(pb, tag);
559 avio_wl32(pb, ie->flags);
560 avio_wl32(pb, ie->pos);
561 avio_wl32(pb, ie->len);
562 avist->entry++;
563 }
564 } while (!empty);
565 ff_end_tag(pb, idx_chunk);
566
567 avi_write_counters(s, avi->riff_id);
568 }
569 return 0;
570}
571
f6fa7814
DM
572static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
573{
574 AVIStream *avist = s->streams[stream_index]->priv_data;
575 AVCodecContext *enc = s->streams[stream_index]->codec;
576
577 av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
578 while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
579 dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
580 AVPacket empty_packet;
581
582 if (dts - avist->packet_count > 60000) {
583 av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
584 return AVERROR(EINVAL);
585 }
586
587 av_init_packet(&empty_packet);
588 empty_packet.size = 0;
589 empty_packet.data = NULL;
590 empty_packet.stream_index = stream_index;
591 avi_write_packet(s, &empty_packet);
592 av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
593 }
594
595 return 0;
596}
597
2ba45a60
DM
598static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
599{
600 unsigned char tag[5];
601 unsigned int flags = 0;
602 const int stream_index = pkt->stream_index;
603 int size = pkt->size;
604 AVIContext *avi = s->priv_data;
605 AVIOContext *pb = s->pb;
606 AVIStream *avist = s->streams[stream_index]->priv_data;
607 AVCodecContext *enc = s->streams[stream_index]->codec;
f6fa7814 608 int ret;
2ba45a60 609
f6fa7814
DM
610 if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
611 ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
2ba45a60
DM
612 if (ret < 0)
613 return ret;
614 }
2ba45a60 615
f6fa7814
DM
616 if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
617 return ret;
618
619 if (pkt->dts != AV_NOPTS_VALUE)
620 avist->last_dts = pkt->dts + pkt->duration;
2ba45a60 621
2ba45a60
DM
622 avist->packet_count++;
623
624 // Make sure to put an OpenDML chunk when the file size exceeds the limits
625 if (pb->seekable &&
626 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
627 avi_write_ix(s);
628 ff_end_tag(pb, avi->movi_list);
629
630 if (avi->riff_id == 1)
631 avi_write_idx1(s);
632
633 ff_end_tag(pb, avi->riff_start);
634 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
635 }
636
637 avi_stream2fourcc(tag, stream_index, enc->codec_type);
638 if (pkt->flags & AV_PKT_FLAG_KEY)
639 flags = 0x10;
640 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
641 avist->audio_strm_length += size;
642
643 if (s->pb->seekable) {
644 AVIIndex *idx = &avist->indexes;
645 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
646 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
647 if (idx->ents_allocated <= idx->entry) {
648 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
649 if (!idx->cluster) {
650 idx->ents_allocated = 0;
651 idx->entry = 0;
652 return AVERROR(ENOMEM);
653 }
654 idx->cluster[cl] =
655 av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
656 if (!idx->cluster[cl])
657 return AVERROR(ENOMEM);
658 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
659 }
660
661 idx->cluster[cl][id].flags = flags;
662 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
663 idx->cluster[cl][id].len = size;
664 avist->max_size = FFMAX(avist->max_size, size);
665 idx->entry++;
666 }
667
668 avio_write(pb, tag, 4);
669 avio_wl32(pb, size);
670 avio_write(pb, pkt->data, size);
671 if (size & 1)
672 avio_w8(pb, 0);
673
674 return 0;
675}
676
677static int avi_write_trailer(AVFormatContext *s)
678{
679 AVIContext *avi = s->priv_data;
680 AVIOContext *pb = s->pb;
681 int res = 0;
682 int i, j, n, nb_frames;
683 int64_t file_size;
684
f6fa7814
DM
685 for (i = 0; i < s->nb_streams; i++) {
686 AVIStream *avist = s->streams[i]->priv_data;
687 write_skip_frames(s, i, avist->last_dts);
688 }
689
2ba45a60
DM
690 if (pb->seekable) {
691 if (avi->riff_id == 1) {
692 ff_end_tag(pb, avi->movi_list);
693 res = avi_write_idx1(s);
694 ff_end_tag(pb, avi->riff_start);
695 } else {
696 avi_write_ix(s);
697 ff_end_tag(pb, avi->movi_list);
698 ff_end_tag(pb, avi->riff_start);
699
700 file_size = avio_tell(pb);
701 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
702 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
703 avio_skip(pb, 16);
704
705 for (n = nb_frames = 0; n < s->nb_streams; n++) {
706 AVCodecContext *stream = s->streams[n]->codec;
707 AVIStream *avist = s->streams[n]->priv_data;
708
709 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
710 if (nb_frames < avist->packet_count)
711 nb_frames = avist->packet_count;
712 } else {
713 if (stream->codec_id == AV_CODEC_ID_MP2 ||
714 stream->codec_id == AV_CODEC_ID_MP3)
715 nb_frames += avist->packet_count;
716 }
717 }
718 avio_wl32(pb, nb_frames);
719 avio_seek(pb, file_size, SEEK_SET);
720
721 avi_write_counters(s, avi->riff_id);
722 }
723 }
724
725 for (i = 0; i < s->nb_streams; i++) {
726 AVIStream *avist = s->streams[i]->priv_data;
727 for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
728 av_freep(&avist->indexes.cluster[j]);
729 av_freep(&avist->indexes.cluster);
730 avist->indexes.ents_allocated = avist->indexes.entry = 0;
731 if (pb->seekable) {
732 avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
733 avio_wl32(pb, avist->max_size);
734 }
735 }
736
737 return res;
738}
739
740AVOutputFormat ff_avi_muxer = {
741 .name = "avi",
742 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
743 .mime_type = "video/x-msvideo",
744 .extensions = "avi",
745 .priv_data_size = sizeof(AVIContext),
746 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
747 .video_codec = AV_CODEC_ID_MPEG4,
748 .write_header = avi_write_header,
749 .write_packet = avi_write_packet,
750 .write_trailer = avi_write_trailer,
751 .codec_tag = (const AVCodecTag * const []) {
752 ff_codec_bmp_tags, ff_codec_wav_tags, 0
753 },
754};