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