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