00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023
00024 #define ALT_BITSTREAM_READER_LE
00025 #include "bitstream.h"
00026
00027 typedef union MacroBlock {
00028 uint16_t pixels[4];
00029 uint32_t pixels32[2];
00030 } MacroBlock;
00031
00032 typedef union SuperBlock {
00033 uint16_t pixels[64];
00034 uint32_t pixels32[32];
00035 } SuperBlock;
00036
00037 typedef struct CodeBook {
00038 unsigned depth;
00039 unsigned size;
00040 MacroBlock* blocks;
00041 } CodeBook;
00042
00043 typedef struct Escape124Context {
00044 AVFrame frame;
00045
00046 unsigned num_superblocks;
00047
00048 CodeBook codebooks[3];
00049 } Escape124Context;
00050
00051 static int can_safely_read(GetBitContext* gb, int bits) {
00052 return get_bits_count(gb) + bits <= gb->size_in_bits;
00053 }
00054
00060 static av_cold int escape124_decode_init(AVCodecContext *avctx)
00061 {
00062 Escape124Context *s = avctx->priv_data;
00063
00064 avctx->pix_fmt = PIX_FMT_RGB555;
00065
00066 s->num_superblocks = ((unsigned)avctx->width / 8) *
00067 ((unsigned)avctx->height / 8);
00068
00069 return 0;
00070 }
00071
00072 static av_cold int escape124_decode_close(AVCodecContext *avctx)
00073 {
00074 unsigned i;
00075 Escape124Context *s = avctx->priv_data;
00076
00077 for (i = 0; i < 3; i++)
00078 av_free(s->codebooks[i].blocks);
00079
00080 if (s->frame.data[0])
00081 avctx->release_buffer(avctx, &s->frame);
00082
00083 return 0;
00084 }
00085
00086 static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth,
00087 unsigned size)
00088 {
00089 unsigned i, j;
00090 CodeBook cb = { 0 };
00091
00092 if (!can_safely_read(gb, size * 34))
00093 return cb;
00094
00095 if (size >= INT_MAX / sizeof(MacroBlock))
00096 return cb;
00097 cb.blocks = av_malloc(size ? size * sizeof(MacroBlock) : 1);
00098 if (!cb.blocks)
00099 return cb;
00100
00101 cb.depth = depth;
00102 cb.size = size;
00103 for (i = 0; i < size; i++) {
00104 unsigned mask_bits = get_bits(gb, 4);
00105 unsigned color0 = get_bits(gb, 15);
00106 unsigned color1 = get_bits(gb, 15);
00107
00108 for (j = 0; j < 4; j++) {
00109 if (mask_bits & (1 << j))
00110 cb.blocks[i].pixels[j] = color1;
00111 else
00112 cb.blocks[i].pixels[j] = color0;
00113 }
00114 }
00115 return cb;
00116 }
00117
00118 static unsigned decode_skip_count(GetBitContext* gb)
00119 {
00120 unsigned value;
00121
00122
00123 if (!can_safely_read(gb, 1))
00124 return -1;
00125 value = get_bits1(gb);
00126 if (!value)
00127 return value;
00128
00129 value += get_bits(gb, 3);
00130 if (value != (1 + ((1 << 3) - 1)))
00131 return value;
00132
00133 value += get_bits(gb, 7);
00134 if (value != (1 + ((1 << 3) - 1)) + ((1 << 7) - 1))
00135 return value;
00136
00137 return value + get_bits(gb, 12);
00138 }
00139
00140 static MacroBlock decode_macroblock(Escape124Context* s, GetBitContext* gb,
00141 int* codebook_index, int superblock_index)
00142 {
00143
00144
00145 unsigned block_index, depth;
00146
00147 if (get_bits1(gb)) {
00148 static const char transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} };
00149 *codebook_index = transitions[*codebook_index][get_bits1(gb)];
00150 }
00151
00152 depth = s->codebooks[*codebook_index].depth;
00153
00154
00155
00156
00157 block_index = depth ? get_bits(gb, depth) : 0;
00158
00159 if (*codebook_index == 1) {
00160 block_index += superblock_index << s->codebooks[1].depth;
00161 }
00162
00163
00164
00165 if (block_index >= s->codebooks[*codebook_index].size)
00166 return (MacroBlock) { { 0 } };
00167
00168 return s->codebooks[*codebook_index].blocks[block_index];
00169 }
00170
00171 static void insert_mb_into_sb(SuperBlock* sb, MacroBlock mb, unsigned index) {
00172
00173 uint32_t *dst = sb->pixels32 + index + (index & -4);
00174
00175
00176 dst[0] = mb.pixels32[0];
00177 dst[4] = mb.pixels32[1];
00178 }
00179
00180 static void copy_superblock(uint16_t* dest, unsigned dest_stride,
00181 uint16_t* src, unsigned src_stride)
00182 {
00183 unsigned y;
00184 if (src)
00185 for (y = 0; y < 8; y++)
00186 memcpy(dest + y * dest_stride, src + y * src_stride,
00187 sizeof(uint16_t) * 8);
00188 else
00189 for (y = 0; y < 8; y++)
00190 memset(dest + y * dest_stride, 0, sizeof(uint16_t) * 8);
00191 }
00192
00193 static const uint16_t mask_matrix[] = {0x1, 0x2, 0x10, 0x20,
00194 0x4, 0x8, 0x40, 0x80,
00195 0x100, 0x200, 0x1000, 0x2000,
00196 0x400, 0x800, 0x4000, 0x8000};
00197
00207 static int escape124_decode_frame(AVCodecContext *avctx,
00208 void *data, int *data_size,
00209 const uint8_t *buf, int buf_size)
00210 {
00211 Escape124Context *s = avctx->priv_data;
00212
00213 GetBitContext gb;
00214 unsigned frame_flags, frame_size;
00215 unsigned i;
00216
00217 unsigned superblock_index, cb_index = 1,
00218 superblock_col_index = 0,
00219 superblocks_per_row = avctx->width / 8, skip = -1;
00220
00221 uint16_t* old_frame_data, *new_frame_data;
00222 unsigned old_stride, new_stride;
00223
00224 AVFrame new_frame = { { 0 } };
00225
00226 init_get_bits(&gb, buf, buf_size * 8);
00227
00228
00229
00230 if (!can_safely_read(&gb, 64))
00231 return -1;
00232
00233 frame_flags = get_bits_long(&gb, 32);
00234 frame_size = get_bits_long(&gb, 32);
00235
00236
00237
00238 if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
00239 av_log(NULL, AV_LOG_DEBUG, "Skipping frame\n");
00240
00241 *data_size = sizeof(AVFrame);
00242 *(AVFrame*)data = s->frame;
00243
00244 return frame_size;
00245 }
00246
00247 for (i = 0; i < 3; i++) {
00248 if (frame_flags & (1 << (17 + i))) {
00249 unsigned cb_depth, cb_size;
00250 if (i == 2) {
00251
00252
00253 cb_size = get_bits_long(&gb, 20);
00254 cb_depth = av_log2(cb_size - 1) + 1;
00255 } else {
00256 cb_depth = get_bits(&gb, 4);
00257 if (i == 0) {
00258
00259
00260 cb_size = 1 << cb_depth;
00261 } else {
00262
00263
00264
00265 cb_size = s->num_superblocks << cb_depth;
00266 }
00267 }
00268 av_free(s->codebooks[i].blocks);
00269 s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size);
00270 if (!s->codebooks[i].blocks)
00271 return -1;
00272 }
00273 }
00274
00275 new_frame.reference = 3;
00276 if (avctx->get_buffer(avctx, &new_frame)) {
00277 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00278 return -1;
00279 }
00280
00281 new_frame_data = (uint16_t*)new_frame.data[0];
00282 new_stride = new_frame.linesize[0] / 2;
00283 old_frame_data = (uint16_t*)s->frame.data[0];
00284 old_stride = s->frame.linesize[0] / 2;
00285
00286 for (superblock_index = 0; superblock_index < s->num_superblocks;
00287 superblock_index++) {
00288 MacroBlock mb;
00289 SuperBlock sb;
00290 unsigned multi_mask = 0;
00291
00292 if (skip == -1) {
00293
00294
00295 skip = decode_skip_count(&gb);
00296 }
00297
00298 if (skip) {
00299 copy_superblock(new_frame_data, new_stride,
00300 old_frame_data, old_stride);
00301 } else {
00302 copy_superblock(sb.pixels, 8,
00303 old_frame_data, old_stride);
00304
00305 while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00306 unsigned mask;
00307 mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
00308 mask = get_bits(&gb, 16);
00309 multi_mask |= mask;
00310 for (i = 0; i < 16; i++) {
00311 if (mask & mask_matrix[i]) {
00312 insert_mb_into_sb(&sb, mb, i);
00313 }
00314 }
00315 }
00316
00317 if (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00318 unsigned inv_mask = get_bits(&gb, 4);
00319 for (i = 0; i < 4; i++) {
00320 if (inv_mask & (1 << i)) {
00321 multi_mask ^= 0xF << i*4;
00322 } else {
00323 multi_mask ^= get_bits(&gb, 4) << i*4;
00324 }
00325 }
00326
00327 for (i = 0; i < 16; i++) {
00328 if (multi_mask & mask_matrix[i]) {
00329 if (!can_safely_read(&gb, 1))
00330 break;
00331 mb = decode_macroblock(s, &gb, &cb_index,
00332 superblock_index);
00333 insert_mb_into_sb(&sb, mb, i);
00334 }
00335 }
00336 } else if (frame_flags & (1 << 16)) {
00337 while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00338 mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
00339 insert_mb_into_sb(&sb, mb, get_bits(&gb, 4));
00340 }
00341 }
00342
00343 copy_superblock(new_frame_data, new_stride, sb.pixels, 8);
00344 }
00345
00346 superblock_col_index++;
00347 new_frame_data += 8;
00348 if (old_frame_data)
00349 old_frame_data += 8;
00350 if (superblock_col_index == superblocks_per_row) {
00351 new_frame_data += new_stride * 8 - superblocks_per_row * 8;
00352 if (old_frame_data)
00353 old_frame_data += old_stride * 8 - superblocks_per_row * 8;
00354 superblock_col_index = 0;
00355 }
00356 skip--;
00357 }
00358
00359 av_log(NULL, AV_LOG_DEBUG,
00360 "Escape sizes: %i, %i, %i\n",
00361 frame_size, buf_size, get_bits_count(&gb) / 8);
00362
00363 if (s->frame.data[0])
00364 avctx->release_buffer(avctx, &s->frame);
00365
00366 *(AVFrame*)data = s->frame = new_frame;
00367 *data_size = sizeof(AVFrame);
00368
00369 return frame_size;
00370 }
00371
00372
00373 AVCodec escape124_decoder = {
00374 "escape124",
00375 CODEC_TYPE_VIDEO,
00376 CODEC_ID_ESCAPE124,
00377 sizeof(Escape124Context),
00378 escape124_decode_init,
00379 NULL,
00380 escape124_decode_close,
00381 escape124_decode_frame,
00382 CODEC_CAP_DR1,
00383 .long_name = NULL_IF_CONFIG_SMALL("Escape 124"),
00384 };
00385