*** matroskadec.c.orig	2015-12-07 11:45:53.976807195 +0100
--- matroskadec.c	2015-12-07 18:17:53.445838314 +0100
***************
*** 312,317 ****
--- 312,319 ----
  
      /* WebM DASH Manifest live flag/ */
      int is_live;
+ 
+     uint8_t *pal;
  } MatroskaDemuxContext;
  
  typedef struct MatroskaBlock {
***************
*** 1824,1830 ****
              codec_id         = st->codec->codec_id;
              extradata_offset = FFMIN(track->codec_priv.size, 18);
          } else if (!strcmp(track->codec_id, "A_QUICKTIME")
!                    && (track->codec_priv.size >= 86)
                     && (track->codec_priv.data)) {
              fourcc = AV_RL32(track->codec_priv.data + 4);
              codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc);
--- 1826,1832 ----
              codec_id         = st->codec->codec_id;
              extradata_offset = FFMIN(track->codec_priv.size, 18);
          } else if (!strcmp(track->codec_id, "A_QUICKTIME")
!                    && (track->codec_priv.size >= 36)
                     && (track->codec_priv.data)) {
              fourcc = AV_RL32(track->codec_priv.data + 4);
              codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc);
***************
*** 1835,1841 ****
          } else if (!strcmp(track->codec_id, "V_QUICKTIME") &&
                     (track->codec_priv.size >= 21)          &&
                     (track->codec_priv.data)) {
!             fourcc   = AV_RL32(track->codec_priv.data + 4);
              codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
              if (ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(track->codec_priv.data))) {
                  fourcc   = AV_RL32(track->codec_priv.data);
--- 1837,1848 ----
          } else if (!strcmp(track->codec_id, "V_QUICKTIME") &&
                     (track->codec_priv.size >= 21)          &&
                     (track->codec_priv.data)) {
!             AVIOContext pb;
!             MOVStreamContext *msc;
!             void *priv_data;
!             int nb_streams;
!             MOVContext *mc;
!             fourcc = AV_RL32(track->codec_priv.data + 4);
              codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
              if (ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(track->codec_priv.data))) {
                  fourcc   = AV_RL32(track->codec_priv.data);
***************
*** 1849,1854 ****
--- 1856,1887 ----
                  av_log(matroska->ctx, AV_LOG_ERROR,
                         "mov FourCC not found %s.\n", buf);
              }
+             priv_data = st->priv_data;
+             nb_streams = s->nb_streams;
+             mc = av_mallocz(sizeof(*mc));
+             if (! mc)
+                 return AVERROR(ENOMEM);
+             mc->fc = s;
+             st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
+             if (!msc) {
+                 av_free(mc);
+                 st->priv_data = priv_data;
+                 return AVERROR(ENOMEM);
+             }
+             ffio_init_context(&pb, track->codec_priv.data, track->codec_priv.size, 0, NULL, NULL, NULL, NULL);
+             /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
+              * so set it temporarily to indicate which stream to update. */
+             s->nb_streams = st->index + 1;
+             ff_mov_read_stsd_entries(mc, &pb, 1);
+             if (msc->has_palette) {
+                 matroska->pal = av_malloc(AVPALETTE_SIZE);
+                 if (matroska->pal)
+                     memcpy(matroska->pal, msc->palette, AVPALETTE_SIZE);
+             }
+             av_free(msc);
+             av_free(mc);
+             st->priv_data = priv_data;
+             s->nb_streams = nb_streams;
          } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
              switch (track->audio.bitdepth) {
              case  8:
***************
*** 2293,2298 ****
--- 2326,2340 ----
      if (matroska->num_packets > 0) {
          memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
          av_freep(&matroska->packets[0]);
+         if (matroska->pal) {
+             uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
+             if (!pal) {
+                 av_log(matroska->ctx, AV_LOG_ERROR, "Cannot append palette to packet\n");
+             } else {
+                 memcpy(pal, matroska->pal, AVPALETTE_SIZE);
+             }
+             av_freep(&matroska->pal);
+         }
          if (matroska->num_packets > 1) {
              void *newpackets;
              memmove(&matroska->packets[0], &matroska->packets[1],
