[FFmpeg-devel] [RFC]Writing mov palette

Carl Eugen Hoyos cehoyos at ag.or.at
Sun Jan 8 01:58:46 CET 2012


Hi!

I used attached hack to test palette in mov, maybe somebody can use it to 
implement passing a palette from encoder to muxer.

Carl Eugen
-------------- next part --------------
diff --git a/libavcodec/rawenc.c b/libavcodec/rawenc.c
index 7077de1..d5e9ee1 100644
--- a/libavcodec/rawenc.c
+++ b/libavcodec/rawenc.c
@@ -40,11 +40,16 @@ static av_cold int raw_init_encoder(AVCodecContext *avctx)
     return 0;
 }
 
+uint32_t *global_palette;
 static int raw_encode(AVCodecContext *avctx,
                             unsigned char *frame, int buf_size, void *data)
 {
     int ret = avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
                                                avctx->height, frame, buf_size);
+    if (avctx->pix_fmt == PIX_FMT_PAL8) {
+        global_palette = av_malloc(256*4);
+        memcpy(global_palette, ((AVPicture *)data)->data[1], AVPALETTE_SIZE);
+    }
 
     if(avctx->codec_tag == AV_RL32("yuv2") && ret > 0 &&
        avctx->pix_fmt   == PIX_FMT_YUYV422) {
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index e5f0c4c..7398b18 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -844,6 +844,7 @@ static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
     return 16;
 }
 
+extern uint32_t *global_palette;
 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
@@ -889,7 +890,19 @@ static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
         avio_wb16(pb, track->enc->bits_per_coded_sample);
     else
         avio_wb16(pb, 0x18); /* Reserved */
+    if (track->mode == MODE_MOV && track->enc->pix_fmt == PIX_FMT_PAL8 && global_palette) {
+        int i;
+        avio_wb16(pb, 0x00); /* colortable id */
+        avio_wb32(pb, 0x00); /* color start */
+        avio_wb16(pb, 0xff); /* color count */
+        avio_wb16(pb, 0xff); /* color end */
+        for (i = 0; i < 256; i++){
+            uint64_t v = global_palette[i];
+            avio_wb64(pb, (v & 0xFF000000) * 0x101000000 | (v & 0xFF0000) * 0x1010000 | (v & 0xFF00) * 0x10100 | (v & 0xFF) * 0x101);
+        }
+    } else {
     avio_wb16(pb, 0xffff); /* Reserved */
+    }
     if(track->tag == MKTAG('m','p','4','v'))
         mov_write_esds_tag(pb, track);
     else if(track->enc->codec_id == CODEC_ID_H263)


More information about the ffmpeg-devel mailing list