lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / truemotion2.c
blobd517906e4372058ed1994db203b1aa86090e1c8f
1 /*
2 * Duck/ON2 TrueMotion 2 Decoder
3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /**
23 * @file
24 * Duck TrueMotion2 decoder.
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "get_bits.h"
30 #include "dsputil.h"
32 #define TM2_ESCAPE 0x80000000
33 #define TM2_DELTAS 64
35 /* Huffman-coded streams of different types of blocks */
36 enum TM2_STREAMS {
37 TM2_C_HI = 0,
38 TM2_C_LO,
39 TM2_L_HI,
40 TM2_L_LO,
41 TM2_UPD,
42 TM2_MOT,
43 TM2_TYPE,
44 TM2_NUM_STREAMS
47 /* Block types */
48 enum TM2_BLOCKS {
49 TM2_HI_RES = 0,
50 TM2_MED_RES,
51 TM2_LOW_RES,
52 TM2_NULL_RES,
53 TM2_UPDATE,
54 TM2_STILL,
55 TM2_MOTION
58 typedef struct TM2Context {
59 AVCodecContext *avctx;
60 AVFrame pic;
62 GetBitContext gb;
63 DSPContext dsp;
65 /* TM2 streams */
66 int *tokens[TM2_NUM_STREAMS];
67 int tok_lens[TM2_NUM_STREAMS];
68 int tok_ptrs[TM2_NUM_STREAMS];
69 int deltas[TM2_NUM_STREAMS][TM2_DELTAS];
70 /* for blocks decoding */
71 int D[4];
72 int CD[4];
73 int *last;
74 int *clast;
76 /* data for current and previous frame */
77 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
78 int *Y1, *U1, *V1, *Y2, *U2, *V2;
79 int y_stride, uv_stride;
80 int cur;
81 } TM2Context;
83 /**
84 * Huffman codes for each of streams
86 typedef struct TM2Codes {
87 VLC vlc; ///< table for Libav bitstream reader
88 int bits;
89 int *recode; ///< table for converting from code indexes to values
90 int length;
91 } TM2Codes;
93 /**
94 * structure for gathering Huffman codes information
96 typedef struct TM2Huff {
97 int val_bits; ///< length of literal
98 int max_bits; ///< maximum length of code
99 int min_bits; ///< minimum length of code
100 int nodes; ///< total number of nodes in tree
101 int num; ///< current number filled
102 int max_num; ///< total number of codes
103 int *nums; ///< literals
104 uint32_t *bits; ///< codes
105 int *lens; ///< codelengths
106 } TM2Huff;
108 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
110 int ret;
111 if (length > huff->max_bits) {
112 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
113 huff->max_bits);
114 return AVERROR_INVALIDDATA;
117 if (!get_bits1(&ctx->gb)) { /* literal */
118 if (length == 0) {
119 length = 1;
121 if (huff->num >= huff->max_num) {
122 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
123 return AVERROR_INVALIDDATA;
125 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
126 huff->bits[huff->num] = prefix;
127 huff->lens[huff->num] = length;
128 huff->num++;
129 return 0;
130 } else { /* non-terminal node */
131 if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
132 return ret;
133 if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
134 return ret;
136 return 0;
139 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
141 TM2Huff huff;
142 int res = 0;
144 huff.val_bits = get_bits(&ctx->gb, 5);
145 huff.max_bits = get_bits(&ctx->gb, 5);
146 huff.min_bits = get_bits(&ctx->gb, 5);
147 huff.nodes = get_bits_long(&ctx->gb, 17);
148 huff.num = 0;
150 /* check for correct codes parameters */
151 if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
152 (huff.max_bits < 0) || (huff.max_bits > 25)) {
153 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
154 "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
155 return AVERROR_INVALIDDATA;
157 if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
158 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
159 "nodes: %i\n", huff.nodes);
160 return AVERROR_INVALIDDATA;
162 /* one-node tree */
163 if (huff.max_bits == 0)
164 huff.max_bits = 1;
166 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
167 huff.max_num = (huff.nodes + 1) >> 1;
168 huff.nums = av_mallocz(huff.max_num * sizeof(int));
169 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
170 huff.lens = av_mallocz(huff.max_num * sizeof(int));
172 res = tm2_read_tree(ctx, 0, 0, &huff);
174 if (huff.num != huff.max_num) {
175 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
176 huff.num, huff.max_num);
177 res = AVERROR_INVALIDDATA;
180 /* convert codes to vlc_table */
181 if (res >= 0) {
182 int i;
184 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
185 huff.lens, sizeof(int), sizeof(int),
186 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
187 if (res < 0)
188 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
189 else {
190 code->bits = huff.max_bits;
191 code->length = huff.max_num;
192 code->recode = av_malloc(code->length * sizeof(int));
193 for (i = 0; i < code->length; i++)
194 code->recode[i] = huff.nums[i];
197 /* free allocated memory */
198 av_free(huff.nums);
199 av_free(huff.bits);
200 av_free(huff.lens);
202 return res;
205 static void tm2_free_codes(TM2Codes *code)
207 av_free(code->recode);
208 if (code->vlc.table)
209 ff_free_vlc(&code->vlc);
212 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
214 int val;
215 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
216 return code->recode[val];
219 #define TM2_OLD_HEADER_MAGIC 0x00000100
220 #define TM2_NEW_HEADER_MAGIC 0x00000101
222 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
224 uint32_t magic = AV_RL32(buf);
226 switch (magic) {
227 case TM2_OLD_HEADER_MAGIC:
228 av_log_missing_feature(ctx->avctx, "TM2 old header", 1);
229 return 0;
230 case TM2_NEW_HEADER_MAGIC:
231 return 0;
232 default:
233 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
234 return AVERROR_INVALIDDATA;
238 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
240 int d, mb;
241 int i, v;
243 d = get_bits(&ctx->gb, 9);
244 mb = get_bits(&ctx->gb, 5);
246 if ((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
247 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
248 return AVERROR_INVALIDDATA;
251 for (i = 0; i < d; i++) {
252 v = get_bits_long(&ctx->gb, mb);
253 if (v & (1 << (mb - 1)))
254 ctx->deltas[stream_id][i] = v - (1 << mb);
255 else
256 ctx->deltas[stream_id][i] = v;
258 for (; i < TM2_DELTAS; i++)
259 ctx->deltas[stream_id][i] = 0;
261 return 0;
264 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
266 int i, ret;
267 int skip = 0;
268 int len, toks, pos;
269 TM2Codes codes;
270 GetByteContext gb;
272 /* get stream length in dwords */
273 bytestream2_init(&gb, buf, buf_size);
274 len = bytestream2_get_be32(&gb);
275 skip = len * 4 + 4;
277 if (len == 0)
278 return 4;
280 if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
281 av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
282 return AVERROR_INVALIDDATA;
285 toks = bytestream2_get_be32(&gb);
286 if (toks & 1) {
287 len = bytestream2_get_be32(&gb);
288 if (len == TM2_ESCAPE) {
289 len = bytestream2_get_be32(&gb);
291 if (len > 0) {
292 pos = bytestream2_tell(&gb);
293 if (skip <= pos)
294 return AVERROR_INVALIDDATA;
295 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
296 if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
297 return ret;
298 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
301 /* skip unused fields */
302 len = bytestream2_get_be32(&gb);
303 if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
304 bytestream2_skip(&gb, 8); /* unused by decoder */
305 } else {
306 bytestream2_skip(&gb, 4); /* unused by decoder */
309 pos = bytestream2_tell(&gb);
310 if (skip <= pos)
311 return AVERROR_INVALIDDATA;
312 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
313 if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
314 return ret;
315 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
317 toks >>= 1;
318 /* check if we have sane number of tokens */
319 if ((toks < 0) || (toks > 0xFFFFFF)) {
320 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
321 tm2_free_codes(&codes);
322 return AVERROR_INVALIDDATA;
324 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
325 ctx->tok_lens[stream_id] = toks;
326 len = bytestream2_get_be32(&gb);
327 if (len > 0) {
328 pos = bytestream2_tell(&gb);
329 if (skip <= pos)
330 return AVERROR_INVALIDDATA;
331 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
332 for (i = 0; i < toks; i++) {
333 if (get_bits_left(&ctx->gb) <= 0) {
334 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
335 return AVERROR_INVALIDDATA;
337 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
338 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
339 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
340 ctx->tokens[stream_id][i], stream_id, i);
341 return AVERROR_INVALIDDATA;
344 } else {
345 for (i = 0; i < toks; i++) {
346 ctx->tokens[stream_id][i] = codes.recode[0];
347 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
348 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
349 ctx->tokens[stream_id][i], stream_id, i);
350 return AVERROR_INVALIDDATA;
354 tm2_free_codes(&codes);
356 return skip;
359 static inline int GET_TOK(TM2Context *ctx,int type)
361 if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
362 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]);
363 return 0;
365 if (type <= TM2_MOT)
366 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
367 return ctx->tokens[type][ctx->tok_ptrs[type]++];
370 /* blocks decoding routines */
372 /* common Y, U, V pointers initialisation */
373 #define TM2_INIT_POINTERS() \
374 int *last, *clast; \
375 int *Y, *U, *V;\
376 int Ystride, Ustride, Vstride;\
378 Ystride = ctx->y_stride;\
379 Vstride = ctx->uv_stride;\
380 Ustride = ctx->uv_stride;\
381 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
382 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
383 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
384 last = ctx->last + bx * 4;\
385 clast = ctx->clast + bx * 4;
387 #define TM2_INIT_POINTERS_2() \
388 int *Yo, *Uo, *Vo;\
389 int oYstride, oUstride, oVstride;\
391 TM2_INIT_POINTERS();\
392 oYstride = Ystride;\
393 oVstride = Vstride;\
394 oUstride = Ustride;\
395 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
396 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
397 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
399 /* recalculate last and delta values for next blocks */
400 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
401 CD[0] = CHR[1] - last[1];\
402 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
403 last[0] = (int)CHR[stride + 0];\
404 last[1] = (int)CHR[stride + 1];}
406 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
407 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
409 int ct, d;
410 int i, j;
412 for (j = 0; j < 4; j++){
413 ct = ctx->D[j];
414 for (i = 0; i < 4; i++){
415 d = deltas[i + j * 4];
416 ct += d;
417 last[i] += ct;
418 Y[i] = av_clip_uint8(last[i]);
420 Y += stride;
421 ctx->D[j] = ct;
425 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
427 int i, j;
428 for (j = 0; j < 2; j++) {
429 for (i = 0; i < 2; i++) {
430 CD[j] += deltas[i + j * 2];
431 last[i] += CD[j];
432 data[i] = last[i];
434 data += stride;
438 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
440 int t;
441 int l;
442 int prev;
444 if (bx > 0)
445 prev = clast[-3];
446 else
447 prev = 0;
448 t = (CD[0] + CD[1]) >> 1;
449 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
450 CD[1] = CD[0] + CD[1] - t;
451 CD[0] = t;
452 clast[0] = l;
454 tm2_high_chroma(data, stride, clast, CD, deltas);
457 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
459 int i;
460 int deltas[16];
461 TM2_INIT_POINTERS();
463 /* hi-res chroma */
464 for (i = 0; i < 4; i++) {
465 deltas[i] = GET_TOK(ctx, TM2_C_HI);
466 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
468 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
469 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
471 /* hi-res luma */
472 for (i = 0; i < 16; i++)
473 deltas[i] = GET_TOK(ctx, TM2_L_HI);
475 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
478 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
480 int i;
481 int deltas[16];
482 TM2_INIT_POINTERS();
484 /* low-res chroma */
485 deltas[0] = GET_TOK(ctx, TM2_C_LO);
486 deltas[1] = deltas[2] = deltas[3] = 0;
487 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
489 deltas[0] = GET_TOK(ctx, TM2_C_LO);
490 deltas[1] = deltas[2] = deltas[3] = 0;
491 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
493 /* hi-res luma */
494 for (i = 0; i < 16; i++)
495 deltas[i] = GET_TOK(ctx, TM2_L_HI);
497 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
500 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
502 int i;
503 int t1, t2;
504 int deltas[16];
505 TM2_INIT_POINTERS();
507 /* low-res chroma */
508 deltas[0] = GET_TOK(ctx, TM2_C_LO);
509 deltas[1] = deltas[2] = deltas[3] = 0;
510 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
512 deltas[0] = GET_TOK(ctx, TM2_C_LO);
513 deltas[1] = deltas[2] = deltas[3] = 0;
514 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
516 /* low-res luma */
517 for (i = 0; i < 16; i++)
518 deltas[i] = 0;
520 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
521 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
522 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
523 deltas[10] = GET_TOK(ctx, TM2_L_LO);
525 if (bx > 0)
526 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
527 else
528 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
529 last[2] = (last[1] + last[3]) >> 1;
531 t1 = ctx->D[0] + ctx->D[1];
532 ctx->D[0] = t1 >> 1;
533 ctx->D[1] = t1 - (t1 >> 1);
534 t2 = ctx->D[2] + ctx->D[3];
535 ctx->D[2] = t2 >> 1;
536 ctx->D[3] = t2 - (t2 >> 1);
538 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
541 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
543 int i;
544 int ct;
545 int left, right, diff;
546 int deltas[16];
547 TM2_INIT_POINTERS();
549 /* null chroma */
550 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
551 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
553 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
554 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
556 /* null luma */
557 for (i = 0; i < 16; i++)
558 deltas[i] = 0;
560 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
562 if (bx > 0)
563 left = last[-1] - ct;
564 else
565 left = 0;
567 right = last[3];
568 diff = right - left;
569 last[0] = left + (diff >> 2);
570 last[1] = left + (diff >> 1);
571 last[2] = right - (diff >> 2);
572 last[3] = right;
574 int tp = left;
576 ctx->D[0] = (tp + (ct >> 2)) - left;
577 left += ctx->D[0];
578 ctx->D[1] = (tp + (ct >> 1)) - left;
579 left += ctx->D[1];
580 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
581 left += ctx->D[2];
582 ctx->D[3] = (tp + ct) - left;
584 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
587 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
589 int i, j;
590 TM2_INIT_POINTERS_2();
592 /* update chroma */
593 for (j = 0; j < 2; j++) {
594 for (i = 0; i < 2; i++){
595 U[i] = Uo[i];
596 V[i] = Vo[i];
598 U += Ustride; V += Vstride;
599 Uo += oUstride; Vo += oVstride;
601 U -= Ustride * 2;
602 V -= Vstride * 2;
603 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
604 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
606 /* update deltas */
607 ctx->D[0] = Yo[3] - last[3];
608 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
609 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
610 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
612 for (j = 0; j < 4; j++) {
613 for (i = 0; i < 4; i++) {
614 Y[i] = Yo[i];
615 last[i] = Yo[i];
617 Y += Ystride;
618 Yo += oYstride;
622 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
624 int i, j;
625 int d;
626 TM2_INIT_POINTERS_2();
628 /* update chroma */
629 for (j = 0; j < 2; j++) {
630 for (i = 0; i < 2; i++) {
631 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
632 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
634 U += Ustride;
635 V += Vstride;
636 Uo += oUstride;
637 Vo += oVstride;
639 U -= Ustride * 2;
640 V -= Vstride * 2;
641 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
642 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
644 /* update deltas */
645 ctx->D[0] = Yo[3] - last[3];
646 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
647 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
648 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
650 for (j = 0; j < 4; j++) {
651 d = last[3];
652 for (i = 0; i < 4; i++) {
653 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
654 last[i] = Y[i];
656 ctx->D[j] = last[3] - d;
657 Y += Ystride;
658 Yo += oYstride;
662 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
664 int i, j;
665 int mx, my;
666 TM2_INIT_POINTERS_2();
668 mx = GET_TOK(ctx, TM2_MOT);
669 my = GET_TOK(ctx, TM2_MOT);
670 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
671 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
673 Yo += my * oYstride + mx;
674 Uo += (my >> 1) * oUstride + (mx >> 1);
675 Vo += (my >> 1) * oVstride + (mx >> 1);
677 /* copy chroma */
678 for (j = 0; j < 2; j++) {
679 for (i = 0; i < 2; i++) {
680 U[i] = Uo[i];
681 V[i] = Vo[i];
683 U += Ustride;
684 V += Vstride;
685 Uo += oUstride;
686 Vo += oVstride;
688 U -= Ustride * 2;
689 V -= Vstride * 2;
690 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
691 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
693 /* copy luma */
694 for (j = 0; j < 4; j++) {
695 for (i = 0; i < 4; i++) {
696 Y[i] = Yo[i];
698 Y += Ystride;
699 Yo += oYstride;
701 /* calculate deltas */
702 Y -= Ystride * 4;
703 ctx->D[0] = Y[3] - last[3];
704 ctx->D[1] = Y[3 + Ystride] - Y[3];
705 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
706 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
707 for (i = 0; i < 4; i++)
708 last[i] = Y[i + Ystride * 3];
711 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
713 int i, j;
714 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
715 int type;
716 int keyframe = 1;
717 int *Y, *U, *V;
718 uint8_t *dst;
720 for (i = 0; i < TM2_NUM_STREAMS; i++)
721 ctx->tok_ptrs[i] = 0;
723 if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
724 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
725 return AVERROR_INVALIDDATA;
728 memset(ctx->last, 0, 4 * bw * sizeof(int));
729 memset(ctx->clast, 0, 4 * bw * sizeof(int));
731 for (j = 0; j < bh; j++) {
732 memset(ctx->D, 0, 4 * sizeof(int));
733 memset(ctx->CD, 0, 4 * sizeof(int));
734 for (i = 0; i < bw; i++) {
735 type = GET_TOK(ctx, TM2_TYPE);
736 switch(type) {
737 case TM2_HI_RES:
738 tm2_hi_res_block(ctx, p, i, j);
739 break;
740 case TM2_MED_RES:
741 tm2_med_res_block(ctx, p, i, j);
742 break;
743 case TM2_LOW_RES:
744 tm2_low_res_block(ctx, p, i, j);
745 break;
746 case TM2_NULL_RES:
747 tm2_null_res_block(ctx, p, i, j);
748 break;
749 case TM2_UPDATE:
750 tm2_update_block(ctx, p, i, j);
751 keyframe = 0;
752 break;
753 case TM2_STILL:
754 tm2_still_block(ctx, p, i, j);
755 keyframe = 0;
756 break;
757 case TM2_MOTION:
758 tm2_motion_block(ctx, p, i, j);
759 keyframe = 0;
760 break;
761 default:
762 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
767 /* copy data from our buffer to AVFrame */
768 Y = (ctx->cur?ctx->Y2:ctx->Y1);
769 U = (ctx->cur?ctx->U2:ctx->U1);
770 V = (ctx->cur?ctx->V2:ctx->V1);
771 dst = p->data[0];
772 for (j = 0; j < h; j++) {
773 for (i = 0; i < w; i++) {
774 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
775 dst[3*i+0] = av_clip_uint8(y + v);
776 dst[3*i+1] = av_clip_uint8(y);
777 dst[3*i+2] = av_clip_uint8(y + u);
780 /* horizontal edge extension */
781 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
782 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
784 /* vertical edge extension */
785 if (j == 0) {
786 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
787 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
788 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
789 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
790 } else if (j == h - 1) {
791 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
792 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
793 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
794 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
797 Y += ctx->y_stride;
798 if (j & 1) {
799 /* horizontal edge extension */
800 U[-2] = U[-1] = U[0];
801 V[-2] = V[-1] = V[0];
802 U[cw + 1] = U[cw] = U[cw - 1];
803 V[cw + 1] = V[cw] = V[cw - 1];
805 /* vertical edge extension */
806 if (j == 1) {
807 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
808 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
809 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
810 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
811 } else if (j == h - 1) {
812 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
813 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
814 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
815 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
818 U += ctx->uv_stride;
819 V += ctx->uv_stride;
821 dst += p->linesize[0];
824 return keyframe;
827 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
828 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
831 #define TM2_HEADER_SIZE 40
833 static int decode_frame(AVCodecContext *avctx,
834 void *data, int *got_frame,
835 AVPacket *avpkt)
837 TM2Context * const l = avctx->priv_data;
838 const uint8_t *buf = avpkt->data;
839 int buf_size = avpkt->size & ~3;
840 AVFrame * const p = &l->pic;
841 int offset = TM2_HEADER_SIZE;
842 int i, t, ret;
843 uint8_t *swbuf;
845 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
846 if (!swbuf) {
847 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
848 return AVERROR(ENOMEM);
850 p->reference = 1;
851 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
852 if ((ret = avctx->reget_buffer(avctx, p)) < 0) {
853 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
854 av_free(swbuf);
855 return ret;
858 l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
860 if ((ret = tm2_read_header(l, swbuf)) < 0) {
861 av_free(swbuf);
862 return ret;
865 for (i = 0; i < TM2_NUM_STREAMS; i++) {
866 if (offset >= buf_size) {
867 av_free(swbuf);
868 return AVERROR_INVALIDDATA;
870 t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i],
871 buf_size - offset);
872 if (t < 0) {
873 av_free(swbuf);
874 return t;
876 offset += t;
878 p->key_frame = tm2_decode_blocks(l, p);
879 if (p->key_frame)
880 p->pict_type = AV_PICTURE_TYPE_I;
881 else
882 p->pict_type = AV_PICTURE_TYPE_P;
884 l->cur = !l->cur;
885 *got_frame = 1;
886 *(AVFrame*)data = l->pic;
887 av_free(swbuf);
889 return buf_size;
892 static av_cold int decode_init(AVCodecContext *avctx)
894 TM2Context * const l = avctx->priv_data;
895 int i, w = avctx->width, h = avctx->height;
897 if ((avctx->width & 3) || (avctx->height & 3)) {
898 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
899 return AVERROR(EINVAL);
902 l->avctx = avctx;
903 l->pic.data[0] = NULL;
904 avctx->pix_fmt = AV_PIX_FMT_BGR24;
906 ff_dsputil_init(&l->dsp, avctx);
908 l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
909 l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
911 for (i = 0; i < TM2_NUM_STREAMS; i++) {
912 l->tokens[i] = NULL;
913 l->tok_lens[i] = 0;
916 w += 8;
917 h += 8;
918 l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
919 l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
920 l->y_stride = w;
921 w = (w + 1) >> 1;
922 h = (h + 1) >> 1;
923 l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
924 l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
925 l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
926 l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
927 l->uv_stride = w;
928 l->cur = 0;
929 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
930 !l->V1_base || !l->U2_base || !l->V2_base ||
931 !l->last || !l->clast) {
932 av_freep(l->Y1_base);
933 av_freep(l->Y2_base);
934 av_freep(l->U1_base);
935 av_freep(l->U2_base);
936 av_freep(l->V1_base);
937 av_freep(l->V2_base);
938 av_freep(l->last);
939 av_freep(l->clast);
940 return AVERROR(ENOMEM);
942 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
943 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
944 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
945 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
946 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
947 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
949 return 0;
952 static av_cold int decode_end(AVCodecContext *avctx)
954 TM2Context * const l = avctx->priv_data;
955 AVFrame *pic = &l->pic;
956 int i;
958 av_free(l->last);
959 av_free(l->clast);
960 for (i = 0; i < TM2_NUM_STREAMS; i++)
961 av_free(l->tokens[i]);
962 if (l->Y1) {
963 av_free(l->Y1_base);
964 av_free(l->U1_base);
965 av_free(l->V1_base);
966 av_free(l->Y2_base);
967 av_free(l->U2_base);
968 av_free(l->V2_base);
971 if (pic->data[0])
972 avctx->release_buffer(avctx, pic);
974 return 0;
977 AVCodec ff_truemotion2_decoder = {
978 .name = "truemotion2",
979 .type = AVMEDIA_TYPE_VIDEO,
980 .id = AV_CODEC_ID_TRUEMOTION2,
981 .priv_data_size = sizeof(TM2Context),
982 .init = decode_init,
983 .close = decode_end,
984 .decode = decode_frame,
985 .capabilities = CODEC_CAP_DR1,
986 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),