• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/escape124.c

Go to the documentation of this file.
00001 /*
00002  * Escape 124 Video Decoder
00003  * Copyright (C) 2008 Eli Friedman (eli.friedman@gmail.com)
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
00207 static int escape124_decode_frame(AVCodecContext *avctx,
00208                                   void *data, int *data_size,
00209                                   AVPacket *avpkt)
00210 {
00211     const uint8_t *buf = avpkt->data;
00212     int buf_size = avpkt->size;
00213     Escape124Context *s = avctx->priv_data;
00214 
00215     GetBitContext gb;
00216     unsigned frame_flags, frame_size;
00217     unsigned i;
00218 
00219     unsigned superblock_index, cb_index = 1,
00220              superblock_col_index = 0,
00221              superblocks_per_row = avctx->width / 8, skip = -1;
00222 
00223     uint16_t* old_frame_data, *new_frame_data;
00224     unsigned old_stride, new_stride;
00225 
00226     AVFrame new_frame = { { 0 } };
00227 
00228     init_get_bits(&gb, buf, buf_size * 8);
00229 
00230     // This call also guards the potential depth reads for the
00231     // codebook unpacking.
00232     if (!can_safely_read(&gb, 64))
00233         return -1;
00234 
00235     frame_flags = get_bits_long(&gb, 32);
00236     frame_size  = get_bits_long(&gb, 32);
00237 
00238     // Leave last frame unchanged
00239     // FIXME: Is this necessary?  I haven't seen it in any real samples
00240     if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
00241         av_log(NULL, AV_LOG_DEBUG, "Skipping frame\n");
00242 
00243         *data_size = sizeof(AVFrame);
00244         *(AVFrame*)data = s->frame;
00245 
00246         return frame_size;
00247     }
00248 
00249     for (i = 0; i < 3; i++) {
00250         if (frame_flags & (1 << (17 + i))) {
00251             unsigned cb_depth, cb_size;
00252             if (i == 2) {
00253                 // This codebook can be cut off at places other than
00254                 // powers of 2, leaving some of the entries undefined.
00255                 cb_size = get_bits_long(&gb, 20);
00256                 cb_depth = av_log2(cb_size - 1) + 1;
00257             } else {
00258                 cb_depth = get_bits(&gb, 4);
00259                 if (i == 0) {
00260                     // This is the most basic codebook: pow(2,depth) entries
00261                     // for a depth-length key
00262                     cb_size = 1 << cb_depth;
00263                 } else {
00264                     // This codebook varies per superblock
00265                     // FIXME: I don't think this handles integer overflow
00266                     // properly
00267                     cb_size = s->num_superblocks << cb_depth;
00268                 }
00269             }
00270             av_free(s->codebooks[i].blocks);
00271             s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size);
00272             if (!s->codebooks[i].blocks)
00273                 return -1;
00274         }
00275     }
00276 
00277     new_frame.reference = 3;
00278     if (avctx->get_buffer(avctx, &new_frame)) {
00279         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00280         return -1;
00281     }
00282 
00283     new_frame_data = (uint16_t*)new_frame.data[0];
00284     new_stride = new_frame.linesize[0] / 2;
00285     old_frame_data = (uint16_t*)s->frame.data[0];
00286     old_stride = s->frame.linesize[0] / 2;
00287 
00288     for (superblock_index = 0; superblock_index < s->num_superblocks;
00289          superblock_index++) {
00290         MacroBlock mb;
00291         SuperBlock sb;
00292         unsigned multi_mask = 0;
00293 
00294         if (skip == -1) {
00295             // Note that this call will make us skip the rest of the blocks
00296             // if the frame prematurely ends
00297             skip = decode_skip_count(&gb);
00298         }
00299 
00300         if (skip) {
00301             copy_superblock(new_frame_data, new_stride,
00302                             old_frame_data, old_stride);
00303         } else {
00304             copy_superblock(sb.pixels, 8,
00305                             old_frame_data, old_stride);
00306 
00307             while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00308                 unsigned mask;
00309                 mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
00310                 mask = get_bits(&gb, 16);
00311                 multi_mask |= mask;
00312                 for (i = 0; i < 16; i++) {
00313                     if (mask & mask_matrix[i]) {
00314                         insert_mb_into_sb(&sb, mb, i);
00315                     }
00316                 }
00317             }
00318 
00319             if (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00320                 unsigned inv_mask = get_bits(&gb, 4);
00321                 for (i = 0; i < 4; i++) {
00322                     if (inv_mask & (1 << i)) {
00323                         multi_mask ^= 0xF << i*4;
00324                     } else {
00325                         multi_mask ^= get_bits(&gb, 4) << i*4;
00326                     }
00327                 }
00328 
00329                 for (i = 0; i < 16; i++) {
00330                     if (multi_mask & mask_matrix[i]) {
00331                         if (!can_safely_read(&gb, 1))
00332                             break;
00333                         mb = decode_macroblock(s, &gb, &cb_index,
00334                                                superblock_index);
00335                         insert_mb_into_sb(&sb, mb, i);
00336                     }
00337                 }
00338             } else if (frame_flags & (1 << 16)) {
00339                 while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
00340                     mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
00341                     insert_mb_into_sb(&sb, mb, get_bits(&gb, 4));
00342                 }
00343             }
00344 
00345             copy_superblock(new_frame_data, new_stride, sb.pixels, 8);
00346         }
00347 
00348         superblock_col_index++;
00349         new_frame_data += 8;
00350         if (old_frame_data)
00351             old_frame_data += 8;
00352         if (superblock_col_index == superblocks_per_row) {
00353             new_frame_data += new_stride * 8 - superblocks_per_row * 8;
00354             if (old_frame_data)
00355                 old_frame_data += old_stride * 8 - superblocks_per_row * 8;
00356             superblock_col_index = 0;
00357         }
00358         skip--;
00359     }
00360 
00361     av_log(NULL, AV_LOG_DEBUG,
00362            "Escape sizes: %i, %i, %i\n",
00363            frame_size, buf_size, get_bits_count(&gb) / 8);
00364 
00365     if (s->frame.data[0])
00366         avctx->release_buffer(avctx, &s->frame);
00367 
00368     *(AVFrame*)data = s->frame = new_frame;
00369     *data_size = sizeof(AVFrame);
00370 
00371     return frame_size;
00372 }
00373 
00374 
00375 AVCodec escape124_decoder = {
00376     "escape124",
00377     AVMEDIA_TYPE_VIDEO,
00378     CODEC_ID_ESCAPE124,
00379     sizeof(Escape124Context),
00380     escape124_decode_init,
00381     NULL,
00382     escape124_decode_close,
00383     escape124_decode_frame,
00384     CODEC_CAP_DR1,
00385     .long_name = NULL_IF_CONFIG_SMALL("Escape 124"),
00386 };
00387 

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1