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

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 Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
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     /* TM2 streams */
00049     int *tokens[TM2_NUM_STREAMS];
00050     int tok_lens[TM2_NUM_STREAMS];
00051     int tok_ptrs[TM2_NUM_STREAMS];
00052     int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
00053     /* for blocks decoding */
00054     int D[4];
00055     int CD[4];
00056     int *last;
00057     int *clast;
00058 
00059     /* data for current and previous frame */
00060     int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
00061     int *Y1, *U1, *V1, *Y2, *U2, *V2;
00062     int y_stride, uv_stride;
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 > 25)) {
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         ff_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 skip = 0;
00255     int len, toks, pos;
00256     TM2Codes codes;
00257     GetByteContext gb;
00258 
00259     /* get stream length in dwords */
00260     bytestream2_init(&gb, buf, buf_size);
00261     len  = bytestream2_get_be32(&gb);
00262     skip = len * 4 + 4;
00263 
00264     if(len == 0)
00265         return 4;
00266 
00267     if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
00268         av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
00269         return -1;
00270     }
00271 
00272     toks = bytestream2_get_be32(&gb);
00273     if(toks & 1) {
00274         len = bytestream2_get_be32(&gb);
00275         if(len == TM2_ESCAPE) {
00276             len = bytestream2_get_be32(&gb);
00277         }
00278         if(len > 0) {
00279             pos = bytestream2_tell(&gb);
00280             if (skip <= pos)
00281                 return -1;
00282             init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00283             if(tm2_read_deltas(ctx, stream_id) == -1)
00284                 return -1;
00285             bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
00286         }
00287     }
00288     /* skip unused fields */
00289     len = bytestream2_get_be32(&gb);
00290     if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
00291         bytestream2_skip(&gb, 8); /* unused by decoder */
00292     } else {
00293         bytestream2_skip(&gb, 4); /* unused by decoder */
00294     }
00295 
00296     pos = bytestream2_tell(&gb);
00297     if (skip <= pos)
00298         return -1;
00299     init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00300     if(tm2_build_huff_table(ctx, &codes) == -1)
00301         return -1;
00302     bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
00303 
00304     toks >>= 1;
00305     /* check if we have sane number of tokens */
00306     if((toks < 0) || (toks > 0xFFFFFF)){
00307         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00308         tm2_free_codes(&codes);
00309         return -1;
00310     }
00311     ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
00312     ctx->tok_lens[stream_id] = toks;
00313     len = bytestream2_get_be32(&gb);
00314     if(len > 0) {
00315         pos = bytestream2_tell(&gb);
00316         if (skip <= pos)
00317             return -1;
00318         init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
00319         for(i = 0; i < toks; i++) {
00320             if (get_bits_left(&ctx->gb) <= 0) {
00321                 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00322                 return -1;
00323             }
00324             ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
00325             if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
00326                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
00327                        ctx->tokens[stream_id][i], stream_id, i);
00328                 return AVERROR_INVALIDDATA;
00329             }
00330         }
00331     } else {
00332         for(i = 0; i < toks; i++) {
00333             ctx->tokens[stream_id][i] = codes.recode[0];
00334             if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
00335                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
00336                        ctx->tokens[stream_id][i], stream_id, i);
00337                 return AVERROR_INVALIDDATA;
00338             }
00339         }
00340     }
00341     tm2_free_codes(&codes);
00342 
00343     return skip;
00344 }
00345 
00346 static inline int GET_TOK(TM2Context *ctx,int type) {
00347     if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
00348         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]);
00349         return 0;
00350     }
00351     if(type <= TM2_MOT)
00352         return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
00353     return ctx->tokens[type][ctx->tok_ptrs[type]++];
00354 }
00355 
00356 /* blocks decoding routines */
00357 
00358 /* common Y, U, V pointers initialisation */
00359 #define TM2_INIT_POINTERS() \
00360     int *last, *clast; \
00361     int *Y, *U, *V;\
00362     int Ystride, Ustride, Vstride;\
00363 \
00364     Ystride = ctx->y_stride;\
00365     Vstride = ctx->uv_stride;\
00366     Ustride = ctx->uv_stride;\
00367     Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
00368     V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
00369     U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
00370     last = ctx->last + bx * 4;\
00371     clast = ctx->clast + bx * 4;
00372 
00373 #define TM2_INIT_POINTERS_2() \
00374     int *Yo, *Uo, *Vo;\
00375     int oYstride, oUstride, oVstride;\
00376 \
00377     TM2_INIT_POINTERS();\
00378     oYstride = Ystride;\
00379     oVstride = Vstride;\
00380     oUstride = Ustride;\
00381     Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
00382     Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
00383     Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
00384 
00385 /* recalculate last and delta values for next blocks */
00386 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
00387     CD[0] = CHR[1] - last[1];\
00388     CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
00389     last[0] = (int)CHR[stride + 0];\
00390     last[1] = (int)CHR[stride + 1];}
00391 
00392 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
00393 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
00394 {
00395     int ct, d;
00396     int i, j;
00397 
00398     for(j = 0; j < 4; j++){
00399         ct = ctx->D[j];
00400         for(i = 0; i < 4; i++){
00401             d = deltas[i + j * 4];
00402             ct += d;
00403             last[i] += ct;
00404             Y[i] = av_clip_uint8(last[i]);
00405         }
00406         Y += stride;
00407         ctx->D[j] = ct;
00408     }
00409 }
00410 
00411 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
00412 {
00413     int i, j;
00414     for(j = 0; j < 2; j++){
00415         for(i = 0; i < 2; i++){
00416             CD[j] += deltas[i + j * 2];
00417             last[i] += CD[j];
00418             data[i] = last[i];
00419         }
00420         data += stride;
00421     }
00422 }
00423 
00424 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
00425 {
00426     int t;
00427     int l;
00428     int prev;
00429 
00430     if(bx > 0)
00431         prev = clast[-3];
00432     else
00433         prev = 0;
00434     t = (CD[0] + CD[1]) >> 1;
00435     l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
00436     CD[1] = CD[0] + CD[1] - t;
00437     CD[0] = t;
00438     clast[0] = l;
00439 
00440     tm2_high_chroma(data, stride, clast, CD, deltas);
00441 }
00442 
00443 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00444 {
00445     int i;
00446     int deltas[16];
00447     TM2_INIT_POINTERS();
00448 
00449     /* hi-res chroma */
00450     for(i = 0; i < 4; i++) {
00451         deltas[i] = GET_TOK(ctx, TM2_C_HI);
00452         deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
00453     }
00454     tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
00455     tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
00456 
00457     /* hi-res luma */
00458     for(i = 0; i < 16; i++)
00459         deltas[i] = GET_TOK(ctx, TM2_L_HI);
00460 
00461     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00462 }
00463 
00464 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00465 {
00466     int i;
00467     int deltas[16];
00468     TM2_INIT_POINTERS();
00469 
00470     /* low-res chroma */
00471     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00472     deltas[1] = deltas[2] = deltas[3] = 0;
00473     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00474 
00475     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00476     deltas[1] = deltas[2] = deltas[3] = 0;
00477     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00478 
00479     /* hi-res luma */
00480     for(i = 0; i < 16; i++)
00481         deltas[i] = GET_TOK(ctx, TM2_L_HI);
00482 
00483     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00484 }
00485 
00486 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00487 {
00488     int i;
00489     int t1, t2;
00490     int deltas[16];
00491     TM2_INIT_POINTERS();
00492 
00493     /* low-res chroma */
00494     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00495     deltas[1] = deltas[2] = deltas[3] = 0;
00496     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00497 
00498     deltas[0] = GET_TOK(ctx, TM2_C_LO);
00499     deltas[1] = deltas[2] = deltas[3] = 0;
00500     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00501 
00502     /* low-res luma */
00503     for(i = 0; i < 16; i++)
00504         deltas[i] = 0;
00505 
00506     deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
00507     deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
00508     deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
00509     deltas[10] = GET_TOK(ctx, TM2_L_LO);
00510 
00511     if(bx > 0)
00512         last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
00513     else
00514         last[0] = (last[1]  - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
00515     last[2] = (last[1] + last[3]) >> 1;
00516 
00517     t1 = ctx->D[0] + ctx->D[1];
00518     ctx->D[0] = t1 >> 1;
00519     ctx->D[1] = t1 - (t1 >> 1);
00520     t2 = ctx->D[2] + ctx->D[3];
00521     ctx->D[2] = t2 >> 1;
00522     ctx->D[3] = t2 - (t2 >> 1);
00523 
00524     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00525 }
00526 
00527 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00528 {
00529     int i;
00530     int ct;
00531     int left, right, diff;
00532     int deltas[16];
00533     TM2_INIT_POINTERS();
00534 
00535     /* null chroma */
00536     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00537     tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00538 
00539     deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00540     tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00541 
00542     /* null luma */
00543     for(i = 0; i < 16; i++)
00544         deltas[i] = 0;
00545 
00546     ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
00547 
00548     if(bx > 0)
00549         left = last[-1] - ct;
00550     else
00551         left = 0;
00552 
00553     right = last[3];
00554     diff = right - left;
00555     last[0] = left + (diff >> 2);
00556     last[1] = left + (diff >> 1);
00557     last[2] = right - (diff >> 2);
00558     last[3] = right;
00559     {
00560         int tp = left;
00561 
00562         ctx->D[0] = (tp + (ct >> 2)) - left;
00563         left += ctx->D[0];
00564         ctx->D[1] = (tp + (ct >> 1)) - left;
00565         left += ctx->D[1];
00566         ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
00567         left += ctx->D[2];
00568         ctx->D[3] = (tp + ct) - left;
00569     }
00570     tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00571 }
00572 
00573 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00574 {
00575     int i, j;
00576     TM2_INIT_POINTERS_2();
00577 
00578     /* update chroma */
00579     for(j = 0; j < 2; j++){
00580         for(i = 0; i < 2; i++){
00581             U[i] = Uo[i];
00582             V[i] = Vo[i];
00583         }
00584         U += Ustride; V += Vstride;
00585         Uo += oUstride; Vo += oVstride;
00586     }
00587     U -= Ustride * 2;
00588     V -= Vstride * 2;
00589     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00590     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00591 
00592     /* update deltas */
00593     ctx->D[0] = Yo[3] - last[3];
00594     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00595     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00596     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00597 
00598     for(j = 0; j < 4; j++){
00599         for(i = 0; i < 4; i++){
00600             Y[i] = Yo[i];
00601             last[i] = Yo[i];
00602         }
00603         Y += Ystride;
00604         Yo += oYstride;
00605     }
00606 }
00607 
00608 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00609 {
00610     int i, j;
00611     int d;
00612     TM2_INIT_POINTERS_2();
00613 
00614     /* update chroma */
00615     for(j = 0; j < 2; j++){
00616         for(i = 0; i < 2; i++){
00617             U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
00618             V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
00619         }
00620         U += Ustride; V += Vstride;
00621         Uo += oUstride; Vo += oVstride;
00622     }
00623     U -= Ustride * 2;
00624     V -= Vstride * 2;
00625     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00626     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00627 
00628     /* update deltas */
00629     ctx->D[0] = Yo[3] - last[3];
00630     ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00631     ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00632     ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00633 
00634     for(j = 0; j < 4; j++){
00635         d = last[3];
00636         for(i = 0; i < 4; i++){
00637             Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
00638             last[i] = Y[i];
00639         }
00640         ctx->D[j] = last[3] - d;
00641         Y += Ystride;
00642         Yo += oYstride;
00643     }
00644 }
00645 
00646 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00647 {
00648     int i, j;
00649     int mx, my;
00650     TM2_INIT_POINTERS_2();
00651 
00652     mx = GET_TOK(ctx, TM2_MOT);
00653     my = GET_TOK(ctx, TM2_MOT);
00654     mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width  - bx * 4);
00655     my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
00656 
00657     Yo += my * oYstride + mx;
00658     Uo += (my >> 1) * oUstride + (mx >> 1);
00659     Vo += (my >> 1) * oVstride + (mx >> 1);
00660 
00661     /* copy chroma */
00662     for(j = 0; j < 2; j++){
00663         for(i = 0; i < 2; i++){
00664             U[i] = Uo[i];
00665             V[i] = Vo[i];
00666         }
00667         U += Ustride; V += Vstride;
00668         Uo += oUstride; Vo += oVstride;
00669     }
00670     U -= Ustride * 2;
00671     V -= Vstride * 2;
00672     TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00673     TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00674 
00675     /* copy luma */
00676     for(j = 0; j < 4; j++){
00677         for(i = 0; i < 4; i++){
00678             Y[i] = Yo[i];
00679         }
00680         Y += Ystride;
00681         Yo += oYstride;
00682     }
00683     /* calculate deltas */
00684     Y -= Ystride * 4;
00685     ctx->D[0] = Y[3] - last[3];
00686     ctx->D[1] = Y[3 + Ystride] - Y[3];
00687     ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
00688     ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
00689     for(i = 0; i < 4; i++)
00690         last[i] = Y[i + Ystride * 3];
00691 }
00692 
00693 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
00694 {
00695     int i, j;
00696     int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
00697     int type;
00698     int keyframe = 1;
00699     int *Y, *U, *V;
00700     uint8_t *dst;
00701 
00702     for(i = 0; i < TM2_NUM_STREAMS; i++)
00703         ctx->tok_ptrs[i] = 0;
00704 
00705     if (ctx->tok_lens[TM2_TYPE]<bw*bh){
00706         av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
00707         return -1;
00708     }
00709 
00710     memset(ctx->last, 0, 4 * bw * sizeof(int));
00711     memset(ctx->clast, 0, 4 * bw * sizeof(int));
00712 
00713     for(j = 0; j < bh; j++) {
00714         memset(ctx->D, 0, 4 * sizeof(int));
00715         memset(ctx->CD, 0, 4 * sizeof(int));
00716         for(i = 0; i < bw; i++) {
00717             type = GET_TOK(ctx, TM2_TYPE);
00718             switch(type) {
00719             case TM2_HI_RES:
00720                 tm2_hi_res_block(ctx, p, i, j);
00721                 break;
00722             case TM2_MED_RES:
00723                 tm2_med_res_block(ctx, p, i, j);
00724                 break;
00725             case TM2_LOW_RES:
00726                 tm2_low_res_block(ctx, p, i, j);
00727                 break;
00728             case TM2_NULL_RES:
00729                 tm2_null_res_block(ctx, p, i, j);
00730                 break;
00731             case TM2_UPDATE:
00732                 tm2_update_block(ctx, p, i, j);
00733                 keyframe = 0;
00734                 break;
00735             case TM2_STILL:
00736                 tm2_still_block(ctx, p, i, j);
00737                 keyframe = 0;
00738                 break;
00739             case TM2_MOTION:
00740                 tm2_motion_block(ctx, p, i, j);
00741                 keyframe = 0;
00742                 break;
00743             default:
00744                 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
00745             }
00746         }
00747     }
00748 
00749     /* copy data from our buffer to AVFrame */
00750     Y = (ctx->cur?ctx->Y2:ctx->Y1);
00751     U = (ctx->cur?ctx->U2:ctx->U1);
00752     V = (ctx->cur?ctx->V2:ctx->V1);
00753     dst = p->data[0];
00754     for(j = 0; j < h; j++){
00755         for(i = 0; i < w; i++){
00756             int y = Y[i], u = U[i >> 1], v = V[i >> 1];
00757             dst[3*i+0] = av_clip_uint8(y + v);
00758             dst[3*i+1] = av_clip_uint8(y);
00759             dst[3*i+2] = av_clip_uint8(y + u);
00760         }
00761 
00762         /* horizontal edge extension */
00763         Y[-4]    = Y[-3]    = Y[-2]    = Y[-1] = Y[0];
00764         Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w]  = Y[w - 1];
00765 
00766         /* vertical edge extension */
00767         if (j == 0) {
00768             memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
00769             memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
00770             memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
00771             memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
00772         } else if (j == h - 1) {
00773             memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
00774             memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
00775             memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
00776             memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
00777         }
00778 
00779         Y += ctx->y_stride;
00780         if (j & 1) {
00781             /* horizontal edge extension */
00782             U[-2]     = U[-1] = U[0];
00783             V[-2]     = V[-1] = V[0];
00784             U[cw + 1] = U[cw] = U[cw - 1];
00785             V[cw + 1] = V[cw] = V[cw - 1];
00786 
00787             /* vertical edge extension */
00788             if (j == 1) {
00789                 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
00790                 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
00791                 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
00792                 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
00793             } else if (j == h - 1) {
00794                 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
00795                 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
00796                 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
00797                 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
00798             }
00799 
00800             U += ctx->uv_stride;
00801             V += ctx->uv_stride;
00802         }
00803         dst += p->linesize[0];
00804     }
00805 
00806     return keyframe;
00807 }
00808 
00809 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
00810     TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
00811 };
00812 
00813 static int decode_frame(AVCodecContext *avctx,
00814                         void *data, int *data_size,
00815                         AVPacket *avpkt)
00816 {
00817     const uint8_t *buf = avpkt->data;
00818     int buf_size = avpkt->size & ~3;
00819     TM2Context * const l = avctx->priv_data;
00820     AVFrame * const p= (AVFrame*)&l->pic;
00821     int i, skip, t;
00822     uint8_t *swbuf;
00823 
00824     swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00825     if(!swbuf){
00826         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00827         return -1;
00828     }
00829     p->reference = 1;
00830     p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00831     if(avctx->reget_buffer(avctx, p) < 0){
00832         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00833         av_free(swbuf);
00834         return -1;
00835     }
00836 
00837     l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
00838     skip = tm2_read_header(l, swbuf);
00839 
00840     if(skip == -1){
00841         av_free(swbuf);
00842         return -1;
00843     }
00844 
00845     for(i = 0; i < TM2_NUM_STREAMS; i++){
00846         if (skip >= buf_size) {
00847             av_free(swbuf);
00848             return AVERROR_INVALIDDATA;
00849         }
00850         t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size - skip);
00851         if(t < 0){
00852             av_free(swbuf);
00853             return t;
00854         }
00855         skip += t;
00856     }
00857     p->key_frame = tm2_decode_blocks(l, p);
00858     if(p->key_frame)
00859         p->pict_type = AV_PICTURE_TYPE_I;
00860     else
00861         p->pict_type = AV_PICTURE_TYPE_P;
00862 
00863     l->cur = !l->cur;
00864     *data_size = sizeof(AVFrame);
00865     *(AVFrame*)data = l->pic;
00866     av_free(swbuf);
00867 
00868     return buf_size;
00869 }
00870 
00871 static av_cold int decode_init(AVCodecContext *avctx){
00872     TM2Context * const l = avctx->priv_data;
00873     int i, w = avctx->width, h = avctx->height;
00874 
00875     if((avctx->width & 3) || (avctx->height & 3)){
00876         av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
00877         return -1;
00878     }
00879 
00880     l->avctx = avctx;
00881     l->pic.data[0]=NULL;
00882     avctx->pix_fmt = PIX_FMT_BGR24;
00883 
00884     dsputil_init(&l->dsp, avctx);
00885 
00886     l->last  = av_malloc(4 * sizeof(*l->last)  * (w >> 2));
00887     l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
00888 
00889     for(i = 0; i < TM2_NUM_STREAMS; i++) {
00890         l->tokens[i] = NULL;
00891         l->tok_lens[i] = 0;
00892     }
00893 
00894     w += 8;
00895     h += 8;
00896     l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
00897     l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
00898     l->y_stride = w;
00899     w = (w + 1) >> 1;
00900     h = (h + 1) >> 1;
00901     l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
00902     l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
00903     l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
00904     l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
00905     l->uv_stride = w;
00906     l->cur = 0;
00907     if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
00908         !l->V1_base || !l->U2_base || !l->V2_base ||
00909         !l->last    || !l->clast) {
00910         av_freep(l->Y1_base);
00911         av_freep(l->Y2_base);
00912         av_freep(l->U1_base);
00913         av_freep(l->U2_base);
00914         av_freep(l->V1_base);
00915         av_freep(l->V2_base);
00916         av_freep(l->last);
00917         av_freep(l->clast);
00918         return AVERROR(ENOMEM);
00919     }
00920     l->Y1 = l->Y1_base + l->y_stride  * 4 + 4;
00921     l->Y2 = l->Y2_base + l->y_stride  * 4 + 4;
00922     l->U1 = l->U1_base + l->uv_stride * 2 + 2;
00923     l->U2 = l->U2_base + l->uv_stride * 2 + 2;
00924     l->V1 = l->V1_base + l->uv_stride * 2 + 2;
00925     l->V2 = l->V2_base + l->uv_stride * 2 + 2;
00926 
00927     return 0;
00928 }
00929 
00930 static av_cold int decode_end(AVCodecContext *avctx){
00931     TM2Context * const l = avctx->priv_data;
00932     AVFrame *pic = &l->pic;
00933     int i;
00934 
00935     av_free(l->last);
00936     av_free(l->clast);
00937     for(i = 0; i < TM2_NUM_STREAMS; i++)
00938         av_free(l->tokens[i]);
00939     if(l->Y1){
00940         av_free(l->Y1_base);
00941         av_free(l->U1_base);
00942         av_free(l->V1_base);
00943         av_free(l->Y2_base);
00944         av_free(l->U2_base);
00945         av_free(l->V2_base);
00946     }
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 };
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1