3 * Copyright (c) 2000 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "avio_internal.h"
30 #include "libavformat/avlanguage.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/dict.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/timestamp.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavcodec/raw.h"
43 * - fill all fields if non streamed (nb_frames for example)
46 typedef struct AVIIentry
{
53 #define AVI_INDEX_CLUSTER_SIZE 16384
55 typedef struct AVIIndex
{
57 int64_t audio_strm_offset
;
60 int master_odml_riff_id_base
;
64 typedef struct AVIContext
{
66 int64_t riff_start
, movi_list
, odml_list
;
67 int64_t frames_hdr_all
;
69 int write_channel_mask
;
72 typedef struct AVIStream
{
73 int64_t frames_hdr_strm
;
74 int64_t audio_strm_length
;
84 int64_t strh_flags_offset
;
86 uint32_t palette
[AVPALETTE_COUNT
];
87 uint32_t old_palette
[AVPALETTE_COUNT
];
91 static int avi_write_packet_internal(AVFormatContext
*s
, AVPacket
*pkt
);
93 static inline AVIIentry
*avi_get_ientry(const AVIIndex
*idx
, int ent_id
)
95 int cl
= ent_id
/ AVI_INDEX_CLUSTER_SIZE
;
96 int id
= ent_id
% AVI_INDEX_CLUSTER_SIZE
;
97 return &idx
->cluster
[cl
][id
];
100 static int avi_add_ientry(AVFormatContext
*s
, int stream_index
, char *tag
,
101 unsigned int flags
, unsigned int size
)
103 AVIContext
*avi
= s
->priv_data
;
104 AVIOContext
*pb
= s
->pb
;
105 AVIStream
*avist
= s
->streams
[stream_index
]->priv_data
;
106 AVIIndex
*idx
= &avist
->indexes
;
107 int cl
= idx
->entry
/ AVI_INDEX_CLUSTER_SIZE
;
108 int id
= idx
->entry
% AVI_INDEX_CLUSTER_SIZE
;
110 if (idx
->ents_allocated
<= idx
->entry
) {
111 idx
->cluster
= av_realloc_f(idx
->cluster
, sizeof(void*), cl
+1);
113 idx
->ents_allocated
= 0;
115 return AVERROR(ENOMEM
);
118 av_malloc(AVI_INDEX_CLUSTER_SIZE
* sizeof(AVIIentry
));
119 if (!idx
->cluster
[cl
])
120 return AVERROR(ENOMEM
);
121 idx
->ents_allocated
+= AVI_INDEX_CLUSTER_SIZE
;
125 memcpy(idx
->cluster
[cl
][id
].tag
, tag
, 4);
127 memset(idx
->cluster
[cl
][id
].tag
, 0, 4);
128 idx
->cluster
[cl
][id
].flags
= flags
;
129 idx
->cluster
[cl
][id
].pos
= avio_tell(pb
) - avi
->movi_list
;
130 idx
->cluster
[cl
][id
].len
= size
;
131 avist
->max_size
= FFMAX(avist
->max_size
, size
);
137 static int64_t avi_start_new_riff(AVFormatContext
*s
, AVIOContext
*pb
,
138 const char *riff_tag
, const char *list_tag
)
140 AVIContext
*avi
= s
->priv_data
;
145 for (i
= 0; i
< s
->nb_streams
; i
++) {
146 AVIStream
*avist
= s
->streams
[i
]->priv_data
;
147 avist
->indexes
.audio_strm_offset
= avist
->audio_strm_length
;
148 avist
->indexes
.entry
= 0;
151 avi
->riff_start
= ff_start_tag(pb
, "RIFF");
152 ffio_wfourcc(pb
, riff_tag
);
153 loff
= ff_start_tag(pb
, "LIST");
154 ffio_wfourcc(pb
, list_tag
);
158 static char *avi_stream2fourcc(char *tag
, int index
, enum AVMediaType type
)
160 tag
[0] = '0' + index
/ 10;
161 tag
[1] = '0' + index
% 10;
162 if (type
== AVMEDIA_TYPE_VIDEO
) {
165 } else if (type
== AVMEDIA_TYPE_SUBTITLE
) {
166 // note: this is not an official code
177 static int avi_write_counters(AVFormatContext
*s
, int riff_id
)
179 AVIOContext
*pb
= s
->pb
;
180 AVIContext
*avi
= s
->priv_data
;
181 int n
, au_byterate
, au_ssize
, au_scale
, nb_frames
= 0;
183 AVCodecParameters
*par
;
185 file_size
= avio_tell(pb
);
186 for (n
= 0; n
< s
->nb_streams
; n
++) {
187 AVIStream
*avist
= s
->streams
[n
]->priv_data
;
189 av_assert0(avist
->frames_hdr_strm
);
190 par
= s
->streams
[n
]->codecpar
;
191 avio_seek(pb
, avist
->frames_hdr_strm
, SEEK_SET
);
192 ff_parse_specific_params(s
->streams
[n
], &au_byterate
, &au_ssize
, &au_scale
);
194 avio_wl32(pb
, avist
->packet_count
);
196 avio_wl32(pb
, avist
->audio_strm_length
/ au_ssize
);
197 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
)
198 nb_frames
= FFMAX(nb_frames
, avist
->packet_count
);
201 av_assert0(avi
->frames_hdr_all
);
202 avio_seek(pb
, avi
->frames_hdr_all
, SEEK_SET
);
203 avio_wl32(pb
, nb_frames
);
205 avio_seek(pb
, file_size
, SEEK_SET
);
210 static void write_odml_master(AVFormatContext
*s
, int stream_index
)
212 AVIOContext
*pb
= s
->pb
;
213 AVStream
*st
= s
->streams
[stream_index
];
214 AVCodecParameters
*par
= st
->codecpar
;
215 AVIStream
*avist
= st
->priv_data
;
216 unsigned char tag
[5];
219 /* Starting to lay out AVI OpenDML master index.
220 * We want to make it JUNK entry for now, since we'd
221 * like to get away without making AVI an OpenDML one
222 * for compatibility reasons. */
223 avist
->indexes
.indx_start
= ff_start_tag(pb
, "JUNK");
224 avio_wl16(pb
, 4); /* wLongsPerEntry */
225 avio_w8(pb
, 0); /* bIndexSubType (0 == frame index) */
226 avio_w8(pb
, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
227 avio_wl32(pb
, 0); /* nEntriesInUse (will fill out later on) */
228 ffio_wfourcc(pb
, avi_stream2fourcc(tag
, stream_index
, par
->codec_type
));
230 avio_wl64(pb
, 0); /* dwReserved[3] */
231 avio_wl32(pb
, 0); /* Must be 0. */
232 for (j
= 0; j
< AVI_MASTER_INDEX_SIZE
* 2; j
++)
234 ff_end_tag(pb
, avist
->indexes
.indx_start
);
237 static int avi_write_header(AVFormatContext
*s
)
239 AVIContext
*avi
= s
->priv_data
;
240 AVIOContext
*pb
= s
->pb
;
241 int bitrate
, n
, i
, nb_frames
, au_byterate
, au_ssize
, au_scale
;
242 AVCodecParameters
*video_par
;
243 AVStream
*video_st
= NULL
;
244 int64_t list1
, list2
, strh
, strf
;
245 AVDictionaryEntry
*t
= NULL
;
248 if (s
->nb_streams
> AVI_MAX_STREAM_COUNT
) {
249 av_log(s
, AV_LOG_ERROR
, "AVI does not support >%d streams\n",
250 AVI_MAX_STREAM_COUNT
);
251 return AVERROR(EINVAL
);
254 for (n
= 0; n
< s
->nb_streams
; n
++) {
255 s
->streams
[n
]->priv_data
= av_mallocz(sizeof(AVIStream
));
256 if (!s
->streams
[n
]->priv_data
)
257 return AVERROR(ENOMEM
);
262 list1
= avi_start_new_riff(s
, pb
, "AVI ", "hdrl");
265 ffio_wfourcc(pb
, "avih");
266 avio_wl32(pb
, 14 * 4);
270 for (n
= 0; n
< s
->nb_streams
; n
++) {
271 AVCodecParameters
*par
= s
->streams
[n
]->codecpar
;
272 bitrate
+= par
->bit_rate
;
273 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
275 video_st
= s
->streams
[n
];
281 // TODO: should be avg_frame_rate
283 avio_wl32(pb
, (uint32_t) (INT64_C(1000000) * video_st
->time_base
.num
/
284 video_st
->time_base
.den
));
287 avio_wl32(pb
, bitrate
/ 8); /* XXX: not quite exact */
288 avio_wl32(pb
, 0); /* padding */
290 avio_wl32(pb
, AVIF_TRUSTCKTYPE
| AVIF_ISINTERLEAVED
); /* flags */
292 avio_wl32(pb
, AVIF_TRUSTCKTYPE
| AVIF_HASINDEX
| AVIF_ISINTERLEAVED
); /* flags */
293 avi
->frames_hdr_all
= avio_tell(pb
); /* remember this offset to fill later */
294 avio_wl32(pb
, nb_frames
); /* nb frames, filled later */
295 avio_wl32(pb
, 0); /* initial frame */
296 avio_wl32(pb
, s
->nb_streams
); /* nb streams */
297 avio_wl32(pb
, 1024 * 1024); /* suggested buffer size */
299 avio_wl32(pb
, video_par
->width
);
300 avio_wl32(pb
, video_par
->height
);
305 avio_wl32(pb
, 0); /* reserved */
306 avio_wl32(pb
, 0); /* reserved */
307 avio_wl32(pb
, 0); /* reserved */
308 avio_wl32(pb
, 0); /* reserved */
311 for (i
= 0; i
< n
; i
++) {
312 AVStream
*st
= s
->streams
[i
];
313 AVCodecParameters
*par
= st
->codecpar
;
314 AVIStream
*avist
= st
->priv_data
;
315 list2
= ff_start_tag(pb
, "LIST");
316 ffio_wfourcc(pb
, "strl");
318 /* stream generic header */
319 strh
= ff_start_tag(pb
, "strh");
320 switch (par
->codec_type
) {
321 case AVMEDIA_TYPE_SUBTITLE
:
322 // XSUB subtitles behave like video tracks, other subtitles
323 // are not (yet) supported.
324 if (par
->codec_id
!= AV_CODEC_ID_XSUB
) {
325 av_log(s
, AV_LOG_ERROR
,
326 "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
327 return AVERROR_PATCHWELCOME
;
329 case AVMEDIA_TYPE_VIDEO
:
330 ffio_wfourcc(pb
, "vids");
332 case AVMEDIA_TYPE_AUDIO
:
333 ffio_wfourcc(pb
, "auds");
335 // case AVMEDIA_TYPE_TEXT:
336 // ffio_wfourcc(pb, "txts");
338 case AVMEDIA_TYPE_DATA
:
339 ffio_wfourcc(pb
, "dats");
342 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
||
343 par
->codec_id
== AV_CODEC_ID_XSUB
)
344 avio_wl32(pb
, par
->codec_tag
);
347 avist
->strh_flags_offset
= avio_tell(pb
);
348 avio_wl32(pb
, 0); /* flags */
349 avio_wl16(pb
, 0); /* priority */
350 avio_wl16(pb
, 0); /* language */
351 avio_wl32(pb
, 0); /* initial frame */
353 ff_parse_specific_params(st
, &au_byterate
, &au_ssize
, &au_scale
);
355 if ( par
->codec_type
== AVMEDIA_TYPE_VIDEO
356 && par
->codec_id
!= AV_CODEC_ID_XSUB
357 && au_byterate
> 1000LL*au_scale
) {
361 avpriv_set_pts_info(st
, 64, au_scale
, au_byterate
);
362 if (par
->codec_id
== AV_CODEC_ID_XSUB
)
363 au_scale
= au_byterate
= 0;
365 avio_wl32(pb
, au_scale
); /* scale */
366 avio_wl32(pb
, au_byterate
); /* rate */
368 avio_wl32(pb
, 0); /* start */
369 /* remember this offset to fill later */
370 avist
->frames_hdr_strm
= avio_tell(pb
);
372 /* FIXME: this may be broken, but who cares */
373 avio_wl32(pb
, AVI_MAX_RIFF_SIZE
);
375 avio_wl32(pb
, 0); /* length, XXX: filled later */
377 /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
378 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
)
379 avio_wl32(pb
, 1024 * 1024);
380 else if (par
->codec_type
== AVMEDIA_TYPE_AUDIO
)
381 avio_wl32(pb
, 12 * 1024);
384 avio_wl32(pb
, -1); /* quality */
385 avio_wl32(pb
, au_ssize
); /* sample size */
387 avio_wl16(pb
, par
->width
);
388 avio_wl16(pb
, par
->height
);
389 ff_end_tag(pb
, strh
);
391 if (par
->codec_type
!= AVMEDIA_TYPE_DATA
) {
393 enum AVPixelFormat pix_fmt
;
395 strf
= ff_start_tag(pb
, "strf");
396 switch (par
->codec_type
) {
397 case AVMEDIA_TYPE_SUBTITLE
:
398 /* XSUB subtitles behave like video tracks, other subtitles
399 * are not (yet) supported. */
400 if (par
->codec_id
!= AV_CODEC_ID_XSUB
)
402 case AVMEDIA_TYPE_VIDEO
:
403 /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
405 && par
->codec_id
== AV_CODEC_ID_RAWVIDEO
406 && par
->format
== AV_PIX_FMT_RGB555LE
407 && par
->bits_per_coded_sample
== 15)
408 par
->bits_per_coded_sample
= 16;
409 avist
->pal_offset
= avio_tell(pb
) + 40;
410 ff_put_bmp_header(pb
, par
, ff_codec_bmp_tags
, 0, 0);
411 pix_fmt
= avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi
,
412 par
->bits_per_coded_sample
);
414 && par
->codec_id
== AV_CODEC_ID_RAWVIDEO
415 && par
->format
!= pix_fmt
416 && par
->format
!= AV_PIX_FMT_NONE
)
417 av_log(s
, AV_LOG_ERROR
, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
418 av_get_pix_fmt_name(par
->format
));
420 if (par
->format
== AV_PIX_FMT_PAL8
) {
421 if (par
->bits_per_coded_sample
< 0 || par
->bits_per_coded_sample
> 8) {
422 av_log(s
, AV_LOG_ERROR
, "PAL8 with %d bps is not allowed\n", par
->bits_per_coded_sample
);
423 return AVERROR(EINVAL
);
428 case AVMEDIA_TYPE_AUDIO
:
429 flags
= (avi
->write_channel_mask
== 0) ? FF_PUT_WAV_HEADER_SKIP_CHANNELMASK
: 0;
430 if ((ret
= ff_put_wav_header(s
, pb
, par
, flags
)) < 0)
434 av_log(s
, AV_LOG_ERROR
,
435 "Invalid or not supported codec type '%s' found in the input\n",
436 (char *)av_x_if_null(av_get_media_type_string(par
->codec_type
), "?"));
437 return AVERROR(EINVAL
);
439 ff_end_tag(pb
, strf
);
440 if ((t
= av_dict_get(st
->metadata
, "title", NULL
, 0))) {
441 ff_riff_write_info_tag(s
->pb
, "strn", t
->value
);
444 if (par
->codec_id
== AV_CODEC_ID_XSUB
445 && (t
= av_dict_get(s
->streams
[i
]->metadata
, "language", NULL
, 0))) {
446 const char* langstr
= ff_convert_lang_to(t
->value
, AV_LANG_ISO639_1
);
449 char* str
= av_asprintf("Subtitle - %s-xx;02", langstr
);
451 return AVERROR(ENOMEM
);
452 ff_riff_write_info_tag(s
->pb
, "strn", str
);
459 write_odml_master(s
, i
);
462 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
&&
463 st
->sample_aspect_ratio
.num
> 0 &&
464 st
->sample_aspect_ratio
.den
> 0) {
465 int vprp
= ff_start_tag(pb
, "vprp");
466 AVRational dar
= av_mul_q(st
->sample_aspect_ratio
,
467 (AVRational
) { par
->width
,
470 av_reduce(&num
, &den
, dar
.num
, dar
.den
, 0xFFFF);
472 avio_wl32(pb
, 0); // video format = unknown
473 avio_wl32(pb
, 0); // video standard = unknown
474 // TODO: should be avg_frame_rate
475 avio_wl32(pb
, (2LL*st
->time_base
.den
+ st
->time_base
.num
- 1) / (2LL * st
->time_base
.num
));
476 avio_wl32(pb
, par
->width
);
477 avio_wl32(pb
, par
->height
);
480 avio_wl32(pb
, par
->width
);
481 avio_wl32(pb
, par
->height
);
482 avio_wl32(pb
, 1); // progressive FIXME
484 avio_wl32(pb
, par
->height
);
485 avio_wl32(pb
, par
->width
);
486 avio_wl32(pb
, par
->height
);
487 avio_wl32(pb
, par
->width
);
493 ff_end_tag(pb
, vprp
);
496 ff_end_tag(pb
, list2
);
500 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
501 avi
->odml_list
= ff_start_tag(pb
, "JUNK");
502 ffio_wfourcc(pb
, "odml");
503 ffio_wfourcc(pb
, "dmlh");
505 for (i
= 0; i
< 248; i
+= 4)
507 ff_end_tag(pb
, avi
->odml_list
);
510 ff_end_tag(pb
, list1
);
512 ff_riff_write_info(s
);
515 padding
= s
->metadata_header_padding
;
519 /* some padding for easier tag editing */
521 list2
= ff_start_tag(pb
, "JUNK");
522 for (i
= padding
; i
> 0; i
-= 4)
524 ff_end_tag(pb
, list2
);
527 avi
->movi_list
= ff_start_tag(pb
, "LIST");
528 ffio_wfourcc(pb
, "movi");
535 static void update_odml_entry(AVFormatContext
*s
, int stream_index
, int64_t ix
, int size
)
537 AVIOContext
*pb
= s
->pb
;
538 AVIContext
*avi
= s
->priv_data
;
539 AVIStream
*avist
= s
->streams
[stream_index
]->priv_data
;
541 int au_byterate
, au_ssize
, au_scale
;
546 /* Updating one entry in the AVI OpenDML master index */
547 avio_seek(pb
, avist
->indexes
.indx_start
- 8, SEEK_SET
);
548 ffio_wfourcc(pb
, "indx"); /* enabling this entry */
550 avio_wl32(pb
, avi
->riff_id
- avist
->indexes
.master_odml_riff_id_base
); /* nEntriesInUse */
551 avio_skip(pb
, 16 * (avi
->riff_id
- avist
->indexes
.master_odml_riff_id_base
));
552 avio_wl64(pb
, ix
); /* qwOffset */
553 avio_wl32(pb
, size
); /* dwSize */
554 ff_parse_specific_params(s
->streams
[stream_index
], &au_byterate
, &au_ssize
, &au_scale
);
555 if (s
->streams
[stream_index
]->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
&& au_ssize
> 0) {
556 uint32_t audio_segm_size
= (avist
->audio_strm_length
- avist
->indexes
.audio_strm_offset
);
557 if ((audio_segm_size
% au_ssize
> 0) && !avist
->sample_requested
) {
558 avpriv_request_sample(s
, "OpenDML index duration for audio packets with partial frames");
559 avist
->sample_requested
= 1;
561 avio_wl32(pb
, audio_segm_size
/ au_ssize
); /* dwDuration (sample count) */
563 avio_wl32(pb
, avist
->indexes
.entry
); /* dwDuration (packet count) */
565 avio_seek(pb
, pos
, SEEK_SET
);
568 static int avi_write_ix(AVFormatContext
*s
)
570 AVIOContext
*pb
= s
->pb
;
571 AVIContext
*avi
= s
->priv_data
;
573 char ix_tag
[] = "ix00";
576 av_assert0(pb
->seekable
);
578 for (i
= 0; i
< s
->nb_streams
; i
++) {
579 AVIStream
*avist
= s
->streams
[i
]->priv_data
;
580 if (avi
->riff_id
- avist
->indexes
.master_odml_riff_id_base
== AVI_MASTER_INDEX_SIZE
) {
582 int size
= 8+2+1+1+4+8+4+4+16*AVI_MASTER_INDEX_SIZE
;
585 update_odml_entry(s
, i
, pos
, size
);
586 write_odml_master(s
, i
);
587 av_assert1(avio_tell(pb
) - pos
== size
);
588 avist
->indexes
.master_odml_riff_id_base
= avi
->riff_id
- 1;
590 av_assert0(avi
->riff_id
- avist
->indexes
.master_odml_riff_id_base
< AVI_MASTER_INDEX_SIZE
);
593 for (i
= 0; i
< s
->nb_streams
; i
++) {
594 AVIStream
*avist
= s
->streams
[i
]->priv_data
;
597 avi_stream2fourcc(tag
, i
, s
->streams
[i
]->codecpar
->codec_type
);
600 /* Writing AVI OpenDML leaf index chunk */
602 ffio_wfourcc(pb
, ix_tag
); /* ix?? */
603 avio_wl32(pb
, avist
->indexes
.entry
* 8 + 24);
605 avio_wl16(pb
, 2); /* wLongsPerEntry */
606 avio_w8(pb
, 0); /* bIndexSubType (0 == frame index) */
607 avio_w8(pb
, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
608 avio_wl32(pb
, avist
->indexes
.entry
);
610 ffio_wfourcc(pb
, tag
); /* dwChunkId */
611 avio_wl64(pb
, avi
->movi_list
); /* qwBaseOffset */
612 avio_wl32(pb
, 0); /* dwReserved_3 (must be 0) */
614 for (j
= 0; j
< avist
->indexes
.entry
; j
++) {
615 AVIIentry
*ie
= avi_get_ientry(&avist
->indexes
, j
);
616 avio_wl32(pb
, ie
->pos
+ 8);
617 avio_wl32(pb
, ((uint32_t) ie
->len
& ~0x80000000) |
618 (ie
->flags
& 0x10 ? 0 : 0x80000000));
621 update_odml_entry(s
, i
, ix
, avio_tell(pb
) - ix
);
626 static int avi_write_idx1(AVFormatContext
*s
)
628 AVIOContext
*pb
= s
->pb
;
629 AVIContext
*avi
= s
->priv_data
;
636 AVIIentry
*ie
= 0, *tie
;
637 int empty
, stream_id
= -1;
639 idx_chunk
= ff_start_tag(pb
, "idx1");
640 for (i
= 0; i
< s
->nb_streams
; i
++) {
641 avist
= s
->streams
[i
]->priv_data
;
647 for (i
= 0; i
< s
->nb_streams
; i
++) {
648 avist
= s
->streams
[i
]->priv_data
;
649 if (avist
->indexes
.entry
<= avist
->entry
)
652 tie
= avi_get_ientry(&avist
->indexes
, avist
->entry
);
653 if (empty
|| tie
->pos
< ie
->pos
) {
660 avist
= s
->streams
[stream_id
]->priv_data
;
662 ffio_wfourcc(pb
, ie
->tag
);
664 avi_stream2fourcc(tag
, stream_id
,
665 s
->streams
[stream_id
]->codecpar
->codec_type
);
666 ffio_wfourcc(pb
, tag
);
668 avio_wl32(pb
, ie
->flags
);
669 avio_wl32(pb
, ie
->pos
);
670 avio_wl32(pb
, ie
->len
);
674 ff_end_tag(pb
, idx_chunk
);
676 avi_write_counters(s
, avi
->riff_id
);
681 static int write_skip_frames(AVFormatContext
*s
, int stream_index
, int64_t dts
)
683 AVIStream
*avist
= s
->streams
[stream_index
]->priv_data
;
684 AVCodecParameters
*par
= s
->streams
[stream_index
]->codecpar
;
686 ff_dlog(s
, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts
), avist
->packet_count
, stream_index
);
687 while (par
->block_align
== 0 && dts
!= AV_NOPTS_VALUE
&&
688 dts
> avist
->packet_count
&& par
->codec_id
!= AV_CODEC_ID_XSUB
&& avist
->packet_count
) {
689 AVPacket empty_packet
;
691 if (dts
- avist
->packet_count
> 60000) {
692 av_log(s
, AV_LOG_ERROR
, "Too large number of skipped frames %"PRId64
" > 60000\n", dts
- avist
->packet_count
);
693 return AVERROR(EINVAL
);
696 av_init_packet(&empty_packet
);
697 empty_packet
.size
= 0;
698 empty_packet
.data
= NULL
;
699 empty_packet
.stream_index
= stream_index
;
700 avi_write_packet_internal(s
, &empty_packet
);
701 ff_dlog(s
, "dup dts:%s packet_count:%d\n", av_ts2str(dts
), avist
->packet_count
);
707 static int avi_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
709 const int stream_index
= pkt
->stream_index
;
710 AVCodecParameters
*par
= s
->streams
[stream_index
]->codecpar
;
713 if (par
->codec_id
== AV_CODEC_ID_H264
&& par
->codec_tag
== MKTAG('H','2','6','4') && pkt
->size
) {
714 ret
= ff_check_h264_startcode(s
, s
->streams
[stream_index
], pkt
);
719 if ((ret
= write_skip_frames(s
, stream_index
, pkt
->dts
)) < 0)
723 return avi_write_packet_internal(s
, pkt
); /* Passthrough */
725 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
726 AVIStream
*avist
= s
->streams
[stream_index
]->priv_data
;
727 AVIOContext
*pb
= s
->pb
;
728 AVPacket
*opkt
= pkt
;
730 if (par
->codec_id
== AV_CODEC_ID_RAWVIDEO
&& par
->codec_tag
== 0) {
731 int64_t bpc
= par
->bits_per_coded_sample
!= 15 ? par
->bits_per_coded_sample
: 16;
732 int expected_stride
= ((par
->width
* bpc
+ 31) >> 5)*4;
733 reshuffle_ret
= ff_reshuffle_raw_rgb(s
, &pkt
, par
, expected_stride
);
734 if (reshuffle_ret
< 0)
735 return reshuffle_ret
;
738 if (par
->format
== AV_PIX_FMT_PAL8
) {
739 ret
= ff_get_packet_palette(s
, opkt
, reshuffle_ret
, avist
->palette
);
743 int pal_size
= 1 << par
->bits_per_coded_sample
;
746 av_assert0(par
->bits_per_coded_sample
>= 0 && par
->bits_per_coded_sample
<= 8);
748 if (pb
->seekable
&& avist
->pal_offset
) {
749 int64_t cur_offset
= avio_tell(pb
);
750 avio_seek(pb
, avist
->pal_offset
, SEEK_SET
);
751 for (i
= 0; i
< pal_size
; i
++) {
752 uint32_t v
= avist
->palette
[i
];
753 avio_wl32(pb
, v
& 0xffffff);
755 avio_seek(pb
, cur_offset
, SEEK_SET
);
756 memcpy(avist
->old_palette
, avist
->palette
, pal_size
* 4);
757 avist
->pal_offset
= 0;
759 if (memcmp(avist
->palette
, avist
->old_palette
, pal_size
* 4)) {
760 unsigned char tag
[5];
761 avi_stream2fourcc(tag
, stream_index
, par
->codec_type
);
762 tag
[2] = 'p'; tag
[3] = 'c';
763 if (s
->pb
->seekable
) {
764 if (avist
->strh_flags_offset
) {
765 int64_t cur_offset
= avio_tell(pb
);
766 avio_seek(pb
, avist
->strh_flags_offset
, SEEK_SET
);
767 avio_wl32(pb
, AVISF_VIDEO_PALCHANGES
);
768 avio_seek(pb
, cur_offset
, SEEK_SET
);
769 avist
->strh_flags_offset
= 0;
771 ret
= avi_add_ientry(s
, stream_index
, tag
, AVIIF_NO_TIME
,
776 pc_tag
= ff_start_tag(pb
, tag
);
778 avio_w8(pb
, pal_size
& 0xFF);
779 avio_wl16(pb
, 0); // reserved
780 for (i
= 0; i
< pal_size
; i
++) {
781 uint32_t v
= avist
->palette
[i
];
784 ff_end_tag(pb
, pc_tag
);
785 memcpy(avist
->old_palette
, avist
->palette
, pal_size
* 4);
790 ret
= avi_write_packet_internal(s
, pkt
);
794 av_packet_free(&pkt
);
799 return avi_write_packet_internal(s
, pkt
);
802 static int avi_write_packet_internal(AVFormatContext
*s
, AVPacket
*pkt
)
804 unsigned char tag
[5];
805 unsigned int flags
= 0;
806 const int stream_index
= pkt
->stream_index
;
807 int size
= pkt
->size
;
808 AVIContext
*avi
= s
->priv_data
;
809 AVIOContext
*pb
= s
->pb
;
810 AVIStream
*avist
= s
->streams
[stream_index
]->priv_data
;
811 AVCodecParameters
*par
= s
->streams
[stream_index
]->codecpar
;
813 if (pkt
->dts
!= AV_NOPTS_VALUE
)
814 avist
->last_dts
= pkt
->dts
+ pkt
->duration
;
816 avist
->packet_count
++;
818 // Make sure to put an OpenDML chunk when the file size exceeds the limits
820 (avio_tell(pb
) - avi
->riff_start
> AVI_MAX_RIFF_SIZE
)) {
822 ff_end_tag(pb
, avi
->movi_list
);
824 if (avi
->riff_id
== 1)
827 ff_end_tag(pb
, avi
->riff_start
);
828 avi
->movi_list
= avi_start_new_riff(s
, pb
, "AVIX", "movi");
831 avi_stream2fourcc(tag
, stream_index
, par
->codec_type
);
832 if (pkt
->flags
& AV_PKT_FLAG_KEY
)
834 if (par
->codec_type
== AVMEDIA_TYPE_AUDIO
)
835 avist
->audio_strm_length
+= size
;
837 if (s
->pb
->seekable
) {
839 ret
= avi_add_ientry(s
, stream_index
, NULL
, flags
, size
);
844 avio_write(pb
, tag
, 4);
846 avio_write(pb
, pkt
->data
, size
);
853 static int avi_write_trailer(AVFormatContext
*s
)
855 AVIContext
*avi
= s
->priv_data
;
856 AVIOContext
*pb
= s
->pb
;
858 int i
, j
, n
, nb_frames
;
861 for (i
= 0; i
< s
->nb_streams
; i
++) {
862 AVIStream
*avist
= s
->streams
[i
]->priv_data
;
863 write_skip_frames(s
, i
, avist
->last_dts
);
867 if (avi
->riff_id
== 1) {
868 ff_end_tag(pb
, avi
->movi_list
);
869 res
= avi_write_idx1(s
);
870 ff_end_tag(pb
, avi
->riff_start
);
873 ff_end_tag(pb
, avi
->movi_list
);
874 ff_end_tag(pb
, avi
->riff_start
);
876 file_size
= avio_tell(pb
);
877 avio_seek(pb
, avi
->odml_list
- 8, SEEK_SET
);
878 ffio_wfourcc(pb
, "LIST"); /* Making this AVI OpenDML one */
881 for (n
= nb_frames
= 0; n
< s
->nb_streams
; n
++) {
882 AVCodecParameters
*par
= s
->streams
[n
]->codecpar
;
883 AVIStream
*avist
= s
->streams
[n
]->priv_data
;
885 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
886 if (nb_frames
< avist
->packet_count
)
887 nb_frames
= avist
->packet_count
;
889 if (par
->codec_id
== AV_CODEC_ID_MP2
||
890 par
->codec_id
== AV_CODEC_ID_MP3
)
891 nb_frames
+= avist
->packet_count
;
894 avio_wl32(pb
, nb_frames
);
895 avio_seek(pb
, file_size
, SEEK_SET
);
897 avi_write_counters(s
, avi
->riff_id
);
901 for (i
= 0; i
< s
->nb_streams
; i
++) {
902 AVIStream
*avist
= s
->streams
[i
]->priv_data
;
903 for (j
= 0; j
< avist
->indexes
.ents_allocated
/ AVI_INDEX_CLUSTER_SIZE
; j
++)
904 av_freep(&avist
->indexes
.cluster
[j
]);
905 av_freep(&avist
->indexes
.cluster
);
906 avist
->indexes
.ents_allocated
= avist
->indexes
.entry
= 0;
908 avio_seek(pb
, avist
->frames_hdr_strm
+ 4, SEEK_SET
);
909 avio_wl32(pb
, avist
->max_size
);
916 #define OFFSET(x) offsetof(AVIContext, x)
917 #define ENC AV_OPT_FLAG_ENCODING_PARAM
918 static const AVOption options
[] = {
919 { "write_channel_mask", "write channel mask into wave format header", OFFSET(write_channel_mask
), AV_OPT_TYPE_BOOL
, { .i64
= 1 }, 0, 1, ENC
},
923 static const AVClass avi_muxer_class
= {
924 .class_name
= "AVI muxer",
925 .item_name
= av_default_item_name
,
927 .version
= LIBAVUTIL_VERSION_INT
,
930 AVOutputFormat ff_avi_muxer
= {
932 .long_name
= NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
933 .mime_type
= "video/x-msvideo",
935 .priv_data_size
= sizeof(AVIContext
),
936 .audio_codec
= CONFIG_LIBMP3LAME
? AV_CODEC_ID_MP3
: AV_CODEC_ID_AC3
,
937 .video_codec
= AV_CODEC_ID_MPEG4
,
938 .write_header
= avi_write_header
,
939 .write_packet
= avi_write_packet
,
940 .write_trailer
= avi_write_trailer
,
941 .codec_tag
= (const AVCodecTag
* const []) {
942 ff_codec_bmp_tags
, ff_codec_wav_tags
, 0
944 .priv_class
= &avi_muxer_class
,