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