configure: Check for a different SDL function
[libav.git] / libavcodec / truemotion2.c
CommitLineData
64af6168
MM
1/*
2 * Duck/ON2 TrueMotion 2 Decoder
3 * Copyright (c) 2005 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
64af6168
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
64af6168 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
64af6168
MM
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
64af6168 20 */
115329f1 21
64af6168 22/**
ba87f080 23 * @file
64af6168
MM
24 * Duck TrueMotion2 decoder.
25 */
115329f1 26
64af6168 27#include "avcodec.h"
bd508d43 28#include "bytestream.h"
9106a698 29#include "get_bits.h"
64af6168
MM
30#include "dsputil.h"
31
32#define TM2_ESCAPE 0x80000000
adeba281 33#define TM2_DELTAS 64
64af6168
MM
34/* Huffman-coded streams of different types of blocks */
35enum TM2_STREAMS{ TM2_C_HI = 0, TM2_C_LO, TM2_L_HI, TM2_L_LO,
36 TM2_UPD, TM2_MOT, TM2_TYPE, TM2_NUM_STREAMS};
37/* Block types */
38enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
39 TM2_UPDATE, TM2_STILL, TM2_MOTION};
40
41typedef struct TM2Context{
42 AVCodecContext *avctx;
43 AVFrame pic;
44
45 GetBitContext gb;
46 DSPContext dsp;
115329f1 47
64af6168
MM
48 /* TM2 streams */
49 int *tokens[TM2_NUM_STREAMS];
50 int tok_lens[TM2_NUM_STREAMS];
51 int tok_ptrs[TM2_NUM_STREAMS];
52 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
53 /* for blocks decoding */
54 int D[4];
55 int CD[4];
56 int *last;
57 int *clast;
115329f1 58
64af6168
MM
59 /* data for current and previous frame */
60 int *Y1, *U1, *V1, *Y2, *U2, *V2;
61 int cur;
62} TM2Context;
63
64/**
65* Huffman codes for each of streams
66*/
67typedef struct TM2Codes{
6001dad6 68 VLC vlc; ///< table for Libav bitstream reader
64af6168
MM
69 int bits;
70 int *recode; ///< table for converting from code indexes to values
71 int length;
72} TM2Codes;
73
74/**
75* structure for gathering Huffman codes information
76*/
77typedef struct TM2Huff{
78 int val_bits; ///< length of literal
79 int max_bits; ///< maximum length of code
80 int min_bits; ///< minimum length of code
81 int nodes; ///< total number of nodes in tree
82 int num; ///< current number filled
83 int max_num; ///< total number of codes
84 int *nums; ///< literals
85 uint32_t *bits; ///< codes
86 int *lens; ///< codelengths
87} TM2Huff;
88
89static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
90{
91 if(length > huff->max_bits) {
92 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits);
93 return -1;
94 }
95
96 if(!get_bits1(&ctx->gb)) { /* literal */
97 if (length == 0) {
98 length = 1;
99 }
100 if(huff->num >= huff->max_num) {
101 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
102 return -1;
103 }
104 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
105 huff->bits[huff->num] = prefix;
106 huff->lens[huff->num] = length;
107 huff->num++;
108 return 0;
109 } else { /* non-terminal node */
110 if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)
111 return -1;
112 if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)
113 return -1;
114 }
115 return 0;
116}
117
118static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
119{
120 TM2Huff huff;
121 int res = 0;
115329f1 122
64af6168
MM
123 huff.val_bits = get_bits(&ctx->gb, 5);
124 huff.max_bits = get_bits(&ctx->gb, 5);
125 huff.min_bits = get_bits(&ctx->gb, 5);
126 huff.nodes = get_bits_long(&ctx->gb, 17);
127 huff.num = 0;
115329f1 128
64af6168
MM
129 /* check for correct codes parameters */
130 if((huff.val_bits < 1) || (huff.val_bits > 32) ||
131 (huff.max_bits < 0) || (huff.max_bits > 32)) {
132 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
133 huff.val_bits, huff.max_bits);
134 return -1;
135 }
2b83e8b7 136 if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
64af6168
MM
137 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
138 return -1;
139 }
140 /* one-node tree */
141 if(huff.max_bits == 0)
142 huff.max_bits = 1;
115329f1 143
64af6168
MM
144 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
145 huff.max_num = (huff.nodes + 1) >> 1;
146 huff.nums = av_mallocz(huff.max_num * sizeof(int));
147 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
148 huff.lens = av_mallocz(huff.max_num * sizeof(int));
115329f1 149
64af6168
MM
150 if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
151 res = -1;
115329f1 152
64af6168
MM
153 if(huff.num != huff.max_num) {
154 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
155 huff.num, huff.max_num);
156 res = -1;
157 }
115329f1 158
64af6168
MM
159 /* convert codes to vlc_table */
160 if(res != -1) {
161 int i;
115329f1 162
64af6168
MM
163 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
164 huff.lens, sizeof(int), sizeof(int),
165 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
166 if(res < 0) {
167 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
168 res = -1;
115329f1 169 } else
64af6168
MM
170 res = 0;
171 if(res != -1) {
172 code->bits = huff.max_bits;
173 code->length = huff.max_num;
174 code->recode = av_malloc(code->length * sizeof(int));
175 for(i = 0; i < code->length; i++)
176 code->recode[i] = huff.nums[i];
177 }
178 }
179 /* free allocated memory */
180 av_free(huff.nums);
181 av_free(huff.bits);
182 av_free(huff.lens);
115329f1 183
64af6168
MM
184 return res;
185}
186
187static void tm2_free_codes(TM2Codes *code)
188{
437fb1c8 189 av_free(code->recode);
64af6168 190 if(code->vlc.table)
e96b4a53 191 ff_free_vlc(&code->vlc);
64af6168
MM
192}
193
194static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
195{
196 int val;
197 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
198 return code->recode[val];
199}
200
a8a8f204 201static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
64af6168
MM
202{
203 uint32_t magic;
a8a8f204 204 const uint8_t *obuf;
115329f1 205
64af6168 206 obuf = buf;
115329f1 207
fead30d4 208 magic = AV_RL32(buf);
64af6168 209 buf += 4;
115329f1 210
64af6168
MM
211 if(magic == 0x00000100) { /* old header */
212/* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
213 return 40;
214 } else if(magic == 0x00000101) { /* new header */
64af6168
MM
215 return 40;
216 } else {
217 av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
218 return -1;
219 }
115329f1 220
ccd425e7 221 return buf - obuf;
64af6168
MM
222}
223
224static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
225 int d, mb;
226 int i, v;
115329f1 227
64af6168
MM
228 d = get_bits(&ctx->gb, 9);
229 mb = get_bits(&ctx->gb, 5);
115329f1 230
64af6168
MM
231 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
232 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
233 return -1;
234 }
115329f1 235
64af6168
MM
236 for(i = 0; i < d; i++) {
237 v = get_bits_long(&ctx->gb, mb);
238 if(v & (1 << (mb - 1)))
239 ctx->deltas[stream_id][i] = v - (1 << mb);
240 else
241 ctx->deltas[stream_id][i] = v;
242 }
243 for(; i < TM2_DELTAS; i++)
244 ctx->deltas[stream_id][i] = 0;
115329f1 245
64af6168
MM
246 return 0;
247}
248
b89f4fb1
DK
249static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
250{
64af6168 251 int i;
64af6168 252 int skip = 0;
bd508d43 253 int len, toks, pos;
64af6168 254 TM2Codes codes;
bd508d43 255 GetByteContext gb;
115329f1 256
64af6168 257 /* get stream length in dwords */
bd508d43
RB
258 bytestream2_init(&gb, buf, buf_size);
259 len = bytestream2_get_be32(&gb);
64af6168 260 skip = len * 4 + 4;
115329f1 261
64af6168
MM
262 if(len == 0)
263 return 4;
115329f1 264
b89f4fb1
DK
265 if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
266 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
267 return -1;
268 }
269
bd508d43 270 toks = bytestream2_get_be32(&gb);
64af6168 271 if(toks & 1) {
bd508d43 272 len = bytestream2_get_be32(&gb);
64af6168 273 if(len == TM2_ESCAPE) {
bd508d43 274 len = bytestream2_get_be32(&gb);
64af6168
MM
275 }
276 if(len > 0) {
bd508d43
RB
277 pos = bytestream2_tell(&gb);
278 if (skip <= pos)
696ace50 279 return -1;
bd508d43 280 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
64af6168
MM
281 if(tm2_read_deltas(ctx, stream_id) == -1)
282 return -1;
bd508d43 283 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
64af6168
MM
284 }
285 }
286 /* skip unused fields */
bd508d43
RB
287 len = bytestream2_get_be32(&gb);
288 if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
289 bytestream2_skip(&gb, 8); /* unused by decoder */
290 } else {
291 bytestream2_skip(&gb, 4); /* unused by decoder */
64af6168 292 }
115329f1 293
bd508d43
RB
294 pos = bytestream2_tell(&gb);
295 if (skip <= pos)
696ace50 296 return -1;
bd508d43 297 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
64af6168
MM
298 if(tm2_build_huff_table(ctx, &codes) == -1)
299 return -1;
bd508d43 300 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
115329f1 301
64af6168
MM
302 toks >>= 1;
303 /* check if we have sane number of tokens */
304 if((toks < 0) || (toks > 0xFFFFFF)){
305 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
306 tm2_free_codes(&codes);
307 return -1;
308 }
309 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
310 ctx->tok_lens[stream_id] = toks;
bd508d43 311 len = bytestream2_get_be32(&gb);
64af6168 312 if(len > 0) {
bd508d43
RB
313 pos = bytestream2_tell(&gb);
314 if (skip <= pos)
696ace50 315 return -1;
bd508d43 316 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
b89f4fb1
DK
317 for(i = 0; i < toks; i++) {
318 if (get_bits_left(&ctx->gb) <= 0) {
319 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
320 return -1;
321 }
64af6168 322 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
b89f4fb1 323 }
64af6168 324 } else {
adeba281
MM
325 for(i = 0; i < toks; i++)
326 ctx->tokens[stream_id][i] = codes.recode[0];
64af6168
MM
327 }
328 tm2_free_codes(&codes);
115329f1 329
64af6168
MM
330 return skip;
331}
332
333static inline int GET_TOK(TM2Context *ctx,int type) {
334 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
335 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]);
336 return 0;
337 }
338 if(type <= TM2_MOT)
339 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
340 return ctx->tokens[type][ctx->tok_ptrs[type]++];
341}
342
343/* blocks decoding routines */
344
345/* common Y, U, V pointers initialisation */
670924fd 346#define TM2_INIT_POINTERS() \
64af6168
MM
347 int *last, *clast; \
348 int *Y, *U, *V;\
349 int Ystride, Ustride, Vstride;\
350\
351 Ystride = ctx->avctx->width;\
352 Vstride = (ctx->avctx->width + 1) >> 1;\
353 Ustride = (ctx->avctx->width + 1) >> 1;\
354 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
355 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
356 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
357 last = ctx->last + bx * 4;\
358 clast = ctx->clast + bx * 4;
359
670924fd 360#define TM2_INIT_POINTERS_2() \
64af6168
MM
361 int *Yo, *Uo, *Vo;\
362 int oYstride, oUstride, oVstride;\
363\
364 TM2_INIT_POINTERS();\
365 oYstride = Ystride;\
366 oVstride = Vstride;\
367 oUstride = Ustride;\
368 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
369 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
370 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
371
372/* recalculate last and delta values for next blocks */
373#define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
96c5bc46 374 CD[0] = CHR[1] - last[1];\
64af6168 375 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
96c5bc46
RD
376 last[0] = (int)CHR[stride + 0];\
377 last[1] = (int)CHR[stride + 1];}
64af6168
MM
378
379/* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
380static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
381{
382 int ct, d;
383 int i, j;
115329f1 384
64af6168
MM
385 for(j = 0; j < 4; j++){
386 ct = ctx->D[j];
387 for(i = 0; i < 4; i++){
388 d = deltas[i + j * 4];
389 ct += d;
390 last[i] += ct;
f66e4f5f 391 Y[i] = av_clip_uint8(last[i]);
64af6168
MM
392 }
393 Y += stride;
394 ctx->D[j] = ct;
395 }
396}
397
398static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
399{
400 int i, j;
401 for(j = 0; j < 2; j++){
402 for(i = 0; i < 2; i++){
403 CD[j] += deltas[i + j * 2];
404 last[i] += CD[j];
96c5bc46 405 data[i] = last[i];
64af6168
MM
406 }
407 data += stride;
408 }
409}
410
411static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
412{
413 int t;
414 int l;
415 int prev;
416
417 if(bx > 0)
418 prev = clast[-3];
419 else
420 prev = 0;
421 t = (CD[0] + CD[1]) >> 1;
48fa22d7 422 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
64af6168
MM
423 CD[1] = CD[0] + CD[1] - t;
424 CD[0] = t;
425 clast[0] = l;
115329f1 426
64af6168
MM
427 tm2_high_chroma(data, stride, clast, CD, deltas);
428}
429
430static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
431{
432 int i;
433 int deltas[16];
434 TM2_INIT_POINTERS();
435
436 /* hi-res chroma */
437 for(i = 0; i < 4; i++) {
438 deltas[i] = GET_TOK(ctx, TM2_C_HI);
439 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
440 }
441 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
442 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
115329f1 443
64af6168
MM
444 /* hi-res luma */
445 for(i = 0; i < 16; i++)
446 deltas[i] = GET_TOK(ctx, TM2_L_HI);
115329f1 447
64af6168
MM
448 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
449}
450
451static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
452{
453 int i;
454 int deltas[16];
455 TM2_INIT_POINTERS();
115329f1 456
64af6168
MM
457 /* low-res chroma */
458 deltas[0] = GET_TOK(ctx, TM2_C_LO);
459 deltas[1] = deltas[2] = deltas[3] = 0;
460 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
461
462 deltas[0] = GET_TOK(ctx, TM2_C_LO);
463 deltas[1] = deltas[2] = deltas[3] = 0;
464 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
465
466 /* hi-res luma */
467 for(i = 0; i < 16; i++)
468 deltas[i] = GET_TOK(ctx, TM2_L_HI);
115329f1 469
64af6168
MM
470 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
471}
472
473static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
474{
475 int i;
476 int t1, t2;
477 int deltas[16];
478 TM2_INIT_POINTERS();
479
480 /* low-res chroma */
481 deltas[0] = GET_TOK(ctx, TM2_C_LO);
482 deltas[1] = deltas[2] = deltas[3] = 0;
483 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
484
485 deltas[0] = GET_TOK(ctx, TM2_C_LO);
486 deltas[1] = deltas[2] = deltas[3] = 0;
487 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
488
489 /* low-res luma */
490 for(i = 0; i < 16; i++)
491 deltas[i] = 0;
115329f1 492
64af6168
MM
493 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
494 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
495 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
496 deltas[10] = GET_TOK(ctx, TM2_L_LO);
115329f1 497
64af6168
MM
498 if(bx > 0)
499 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
500 else
501 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
502 last[2] = (last[1] + last[3]) >> 1;
503
504 t1 = ctx->D[0] + ctx->D[1];
505 ctx->D[0] = t1 >> 1;
506 ctx->D[1] = t1 - (t1 >> 1);
507 t2 = ctx->D[2] + ctx->D[3];
508 ctx->D[2] = t2 >> 1;
509 ctx->D[3] = t2 - (t2 >> 1);
115329f1 510
64af6168
MM
511 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
512}
513
514static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
515{
516 int i;
517 int ct;
518 int left, right, diff;
519 int deltas[16];
520 TM2_INIT_POINTERS();
115329f1 521
64af6168
MM
522 /* null chroma */
523 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
524 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
525
526 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
527 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
115329f1 528
64af6168
MM
529 /* null luma */
530 for(i = 0; i < 16; i++)
531 deltas[i] = 0;
532
533 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
115329f1 534
64af6168
MM
535 if(bx > 0)
536 left = last[-1] - ct;
537 else
538 left = 0;
115329f1 539
64af6168
MM
540 right = last[3];
541 diff = right - left;
542 last[0] = left + (diff >> 2);
543 last[1] = left + (diff >> 1);
544 last[2] = right - (diff >> 2);
545 last[3] = right;
546 {
547 int tp = left;
115329f1 548
64af6168
MM
549 ctx->D[0] = (tp + (ct >> 2)) - left;
550 left += ctx->D[0];
551 ctx->D[1] = (tp + (ct >> 1)) - left;
552 left += ctx->D[1];
553 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
554 left += ctx->D[2];
555 ctx->D[3] = (tp + ct) - left;
556 }
557 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
558}
559
560static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
561{
562 int i, j;
563 TM2_INIT_POINTERS_2();
564
565 /* update chroma */
566 for(j = 0; j < 2; j++){
567 for(i = 0; i < 2; i++){
568 U[i] = Uo[i];
569 V[i] = Vo[i];
570 }
571 U += Ustride; V += Vstride;
572 Uo += oUstride; Vo += oVstride;
573 }
574 U -= Ustride * 2;
575 V -= Vstride * 2;
576 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
577 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
578
579 /* update deltas */
580 ctx->D[0] = Yo[3] - last[3];
581 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
582 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
583 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
584
585 for(j = 0; j < 4; j++){
586 for(i = 0; i < 4; i++){
587 Y[i] = Yo[i];
588 last[i] = Yo[i];
589 }
590 Y += Ystride;
591 Yo += oYstride;
592 }
593}
594
595static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
596{
597 int i, j;
598 int d;
599 TM2_INIT_POINTERS_2();
115329f1 600
64af6168
MM
601 /* update chroma */
602 for(j = 0; j < 2; j++){
603 for(i = 0; i < 2; i++){
604 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
605 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
606 }
607 U += Ustride; V += Vstride;
608 Uo += oUstride; Vo += oVstride;
609 }
610 U -= Ustride * 2;
611 V -= Vstride * 2;
612 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
613 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
614
615 /* update deltas */
616 ctx->D[0] = Yo[3] - last[3];
617 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
618 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
619 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
620
621 for(j = 0; j < 4; j++){
622 d = last[3];
623 for(i = 0; i < 4; i++){
624 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
625 last[i] = Y[i];
626 }
627 ctx->D[j] = last[3] - d;
628 Y += Ystride;
629 Yo += oYstride;
630 }
631}
632
633static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
634{
635 int i, j;
636 int mx, my;
637 TM2_INIT_POINTERS_2();
638
639 mx = GET_TOK(ctx, TM2_MOT);
640 my = GET_TOK(ctx, TM2_MOT);
115329f1 641
64af6168
MM
642 Yo += my * oYstride + mx;
643 Uo += (my >> 1) * oUstride + (mx >> 1);
644 Vo += (my >> 1) * oVstride + (mx >> 1);
115329f1 645
64af6168
MM
646 /* copy chroma */
647 for(j = 0; j < 2; j++){
648 for(i = 0; i < 2; i++){
649 U[i] = Uo[i];
650 V[i] = Vo[i];
651 }
652 U += Ustride; V += Vstride;
653 Uo += oUstride; Vo += oVstride;
654 }
655 U -= Ustride * 2;
656 V -= Vstride * 2;
657 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
658 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
659
660 /* copy luma */
661 for(j = 0; j < 4; j++){
662 for(i = 0; i < 4; i++){
663 Y[i] = Yo[i];
664 }
665 Y += Ystride;
666 Yo += oYstride;
667 }
668 /* calculate deltas */
669 Y -= Ystride * 4;
670 ctx->D[0] = Y[3] - last[3];
671 ctx->D[1] = Y[3 + Ystride] - Y[3];
672 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
673 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
674 for(i = 0; i < 4; i++)
675 last[i] = Y[i + Ystride * 3];
676}
677
678static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
679{
680 int i, j;
681 int bw, bh;
682 int type;
683 int keyframe = 1;
96c5bc46
RD
684 int *Y, *U, *V;
685 uint8_t *dst;
115329f1 686
64af6168
MM
687 bw = ctx->avctx->width >> 2;
688 bh = ctx->avctx->height >> 2;
689
690 for(i = 0; i < TM2_NUM_STREAMS; i++)
691 ctx->tok_ptrs[i] = 0;
115329f1 692
64af6168
MM
693 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
694 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
695 return -1;
696 }
115329f1 697
64af6168
MM
698 memset(ctx->last, 0, 4 * bw * sizeof(int));
699 memset(ctx->clast, 0, 4 * bw * sizeof(int));
700
701 for(j = 0; j < bh; j++) {
702 memset(ctx->D, 0, 4 * sizeof(int));
703 memset(ctx->CD, 0, 4 * sizeof(int));
704 for(i = 0; i < bw; i++) {
705 type = GET_TOK(ctx, TM2_TYPE);
706 switch(type) {
707 case TM2_HI_RES:
708 tm2_hi_res_block(ctx, p, i, j);
709 break;
710 case TM2_MED_RES:
711 tm2_med_res_block(ctx, p, i, j);
712 break;
713 case TM2_LOW_RES:
714 tm2_low_res_block(ctx, p, i, j);
715 break;
716 case TM2_NULL_RES:
717 tm2_null_res_block(ctx, p, i, j);
718 break;
719 case TM2_UPDATE:
720 tm2_update_block(ctx, p, i, j);
721 keyframe = 0;
722 break;
723 case TM2_STILL:
724 tm2_still_block(ctx, p, i, j);
725 keyframe = 0;
726 break;
727 case TM2_MOTION:
728 tm2_motion_block(ctx, p, i, j);
729 keyframe = 0;
730 break;
731 default:
732 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
733 }
734 }
735 }
115329f1
DB
736
737 /* copy data from our buffer to AVFrame */
96c5bc46
RD
738 Y = (ctx->cur?ctx->Y2:ctx->Y1);
739 U = (ctx->cur?ctx->U2:ctx->U1);
740 V = (ctx->cur?ctx->V2:ctx->V1);
741 dst = p->data[0];
64af6168
MM
742 for(j = 0; j < ctx->avctx->height; j++){
743 for(i = 0; i < ctx->avctx->width; i++){
96c5bc46
RD
744 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
745 dst[3*i+0] = av_clip_uint8(y + v);
746 dst[3*i+1] = av_clip_uint8(y);
747 dst[3*i+2] = av_clip_uint8(y + u);
64af6168 748 }
96c5bc46
RD
749 Y += ctx->avctx->width;
750 if (j & 1) {
751 U += ctx->avctx->width >> 1;
752 V += ctx->avctx->width >> 1;
64af6168 753 }
96c5bc46 754 dst += p->linesize[0];
64af6168 755 }
115329f1 756
64af6168
MM
757 return keyframe;
758}
759
a2a98637
KS
760static const int tm2_stream_order[TM2_NUM_STREAMS] = {
761 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
762};
763
115329f1 764static int decode_frame(AVCodecContext *avctx,
64af6168 765 void *data, int *data_size,
7a00bbad 766 AVPacket *avpkt)
64af6168 767{
7a00bbad 768 const uint8_t *buf = avpkt->data;
bd508d43 769 int buf_size = avpkt->size & ~3;
64af6168 770 TM2Context * const l = avctx->priv_data;
562b6c74 771 AVFrame * const p = &l->pic;
a2a98637 772 int i, skip, t;
ad4d0b9e 773 uint8_t *swbuf;
64af6168 774
ad4d0b9e
KS
775 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
776 if(!swbuf){
777 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
778 return -1;
779 }
64af6168 780 p->reference = 1;
adeba281
MM
781 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
782 if(avctx->reget_buffer(avctx, p) < 0){
64af6168 783 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
ad4d0b9e 784 av_free(swbuf);
64af6168
MM
785 return -1;
786 }
787
ad4d0b9e
KS
788 l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
789 skip = tm2_read_header(l, swbuf);
115329f1 790
ad4d0b9e
KS
791 if(skip == -1){
792 av_free(swbuf);
64af6168 793 return -1;
ad4d0b9e 794 }
115329f1 795
a2a98637 796 for(i = 0; i < TM2_NUM_STREAMS; i++){
bd508d43
RB
797 if (skip >= buf_size) {
798 av_free(swbuf);
799 return AVERROR_INVALIDDATA;
800 }
801 t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size - skip);
a2a98637 802 if(t == -1){
ad4d0b9e 803 av_free(swbuf);
a2a98637
KS
804 return -1;
805 }
806 skip += t;
807 }
64af6168
MM
808 p->key_frame = tm2_decode_blocks(l, p);
809 if(p->key_frame)
975a1447 810 p->pict_type = AV_PICTURE_TYPE_I;
64af6168 811 else
975a1447 812 p->pict_type = AV_PICTURE_TYPE_P;
115329f1 813
64af6168
MM
814 l->cur = !l->cur;
815 *data_size = sizeof(AVFrame);
816 *(AVFrame*)data = l->pic;
ad4d0b9e 817 av_free(swbuf);
115329f1 818
64af6168
MM
819 return buf_size;
820}
821
98a6fff9 822static av_cold int decode_init(AVCodecContext *avctx){
64af6168
MM
823 TM2Context * const l = avctx->priv_data;
824 int i;
825
64af6168
MM
826 if((avctx->width & 3) || (avctx->height & 3)){
827 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
828 return -1;
829 }
115329f1 830
64af6168
MM
831 l->avctx = avctx;
832 l->pic.data[0]=NULL;
96c5bc46 833 avctx->pix_fmt = PIX_FMT_BGR24;
64af6168 834
9cf0841e 835 ff_dsputil_init(&l->dsp, avctx);
115329f1 836
64af6168
MM
837 l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
838 l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
115329f1 839
64af6168
MM
840 for(i = 0; i < TM2_NUM_STREAMS; i++) {
841 l->tokens[i] = NULL;
842 l->tok_lens[i] = 0;
843 }
115329f1 844
64af6168
MM
845 l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
846 l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
847 l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
848 l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
849 l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
850 l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
851 l->cur = 0;
115329f1 852
64af6168
MM
853 return 0;
854}
855
98a6fff9 856static av_cold int decode_end(AVCodecContext *avctx){
64af6168 857 TM2Context * const l = avctx->priv_data;
6d924b5a 858 AVFrame *pic = &l->pic;
64af6168
MM
859 int i;
860
437fb1c8
CB
861 av_free(l->last);
862 av_free(l->clast);
64af6168 863 for(i = 0; i < TM2_NUM_STREAMS; i++)
437fb1c8 864 av_free(l->tokens[i]);
64af6168
MM
865 if(l->Y1){
866 av_free(l->Y1);
867 av_free(l->U1);
868 av_free(l->V1);
869 av_free(l->Y2);
870 av_free(l->U2);
871 av_free(l->V2);
872 }
6d924b5a
KS
873
874 if (pic->data[0])
875 avctx->release_buffer(avctx, pic);
6d924b5a 876
64af6168
MM
877 return 0;
878}
879
d36beb3f 880AVCodec ff_truemotion2_decoder = {
ec6402b7
AK
881 .name = "truemotion2",
882 .type = AVMEDIA_TYPE_VIDEO,
883 .id = CODEC_ID_TRUEMOTION2,
884 .priv_data_size = sizeof(TM2Context),
885 .init = decode_init,
886 .close = decode_end,
887 .decode = decode_frame,
888 .capabilities = CODEC_CAP_DR1,
fe4bf374 889 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
64af6168 890};