avprobe: also output dar/par if only defined in stream
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / truemotion2.c
blob2d7a5102c24a17a29010f7e95f78b563307d2ee6
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
34 /* Huffman-coded streams of different types of blocks */
35 enum 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 */
38 enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
39 TM2_UPDATE, TM2_STILL, TM2_MOTION};
41 typedef struct TM2Context{
42 AVCodecContext *avctx;
43 AVFrame pic;
45 GetBitContext gb;
46 DSPContext dsp;
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;
59 /* data for current and previous frame */
60 int *Y1_base, *U1_base, *V1_base, *Y2_base, *U2_base, *V2_base;
61 int *Y1, *U1, *V1, *Y2, *U2, *V2;
62 int y_stride, uv_stride;
63 int cur;
64 } TM2Context;
66 /**
67 * Huffman codes for each of streams
69 typedef struct TM2Codes{
70 VLC vlc; ///< table for Libav bitstream reader
71 int bits;
72 int *recode; ///< table for converting from code indexes to values
73 int length;
74 } TM2Codes;
76 /**
77 * structure for gathering Huffman codes information
79 typedef struct TM2Huff{
80 int val_bits; ///< length of literal
81 int max_bits; ///< maximum length of code
82 int min_bits; ///< minimum length of code
83 int nodes; ///< total number of nodes in tree
84 int num; ///< current number filled
85 int max_num; ///< total number of codes
86 int *nums; ///< literals
87 uint32_t *bits; ///< codes
88 int *lens; ///< codelengths
89 } TM2Huff;
91 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
93 if(length > huff->max_bits) {
94 av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n", huff->max_bits);
95 return -1;
98 if(!get_bits1(&ctx->gb)) { /* literal */
99 if (length == 0) {
100 length = 1;
102 if(huff->num >= huff->max_num) {
103 av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
104 return -1;
106 huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
107 huff->bits[huff->num] = prefix;
108 huff->lens[huff->num] = length;
109 huff->num++;
110 return 0;
111 } else { /* non-terminal node */
112 if(tm2_read_tree(ctx, prefix << 1, length + 1, huff) == -1)
113 return -1;
114 if(tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff) == -1)
115 return -1;
117 return 0;
120 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
122 TM2Huff huff;
123 int res = 0;
125 huff.val_bits = get_bits(&ctx->gb, 5);
126 huff.max_bits = get_bits(&ctx->gb, 5);
127 huff.min_bits = get_bits(&ctx->gb, 5);
128 huff.nodes = get_bits_long(&ctx->gb, 17);
129 huff.num = 0;
131 /* check for correct codes parameters */
132 if((huff.val_bits < 1) || (huff.val_bits > 32) ||
133 (huff.max_bits < 0) || (huff.max_bits > 25)) {
134 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal length: %i, max code length: %i\n",
135 huff.val_bits, huff.max_bits);
136 return -1;
138 if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
139 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
140 return -1;
142 /* one-node tree */
143 if(huff.max_bits == 0)
144 huff.max_bits = 1;
146 /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
147 huff.max_num = (huff.nodes + 1) >> 1;
148 huff.nums = av_mallocz(huff.max_num * sizeof(int));
149 huff.bits = av_mallocz(huff.max_num * sizeof(uint32_t));
150 huff.lens = av_mallocz(huff.max_num * sizeof(int));
152 if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
153 res = -1;
155 if(huff.num != huff.max_num) {
156 av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
157 huff.num, huff.max_num);
158 res = -1;
161 /* convert codes to vlc_table */
162 if(res != -1) {
163 int i;
165 res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
166 huff.lens, sizeof(int), sizeof(int),
167 huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
168 if(res < 0) {
169 av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
170 res = -1;
171 } else
172 res = 0;
173 if(res != -1) {
174 code->bits = huff.max_bits;
175 code->length = huff.max_num;
176 code->recode = av_malloc(code->length * sizeof(int));
177 for(i = 0; i < code->length; i++)
178 code->recode[i] = huff.nums[i];
181 /* free allocated memory */
182 av_free(huff.nums);
183 av_free(huff.bits);
184 av_free(huff.lens);
186 return res;
189 static void tm2_free_codes(TM2Codes *code)
191 av_free(code->recode);
192 if(code->vlc.table)
193 ff_free_vlc(&code->vlc);
196 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
198 int val;
199 val = get_vlc2(gb, code->vlc.table, code->bits, 1);
200 return code->recode[val];
203 #define TM2_OLD_HEADER_MAGIC 0x00000100
204 #define TM2_NEW_HEADER_MAGIC 0x00000101
206 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
208 uint32_t magic = AV_RL32(buf);
210 switch (magic) {
211 case TM2_OLD_HEADER_MAGIC:
212 av_log_missing_feature(ctx->avctx, "TM2 old header", 1);
213 return 0;
214 case TM2_NEW_HEADER_MAGIC:
215 return 0;
216 default:
217 av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
218 return AVERROR_INVALIDDATA;
222 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
223 int d, mb;
224 int i, v;
226 d = get_bits(&ctx->gb, 9);
227 mb = get_bits(&ctx->gb, 5);
229 if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
230 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
231 return -1;
234 for(i = 0; i < d; i++) {
235 v = get_bits_long(&ctx->gb, mb);
236 if(v & (1 << (mb - 1)))
237 ctx->deltas[stream_id][i] = v - (1 << mb);
238 else
239 ctx->deltas[stream_id][i] = v;
241 for(; i < TM2_DELTAS; i++)
242 ctx->deltas[stream_id][i] = 0;
244 return 0;
247 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
249 int i;
250 int skip = 0;
251 int len, toks, pos;
252 TM2Codes codes;
253 GetByteContext gb;
255 /* get stream length in dwords */
256 bytestream2_init(&gb, buf, buf_size);
257 len = bytestream2_get_be32(&gb);
258 skip = len * 4 + 4;
260 if(len == 0)
261 return 4;
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;
268 toks = bytestream2_get_be32(&gb);
269 if(toks & 1) {
270 len = bytestream2_get_be32(&gb);
271 if(len == TM2_ESCAPE) {
272 len = bytestream2_get_be32(&gb);
274 if(len > 0) {
275 pos = bytestream2_tell(&gb);
276 if (skip <= pos)
277 return -1;
278 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
279 if(tm2_read_deltas(ctx, stream_id) == -1)
280 return -1;
281 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
284 /* skip unused fields */
285 len = bytestream2_get_be32(&gb);
286 if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
287 bytestream2_skip(&gb, 8); /* unused by decoder */
288 } else {
289 bytestream2_skip(&gb, 4); /* unused by decoder */
292 pos = bytestream2_tell(&gb);
293 if (skip <= pos)
294 return -1;
295 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
296 if(tm2_build_huff_table(ctx, &codes) == -1)
297 return -1;
298 bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
300 toks >>= 1;
301 /* check if we have sane number of tokens */
302 if((toks < 0) || (toks > 0xFFFFFF)){
303 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
304 tm2_free_codes(&codes);
305 return -1;
307 ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
308 ctx->tok_lens[stream_id] = toks;
309 len = bytestream2_get_be32(&gb);
310 if(len > 0) {
311 pos = bytestream2_tell(&gb);
312 if (skip <= pos)
313 return -1;
314 init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
315 for(i = 0; i < toks; i++) {
316 if (get_bits_left(&ctx->gb) <= 0) {
317 av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
318 return -1;
320 ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
321 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
322 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
323 ctx->tokens[stream_id][i], stream_id, i);
324 return AVERROR_INVALIDDATA;
327 } else {
328 for(i = 0; i < toks; i++) {
329 ctx->tokens[stream_id][i] = codes.recode[0];
330 if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
331 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
332 ctx->tokens[stream_id][i], stream_id, i);
333 return AVERROR_INVALIDDATA;
337 tm2_free_codes(&codes);
339 return skip;
342 static inline int GET_TOK(TM2Context *ctx,int type) {
343 if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
344 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]);
345 return 0;
347 if(type <= TM2_MOT)
348 return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
349 return ctx->tokens[type][ctx->tok_ptrs[type]++];
352 /* blocks decoding routines */
354 /* common Y, U, V pointers initialisation */
355 #define TM2_INIT_POINTERS() \
356 int *last, *clast; \
357 int *Y, *U, *V;\
358 int Ystride, Ustride, Vstride;\
360 Ystride = ctx->y_stride;\
361 Vstride = ctx->uv_stride;\
362 Ustride = ctx->uv_stride;\
363 Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
364 V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
365 U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
366 last = ctx->last + bx * 4;\
367 clast = ctx->clast + bx * 4;
369 #define TM2_INIT_POINTERS_2() \
370 int *Yo, *Uo, *Vo;\
371 int oYstride, oUstride, oVstride;\
373 TM2_INIT_POINTERS();\
374 oYstride = Ystride;\
375 oVstride = Vstride;\
376 oUstride = Ustride;\
377 Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
378 Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
379 Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
381 /* recalculate last and delta values for next blocks */
382 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
383 CD[0] = CHR[1] - last[1];\
384 CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
385 last[0] = (int)CHR[stride + 0];\
386 last[1] = (int)CHR[stride + 1];}
388 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
389 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
391 int ct, d;
392 int i, j;
394 for(j = 0; j < 4; j++){
395 ct = ctx->D[j];
396 for(i = 0; i < 4; i++){
397 d = deltas[i + j * 4];
398 ct += d;
399 last[i] += ct;
400 Y[i] = av_clip_uint8(last[i]);
402 Y += stride;
403 ctx->D[j] = ct;
407 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
409 int i, j;
410 for(j = 0; j < 2; j++){
411 for(i = 0; i < 2; i++){
412 CD[j] += deltas[i + j * 2];
413 last[i] += CD[j];
414 data[i] = last[i];
416 data += stride;
420 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
422 int t;
423 int l;
424 int prev;
426 if(bx > 0)
427 prev = clast[-3];
428 else
429 prev = 0;
430 t = (CD[0] + CD[1]) >> 1;
431 l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
432 CD[1] = CD[0] + CD[1] - t;
433 CD[0] = t;
434 clast[0] = l;
436 tm2_high_chroma(data, stride, clast, CD, deltas);
439 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
441 int i;
442 int deltas[16];
443 TM2_INIT_POINTERS();
445 /* hi-res chroma */
446 for(i = 0; i < 4; i++) {
447 deltas[i] = GET_TOK(ctx, TM2_C_HI);
448 deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
450 tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
451 tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
453 /* hi-res luma */
454 for(i = 0; i < 16; i++)
455 deltas[i] = GET_TOK(ctx, TM2_L_HI);
457 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
460 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
462 int i;
463 int deltas[16];
464 TM2_INIT_POINTERS();
466 /* low-res chroma */
467 deltas[0] = GET_TOK(ctx, TM2_C_LO);
468 deltas[1] = deltas[2] = deltas[3] = 0;
469 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
471 deltas[0] = GET_TOK(ctx, TM2_C_LO);
472 deltas[1] = deltas[2] = deltas[3] = 0;
473 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
475 /* hi-res luma */
476 for(i = 0; i < 16; i++)
477 deltas[i] = GET_TOK(ctx, TM2_L_HI);
479 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
482 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
484 int i;
485 int t1, t2;
486 int deltas[16];
487 TM2_INIT_POINTERS();
489 /* low-res chroma */
490 deltas[0] = GET_TOK(ctx, TM2_C_LO);
491 deltas[1] = deltas[2] = deltas[3] = 0;
492 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
494 deltas[0] = GET_TOK(ctx, TM2_C_LO);
495 deltas[1] = deltas[2] = deltas[3] = 0;
496 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
498 /* low-res luma */
499 for(i = 0; i < 16; i++)
500 deltas[i] = 0;
502 deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
503 deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
504 deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
505 deltas[10] = GET_TOK(ctx, TM2_L_LO);
507 if(bx > 0)
508 last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
509 else
510 last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
511 last[2] = (last[1] + last[3]) >> 1;
513 t1 = ctx->D[0] + ctx->D[1];
514 ctx->D[0] = t1 >> 1;
515 ctx->D[1] = t1 - (t1 >> 1);
516 t2 = ctx->D[2] + ctx->D[3];
517 ctx->D[2] = t2 >> 1;
518 ctx->D[3] = t2 - (t2 >> 1);
520 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
523 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
525 int i;
526 int ct;
527 int left, right, diff;
528 int deltas[16];
529 TM2_INIT_POINTERS();
531 /* null chroma */
532 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
533 tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
535 deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
536 tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
538 /* null luma */
539 for(i = 0; i < 16; i++)
540 deltas[i] = 0;
542 ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
544 if(bx > 0)
545 left = last[-1] - ct;
546 else
547 left = 0;
549 right = last[3];
550 diff = right - left;
551 last[0] = left + (diff >> 2);
552 last[1] = left + (diff >> 1);
553 last[2] = right - (diff >> 2);
554 last[3] = right;
556 int tp = left;
558 ctx->D[0] = (tp + (ct >> 2)) - left;
559 left += ctx->D[0];
560 ctx->D[1] = (tp + (ct >> 1)) - left;
561 left += ctx->D[1];
562 ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
563 left += ctx->D[2];
564 ctx->D[3] = (tp + ct) - left;
566 tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
569 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
571 int i, j;
572 TM2_INIT_POINTERS_2();
574 /* update chroma */
575 for(j = 0; j < 2; j++){
576 for(i = 0; i < 2; i++){
577 U[i] = Uo[i];
578 V[i] = Vo[i];
580 U += Ustride; V += Vstride;
581 Uo += oUstride; Vo += oVstride;
583 U -= Ustride * 2;
584 V -= Vstride * 2;
585 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
586 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
588 /* update deltas */
589 ctx->D[0] = Yo[3] - last[3];
590 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
591 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
592 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
594 for(j = 0; j < 4; j++){
595 for(i = 0; i < 4; i++){
596 Y[i] = Yo[i];
597 last[i] = Yo[i];
599 Y += Ystride;
600 Yo += oYstride;
604 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
606 int i, j;
607 int d;
608 TM2_INIT_POINTERS_2();
610 /* update chroma */
611 for(j = 0; j < 2; j++){
612 for(i = 0; i < 2; i++){
613 U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
614 V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
616 U += Ustride; V += Vstride;
617 Uo += oUstride; Vo += oVstride;
619 U -= Ustride * 2;
620 V -= Vstride * 2;
621 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
622 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
624 /* update deltas */
625 ctx->D[0] = Yo[3] - last[3];
626 ctx->D[1] = Yo[3 + oYstride] - Yo[3];
627 ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
628 ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
630 for(j = 0; j < 4; j++){
631 d = last[3];
632 for(i = 0; i < 4; i++){
633 Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
634 last[i] = Y[i];
636 ctx->D[j] = last[3] - d;
637 Y += Ystride;
638 Yo += oYstride;
642 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
644 int i, j;
645 int mx, my;
646 TM2_INIT_POINTERS_2();
648 mx = GET_TOK(ctx, TM2_MOT);
649 my = GET_TOK(ctx, TM2_MOT);
650 mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
651 my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
653 Yo += my * oYstride + mx;
654 Uo += (my >> 1) * oUstride + (mx >> 1);
655 Vo += (my >> 1) * oVstride + (mx >> 1);
657 /* copy chroma */
658 for(j = 0; j < 2; j++){
659 for(i = 0; i < 2; i++){
660 U[i] = Uo[i];
661 V[i] = Vo[i];
663 U += Ustride; V += Vstride;
664 Uo += oUstride; Vo += oVstride;
666 U -= Ustride * 2;
667 V -= Vstride * 2;
668 TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
669 TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
671 /* copy luma */
672 for(j = 0; j < 4; j++){
673 for(i = 0; i < 4; i++){
674 Y[i] = Yo[i];
676 Y += Ystride;
677 Yo += oYstride;
679 /* calculate deltas */
680 Y -= Ystride * 4;
681 ctx->D[0] = Y[3] - last[3];
682 ctx->D[1] = Y[3 + Ystride] - Y[3];
683 ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
684 ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
685 for(i = 0; i < 4; i++)
686 last[i] = Y[i + Ystride * 3];
689 static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
691 int i, j;
692 int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
693 int type;
694 int keyframe = 1;
695 int *Y, *U, *V;
696 uint8_t *dst;
698 for(i = 0; i < TM2_NUM_STREAMS; i++)
699 ctx->tok_ptrs[i] = 0;
701 if (ctx->tok_lens[TM2_TYPE]<bw*bh){
702 av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
703 return -1;
706 memset(ctx->last, 0, 4 * bw * sizeof(int));
707 memset(ctx->clast, 0, 4 * bw * sizeof(int));
709 for(j = 0; j < bh; j++) {
710 memset(ctx->D, 0, 4 * sizeof(int));
711 memset(ctx->CD, 0, 4 * sizeof(int));
712 for(i = 0; i < bw; i++) {
713 type = GET_TOK(ctx, TM2_TYPE);
714 switch(type) {
715 case TM2_HI_RES:
716 tm2_hi_res_block(ctx, p, i, j);
717 break;
718 case TM2_MED_RES:
719 tm2_med_res_block(ctx, p, i, j);
720 break;
721 case TM2_LOW_RES:
722 tm2_low_res_block(ctx, p, i, j);
723 break;
724 case TM2_NULL_RES:
725 tm2_null_res_block(ctx, p, i, j);
726 break;
727 case TM2_UPDATE:
728 tm2_update_block(ctx, p, i, j);
729 keyframe = 0;
730 break;
731 case TM2_STILL:
732 tm2_still_block(ctx, p, i, j);
733 keyframe = 0;
734 break;
735 case TM2_MOTION:
736 tm2_motion_block(ctx, p, i, j);
737 keyframe = 0;
738 break;
739 default:
740 av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
745 /* copy data from our buffer to AVFrame */
746 Y = (ctx->cur?ctx->Y2:ctx->Y1);
747 U = (ctx->cur?ctx->U2:ctx->U1);
748 V = (ctx->cur?ctx->V2:ctx->V1);
749 dst = p->data[0];
750 for(j = 0; j < h; j++){
751 for(i = 0; i < w; i++){
752 int y = Y[i], u = U[i >> 1], v = V[i >> 1];
753 dst[3*i+0] = av_clip_uint8(y + v);
754 dst[3*i+1] = av_clip_uint8(y);
755 dst[3*i+2] = av_clip_uint8(y + u);
758 /* horizontal edge extension */
759 Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
760 Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
762 /* vertical edge extension */
763 if (j == 0) {
764 memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
765 memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
766 memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
767 memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
768 } else if (j == h - 1) {
769 memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
770 memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
771 memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
772 memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
775 Y += ctx->y_stride;
776 if (j & 1) {
777 /* horizontal edge extension */
778 U[-2] = U[-1] = U[0];
779 V[-2] = V[-1] = V[0];
780 U[cw + 1] = U[cw] = U[cw - 1];
781 V[cw + 1] = V[cw] = V[cw - 1];
783 /* vertical edge extension */
784 if (j == 1) {
785 memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
786 memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
787 memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
788 memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
789 } else if (j == h - 1) {
790 memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
791 memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
792 memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
793 memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
796 U += ctx->uv_stride;
797 V += ctx->uv_stride;
799 dst += p->linesize[0];
802 return keyframe;
805 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
806 TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
809 #define TM2_HEADER_SIZE 40
811 static int decode_frame(AVCodecContext *avctx,
812 void *data, int *got_frame,
813 AVPacket *avpkt)
815 const uint8_t *buf = avpkt->data;
816 int buf_size = avpkt->size & ~3;
817 TM2Context * const l = avctx->priv_data;
818 AVFrame * const p = &l->pic;
819 int i, offset = TM2_HEADER_SIZE, t, ret;
820 uint8_t *swbuf;
822 swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
823 if(!swbuf){
824 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
825 return -1;
827 p->reference = 1;
828 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
829 if(avctx->reget_buffer(avctx, p) < 0){
830 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
831 av_free(swbuf);
832 return -1;
835 l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
837 if ((ret = tm2_read_header(l, swbuf)) < 0) {
838 av_free(swbuf);
839 return ret;
842 for(i = 0; i < TM2_NUM_STREAMS; i++){
843 if (offset >= buf_size) {
844 av_free(swbuf);
845 return AVERROR_INVALIDDATA;
847 t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i],
848 buf_size - offset);
849 if(t < 0){
850 av_free(swbuf);
851 return t;
853 offset += t;
855 p->key_frame = tm2_decode_blocks(l, p);
856 if(p->key_frame)
857 p->pict_type = AV_PICTURE_TYPE_I;
858 else
859 p->pict_type = AV_PICTURE_TYPE_P;
861 l->cur = !l->cur;
862 *got_frame = 1;
863 *(AVFrame*)data = l->pic;
864 av_free(swbuf);
866 return buf_size;
869 static av_cold int decode_init(AVCodecContext *avctx){
870 TM2Context * const l = avctx->priv_data;
871 int i, w = avctx->width, h = avctx->height;
873 if((avctx->width & 3) || (avctx->height & 3)){
874 av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
875 return -1;
878 l->avctx = avctx;
879 l->pic.data[0]=NULL;
880 avctx->pix_fmt = AV_PIX_FMT_BGR24;
882 ff_dsputil_init(&l->dsp, avctx);
884 l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
885 l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
887 for(i = 0; i < TM2_NUM_STREAMS; i++) {
888 l->tokens[i] = NULL;
889 l->tok_lens[i] = 0;
892 w += 8;
893 h += 8;
894 l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
895 l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
896 l->y_stride = w;
897 w = (w + 1) >> 1;
898 h = (h + 1) >> 1;
899 l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
900 l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
901 l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
902 l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
903 l->uv_stride = w;
904 l->cur = 0;
905 if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
906 !l->V1_base || !l->U2_base || !l->V2_base ||
907 !l->last || !l->clast) {
908 av_freep(l->Y1_base);
909 av_freep(l->Y2_base);
910 av_freep(l->U1_base);
911 av_freep(l->U2_base);
912 av_freep(l->V1_base);
913 av_freep(l->V2_base);
914 av_freep(l->last);
915 av_freep(l->clast);
916 return AVERROR(ENOMEM);
918 l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
919 l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
920 l->U1 = l->U1_base + l->uv_stride * 2 + 2;
921 l->U2 = l->U2_base + l->uv_stride * 2 + 2;
922 l->V1 = l->V1_base + l->uv_stride * 2 + 2;
923 l->V2 = l->V2_base + l->uv_stride * 2 + 2;
925 return 0;
928 static av_cold int decode_end(AVCodecContext *avctx){
929 TM2Context * const l = avctx->priv_data;
930 AVFrame *pic = &l->pic;
931 int i;
933 av_free(l->last);
934 av_free(l->clast);
935 for(i = 0; i < TM2_NUM_STREAMS; i++)
936 av_free(l->tokens[i]);
937 if(l->Y1){
938 av_free(l->Y1_base);
939 av_free(l->U1_base);
940 av_free(l->V1_base);
941 av_free(l->Y2_base);
942 av_free(l->U2_base);
943 av_free(l->V2_base);
946 if (pic->data[0])
947 avctx->release_buffer(avctx, pic);
949 return 0;
952 AVCodec ff_truemotion2_decoder = {
953 .name = "truemotion2",
954 .type = AVMEDIA_TYPE_VIDEO,
955 .id = AV_CODEC_ID_TRUEMOTION2,
956 .priv_data_size = sizeof(TM2Context),
957 .init = decode_init,
958 .close = decode_end,
959 .decode = decode_frame,
960 .capabilities = CODEC_CAP_DR1,
961 .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),