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.

892 lines
25KB

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