diff options
Diffstat (limited to 'libmpcodecs')
-rw-r--r-- | libmpcodecs/native/msvidc.c | 394 | ||||
-rw-r--r-- | libmpcodecs/native/qtrpza.c | 262 | ||||
-rw-r--r-- | libmpcodecs/native/qtsmc.c | 513 | ||||
-rw-r--r-- | libmpcodecs/vd_msrle.c | 435 | ||||
-rw-r--r-- | libmpcodecs/vd_msvidc.c | 74 | ||||
-rw-r--r-- | libmpcodecs/vd_qt8bps.c | 228 | ||||
-rw-r--r-- | libmpcodecs/vd_qtrpza.c | 53 | ||||
-rw-r--r-- | libmpcodecs/vd_qtsmc.c | 67 |
8 files changed, 0 insertions, 2026 deletions
diff --git a/libmpcodecs/native/msvidc.c b/libmpcodecs/native/msvidc.c deleted file mode 100644 index b312f01b15..0000000000 --- a/libmpcodecs/native/msvidc.c +++ /dev/null @@ -1,394 +0,0 @@ -/* - Microsoft Video 1 Decoder - - (C) 2001 Mike Melanson - - The description of the algorithm you can read here: - http://www.pcisys.net/~melanson/codecs/ - - 32bpp support by Alex Beregszaszi -*/ - -#include "config.h" -#include "bswap.h" -#define quad quad_m - -#define LE_16(x) (le2me_16(*(unsigned short *)(x))) - -#define DECODE_BGR555_TO_BGR888(x) \ - x.c1_b = (x.c1 >> 7) & 0xF8; \ - x.c1_g = (x.c1 >> 2) & 0xF8; \ - x.c1_r = (x.c1 << 3) & 0xF8; \ - x.c2_b = (x.c2 >> 7) & 0xF8; \ - x.c2_g = (x.c2 >> 2) & 0xF8; \ - x.c2_r = (x.c2 << 3) & 0xF8; - -#define DECODE_PALETTE_TO_BGR888(x) \ - x.c1_b = palette_map[x.c1 * 4 + 2]; \ - x.c1_g = palette_map[x.c1 * 4 + 1]; \ - x.c1_r = palette_map[x.c1 * 4 + 0]; \ - x.c2_b = palette_map[x.c2 * 4 + 2]; \ - x.c2_g = palette_map[x.c2 * 4 + 1]; \ - x.c2_r = palette_map[x.c2 * 4 + 0]; - -struct -{ - unsigned short c1, c2; - unsigned char c1_r, c1_g, c1_b; - unsigned char c2_r, c2_g, c2_b; -} quad[2][2]; - -void AVI_Decode_Video1_16( - char *encoded, - int encoded_size, - char *decoded, - int width, - int height, - int bytes_per_pixel) -{ - int block_ptr, pixel_ptr; - int total_blocks; - int pixel_x, pixel_y; // pixel width and height iterators - int block_x, block_y; // block width and height iterators - int blocks_wide, blocks_high; // width and height in 4x4 blocks - int block_inc; - int row_dec; - - // decoding parameters - int stream_ptr; - unsigned char byte_a, byte_b; - unsigned short flags; - int skip_blocks; - - stream_ptr = 0; - skip_blocks = 0; - blocks_wide = width / 4; - blocks_high = height / 4; - total_blocks = blocks_wide * blocks_high; - block_inc = 4 * bytes_per_pixel; - row_dec = (width + 4) * bytes_per_pixel; - - for (block_y = blocks_high; block_y > 0; block_y--) - { - block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel); - for (block_x = blocks_wide; block_x > 0; block_x--) - { - // check if this block should be skipped - if (skip_blocks) - { - block_ptr += block_inc; - skip_blocks--; - total_blocks--; - continue; - } - - pixel_ptr = block_ptr; - - // get the next two bytes in the encoded data stream - byte_a = encoded[stream_ptr++]; - byte_b = encoded[stream_ptr++]; - - // check if the decode is finished - if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) - return; - - // check if this is a skip code - else if ((byte_b & 0xFC) == 0x84) - { - // but don't count the current block - skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1; - } - - // check if this is in the 2- or 8-color classes - else if (byte_b < 0x80) - { - flags = (byte_b << 8) | byte_a; - - quad[0][0].c1 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[0][0].c2 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - - DECODE_BGR555_TO_BGR888(quad[0][0]); - - if (quad[0][0].c1 & 0x8000) - { - // 8-color encoding - quad[1][0].c1 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[1][0].c2 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[0][1].c1 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[0][1].c2 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[1][1].c1 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - quad[1][1].c2 = LE_16(&encoded[stream_ptr]); - stream_ptr += 2; - - DECODE_BGR555_TO_BGR888(quad[0][1]); - DECODE_BGR555_TO_BGR888(quad[1][0]); - DECODE_BGR555_TO_BGR888(quad[1][1]); - - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - if (flags & 1) - { - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - else - { - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - - // get the next flag ready to go - flags >>= 1; - } - pixel_ptr -= row_dec; - } - } - else - { - // 2-color encoding - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - if (flags & 1) - { - decoded[pixel_ptr++] = quad[0][0].c1_r; - decoded[pixel_ptr++] = quad[0][0].c1_g; - decoded[pixel_ptr++] = quad[0][0].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - else - { - decoded[pixel_ptr++] = quad[0][0].c2_r; - decoded[pixel_ptr++] = quad[0][0].c2_g; - decoded[pixel_ptr++] = quad[0][0].c2_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - - // get the next flag ready to go - flags >>= 1; - } - pixel_ptr -= row_dec; - } - } - } - - // otherwise, it's a 1-color block - else - { - quad[0][0].c1 = (byte_b << 8) | byte_a; - DECODE_BGR555_TO_BGR888(quad[0][0]); - - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - decoded[pixel_ptr++] = quad[0][0].c1_r; - decoded[pixel_ptr++] = quad[0][0].c1_g; - decoded[pixel_ptr++] = quad[0][0].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - pixel_ptr -= row_dec; - } - } - - block_ptr += block_inc; - total_blocks--; - } - } -} - -void AVI_Decode_Video1_8( - char *encoded, - int encoded_size, - char *decoded, - int width, - int height, - unsigned char *palette_map, - int bytes_per_pixel) -{ - int block_ptr, pixel_ptr; - int total_blocks; - int pixel_x, pixel_y; // pixel width and height iterators - int block_x, block_y; // block width and height iterators - int blocks_wide, blocks_high; // width and height in 4x4 blocks - int block_inc; - int row_dec; - - // decoding parameters - int stream_ptr; - unsigned char byte_a, byte_b; - unsigned short flags; - int skip_blocks; - - stream_ptr = 0; - skip_blocks = 0; - blocks_wide = width / 4; - blocks_high = height / 4; - total_blocks = blocks_wide * blocks_high; - block_inc = 4 * bytes_per_pixel; - row_dec = (width + 4) * bytes_per_pixel; - - for (block_y = blocks_high; block_y > 0; block_y--) - { - block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel); - for (block_x = blocks_wide; block_x > 0; block_x--) - { - // check if this block should be skipped - if (skip_blocks) - { - block_ptr += block_inc; - skip_blocks--; - total_blocks--; - continue; - } - - pixel_ptr = block_ptr; - - // get the next two bytes in the encoded data stream - byte_a = encoded[stream_ptr++]; - byte_b = encoded[stream_ptr++]; - - // check if the decode is finished - if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) - return; - - // check if this is a skip code - else if ((byte_b & 0xFC) == 0x84) - { - // but don't count the current block - skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1; - } - - // check if this is a 2-color block - else if (byte_b < 0x80) - { - flags = (byte_b << 8) | byte_a; - - quad[0][0].c1 = (unsigned char)encoded[stream_ptr++]; - quad[0][0].c2 = (unsigned char)encoded[stream_ptr++]; - - DECODE_PALETTE_TO_BGR888(quad[0][0]); - - // 2-color encoding - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - if (flags & 1) - { - decoded[pixel_ptr++] = quad[0][0].c1_r; - decoded[pixel_ptr++] = quad[0][0].c1_g; - decoded[pixel_ptr++] = quad[0][0].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - else - { - decoded[pixel_ptr++] = quad[0][0].c2_r; - decoded[pixel_ptr++] = quad[0][0].c2_g; - decoded[pixel_ptr++] = quad[0][0].c2_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - - // get the next flag ready to go - flags >>= 1; - } - pixel_ptr -= row_dec; - } - } - - // check if it's an 8-color block - else if (byte_b >= 0x90) - { - flags = (byte_b << 8) | byte_a; - - quad[0][0].c1 = (unsigned char)encoded[stream_ptr++]; - quad[0][0].c2 = (unsigned char)encoded[stream_ptr++]; - quad[1][0].c1 = (unsigned char)encoded[stream_ptr++]; - quad[1][0].c2 = (unsigned char)encoded[stream_ptr++]; - - quad[0][1].c1 = (unsigned char)encoded[stream_ptr++]; - quad[0][1].c2 = (unsigned char)encoded[stream_ptr++]; - quad[1][1].c1 = (unsigned char)encoded[stream_ptr++]; - quad[1][1].c2 = (unsigned char)encoded[stream_ptr++]; - - DECODE_PALETTE_TO_BGR888(quad[0][0]); - DECODE_PALETTE_TO_BGR888(quad[0][1]); - DECODE_PALETTE_TO_BGR888(quad[1][0]); - DECODE_PALETTE_TO_BGR888(quad[1][1]); - - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - if (flags & 1) - { - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - else - { - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g; - decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - - // get the next flag ready to go - flags >>= 1; - } - pixel_ptr -= row_dec; - } - } - - // otherwise, it's a 1-color block - else - { - // init c2 along with c1 just so c2 is a known value for macro - quad[0][0].c1 = quad[0][0].c2 = byte_a; - DECODE_PALETTE_TO_BGR888(quad[0][0]); - - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - decoded[pixel_ptr++] = quad[0][0].c1_r; - decoded[pixel_ptr++] = quad[0][0].c1_g; - decoded[pixel_ptr++] = quad[0][0].c1_b; - if (bytes_per_pixel == 4) /* 32bpp */ - pixel_ptr++; - } - pixel_ptr -= row_dec; - } - } - - block_ptr += block_inc; - total_blocks--; - } - } -} - diff --git a/libmpcodecs/native/qtrpza.c b/libmpcodecs/native/qtrpza.c deleted file mode 100644 index 6f0db5f921..0000000000 --- a/libmpcodecs/native/qtrpza.c +++ /dev/null @@ -1,262 +0,0 @@ -/* - * - * Apple Video (rpza) QuickTime Decoder for Mplayer - * (c) 2002 Roberto Togni - * - * Fourcc: rpza, azpr - * - * Some code comes from qtsmc.c by Mike Melanson - * - * A description of the decoding algorithm can be found here: - * http://www.pcisys.net/~melanson/codecs/ - */ - -#include "config.h" -#include "bswap.h" -#include "mp_msg.h" - -#define BE_16(x) (be2me_16(*(unsigned short *)(x))) -#define BE_32(x) (be2me_32(*(unsigned int *)(x))) - - -#define ADVANCE_BLOCK() \ -{ \ - pixel_ptr += block_x_inc; \ - if (pixel_ptr >= (width * bytes_per_pixel)) \ - { \ - pixel_ptr = 0; \ - row_ptr += block_y_inc * 4; \ - } \ - total_blocks--; \ - if (total_blocks < 0) \ - { \ - mp_msg(MSGT_DECVIDEO, MSGL_WARN, "block counter just went negative (this should not happen)\n"); \ - return; \ - } \ -} - -#define PAINT_CURRENT_PIXEL(r, g, b, color) \ -{ \ - if (bytes_per_pixel == 2) { \ - (*(unsigned short*)(&decoded[block_ptr])) = color & 0x7fff; \ - block_ptr += 2; \ - } else { \ - decoded[block_ptr++] = (b); \ - decoded[block_ptr++] = (g); \ - decoded[block_ptr++] = (r); \ - if (bytes_per_pixel == 4) /* 32bpp */ \ - block_ptr++; \ - } \ -} - -#define COLOR_FIX(col_out, col_in) (col_out) = ((col_in) << 3) | ((col_in) >> 2) - -#define COLOR_TO_RGB(r, g, b, color) \ -{ \ - if (bytes_per_pixel != 2) { \ - unsigned short tmp; \ - tmp = (color >> 10) & 0x1f; \ - COLOR_FIX (r, tmp); \ - tmp = (color >> 5) & 0x1f; \ - COLOR_FIX (g, tmp); \ - tmp = color & 0x1f; \ - COLOR_FIX (b, tmp); \ - } \ -} - -#define COLORAB_TO_RGB4(rgb4, color4, colorA, colorB) \ -{ \ - unsigned short ta, tb, tt; \ - if (bytes_per_pixel != 2) { \ - ta = (colorA >> 10) & 0x1f; \ - tb = (colorB >> 10) & 0x1f; \ - COLOR_FIX (rgb4[3][0], ta); \ - COLOR_FIX (rgb4[0][0], tb); \ - tt = (11 * ta + 21 * tb) >> 5; \ - COLOR_FIX (rgb4[1][0], tt); \ - tt = (21 * ta + 11 * tb) >> 5; \ - COLOR_FIX (rgb4[2][0], tt); \ - ta = (colorA >> 5) & 0x1f; \ - tb = (colorB >> 5) & 0x1f; \ - COLOR_FIX (rgb4[3][1], ta); \ - COLOR_FIX (rgb4[0][1], tb); \ - tt = (11 * ta + 21 * tb) >> 5; \ - COLOR_FIX (rgb4[1][1], tt); \ - tt = (21 * ta + 11 * tb) >> 5; \ - COLOR_FIX (rgb4[2][1], tt); \ - ta = colorA & 0x1f; \ - tb = colorB & 0x1f; \ - COLOR_FIX (rgb4[3][2], ta); \ - COLOR_FIX (rgb4[0][2], tb); \ - tt = (11 * ta + 21 * tb) >> 5; \ - COLOR_FIX (rgb4[1][2], tt); \ - tt = (21 * ta + 11 * tb) >> 5; \ - COLOR_FIX (rgb4[2][2], tt); \ - } else { \ - color4[3] = colorA; \ - color4[0] = colorB; \ - ta = (colorA >> 10) & 0x1f; \ - tb = (colorB >> 10) & 0x1f; \ - color4[1] = ((11 * ta + 21 * tb) << 5) & 0x7c00; \ - color4[2] = ((21 * ta + 11 * tb) << 5) & 0x7c00; \ - ta = (colorA >> 5) & 0x1f; \ - tb = (colorB >> 5) & 0x1f; \ - color4[1] |= (11 * ta + 21 * tb) & 0x3e0; \ - color4[2] |= (21 * ta + 11 * tb) & 0x3e0; \ - ta = colorA & 0x1f; \ - tb = colorB & 0x1f; \ - color4[1] |= (11 * ta + 21 * tb) >> 5; \ - color4[2] |= (21 * ta + 11 * tb) >> 5; \ - } \ -} - - - -/* - * rpza frame decoder - * - * Input values: - * - * *encoded: buffer of encoded data (chunk) - * encoded_size: length of encoded buffer - * *decoded: buffer where decoded data is written (image buffer) - * width: width of decoded frame in pixels - * height: height of decoded frame in pixels - * bytes_per_pixel: bytes/pixel in output image (color depth) - * - */ - -void qt_decode_rpza(char *encoded, int encoded_size, char *decoded, int width, - int height, int bytes_per_pixel) -{ - - int stream_ptr = 0; - int chunk_size; - unsigned char opcode; - int n_blocks; - unsigned short colorA, colorB; - unsigned char r, g, b; - unsigned char rgb4[4][3]; - unsigned short color4[4]; - unsigned char index, idx; - - int row_ptr = 0; - int pixel_ptr = 0; - int pixel_x, pixel_y; - int row_inc = bytes_per_pixel * (width - 4); - int block_x_inc = bytes_per_pixel * 4; - int block_y_inc = bytes_per_pixel * width; - int block_ptr; - int total_blocks; - - - /* First byte is always 0xe1. Warn if it's different */ - if ((unsigned char)encoded[stream_ptr] != 0xe1) - mp_msg(MSGT_DECVIDEO, MSGL_WARN, - "First chunk byte is 0x%02x instead of 0x1e\n", - (unsigned char)encoded[stream_ptr]); - - /* Get chunk size, ingnoring first byte */ - chunk_size = BE_32(&encoded[stream_ptr]) & 0x00FFFFFF; - stream_ptr += 4; - - /* If length mismatch use size from MOV file and try to decode anyway */ - if (chunk_size != encoded_size) - mp_msg(MSGT_DECVIDEO, MSGL_WARN, "MOV chunk size != encoded chunk size; using MOV chunk size\n"); - - chunk_size = encoded_size; - - /* Number of 4x4 blocks in frame. */ - total_blocks = (width * height) / (4 * 4); - - /* Process chunk data */ - while (stream_ptr < chunk_size) { - opcode = encoded[stream_ptr++]; /* Get opcode */ - - n_blocks = (opcode & 0x1f) +1; /* Extract block counter from opcode */ - - /* If opcode MSbit is 0, we need more data to decide what to do */ - if ((opcode & 0x80) == 0) { - colorA = (opcode << 8) | ((unsigned char)encoded[stream_ptr++]); - opcode = 0; - if ((encoded[stream_ptr] & 0x80) != 0) { - /* Must behave as opcode 110xxxxx, using colorA computed above.*/ - /* Use fake opcode 0x20 to enter switch block at the right place */ - opcode = 0x20; - n_blocks = 1; - } - } - switch (opcode & 0xe0) { - /* Skip blocks */ - case 0x80: - while (n_blocks--) - ADVANCE_BLOCK(); - break; - - /* Fill blocks with one color */ - case 0xa0: - colorA = BE_16 (&encoded[stream_ptr]); - stream_ptr += 2; - COLOR_TO_RGB (r, g, b, colorA); - while (n_blocks--) { - block_ptr = row_ptr + pixel_ptr; - for (pixel_y = 0; pixel_y < 4; pixel_y++) { - for (pixel_x = 0; pixel_x < 4; pixel_x++){ - PAINT_CURRENT_PIXEL(r, g, b, colorA); - } - block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - /* Fill blocks with 4 colors */ - case 0xc0: - colorA = BE_16 (&encoded[stream_ptr]); - stream_ptr += 2; - case 0x20: - colorB = BE_16 (&encoded[stream_ptr]); - stream_ptr += 2; - COLORAB_TO_RGB4 (rgb4, color4, colorA, colorB); - while (n_blocks--) { - block_ptr = row_ptr + pixel_ptr; - for (pixel_y = 0; pixel_y < 4; pixel_y++) { - index = encoded[stream_ptr++]; - for (pixel_x = 0; pixel_x < 4; pixel_x++){ - idx = (index >> (2 * (3 - pixel_x))) & 0x03; - PAINT_CURRENT_PIXEL(rgb4[idx][0], rgb4[idx][1], rgb4[idx][2], color4[idx]); - } - block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - /* Fill block with 16 colors */ - case 0x00: - block_ptr = row_ptr + pixel_ptr; - for (pixel_y = 0; pixel_y < 4; pixel_y++) { - for (pixel_x = 0; pixel_x < 4; pixel_x++){ - /* We already have color of upper left pixel */ - if ((pixel_y != 0) || (pixel_x !=0)) { - colorA = BE_16 (&encoded[stream_ptr]); - stream_ptr += 2; - } - COLOR_TO_RGB (r, g, b, colorA); - PAINT_CURRENT_PIXEL(r, g, b, colorA); - } - block_ptr += row_inc; - } - ADVANCE_BLOCK(); - break; - - /* Unknown opcode */ - default: - mp_msg(MSGT_DECVIDEO, MSGL_HINT, "Unknown opcode %d in rpza chunk." - " Skip remaining %lu bytes of chunk data.\n", opcode, - chunk_size - stream_ptr); - return; - } /* Opcode switch */ - - } -} diff --git a/libmpcodecs/native/qtsmc.c b/libmpcodecs/native/qtsmc.c deleted file mode 100644 index 05d98058ae..0000000000 --- a/libmpcodecs/native/qtsmc.c +++ /dev/null @@ -1,513 +0,0 @@ -/* - Apple Graphics (SMC) Decoder for MPlayer - by Mike Melanson - Special thanks for Roberto Togni <rtogni@bresciaonline.it> for - tracking down the final, nagging bugs. - - The description of the decoding algorithm can be found here: - http://www.pcisys.net/~melanson/codecs/ -*/ - -#include <stdlib.h> -#include "config.h" -#include "bswap.h" -#include "mp_msg.h" - -#define BE_16(x) (be2me_16(*(unsigned short *)(x))) -#define BE_32(x) (be2me_32(*(unsigned int *)(x))) - -#define COLORS_PER_TABLE 256 -#define BYTES_PER_COLOR 4 - -#define CPAIR 2 -#define CQUAD 4 -#define COCTET 8 - -static unsigned char *color_pairs; -static unsigned char *color_quads; -static unsigned char *color_octets; - -static int color_pair_index; -static int color_quad_index; -static int color_octet_index; - -static int smc_initialized; - -// returns 0 if successfully initialized (enough memory was available), -// non-zero on failure -int qt_init_decode_smc(void) -{ - // be pessimistic to start - smc_initialized = 0; - - // allocate memory for the 3 palette tables - if ((color_pairs = (unsigned char *)malloc( - COLORS_PER_TABLE * BYTES_PER_COLOR * 2)) == 0) - return 1; - if ((color_quads = (unsigned char *)malloc( - COLORS_PER_TABLE * BYTES_PER_COLOR * 4)) == 0) - return 1; - if ((color_octets = (unsigned char *)malloc( - COLORS_PER_TABLE * BYTES_PER_COLOR * 8)) == 0) - return 1; - - // if execution got this far, initialization succeeded - smc_initialized = 1; - return 0; -} - -#define GET_BLOCK_COUNT \ - (opcode & 0x10) ? (1 + encoded[stream_ptr++]) : 1 + (opcode & 0x0F); -#define ADVANCE_BLOCK() \ -{ \ - pixel_ptr += block_x_inc; \ - if (pixel_ptr >= byte_width) \ - { \ - pixel_ptr = 0; \ - row_ptr += block_y_inc * 4; \ - } \ - total_blocks--; \ - if (total_blocks < 0) \ - { \ - mp_msg(MSGT_DECVIDEO, MSGL_WARN, "block counter just went negative (this should not happen)\n"); \ - return; \ - } \ -} - -void qt_decode_smc( - unsigned char *encoded, - int encoded_size, - unsigned char *decoded, - int pixel_width, - int pixel_height, - unsigned char *palette_map, - int bytes_per_pixel) -{ - int i; - int stream_ptr = 0; - int chunk_size; - unsigned char opcode; - int n_blocks; - unsigned int color_flags; - unsigned int color_flags_a; - unsigned int color_flags_b; - unsigned int flag_mask; - - int byte_width = pixel_width * bytes_per_pixel; // width of a row in bytes - int byte_height = pixel_height * byte_width; // max image size, basically - int row_ptr = 0; - int pixel_ptr = 0; - int pixel_x, pixel_y; - int row_inc = bytes_per_pixel * (pixel_width - 4); - int block_x_inc = bytes_per_pixel * 4; - int block_y_inc = bytes_per_pixel * pixel_width; - int block_ptr; - int prev_block_ptr; - int prev_block_ptr1, prev_block_ptr2; - int prev_block_flag; - int total_blocks; - int color_table_index; // indexes to color pair, quad, or octet tables - int color_index; // indexes into palette map - - if (!smc_initialized) - return; - - // reset color tables - color_pair_index = 0; - color_quad_index = 0; - color_octet_index = 0; - - chunk_size = BE_32(&encoded[stream_ptr]) & 0x00FFFFFF; - stream_ptr += 4; - if (chunk_size != encoded_size) - mp_msg(MSGT_DECVIDEO, MSGL_WARN, "MOV chunk size != encoded chunk size; using MOV chunk size\n"); - - chunk_size = encoded_size; - total_blocks = (pixel_width * pixel_height) / (4 * 4); - - // traverse through the blocks - while (total_blocks) - { - // sanity checks - // make sure stream ptr hasn't gone out of bounds - if (stream_ptr > chunk_size) - { - mp_msg(MSGT_DECVIDEO, MSGL_ERR, - "SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n", - stream_ptr, chunk_size); - return; - } - // make sure the row pointer hasn't gone wild - if (row_ptr >= byte_height) - { - mp_msg(MSGT_DECVIDEO, MSGL_ERR, - "SMC decoder just went out of bounds (row ptr = %d, height = %d)\n", - row_ptr, byte_height); - return; - } - - opcode = encoded[stream_ptr++]; - switch (opcode & 0xF0) - { - // skip n blocks - case 0x00: - case 0x10: - n_blocks = GET_BLOCK_COUNT; - while (n_blocks--) - ADVANCE_BLOCK(); - break; - - // repeat last block n times - case 0x20: - case 0x30: - n_blocks = GET_BLOCK_COUNT; - - // sanity check - if ((row_ptr == 0) && (pixel_ptr == 0)) - { - mp_msg(MSGT_DECVIDEO, MSGL_WARN, - "encountered repeat block opcode (%02X) but no blocks rendered yet\n", - opcode & 0xF0); - break; - } - - // figure out where the previous block started - if (pixel_ptr == 0) - prev_block_ptr1 = (row_ptr - block_y_inc * 4) + - byte_width - block_x_inc; - else - prev_block_ptr1 = row_ptr + pixel_ptr - block_x_inc; - - while (n_blocks--) - { - block_ptr = row_ptr + pixel_ptr; - prev_block_ptr = prev_block_ptr1; - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - decoded[block_ptr++] = decoded[prev_block_ptr++]; - decoded[block_ptr++] = decoded[prev_block_ptr++]; - decoded[block_ptr++] = decoded[prev_block_ptr++]; - if (bytes_per_pixel == 4) /* 32bpp */ - { - block_ptr++; - prev_block_ptr++; - } - } - block_ptr += row_inc; - prev_block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - // repeat previous pair of blocks n times - case 0x40: - case 0x50: - n_blocks = GET_BLOCK_COUNT; - n_blocks *= 2; - - // sanity check - if ((row_ptr == 0) && (pixel_ptr < 2 * block_x_inc)) - { - mp_msg(MSGT_DECVIDEO, MSGL_WARN, - "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n", - opcode & 0xF0); - break; - } - - // figure out where the previous 2 blocks started - if (pixel_ptr == 0) - prev_block_ptr1 = (row_ptr - block_y_inc * 4) + - byte_width - block_x_inc * 2; - else if (pixel_ptr == block_x_inc) - prev_block_ptr1 = (row_ptr - block_y_inc * 4) + - byte_width - block_x_inc; - else - prev_block_ptr1 = row_ptr + pixel_ptr - block_x_inc * 2; - - if (pixel_ptr == 0) - prev_block_ptr2 = (row_ptr - block_y_inc * 4) + - (byte_width - block_x_inc); - else - prev_block_ptr2 = row_ptr + pixel_ptr - block_x_inc; - - prev_block_flag = 0; - while (n_blocks--) - { - block_ptr = row_ptr + pixel_ptr; - if (prev_block_flag) - prev_block_ptr = prev_block_ptr2; - else - prev_block_ptr = prev_block_ptr1; - prev_block_flag = !prev_block_flag; - - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - decoded[block_ptr++] = decoded[prev_block_ptr++]; - decoded[block_ptr++] = decoded[prev_block_ptr++]; - decoded[block_ptr++] = decoded[prev_block_ptr++]; - if (bytes_per_pixel == 4) /* 32bpp */ - { - block_ptr++; - prev_block_ptr++; - } - } - block_ptr += row_inc; - prev_block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - // 1-color block encoding - case 0x60: - case 0x70: - n_blocks = GET_BLOCK_COUNT; - color_index = encoded[stream_ptr++] * 4; - - while (n_blocks--) - { - block_ptr = row_ptr + pixel_ptr; - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - decoded[block_ptr++] = palette_map[color_index + 0]; - decoded[block_ptr++] = palette_map[color_index + 1]; - decoded[block_ptr++] = palette_map[color_index + 2]; - if (bytes_per_pixel == 4) /* 32bpp */ - block_ptr++; - } - block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - // 2-color block encoding - case 0x80: - case 0x90: - n_blocks = (opcode & 0x0F) + 1; - - // figure out which color pair to use to paint the 2-color block - if ((opcode & 0xF0) == 0x80) - { - // fetch the next 2 colors from bytestream and store in next - // available entry in the color pair table - for (i = 0; i < CPAIR; i++) - { - color_index = encoded[stream_ptr++] * BYTES_PER_COLOR; - color_table_index = CPAIR * BYTES_PER_COLOR * color_pair_index + - (i * BYTES_PER_COLOR); - color_pairs[color_table_index + 0] = palette_map[color_index + 0]; - color_pairs[color_table_index + 1] = palette_map[color_index + 1]; - color_pairs[color_table_index + 2] = palette_map[color_index + 2]; - } - // this is the base index to use for this block - color_table_index = CPAIR * BYTES_PER_COLOR * color_pair_index; - color_pair_index++; - if (color_pair_index == COLORS_PER_TABLE) - color_pair_index = 0; - } - else - color_table_index = CPAIR * BYTES_PER_COLOR * encoded[stream_ptr++]; - - while (n_blocks--) - { - color_flags = BE_16(&encoded[stream_ptr]); - stream_ptr += 2; - flag_mask = 0x8000; - block_ptr = row_ptr + pixel_ptr; - for (pixel_y = 0; pixel_y < 4; pixel_y++) - { - for (pixel_x = 0; pixel_x < 4; pixel_x++) - { - if (color_flags & flag_mask) - color_index = color_table_index + BYTES_PER_COLOR; - else - color_index = color_table_index; - flag_mask >>= 1; - - decoded[block_ptr++] = color_pairs[color_index + 0]; - decoded[block_ptr++] = color_pairs[color_index + 1]; - decoded[block_ptr++] = color_pairs[color_index + 2]; - if (bytes_per_pixel == 4) /* 32bpp */ - block_ptr++; - } - block_ptr += row_inc; - } - ADVANCE_BLOCK(); - } - break; - - // 4-color block encoding - case 0xA0: - case 0xB0: - n_blocks = (opcode & 0x0F) + 1; - - // figure out which color quad to use to paint the 4-color block - if ((opcode & 0xF0) == 0xA0) - { - // fetch the next 4 colors from bytestream and store in next - // available entry in the color quad table - for (i = 0; i < CQUAD; i++) - { - color_index = encoded[stream_ptr++] * BYTES_PER_COLOR; - color_table_index = CQUAD * BYTES_PER_COLOR * color_quad_index + - (i * BYTES_PER_COLOR); - color_quads[color_table_index + 0] = palette_map[color_index + 0]; - color_quads[color_table_index + 1] = palette_map[color_index + 1]; - color_quads[color_table_index + 2] = palette_map[color_index + 2]; - } - // this is the base index to use for this block - color_table_index = CQUAD * BYTE |