[FFmpeg-devel,3/5] Interplay MVE: Implement frame format 0x06

Submitted by Hein-Pieter van Braam on June 18, 2017, 1:25 a.m.

Details

Message ID 20170618012547.13614-4-hp@tmm.cx
State Superseded
Headers show

Commit Message

Hein-Pieter van Braam June 18, 2017, 1:25 a.m.
This implements the 0x06 frame format for Interplay MVE movies. The
format is relatively simple. The video data consists of two parts:

16 bits per 8x8 block movement data
a number of 8x8 blocks of pixel data

For each 8x8 block of pixel data the movement data is consulted. There
are 3 possible meanings of the movement data:
* zero     : copy the 8x8 block from the pixel data
* negative : copy the 8x8 block from the previous frame from an offset
             determined by the actual value of the entry -0xC000.
* positive : copy the 8x8 block from the current frame from an offset
             determined by the actual value of the entry -0x4000

Decoding happens in two passes, in the fist pass only new pixeldata is
copied, during the second pass data is copied from the previous and
current frames.

The codec expects that the current frame being decoded to still has the
data from 2 frames ago on it when decoding starts.

Signed-off-by: Hein-Pieter van Braam <hp@tmm.cx>
---
 libavcodec/interplayvideo.c | 125 ++++++++++++++++++++++++++++++++++++++++----
 libavformat/ipmovie.c       |  15 ++++--
 2 files changed, 126 insertions(+), 14 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/interplayvideo.c b/libavcodec/interplayvideo.c
index fb2d6fc..e227533 100644
--- a/libavcodec/interplayvideo.c
+++ b/libavcodec/interplayvideo.c
@@ -903,7 +903,82 @@  static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame)
     ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
 };
 
-static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
+static void ipvideo_format_06_firstpass(IpvideoContext *s, AVFrame *frame, short opcode)
+{
+    int line;
+
+    if (! opcode) {
+        for (line = 0; line < 8; ++line) {
+            bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
+            s->pixel_ptr += s->stride;
+        }
+    } else {
+        /* Don't try to copy second_last_frame data on the first frames */
+        if (s->avctx->frame_number > 2)
+            copy_from(s, s->second_last_frame, frame, 0, 0);
+    }
+}
+
+static void ipvideo_format_06_secondpass(IpvideoContext *s, AVFrame *frame, short opcode)
+{
+    int off_x, off_y;
+
+    if (opcode < 0) {
+        off_x = ((unsigned short)opcode - 0xC000) % frame->linesize[0];
+        off_y = ((unsigned short)opcode - 0xC000) / frame->linesize[0];
+        copy_from(s, s->last_frame, frame, off_x, off_y);
+    }
+    if (opcode > 0) {
+        off_x = ((unsigned short)opcode - 0x4000) % frame->linesize[0];
+        off_y = ((unsigned short)opcode - 0x4000) / frame->linesize[0];
+        copy_from(s, frame, frame, off_x, off_y);
+    }
+}
+
+static void (* const ipvideo_format_06_passes[])(IpvideoContext *s, AVFrame *frame, short op) = {
+    ipvideo_format_06_firstpass, ipvideo_format_06_secondpass,
+};
+
+static void ipvideo_decode_format_06_opcodes(IpvideoContext *s, AVFrame *frame)
+{
+    int pass, x, y;
+    short opcode;
+    GetByteContext decoding_map_ptr;
+
+    /* this is PAL8, so make the palette available */
+    memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
+    s->stride = frame->linesize[0];
+
+    s->line_inc = s->stride - 8;
+    s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
+                                  + (s->avctx->width - 8) * (1 + s->is_16bpp);
+
+    bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
+
+    for (pass = 0; pass < 2; ++pass) {
+        bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
+        for (y = 0; y < s->avctx->height; y += 8) {
+            for (x = 0; x < s->avctx->width; x += 8) {
+                opcode = bytestream2_get_le16(&decoding_map_ptr);
+
+                ff_tlog(s->avctx,
+                        "  block @ (%3d, %3d): opcode 0x%X, data ptr offset %d\n",
+                        x, y, opcode, bytestream2_tell(&s->stream_ptr));
+
+                s->pixel_ptr = frame->data[0] + x + y*frame->linesize[0];
+                ipvideo_format_06_passes[pass](s, frame, opcode);
+            }
+        }
+    }
+
+    if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
+        av_log(s->avctx, AV_LOG_DEBUG,
+               "decode finished with %d bytes left over\n",
+               bytestream2_get_bytes_left(&s->stream_ptr));
+    }
+}
+
+static void ipvideo_decode_format_11_opcodes(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char opcode;
@@ -1007,18 +1082,39 @@  static int ipvideo_decode_frame(AVCodecContext *avctx,
     video_data_size      = AV_RL16(buf + 2);
     s->decoding_map_size = AV_RL16(buf + 4);
 
-    if (frame_format != 0x11)
-        av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
+    switch(frame_format) {
+        case 0x06:
+            if (s->decoding_map_size) {
+                av_log(avctx, AV_LOG_ERROR, "Decoding map for format 0x06\n");
+                return AVERROR_INVALIDDATA;
+            }
 
-    if (! s->decoding_map_size) {
-        av_log(avctx, AV_LOG_ERROR, "Empty decoding map\n");
-        return AVERROR_INVALIDDATA;
-    }
+            if (s->is_16bpp) {
+                av_log(avctx, AV_LOG_ERROR, "Video format 0x06 does not support 16bpp movies\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            /* Decoding map for 0x06 frame format is at the top of pixeldata */
+            s->decoding_map_size = ((s->avctx->width / 8) * (s->avctx->height / 8)) * 2;
+            s->decoding_map = buf + 6 + 14; /* 14 bits of op data */
+            bytestream2_init(&s->stream_ptr, buf + 6 + s->decoding_map_size + 14, video_data_size);
 
-    bytestream2_init(&s->stream_ptr, buf + 6, video_data_size);
+            break;
 
-    /* decoding map contains 4 bits of information per 8x8 block */
-    s->decoding_map = buf + 6 + video_data_size;
+        case 0x11:
+            if (! s->decoding_map_size) {
+                av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x11\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            bytestream2_init(&s->stream_ptr, buf + 6, video_data_size);
+            s->decoding_map = buf + 6 + video_data_size;
+
+            break;
+
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
+    }
 
     /* compressed buffer needs to be large enough to at least hold an entire
      * decoding map */
@@ -1039,7 +1135,14 @@  static int ipvideo_decode_frame(AVCodecContext *avctx,
         }
     }
 
-    ipvideo_decode_opcodes(s, frame);
+    switch(frame_format) {
+        case 0x06:
+            ipvideo_decode_format_06_opcodes(s, frame);
+            break;
+        case 0x11:
+            ipvideo_decode_format_11_opcodes(s, frame);
+            break;
+    }
 
     *got_frame = send_buffer;
 
diff --git a/libavformat/ipmovie.c b/libavformat/ipmovie.c
index 23f5d68..6f4d90c 100644
--- a/libavformat/ipmovie.c
+++ b/libavformat/ipmovie.c
@@ -58,7 +58,7 @@ 
 #define OPCODE_INIT_AUDIO_BUFFERS      0x03
 #define OPCODE_START_STOP_AUDIO        0x04
 #define OPCODE_INIT_VIDEO_BUFFERS      0x05
-#define OPCODE_UNKNOWN_06              0x06
+#define OPCODE_VIDEO_DATA_06           0x06
 #define OPCODE_SEND_BUFFER             0x07
 #define OPCODE_AUDIO_FRAME             0x08
 #define OPCODE_SILENCE_FRAME           0x09
@@ -444,7 +444,6 @@  static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
                     s->video_width, s->video_height);
             break;
 
-        case OPCODE_UNKNOWN_06:
         case OPCODE_UNKNOWN_0E:
         case OPCODE_UNKNOWN_10:
         case OPCODE_UNKNOWN_12:
@@ -537,8 +536,18 @@  static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
             avio_skip(pb, opcode_size);
             break;
 
+        case OPCODE_VIDEO_DATA_06:
+            av_log(s->avf, AV_LOG_TRACE, "set video data format 0x06\n");
+            s->frame_format = 0x06;
+
+            /* log position and move on for now */
+            s->video_chunk_offset = avio_tell(pb);
+            s->video_chunk_size = opcode_size;
+            avio_skip(pb, opcode_size);
+            break;
+
         case OPCODE_VIDEO_DATA_11:
-            av_log(s->avf, AV_LOG_TRACE, "set video data\n");
+            av_log(s->avf, AV_LOG_TRACE, "set video data format 0x11\n");
             s->frame_format = 0x11;
 
             /* log position and move on for now */