mimic.c File Reference

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "avcodec.h"
#include "bitstream.h"
#include "bytestream.h"
#include "dsputil.h"

Go to the source code of this file.

Data Structures

struct  MimicContext

Defines

#define MIMIC_HEADER_SIZE   20

Functions

static av_cold int mimic_decode_init (AVCodecContext *avctx)
static int vlc_decode_block (MimicContext *ctx, int num_coeffs, int qscale)
static int decode (MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
static void prepare_avpic (MimicContext *ctx, AVPicture *dst, AVPicture *src)
static int mimic_decode_frame (AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
static av_cold int mimic_decode_end (AVCodecContext *avctx)

Variables

static const uint32_t huffcodes []
static const uint8_t huffbits []
static const uint8_t col_zag [64]
static const int8_t vlcdec_lookup [9][64]
AVCodec mimic_decoder


Define Documentation

#define MIMIC_HEADER_SIZE   20

Definition at line 31 of file mimic.c.

Referenced by mimic_decode_frame().


Function Documentation

static int decode ( MimicContext ctx,
int  quality,
int  num_coeffs,
int  is_iframe 
) [static]

Definition at line 205 of file mimic.c.

References av_clip(), av_log(), AV_LOG_ERROR, MimicContext::avctx, MimicContext::cur_index, AVPicture::data, MimicContext::dsp, MimicContext::flipped_ptrs, MimicContext::gb, get_bits(), get_bits1(), DSPContext::idct_put, AVPicture::linesize, MimicContext::num_hblocks, MimicContext::num_vblocks, MimicContext::prev_index, DSPContext::put_pixels_tab, src, stride, and vlc_decode_block().

Referenced by ra288_decode_frame(), and show_formats().

00207 {
00208     int y, x, plane;
00209 
00210     for(plane = 0; plane < 3; plane++) {
00211         const int is_chroma = !!plane;
00212         const int qscale = av_clip(10000-quality,is_chroma?1000:2000,10000)<<2;
00213         const int stride = ctx->flipped_ptrs[ctx->cur_index].linesize[plane];
00214         const uint8_t *src = ctx->flipped_ptrs[ctx->prev_index].data[plane];
00215         uint8_t       *dst = ctx->flipped_ptrs[ctx->cur_index ].data[plane];
00216 
00217         for(y = 0; y < ctx->num_vblocks[plane]; y++) {
00218             for(x = 0; x < ctx->num_hblocks[plane]; x++) {
00219 
00220                 /* Check for a change condition in the current block.
00221                  * - iframes always change.
00222                  * - Luma plane changes on get_bits1 == 0
00223                  * - Chroma planes change on get_bits1 == 1 */
00224                 if(is_iframe || get_bits1(&ctx->gb) == is_chroma) {
00225 
00226                     /* Luma planes may use a backreference from the 15 last
00227                      * frames preceding the previous. (get_bits1 == 1)
00228                      * Chroma planes don't use backreferences. */
00229                     if(is_chroma || is_iframe || !get_bits1(&ctx->gb)) {
00230 
00231                         if(!vlc_decode_block(ctx, num_coeffs, qscale))
00232                             return 0;
00233                         ctx->dsp.idct_put(dst, stride, ctx->dct_block);
00234                     } else {
00235                         unsigned int backref = get_bits(&ctx->gb, 4);
00236                         int index = (ctx->cur_index+backref)&15;
00237                         uint8_t *p = ctx->flipped_ptrs[index].data[0];
00238 
00239                         if(p) {
00240                             p += src -
00241                                 ctx->flipped_ptrs[ctx->prev_index].data[plane];
00242                             ctx->dsp.put_pixels_tab[1][0](dst, p, stride, 8);
00243                         } else {
00244                             av_log(ctx->avctx, AV_LOG_ERROR,
00245                                      "No such backreference! Buggy sample.\n");
00246                         }
00247                     }
00248                 } else {
00249                     ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
00250                 }
00251                 src += 8;
00252                 dst += 8;
00253             }
00254             src += (stride - ctx->num_hblocks[plane])<<3;
00255             dst += (stride - ctx->num_hblocks[plane])<<3;
00256         }
00257     }
00258 
00259     return 1;
00260 }

static av_cold int mimic_decode_end ( AVCodecContext avctx  )  [static]

Definition at line 365 of file mimic.c.

References av_free(), MimicContext::buf_ptrs, free_vlc(), AVCodecContext::priv_data, AVCodecContext::release_buffer, MimicContext::swap_buf, and MimicContext::vlc.

00366 {
00367     MimicContext *ctx = avctx->priv_data;
00368     int i;
00369 
00370     av_free(ctx->swap_buf);
00371     for(i = 0; i < 16; i++)
00372         if(ctx->buf_ptrs[i].data[0])
00373             avctx->release_buffer(avctx, &ctx->buf_ptrs[i]);
00374     free_vlc(&ctx->vlc);
00375 
00376     return 0;
00377 }

static int mimic_decode_frame ( AVCodecContext avctx,
void *  data,
int *  data_size,
const uint8_t *  buf,
int  buf_size 
) [static]

Definition at line 276 of file mimic.c.

References av_log(), AV_LOG_ERROR, MimicContext::avctx, AVCodecContext::height, height, MIMIC_HEADER_SIZE, MimicContext::num_hblocks, MimicContext::num_vblocks, AVCodecContext::pix_fmt, PIX_FMT_YUV420P, AVCodecContext::priv_data, AVCodecContext::width, and width.

00278 {
00279     MimicContext *ctx = avctx->priv_data;
00280     int is_pframe;
00281     int width, height;
00282     int quality, num_coeffs;
00283     int swap_buf_size = buf_size - MIMIC_HEADER_SIZE;
00284 
00285     if(buf_size < MIMIC_HEADER_SIZE) {
00286         av_log(avctx, AV_LOG_ERROR, "insufficient data\n");
00287         return -1;
00288     }
00289 
00290     buf       += 2; /* some constant (always 256) */
00291     quality    = bytestream_get_le16(&buf);
00292     width      = bytestream_get_le16(&buf);
00293     height     = bytestream_get_le16(&buf);
00294     buf       += 4; /* some constant */
00295     is_pframe  = bytestream_get_le32(&buf);
00296     num_coeffs = bytestream_get_byte(&buf);
00297     buf       += 3; /* some constant */
00298 
00299     if(!ctx->avctx) {
00300         int i;
00301 
00302         if(!(width == 160 && height == 120) &&
00303            !(width == 320 && height == 240)) {
00304             av_log(avctx, AV_LOG_ERROR, "invalid width/height!\n");
00305             return -1;
00306         }
00307 
00308         ctx->avctx     = avctx;
00309         avctx->width   = width;
00310         avctx->height  = height;
00311         avctx->pix_fmt = PIX_FMT_YUV420P;
00312         for(i = 0; i < 3; i++) {
00313             ctx->num_vblocks[i] = -((-height) >> (3 + !!i));
00314             ctx->num_hblocks[i] =     width   >> (3 + !!i) ;
00315         }
00316     } else if(width != ctx->avctx->width || height != ctx->avctx->height) {
00317         av_log(avctx, AV_LOG_ERROR, "resolution changing is not supported\n");
00318         return -1;
00319     }
00320 
00321     if(is_pframe && !ctx->buf_ptrs[ctx->prev_index].data[0]) {
00322         av_log(avctx, AV_LOG_ERROR, "decoding must start with keyframe\n");
00323         return -1;
00324     }
00325 
00326     ctx->buf_ptrs[ctx->cur_index].reference = 1;
00327     if(avctx->get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
00328         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00329         return -1;
00330     }
00331 
00332     prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index],
00333                   (AVPicture*) &ctx->buf_ptrs[ctx->cur_index]);
00334 
00335     ctx->swap_buf = av_fast_realloc(ctx->swap_buf, &ctx->swap_buf_size,
00336                                  swap_buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00337     if(!ctx->swap_buf)
00338         return AVERROR_NOMEM;
00339 
00340     ctx->dsp.bswap_buf((uint32_t*)ctx->swap_buf,
00341                         (const uint32_t*) buf,
00342                         swap_buf_size>>2);
00343     init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3);
00344 
00345     if(!decode(ctx, quality, num_coeffs, !is_pframe)) {
00346         avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
00347         return -1;
00348     }
00349 
00350     ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE;
00351     *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index];
00352     *data_size = sizeof(AVFrame);
00353 
00354     ctx->prev_index = ctx->cur_index;
00355     ctx->cur_index--;
00356     ctx->cur_index &= 15;
00357 
00358     /* Only release frames that aren't used for backreferences anymore */
00359     if(ctx->buf_ptrs[ctx->cur_index].data[0])
00360         avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
00361 
00362     return buf_size;
00363 }

static av_cold int mimic_decode_init ( AVCodecContext avctx  )  [static]

Definition at line 106 of file mimic.c.

References av_log(), AV_LOG_ERROR, col_zag, MimicContext::cur_index, MimicContext::dsp, dsputil_init(), ff_init_scantable(), huffbits, huffcodes, DSPContext::idct_permutation, init_vlc, MimicContext::prev_index, AVCodecContext::priv_data, MimicContext::scantable, and MimicContext::vlc.

00107 {
00108     MimicContext *ctx = avctx->priv_data;
00109 
00110     ctx->prev_index = 0;
00111     ctx->cur_index = 15;
00112 
00113     if(init_vlc(&ctx->vlc, 11, sizeof(huffbits)/sizeof(huffbits[0]),
00114                  huffbits, 1, 1, huffcodes, 4, 4, 0)) {
00115         av_log(avctx, AV_LOG_ERROR, "error initializing vlc table\n");
00116         return -1;
00117     }
00118     dsputil_init(&ctx->dsp, avctx);
00119     ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, col_zag);
00120 
00121     return 0;
00122 }

static void prepare_avpic ( MimicContext ctx,
AVPicture dst,
AVPicture src 
) [static]

Flip the buffer upside-down and put it in the YVU order to match the way Mimic encodes frames.

Definition at line 266 of file mimic.c.

References MimicContext::avctx, AVPicture::data, AVCodecContext::height, AVPicture::linesize, and src.

00267 {
00268     int i;
00269     dst->data[0] = src->data[0]+( ctx->avctx->height    -1)*src->linesize[0];
00270     dst->data[1] = src->data[2]+((ctx->avctx->height>>1)-1)*src->linesize[2];
00271     dst->data[2] = src->data[1]+((ctx->avctx->height>>1)-1)*src->linesize[1];
00272     for(i = 0; i < 3; i++)
00273         dst->linesize[i] = -src->linesize[i];
00274 }

static int vlc_decode_block ( MimicContext ctx,
int  num_coeffs,
int  qscale 
) [static]

Definition at line 161 of file mimic.c.

References VLC::bits, MimicContext::gb, get_bits(), get_vlc2(), ScanTable::permutated, MimicContext::scantable, VLC::table, MimicContext::vlc, and vlcdec_lookup.

Referenced by decode().

00162 {
00163     DCTELEM *block = ctx->dct_block;
00164     unsigned int pos;
00165 
00166     memset(block, 0, 64 * sizeof(DCTELEM));
00167 
00168     block[0] = get_bits(&ctx->gb, 8) << 3;
00169 
00170     for(pos = 1; pos < num_coeffs; pos++) {
00171         uint32_t vlc, num_bits;
00172         int value;
00173         int coeff;
00174 
00175         vlc = get_vlc2(&ctx->gb, ctx->vlc.table, ctx->vlc.bits, 3);
00176         if(!vlc) /* end-of-block code */
00177             return 1;
00178         if(vlc == -1)
00179             return 0;
00180 
00181         /* pos_add and num_bits are coded in the vlc code */
00182         pos +=     vlc&15; // pos_add
00183         num_bits = vlc>>4; // num_bits
00184 
00185         if(pos >= 64)
00186             return 0;
00187 
00188         value = get_bits(&ctx->gb, num_bits);
00189 
00190         /* FFmpeg's IDCT behaves somewhat different from the original code, so
00191          * a factor of 4 was added to the input */
00192 
00193         coeff = vlcdec_lookup[num_bits][value];
00194         if(pos<3)
00195             coeff <<= 4;
00196         else /* TODO Use >> 10 instead of / 1001 */
00197             coeff = (coeff * qscale) / 1001;
00198 
00199         block[ctx->scantable.permutated[pos]] = coeff;
00200     }
00201 
00202     return 1;
00203 }


Variable Documentation

const uint8_t col_zag[64] [static]

Initial value:

 {
     0,  8,  1,  2,  9, 16, 24, 17,
    10,  3,  4, 11, 18, 25, 32, 40,
    33, 26, 19, 12,  5,  6, 13, 20,
    27, 34, 41, 48, 56, 49, 42, 35,
    28, 21, 14,  7, 15, 22, 29, 36,
    43, 50, 57, 58, 51, 44, 37, 30,
    23, 31, 38, 45, 52, 59, 39, 46,
    53, 60, 61, 54, 47, 55, 62, 63,
}

Definition at line 95 of file mimic.c.

Referenced by mimic_decode_init().

const uint8_t huffbits[] [static]

Initial value:

 {
     4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  2,  4,  5,  6,  7,  7,  7,  8,
     8, 10, 11, 11, 11, 11, 12, 12,  2,  6,  7,  8,
     9,  9, 12, 12, 13, 13, 13, 13, 14, 14, 14,  0,
     3,  6,  9, 14, 15, 15, 15, 15, 16, 16, 16, 16,
    17, 17, 17,  0,  4,  8,  9, 17, 18, 18, 18, 18,
    19, 19, 19, 19, 20, 20, 20,  0,  5, 10, 20, 21,
    21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23,  0,
     6, 10, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26,
    26, 26, 27,  0, 10, 27, 27, 27, 28, 28, 28, 28,
    29, 29, 29, 29, 30, 30, 30,
}

Definition at line 81 of file mimic.c.

Referenced by mimic_decode_init().

const uint32_t huffcodes[] [static]

Initial value:

 {
    0x0000000a, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000000b,
    0x0000001b, 0x00000038, 0x00000078, 0x00000079, 0x0000007a, 0x000000f9,
    0x000000fa, 0x000003fb, 0x000007f8, 0x000007f9, 0x000007fa, 0x000007fb,
    0x00000ff8, 0x00000ff9, 0x00000001, 0x00000039, 0x0000007b, 0x000000fb,
    0x000001f8, 0x000001f9, 0x00000ffa, 0x00000ffb, 0x00001ff8, 0x00001ff9,
    0x00001ffa, 0x00001ffb, 0x00003ff8, 0x00003ff9, 0x00003ffa, 0x00000000,
    0x00000004, 0x0000003a, 0x000001fa, 0x00003ffb, 0x00007ff8, 0x00007ff9,
    0x00007ffa, 0x00007ffb, 0x0000fff8, 0x0000fff9, 0x0000fffa, 0x0000fffb,
    0x0001fff8, 0x0001fff9, 0x0001fffa, 0x00000000, 0x0000000c, 0x000000f8,
    0x000001fb, 0x0001fffb, 0x0003fff8, 0x0003fff9, 0x0003fffa, 0x0003fffb,
    0x0007fff8, 0x0007fff9, 0x0007fffa, 0x0007fffb, 0x000ffff8, 0x000ffff9,
    0x000ffffa, 0x00000000, 0x0000001a, 0x000003f8, 0x000ffffb, 0x001ffff8,
    0x001ffff9, 0x001ffffa, 0x001ffffb, 0x003ffff8, 0x003ffff9, 0x003ffffa,
    0x003ffffb, 0x007ffff8, 0x007ffff9, 0x007ffffa, 0x007ffffb, 0x00000000,
    0x0000003b, 0x000003f9, 0x00fffff8, 0x00fffff9, 0x00fffffa, 0x00fffffb,
    0x01fffff8, 0x01fffff9, 0x01fffffa, 0x01fffffb, 0x03fffff8, 0x03fffff9,
    0x03fffffa, 0x03fffffb, 0x07fffff8, 0x00000000, 0x000003fa, 0x07fffff9,
    0x07fffffa, 0x07fffffb, 0x0ffffff8, 0x0ffffff9, 0x0ffffffa, 0x0ffffffb,
    0x1ffffff8, 0x1ffffff9, 0x1ffffffa, 0x1ffffffb, 0x3ffffff8, 0x3ffffff9,
    0x3ffffffa,
}

Definition at line 56 of file mimic.c.

Referenced by mimic_decode_init().

AVCodec mimic_decoder

Initial value:

Definition at line 379 of file mimic.c.

const int8_t vlcdec_lookup[9][64] [static]

Definition at line 124 of file mimic.c.

Referenced by vlc_decode_block().


Generated on Thu Nov 20 08:45:33 2008 for libextractor by  doxygen 1.5.1