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.

1057 lines
31KB

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