]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/avienc.c
Merge commit '5b9cdf8cba114c41239bf0f9f5e0ccb6977d1c8d'
[ffmpeg] / libavformat / avienc.c
index ca505f407a3e46f103c3a4625dc4124142d6d3cc..c908c21e4fc9c39332e949f10e1878b17a8a102d 100644 (file)
  */
 
 typedef struct AVIIentry {
-    unsigned int flags, pos, len;
+    char tag[4];
+    unsigned int flags;
+    unsigned int pos;
+    unsigned int len;
 } AVIIentry;
 
 #define AVI_INDEX_CLUSTER_SIZE 16384
@@ -74,12 +77,15 @@ typedef struct AVIStream {
     int max_size;
     int sample_requested;
 
-    int64_t pal_offset;
-    int hdr_pal_done;
-
     int64_t last_dts;
 
     AVIIndex indexes;
+
+    int64_t strh_flags_offset;
+
+    uint32_t palette[AVPALETTE_COUNT];
+    uint32_t old_palette[AVPALETTE_COUNT];
+    int64_t pal_offset;
 } AVIStream;
 
 static int avi_write_packet_internal(AVFormatContext *s, AVPacket *pkt);
@@ -91,6 +97,43 @@ static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
     return &idx->cluster[cl][id];
 }
 
+static int avi_add_ientry(AVFormatContext *s, int stream_index, char *tag,
+                          unsigned int flags, unsigned int size)
+{
+    AVIContext *avi  = s->priv_data;
+    AVIOContext *pb  = s->pb;
+    AVIStream *avist = s->streams[stream_index]->priv_data;
+    AVIIndex *idx    = &avist->indexes;
+    int cl           = idx->entry / AVI_INDEX_CLUSTER_SIZE;
+    int id           = idx->entry % AVI_INDEX_CLUSTER_SIZE;
+
+    if (idx->ents_allocated <= idx->entry) {
+        idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
+        if (!idx->cluster) {
+            idx->ents_allocated = 0;
+            idx->entry          = 0;
+            return AVERROR(ENOMEM);
+        }
+        idx->cluster[cl] =
+            av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
+        if (!idx->cluster[cl])
+            return AVERROR(ENOMEM);
+        idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
+    }
+
+    if (tag)
+        memcpy(idx->cluster[cl][id].tag, tag, 4);
+    else
+        memset(idx->cluster[cl][id].tag, 0, 4);
+    idx->cluster[cl][id].flags = flags;
+    idx->cluster[cl][id].pos   = avio_tell(pb) - avi->movi_list;
+    idx->cluster[cl][id].len   = size;
+    avist->max_size = FFMAX(avist->max_size, size);
+    idx->entry++;
+
+    return 0;
+}
+
 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
                                   const char *riff_tag, const char *list_tag)
 {
@@ -137,21 +180,21 @@ static int avi_write_counters(AVFormatContext *s, int riff_id)
     AVIContext *avi = s->priv_data;
     int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
     int64_t file_size;
-    AVCodecContext *stream;
+    AVCodecParameters *par;
 
     file_size = avio_tell(pb);
     for (n = 0; n < s->nb_streams; n++) {
         AVIStream *avist = s->streams[n]->priv_data;
 
         av_assert0(avist->frames_hdr_strm);
-        stream = s->streams[n]->codec;
+        par = s->streams[n]->codecpar;
         avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
         ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
         if (au_ssize == 0)
             avio_wl32(pb, avist->packet_count);
         else
             avio_wl32(pb, avist->audio_strm_length / au_ssize);
-        if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
+        if (par->codec_type == AVMEDIA_TYPE_VIDEO)
             nb_frames = FFMAX(nb_frames, avist->packet_count);
     }
     if (riff_id == 1) {
@@ -168,7 +211,7 @@ static void write_odml_master(AVFormatContext *s, int stream_index)
 {
     AVIOContext *pb = s->pb;
     AVStream *st = s->streams[stream_index];
-    AVCodecContext *enc = st->codec;
+    AVCodecParameters *par = st->codecpar;
     AVIStream *avist = st->priv_data;
     unsigned char tag[5];
     int j;
@@ -182,7 +225,7 @@ static void write_odml_master(AVFormatContext *s, int stream_index)
     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) */
-    ffio_wfourcc(pb, avi_stream2fourcc(tag, stream_index, enc->codec_type));
+    ffio_wfourcc(pb, avi_stream2fourcc(tag, stream_index, par->codec_type));
                         /* dwChunkId */
     avio_wl64(pb, 0);   /* dwReserved[3] */
     avio_wl32(pb, 0);   /* Must be 0.    */
@@ -196,7 +239,7 @@ static int avi_write_header(AVFormatContext *s)
     AVIContext *avi = s->priv_data;
     AVIOContext *pb = s->pb;
     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
-    AVCodecContext *video_enc;
+    AVCodecParameters *video_par;
     AVStream *video_st = NULL;
     int64_t list1, list2, strh, strf;
     AVDictionaryEntry *t = NULL;
@@ -223,12 +266,12 @@ static int avi_write_header(AVFormatContext *s)
     avio_wl32(pb, 14 * 4);
     bitrate = 0;
 
-    video_enc = NULL;
+    video_par = NULL;
     for (n = 0; n < s->nb_streams; n++) {
-        AVCodecContext *codec = s->streams[n]->codec;
-        bitrate += codec->bit_rate;
-        if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
-            video_enc = codec;
+        AVCodecParameters *par = s->streams[n]->codecpar;
+        bitrate += par->bit_rate;
+        if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
+            video_par = par;
             video_st = s->streams[n];
         }
     }
@@ -252,9 +295,9 @@ static int avi_write_header(AVFormatContext *s)
     avio_wl32(pb, 0); /* initial frame */
     avio_wl32(pb, s->nb_streams); /* nb streams */
     avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
-    if (video_enc) {
-        avio_wl32(pb, video_enc->width);
-        avio_wl32(pb, video_enc->height);
+    if (video_par) {
+        avio_wl32(pb, video_par->width);
+        avio_wl32(pb, video_par->height);
     } else {
         avio_wl32(pb, 0);
         avio_wl32(pb, 0);
@@ -267,18 +310,18 @@ static int avi_write_header(AVFormatContext *s)
     /* stream list */
     for (i = 0; i < n; i++) {
         AVStream *st = s->streams[i];
-        AVCodecContext *enc = st->codec;
+        AVCodecParameters *par = st->codecpar;
         AVIStream *avist = st->priv_data;
         list2 = ff_start_tag(pb, "LIST");
         ffio_wfourcc(pb, "strl");
 
         /* stream generic header */
         strh = ff_start_tag(pb, "strh");
-        switch (enc->codec_type) {
+        switch (par->codec_type) {
         case AVMEDIA_TYPE_SUBTITLE:
             // XSUB subtitles behave like video tracks, other subtitles
             // are not (yet) supported.
-            if (enc->codec_id != AV_CODEC_ID_XSUB) {
+            if (par->codec_id != AV_CODEC_ID_XSUB) {
                 av_log(s, AV_LOG_ERROR,
                        "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
                 return AVERROR_PATCHWELCOME;
@@ -296,11 +339,12 @@ static int avi_write_header(AVFormatContext *s)
             ffio_wfourcc(pb, "dats");
             break;
         }
-        if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
-            enc->codec_id == AV_CODEC_ID_XSUB)
-            avio_wl32(pb, enc->codec_tag);
+        if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
+            par->codec_id == AV_CODEC_ID_XSUB)
+            avio_wl32(pb, par->codec_tag);
         else
             avio_wl32(pb, 1);
+        avist->strh_flags_offset = avio_tell(pb);
         avio_wl32(pb, 0); /* flags */
         avio_wl16(pb, 0); /* priority */
         avio_wl16(pb, 0); /* language */
@@ -308,14 +352,14 @@ static int avi_write_header(AVFormatContext *s)
 
         ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
 
-        if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
-            && enc->codec_id != AV_CODEC_ID_XSUB
+        if (   par->codec_type == AVMEDIA_TYPE_VIDEO
+            && par->codec_id != AV_CODEC_ID_XSUB
             && au_byterate > 1000LL*au_scale) {
             au_byterate = 600;
             au_scale    = 1;
         }
         avpriv_set_pts_info(st, 64, au_scale, au_byterate);
-        if (enc->codec_id == AV_CODEC_ID_XSUB)
+        if (par->codec_id == AV_CODEC_ID_XSUB)
             au_scale = au_byterate = 0;
 
         avio_wl32(pb, au_scale); /* scale */
@@ -331,57 +375,57 @@ static int avi_write_header(AVFormatContext *s)
             avio_wl32(pb, 0);  /* length, XXX: filled later */
 
         /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
-        if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
+        if (par->codec_type == AVMEDIA_TYPE_VIDEO)
             avio_wl32(pb, 1024 * 1024);
-        else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
+        else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
             avio_wl32(pb, 12 * 1024);
         else
             avio_wl32(pb, 0);
         avio_wl32(pb, -1); /* quality */
         avio_wl32(pb, au_ssize); /* sample size */
         avio_wl32(pb, 0);
-        avio_wl16(pb, enc->width);
-        avio_wl16(pb, enc->height);
+        avio_wl16(pb, par->width);
+        avio_wl16(pb, par->height);
         ff_end_tag(pb, strh);
 
-        if (enc->codec_type != AVMEDIA_TYPE_DATA) {
+        if (par->codec_type != AVMEDIA_TYPE_DATA) {
             int ret, flags;
             enum AVPixelFormat pix_fmt;
 
             strf = ff_start_tag(pb, "strf");
-            switch (enc->codec_type) {
+            switch (par->codec_type) {
             case AVMEDIA_TYPE_SUBTITLE:
                 /* XSUB subtitles behave like video tracks, other subtitles
                  * are not (yet) supported. */
-                if (enc->codec_id != AV_CODEC_ID_XSUB)
+                if (par->codec_id != AV_CODEC_ID_XSUB)
                     break;
             case AVMEDIA_TYPE_VIDEO:
                 /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
-                if (  !enc->codec_tag
-                    && enc->codec_id == AV_CODEC_ID_RAWVIDEO
-                    && enc->pix_fmt == AV_PIX_FMT_RGB555LE
-                    && enc->bits_per_coded_sample == 15)
-                    enc->bits_per_coded_sample = 16;
+                if (  !par->codec_tag
+                    && par->codec_id == AV_CODEC_ID_RAWVIDEO
+                    && par->format == AV_PIX_FMT_RGB555LE
+                    && par->bits_per_coded_sample == 15)
+                    par->bits_per_coded_sample = 16;
                 avist->pal_offset = avio_tell(pb) + 40;
-                ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0, 0);
+                ff_put_bmp_header(pb, par, ff_codec_bmp_tags, 0, 0);
                 pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
-                                              enc->bits_per_coded_sample);
-                if (   !enc->codec_tag
-                    && enc->codec_id == AV_CODEC_ID_RAWVIDEO
-                    && enc->pix_fmt != pix_fmt
-                    && enc->pix_fmt != AV_PIX_FMT_NONE)
+                                              par->bits_per_coded_sample);
+                if (   !par->codec_tag
+                    && par->codec_id == AV_CODEC_ID_RAWVIDEO
+                    && par->format != pix_fmt
+                    && par->format != AV_PIX_FMT_NONE)
                     av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
-                          av_get_pix_fmt_name(enc->pix_fmt));
+                          av_get_pix_fmt_name(par->format));
                 break;
             case AVMEDIA_TYPE_AUDIO:
                 flags = (avi->write_channel_mask == 0) ? FF_PUT_WAV_HEADER_SKIP_CHANNELMASK : 0;
-                if ((ret = ff_put_wav_header(pb, enc, flags)) < 0)
+                if ((ret = ff_put_wav_header(s, pb, par, flags)) < 0)
                     return ret;
                 break;
             default:
                 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(enc->codec_type), "?"));
+                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
@@ -389,7 +433,7 @@ static int avi_write_header(AVFormatContext *s)
                 ff_riff_write_info_tag(s->pb, "strn", t->value);
                 t = NULL;
             }
-            if (enc->codec_id == AV_CODEC_ID_XSUB
+            if (par->codec_id == AV_CODEC_ID_XSUB
             && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
                 const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
                 t = NULL;
@@ -407,13 +451,13 @@ static int avi_write_header(AVFormatContext *s)
             write_odml_master(s, i);
         }
 
-        if (enc->codec_type == AVMEDIA_TYPE_VIDEO   &&
+        if (par->codec_type == AVMEDIA_TYPE_VIDEO   &&
             st->sample_aspect_ratio.num > 0 &&
             st->sample_aspect_ratio.den > 0) {
             int vprp       = ff_start_tag(pb, "vprp");
             AVRational dar = av_mul_q(st->sample_aspect_ratio,
-                                      (AVRational) { enc->width,
-                                                     enc->height });
+                                      (AVRational) { par->width,
+                                                     par->height });
             int num, den;
             av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
 
@@ -421,18 +465,18 @@ static int avi_write_header(AVFormatContext *s)
             avio_wl32(pb, 0); // video standard = unknown
             // TODO: should be avg_frame_rate
             avio_wl32(pb, (2LL*st->time_base.den + st->time_base.num - 1) / (2LL * st->time_base.num));
-            avio_wl32(pb, enc->width);
-            avio_wl32(pb, enc->height);
+            avio_wl32(pb, par->width);
+            avio_wl32(pb, par->height);
             avio_wl16(pb, den);
             avio_wl16(pb, num);
-            avio_wl32(pb, enc->width);
-            avio_wl32(pb, enc->height);
+            avio_wl32(pb, par->width);
+            avio_wl32(pb, par->height);
             avio_wl32(pb, 1); // progressive FIXME
 
-            avio_wl32(pb, enc->height);
-            avio_wl32(pb, enc->width);
-            avio_wl32(pb, enc->height);
-            avio_wl32(pb, enc->width);
+            avio_wl32(pb, par->height);
+            avio_wl32(pb, par->width);
+            avio_wl32(pb, par->height);
+            avio_wl32(pb, par->width);
             avio_wl32(pb, 0);
             avio_wl32(pb, 0);
 
@@ -500,7 +544,7 @@ static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix,
     avio_wl64(pb, ix);                    /* qwOffset */
     avio_wl32(pb, size);                  /* dwSize */
     ff_parse_specific_params(s->streams[stream_index], &au_byterate, &au_ssize, &au_scale);
-    if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
+    if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
         uint32_t audio_segm_size = (avist->audio_strm_length - avist->indexes.audio_strm_offset);
         if ((audio_segm_size % au_ssize > 0) && !avist->sample_requested) {
             avpriv_request_sample(s, "OpenDML index duration for audio packets with partial frames");
@@ -542,7 +586,7 @@ static int avi_write_ix(AVFormatContext *s)
         AVIStream *avist = s->streams[i]->priv_data;
         int64_t ix;
 
-        avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
+        avi_stream2fourcc(tag, i, s->streams[i]->codecpar->codec_type);
         ix_tag[3] = '0' + i;
 
         /* Writing AVI OpenDML leaf index chunk */
@@ -606,9 +650,13 @@ static int avi_write_idx1(AVFormatContext *s)
             }
             if (!empty) {
                 avist = s->streams[stream_id]->priv_data;
-                avi_stream2fourcc(tag, stream_id,
-                                  s->streams[stream_id]->codec->codec_type);
-                ffio_wfourcc(pb, tag);
+                if (*ie->tag)
+                    ffio_wfourcc(pb, ie->tag);
+                else {
+                    avi_stream2fourcc(tag, stream_id,
+                                  s->streams[stream_id]->codecpar->codec_type);
+                    ffio_wfourcc(pb, tag);
+                }
                 avio_wl32(pb, ie->flags);
                 avio_wl32(pb, ie->pos);
                 avio_wl32(pb, ie->len);
@@ -625,11 +673,11 @@ static int avi_write_idx1(AVFormatContext *s)
 static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
 {
     AVIStream *avist    = s->streams[stream_index]->priv_data;
-    AVCodecContext *enc = s->streams[stream_index]->codec;
+    AVCodecParameters *par = s->streams[stream_index]->codecpar;
 
     ff_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
-    while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
-           dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
+    while (par->block_align == 0 && dts != AV_NOPTS_VALUE &&
+           dts > avist->packet_count && par->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
         AVPacket empty_packet;
 
         if (dts - avist->packet_count > 60000) {
@@ -650,16 +698,11 @@ static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
 
 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    unsigned char tag[5];
     const int stream_index = pkt->stream_index;
-    const uint8_t *data    = pkt->data;
-    int size               = pkt->size;
-    AVIOContext *pb     = s->pb;
-    AVCodecContext *enc = s->streams[stream_index]->codec;
-    AVIStream *avist    = s->streams[stream_index]->priv_data;
+    AVCodecParameters *par = s->streams[stream_index]->codecpar;
     int ret;
 
-    if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
+    if (par->codec_id == AV_CODEC_ID_H264 && par->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
         ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
         if (ret < 0)
             return ret;
@@ -668,39 +711,74 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
     if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
         return ret;
 
-    if (enc->codec_id == AV_CODEC_ID_RAWVIDEO && enc->codec_tag == 0) {
-        int64_t bpc = enc->bits_per_coded_sample != 15 ? enc->bits_per_coded_sample : 16;
-        int expected_stride = ((enc->width * bpc + 31) >> 5)*4;
-
-        ret = ff_reshuffle_raw_rgb(s, &pkt, enc, expected_stride);
-        if (ret < 0)
-            return ret;
-        if (ret) {
-            if (ret == CONTAINS_PAL) {
+    if (!pkt->size)
+        return avi_write_packet_internal(s, pkt); /* Passthrough */
+
+    if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
+        AVIStream *avist = s->streams[stream_index]->priv_data;
+        AVIOContext *pb  = s->pb;
+        AVPacket *opkt   = pkt;
+        if (par->codec_id == AV_CODEC_ID_RAWVIDEO && par->codec_tag == 0) {
+            int64_t bpc = par->bits_per_coded_sample != 15 ? par->bits_per_coded_sample : 16;
+            int expected_stride = ((par->width * bpc + 31) >> 5)*4;
+            ret = ff_reshuffle_raw_rgb(s, &pkt, par, expected_stride);
+            if (ret < 0)
+                return ret;
+        } else
+            ret = 0;
+        if (par->format == AV_PIX_FMT_PAL8) {
+            int ret2 = ff_get_packet_palette(s, opkt, ret, avist->palette);
+            if (ret2 < 0)
+                return ret2;
+            if (ret2) {
+                int pal_size = 1 << par->bits_per_coded_sample;
                 int pc_tag, i;
-                int pal_size = 1 << enc->bits_per_coded_sample;
-                if (!avist->hdr_pal_done) {
+
+                av_assert0(par->bits_per_coded_sample >= 0 && par->bits_per_coded_sample <= 8);
+
+                if (pb->seekable && avist->pal_offset) {
                     int64_t cur_offset = avio_tell(pb);
                     avio_seek(pb, avist->pal_offset, SEEK_SET);
                     for (i = 0; i < pal_size; i++) {
-                        uint32_t v = AV_RL32(data + size - 4*pal_size + 4*i);
+                        uint32_t v = avist->palette[i];
                         avio_wl32(pb, v & 0xffffff);
                     }
                     avio_seek(pb, cur_offset, SEEK_SET);
-                    avist->hdr_pal_done++;
+                    memcpy(avist->old_palette, avist->palette, pal_size * 4);
+                    avist->pal_offset = 0;
                 }
-                avi_stream2fourcc(tag, stream_index, enc->codec_type);
-                tag[2] = 'p'; tag[3] = 'c';
-                pc_tag = ff_start_tag(pb, tag);
-                avio_w8(pb, 0);
-                avio_w8(pb, pal_size & 0xFF);
-                avio_wl16(pb, 0); // reserved
-                for (i = 0; i < pal_size; i++) {
-                    uint32_t v = AV_RL32(data + size - 4*pal_size + 4*i);
-                    avio_wb32(pb, v<<8);
+                if (memcmp(avist->palette, avist->old_palette, pal_size * 4)) {
+                    unsigned char tag[5];
+                    avi_stream2fourcc(tag, stream_index, par->codec_type);
+                    tag[2] = 'p'; tag[3] = 'c';
+                    if (s->pb->seekable) {
+                        int ret;
+                        if (avist->strh_flags_offset) {
+                            int64_t cur_offset = avio_tell(pb);
+                            avio_seek(pb, avist->strh_flags_offset, SEEK_SET);
+                            avio_wl32(pb, AVISF_VIDEO_PALCHANGES);
+                            avio_seek(pb, cur_offset, SEEK_SET);
+                            avist->strh_flags_offset = 0;
+                        }
+                        ret = avi_add_ientry(s, stream_index, tag, AVIIF_NO_TIME,
+                                       pal_size * 4 + 4);
+                        if (ret < 0)
+                            return ret;
+                    }
+                    pc_tag = ff_start_tag(pb, tag);
+                    avio_w8(pb, 0);
+                    avio_w8(pb, pal_size & 0xFF);
+                    avio_wl16(pb, 0); // reserved
+                    for (i = 0; i < pal_size; i++) {
+                        uint32_t v = avist->palette[i];
+                        avio_wb32(pb, v<<8);
+                    }
+                    ff_end_tag(pb, pc_tag);
+                    memcpy(avist->old_palette, avist->palette, pal_size * 4);
                 }
-                ff_end_tag(pb, pc_tag);
             }
+        }
+        if (ret) {
             ret = avi_write_packet_internal(s, pkt);
             av_packet_free(&pkt);
             return ret;
@@ -719,7 +797,7 @@ static int avi_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
     AVIContext *avi     = s->priv_data;
     AVIOContext *pb     = s->pb;
     AVIStream *avist    = s->streams[stream_index]->priv_data;
-    AVCodecContext *enc = s->streams[stream_index]->codec;
+    AVCodecParameters *par = s->streams[stream_index]->codecpar;
 
     if (pkt->dts != AV_NOPTS_VALUE)
         avist->last_dts = pkt->dts + pkt->duration;
@@ -739,35 +817,17 @@ static int avi_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
         avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
     }
 
-    avi_stream2fourcc(tag, stream_index, enc->codec_type);
+    avi_stream2fourcc(tag, stream_index, par->codec_type);
     if (pkt->flags & AV_PKT_FLAG_KEY)
         flags = 0x10;
-    if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
+    if (par->codec_type == AVMEDIA_TYPE_AUDIO)
         avist->audio_strm_length += size;
 
     if (s->pb->seekable) {
-        AVIIndex *idx = &avist->indexes;
-        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
-        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
-        if (idx->ents_allocated <= idx->entry) {
-            idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
-            if (!idx->cluster) {
-                idx->ents_allocated = 0;
-                idx->entry          = 0;
-                return AVERROR(ENOMEM);
-            }
-            idx->cluster[cl] =
-                av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
-            if (!idx->cluster[cl])
-                return AVERROR(ENOMEM);
-            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
-        }
-
-        idx->cluster[cl][id].flags = flags;
-        idx->cluster[cl][id].pos   = avio_tell(pb) - avi->movi_list;
-        idx->cluster[cl][id].len   = size;
-        avist->max_size = FFMAX(avist->max_size, size);
-        idx->entry++;
+        int ret;
+        ret = avi_add_ientry(s, stream_index, NULL, flags, size);
+        if (ret < 0)
+            return ret;
     }
 
     avio_write(pb, tag, 4);
@@ -808,15 +868,15 @@ static int avi_write_trailer(AVFormatContext *s)
             avio_skip(pb, 16);
 
             for (n = nb_frames = 0; n < s->nb_streams; n++) {
-                AVCodecContext *stream = s->streams[n]->codec;
+                AVCodecParameters *par = s->streams[n]->codecpar;
                 AVIStream *avist       = s->streams[n]->priv_data;
 
-                if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
+                if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
                     if (nb_frames < avist->packet_count)
                         nb_frames = avist->packet_count;
                 } else {
-                    if (stream->codec_id == AV_CODEC_ID_MP2 ||
-                        stream->codec_id == AV_CODEC_ID_MP3)
+                    if (par->codec_id == AV_CODEC_ID_MP2 ||
+                        par->codec_id == AV_CODEC_ID_MP3)
                         nb_frames += avist->packet_count;
                 }
             }