This one is somewhat experimental so far, since it doesn't work well with ff_reshuffle_raw_rgb() for some reason. Perhaps you could look into the reason why, Michael.

Mats

--
Mats Peterson
http://matsp888.no-ip.org/~mats/
>From 6a4a59d5e4cf08f14e77710234685239638d583d Mon Sep 17 00:00:00 2001
From: Mats Peterson <matsp...@yahoo.com>
Date: Sat, 20 Feb 2016 13:00:52 +0100
Subject: [PATCH] lavf/movenc: Add palette to video sample description

---
 libavformat/movenc.c |   43 ++++++++++++++++++++++++++++++++++++++++++-
 libavformat/movenc.h |    3 +++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index b9c0f7a..6e87df1 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1714,7 +1714,29 @@ static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
         avio_wb16(pb, track->enc->bits_per_coded_sample);
     else
         avio_wb16(pb, 0x18); /* Reserved */
-    avio_wb16(pb, 0xffff); /* Reserved */
+
+    if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO &&
+        track->enc->bits_per_coded_sample >= 1 && track->enc->bits_per_coded_sample <= 8) {
+        int i;
+        int pal_size = 1 << track->enc->bits_per_coded_sample;
+        avio_wb16(pb, 0);             /* Color table ID */
+        avio_wb32(pb, 0);             /* Color table seed */
+        avio_wb16(pb, 0x8000);        /* Color table flags */
+        avio_wb16(pb, pal_size - 1);  /* Color table size (zero-relative) */
+        for (i = 0; i < pal_size; i++) {
+            uint32_t v = mov->palette[i];
+            uint32_t r, g, b;
+            avio_wb16(pb, 0);
+            r = (v >> 16) & 0xff;
+            avio_wb16(pb, (r << 8) | r);
+            g = (v >> 8) & 0xff;
+            avio_wb16(pb, (g << 8) | g);
+            b = v & 0xff;
+            avio_wb16(pb, (b << 8) | b);
+        }
+    } else
+        avio_wb16(pb, 0xffff); /* Color table ID, -1 for no or default palette */
+
     if (track->tag == MKTAG('m','p','4','v'))
         mov_write_esds_tag(pb, track);
     else if (track->enc->codec_id == AV_CODEC_ID_H263)
@@ -4352,6 +4374,25 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     int size = pkt->size, ret = 0;
     uint8_t *reformatted_data = NULL;
 
+    if (enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
+        int64_t bpc = enc->bits_per_coded_sample != 15 ? enc->bits_per_coded_sample : 16;
+        int expected_stride = ((enc->width * bpc + 15) >> 4)*2;
+        ret = ff_reshuffle_raw_rgb(s, &pkt, enc, expected_stride);
+        if (ret < 0)
+            return ret;
+        size = pkt->size;
+        if (!mov->pal_done) {
+            if (ret == CONTAINS_PAL) {
+                int pal_size = 1 << enc->bits_per_coded_sample;
+                memcpy(mov->palette, pkt->data + size - 4*pal_size, 4*pal_size);
+            } else if (enc->bits_per_coded_sample == 1) {
+                /* Initialize 1 bpp palette to black & white */
+                memset(mov->palette, 0xff, 4);
+            }
+            mov->pal_done++;
+        }
+    }
+
     if (trk->entry) {
         int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
         if (duration < 0 || duration > INT_MAX) {
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index deb90fe..f257591 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -170,6 +170,9 @@ typedef struct MOVMuxContext {
     uint64_t mdat_size;
     MOVTrack *tracks;
 
+    uint32_t palette[AVPALETTE_SIZE / 4];
+    int pal_done;
+
     int flags;
     int rtp_flags;
 
-- 
1.7.10.4

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

Reply via email to