Just for fun, here is an experimental patch that makes use of palette side data packets if they exist. I suppose the packets (including side data packets) will be unaltered from demuxing to muxing when using stream copy, right? In any case, it produces flickering output for some reason.

Mats

--
Mats Peterson
http://matsp888.no-ip.org/~mats/
>From 15dbacd20db011ebb3255aa2023e6132af56c0a6 Mon Sep 17 00:00:00 2001
From: Mats Peterson <matsp...@yahoo.com>
Date: Sat, 27 Feb 2016 22:33:29 +0100
Subject: [PATCH] lavf/avienc: Use palette side data packet if available

---
 libavformat/avienc.c |   59 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index ca505f4..c1d26c2 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -671,36 +671,49 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
     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;
+        const uint8_t *pal = NULL;
+        int pal_size = 1 << enc->bits_per_coded_sample;
+        int i;
 
         ret = ff_reshuffle_raw_rgb(s, &pkt, enc, expected_stride);
         if (ret < 0)
             return ret;
-        if (ret) {
-            if (ret == CONTAINS_PAL) {
-                int pc_tag, i;
-                int pal_size = 1 << enc->bits_per_coded_sample;
-                if (!avist->hdr_pal_done) {
-                    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);
-                        avio_wl32(pb, v & 0xffffff);
-                    }
-                    avio_seek(pb, cur_offset, SEEK_SET);
-                    avist->hdr_pal_done++;
-                }
-                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 < pkt->side_data_elems; i++) {
+            if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
+                pal = pkt->side_data[i].data;
+                break;
+            }
+        }
+        if (!pal && ret == CONTAINS_PAL)
+            pal = data + size - AVPALETTE_SIZE;
+
+        if (pal) {
+            int pc_tag, i;
+            if (!avist->hdr_pal_done) {
+                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);
-                    avio_wb32(pb, v<<8);
+                    uint32_t v = AV_RL32(pal + 4*i);
+                    avio_wl32(pb, v & 0xffffff);
                 }
-                ff_end_tag(pb, pc_tag);
+                avio_seek(pb, cur_offset, SEEK_SET);
+                avist->hdr_pal_done++;
+            }
+            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(pal + 4*i);
+                avio_wb32(pb, v<<8);
             }
+            ff_end_tag(pb, pc_tag);
+        }
+
+        if (ret) {
             ret = avi_write_packet_internal(s, pkt);
             av_packet_free(&pkt);
             return ret;
-- 
1.7.10.4

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to