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