libavcodec/truemotion2.c
Go to the documentation of this file.
00001 /*
00002  * Duck/ON2 TrueMotion 2 Decoder
00003  * Copyright (c) 2005 Konstantin Shishkov
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 
00027 #include "avcodec.h"
00028 #include "bytestream.h"
00029 #include "get_bits.h"
00030 #include "dsputil.h"
00031 
00032 #define TM2_ESCAPE 0x80000000
00033 #define TM2_DELTAS 64
00034 /* Huffman-coded streams of different types of blocks */
00035 enum TM2_STREAMS{ TM2_C_HI = 0, TM2_C_LO, TM2_L_HI, TM2_L_LO,
00036      TM2_UPD, TM2_MOT, TM2_TYPE, TM2_NUM_STREAMS};
00037 /* Block types */
00038 enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
00039                  TM2_UPDATE, TM2_STILL, TM2_MOTION};
00040 
00041 typedef struct TM2Context{
00042     AVCodecContext *avctx;
00043     AVFrame pic;
00044 
00045     GetBitContext gb;
00046     DSPContext dsp;
00047 
00048     uint8_t *buffer;
00049     int buffer_size;
00050 
00051     /* TM2 streams */
00052     int *tokens[TM2_NUM_STREAMS];
00053     int tok_lens[TM2_NUM_STREAMS];
00054     int tok_ptrs[TM2_NUM_STREAMS];
00055     int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
00056     /* for blocks decoding */
00057     int D[4];
00058     int CD[4];
00059     int *last;
00060     int *clast;
00061 
00062     /* data for current and previous frame */
00063     int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
00064     int *Y1, *U1, *V1, *Y2, *U2, *V2;
00065     int y_stride, uv_stride;
00066     int cur;
00067 } TM2Context;
00068 
00072 typedef struct TM2Codes{
00073     VLC vlc; 
00074     int bits;
00075     int *recode; 
00076     int length;
00077 } TM2Codes;
00078 
00082 typedef struct TM2Huff{
00083     int val_bits; 
00084     int max_bits; 
00085     int min_bits; 
00086     int nodes; 
00087     int num; 
00088     int max_num; 
00089     int *nums; 
00090     uint32_t *bits; 
00091     int *lens; 
00092 } TM2Huff;
00093 
00094 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
00095 {
00096     if(length > huff->max_bits) {
00097         av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits);
00098         return -1;
00099     }
00100 
00101     if(!get_bits1(&ctx->gb)) { /* literal */
00102         if (length == 0) {
00103             length = 1;
00104         }
00105         if(huff->num >= huff->max_num) {
00106             av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
00107             return -1;
00108         }
00109         huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
00110         huff->bits[huff->num] = prefix;
00111         huff->lens[huff->num] = length;
00112         huff->num++;
00113         return 0;
00114     } else { /* non-terminal node */
00115         if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)
00116             return -1;
00117         if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)
00118             return -1;
00119     }
00120     return 0;
00121 }
00122 
00123 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
00124 {
00125     TM2Huff huff;
00126     int res = 0;
00127 
00128     huff.val_bits = get_bits(&ctx->gb, 5);
00129     huff.max_bits = get_bits(&ctx->gb, 5);
00130     huff.min_bits = get_bits(&ctx->gb, 5);
00131     huff.nodes = get_bits_long(&ctx->gb, 17);
00132     huff.num = 0;
00133 
00134     /* check for correct codes parameters */
00135     if((huff.val_bits < 1) || (huff.val_bits > 32) ||
00136        (huff.max_bits < 0) || (huff.max_bits > 25)) {
00137         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
00138                huff.val_bits, huff.max_bits);
00139         return -1;
00140     }
00141     if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
00142         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
00143         return -1;
00144     }
00145     /* one-node tree */
00146     if(huff.max_bits == 0)
00147         huff.max_bits = 1;
00148 
00149     /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
00150     huff.max_num = (huff.nodes + 1) >> 1;
00151     huff.nums = av_mallocz(huff.max_num * sizeof(int));
00152     huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
00153     huff.lens = av_mallocz(huff.max_num * sizeof(int));
00154 
00155     if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
00156         res = -1;
00157 
00158     if(huff.num != huff.max_num) {
00159         av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
00160                huff.num, huff.max_num);
00161         res = -1;
00162     }
00163 
00164     /* convert codes to vlc_table */
00165     if(res != -1) {
00166         int i;
00167 
00168         res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
00169                     huff.lens, sizeof(int), sizeof(int),
00170                     huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
00171         if(res < 0) {
00172             av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00173             res = -1;
00174         } else
00175             res = 0;
00176         if(res != -1) {
00177             code->bits = huff.max_bits;
00178             code->length = huff.max_num;
00179             code->recode = av_malloc(code->length * sizeof(int));
00180             for(i = 0; i < code->length; i++)
00181                 code->recode[i] = huff.nums[i];
00182         }
00183     }
00184     /* free allocated memory */
00185     av_free(huff.nums);
00186     av_free(huff.bits);
00187     av_free(huff.lens);
00188 
00189     return res;
00190 }
00191 
00192 static void tm2_free_codes(TM2Codes *code)
00193 {
00194     av_free(code->recode);
00195     if(code->vlc.table)
00196         free_vlc(&code->vlc);
00197 }
00198 
00199 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
00200 {
00201     int val;
00202     val = get_vlc2(gb, code->vlc.table, code->bits, 1);
00203     return code->recode[val];
00204 }
00205 
00206 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
00207 {
00208     uint32_t magic;
00209     const uint8_t *obuf;
00210 
00211     obuf = buf;
00212 
00213     magic = AV_RL32(buf);
00214     buf += 4;
00215 
00216     if(magic == 0x00000100) { /* old header */
00217 /*      av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
00218         return 40;
00219     } else if(magic == 0x00000101) { /* new header */
00220         return 40;
00221     } else {
00222         av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
00223         return -1;
00224     }
00225 
00226     return buf - obuf;
00227 }
00228 
00229 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
00230     int d, mb;
00231     int i, v;
00232 
00233     d = get_bits(&ctx->gb, 9);
00234     mb = get_bits(&ctx->gb, 5);
00235 
00236     if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
00237         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
00238         return -1;
00239     }
00240 
00241     for(i = 0; i < d; i++) {
00242         v = get_bits_long(&ctx->gb, mb);
00243         if(v & (1 << (mb - 1)))
00244             ctx->deltas[stream_id][i] = v - (1 << mb);
00245         else
00246             ctx->deltas[stream_id][i] = v;
00247     }
00248     for(; i < TM2_DELTAS; i++)
00249         ctx->deltas[stream_id][i] = 0;
00250 
00251     return 0;
00252 }
00253 
00254 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
00255 {
00256     int i;
00257     int skip = 0;
00258     int len, toks, pos;
00259     TM2Codes codes;
00260     GetByteContext gb;
00261 
00262     /* get stream length in dwords */
00263     bytestream2_init(&gb, buf, buf_size);
00264     len  = bytestream2_get_be32(&gb);
00265     skip = len * 4 + 4;
00266 
00267     if(len == 0)
00268         return 4;
00269 
00270     if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
00271         av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
00272         return -1;
00273     }
00274 
00275     toks = bytestream2_get_be32(&gb);
00276     if(toks & 1) {
00277         len = bytestream2_get_be32(&gb);
00278         if(len == TM2_ESCAPE) {
00279             len = bytestream2_get_be32(&gb);
00280         }
00281         if(len > 0) {
00282             pos = bytestream2_tell(&gb);
00283             if (skip <= pos)
00284                 return -1;
00285             init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00286             if(tm2_read_deltas(ctx, stream_id) == -1)
00287                 return -1;
00288             bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
00289         }
00290     }
00291     /* skip unused fields */
00292     len = bytestream2_get_be32(&gb);
00293     if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
00294         bytestream2_skip(&gb, 8); /* unused by decoder */
00295     } else {
00296         bytestream2_skip(&gb, 4); /* unused by decoder */
00297     }
00298 
00299     pos = bytestream2_tell(&gb);
00300     if (skip <= pos)
00301         return -1;
00302     init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00303     if(tm2_build_huff_table(ctx, &codes) == -1)
00304         return -1;
00305     bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
00306 
00307     toks >>= 1;
00308     /* check if we have sane number of tokens */
00309     if((toks < 0) || (toks > 0xFFFFFF)){
00310         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00311         tm2_free_codes(&codes);
00312         return -1;
00313     }
00314     ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
00315     ctx->tok_lens[stream_id] = toks;
00316     len = bytestream2_get_be32(&gb);
00317     if(len > 0) {
00318         pos = bytestream2_tell(&gb);
00319         if (skip <= pos)
00320             return -1;
00321         init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00322         for(i = 0; i < toks; i++) {
00323             if (get_bits_left(&ctx->gb) <= 0) {
00324                 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00325                 return -1;
00326             }
00327             ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
00328             if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
00329                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
00330                        ctx->tokens[stream_id][i], stream_id, i);
00331                 return AVERROR_INVALIDDATA;
00332             }
00333         }
00334     } else {
00335         for(i = 0; i < toks; i++) {
00336             ctx->tokens[stream_id][i] = codes.recode[0];
00337             if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
00338                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
00339                        ctx->tokens[stream_id][i], stream_id, i);
00340                 return AVERROR_INVALIDDATA;
00341             }
00342         }
00343     }
00344     tm2_free_codes(&codes);
00345 
00346     return skip;
00347 }
00348 
00349 static inline int GET_TOK(TM2Context *ctx,int type) {
00350     if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
00351         av_log(ctx->avctx, AV_LOG_ERROR, "Read token from stream %i out of bounds (%i>=%i)\n", type, ctx->tok_ptrs[type], ctx->tok_lens[type]);
00352         return 0;
00353     }
00354     if(type <= TM2_MOT)
00355         return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
00356     return ctx->tokens[type][ctx->tok_ptrs[type]++];
00357 }
00358 
00359 /* blocks decoding routines */
00360 
00361 /* common Y, U, V pointers initialisation */
00362 #define TM2_INIT_POINTERS() \
00363     int *last, *clast; \
00364     int *Y, *U, *V;\
00365     int Ystride, Ustride, Vstride;\
00366 \
00367     Ystride = ctx->y_stride;\
00368     Vstride = ctx->uv_stride;\
00369     Ustride = ctx->uv_stride;\
00370     Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
00371     V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
00372     U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
00373     last = ctx->last + bx * 4;\
00374     clast = ctx->clast + bx * 4;
00375 
00376 #define TM2_INIT_POINTERS_2() \
00377     int *Yo, *Uo, *Vo;\
00378     int oYstride, oUstride, oVstride;\
00379 \
00380     TM2_INIT_POINTERS();\
00381     oYstride = Ystride;\
00382     oVstride = Vstride;\
00383     oUstride = Ustride;\
00384     Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
00385     Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
00386     Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
00387 
00388 /* recalculate last and delta values for next blocks */
00389 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
00390     CD[0] = CHR[1] - last[1];\
00391     CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
00392     last[0] = (int)CHR[stride + 0];\
00393     last[1] = (int)CHR[stride + 1];}
00394 
00395 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
00396 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
00397 {
00398     int ct, d;
00399     int i, j;
00400 
00401     for(j = 0; j < 4; j++){
00402         ct = ctx->D[j];
00403         for(i = 0; i < 4; i++){
00404             d = deltas[i + j * 4];
00405             ct += d;
00406             last[i] += ct;
00407             Y[i] = av_clip_uint8(last[i]);
00408         }
00409         Y += stride;
00410         ctx->D[j] = ct;
00411     }
00412 }
00413 
00414 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
00415 {
00416     int i, j;
00417     for(j = 0; j < 2; j++){
00418         for(i = 0; i < 2; i++){
00419             CD[j] += deltas[i + j * 2];
00420             last[i] += CD[j];
00421             data[i] = last[i];
00422         }
00423         data += stride;
00424     }
00425 }
00426 
00427 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
00428 {
00429     int t;
00430     int l;
00431     int prev;
00432 
00433     if(bx > 0)
00434         prev = clast[-3];
00435     else
00436         prev = 0;
00437     t = (CD[0] + CD[1]) >> 1;
00438     l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
00439     CD[1] = CD[0] + CD[1] - t;
00440     CD[0] = t;
00441     clast[0] = l;
00442 
00443     tm2_high_chroma(data, stride, clast, CD, deltas);
00444 }
00445 
00446 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00447 {
00448     int i;
00449     int deltas[16];
00450     TM2_INIT_POINTERS();
00451 
00452     /* hi-res chroma */
00453     for(i = 0; i < 4; i++) {
00454         deltas[i] = GET_TOK(ctx, TM2_C_HI);
00455         deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
00456     }
00457     tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
00458     tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
00459 
00460     /* hi-res luma */
00461     for(i = 0; i < 16; i++)
00462         deltas[i] = GET_TOK(ctx, TM2_L_HI);
00463 
00464     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00465 }
00466 
00467 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00468 {
00469     int i;
00470     int deltas[16];
00471     TM2_INIT_POINTERS();
00472 
00473     /* low-res chroma */
00474     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00475     deltas[1] = deltas[2] = deltas[3] = 0;
00476     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00477 
00478     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00479     deltas[1] = deltas[2] = deltas[3] = 0;
00480     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00481 
00482     /* hi-res luma */
00483     for(i = 0; i < 16; i++)
00484         deltas[i] = GET_TOK(ctx, TM2_L_HI);
00485 
00486     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00487 }
00488 
00489 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00490 {
00491     int i;
00492     int t1, t2;
00493     int deltas[16];
00494     TM2_INIT_POINTERS();
00495 
00496     /* low-res chroma */
00497     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00498     deltas[1] = deltas[2] = deltas[3] = 0;
00499     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00500 
00501     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00502     deltas[1] = deltas[2] = deltas[3] = 0;
00503     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00504 
00505     /* low-res luma */
00506     for(i = 0; i < 16; i++)
00507         deltas[i] = 0;
00508 
00509     deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
00510     deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
00511     deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
00512     deltas[10] = GET_TOK(ctx, TM2_L_LO);
00513 
00514     if(bx > 0)
00515         last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
00516     else
00517         last[0] = (last[1]  - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
00518     last[2] = (last[1] + last[3]) >> 1;
00519 
00520     t1 = ctx->D[0] + ctx->D[1];
00521     ctx->D[0] = t1 >> 1;
00522     ctx->D[1] = t1 - (t1 >> 1);
00523     t2 = ctx->D[2] + ctx->D[3];
00524     ctx->D[2] = t2 >> 1;
00525     ctx->D[3] = t2 - (t2 >> 1);
00526 
00527     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00528 }
00529 
00530 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00531 {
00532     int i;
00533     int ct;
00534     int left, right, diff;
00535     int deltas[16];
00536     TM2_INIT_POINTERS();
00537 
00538     /* null chroma */
00539     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00540     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00541 
00542     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00543     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00544 
00545     /* null luma */
00546     for(i = 0; i < 16; i++)
00547         deltas[i] = 0;
00548 
00549     ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
00550 
00551     if(bx > 0)
00552         left = last[-1] - ct;
00553     else
00554         left = 0;
00555 
00556     right = last[3];
00557     diff = right - left;
00558     last[0] = left + (diff >> 2);
00559     last[1] = left + (diff >> 1);
00560     last[2] = right - (diff >> 2);
00561     last[3] = right;
00562     {
00563         int tp = left;
00564 
00565         ctx->D[0] = (tp + (ct >> 2)) - left;
00566         left += ctx->D[0];
00567         ctx->D[1] = (tp + (ct >> 1)) - left;
00568         left += ctx->D[1];
00569         ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
00570         left += ctx->D[2];
00571         ctx->D[3] = (tp + ct) - left;
00572     }
00573     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00574 }
00575 
00576 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00577 {
00578     int i, j;
00579     TM2_INIT_POINTERS_2();
00580 
00581     /* update chroma */
00582     for(j = 0; j < 2; j++){
00583         for(i = 0; i < 2; i++){
00584             U[i] = Uo[i];
00585             V[i] = Vo[i];
00586         }
00587         U += Ustride; V += Vstride;
00588         Uo += oUstride; Vo += oVstride;
00589     }
00590     U -= Ustride * 2;
00591     V -= Vstride * 2;
00592     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00593     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00594 
00595     /* update deltas */
00596     ctx->D[0] = Yo[3] - last[3];
00597     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00598     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00599     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00600 
00601     for(j = 0; j < 4; j++){
00602         for(i = 0; i < 4; i++){
00603             Y[i] = Yo[i];
00604             last[i] = Yo[i];
00605         }
00606         Y += Ystride;
00607         Yo += oYstride;
00608     }
00609 }
00610 
00611 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00612 {
00613     int i, j;
00614     int d;
00615     TM2_INIT_POINTERS_2();
00616 
00617     /* update chroma */
00618     for(j = 0; j < 2; j++){
00619         for(i = 0; i < 2; i++){
00620             U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
00621             V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
00622         }
00623         U += Ustride; V += Vstride;
00624         Uo += oUstride; Vo += oVstride;
00625     }
00626     U -= Ustride * 2;
00627     V -= Vstride * 2;
00628     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00629     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00630 
00631     /* update deltas */
00632     ctx->D[0] = Yo[3] - last[3];
00633     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00634     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00635     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00636 
00637     for(j = 0; j < 4; j++){
00638         d = last[3];
00639         for(i = 0; i < 4; i++){
00640             Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
00641             last[i] = Y[i];
00642         }
00643         ctx->D[j] = last[3] - d;
00644         Y += Ystride;
00645         Yo += oYstride;
00646     }
00647 }
00648 
00649 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00650 {
00651     int i, j;
00652     int mx, my;
00653     TM2_INIT_POINTERS_2();
00654 
00655     mx = GET_TOK(ctx, TM2_MOT);
00656     my = GET_TOK(ctx, TM2_MOT);
00657     mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
00658     my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
00659 
00660     Yo += my * oYstride + mx;
00661     Uo += (my >> 1) * oUstride + (mx >> 1);
00662     Vo += (my >> 1) * oVstride + (mx >> 1);
00663 
00664     /* copy chroma */
00665     for(j = 0; j < 2; j++){
00666         for(i = 0; i < 2; i++){
00667             U[i] = Uo[i];
00668             V[i] = Vo[i];
00669         }
00670         U += Ustride; V += Vstride;
00671         Uo += oUstride; Vo += oVstride;
00672     }
00673     U -= Ustride * 2;
00674     V -= Vstride * 2;
00675     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00676     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00677 
00678     /* copy luma */
00679     for(j = 0; j < 4; j++){
00680         for(i = 0; i < 4; i++){
00681             Y[i] = Yo[i];
00682         }
00683         Y += Ystride;
00684         Yo += oYstride;
00685     }
00686     /* calculate deltas */
00687     Y -= Ystride * 4;
00688     ctx->D[0] = Y[3] - last[3];
00689     ctx->D[1] = Y[3 + Ystride] - Y[3];
00690     ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
00691     ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
00692     for(i = 0; i < 4; i++)
00693         last[i] = Y[i + Ystride * 3];
00694 }
00695 
00696 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
00697 {
00698     int i, j;
00699     int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
00700     int type;
00701     int keyframe = 1;
00702     int *Y, *U, *V;
00703     uint8_t *dst;
00704 
00705     for(i = 0; i < TM2_NUM_STREAMS; i++)
00706         ctx->tok_ptrs[i] = 0;
00707 
00708     if (ctx->tok_lens[TM2_TYPE]<bw*bh){
00709         av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
00710         return -1;
00711     }
00712 
00713     memset(ctx->last, 0, 4 * bw * sizeof(int));
00714     memset(ctx->clast, 0, 4 * bw * sizeof(int));
00715 
00716     for(j = 0; j < bh; j++) {
00717         memset(ctx->D, 0, 4 * sizeof(int));
00718         memset(ctx->CD, 0, 4 * sizeof(int));
00719         for(i = 0; i < bw; i++) {
00720             type = GET_TOK(ctx, TM2_TYPE);
00721             switch(type) {
00722             case TM2_HI_RES:
00723                 tm2_hi_res_block(ctx, p, i, j);
00724                 break;
00725             case TM2_MED_RES:
00726                 tm2_med_res_block(ctx, p, i, j);
00727                 break;
00728             case TM2_LOW_RES:
00729                 tm2_low_res_block(ctx, p, i, j);
00730                 break;
00731             case TM2_NULL_RES:
00732                 tm2_null_res_block(ctx, p, i, j);
00733                 break;
00734             case TM2_UPDATE:
00735                 tm2_update_block(ctx, p, i, j);
00736                 keyframe = 0;
00737                 break;
00738             case TM2_STILL:
00739                 tm2_still_block(ctx, p, i, j);
00740                 keyframe = 0;
00741                 break;
00742             case TM2_MOTION:
00743                 tm2_motion_block(ctx, p, i, j);
00744                 keyframe = 0;
00745                 break;
00746             default:
00747                 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
00748             }
00749         }
00750     }
00751 
00752     /* copy data from our buffer to AVFrame */
00753     Y = (ctx->cur?ctx->Y2:ctx->Y1);
00754     U = (ctx->cur?ctx->U2:ctx->U1);
00755     V = (ctx->cur?ctx->V2:ctx->V1);
00756     dst = p->data[0];
00757     for(j = 0; j < h; j++){
00758         for(i = 0; i < w; i++){
00759             int y = Y[i], u = U[i >> 1], v = V[i >> 1];
00760             dst[3*i+0] = av_clip_uint8(y + v);
00761             dst[3*i+1] = av_clip_uint8(y);
00762             dst[3*i+2] = av_clip_uint8(y + u);
00763         }
00764 
00765         /* horizontal edge extension */
00766         Y[-4]    = Y[-3]    = Y[-2]    = Y[-1] = Y[0];
00767         Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w]  = Y[w - 1];
00768 
00769         /* vertical edge extension */
00770         if (j == 0) {
00771             memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
00772             memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
00773             memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
00774             memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
00775         } else if (j == h - 1) {
00776             memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
00777             memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
00778             memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
00779             memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
00780         }
00781 
00782         Y += ctx->y_stride;
00783         if (j & 1) {
00784             /* horizontal edge extension */
00785             U[-2]     = U[-1] = U[0];
00786             V[-2]     = V[-1] = V[0];
00787             U[cw + 1] = U[cw] = U[cw - 1];
00788             V[cw + 1] = V[cw] = V[cw - 1];
00789 
00790             /* vertical edge extension */
00791             if (j == 1) {
00792                 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
00793                 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
00794                 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
00795                 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
00796             } else if (j == h - 1) {
00797                 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
00798                 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
00799                 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
00800                 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
00801             }
00802 
00803             U += ctx->uv_stride;
00804             V += ctx->uv_stride;
00805         }
00806         dst += p->linesize[0];
00807     }
00808 
00809     return keyframe;
00810 }
00811 
00812 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
00813     TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
00814 };
00815 
00816 static int decode_frame(AVCodecContext *avctx,
00817                         void *data, int *data_size,
00818                         AVPacket *avpkt)
00819 {
00820     const uint8_t *buf = avpkt->data;
00821     int buf_size = avpkt->size & ~3;
00822     TM2Context * const l = avctx->priv_data;
00823     AVFrame * const p= (AVFrame*)&l->pic;
00824     int i, skip, t;
00825 
00826     av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
00827     if(!l->buffer){
00828         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00829         return -1;
00830     }
00831     p->reference = 3;
00832     p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00833     if(avctx->reget_buffer(avctx, p) < 0){
00834         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00835         return -1;
00836     }
00837 
00838     l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2);
00839     skip = tm2_read_header(l, l->buffer);
00840 
00841     if(skip == -1){
00842         return -1;
00843     }
00844 
00845     for(i = 0; i < TM2_NUM_STREAMS; i++){
00846         if (skip >= buf_size) {
00847             return AVERROR_INVALIDDATA;
00848         }
00849         t = tm2_read_stream(l, l->buffer + skip, tm2_stream_order[i], buf_size - skip);
00850         if(t < 0){
00851             return t;
00852         }
00853         skip += t;
00854     }
00855     p->key_frame = tm2_decode_blocks(l, p);
00856     if(p->key_frame)
00857         p->pict_type = AV_PICTURE_TYPE_I;
00858     else
00859         p->pict_type = AV_PICTURE_TYPE_P;
00860 
00861     l->cur = !l->cur;
00862     *data_size = sizeof(AVFrame);
00863     *(AVFrame*)data = l->pic;
00864 
00865     return buf_size;
00866 }
00867 
00868 static av_cold int decode_init(AVCodecContext *avctx){
00869     TM2Context * const l = avctx->priv_data;
00870     int i, w = avctx->width, h = avctx->height;
00871 
00872     if((avctx->width & 3) || (avctx->height & 3)){
00873         av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
00874         return -1;
00875     }
00876 
00877     l->avctx = avctx;
00878     l->pic.data[0]=NULL;
00879     avctx->pix_fmt = PIX_FMT_BGR24;
00880     avcodec_get_frame_defaults(&l->pic);
00881 
00882     dsputil_init(&l->dsp, avctx);
00883 
00884     l->last  = av_malloc(4 * sizeof(*l->last)  * (w >> 2));
00885     l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
00886 
00887     for(i = 0; i < TM2_NUM_STREAMS; i++) {
00888         l->tokens[i] = NULL;
00889         l->tok_lens[i] = 0;
00890     }
00891 
00892     w += 8;
00893     h += 8;
00894     l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
00895     l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
00896     l->y_stride = w;
00897     w = (w + 1) >> 1;
00898     h = (h + 1) >> 1;
00899     l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
00900     l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
00901     l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
00902     l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
00903     l->uv_stride = w;
00904     l->cur = 0;
00905     if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
00906         !l->V1_base || !l->U2_base || !l->V2_base ||
00907         !l->last    || !l->clast) {
00908         av_freep(l->Y1_base);
00909         av_freep(l->Y2_base);
00910         av_freep(l->U1_base);
00911         av_freep(l->U2_base);
00912         av_freep(l->V1_base);
00913         av_freep(l->V2_base);
00914         av_freep(l->last);
00915         av_freep(l->clast);
00916         return AVERROR(ENOMEM);
00917     }
00918     l->Y1 = l->Y1_base + l->y_stride  * 4 + 4;
00919     l->Y2 = l->Y2_base + l->y_stride  * 4 + 4;
00920     l->U1 = l->U1_base + l->uv_stride * 2 + 2;
00921     l->U2 = l->U2_base + l->uv_stride * 2 + 2;
00922     l->V1 = l->V1_base + l->uv_stride * 2 + 2;
00923     l->V2 = l->V2_base + l->uv_stride * 2 + 2;
00924 
00925     return 0;
00926 }
00927 
00928 static av_cold int decode_end(AVCodecContext *avctx){
00929     TM2Context * const l = avctx->priv_data;
00930     AVFrame *pic = &l->pic;
00931     int i;
00932 
00933     av_free(l->last);
00934     av_free(l->clast);
00935     for(i = 0; i < TM2_NUM_STREAMS; i++)
00936         av_free(l->tokens[i]);
00937     if(l->Y1){
00938         av_free(l->Y1_base);
00939         av_free(l->U1_base);
00940         av_free(l->V1_base);
00941         av_free(l->Y2_base);
00942         av_free(l->U2_base);
00943         av_free(l->V2_base);
00944     }
00945     av_freep(&l->buffer);
00946     l->buffer_size = 0;
00947 
00948     if (pic->data[0])
00949         avctx->release_buffer(avctx, pic);
00950 
00951     return 0;
00952 }
00953 
00954 AVCodec ff_truemotion2_decoder = {
00955     .name           = "truemotion2",
00956     .type           = AVMEDIA_TYPE_VIDEO,
00957     .id             = CODEC_ID_TRUEMOTION2,
00958     .priv_data_size = sizeof(TM2Context),
00959     .init           = decode_init,
00960     .close          = decode_end,
00961     .decode         = decode_frame,
00962     .capabilities   = CODEC_CAP_DR1,
00963     .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
00964 };