Use default Macintosh palette for AV_PIX_FMT_GRAY8 and AV_PIX_FMT_MONO[WB].

--
Mats Peterson
http://matsp888.no-ip.org/~mats/
>From 6a6c6fab1881971e9382f8ddcba3e64dc15c37c8 Mon Sep 17 00:00:00 2001
From: Mats Peterson <matsp...@yahoo.com>
Date: Tue, 23 Feb 2016 05:34:21 +0100
Subject: [PATCH v20] lavf/movenc: Add palette to video description

---
 libavformat/movenc.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++--
 libavformat/movenc.h |    5 ++++
 2 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index b9c0f7a..14bd6a2 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1711,10 +1711,34 @@ static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
     avio_write(pb, compressor_name, 31);
 
     if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
-        avio_wb16(pb, track->enc->bits_per_coded_sample);
+        avio_wb16(pb, track->enc->bits_per_coded_sample |
+                  (track->enc->pix_fmt == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
     else
         avio_wb16(pb, 0x18); /* Reserved */
-    avio_wb16(pb, 0xffff); /* Reserved */
+
+    if (mov->is_unaligned_qt_rgb &&
+        track->enc->pix_fmt != AV_PIX_FMT_GRAY8 &&
+        track->enc->pix_fmt != AV_PIX_FMT_MONOWHITE &&
+        track->enc->pix_fmt != AV_PIX_FMT_MONOBLACK &&
+        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++) {
+            uint16_t r = (mov->palette[i] >> 16) & 0xff;
+            uint16_t g = (mov->palette[i] >> 8)  & 0xff;
+            uint16_t b = mov->palette[i]         & 0xff;
+            avio_wb16(pb, 0);
+            avio_wb16(pb, (r << 8) | r);
+            avio_wb16(pb, (g << 8) | g);
+            avio_wb16(pb, (b << 8) | b);
+        }
+    } 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 == AV_CODEC_ID_H263)
@@ -1777,6 +1801,8 @@ static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
     if (avid)
         avio_wb32(pb, 0);
 
+    mov->pal_done = 0;
+
     return update_size(pb, pos);
 }
 
@@ -4703,6 +4729,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     } else {
         int i;
         MOVMuxContext *mov = s->priv_data;
+        MOVTrack *trk = &mov->tracks[pkt->stream_index];
 
         if (!pkt->size)
             return mov_write_single_packet(s, pkt); /* Passthrough. */
@@ -4739,6 +4766,43 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
             }
         }
 
+        if (trk->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
+            mov->is_unaligned_qt_rgb =
+                    trk->mode == MODE_MOV &&
+                    trk->enc->codec_id == AV_CODEC_ID_RAWVIDEO &&
+                   (trk->enc->pix_fmt == AV_PIX_FMT_RGB24 ||
+                    trk->enc->pix_fmt == AV_PIX_FMT_BGR24 ||
+                    trk->enc->pix_fmt == AV_PIX_FMT_PAL8 ||
+                    trk->enc->pix_fmt == AV_PIX_FMT_GRAY8 ||
+                    trk->enc->pix_fmt == AV_PIX_FMT_MONOWHITE ||
+                    trk->enc->pix_fmt == AV_PIX_FMT_MONOBLACK);
+
+            if (mov->is_unaligned_qt_rgb) {
+                const uint8_t *data = pkt->data;
+                int size = pkt->size;
+                int64_t bpc = trk->enc->bits_per_coded_sample != 15 ? trk->enc->bits_per_coded_sample : 16;
+                int expected_stride = ((trk->enc->width * bpc + 15) >> 4)*2;
+                int ret = ff_reshuffle_raw_rgb(s, &pkt, trk->enc, expected_stride);
+                if (ret < 0)
+                    return ret;
+                if (ret == CONTAINS_PAL && !mov->pal_done) {
+                    memset(mov->palette, 0, AVPALETTE_SIZE);
+                    int pal_size = 1 << trk->enc->bits_per_coded_sample;
+                    memcpy(mov->palette, data + size - 4*pal_size, 4*pal_size);
+                    mov->pal_done++;
+                } else if (trk->enc->pix_fmt == AV_PIX_FMT_GRAY8 ||
+                           trk->enc->pix_fmt == AV_PIX_FMT_MONOBLACK) {
+                    for (i = 0; i < pkt->size; i++)
+                        pkt->data[i] = ~pkt->data[i];
+                }
+                if (ret) {
+                    ret = mov_write_single_packet(s, pkt);
+                    av_packet_free(&pkt);
+                    return ret;
+                }
+            }
+        }
+
         return mov_write_single_packet(s, pkt);
     }
 }
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index deb90fe..6452172 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -209,6 +209,11 @@ typedef struct MOVMuxContext {
     uint8_t *encryption_kid;
     int encryption_kid_len;
 
+    uint32_t palette[AVPALETTE_COUNT];
+    int pal_done;
+
+    int is_unaligned_qt_rgb;
+
 } MOVMuxContext;
 
 #define FF_MOV_FLAG_RTP_HINT              (1 <<  0)
-- 
1.7.10.4

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

Reply via email to