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.

883 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. obuf = buf;
  182. magic = AV_RL32(buf);
  183. buf += 4;
  184. if(magic == 0x00000100) { /* old header */
  185. /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
  186. return 40;
  187. } else if(magic == 0x00000101) { /* new header */
  188. return 40;
  189. } else {
  190. av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
  191. return -1;
  192. }
  193. return buf - obuf;
  194. }
  195. static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
  196. int d, mb;
  197. int i, v;
  198. d = get_bits(&ctx->gb, 9);
  199. mb = get_bits(&ctx->gb, 5);
  200. if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
  201. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
  202. return -1;
  203. }
  204. for(i = 0; i < d; i++) {
  205. v = get_bits_long(&ctx->gb, mb);
  206. if(v & (1 << (mb - 1)))
  207. ctx->deltas[stream_id][i] = v - (1 << mb);
  208. else
  209. ctx->deltas[stream_id][i] = v;
  210. }
  211. for(; i < TM2_DELTAS; i++)
  212. ctx->deltas[stream_id][i] = 0;
  213. return 0;
  214. }
  215. static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
  216. {
  217. int i;
  218. int cur = 0;
  219. int skip = 0;
  220. int len, toks;
  221. TM2Codes codes;
  222. /* get stream length in dwords */
  223. len = AV_RB32(buf); buf += 4; cur += 4;
  224. skip = len * 4 + 4;
  225. if(len == 0)
  226. return 4;
  227. if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
  228. av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
  229. return -1;
  230. }
  231. toks = AV_RB32(buf); buf += 4; cur += 4;
  232. if(toks & 1) {
  233. len = AV_RB32(buf); buf += 4; cur += 4;
  234. if(len == TM2_ESCAPE) {
  235. len = AV_RB32(buf); buf += 4; cur += 4;
  236. }
  237. if(len > 0) {
  238. if (skip <= cur)
  239. return -1;
  240. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  241. if(tm2_read_deltas(ctx, stream_id) == -1)
  242. return -1;
  243. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  244. cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  245. }
  246. }
  247. /* skip unused fields */
  248. if(AV_RB32(buf) == TM2_ESCAPE) {
  249. buf += 4; cur += 4; /* some unknown length - could be escaped too */
  250. }
  251. buf += 4; cur += 4;
  252. buf += 4; cur += 4; /* unused by decoder */
  253. if (skip <= cur)
  254. return -1;
  255. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  256. if(tm2_build_huff_table(ctx, &codes) == -1)
  257. return -1;
  258. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  259. cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  260. toks >>= 1;
  261. /* check if we have sane number of tokens */
  262. if((toks < 0) || (toks > 0xFFFFFF)){
  263. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
  264. tm2_free_codes(&codes);
  265. return -1;
  266. }
  267. ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
  268. ctx->tok_lens[stream_id] = toks;
  269. len = AV_RB32(buf); buf += 4; cur += 4;
  270. if(len > 0) {
  271. if (skip <= cur)
  272. return -1;
  273. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  274. for(i = 0; i < toks; i++) {
  275. if (get_bits_left(&ctx->gb) <= 0) {
  276. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
  277. return -1;
  278. }
  279. ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
  280. }
  281. } else {
  282. for(i = 0; i < toks; i++)
  283. ctx->tokens[stream_id][i] = codes.recode[0];
  284. }
  285. tm2_free_codes(&codes);
  286. return skip;
  287. }
  288. static inline int GET_TOK(TM2Context *ctx,int type) {
  289. if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
  290. 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]);
  291. return 0;
  292. }
  293. if(type <= TM2_MOT)
  294. return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
  295. return ctx->tokens[type][ctx->tok_ptrs[type]++];
  296. }
  297. /* blocks decoding routines */
  298. /* common Y, U, V pointers initialisation */
  299. #define TM2_INIT_POINTERS() \
  300. int *last, *clast; \
  301. int *Y, *U, *V;\
  302. int Ystride, Ustride, Vstride;\
  303. \
  304. Ystride = ctx->avctx->width;\
  305. Vstride = (ctx->avctx->width + 1) >> 1;\
  306. Ustride = (ctx->avctx->width + 1) >> 1;\
  307. Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
  308. V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
  309. U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
  310. last = ctx->last + bx * 4;\
  311. clast = ctx->clast + bx * 4;
  312. #define TM2_INIT_POINTERS_2() \
  313. int *Yo, *Uo, *Vo;\
  314. int oYstride, oUstride, oVstride;\
  315. \
  316. TM2_INIT_POINTERS();\
  317. oYstride = Ystride;\
  318. oVstride = Vstride;\
  319. oUstride = Ustride;\
  320. Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
  321. Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
  322. Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
  323. /* recalculate last and delta values for next blocks */
  324. #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
  325. CD[0] = CHR[1] - last[1];\
  326. CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
  327. last[0] = (int)CHR[stride + 0];\
  328. last[1] = (int)CHR[stride + 1];}
  329. /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
  330. static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
  331. {
  332. int ct, d;
  333. int i, j;
  334. for(j = 0; j < 4; j++){
  335. ct = ctx->D[j];
  336. for(i = 0; i < 4; i++){
  337. d = deltas[i + j * 4];
  338. ct += d;
  339. last[i] += ct;
  340. Y[i] = av_clip_uint8(last[i]);
  341. }
  342. Y += stride;
  343. ctx->D[j] = ct;
  344. }
  345. }
  346. static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
  347. {
  348. int i, j;
  349. for(j = 0; j < 2; j++){
  350. for(i = 0; i < 2; i++){
  351. CD[j] += deltas[i + j * 2];
  352. last[i] += CD[j];
  353. data[i] = last[i];
  354. }
  355. data += stride;
  356. }
  357. }
  358. static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
  359. {
  360. int t;
  361. int l;
  362. int prev;
  363. if(bx > 0)
  364. prev = clast[-3];
  365. else
  366. prev = 0;
  367. t = (CD[0] + CD[1]) >> 1;
  368. l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
  369. CD[1] = CD[0] + CD[1] - t;
  370. CD[0] = t;
  371. clast[0] = l;
  372. tm2_high_chroma(data, stride, clast, CD, deltas);
  373. }
  374. static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  375. {
  376. int i;
  377. int deltas[16];
  378. TM2_INIT_POINTERS();
  379. /* hi-res chroma */
  380. for(i = 0; i < 4; i++) {
  381. deltas[i] = GET_TOK(ctx, TM2_C_HI);
  382. deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
  383. }
  384. tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
  385. tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
  386. /* hi-res luma */
  387. for(i = 0; i < 16; i++)
  388. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  389. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  390. }
  391. static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  392. {
  393. int i;
  394. int deltas[16];
  395. TM2_INIT_POINTERS();
  396. /* low-res chroma */
  397. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  398. deltas[1] = deltas[2] = deltas[3] = 0;
  399. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  400. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  401. deltas[1] = deltas[2] = deltas[3] = 0;
  402. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  403. /* hi-res luma */
  404. for(i = 0; i < 16; i++)
  405. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  406. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  407. }
  408. static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  409. {
  410. int i;
  411. int t1, t2;
  412. int deltas[16];
  413. TM2_INIT_POINTERS();
  414. /* low-res chroma */
  415. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  416. deltas[1] = deltas[2] = deltas[3] = 0;
  417. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  418. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  419. deltas[1] = deltas[2] = deltas[3] = 0;
  420. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  421. /* low-res luma */
  422. for(i = 0; i < 16; i++)
  423. deltas[i] = 0;
  424. deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
  425. deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
  426. deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
  427. deltas[10] = GET_TOK(ctx, TM2_L_LO);
  428. if(bx > 0)
  429. last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
  430. else
  431. last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
  432. last[2] = (last[1] + last[3]) >> 1;
  433. t1 = ctx->D[0] + ctx->D[1];
  434. ctx->D[0] = t1 >> 1;
  435. ctx->D[1] = t1 - (t1 >> 1);
  436. t2 = ctx->D[2] + ctx->D[3];
  437. ctx->D[2] = t2 >> 1;
  438. ctx->D[3] = t2 - (t2 >> 1);
  439. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  440. }
  441. static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  442. {
  443. int i;
  444. int ct;
  445. int left, right, diff;
  446. int deltas[16];
  447. TM2_INIT_POINTERS();
  448. /* null chroma */
  449. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  450. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  451. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  452. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  453. /* null luma */
  454. for(i = 0; i < 16; i++)
  455. deltas[i] = 0;
  456. ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
  457. if(bx > 0)
  458. left = last[-1] - ct;
  459. else
  460. left = 0;
  461. right = last[3];
  462. diff = right - left;
  463. last[0] = left + (diff >> 2);
  464. last[1] = left + (diff >> 1);
  465. last[2] = right - (diff >> 2);
  466. last[3] = right;
  467. {
  468. int tp = left;
  469. ctx->D[0] = (tp + (ct >> 2)) - left;
  470. left += ctx->D[0];
  471. ctx->D[1] = (tp + (ct >> 1)) - left;
  472. left += ctx->D[1];
  473. ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
  474. left += ctx->D[2];
  475. ctx->D[3] = (tp + ct) - left;
  476. }
  477. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  478. }
  479. static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  480. {
  481. int i, j;
  482. TM2_INIT_POINTERS_2();
  483. /* update chroma */
  484. for(j = 0; j < 2; j++){
  485. for(i = 0; i < 2; i++){
  486. U[i] = Uo[i];
  487. V[i] = Vo[i];
  488. }
  489. U += Ustride; V += Vstride;
  490. Uo += oUstride; Vo += oVstride;
  491. }
  492. U -= Ustride * 2;
  493. V -= Vstride * 2;
  494. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  495. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  496. /* update deltas */
  497. ctx->D[0] = Yo[3] - last[3];
  498. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  499. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  500. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  501. for(j = 0; j < 4; j++){
  502. for(i = 0; i < 4; i++){
  503. Y[i] = Yo[i];
  504. last[i] = Yo[i];
  505. }
  506. Y += Ystride;
  507. Yo += oYstride;
  508. }
  509. }
  510. static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  511. {
  512. int i, j;
  513. int d;
  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] + GET_TOK(ctx, TM2_UPD);
  519. V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
  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. d = last[3];
  535. for(i = 0; i < 4; i++){
  536. Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
  537. last[i] = Y[i];
  538. }
  539. ctx->D[j] = last[3] - d;
  540. Y += Ystride;
  541. Yo += oYstride;
  542. }
  543. }
  544. static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  545. {
  546. int i, j;
  547. int mx, my;
  548. TM2_INIT_POINTERS_2();
  549. mx = GET_TOK(ctx, TM2_MOT);
  550. my = GET_TOK(ctx, TM2_MOT);
  551. Yo += my * oYstride + mx;
  552. Uo += (my >> 1) * oUstride + (mx >> 1);
  553. Vo += (my >> 1) * oVstride + (mx >> 1);
  554. /* copy chroma */
  555. for(j = 0; j < 2; j++){
  556. for(i = 0; i < 2; i++){
  557. U[i] = Uo[i];
  558. V[i] = Vo[i];
  559. }
  560. U += Ustride; V += Vstride;
  561. Uo += oUstride; Vo += oVstride;
  562. }
  563. U -= Ustride * 2;
  564. V -= Vstride * 2;
  565. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  566. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  567. /* copy luma */
  568. for(j = 0; j < 4; j++){
  569. for(i = 0; i < 4; i++){
  570. Y[i] = Yo[i];
  571. }
  572. Y += Ystride;
  573. Yo += oYstride;
  574. }
  575. /* calculate deltas */
  576. Y -= Ystride * 4;
  577. ctx->D[0] = Y[3] - last[3];
  578. ctx->D[1] = Y[3 + Ystride] - Y[3];
  579. ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
  580. ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
  581. for(i = 0; i < 4; i++)
  582. last[i] = Y[i + Ystride * 3];
  583. }
  584. static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
  585. {
  586. int i, j;
  587. int bw, bh;
  588. int type;
  589. int keyframe = 1;
  590. int *Y, *U, *V;
  591. uint8_t *dst;
  592. bw = ctx->avctx->width >> 2;
  593. bh = ctx->avctx->height >> 2;
  594. for(i = 0; i < TM2_NUM_STREAMS; i++)
  595. ctx->tok_ptrs[i] = 0;
  596. if (ctx->tok_lens[TM2_TYPE]<bw*bh){
  597. av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
  598. return -1;
  599. }
  600. memset(ctx->last, 0, 4 * bw * sizeof(int));
  601. memset(ctx->clast, 0, 4 * bw * sizeof(int));
  602. for(j = 0; j < bh; j++) {
  603. memset(ctx->D, 0, 4 * sizeof(int));
  604. memset(ctx->CD, 0, 4 * sizeof(int));
  605. for(i = 0; i < bw; i++) {
  606. type = GET_TOK(ctx, TM2_TYPE);
  607. switch(type) {
  608. case TM2_HI_RES:
  609. tm2_hi_res_block(ctx, p, i, j);
  610. break;
  611. case TM2_MED_RES:
  612. tm2_med_res_block(ctx, p, i, j);
  613. break;
  614. case TM2_LOW_RES:
  615. tm2_low_res_block(ctx, p, i, j);
  616. break;
  617. case TM2_NULL_RES:
  618. tm2_null_res_block(ctx, p, i, j);
  619. break;
  620. case TM2_UPDATE:
  621. tm2_update_block(ctx, p, i, j);
  622. keyframe = 0;
  623. break;
  624. case TM2_STILL:
  625. tm2_still_block(ctx, p, i, j);
  626. keyframe = 0;
  627. break;
  628. case TM2_MOTION:
  629. tm2_motion_block(ctx, p, i, j);
  630. keyframe = 0;
  631. break;
  632. default:
  633. av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
  634. }
  635. }
  636. }
  637. /* copy data from our buffer to AVFrame */
  638. Y = (ctx->cur?ctx->Y2:ctx->Y1);
  639. U = (ctx->cur?ctx->U2:ctx->U1);
  640. V = (ctx->cur?ctx->V2:ctx->V1);
  641. dst = p->data[0];
  642. for(j = 0; j < ctx->avctx->height; j++){
  643. for(i = 0; i < ctx->avctx->width; i++){
  644. int y = Y[i], u = U[i >> 1], v = V[i >> 1];
  645. dst[3*i+0] = av_clip_uint8(y + v);
  646. dst[3*i+1] = av_clip_uint8(y);
  647. dst[3*i+2] = av_clip_uint8(y + u);
  648. }
  649. Y += ctx->avctx->width;
  650. if (j & 1) {
  651. U += ctx->avctx->width >> 1;
  652. V += ctx->avctx->width >> 1;
  653. }
  654. dst += p->linesize[0];
  655. }
  656. return keyframe;
  657. }
  658. static const int tm2_stream_order[TM2_NUM_STREAMS] = {
  659. TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
  660. };
  661. static int decode_frame(AVCodecContext *avctx,
  662. void *data, int *data_size,
  663. AVPacket *avpkt)
  664. {
  665. const uint8_t *buf = avpkt->data;
  666. int buf_size = avpkt->size;
  667. TM2Context * const l = avctx->priv_data;
  668. AVFrame * const p= (AVFrame*)&l->pic;
  669. int i, skip, t;
  670. uint8_t *swbuf;
  671. swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  672. if(!swbuf){
  673. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  674. return -1;
  675. }
  676. p->reference = 1;
  677. p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  678. if(avctx->reget_buffer(avctx, p) < 0){
  679. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  680. av_free(swbuf);
  681. return -1;
  682. }
  683. l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
  684. skip = tm2_read_header(l, swbuf);
  685. if(skip == -1){
  686. av_free(swbuf);
  687. return -1;
  688. }
  689. for(i = 0; i < TM2_NUM_STREAMS; i++){
  690. t = tm2_read_stream(l, swbuf + skip, tm2_stream_order[i], buf_size);
  691. if(t == -1){
  692. av_free(swbuf);
  693. return -1;
  694. }
  695. skip += t;
  696. }
  697. p->key_frame = tm2_decode_blocks(l, p);
  698. if(p->key_frame)
  699. p->pict_type = AV_PICTURE_TYPE_I;
  700. else
  701. p->pict_type = AV_PICTURE_TYPE_P;
  702. l->cur = !l->cur;
  703. *data_size = sizeof(AVFrame);
  704. *(AVFrame*)data = l->pic;
  705. av_free(swbuf);
  706. return buf_size;
  707. }
  708. static av_cold int decode_init(AVCodecContext *avctx){
  709. TM2Context * const l = avctx->priv_data;
  710. int i;
  711. if((avctx->width & 3) || (avctx->height & 3)){
  712. av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
  713. return -1;
  714. }
  715. l->avctx = avctx;
  716. l->pic.data[0]=NULL;
  717. avctx->pix_fmt = PIX_FMT_BGR24;
  718. dsputil_init(&l->dsp, avctx);
  719. l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  720. l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  721. for(i = 0; i < TM2_NUM_STREAMS; i++) {
  722. l->tokens[i] = NULL;
  723. l->tok_lens[i] = 0;
  724. }
  725. l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  726. l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  727. l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  728. l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  729. l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  730. l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  731. l->cur = 0;
  732. return 0;
  733. }
  734. static av_cold int decode_end(AVCodecContext *avctx){
  735. TM2Context * const l = avctx->priv_data;
  736. AVFrame *pic = &l->pic;
  737. int i;
  738. av_free(l->last);
  739. av_free(l->clast);
  740. for(i = 0; i < TM2_NUM_STREAMS; i++)
  741. av_free(l->tokens[i]);
  742. if(l->Y1){
  743. av_free(l->Y1);
  744. av_free(l->U1);
  745. av_free(l->V1);
  746. av_free(l->Y2);
  747. av_free(l->U2);
  748. av_free(l->V2);
  749. }
  750. if (pic->data[0])
  751. avctx->release_buffer(avctx, pic);
  752. return 0;
  753. }
  754. AVCodec ff_truemotion2_decoder = {
  755. .name = "truemotion2",
  756. .type = AVMEDIA_TYPE_VIDEO,
  757. .id = CODEC_ID_TRUEMOTION2,
  758. .priv_data_size = sizeof(TM2Context),
  759. .init = decode_init,
  760. .close = decode_end,
  761. .decode = decode_frame,
  762. .capabilities = CODEC_CAP_DR1,
  763. .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
  764. };