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