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