revised palette API, courtesy of Roberto Togni (rtogni at freemail.it)
[ffmpeg.git] / libavformat / ipmovie.c
index ce37ded..caaa3dd 100644 (file)
@@ -96,7 +96,7 @@ typedef struct IPMVEContext {
     unsigned char *buf;
     int buf_size;
 
-    int fps;
+    float fps;
     int frame_pts_inc;
 
     unsigned int video_width;
@@ -121,6 +121,8 @@ typedef struct IPMVEContext {
 
     offset_t next_chunk_offset;
 
+    AVPaletteControl palette_control;
+
 } IPMVEContext;
 
 static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb, 
@@ -131,6 +133,12 @@ static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
 
     if (s->audio_chunk_offset) {
 
+        /* adjust for PCM audio by skipping chunk header */
+        if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
+            s->audio_chunk_offset += 6;
+            s->audio_chunk_size -= 6;
+        }
+
         url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
         s->audio_chunk_offset = 0;
 
@@ -165,38 +173,35 @@ static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
 
     } else if (s->decode_map_chunk_offset) {
 
-        url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
-        s->decode_map_chunk_offset = 0;
+        /* send both the decode map and the video data together */
 
-        if (av_new_packet(pkt, s->decode_map_chunk_size))
+        if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
             return CHUNK_NOMEM;
 
-        pkt->stream_index = s->video_stream_index;
-        pkt->pts = s->video_pts;
+        url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
+        s->decode_map_chunk_offset = 0;
+
         if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) != 
             s->decode_map_chunk_size) {
             av_free_packet(pkt);
             return CHUNK_EOF;
         }
 
-        chunk_type = CHUNK_VIDEO;
-
-    } else if (s->video_chunk_offset) {
-
         url_fseek(pb, s->video_chunk_offset, SEEK_SET);
         s->video_chunk_offset = 0;
 
-        if (av_new_packet(pkt, s->video_chunk_size))
-            return CHUNK_NOMEM;
-
-        pkt->stream_index = s->video_stream_index;
-        pkt->pts = s->video_pts;
-        if (get_buffer(pb, pkt->data, s->video_chunk_size) != 
-            s->video_chunk_size) {
+        if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
+            s->video_chunk_size) != s->video_chunk_size) {
             av_free_packet(pkt);
             return CHUNK_EOF;
         }
 
+        pkt->stream_index = s->video_stream_index;
+        pkt->pts = s->video_pts;
+
+        debug_ipmovie("sending video frame with pts %lld\n",
+            pkt->pts);
+
         s->video_pts += s->frame_pts_inc;
 
         chunk_type = CHUNK_VIDEO;
@@ -224,9 +229,10 @@ static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
     unsigned char opcode_version;
     int opcode_size;
     unsigned char scratch[1024];
-    int j;
+    int i, j;
     int first_color, last_color;
     int audio_flags;
+    unsigned char r, g, b;
 
     /* see if there are any pending packets */
     chunk_type = load_ipmovie_packet(s, pb, pkt);
@@ -328,10 +334,9 @@ static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
                 chunk_type = CHUNK_BAD;
                 break;
             }
-            s->fps = 1000000 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
-            s->fps++;  /* above calculation usually yields 14.9; we need 15 */
+            s->fps = 1000000.0 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
             s->frame_pts_inc = 90000 / s->fps;
-            debug_ipmovie("%d frames/second (timer div = %d, subdiv = %d)\n",
+            debug_ipmovie("  %.2f frames/second (timer div = %d, subdiv = %d)\n",
                 s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
             break;
 
@@ -447,7 +452,7 @@ static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
 
             /* load the palette into internal data structure */
             first_color = LE_16(&scratch[0]);
-            last_color = LE_16(&scratch[2]);
+            last_color = first_color + LE_16(&scratch[2]);
             /* sanity check (since they are 16 bit values) */
             if ((first_color > 0xFF) || (last_color > 0xFF)) {
                 debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
@@ -456,13 +461,16 @@ static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
                 break;
             }
             j = 4;  /* offset of first palette data */
-#if 0
             for (i = first_color; i <= last_color; i++) {
-                s->palette[i].r = scratch[j++] * 4;
-                s->palette[i].g = scratch[j++] * 4;
-                s->palette[i].b = scratch[j++] * 4;
+                /* the palette is stored as a 6-bit VGA palette, thus each
+                 * component is shifted up to a 8-bit range */
+                r = scratch[j++] * 4;
+                g = scratch[j++] * 4;
+                b = scratch[j++] * 4;
+                s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
             }
-#endif
+            /* indicate a palette change */
+            s->palette_control.palette_changed = 1;
             break;
 
         case OPCODE_SET_PALETTE_COMPRESSED:
@@ -523,6 +531,8 @@ static int ipmovie_read_header(AVFormatContext *s,
     ByteIOContext *pb = &s->pb;
     AVPacket pkt;
     AVStream *st;
+    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
+    int chunk_type;
 
     /* initialize private context members */
     ipmovie->video_pts = ipmovie->audio_frame_count = 0;
@@ -536,8 +546,17 @@ static int ipmovie_read_header(AVFormatContext *s,
     if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
         return AVERROR_INVALIDDATA;
 
-    /* process the next chunk which should be CHUNK_INIT_AUDIO */
-    if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
+    /* peek ahead to the next chunk-- if it is an init audio chunk, process
+     * it; if it is the first video chunk, this is a silent file */
+    if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
+        CHUNK_PREAMBLE_SIZE)
+        return -EIO;
+    chunk_type = LE_16(&chunk_preamble[2]);
+    url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
+
+    if (chunk_type == CHUNK_VIDEO)
+        ipmovie->audio_type = 0;  /* no audio */
+    else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
         return AVERROR_INVALIDDATA;
 
     /* set the pts reference (1 pts = 1/90000) */
@@ -555,20 +574,26 @@ static int ipmovie_read_header(AVFormatContext *s,
     st->codec.width = ipmovie->video_width;
     st->codec.height = ipmovie->video_height;
 
-    st = av_new_stream(s, 0);
-    if (!st)
-        return AVERROR_NOMEM;
-    ipmovie->audio_stream_index = st->index;
-    st->codec.codec_type = CODEC_TYPE_AUDIO;
-    st->codec.codec_id = ipmovie->audio_type;
-    st->codec.codec_tag = 0;  /* no tag */
-    st->codec.channels = ipmovie->audio_channels;
-    st->codec.sample_rate = ipmovie->audio_sample_rate;
-    st->codec.bits_per_sample = ipmovie->audio_bits;
-    st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
-        st->codec.bits_per_sample / 
-        (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM) ? 2 : 1;
-    st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
+    /* palette considerations */
+    st->codec.palctrl = &ipmovie->palette_control;
+
+    if (ipmovie->audio_type) {
+        st = av_new_stream(s, 0);
+        if (!st)
+            return AVERROR_NOMEM;
+        ipmovie->audio_stream_index = st->index;
+        st->codec.codec_type = CODEC_TYPE_AUDIO;
+        st->codec.codec_id = ipmovie->audio_type;
+        st->codec.codec_tag = 0;  /* no tag */
+        st->codec.channels = ipmovie->audio_channels;
+        st->codec.sample_rate = ipmovie->audio_sample_rate;
+        st->codec.bits_per_sample = ipmovie->audio_bits;
+        st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
+            st->codec.bits_per_sample;
+        if (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM)
+            st->codec.bit_rate /= 2;
+        st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
+    }
 
     return 0;
 }