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