FFmpeg  3.4.9
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Duck TrueMotion2 decoder.
25  */
26 
27 #include <inttypes.h>
28 
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 
35 #define TM2_ESCAPE 0x80000000
36 #define TM2_DELTAS 64
37 
38 /* Huffman-coded streams of different types of blocks */
40  TM2_C_HI = 0,
48 };
49 
50 /* Block types */
51 enum TM2_BLOCKS {
59 };
60 
61 typedef struct TM2Context {
64 
66  int error;
68 
71 
72  /* TM2 streams */
77  /* for blocks decoding */
78  int D[4];
79  int CD[4];
80  int *last;
81  int *clast;
82 
83  /* data for current and previous frame */
85  int *Y1, *U1, *V1, *Y2, *U2, *V2;
87  int cur;
88 } TM2Context;
89 
90 /**
91 * Huffman codes for each of streams
92 */
93 typedef struct TM2Codes {
94  VLC vlc; ///< table for FFmpeg bitstream reader
95  int bits;
96  int *recode; ///< table for converting from code indexes to values
97  int length;
98 } TM2Codes;
99 
100 /**
101 * structure for gathering Huffman codes information
102 */
103 typedef struct TM2Huff {
104  int val_bits; ///< length of literal
105  int max_bits; ///< maximum length of code
106  int min_bits; ///< minimum length of code
107  int nodes; ///< total number of nodes in tree
108  int num; ///< current number filled
109  int max_num; ///< total number of codes
110  int *nums; ///< literals
111  uint32_t *bits; ///< codes
112  int *lens; ///< codelengths
113 } TM2Huff;
114 
115 static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
116 {
117  int ret;
118  if (length > huff->max_bits) {
119  av_log(ctx->avctx, AV_LOG_ERROR, "Tree exceeded its given depth (%i)\n",
120  huff->max_bits);
121  return AVERROR_INVALIDDATA;
122  }
123 
124  if (!get_bits1(&ctx->gb)) { /* literal */
125  if (length == 0) {
126  length = 1;
127  }
128  if (huff->num >= huff->max_num) {
129  av_log(ctx->avctx, AV_LOG_DEBUG, "Too many literals\n");
130  return AVERROR_INVALIDDATA;
131  }
132  huff->nums[huff->num] = get_bits_long(&ctx->gb, huff->val_bits);
133  huff->bits[huff->num] = prefix;
134  huff->lens[huff->num] = length;
135  huff->num++;
136  return 0;
137  } else { /* non-terminal node */
138  if ((ret = tm2_read_tree(ctx, prefix << 1, length + 1, huff)) < 0)
139  return ret;
140  if ((ret = tm2_read_tree(ctx, (prefix << 1) | 1, length + 1, huff)) < 0)
141  return ret;
142  }
143  return 0;
144 }
145 
147 {
148  TM2Huff huff;
149  int res = 0;
150 
151  huff.val_bits = get_bits(&ctx->gb, 5);
152  huff.max_bits = get_bits(&ctx->gb, 5);
153  huff.min_bits = get_bits(&ctx->gb, 5);
154  huff.nodes = get_bits_long(&ctx->gb, 17);
155  huff.num = 0;
156 
157  /* check for correct codes parameters */
158  if ((huff.val_bits < 1) || (huff.val_bits > 32) ||
159  (huff.max_bits < 0) || (huff.max_bits > 25)) {
160  av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect tree parameters - literal "
161  "length: %i, max code length: %i\n", huff.val_bits, huff.max_bits);
162  return AVERROR_INVALIDDATA;
163  }
164  if ((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
165  av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree "
166  "nodes: %i\n", huff.nodes);
167  return AVERROR_INVALIDDATA;
168  }
169  /* one-node tree */
170  if (huff.max_bits == 0)
171  huff.max_bits = 1;
172 
173  /* allocate space for codes - it is exactly ceil(nodes / 2) entries */
174  huff.max_num = (huff.nodes + 1) >> 1;
175  huff.nums = av_calloc(huff.max_num, sizeof(int));
176  huff.bits = av_calloc(huff.max_num, sizeof(uint32_t));
177  huff.lens = av_calloc(huff.max_num, sizeof(int));
178 
179  if (!huff.nums || !huff.bits || !huff.lens) {
180  res = AVERROR(ENOMEM);
181  goto out;
182  }
183 
184  res = tm2_read_tree(ctx, 0, 0, &huff);
185 
186  if (huff.num != huff.max_num) {
187  av_log(ctx->avctx, AV_LOG_ERROR, "Got less codes than expected: %i of %i\n",
188  huff.num, huff.max_num);
189  res = AVERROR_INVALIDDATA;
190  }
191 
192  /* convert codes to vlc_table */
193  if (res >= 0) {
194  int i;
195 
196  res = init_vlc(&code->vlc, huff.max_bits, huff.max_num,
197  huff.lens, sizeof(int), sizeof(int),
198  huff.bits, sizeof(uint32_t), sizeof(uint32_t), 0);
199  if (res < 0)
200  av_log(ctx->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
201  else {
202  code->bits = huff.max_bits;
203  code->length = huff.max_num;
204  code->recode = av_malloc_array(code->length, sizeof(int));
205  if (!code->recode) {
206  res = AVERROR(ENOMEM);
207  goto out;
208  }
209  for (i = 0; i < code->length; i++)
210  code->recode[i] = huff.nums[i];
211  }
212  }
213 
214 out:
215  /* free allocated memory */
216  av_free(huff.nums);
217  av_free(huff.bits);
218  av_free(huff.lens);
219 
220  return res;
221 }
222 
223 static void tm2_free_codes(TM2Codes *code)
224 {
225  av_free(code->recode);
226  if (code->vlc.table)
227  ff_free_vlc(&code->vlc);
228 }
229 
230 static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
231 {
232  int val;
233  val = get_vlc2(gb, code->vlc.table, code->bits, 1);
234  if(val<0)
235  return -1;
236  return code->recode[val];
237 }
238 
239 #define TM2_OLD_HEADER_MAGIC 0x00000100
240 #define TM2_NEW_HEADER_MAGIC 0x00000101
241 
242 static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
243 {
244  uint32_t magic = AV_RL32(buf);
245 
246  switch (magic) {
248  avpriv_request_sample(ctx->avctx, "Old TM2 header");
249  return 0;
251  return 0;
252  default:
253  av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
254  magic);
255  return AVERROR_INVALIDDATA;
256  }
257 }
258 
259 static int tm2_read_deltas(TM2Context *ctx, int stream_id)
260 {
261  int d, mb;
262  int i, v;
263 
264  d = get_bits(&ctx->gb, 9);
265  mb = get_bits(&ctx->gb, 5);
266 
267  av_assert2(mb < 32);
268  if ((d < 1) || (d > TM2_DELTAS) || (mb < 1)) {
269  av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
270  return AVERROR_INVALIDDATA;
271  }
272 
273  for (i = 0; i < d; i++) {
274  v = get_bits_long(&ctx->gb, mb);
275  if (v & (1 << (mb - 1)))
276  ctx->deltas[stream_id][i] = v - (1U << mb);
277  else
278  ctx->deltas[stream_id][i] = v;
279  }
280  for (; i < TM2_DELTAS; i++)
281  ctx->deltas[stream_id][i] = 0;
282 
283  return 0;
284 }
285 
286 static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
287 {
288  int i, ret;
289  int skip = 0;
290  int len, toks, pos;
291  TM2Codes codes;
293 
294  if (buf_size < 4) {
295  av_log(ctx->avctx, AV_LOG_ERROR, "not enough space for len left\n");
296  return AVERROR_INVALIDDATA;
297  }
298 
299  /* get stream length in dwords */
300  bytestream2_init(&gb, buf, buf_size);
301  len = bytestream2_get_be32(&gb);
302 
303  if (len == 0)
304  return 4;
305 
306  if (len >= INT_MAX / 4 - 1 || len < 0 || len * 4 + 4 > buf_size) {
307  av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
308  return AVERROR_INVALIDDATA;
309  }
310  skip = len * 4 + 4;
311 
312  toks = bytestream2_get_be32(&gb);
313  if (toks & 1) {
314  len = bytestream2_get_be32(&gb);
315  if (len == TM2_ESCAPE) {
316  len = bytestream2_get_be32(&gb);
317  }
318  if (len > 0) {
319  pos = bytestream2_tell(&gb);
320  if (skip <= pos)
321  return AVERROR_INVALIDDATA;
322  init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
323  if ((ret = tm2_read_deltas(ctx, stream_id)) < 0)
324  return ret;
325  bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
326  }
327  }
328  /* skip unused fields */
329  len = bytestream2_get_be32(&gb);
330  if (len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
331  bytestream2_skip(&gb, 8); /* unused by decoder */
332  } else {
333  bytestream2_skip(&gb, 4); /* unused by decoder */
334  }
335 
336  pos = bytestream2_tell(&gb);
337  if (skip <= pos)
338  return AVERROR_INVALIDDATA;
339  init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
340  if ((ret = tm2_build_huff_table(ctx, &codes)) < 0)
341  return ret;
342  bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
343 
344  toks >>= 1;
345  /* check if we have sane number of tokens */
346  if ((toks < 0) || (toks > 0xFFFFFF)) {
347  av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
348  ret = AVERROR_INVALIDDATA;
349  goto end;
350  }
351  ret = av_reallocp_array(&ctx->tokens[stream_id], toks, sizeof(int));
352  if (ret < 0) {
353  ctx->tok_lens[stream_id] = 0;
354  goto end;
355  }
356  ctx->tok_lens[stream_id] = toks;
357  len = bytestream2_get_be32(&gb);
358  if (len > 0) {
359  pos = bytestream2_tell(&gb);
360  if (skip <= pos) {
361  ret = AVERROR_INVALIDDATA;
362  goto end;
363  }
364  init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
365  for (i = 0; i < toks; i++) {
366  if (get_bits_left(&ctx->gb) <= 0) {
367  av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
368  ret = AVERROR_INVALIDDATA;
369  goto end;
370  }
371  ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
372  if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS || ctx->tokens[stream_id][i]<0) {
373  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
374  ctx->tokens[stream_id][i], stream_id, i);
375  ret = AVERROR_INVALIDDATA;
376  goto end;
377  }
378  }
379  } else {
380  for (i = 0; i < toks; i++) {
381  ctx->tokens[stream_id][i] = codes.recode[0];
382  if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
383  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
384  ctx->tokens[stream_id][i], stream_id, i);
385  ret = AVERROR_INVALIDDATA;
386  goto end;
387  }
388  }
389  }
390 
391  ret = skip;
392 
393 end:
394  tm2_free_codes(&codes);
395  return ret;
396 }
397 
398 static inline int GET_TOK(TM2Context *ctx,int type)
399 {
400  if (ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
401  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]);
402  ctx->error = 1;
403  return 0;
404  }
405  if (type <= TM2_MOT) {
406  if (ctx->tokens[type][ctx->tok_ptrs[type]] >= TM2_DELTAS) {
407  av_log(ctx->avctx, AV_LOG_ERROR, "token %d is too large\n", ctx->tokens[type][ctx->tok_ptrs[type]]);
408  return 0;
409  }
410  return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
411  }
412  return ctx->tokens[type][ctx->tok_ptrs[type]++];
413 }
414 
415 /* blocks decoding routines */
416 
417 /* common Y, U, V pointers initialisation */
418 #define TM2_INIT_POINTERS() \
419  int *last, *clast; \
420  int *Y, *U, *V;\
421  int Ystride, Ustride, Vstride;\
422 \
423  Ystride = ctx->y_stride;\
424  Vstride = ctx->uv_stride;\
425  Ustride = ctx->uv_stride;\
426  Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
427  V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
428  U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
429  last = ctx->last + bx * 4;\
430  clast = ctx->clast + bx * 4;
431 
432 #define TM2_INIT_POINTERS_2() \
433  int *Yo, *Uo, *Vo;\
434  int oYstride, oUstride, oVstride;\
435 \
436  TM2_INIT_POINTERS();\
437  oYstride = Ystride;\
438  oVstride = Vstride;\
439  oUstride = Ustride;\
440  Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
441  Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
442  Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
443 
444 /* recalculate last and delta values for next blocks */
445 #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
446  CD[0] = (unsigned)CHR[ 1] - (unsigned)last[1];\
447  CD[1] = (unsigned)CHR[stride + 1] - (unsigned) CHR[1];\
448  last[0] = (int)CHR[stride + 0];\
449  last[1] = (int)CHR[stride + 1];}
450 
451 /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
452 static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
453 {
454  unsigned ct, d;
455  int i, j;
456 
457  for (j = 0; j < 4; j++){
458  ct = ctx->D[j];
459  for (i = 0; i < 4; i++){
460  d = deltas[i + j * 4];
461  ct += d;
462  last[i] += ct;
463  Y[i] = av_clip_uint8(last[i]);
464  }
465  Y += stride;
466  ctx->D[j] = ct;
467  }
468 }
469 
470 static inline void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
471 {
472  int i, j;
473  for (j = 0; j < 2; j++) {
474  for (i = 0; i < 2; i++) {
475  CD[j] += deltas[i + j * 2];
476  last[i] += CD[j];
477  data[i] = last[i];
478  }
479  data += stride;
480  }
481 }
482 
483 static inline void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
484 {
485  int t;
486  int l;
487  int prev;
488 
489  if (bx > 0)
490  prev = clast[-3];
491  else
492  prev = 0;
493  t = (int)(CD[0] + CD[1]) >> 1;
494  l = (int)(prev - CD[0] - CD[1] + clast[1]) >> 1;
495  CD[1] = CD[0] + CD[1] - t;
496  CD[0] = t;
497  clast[0] = l;
498 
499  tm2_high_chroma(data, stride, clast, CD, deltas);
500 }
501 
502 static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
503 {
504  int i;
505  int deltas[16];
507 
508  /* hi-res chroma */
509  for (i = 0; i < 4; i++) {
510  deltas[i] = GET_TOK(ctx, TM2_C_HI);
511  deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
512  }
513  tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
514  tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
515 
516  /* hi-res luma */
517  for (i = 0; i < 16; i++)
518  deltas[i] = GET_TOK(ctx, TM2_L_HI);
519 
520  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
521 }
522 
523 static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
524 {
525  int i;
526  int deltas[16];
528 
529  /* low-res chroma */
530  deltas[0] = GET_TOK(ctx, TM2_C_LO);
531  deltas[1] = deltas[2] = deltas[3] = 0;
532  tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
533 
534  deltas[0] = GET_TOK(ctx, TM2_C_LO);
535  deltas[1] = deltas[2] = deltas[3] = 0;
536  tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
537 
538  /* hi-res luma */
539  for (i = 0; i < 16; i++)
540  deltas[i] = GET_TOK(ctx, TM2_L_HI);
541 
542  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
543 }
544 
545 static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
546 {
547  int i;
548  int t1, t2;
549  int deltas[16];
551 
552  /* low-res chroma */
553  deltas[0] = GET_TOK(ctx, TM2_C_LO);
554  deltas[1] = deltas[2] = deltas[3] = 0;
555  tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
556 
557  deltas[0] = GET_TOK(ctx, TM2_C_LO);
558  deltas[1] = deltas[2] = deltas[3] = 0;
559  tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
560 
561  /* low-res luma */
562  for (i = 0; i < 16; i++)
563  deltas[i] = 0;
564 
565  deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
566  deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
567  deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
568  deltas[10] = GET_TOK(ctx, TM2_L_LO);
569 
570  if (bx > 0)
571  last[0] = (int)((unsigned)last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
572  else
573  last[0] = (int)((unsigned)last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
574  last[2] = (int)((unsigned)last[1] + last[3]) >> 1;
575 
576  t1 = ctx->D[0] + (unsigned)ctx->D[1];
577  ctx->D[0] = t1 >> 1;
578  ctx->D[1] = t1 - (t1 >> 1);
579  t2 = ctx->D[2] + (unsigned)ctx->D[3];
580  ctx->D[2] = t2 >> 1;
581  ctx->D[3] = t2 - (t2 >> 1);
582 
583  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
584 }
585 
586 static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
587 {
588  int i;
589  int ct;
590  unsigned left, right;
591  int diff;
592  int deltas[16];
594 
595  /* null chroma */
596  deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
597  tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
598 
599  deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
600  tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
601 
602  /* null luma */
603  for (i = 0; i < 16; i++)
604  deltas[i] = 0;
605 
606  ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
607 
608  if (bx > 0)
609  left = last[-1] - (unsigned)ct;
610  else
611  left = 0;
612 
613  right = last[3];
614  diff = right - left;
615  last[0] = left + (diff >> 2);
616  last[1] = left + (diff >> 1);
617  last[2] = right - (diff >> 2);
618  last[3] = right;
619  {
620  unsigned tp = left;
621 
622  ctx->D[0] = (tp + (ct >> 2)) - left;
623  left += ctx->D[0];
624  ctx->D[1] = (tp + (ct >> 1)) - left;
625  left += ctx->D[1];
626  ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
627  left += ctx->D[2];
628  ctx->D[3] = (tp + ct) - left;
629  }
630  tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
631 }
632 
633 static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
634 {
635  int i, j;
637 
638  /* update chroma */
639  for (j = 0; j < 2; j++) {
640  for (i = 0; i < 2; i++){
641  U[i] = Uo[i];
642  V[i] = Vo[i];
643  }
644  U += Ustride; V += Vstride;
645  Uo += oUstride; Vo += oVstride;
646  }
647  U -= Ustride * 2;
648  V -= Vstride * 2;
649  TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
650  TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
651 
652  /* update deltas */
653  ctx->D[0] = Yo[3] - last[3];
654  ctx->D[1] = Yo[3 + oYstride] - Yo[3];
655  ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
656  ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
657 
658  for (j = 0; j < 4; j++) {
659  for (i = 0; i < 4; i++) {
660  Y[i] = Yo[i];
661  last[i] = Yo[i];
662  }
663  Y += Ystride;
664  Yo += oYstride;
665  }
666 }
667 
668 static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
669 {
670  int i, j;
671  unsigned d;
673 
674  /* update chroma */
675  for (j = 0; j < 2; j++) {
676  for (i = 0; i < 2; i++) {
677  U[i] = Uo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
678  V[i] = Vo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
679  }
680  U += Ustride;
681  V += Vstride;
682  Uo += oUstride;
683  Vo += oVstride;
684  }
685  U -= Ustride * 2;
686  V -= Vstride * 2;
687  TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
688  TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
689 
690  /* update deltas */
691  ctx->D[0] = (unsigned)Yo[3] - last[3];
692  ctx->D[1] = (unsigned)Yo[3 + oYstride] - Yo[3];
693  ctx->D[2] = (unsigned)Yo[3 + oYstride * 2] - Yo[3 + oYstride];
694  ctx->D[3] = (unsigned)Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
695 
696  for (j = 0; j < 4; j++) {
697  d = last[3];
698  for (i = 0; i < 4; i++) {
699  Y[i] = Yo[i] + (unsigned)GET_TOK(ctx, TM2_UPD);
700  last[i] = Y[i];
701  }
702  ctx->D[j] = last[3] - d;
703  Y += Ystride;
704  Yo += oYstride;
705  }
706 }
707 
708 static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
709 {
710  int i, j;
711  int mx, my;
713 
714  mx = GET_TOK(ctx, TM2_MOT);
715  my = GET_TOK(ctx, TM2_MOT);
716  mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
717  my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
718 
719  if (4*bx+mx<0 || 4*by+my<0 || 4*bx+mx+4 > ctx->avctx->width || 4*by+my+4 > ctx->avctx->height) {
720  av_log(ctx->avctx, AV_LOG_ERROR, "MV out of picture\n");
721  return;
722  }
723 
724  Yo += my * oYstride + mx;
725  Uo += (my >> 1) * oUstride + (mx >> 1);
726  Vo += (my >> 1) * oVstride + (mx >> 1);
727 
728  /* copy chroma */
729  for (j = 0; j < 2; j++) {
730  for (i = 0; i < 2; i++) {
731  U[i] = Uo[i];
732  V[i] = Vo[i];
733  }
734  U += Ustride;
735  V += Vstride;
736  Uo += oUstride;
737  Vo += oVstride;
738  }
739  U -= Ustride * 2;
740  V -= Vstride * 2;
741  TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
742  TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
743 
744  /* copy luma */
745  for (j = 0; j < 4; j++) {
746  for (i = 0; i < 4; i++) {
747  Y[i] = Yo[i];
748  }
749  Y += Ystride;
750  Yo += oYstride;
751  }
752  /* calculate deltas */
753  Y -= Ystride * 4;
754  ctx->D[0] = (unsigned)Y[3] - last[3];
755  ctx->D[1] = (unsigned)Y[3 + Ystride] - Y[3];
756  ctx->D[2] = (unsigned)Y[3 + Ystride * 2] - Y[3 + Ystride];
757  ctx->D[3] = (unsigned)Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
758  for (i = 0; i < 4; i++)
759  last[i] = Y[i + Ystride * 3];
760 }
761 
763 {
764  int i, j;
765  int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
766  int type;
767  int keyframe = 1;
768  int *Y, *U, *V;
769  uint8_t *dst;
770 
771  for (i = 0; i < TM2_NUM_STREAMS; i++)
772  ctx->tok_ptrs[i] = 0;
773 
774  if (ctx->tok_lens[TM2_TYPE]<bw*bh) {
775  av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
776  return AVERROR_INVALIDDATA;
777  }
778 
779  memset(ctx->last, 0, 4 * bw * sizeof(int));
780  memset(ctx->clast, 0, 4 * bw * sizeof(int));
781 
782  for (j = 0; j < bh; j++) {
783  memset(ctx->D, 0, 4 * sizeof(int));
784  memset(ctx->CD, 0, 4 * sizeof(int));
785  for (i = 0; i < bw; i++) {
786  type = GET_TOK(ctx, TM2_TYPE);
787  switch(type) {
788  case TM2_HI_RES:
789  tm2_hi_res_block(ctx, p, i, j);
790  break;
791  case TM2_MED_RES:
792  tm2_med_res_block(ctx, p, i, j);
793  break;
794  case TM2_LOW_RES:
795  tm2_low_res_block(ctx, p, i, j);
796  break;
797  case TM2_NULL_RES:
798  tm2_null_res_block(ctx, p, i, j);
799  break;
800  case TM2_UPDATE:
801  tm2_update_block(ctx, p, i, j);
802  keyframe = 0;
803  break;
804  case TM2_STILL:
805  tm2_still_block(ctx, p, i, j);
806  keyframe = 0;
807  break;
808  case TM2_MOTION:
809  tm2_motion_block(ctx, p, i, j);
810  keyframe = 0;
811  break;
812  default:
813  av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
814  }
815  if (ctx->error)
816  return AVERROR_INVALIDDATA;
817  }
818  }
819 
820  /* copy data from our buffer to AVFrame */
821  Y = (ctx->cur?ctx->Y2:ctx->Y1);
822  U = (ctx->cur?ctx->U2:ctx->U1);
823  V = (ctx->cur?ctx->V2:ctx->V1);
824  dst = p->data[0];
825  for (j = 0; j < h; j++) {
826  for (i = 0; i < w; i++) {
827  unsigned y = Y[i], u = U[i >> 1], v = V[i >> 1];
828  dst[3*i+0] = av_clip_uint8(y + v);
829  dst[3*i+1] = av_clip_uint8(y);
830  dst[3*i+2] = av_clip_uint8(y + u);
831  }
832 
833  /* horizontal edge extension */
834  Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
835  Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
836 
837  /* vertical edge extension */
838  if (j == 0) {
839  memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
840  memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
841  memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
842  memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
843  } else if (j == h - 1) {
844  memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
845  memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
846  memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
847  memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
848  }
849 
850  Y += ctx->y_stride;
851  if (j & 1) {
852  /* horizontal edge extension */
853  U[-2] = U[-1] = U[0];
854  V[-2] = V[-1] = V[0];
855  U[cw + 1] = U[cw] = U[cw - 1];
856  V[cw + 1] = V[cw] = V[cw - 1];
857 
858  /* vertical edge extension */
859  if (j == 1) {
860  memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
861  memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
862  memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
863  memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
864  } else if (j == h - 1) {
865  memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
866  memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
867  memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
868  memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
869  }
870 
871  U += ctx->uv_stride;
872  V += ctx->uv_stride;
873  }
874  dst += p->linesize[0];
875  }
876 
877  return keyframe;
878 }
879 
880 static const int tm2_stream_order[TM2_NUM_STREAMS] = {
882 };
883 
884 #define TM2_HEADER_SIZE 40
885 
887  void *data, int *got_frame,
888  AVPacket *avpkt)
889 {
890  TM2Context * const l = avctx->priv_data;
891  const uint8_t *buf = avpkt->data;
892  int buf_size = avpkt->size & ~3;
893  AVFrame * const p = l->pic;
894  int offset = TM2_HEADER_SIZE;
895  int i, t, ret;
896 
897  l->error = 0;
898 
899  av_fast_padded_malloc(&l->buffer, &l->buffer_size, buf_size);
900  if (!l->buffer) {
901  av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
902  return AVERROR(ENOMEM);
903  }
904 
905  if ((ret = ff_reget_buffer(avctx, p)) < 0)
906  return ret;
907 
908  l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
909  buf_size >> 2);
910 
911  if ((ret = tm2_read_header(l, l->buffer)) < 0) {
912  return ret;
913  }
914 
915  for (i = 0; i < TM2_NUM_STREAMS; i++) {
916  if (offset >= buf_size) {
917  av_log(avctx, AV_LOG_ERROR, "no space for tm2_read_stream\n");
918  return AVERROR_INVALIDDATA;
919  }
920 
921  t = tm2_read_stream(l, l->buffer + offset, tm2_stream_order[i],
922  buf_size - offset);
923  if (t < 0) {
924  int j = tm2_stream_order[i];
925  if (l->tok_lens[j])
926  memset(l->tokens[j], 0, sizeof(**l->tokens) * l->tok_lens[j]);
927  return t;
928  }
929  offset += t;
930  }
931  p->key_frame = tm2_decode_blocks(l, p);
932  if (p->key_frame)
934  else
936 
937  l->cur = !l->cur;
938  *got_frame = 1;
939  ret = av_frame_ref(data, l->pic);
940 
941  return (ret < 0) ? ret : buf_size;
942 }
943 
945 {
946  TM2Context * const l = avctx->priv_data;
947  int i, w = avctx->width, h = avctx->height;
948 
949  if ((avctx->width & 3) || (avctx->height & 3)) {
950  av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
951  return AVERROR(EINVAL);
952  }
953 
954  l->avctx = avctx;
955  avctx->pix_fmt = AV_PIX_FMT_BGR24;
956 
957  l->pic = av_frame_alloc();
958  if (!l->pic)
959  return AVERROR(ENOMEM);
960 
961  ff_bswapdsp_init(&l->bdsp);
962 
963  l->last = av_malloc_array(w >> 2, 4 * sizeof(*l->last) );
964  l->clast = av_malloc_array(w >> 2, 4 * sizeof(*l->clast));
965 
966  for (i = 0; i < TM2_NUM_STREAMS; i++) {
967  l->tokens[i] = NULL;
968  l->tok_lens[i] = 0;
969  }
970 
971  w += 8;
972  h += 8;
973  l->Y1_base = av_calloc(w * h, sizeof(*l->Y1_base));
974  l->Y2_base = av_calloc(w * h, sizeof(*l->Y2_base));
975  l->y_stride = w;
976  w = (w + 1) >> 1;
977  h = (h + 1) >> 1;
978  l->U1_base = av_calloc(w * h, sizeof(*l->U1_base));
979  l->V1_base = av_calloc(w * h, sizeof(*l->V1_base));
980  l->U2_base = av_calloc(w * h, sizeof(*l->U2_base));
981  l->V2_base = av_calloc(w * h, sizeof(*l->V1_base));
982  l->uv_stride = w;
983  l->cur = 0;
984  if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
985  !l->V1_base || !l->U2_base || !l->V2_base ||
986  !l->last || !l->clast) {
987  av_freep(&l->Y1_base);
988  av_freep(&l->Y2_base);
989  av_freep(&l->U1_base);
990  av_freep(&l->U2_base);
991  av_freep(&l->V1_base);
992  av_freep(&l->V2_base);
993  av_freep(&l->last);
994  av_freep(&l->clast);
995  av_frame_free(&l->pic);
996  return AVERROR(ENOMEM);
997  }
998  l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
999  l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
1000  l->U1 = l->U1_base + l->uv_stride * 2 + 2;
1001  l->U2 = l->U2_base + l->uv_stride * 2 + 2;
1002  l->V1 = l->V1_base + l->uv_stride * 2 + 2;
1003  l->V2 = l->V2_base + l->uv_stride * 2 + 2;
1004 
1005  return 0;
1006 }
1007 
1009 {
1010  TM2Context * const l = avctx->priv_data;
1011  int i;
1012 
1013  av_free(l->last);
1014  av_free(l->clast);
1015  for (i = 0; i < TM2_NUM_STREAMS; i++)
1016  av_freep(&l->tokens[i]);
1017  if (l->Y1) {
1018  av_freep(&l->Y1_base);
1019  av_freep(&l->U1_base);
1020  av_freep(&l->V1_base);
1021  av_freep(&l->Y2_base);
1022  av_freep(&l->U2_base);
1023  av_freep(&l->V2_base);
1024  }
1025  av_freep(&l->buffer);
1026  l->buffer_size = 0;
1027 
1028  av_frame_free(&l->pic);
1029 
1030  return 0;
1031 }
1032 
1034  .name = "truemotion2",
1035  .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
1036  .type = AVMEDIA_TYPE_VIDEO,
1038  .priv_data_size = sizeof(TM2Context),
1039  .init = decode_init,
1040  .close = decode_end,
1041  .decode = decode_frame,
1042  .capabilities = AV_CODEC_CAP_DR1,
1043 };
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
int * V2
Definition: truemotion2.c:85
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:262
int * U1_base
Definition: truemotion2.c:84
int D[4]
Definition: truemotion2.c:78
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define avpriv_request_sample(...)
int * last
Definition: truemotion2.c:80
int * recode
table for converting from code indexes to values
Definition: truemotion2.c:96
int size
Definition: avcodec.h:1680
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:121
AVCodec ff_truemotion2_decoder
Definition: truemotion2.c:1033
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3739
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1718
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
int num
current number filled
Definition: truemotion2.c:108
static int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
Definition: truemotion2.c:242
#define TM2_HEADER_SIZE
Definition: truemotion2.c:884
structure for gathering Huffman codes information
Definition: truemotion2.c:103
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
#define mb
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
#define Y
Definition: vf_boxblur.c:76
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
TM2_STREAMS
Definition: truemotion2.c:39
static const int tm2_stream_order[TM2_NUM_STREAMS]
Definition: truemotion2.c:880
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVCodecContext * avctx
Definition: truemotion2.c:62
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:395
int max_bits
maximum length of code
Definition: truemotion2.c:105
static int tm2_read_deltas(TM2Context *ctx, int stream_id)
Definition: truemotion2.c:259
const char data[16]
Definition: mxf.c:90
static void tm2_free_codes(TM2Codes *code)
Definition: truemotion2.c:223
uint8_t * data
Definition: avcodec.h:1679
int min_bits
minimum length of code
Definition: truemotion2.c:106
int * U2
Definition: truemotion2.c:85
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
static int tm2_read_tree(TM2Context *ctx, uint32_t prefix, int length, TM2Huff *huff)
Definition: truemotion2.c:115
VLC vlc
table for FFmpeg bitstream reader
Definition: truemotion2.c:94
bitstream reader API header.
static void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:502
static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
Definition: truemotion2.c:286
#define av_log(a,...)
static void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:668
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:589
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int val_bits
length of literal
Definition: truemotion2.c:104
#define AVERROR(e)
Definition: error.h:43
static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
Definition: truemotion2.c:146
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
static void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:545
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
Definition: truemotion2.c:762
int * tokens[TM2_NUM_STREAMS]
Definition: truemotion2.c:73
#define t1
Definition: regdef.h:29
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
int * clast
Definition: truemotion2.c:81
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
Definition: vlc.h:26
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:191
AVFrame * pic
Definition: truemotion2.c:63
int * V1
Definition: truemotion2.c:85
int * V2_base
Definition: truemotion2.c:84
int max_num
total number of codes
Definition: truemotion2.c:109
int bits
Definition: truemotion2.c:95
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
static int GET_TOK(TM2Context *ctx, int type)
Definition: truemotion2.c:398
int width
picture width / height.
Definition: avcodec.h:1948
#define TM2_OLD_HEADER_MAGIC
Definition: truemotion2.c:239
AVFormatContext * ctx
Definition: movenc.c:48
int uv_stride
Definition: truemotion2.c:86
uint8_t * buffer
Definition: truemotion2.c:69
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:556
#define AV_RL32
Definition: intreadwrite.h:146
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
static int tm2_get_token(GetBitContext *gb, TM2Codes *code)
Definition: truemotion2.c:230
int CD[4]
Definition: truemotion2.c:79
int * nums
literals
Definition: truemotion2.c:110
if(ret< 0)
Definition: vf_mcdeint.c:279
int nodes
total number of nodes in tree
Definition: truemotion2.c:107
static av_cold int decode_end(AVCodecContext *avctx)
Definition: truemotion2.c:1008
int * Y1
Definition: truemotion2.c:85
int * V1_base
Definition: truemotion2.c:84
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
#define TM2_INIT_POINTERS_2()
Definition: truemotion2.c:432
static void tm2_low_chroma(int *data, int stride, int *clast, unsigned *CD, int *deltas, int bx)
Definition: truemotion2.c:483
main external API structure.
Definition: avcodec.h:1761
static void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:633
#define TM2_DELTAS
Definition: truemotion2.c:36
void * buf
Definition: avisynth_c.h:690
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:314
#define TM2_INIT_POINTERS()
Definition: truemotion2.c:418
int * Y1_base
Definition: truemotion2.c:84
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:426
static void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:523
static void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:708
uint32_t * bits
codes
Definition: truemotion2.c:111
int length
Definition: truemotion2.c:97
#define u(width,...)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:347
static void tm2_apply_deltas(TM2Context *ctx, int *Y, int stride, int *deltas, int *last)
Definition: truemotion2.c:452
#define TM2_ESCAPE
Definition: truemotion2.c:35
#define TM2_RECALC_BLOCK(CHR, stride, last, CD)
Definition: truemotion2.c:445
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
int * U1
Definition: truemotion2.c:85
int * Y2
Definition: truemotion2.c:85
int
common internal api header.
int y_stride
Definition: truemotion2.c:86
static void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
Definition: truemotion2.c:586
int deltas[TM2_NUM_STREAMS][TM2_DELTAS]
Definition: truemotion2.c:76
int * lens
codelengths
Definition: truemotion2.c:112
static av_cold int decode_init(AVCodecContext *avctx)
Definition: truemotion2.c:944
int * Y2_base
Definition: truemotion2.c:84
int buffer_size
Definition: truemotion2.c:70
static void tm2_high_chroma(int *data, int stride, int *last, unsigned *CD, int *deltas)
Definition: truemotion2.c:470
#define TM2_NEW_HEADER_MAGIC
Definition: truemotion2.c:240
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1803
int * U2_base
Definition: truemotion2.c:84
TM2_BLOCKS
Definition: truemotion2.c:51
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
GetBitContext gb
Definition: truemotion2.c:65
int len
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int tok_lens[TM2_NUM_STREAMS]
Definition: truemotion2.c:74
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
FILE * out
Definition: movenc.c:54
#define av_freep(p)
Huffman codes for each of streams.
Definition: truemotion2.c:93
#define av_malloc_array(a, b)
const char int length
Definition: avisynth_c.h:768
int tok_ptrs[TM2_NUM_STREAMS]
Definition: truemotion2.c:75
This structure stores compressed data.
Definition: avcodec.h:1656
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
BswapDSPContext bdsp
Definition: truemotion2.c:67
#define t2
Definition: regdef.h:30
Predicted.
Definition: avutil.h:275
#define V
Definition: avdct.c:30
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: truemotion2.c:886