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.

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