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