You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1027 lines
30KB

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