truemotion2.c
Go to the documentation of this file.
1 /*
2  * Duck/ON2 TrueMotion 2 Decoder
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "get_bits.h"
30 #include "dsputil.h"
31 
32 #define TM2_ESCAPE 0x80000000
33 #define TM2_DELTAS 64
34 /* Huffman-coded streams of different types of blocks */
37 /* Block types */
40 
41 typedef struct TM2Context{
44 
47 
48  /* TM2 streams */
53  /* for blocks decoding */
54  int D[4];
55  int CD[4];
56  int *last;
57  int *clast;
58 
59  /* data for current and previous frame */
61  int *Y1, *U1, *V1, *Y2, *U2, *V2;
63  int cur;
64 } TM2Context;
65 
69 typedef struct TM2Codes{
70  VLC vlc;
71  int bits;
72  int *recode;
73  int length;
74 } TM2Codes;
75 
79 typedef struct TM2Huff{
80  int val_bits;
81  int max_bits;
82  int min_bits;
83  int nodes;
84  int num;
85  int max_num;
86  int *nums;
87  uint32_t *bits;
88  int *lens;
89 } TM2Huff;
90 
91 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
92 {
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;
96  }
97 
98  if(!get_bits1(&ctx->gb)) { /* literal */
99  if (length == 0) {
100  length = 1;
101  }
102  if(huff->num >= huff->max_num) {
103  av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
104  return -1;
105  }
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;
116  }
117  return 0;
118 }
119 
120 static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
121 {
122  TM2Huff huff;
123  int res = 0;
124 
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;
130 
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;
137  }
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;
141  }
142  /* one-node tree */
143  if(huff.max_bits == 0)
144  huff.max_bits = 1;
145 
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));
151 
152  if(tm2_read_tree(ctx, 0, 0, &huff) == -1)
153  res = -1;
154 
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;
159  }
160 
161  /* convert codes to vlc_table */
162  if(res != -1) {
163  int i;
164 
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];
179  }
180  }
181  /* free allocated memory */
182  av_free(huff.nums);
183  av_free(huff.bits);
184  av_free(huff.lens);
185 
186  return res;
187 }
188 
189 static void tm2_free_codes(TM2Codes *code)
190 {
191  av_free(code->recode);
192  if(code->vlc.table)
193  ff_free_vlc(&code->vlc);
194 }
195 
196 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
197 {
198  int val;
199  val = get_vlc2(gb, code->vlc.table, code->bits, 1);
200  return code->recode[val];
201 }
202 
203 #define TM2_OLD_HEADER_MAGIC 0x00000100
204 #define TM2_NEW_HEADER_MAGIC 0x00000101
205 
206 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
207 {
208  uint32_t magic = AV_RL32(buf);
209 
210  switch (magic) {
212  av_log_missing_feature(ctx->avctx, "TM2 old header", 1);
213  return 0;
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;
219  }
220 }
221 
222 static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
223  int d, mb;
224  int i, v;
225 
226  d = get_bits(&ctx->gb, 9);
227  mb = get_bits(&ctx->gb, 5);
228 
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;
232  }
233 
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;
240  }
241  for(; i < TM2_DELTAS; i++)
242  ctx->deltas[stream_id][i] = 0;
243 
244  return 0;
245 }
246 
247 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
248 {
249  int i;
250  int skip = 0;
251  int len, toks, pos;
252  TM2Codes codes;
253  GetByteContext gb;
254 
255  /* get stream length in dwords */
256  bytestream2_init(&gb, buf, buf_size);
257  len = bytestream2_get_be32(&gb);
258  skip = len * 4 + 4;
259 
260  if(len == 0)
261  return 4;
262 
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 
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);
273  }
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);
282  }
283  }
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 */
290  }
291 
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);
299 
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;
306  }
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;
319  }
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;
325  }
326  }
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;
334  }
335  }
336  }
337  tm2_free_codes(&codes);
338 
339  return skip;
340 }
341 
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;
346  }
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]++];
350 }
351 
352 /* blocks decoding routines */
353 
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;\
359 \
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;
368 
369 #define TM2_INIT_POINTERS_2() \
370  int *Yo, *Uo, *Vo;\
371  int oYstride, oUstride, oVstride;\
372 \
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;
380 
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];}
387 
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)
390 {
391  int ct, d;
392  int i, j;
393 
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]);
401  }
402  Y += stride;
403  ctx->D[j] = ct;
404  }
405 }
406 
407 static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
408 {
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];
415  }
416  data += stride;
417  }
418 }
419 
420 static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
421 {
422  int t;
423  int l;
424  int prev;
425 
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;
435 
436  tm2_high_chroma(data, stride, clast, CD, deltas);
437 }
438 
439 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
440 {
441  int i;
442  int deltas[16];
444 
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);
449  }
450  tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
451  tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
452 
453  /* hi-res luma */
454  for(i = 0; i < 16; i++)
455  deltas[i] = GET_TOK(ctx, TM2_L_HI);
456 
457  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
458 }
459 
460 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
461 {
462  int i;
463  int deltas[16];
465 
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);
470 
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);
474 
475  /* hi-res luma */
476  for(i = 0; i < 16; i++)
477  deltas[i] = GET_TOK(ctx, TM2_L_HI);
478 
479  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
480 }
481 
482 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
483 {
484  int i;
485  int t1, t2;
486  int deltas[16];
488 
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);
493 
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);
497 
498  /* low-res luma */
499  for(i = 0; i < 16; i++)
500  deltas[i] = 0;
501 
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);
506 
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;
512 
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);
519 
520  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
521 }
522 
523 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
524 {
525  int i;
526  int ct;
527  int left, right, diff;
528  int deltas[16];
530 
531  /* null chroma */
532  deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
533  tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
534 
535  deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
536  tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
537 
538  /* null luma */
539  for(i = 0; i < 16; i++)
540  deltas[i] = 0;
541 
542  ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
543 
544  if(bx > 0)
545  left = last[-1] - ct;
546  else
547  left = 0;
548 
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;
555  {
556  int tp = left;
557 
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;
565  }
566  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
567 }
568 
569 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
570 {
571  int i, j;
573 
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];
579  }
580  U += Ustride; V += Vstride;
581  Uo += oUstride; Vo += oVstride;
582  }
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));
587 
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];
593 
594  for(j = 0; j < 4; j++){
595  for(i = 0; i < 4; i++){
596  Y[i] = Yo[i];
597  last[i] = Yo[i];
598  }
599  Y += Ystride;
600  Yo += oYstride;
601  }
602 }
603 
604 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
605 {
606  int i, j;
607  int d;
609 
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);
615  }
616  U += Ustride; V += Vstride;
617  Uo += oUstride; Vo += oVstride;
618  }
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));
623 
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];
629 
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];
635  }
636  ctx->D[j] = last[3] - d;
637  Y += Ystride;
638  Yo += oYstride;
639  }
640 }
641 
642 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
643 {
644  int i, j;
645  int mx, my;
647 
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);
652 
653  Yo += my * oYstride + mx;
654  Uo += (my >> 1) * oUstride + (mx >> 1);
655  Vo += (my >> 1) * oVstride + (mx >> 1);
656 
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];
662  }
663  U += Ustride; V += Vstride;
664  Uo += oUstride; Vo += oVstride;
665  }
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));
670 
671  /* copy luma */
672  for(j = 0; j < 4; j++){
673  for(i = 0; i < 4; i++){
674  Y[i] = Yo[i];
675  }
676  Y += Ystride;
677  Yo += oYstride;
678  }
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];
687 }
688 
690 {
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;
697 
698  for(i = 0; i < TM2_NUM_STREAMS; i++)
699  ctx->tok_ptrs[i] = 0;
700 
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;
704  }
705 
706  memset(ctx->last, 0, 4 * bw * sizeof(int));
707  memset(ctx->clast, 0, 4 * bw * sizeof(int));
708 
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);
741  }
742  }
743  }
744 
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);
756  }
757 
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];
761 
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);
773  }
774 
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];
782 
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);
794  }
795 
796  U += ctx->uv_stride;
797  V += ctx->uv_stride;
798  }
799  dst += p->linesize[0];
800  }
801 
802  return keyframe;
803 }
804 
805 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
807 };
808 
809 #define TM2_HEADER_SIZE 40
810 
811 static int decode_frame(AVCodecContext *avctx,
812  void *data, int *got_frame,
813  AVPacket *avpkt)
814 {
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;
821 
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;
826  }
827  p->reference = 1;
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;
833  }
834 
835  l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
836 
837  if ((ret = tm2_read_header(l, swbuf)) < 0) {
838  av_free(swbuf);
839  return ret;
840  }
841 
842  for(i = 0; i < TM2_NUM_STREAMS; i++){
843  if (offset >= buf_size) {
844  av_free(swbuf);
845  return AVERROR_INVALIDDATA;
846  }
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;
852  }
853  offset += t;
854  }
855  p->key_frame = tm2_decode_blocks(l, p);
856  if(p->key_frame)
858  else
860 
861  l->cur = !l->cur;
862  *got_frame = 1;
863  *(AVFrame*)data = l->pic;
864  av_free(swbuf);
865 
866  return buf_size;
867 }
868 
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;
872 
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;
876  }
877 
878  l->avctx = avctx;
879  l->pic.data[0]=NULL;
880  avctx->pix_fmt = AV_PIX_FMT_BGR24;
881 
882  ff_dsputil_init(&l->dsp, avctx);
883 
884  l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
885  l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
886 
887  for(i = 0; i < TM2_NUM_STREAMS; i++) {
888  l->tokens[i] = NULL;
889  l->tok_lens[i] = 0;
890  }
891 
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);
917  }
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;
924 
925  return 0;
926 }
927 
928 static av_cold int decode_end(AVCodecContext *avctx){
929  TM2Context * const l = avctx->priv_data;
930  AVFrame *pic = &l->pic;
931  int i;
932 
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);
944  }
945 
946  if (pic->data[0])
947  avctx->release_buffer(avctx, pic);
948 
949  return 0;
950 }
951 
953  .name = "truemotion2",
954  .type = AVMEDIA_TYPE_VIDEO,
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"),
962 };
static void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
Definition: truemotion2.c:420
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
struct TM2Huff TM2Huff
structure for gathering Huffman codes information
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
int * V2
Definition: truemotion2.c:61
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int * U1_base
Definition: truemotion2.c:60
int D[4]
Definition: truemotion2.c:54
int * last
Definition: truemotion2.c:56
int * recode
table for converting from code indexes to values
Definition: truemotion2.c:72
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
AVCodec ff_truemotion2_decoder
Definition: truemotion2.c:952
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
int num
current number filled
Definition: truemotion2.c:84
static int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
Definition: truemotion2.c:206
#define TM2_HEADER_SIZE
Definition: truemotion2.c:809
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
structure for gathering Huffman codes information
Definition: truemotion2.c:79
uint8_t
TM2_STREAMS
Definition: truemotion2.c:35
static const int tm2_stream_order[TM2_NUM_STREAMS]
Definition: truemotion2.c:805
AVCodecContext * avctx
Definition: truemotion2.c:42
int max_bits
maximum length of code
Definition: truemotion2.c:81
static int tm2_read_deltas(TM2Context *ctx, int stream_id)
Definition: truemotion2.c:222
const char data[16]
Definition: mxf.c:66
static void tm2_free_codes(TM2Codes *code)
Definition: truemotion2.c:189
uint8_t * data
Definition: avcodec.h:915
DSPContext dsp
Definition: truemotion2.c:46
int min_bits
minimum length of code
Definition: truemotion2.c:82
int * U2
Definition: truemotion2.c:61
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
Definition: truemotion2.c:91
VLC vlc
table for Libav bitstream reader
Definition: truemotion2.c:70
bitstream reader API header.
static void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:439
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
Definition: truemotion2.c:247
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:604
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int val_bits
length of literal
Definition: truemotion2.c:80
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: dsputil.h:343
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
Definition: truemotion2.c:120
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
static void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:482
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
Definition: truemotion2.c:689
int * tokens[TM2_NUM_STREAMS]
Definition: truemotion2.c:49
#define t1
Definition: regdef.h:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int * clast
Definition: truemotion2.c:57
Definition: get_bits.h:63
Definition: vf_drawbox.c:36
int * V1
Definition: truemotion2.c:61
int * V2_base
Definition: truemotion2.c:60
struct TM2Codes TM2Codes
Huffman codes for each of streams.
int max_num
total number of codes
Definition: truemotion2.c:85
int bits
Definition: truemotion2.c:71
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
static int GET_TOK(TM2Context *ctx, int type)
Definition: truemotion2.c:342
int width
picture width / height.
Definition: avcodec.h:1508
#define TM2_OLD_HEADER_MAGIC
Definition: truemotion2.c:203
int uv_stride
Definition: truemotion2.c:62
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:515
#define AV_RL32
Definition: intreadwrite.h:146
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static int tm2_get_token(GetBitContext *gb, TM2Codes *code)
Definition: truemotion2.c:196
int CD[4]
Definition: truemotion2.c:55
int * nums
literals
Definition: truemotion2.c:86
int nodes
total number of nodes in tree
Definition: truemotion2.c:83
static av_cold int decode_end(AVCodecContext *avctx)
Definition: truemotion2.c:928
int * Y1
Definition: truemotion2.c:61
int * V1_base
Definition: truemotion2.c:60
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
#define TM2_INIT_POINTERS_2()
Definition: truemotion2.c:369
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:569
AVFrame pic
Definition: truemotion2.c:43
#define TM2_DELTAS
Definition: truemotion2.c:33
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:418
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
#define TM2_INIT_POINTERS()
Definition: truemotion2.c:355
int * Y1_base
Definition: truemotion2.c:60
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.
Definition: utils.c:2007
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
static void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:460
static void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:642
uint32_t * bits
codes
Definition: truemotion2.c:87
int length
Definition: truemotion2.c:73
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
static void tm2_apply_deltas(TM2Context *ctx, int *Y, int stride, int *deltas, int *last)
Definition: truemotion2.c:389
#define TM2_ESCAPE
Definition: truemotion2.c:32
#define TM2_RECALC_BLOCK(CHR, stride, last, CD)
Definition: truemotion2.c:382
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int * U1
Definition: truemotion2.c:61
Definition: vf_drawbox.c:36
int * Y2
Definition: truemotion2.c:61
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
int y_stride
Definition: truemotion2.c:62
static void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:523
int deltas[TM2_NUM_STREAMS][TM2_DELTAS]
Definition: truemotion2.c:52
int * lens
codelengths
Definition: truemotion2.c:88
static av_cold int decode_init(AVCodecContext *avctx)
Definition: truemotion2.c:869
int * Y2_base
Definition: truemotion2.c:60
DSP utils.
#define TM2_NEW_HEADER_MAGIC
Definition: truemotion2.c:204
void * priv_data
Definition: avcodec.h:1382
int * U2_base
Definition: truemotion2.c:60
TM2_BLOCKS
Definition: truemotion2.c:38
GetBitContext gb
Definition: truemotion2.c:45
int len
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int tok_lens[TM2_NUM_STREAMS]
Definition: truemotion2.c:50
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
Definition: truemotion2.c:407
struct TM2Context TM2Context
Huffman codes for each of streams.
Definition: truemotion2.c:69
int tok_ptrs[TM2_NUM_STREAMS]
Definition: truemotion2.c:51
This structure stores compressed data.
Definition: avcodec.h:898
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:322
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
#define t2
Definition: regdef.h:30
Predicted.
Definition: avutil.h:246
DSPContext.
Definition: dsputil.h:194
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: truemotion2.c:811
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)