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 int length;
00205
00206 obuf = buf;
00207
00208 magic = AV_RL32(buf);
00209 buf += 4;
00210
00211 if(magic == 0x00000100) {
00212
00213 return 40;
00214 } else if(magic == 0x00000101) {
00215 int w, h, size, flags, xr, yr;
00216
00217 length = AV_RL32(buf);
00218 buf += 4;
00219
00220 init_get_bits(&ctx->gb, buf, 32 * 8);
00221 size = get_bits_long(&ctx->gb, 31);
00222 h = get_bits(&ctx->gb, 15);
00223 w = get_bits(&ctx->gb, 15);
00224 flags = get_bits_long(&ctx->gb, 31);
00225 yr = get_bits(&ctx->gb, 9);
00226 xr = get_bits(&ctx->gb, 9);
00227
00228 return 40;
00229 } else {
00230 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
00231 return -1;
00232 }
00233
00234 return buf - obuf;
00235 }
00236
00237 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
00238 int d, mb;
00239 int i, v;
00240
00241 d = get_bits(&ctx->gb, 9);
00242 mb = get_bits(&ctx->gb, 5);
00243
00244 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
00245 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
00246 return -1;
00247 }
00248
00249 for(i = 0; i < d; i++) {
00250 v = get_bits_long(&ctx->gb, mb);
00251 if(v & (1 << (mb - 1)))
00252 ctx->deltas[stream_id][i] = v - (1 << mb);
00253 else
00254 ctx->deltas[stream_id][i] = v;
00255 }
00256 for(; i < TM2_DELTAS; i++)
00257 ctx->deltas[stream_id][i] = 0;
00258
00259 return 0;
00260 }
00261
00262 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
00263 {
00264 int i;
00265 int cur = 0;
00266 int skip = 0;
00267 int len, toks;
00268 TM2Codes codes;
00269
00270
00271 len = AV_RB32(buf); buf += 4; cur += 4;
00272 skip = len * 4 + 4;
00273
00274 if(len == 0)
00275 return 4;
00276
00277 if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
00278 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
00279 return -1;
00280 }
00281
00282 toks = AV_RB32(buf); buf += 4; cur += 4;
00283 if(toks & 1) {
00284 len = AV_RB32(buf); buf += 4; cur += 4;
00285 if(len == TM2_ESCAPE) {
00286 len = AV_RB32(buf); buf += 4; cur += 4;
00287 }
00288 if(len > 0) {
00289 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
00290 if(tm2_read_deltas(ctx, stream_id) == -1)
00291 return -1;
00292 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
00293 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
00294 }
00295 }
00296
00297 if(AV_RB32(buf) == TM2_ESCAPE) {
00298 buf += 4; cur += 4;
00299 }
00300 buf += 4; cur += 4;
00301 buf += 4; cur += 4;
00302
00303 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
00304 if(tm2_build_huff_table(ctx, &codes) == -1)
00305 return -1;
00306 buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
00307 cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
00308
00309 toks >>= 1;
00310
00311 if((toks < 0) || (toks > 0xFFFFFF)){
00312 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00313 tm2_free_codes(&codes);
00314 return -1;
00315 }
00316 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
00317 ctx->tok_lens[stream_id] = toks;
00318 len = AV_RB32(buf); buf += 4; cur += 4;
00319 if(len > 0) {
00320 init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
00321 for(i = 0; i < toks; i++) {
00322 if (get_bits_left(&ctx->gb) <= 0) {
00323 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
00324 return -1;
00325 }
00326 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
00327 }
00328 } else {
00329 for(i = 0; i < toks; i++)
00330 ctx->tokens[stream_id][i] = codes.recode[0];
00331 }
00332 tm2_free_codes(&codes);
00333
00334 return skip;
00335 }
00336
00337 static inline int GET_TOK(TM2Context *ctx,int type) {
00338 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
00339 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]);
00340 return 0;
00341 }
00342 if(type <= TM2_MOT)
00343 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
00344 return ctx->tokens[type][ctx->tok_ptrs[type]++];
00345 }
00346
00347
00348
00349
00350 #define TM2_INIT_POINTERS() \
00351 int *last, *clast; \
00352 int *Y, *U, *V;\
00353 int Ystride, Ustride, Vstride;\
00354 \
00355 Ystride = ctx->avctx->width;\
00356 Vstride = (ctx->avctx->width + 1) >> 1;\
00357 Ustride = (ctx->avctx->width + 1) >> 1;\
00358 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
00359 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
00360 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
00361 last = ctx->last + bx * 4;\
00362 clast = ctx->clast + bx * 4;
00363
00364 #define TM2_INIT_POINTERS_2() \
00365 int *Yo, *Uo, *Vo;\
00366 int oYstride, oUstride, oVstride;\
00367 \
00368 TM2_INIT_POINTERS();\
00369 oYstride = Ystride;\
00370 oVstride = Vstride;\
00371 oUstride = Ustride;\
00372 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
00373 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
00374 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
00375
00376
00377 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
00378 CD[0] = CHR[1] - last[1];\
00379 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
00380 last[0] = (int)CHR[stride + 0];\
00381 last[1] = (int)CHR[stride + 1];}
00382
00383
00384 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
00385 {
00386 int ct, d;
00387 int i, j;
00388
00389 for(j = 0; j < 4; j++){
00390 ct = ctx->D[j];
00391 for(i = 0; i < 4; i++){
00392 d = deltas[i + j * 4];
00393 ct += d;
00394 last[i] += ct;
00395 Y[i] = av_clip_uint8(last[i]);
00396 }
00397 Y += stride;
00398 ctx->D[j] = ct;
00399 }
00400 }
00401
00402 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
00403 {
00404 int i, j;
00405 for(j = 0; j < 2; j++){
00406 for(i = 0; i < 2; i++){
00407 CD[j] += deltas[i + j * 2];
00408 last[i] += CD[j];
00409 data[i] = last[i];
00410 }
00411 data += stride;
00412 }
00413 }
00414
00415 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
00416 {
00417 int t;
00418 int l;
00419 int prev;
00420
00421 if(bx > 0)
00422 prev = clast[-3];
00423 else
00424 prev = 0;
00425 t = (CD[0] + CD[1]) >> 1;
00426 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
00427 CD[1] = CD[0] + CD[1] - t;
00428 CD[0] = t;
00429 clast[0] = l;
00430
00431 tm2_high_chroma(data, stride, clast, CD, deltas);
00432 }
00433
00434 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00435 {
00436 int i;
00437 int deltas[16];
00438 TM2_INIT_POINTERS();
00439
00440
00441 for(i = 0; i < 4; i++) {
00442 deltas[i] = GET_TOK(ctx, TM2_C_HI);
00443 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
00444 }
00445 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
00446 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
00447
00448
00449 for(i = 0; i < 16; i++)
00450 deltas[i] = GET_TOK(ctx, TM2_L_HI);
00451
00452 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00453 }
00454
00455 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00456 {
00457 int i;
00458 int deltas[16];
00459 TM2_INIT_POINTERS();
00460
00461
00462 deltas[0] = GET_TOK(ctx, TM2_C_LO);
00463 deltas[1] = deltas[2] = deltas[3] = 0;
00464 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00465
00466 deltas[0] = GET_TOK(ctx, TM2_C_LO);
00467 deltas[1] = deltas[2] = deltas[3] = 0;
00468 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00469
00470
00471 for(i = 0; i < 16; i++)
00472 deltas[i] = GET_TOK(ctx, TM2_L_HI);
00473
00474 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00475 }
00476
00477 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00478 {
00479 int i;
00480 int t1, t2;
00481 int deltas[16];
00482 TM2_INIT_POINTERS();
00483
00484
00485 deltas[0] = GET_TOK(ctx, TM2_C_LO);
00486 deltas[1] = deltas[2] = deltas[3] = 0;
00487 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00488
00489 deltas[0] = GET_TOK(ctx, TM2_C_LO);
00490 deltas[1] = deltas[2] = deltas[3] = 0;
00491 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00492
00493
00494 for(i = 0; i < 16; i++)
00495 deltas[i] = 0;
00496
00497 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
00498 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
00499 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
00500 deltas[10] = GET_TOK(ctx, TM2_L_LO);
00501
00502 if(bx > 0)
00503 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
00504 else
00505 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
00506 last[2] = (last[1] + last[3]) >> 1;
00507
00508 t1 = ctx->D[0] + ctx->D[1];
00509 ctx->D[0] = t1 >> 1;
00510 ctx->D[1] = t1 - (t1 >> 1);
00511 t2 = ctx->D[2] + ctx->D[3];
00512 ctx->D[2] = t2 >> 1;
00513 ctx->D[3] = t2 - (t2 >> 1);
00514
00515 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00516 }
00517
00518 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00519 {
00520 int i;
00521 int ct;
00522 int left, right, diff;
00523 int deltas[16];
00524 TM2_INIT_POINTERS();
00525
00526
00527 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00528 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
00529
00530 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
00531 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
00532
00533
00534 for(i = 0; i < 16; i++)
00535 deltas[i] = 0;
00536
00537 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
00538
00539 if(bx > 0)
00540 left = last[-1] - ct;
00541 else
00542 left = 0;
00543
00544 right = last[3];
00545 diff = right - left;
00546 last[0] = left + (diff >> 2);
00547 last[1] = left + (diff >> 1);
00548 last[2] = right - (diff >> 2);
00549 last[3] = right;
00550 {
00551 int tp = left;
00552
00553 ctx->D[0] = (tp + (ct >> 2)) - left;
00554 left += ctx->D[0];
00555 ctx->D[1] = (tp + (ct >> 1)) - left;
00556 left += ctx->D[1];
00557 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
00558 left += ctx->D[2];
00559 ctx->D[3] = (tp + ct) - left;
00560 }
00561 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
00562 }
00563
00564 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00565 {
00566 int i, j;
00567 TM2_INIT_POINTERS_2();
00568
00569
00570 for(j = 0; j < 2; j++){
00571 for(i = 0; i < 2; i++){
00572 U[i] = Uo[i];
00573 V[i] = Vo[i];
00574 }
00575 U += Ustride; V += Vstride;
00576 Uo += oUstride; Vo += oVstride;
00577 }
00578 U -= Ustride * 2;
00579 V -= Vstride * 2;
00580 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00581 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00582
00583
00584 ctx->D[0] = Yo[3] - last[3];
00585 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00586 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00587 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00588
00589 for(j = 0; j < 4; j++){
00590 for(i = 0; i < 4; i++){
00591 Y[i] = Yo[i];
00592 last[i] = Yo[i];
00593 }
00594 Y += Ystride;
00595 Yo += oYstride;
00596 }
00597 }
00598
00599 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00600 {
00601 int i, j;
00602 int d;
00603 TM2_INIT_POINTERS_2();
00604
00605
00606 for(j = 0; j < 2; j++){
00607 for(i = 0; i < 2; i++){
00608 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
00609 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
00610 }
00611 U += Ustride; V += Vstride;
00612 Uo += oUstride; Vo += oVstride;
00613 }
00614 U -= Ustride * 2;
00615 V -= Vstride * 2;
00616 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00617 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00618
00619
00620 ctx->D[0] = Yo[3] - last[3];
00621 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
00622 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
00623 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
00624
00625 for(j = 0; j < 4; j++){
00626 d = last[3];
00627 for(i = 0; i < 4; i++){
00628 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
00629 last[i] = Y[i];
00630 }
00631 ctx->D[j] = last[3] - d;
00632 Y += Ystride;
00633 Yo += oYstride;
00634 }
00635 }
00636
00637 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
00638 {
00639 int i, j;
00640 int mx, my;
00641 TM2_INIT_POINTERS_2();
00642
00643 mx = GET_TOK(ctx, TM2_MOT);
00644 my = GET_TOK(ctx, TM2_MOT);
00645
00646 Yo += my * oYstride + mx;
00647 Uo += (my >> 1) * oUstride + (mx >> 1);
00648 Vo += (my >> 1) * oVstride + (mx >> 1);
00649
00650
00651 for(j = 0; j < 2; j++){
00652 for(i = 0; i < 2; i++){
00653 U[i] = Uo[i];
00654 V[i] = Vo[i];
00655 }
00656 U += Ustride; V += Vstride;
00657 Uo += oUstride; Vo += oVstride;
00658 }
00659 U -= Ustride * 2;
00660 V -= Vstride * 2;
00661 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
00662 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
00663
00664
00665 for(j = 0; j < 4; j++){
00666 for(i = 0; i < 4; i++){
00667 Y[i] = Yo[i];
00668 }
00669 Y += Ystride;
00670 Yo += oYstride;
00671 }
00672
00673 Y -= Ystride * 4;
00674 ctx->D[0] = Y[3] - last[3];
00675 ctx->D[1] = Y[3 + Ystride] - Y[3];
00676 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
00677 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
00678 for(i = 0; i < 4; i++)
00679 last[i] = Y[i + Ystride * 3];
00680 }
00681
00682 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
00683 {
00684 int i, j;
00685 int bw, bh;
00686 int type;
00687 int keyframe = 1;
00688 int *Y, *U, *V;
00689 uint8_t *dst;
00690
00691 bw = ctx->avctx->width >> 2;
00692 bh = ctx->avctx->height >> 2;
00693
00694 for(i = 0; i < TM2_NUM_STREAMS; i++)
00695 ctx->tok_ptrs[i] = 0;
00696
00697 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
00698 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
00699 return -1;
00700 }
00701
00702 memset(ctx->last, 0, 4 * bw * sizeof(int));
00703 memset(ctx->clast, 0, 4 * bw * sizeof(int));
00704
00705 for(j = 0; j < bh; j++) {
00706 memset(ctx->D, 0, 4 * sizeof(int));
00707 memset(ctx->CD, 0, 4 * sizeof(int));
00708 for(i = 0; i < bw; i++) {
00709 type = GET_TOK(ctx, TM2_TYPE);
00710 switch(type) {
00711 case TM2_HI_RES:
00712 tm2_hi_res_block(ctx, p, i, j);
00713 break;
00714 case TM2_MED_RES:
00715 tm2_med_res_block(ctx, p, i, j);
00716 break;
00717 case TM2_LOW_RES:
00718 tm2_low_res_block(ctx, p, i, j);
00719 break;
00720 case TM2_NULL_RES:
00721 tm2_null_res_block(ctx, p, i, j);
00722 break;
00723 case TM2_UPDATE:
00724 tm2_update_block(ctx, p, i, j);
00725 keyframe = 0;
00726 break;
00727 case TM2_STILL:
00728 tm2_still_block(ctx, p, i, j);
00729 keyframe = 0;
00730 break;
00731 case TM2_MOTION:
00732 tm2_motion_block(ctx, p, i, j);
00733 keyframe = 0;
00734 break;
00735 default:
00736 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
00737 }
00738 }
00739 }
00740
00741
00742 Y = (ctx->cur?ctx->Y2:ctx->Y1);
00743 U = (ctx->cur?ctx->U2:ctx->U1);
00744 V = (ctx->cur?ctx->V2:ctx->V1);
00745 dst = p->data[0];
00746 for(j = 0; j < ctx->avctx->height; j++){
00747 for(i = 0; i < ctx->avctx->width; i++){
00748 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
00749 dst[3*i+0] = av_clip_uint8(y + v);
00750 dst[3*i+1] = av_clip_uint8(y);
00751 dst[3*i+2] = av_clip_uint8(y + u);
00752 }
00753 Y += ctx->avctx->width;
00754 if (j & 1) {
00755 U += ctx->avctx->width >> 1;
00756 V += ctx->avctx->width >> 1;
00757 }
00758 dst += p->linesize[0];
00759 }
00760
00761 return keyframe;
00762 }
00763
00764 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
00765 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
00766 };
00767
00768 static int decode_frame(AVCodecContext *avctx,
00769 void *data, int *data_size,
00770 AVPacket *avpkt)
00771 {
00772 const uint8_t *buf = avpkt->data;
00773 int buf_size = avpkt->size;
00774 TM2Context * const l = avctx->priv_data;
00775 AVFrame * const p= (AVFrame*)&l->pic;
00776 int i, skip, t;
00777 uint8_t *swbuf;
00778
00779 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00780 if(!swbuf){
00781 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00782 return -1;
00783 }
00784 p->reference = 1;
00785 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00786 if(avctx->reget_buffer(avctx, p) < 0){
00787 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00788 av_free(swbuf);
00789 return -1;
00790 }
00791
00792 l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
00793 skip = tm2_read_header(l, swbuf);
00794
00795 if(skip == -1){
00796 av_free(swbuf);
00797 return -1;
00798 }
00799
00800 for(i = 0; i < TM2_NUM_STREAMS; i++){
00801 t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size);
00802 if(t == -1){
00803 av_free(swbuf);
00804 return -1;
00805 }
00806 skip += t;
00807 }
00808 p->key_frame = tm2_decode_blocks(l, p);
00809 if(p->key_frame)
00810 p->pict_type = FF_I_TYPE;
00811 else
00812 p->pict_type = FF_P_TYPE;
00813
00814 l->cur = !l->cur;
00815 *data_size = sizeof(AVFrame);
00816 *(AVFrame*)data = l->pic;
00817 av_free(swbuf);
00818
00819 return buf_size;
00820 }
00821
00822 static av_cold int decode_init(AVCodecContext *avctx){
00823 TM2Context * const l = avctx->priv_data;
00824 int i;
00825
00826 if((avctx->width & 3) || (avctx->height & 3)){
00827 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
00828 return -1;
00829 }
00830
00831 l->avctx = avctx;
00832 l->pic.data[0]=NULL;
00833 avctx->pix_fmt = PIX_FMT_BGR24;
00834
00835 dsputil_init(&l->dsp, avctx);
00836
00837 l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
00838 l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
00839
00840 for(i = 0; i < TM2_NUM_STREAMS; i++) {
00841 l->tokens[i] = NULL;
00842 l->tok_lens[i] = 0;
00843 }
00844
00845 l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
00846 l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
00847 l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
00848 l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
00849 l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
00850 l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
00851 l->cur = 0;
00852
00853 return 0;
00854 }
00855
00856 static av_cold int decode_end(AVCodecContext *avctx){
00857 TM2Context * const l = avctx->priv_data;
00858 AVFrame *pic = &l->pic;
00859 int i;
00860
00861 av_free(l->last);
00862 av_free(l->clast);
00863 for(i = 0; i < TM2_NUM_STREAMS; i++)
00864 av_free(l->tokens[i]);
00865 if(l->Y1){
00866 av_free(l->Y1);
00867 av_free(l->U1);
00868 av_free(l->V1);
00869 av_free(l->Y2);
00870 av_free(l->U2);
00871 av_free(l->V2);
00872 }
00873
00874 if (pic->data[0])
00875 avctx->release_buffer(avctx, pic);
00876
00877 return 0;
00878 }
00879
00880 AVCodec ff_truemotion2_decoder = {
00881 "truemotion2",
00882 AVMEDIA_TYPE_VIDEO,
00883 CODEC_ID_TRUEMOTION2,
00884 sizeof(TM2Context),
00885 decode_init,
00886 NULL,
00887 decode_end,
00888 decode_frame,
00889 CODEC_CAP_DR1,
00890 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
00891 };