de6d9b64 |
/* |
7fbde343 |
* AVI muxer |
406792e7 |
* Copyright (c) 2000 Fabrice Bellard |
de6d9b64 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
19720f15 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
de6d9b64 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. |
de6d9b64 |
* |
19720f15 |
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 |
*/ |
f35d5f96 |
//#define DEBUG
|
de6d9b64 |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
de6d9b64 |
#include "avi.h" |
0abdb293 |
#include "avio_internal.h" |
9d9f4119 |
#include "riff.h" |
04d2540c |
#include "libavutil/intreadwrite.h" |
d2d67e42 |
#include "libavutil/dict.h" |
eaf65538 |
#include "libavutil/avassert.h" |
f35d5f96 |
#include "libavutil/timestamp.h" |
de6d9b64 |
/* |
115329f1 |
* TODO: |
de6d9b64 |
* - fill all fields if non streamed (nb_frames for example)
*/
|
e738cee9 |
typedef struct AVIIentry { |
de6d9b64 |
unsigned int flags, pos, len; |
e738cee9 |
} AVIIentry;
#define AVI_INDEX_CLUSTER_SIZE 16384
typedef struct AVIIndex { |
bc5c918e |
int64_t indx_start; |
e738cee9 |
int entry;
int ents_allocated;
AVIIentry** cluster; |
de6d9b64 |
} AVIIndex;
typedef struct { |
bc5c918e |
int64_t riff_start, movi_list, odml_list; |
b4f0e04c |
int64_t frames_hdr_all; |
e738cee9 |
int riff_id; |
de6d9b64 |
} AVIContext;
|
b4f0e04c |
typedef struct {
int64_t frames_hdr_strm;
int audio_strm_length;
int packet_count; |
12f1b1fe |
int entry; |
b4f0e04c |
AVIIndex indexes;
} AVIStream ;
|
115329f1 |
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id) |
e738cee9 |
{
int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
return &idx->cluster[cl][id];
}
|
ae628ec1 |
static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb, |
bc5c918e |
const char* riff_tag, const char* list_tag) |
ce9fce63 |
{ |
b4f0e04c |
AVIContext *avi= s->priv_data; |
bc5c918e |
int64_t loff; |
e738cee9 |
int i; |
115329f1 |
|
e738cee9 |
avi->riff_id++; |
b4f0e04c |
for (i=0; i<s->nb_streams; i++){
AVIStream *avist= s->streams[i]->priv_data;
avist->indexes.entry = 0;
} |
115329f1 |
|
1a40491e |
avi->riff_start = ff_start_tag(pb, "RIFF"); |
0abdb293 |
ffio_wfourcc(pb, riff_tag); |
1a40491e |
loff = ff_start_tag(pb, "LIST"); |
0abdb293 |
ffio_wfourcc(pb, list_tag); |
ce9fce63 |
return loff;
}
|
72415b2a |
static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type) |
e738cee9 |
{ |
4acc94e9 |
tag[0] = '0' + index/10;
tag[1] = '0' + index%10; |
72415b2a |
if (type == AVMEDIA_TYPE_VIDEO) { |
e738cee9 |
tag[2] = 'd';
tag[3] = 'c'; |
72415b2a |
} else if (type == AVMEDIA_TYPE_SUBTITLE) { |
2a04d6e7 |
// note: this is not an official code
tag[2] = 's';
tag[3] = 'b'; |
e738cee9 |
} else {
tag[2] = 'w';
tag[3] = 'b';
}
tag[4] = '\0';
return tag;
}
|
5bce834e |
static int avi_write_counters(AVFormatContext* s, int riff_id)
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
5bce834e |
AVIContext *avi = s->priv_data;
int n, au_byterate, au_ssize, au_scale, nb_frames = 0; |
bc5c918e |
int64_t file_size; |
5bce834e |
AVCodecContext* stream;
|
a2704c97 |
file_size = avio_tell(pb); |
5bce834e |
for(n = 0; n < s->nb_streams; n++) { |
b4f0e04c |
AVIStream *avist= s->streams[n]->priv_data;
|
eaf65538 |
av_assert0(avist->frames_hdr_strm); |
5bce834e |
stream = s->streams[n]->codec; |
6b4aa5da |
avio_seek(pb, avist->frames_hdr_strm, SEEK_SET); |
5bce834e |
ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
if(au_ssize == 0) { |
77eb5504 |
avio_wl32(pb, avist->packet_count); |
5bce834e |
} else { |
77eb5504 |
avio_wl32(pb, avist->audio_strm_length / au_ssize); |
5bce834e |
} |
72415b2a |
if(stream->codec_type == AVMEDIA_TYPE_VIDEO) |
b4f0e04c |
nb_frames = FFMAX(nb_frames, avist->packet_count); |
5bce834e |
}
if(riff_id == 1) { |
eaf65538 |
av_assert0(avi->frames_hdr_all); |
6b4aa5da |
avio_seek(pb, avi->frames_hdr_all, SEEK_SET); |
77eb5504 |
avio_wl32(pb, nb_frames); |
5bce834e |
} |
6b4aa5da |
avio_seek(pb, file_size, SEEK_SET); |
5bce834e |
return 0;
}
|
de6d9b64 |
static int avi_write_header(AVFormatContext *s)
{ |
c9a65ca8 |
AVIContext *avi = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
86bec9a1 |
int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale; |
de6d9b64 |
AVCodecContext *stream, *video_enc; |
bc5c918e |
int64_t list1, list2, strh, strf; |
d2d67e42 |
AVDictionaryEntry *t = NULL; |
de6d9b64 |
|
4acc94e9 |
if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
AVI_MAX_STREAM_COUNT); |
a0f2d3ff |
return AVERROR(EINVAL); |
4acc94e9 |
}
|
b4f0e04c |
for(n=0;n<s->nb_streams;n++) {
s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
if(!s->streams[n]->priv_data)
return AVERROR(ENOMEM);
}
|
de6d9b64 |
/* header list */ |
e738cee9 |
avi->riff_id = 0; |
b4f0e04c |
list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl"); |
de6d9b64 |
/* avi header */ |
0abdb293 |
ffio_wfourcc(pb, "avih"); |
77eb5504 |
avio_wl32(pb, 14 * 4); |
de6d9b64 |
bitrate = 0;
video_enc = NULL;
for(n=0;n<s->nb_streams;n++) { |
01f4895c |
stream = s->streams[n]->codec; |
de6d9b64 |
bitrate += stream->bit_rate; |
72415b2a |
if (stream->codec_type == AVMEDIA_TYPE_VIDEO) |
de6d9b64 |
video_enc = stream;
} |
115329f1 |
|
de6d9b64 |
nb_frames = 0;
|
850742d7 |
if(video_enc){ |
77eb5504 |
avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den)); |
850742d7 |
} else { |
77eb5504 |
avio_wl32(pb, 0); |
850742d7 |
} |
77eb5504 |
avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
avio_wl32(pb, 0); /* padding */ |
8978feda |
if (!pb->seekable) |
77eb5504 |
avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */ |
a2a2668d |
else |
77eb5504 |
avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */ |
a2704c97 |
avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */ |
77eb5504 |
avio_wl32(pb, nb_frames); /* nb frames, filled later */
avio_wl32(pb, 0); /* initial frame */
avio_wl32(pb, s->nb_streams); /* nb streams */
avio_wl32(pb, 1024 * 1024); /* suggested buffer size */ |
115329f1 |
if(video_enc){ |
77eb5504 |
avio_wl32(pb, video_enc->width);
avio_wl32(pb, video_enc->height); |
850742d7 |
} else { |
77eb5504 |
avio_wl32(pb, 0);
avio_wl32(pb, 0); |
115329f1 |
} |
77eb5504 |
avio_wl32(pb, 0); /* reserved */
avio_wl32(pb, 0); /* reserved */
avio_wl32(pb, 0); /* reserved */
avio_wl32(pb, 0); /* reserved */ |
115329f1 |
|
de6d9b64 |
/* stream list */
for(i=0;i<n;i++) { |
b4f0e04c |
AVIStream *avist= s->streams[i]->priv_data; |
1a40491e |
list2 = ff_start_tag(pb, "LIST"); |
0abdb293 |
ffio_wfourcc(pb, "strl"); |
115329f1 |
|
01f4895c |
stream = s->streams[i]->codec; |
de6d9b64 |
/* stream generic header */ |
1a40491e |
strh = ff_start_tag(pb, "strh"); |
de6d9b64 |
switch(stream->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_SUBTITLE: |
2a04d6e7 |
// XSUB subtitles behave like video tracks, other subtitles
// are not (yet) supported. |
36ef5369 |
if (stream->codec_id != AV_CODEC_ID_XSUB) { |
d6a93490 |
av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
return AVERROR_PATCHWELCOME;
} |
0abdb293 |
case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
// case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break; |
de6d9b64 |
} |
72415b2a |
if(stream->codec_type == AVMEDIA_TYPE_VIDEO || |
36ef5369 |
stream->codec_id == AV_CODEC_ID_XSUB) |
77eb5504 |
avio_wl32(pb, stream->codec_tag); |
1d92cc2b |
else |
77eb5504 |
avio_wl32(pb, 1);
avio_wl32(pb, 0); /* flags */
avio_wl16(pb, 0); /* priority */
avio_wl16(pb, 0); /* language */
avio_wl32(pb, 0); /* initial frame */ |
1d92cc2b |
ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
|
bacebe1f |
if ( stream->codec_type == AVMEDIA_TYPE_VIDEO
&& stream->codec_id != AV_CODEC_ID_XSUB
&& au_byterate > 1000LL*au_scale) {
au_byterate = 600;
au_scale = 1;
} |
87585129 |
avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate); |
7a72695c |
if(stream->codec_id == AV_CODEC_ID_XSUB) |
87585129 |
au_scale = au_byterate = 0;
|
77eb5504 |
avio_wl32(pb, au_scale); /* scale */
avio_wl32(pb, au_byterate); /* rate */ |
1d92cc2b |
|
77eb5504 |
avio_wl32(pb, 0); /* start */ |
a2704c97 |
avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */ |
8978feda |
if (!pb->seekable) |
77eb5504 |
avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */ |
bb270c08 |
else |
77eb5504 |
avio_wl32(pb, 0); /* length, XXX: filled later */ |
115329f1 |
|
1d92cc2b |
/* suggested buffer size */ //FIXME set at the end to largest chunk |
72415b2a |
if(stream->codec_type == AVMEDIA_TYPE_VIDEO) |
77eb5504 |
avio_wl32(pb, 1024 * 1024); |
72415b2a |
else if(stream->codec_type == AVMEDIA_TYPE_AUDIO) |
77eb5504 |
avio_wl32(pb, 12 * 1024); |
1d92cc2b |
else |
77eb5504 |
avio_wl32(pb, 0);
avio_wl32(pb, -1); /* quality */
avio_wl32(pb, au_ssize); /* sample size */
avio_wl32(pb, 0);
avio_wl16(pb, stream->width);
avio_wl16(pb, stream->height); |
1a40491e |
ff_end_tag(pb, strh); |
de6d9b64 |
|
72415b2a |
if(stream->codec_type != AVMEDIA_TYPE_DATA){ |
a0f2d3ff |
int ret;
|
1a40491e |
strf = ff_start_tag(pb, "strf"); |
de6d9b64 |
switch(stream->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_SUBTITLE: |
2a04d6e7 |
// XSUB subtitles behave like video tracks, other subtitles
// are not (yet) supported. |
36ef5369 |
if (stream->codec_id != AV_CODEC_ID_XSUB) break; |
72415b2a |
case AVMEDIA_TYPE_VIDEO: |
1a40491e |
ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0); |
de6d9b64 |
break; |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
a0f2d3ff |
if ((ret = ff_put_wav_header(pb, stream)) < 0) {
return ret; |
46a3d068 |
} |
de6d9b64 |
break; |
53cafac0 |
default: |
a0f2d3ff |
av_log(s, AV_LOG_ERROR,
"Invalid or not supported codec type '%s' found in the input\n",
(char *)av_x_if_null(av_get_media_type_string(stream->codec_type), "?"));
return AVERROR(EINVAL); |
de6d9b64 |
} |
1a40491e |
ff_end_tag(pb, strf); |
d2d67e42 |
if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) { |
71e92414 |
ff_riff_write_info_tag(s->pb, "strn", t->value); |
04d2540c |
t = NULL;
} |
1d92cc2b |
} |
115329f1 |
|
8978feda |
if (pb->seekable) { |
bb270c08 |
unsigned char tag[5];
int j; |
115329f1 |
/* Starting to lay out AVI OpenDML master index. |
bb270c08 |
* We want to make it JUNK entry for now, since we'd
* like to get away without making AVI an OpenDML one
* for compatibility reasons.
*/ |
b4f0e04c |
avist->indexes.entry = avist->indexes.ents_allocated = 0;
avist->indexes.indx_start = ff_start_tag(pb, "JUNK"); |
77eb5504 |
avio_wl16(pb, 4); /* wLongsPerEntry */
avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */ |
99f42c27 |
ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type)); |
bb270c08 |
/* dwChunkId */ |
77eb5504 |
avio_wl64(pb, 0); /* dwReserved[3]
avio_wl32(pb, 0); Must be 0. */ |
bb270c08 |
for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++) |
77eb5504 |
avio_wl64(pb, 0); |
b4f0e04c |
ff_end_tag(pb, avist->indexes.indx_start); |
bb270c08 |
} |
115329f1 |
|
72415b2a |
if( stream->codec_type == AVMEDIA_TYPE_VIDEO |
59729451 |
&& s->streams[i]->sample_aspect_ratio.num>0
&& s->streams[i]->sample_aspect_ratio.den>0){ |
1a40491e |
int vprp= ff_start_tag(pb, "vprp"); |
59729451 |
AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio, |
5b087549 |
(AVRational){stream->width, stream->height});
int num, den;
av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
|
77eb5504 |
avio_wl32(pb, 0); //video format = unknown
avio_wl32(pb, 0); //video standard= unknown
avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
avio_wl32(pb, stream->width );
avio_wl32(pb, stream->height);
avio_wl16(pb, den);
avio_wl16(pb, num);
avio_wl32(pb, stream->width );
avio_wl32(pb, stream->height);
avio_wl32(pb, 1); //progressive FIXME
avio_wl32(pb, stream->height);
avio_wl32(pb, stream->width );
avio_wl32(pb, stream->height);
avio_wl32(pb, stream->width );
avio_wl32(pb, 0);
avio_wl32(pb, 0);
avio_wl32(pb, 0);
avio_wl32(pb, 0); |
1a40491e |
ff_end_tag(pb, vprp); |
5b087549 |
}
|
1a40491e |
ff_end_tag(pb, list2); |
de6d9b64 |
} |
115329f1 |
|
8978feda |
if (pb->seekable) { |
e738cee9 |
/* AVI could become an OpenDML one, if it grows beyond 2Gb range */ |
1a40491e |
avi->odml_list = ff_start_tag(pb, "JUNK"); |
0abdb293 |
ffio_wfourcc(pb, "odml");
ffio_wfourcc(pb, "dmlh"); |
77eb5504 |
avio_wl32(pb, 248); |
e738cee9 |
for (i = 0; i < 248; i+= 4) |
77eb5504 |
avio_wl32(pb, 0); |
1a40491e |
ff_end_tag(pb, avi->odml_list); |
e738cee9 |
} |
de6d9b64 |
|
1a40491e |
ff_end_tag(pb, list1); |
115329f1 |
|
71e92414 |
ff_riff_write_info(s); |
57060f89 |
/* some padding for easier tag editing */ |
1a40491e |
list2 = ff_start_tag(pb, "JUNK"); |
57060f89 |
for (i = 0; i < 1016; i += 4) |
77eb5504 |
avio_wl32(pb, 0); |
1a40491e |
ff_end_tag(pb, list2); |
57060f89 |
|
1a40491e |
avi->movi_list = ff_start_tag(pb, "LIST"); |
0abdb293 |
ffio_wfourcc(pb, "movi"); |
de6d9b64 |
|
b7f2fdde |
avio_flush(pb); |
de6d9b64 |
return 0;
}
|
e738cee9 |
static int avi_write_ix(AVFormatContext *s)
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
e738cee9 |
AVIContext *avi = s->priv_data; |
191e8ca7 |
char tag[5];
char ix_tag[] = "ix00"; |
e738cee9 |
int i, j; |
115329f1 |
|
eaf65538 |
av_assert0(pb->seekable); |
a2a2668d |
|
a0f2d3ff |
if (avi->riff_id > AVI_MASTER_INDEX_SIZE) {
av_log(s, AV_LOG_ERROR, "Invalid riff index %d > %d\n",
avi->riff_id, AVI_MASTER_INDEX_SIZE);
return AVERROR(EINVAL);
} |
115329f1 |
|
e738cee9 |
for (i=0;i<s->nb_streams;i++) { |
b4f0e04c |
AVIStream *avist= s->streams[i]->priv_data; |
bc5c918e |
int64_t ix, pos; |
115329f1 |
|
99f42c27 |
avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type); |
bb270c08 |
ix_tag[3] = '0' + i; |
115329f1 |
|
bb270c08 |
/* Writing AVI OpenDML leaf index chunk */ |
a2704c97 |
ix = avio_tell(pb); |
99f42c27 |
ffio_wfourcc(pb, ix_tag); /* ix?? */ |
77eb5504 |
avio_wl32(pb, avist->indexes.entry * 8 + 24); |
bb270c08 |
/* chunk size */ |
77eb5504 |
avio_wl16(pb, 2); /* wLongsPerEntry */
avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
avio_wl32(pb, avist->indexes.entry); |
bb270c08 |
/* nEntriesInUse */ |
99f42c27 |
ffio_wfourcc(pb, tag); /* dwChunkId */ |
77eb5504 |
avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */ |
e738cee9 |
|
b4f0e04c |
for (j=0; j<avist->indexes.entry; j++) {
AVIIentry* ie = avi_get_ientry(&avist->indexes, j); |
77eb5504 |
avio_wl32(pb, ie->pos + 8);
avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) | |
bb270c08 |
(ie->flags & 0x10 ? 0 : 0x80000000)); |
e738cee9 |
} |
b7f2fdde |
avio_flush(pb); |
a2704c97 |
pos = avio_tell(pb); |
115329f1 |
|
bb270c08 |
/* Updating one entry in the AVI OpenDML master index */ |
6b4aa5da |
avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET); |
0abdb293 |
ffio_wfourcc(pb, "indx"); /* enabling this entry */ |
45a8a02a |
avio_skip(pb, 8); |
77eb5504 |
avio_wl32(pb, avi->riff_id); /* nEntriesInUse */ |
45a8a02a |
avio_skip(pb, 16*avi->riff_id); |
77eb5504 |
avio_wl64(pb, ix); /* qwOffset */
avio_wl32(pb, pos - ix); /* dwSize */
avio_wl32(pb, avist->indexes.entry); /* dwDuration */ |
bb270c08 |
|
6b4aa5da |
avio_seek(pb, pos, SEEK_SET); |
e738cee9 |
}
return 0;
}
static int avi_write_idx1(AVFormatContext *s) |
ce9fce63 |
{ |
ae628ec1 |
AVIOContext *pb = s->pb; |
ce9fce63 |
AVIContext *avi = s->priv_data; |
bc5c918e |
int64_t idx_chunk; |
5bce834e |
int i; |
191e8ca7 |
char tag[5]; |
ce9fce63 |
|
8978feda |
if (pb->seekable) { |
12f1b1fe |
AVIStream *avist; |
bb270c08 |
AVIIentry* ie = 0, *tie;
int empty, stream_id = -1;
|
1a40491e |
idx_chunk = ff_start_tag(pb, "idx1"); |
12f1b1fe |
for(i=0; i<s->nb_streams; i++){
avist= s->streams[i]->priv_data;
avist->entry=0;
}
|
bb270c08 |
do {
empty = 1;
for (i=0; i<s->nb_streams; i++) { |
12f1b1fe |
avist= s->streams[i]->priv_data;
if (avist->indexes.entry <= avist->entry) |
bb270c08 |
continue;
|
12f1b1fe |
tie = avi_get_ientry(&avist->indexes, avist->entry); |
bb270c08 |
if (empty || tie->pos < ie->pos) {
ie = tie;
stream_id = i;
}
empty = 0;
}
if (!empty) { |
12f1b1fe |
avist= s->streams[stream_id]->priv_data; |
99f42c27 |
avi_stream2fourcc(tag, stream_id, |
bb270c08 |
s->streams[stream_id]->codec->codec_type); |
99f42c27 |
ffio_wfourcc(pb, tag); |
77eb5504 |
avio_wl32(pb, ie->flags);
avio_wl32(pb, ie->pos);
avio_wl32(pb, ie->len); |
12f1b1fe |
avist->entry++; |
bb270c08 |
}
} while (!empty); |
1a40491e |
ff_end_tag(pb, idx_chunk); |
ce9fce63 |
|
5bce834e |
avi_write_counters(s, avi->riff_id); |
ce9fce63 |
}
return 0;
}
|
e928649b |
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) |
de6d9b64 |
{
AVIContext *avi = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
de6d9b64 |
unsigned char tag[5]; |
e928649b |
unsigned int flags=0;
const int stream_index= pkt->stream_index; |
b4f0e04c |
AVIStream *avist= s->streams[stream_index]->priv_data; |
01f4895c |
AVCodecContext *enc= s->streams[stream_index]->codec; |
e928649b |
int size= pkt->size; |
ce9fce63 |
|
f35d5f96 |
av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(pkt->dts), avist->packet_count, stream_index); |
414c6bf0 |
while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count){ |
50c3dd32 |
AVPacket empty_packet;
|
9cb9e39c |
if(pkt->dts - avist->packet_count > 60000){ |
be0b37c6 |
av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", pkt->dts - avist->packet_count); |
9cb9e39c |
return AVERROR(EINVAL);
}
|
50c3dd32 |
av_init_packet(&empty_packet);
empty_packet.size= 0;
empty_packet.data= NULL;
empty_packet.stream_index= stream_index;
avi_write_packet(s, &empty_packet); |
f35d5f96 |
av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(pkt->dts), avist->packet_count); |
50c3dd32 |
} |
b4f0e04c |
avist->packet_count++; |
50c3dd32 |
|
a2a2668d |
// Make sure to put an OpenDML chunk when the file size exceeds the limits |
8978feda |
if (pb->seekable && |
a2704c97 |
(avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) { |
115329f1 |
|
e738cee9 |
avi_write_ix(s); |
1a40491e |
ff_end_tag(pb, avi->movi_list); |
115329f1 |
|
bb270c08 |
if (avi->riff_id == 1)
avi_write_idx1(s); |
e738cee9 |
|
1a40491e |
ff_end_tag(pb, avi->riff_start); |
b4f0e04c |
avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi"); |
ce9fce63 |
} |
115329f1 |
|
99f42c27 |
avi_stream2fourcc(tag, stream_index, enc->codec_type); |
cc947f04 |
if(pkt->flags&AV_PKT_FLAG_KEY) |
e928649b |
flags = 0x10; |
72415b2a |
if (enc->codec_type == AVMEDIA_TYPE_AUDIO) { |
b4f0e04c |
avist->audio_strm_length += size; |
e928649b |
} |
de6d9b64 |
|
8978feda |
if (s->pb->seekable) { |
b4f0e04c |
AVIIndex* idx = &avist->indexes; |
bb270c08 |
int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
int id = idx->entry % AVI_INDEX_CLUSTER_SIZE; |
e738cee9 |
if (idx->ents_allocated <= idx->entry) { |
e47cfe9e |
idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1); |
bb270c08 |
if (!idx->cluster) |
a0f2d3ff |
return AVERROR(ENOMEM); |
e738cee9 |
idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry)); |
bb270c08 |
if (!idx->cluster[cl]) |
a0f2d3ff |
return AVERROR(ENOMEM); |
bb270c08 |
idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
} |
115329f1 |
|
bb270c08 |
idx->cluster[cl][id].flags = flags; |
a2704c97 |
idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list; |
e738cee9 |
idx->cluster[cl][id].len = size; |
bb270c08 |
idx->entry++; |
de6d9b64 |
} |
115329f1 |
|
77eb5504 |
avio_write(pb, tag, 4);
avio_wl32(pb, size);
avio_write(pb, pkt->data, size); |
de6d9b64 |
if (size & 1) |
77eb5504 |
avio_w8(pb, 0); |
de6d9b64 |
|
b7f2fdde |
avio_flush(pb); |
de6d9b64 |
return 0;
}
static int avi_write_trailer(AVFormatContext *s)
{
AVIContext *avi = s->priv_data; |
ae628ec1 |
AVIOContext *pb = s->pb; |
ce9fce63 |
int res = 0; |
e738cee9 |
int i, j, n, nb_frames; |
bc5c918e |
int64_t file_size; |
e738cee9 |
|
8978feda |
if (pb->seekable){ |
5c4e1928 |
if (avi->riff_id == 1) { |
1a40491e |
ff_end_tag(pb, avi->movi_list); |
5c4e1928 |
res = avi_write_idx1(s); |
1a40491e |
ff_end_tag(pb, avi->riff_start); |
5c4e1928 |
} else {
avi_write_ix(s); |
1a40491e |
ff_end_tag(pb, avi->movi_list);
ff_end_tag(pb, avi->riff_start); |
5c4e1928 |
|
a2704c97 |
file_size = avio_tell(pb); |
6b4aa5da |
avio_seek(pb, avi->odml_list - 8, SEEK_SET); |
0abdb293 |
ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */ |
45a8a02a |
avio_skip(pb, 16); |
5c4e1928 |
for (n=nb_frames=0;n<s->nb_streams;n++) {
AVCodecContext *stream = s->streams[n]->codec; |
b4f0e04c |
AVIStream *avist= s->streams[n]->priv_data;
|
72415b2a |
if (stream->codec_type == AVMEDIA_TYPE_VIDEO) { |
b4f0e04c |
if (nb_frames < avist->packet_count)
nb_frames = avist->packet_count; |
5c4e1928 |
} else { |
36ef5369 |
if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) { |
b4f0e04c |
nb_frames += avist->packet_count; |
5c4e1928 |
} |
86bec9a1 |
}
} |
77eb5504 |
avio_wl32(pb, nb_frames); |
6b4aa5da |
avio_seek(pb, file_size, SEEK_SET); |
5bce834e |
|
5c4e1928 |
avi_write_counters(s, avi->riff_id);
} |
a2a2668d |
} |
e738cee9 |
|
b4f0e04c |
for (i=0; i<s->nb_streams; i++) {
AVIStream *avist= s->streams[i]->priv_data;
for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
av_free(avist->indexes.cluster[j]);
av_freep(&avist->indexes.cluster);
avist->indexes.ents_allocated = avist->indexes.entry = 0; |
e738cee9 |
} |
115329f1 |
|
ce9fce63 |
return res; |
de6d9b64 |
}
|
c6610a21 |
AVOutputFormat ff_avi_muxer = { |
dfc2c4d9 |
.name = "avi", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"), |
dfc2c4d9 |
.mime_type = "video/x-msvideo",
.extensions = "avi",
.priv_data_size = sizeof(AVIContext), |
36ef5369 |
.audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
.video_codec = AV_CODEC_ID_MPEG4, |
dfc2c4d9 |
.write_header = avi_write_header,
.write_packet = avi_write_packet,
.write_trailer = avi_write_trailer, |
20234a4b |
.codec_tag = (const AVCodecTag* const []){
ff_codec_bmp_tags, ff_codec_wav_tags, 0
},
.flags = AVFMT_VARIABLE_FPS, |
de6d9b64 |
}; |