Libav 0.7.1
|
00001 /* 00002 * Escape 124 Video Decoder 00003 * Copyright (C) 2008 Eli Friedman (eli.friedman@gmail.com) 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 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 // This function reads a maximum of 23 bits, 00122 // which is within the padding space 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 // This function reads a maximum of 22 bits; the callers 00144 // guard this function appropriately 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 // depth = 0 means that this shouldn't read any bits; 00155 // in theory, this is the same as get_bits(gb, 0), but 00156 // that doesn't actually work. 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 // This condition can occur with invalid bitstreams and 00164 // *codebook_index == 2 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 // Formula: ((index / 4) * 16 + (index % 4) * 2) / 2 00173 uint32_t *dst = sb->pixels32 + index + (index & -4); 00174 00175 // This technically violates C99 aliasing rules, but it should be safe. 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 00198 static int escape124_decode_frame(AVCodecContext *avctx, 00199 void *data, int *data_size, 00200 AVPacket *avpkt) 00201 { 00202 const uint8_t *buf = avpkt->data; 00203 int buf_size = avpkt->size; 00204 Escape124Context *s = avctx->priv_data; 00205 00206 GetBitContext gb; 00207 unsigned frame_flags, frame_size; 00208 unsigned i; 00209 00210 unsigned superblock_index, cb_index = 1, 00211 superblock_col_index = 0, 00212 superblocks_per_row = avctx->width / 8, skip = -1; 00213 00214 uint16_t* old_frame_data, *new_frame_data; 00215 unsigned old_stride, new_stride; 00216 00217 AVFrame new_frame = { { 0 } }; 00218 00219 init_get_bits(&gb, buf, buf_size * 8); 00220 00221 // This call also guards the potential depth reads for the 00222 // codebook unpacking. 00223 if (!can_safely_read(&gb, 64)) 00224 return -1; 00225 00226 frame_flags = get_bits_long(&gb, 32); 00227 frame_size = get_bits_long(&gb, 32); 00228 00229 // Leave last frame unchanged 00230 // FIXME: Is this necessary? I haven't seen it in any real samples 00231 if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) { 00232 av_log(NULL, AV_LOG_DEBUG, "Skipping frame\n"); 00233 00234 *data_size = sizeof(AVFrame); 00235 *(AVFrame*)data = s->frame; 00236 00237 return frame_size; 00238 } 00239 00240 for (i = 0; i < 3; i++) { 00241 if (frame_flags & (1 << (17 + i))) { 00242 unsigned cb_depth, cb_size; 00243 if (i == 2) { 00244 // This codebook can be cut off at places other than 00245 // powers of 2, leaving some of the entries undefined. 00246 cb_size = get_bits_long(&gb, 20); 00247 cb_depth = av_log2(cb_size - 1) + 1; 00248 } else { 00249 cb_depth = get_bits(&gb, 4); 00250 if (i == 0) { 00251 // This is the most basic codebook: pow(2,depth) entries 00252 // for a depth-length key 00253 cb_size = 1 << cb_depth; 00254 } else { 00255 // This codebook varies per superblock 00256 // FIXME: I don't think this handles integer overflow 00257 // properly 00258 cb_size = s->num_superblocks << cb_depth; 00259 } 00260 } 00261 av_free(s->codebooks[i].blocks); 00262 s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size); 00263 if (!s->codebooks[i].blocks) 00264 return -1; 00265 } 00266 } 00267 00268 new_frame.reference = 3; 00269 if (avctx->get_buffer(avctx, &new_frame)) { 00270 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00271 return -1; 00272 } 00273 00274 new_frame_data = (uint16_t*)new_frame.data[0]; 00275 new_stride = new_frame.linesize[0] / 2; 00276 old_frame_data = (uint16_t*)s->frame.data[0]; 00277 old_stride = s->frame.linesize[0] / 2; 00278 00279 for (superblock_index = 0; superblock_index < s->num_superblocks; 00280 superblock_index++) { 00281 MacroBlock mb; 00282 SuperBlock sb; 00283 unsigned multi_mask = 0; 00284 00285 if (skip == -1) { 00286 // Note that this call will make us skip the rest of the blocks 00287 // if the frame prematurely ends 00288 skip = decode_skip_count(&gb); 00289 } 00290 00291 if (skip) { 00292 copy_superblock(new_frame_data, new_stride, 00293 old_frame_data, old_stride); 00294 } else { 00295 copy_superblock(sb.pixels, 8, 00296 old_frame_data, old_stride); 00297 00298 while (can_safely_read(&gb, 1) && !get_bits1(&gb)) { 00299 unsigned mask; 00300 mb = decode_macroblock(s, &gb, &cb_index, superblock_index); 00301 mask = get_bits(&gb, 16); 00302 multi_mask |= mask; 00303 for (i = 0; i < 16; i++) { 00304 if (mask & mask_matrix[i]) { 00305 insert_mb_into_sb(&sb, mb, i); 00306 } 00307 } 00308 } 00309 00310 if (can_safely_read(&gb, 1) && !get_bits1(&gb)) { 00311 unsigned inv_mask = get_bits(&gb, 4); 00312 for (i = 0; i < 4; i++) { 00313 if (inv_mask & (1 << i)) { 00314 multi_mask ^= 0xF << i*4; 00315 } else { 00316 multi_mask ^= get_bits(&gb, 4) << i*4; 00317 } 00318 } 00319 00320 for (i = 0; i < 16; i++) { 00321 if (multi_mask & mask_matrix[i]) { 00322 if (!can_safely_read(&gb, 1)) 00323 break; 00324 mb = decode_macroblock(s, &gb, &cb_index, 00325 superblock_index); 00326 insert_mb_into_sb(&sb, mb, i); 00327 } 00328 } 00329 } else if (frame_flags & (1 << 16)) { 00330 while (can_safely_read(&gb, 1) && !get_bits1(&gb)) { 00331 mb = decode_macroblock(s, &gb, &cb_index, superblock_index); 00332 insert_mb_into_sb(&sb, mb, get_bits(&gb, 4)); 00333 } 00334 } 00335 00336 copy_superblock(new_frame_data, new_stride, sb.pixels, 8); 00337 } 00338 00339 superblock_col_index++; 00340 new_frame_data += 8; 00341 if (old_frame_data) 00342 old_frame_data += 8; 00343 if (superblock_col_index == superblocks_per_row) { 00344 new_frame_data += new_stride * 8 - superblocks_per_row * 8; 00345 if (old_frame_data) 00346 old_frame_data += old_stride * 8 - superblocks_per_row * 8; 00347 superblock_col_index = 0; 00348 } 00349 skip--; 00350 } 00351 00352 av_log(NULL, AV_LOG_DEBUG, 00353 "Escape sizes: %i, %i, %i\n", 00354 frame_size, buf_size, get_bits_count(&gb) / 8); 00355 00356 if (s->frame.data[0]) 00357 avctx->release_buffer(avctx, &s->frame); 00358 00359 *(AVFrame*)data = s->frame = new_frame; 00360 *data_size = sizeof(AVFrame); 00361 00362 return frame_size; 00363 } 00364 00365 00366 AVCodec ff_escape124_decoder = { 00367 "escape124", 00368 AVMEDIA_TYPE_VIDEO, 00369 CODEC_ID_ESCAPE124, 00370 sizeof(Escape124Context), 00371 escape124_decode_init, 00372 NULL, 00373 escape124_decode_close, 00374 escape124_decode_frame, 00375 CODEC_CAP_DR1, 00376 .long_name = NULL_IF_CONFIG_SMALL("Escape 124"), 00377 }; 00378