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.

1003 lines
29KB

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