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