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