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.

890 lines
25KB

  1. /*
  2. * Duck/ON2 TrueMotion 2 Decoder
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file truemotion2.c
  22. * Duck TrueMotion2 decoder.
  23. */
  24. #include "avcodec.h"
  25. #include "common.h"
  26. #include "bitstream.h"
  27. #include "dsputil.h"
  28. #define TM2_ESCAPE 0x80000000
  29. #define TM2_DELTAS 32
  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 FFmpeg 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. if(code->recode)
  168. av_free(code->recode);
  169. if(code->vlc.table)
  170. free_vlc(&code->vlc);
  171. }
  172. static inline int tm2_get_token(GetBitContext *gb, TM2Codes *code)
  173. {
  174. int val;
  175. val = get_vlc2(gb, code->vlc.table, code->bits, 1);
  176. return code->recode[val];
  177. }
  178. static inline int tm2_read_header(TM2Context *ctx, uint8_t *buf)
  179. {
  180. uint32_t magic;
  181. uint8_t *obuf;
  182. int length;
  183. obuf = buf;
  184. magic = LE_32(buf);
  185. buf += 4;
  186. if(magic == 0x00000100) { /* old header */
  187. /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
  188. return 40;
  189. } else if(magic == 0x00000101) { /* new header */
  190. int w, h, size, flags, xr, yr;
  191. length = LE_32(buf);
  192. buf += 4;
  193. init_get_bits(&ctx->gb, buf, 32);
  194. size = get_bits_long(&ctx->gb, 31);
  195. h = get_bits(&ctx->gb, 15);
  196. w = get_bits(&ctx->gb, 15);
  197. flags = get_bits_long(&ctx->gb, 31);
  198. yr = get_bits(&ctx->gb, 9);
  199. xr = get_bits(&ctx->gb, 9);
  200. return 40;
  201. } else {
  202. av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
  203. return -1;
  204. }
  205. return (buf - obuf);
  206. }
  207. static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
  208. int d, mb;
  209. int i, v;
  210. d = get_bits(&ctx->gb, 9);
  211. mb = get_bits(&ctx->gb, 5);
  212. if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
  213. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
  214. return -1;
  215. }
  216. for(i = 0; i < d; i++) {
  217. v = get_bits_long(&ctx->gb, mb);
  218. if(v & (1 << (mb - 1)))
  219. ctx->deltas[stream_id][i] = v - (1 << mb);
  220. else
  221. ctx->deltas[stream_id][i] = v;
  222. }
  223. for(; i < TM2_DELTAS; i++)
  224. ctx->deltas[stream_id][i] = 0;
  225. return 0;
  226. }
  227. static int tm2_read_stream(TM2Context *ctx, uint8_t *buf, int stream_id) {
  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 = BE_32(buf); buf += 4; cur += 4;
  235. skip = len * 4 + 4;
  236. if(len == 0)
  237. return 4;
  238. toks = BE_32(buf); buf += 4; cur += 4;
  239. if(toks & 1) {
  240. len = BE_32(buf); buf += 4; cur += 4;
  241. if(len == TM2_ESCAPE) {
  242. len = BE_32(buf); buf += 4; cur += 4;
  243. }
  244. if(len > 0) {
  245. init_get_bits(&ctx->gb, buf, skip - cur);
  246. if(tm2_read_deltas(ctx, stream_id) == -1)
  247. return -1;
  248. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  249. cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  250. }
  251. }
  252. /* skip unused fields */
  253. if(BE_32(buf) == TM2_ESCAPE) {
  254. buf += 4; cur += 4; /* some unknown length - could be escaped too */
  255. }
  256. buf += 4; cur += 4;
  257. buf += 4; cur += 4; /* unused by decoder */
  258. init_get_bits(&ctx->gb, buf, skip - cur);
  259. if(tm2_build_huff_table(ctx, &codes) == -1)
  260. return -1;
  261. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  262. cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  263. toks >>= 1;
  264. /* check if we have sane number of tokens */
  265. if((toks < 0) || (toks > 0xFFFFFF)){
  266. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
  267. tm2_free_codes(&codes);
  268. return -1;
  269. }
  270. ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
  271. ctx->tok_lens[stream_id] = toks;
  272. len = BE_32(buf); buf += 4; cur += 4;
  273. if(len > 0) {
  274. init_get_bits(&ctx->gb, buf, skip - cur);
  275. for(i = 0; i < toks; i++)
  276. ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
  277. } else {
  278. memset(ctx->tokens[stream_id], 0, toks * sizeof(int));
  279. }
  280. tm2_free_codes(&codes);
  281. return skip;
  282. }
  283. static inline int GET_TOK(TM2Context *ctx,int type) {
  284. if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
  285. 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]);
  286. return 0;
  287. }
  288. if(type <= TM2_MOT)
  289. return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
  290. return ctx->tokens[type][ctx->tok_ptrs[type]++];
  291. }
  292. /* blocks decoding routines */
  293. /* common Y, U, V pointers initialisation */
  294. #define TM2_INIT_POINTERS(); \
  295. int *last, *clast; \
  296. int *Y, *U, *V;\
  297. int Ystride, Ustride, Vstride;\
  298. \
  299. Ystride = ctx->avctx->width;\
  300. Vstride = (ctx->avctx->width + 1) >> 1;\
  301. Ustride = (ctx->avctx->width + 1) >> 1;\
  302. Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
  303. V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
  304. U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
  305. last = ctx->last + bx * 4;\
  306. clast = ctx->clast + bx * 4;
  307. #define TM2_INIT_POINTERS_2(); \
  308. int *Yo, *Uo, *Vo;\
  309. int oYstride, oUstride, oVstride;\
  310. \
  311. TM2_INIT_POINTERS();\
  312. oYstride = Ystride;\
  313. oVstride = Vstride;\
  314. oUstride = Ustride;\
  315. Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
  316. Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
  317. Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
  318. /* recalculate last and delta values for next blocks */
  319. #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
  320. CD[0] = (CHR[1] - 128) - last[1];\
  321. CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
  322. last[0] = (int)CHR[stride + 0] - 128;\
  323. last[1] = (int)CHR[stride + 1] - 128;}
  324. /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
  325. static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
  326. {
  327. int ct, d;
  328. int i, j;
  329. for(j = 0; j < 4; j++){
  330. ct = ctx->D[j];
  331. for(i = 0; i < 4; i++){
  332. d = deltas[i + j * 4];
  333. ct += d;
  334. last[i] += ct;
  335. Y[i] = clip_uint8(last[i]);
  336. }
  337. Y += stride;
  338. ctx->D[j] = ct;
  339. }
  340. }
  341. static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
  342. {
  343. int i, j;
  344. for(j = 0; j < 2; j++){
  345. for(i = 0; i < 2; i++){
  346. CD[j] += deltas[i + j * 2];
  347. last[i] += CD[j];
  348. data[i] = last[i] + 128;
  349. }
  350. data += stride;
  351. }
  352. }
  353. static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
  354. {
  355. int t;
  356. int l;
  357. int prev;
  358. if(bx > 0)
  359. prev = clast[-3];
  360. else
  361. prev = 0;
  362. t = (CD[0] + CD[1]) >> 1;
  363. l = (prev - CD[0] - CD[1] + clast[1] + 1) >> 1;
  364. CD[1] = CD[0] + CD[1] - t;
  365. CD[0] = t;
  366. clast[0] = l;
  367. tm2_high_chroma(data, stride, clast, CD, deltas);
  368. }
  369. static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  370. {
  371. int i;
  372. int deltas[16];
  373. TM2_INIT_POINTERS();
  374. /* hi-res chroma */
  375. for(i = 0; i < 4; i++) {
  376. deltas[i] = GET_TOK(ctx, TM2_C_HI);
  377. deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
  378. }
  379. tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
  380. tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
  381. /* hi-res luma */
  382. for(i = 0; i < 16; i++)
  383. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  384. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  385. }
  386. static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  387. {
  388. int i;
  389. int deltas[16];
  390. TM2_INIT_POINTERS();
  391. /* low-res chroma */
  392. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  393. deltas[1] = deltas[2] = deltas[3] = 0;
  394. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  395. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  396. deltas[1] = deltas[2] = deltas[3] = 0;
  397. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  398. /* hi-res luma */
  399. for(i = 0; i < 16; i++)
  400. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  401. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  402. }
  403. static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  404. {
  405. int i;
  406. int t1, t2;
  407. int deltas[16];
  408. TM2_INIT_POINTERS();
  409. /* low-res chroma */
  410. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  411. deltas[1] = deltas[2] = deltas[3] = 0;
  412. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  413. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  414. deltas[1] = deltas[2] = deltas[3] = 0;
  415. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  416. /* low-res luma */
  417. for(i = 0; i < 16; i++)
  418. deltas[i] = 0;
  419. deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
  420. deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
  421. deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
  422. deltas[10] = GET_TOK(ctx, TM2_L_LO);
  423. if(bx > 0)
  424. last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
  425. else
  426. last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
  427. last[2] = (last[1] + last[3]) >> 1;
  428. t1 = ctx->D[0] + ctx->D[1];
  429. ctx->D[0] = t1 >> 1;
  430. ctx->D[1] = t1 - (t1 >> 1);
  431. t2 = ctx->D[2] + ctx->D[3];
  432. ctx->D[2] = t2 >> 1;
  433. ctx->D[3] = t2 - (t2 >> 1);
  434. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  435. }
  436. static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  437. {
  438. int i;
  439. int ct;
  440. int left, right, diff;
  441. int deltas[16];
  442. TM2_INIT_POINTERS();
  443. /* null chroma */
  444. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  445. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  446. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  447. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  448. /* null luma */
  449. for(i = 0; i < 16; i++)
  450. deltas[i] = 0;
  451. ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
  452. if(bx > 0)
  453. left = last[-1] - ct;
  454. else
  455. left = 0;
  456. right = last[3];
  457. diff = right - left;
  458. last[0] = left + (diff >> 2);
  459. last[1] = left + (diff >> 1);
  460. last[2] = right - (diff >> 2);
  461. last[3] = right;
  462. {
  463. int tp = left;
  464. ctx->D[0] = (tp + (ct >> 2)) - left;
  465. left += ctx->D[0];
  466. ctx->D[1] = (tp + (ct >> 1)) - left;
  467. left += ctx->D[1];
  468. ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
  469. left += ctx->D[2];
  470. ctx->D[3] = (tp + ct) - left;
  471. }
  472. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  473. }
  474. static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  475. {
  476. int i, j;
  477. TM2_INIT_POINTERS_2();
  478. /* update chroma */
  479. for(j = 0; j < 2; j++){
  480. for(i = 0; i < 2; i++){
  481. U[i] = Uo[i];
  482. V[i] = Vo[i];
  483. }
  484. U += Ustride; V += Vstride;
  485. Uo += oUstride; Vo += oVstride;
  486. }
  487. U -= Ustride * 2;
  488. V -= Vstride * 2;
  489. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  490. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  491. /* update deltas */
  492. ctx->D[0] = Yo[3] - last[3];
  493. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  494. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  495. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  496. for(j = 0; j < 4; j++){
  497. for(i = 0; i < 4; i++){
  498. Y[i] = Yo[i];
  499. last[i] = Yo[i];
  500. }
  501. Y += Ystride;
  502. Yo += oYstride;
  503. }
  504. }
  505. static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  506. {
  507. int i, j;
  508. int d;
  509. TM2_INIT_POINTERS_2();
  510. /* update chroma */
  511. for(j = 0; j < 2; j++){
  512. for(i = 0; i < 2; i++){
  513. U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
  514. V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
  515. }
  516. U += Ustride; V += Vstride;
  517. Uo += oUstride; Vo += oVstride;
  518. }
  519. U -= Ustride * 2;
  520. V -= Vstride * 2;
  521. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  522. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  523. /* update deltas */
  524. ctx->D[0] = Yo[3] - last[3];
  525. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  526. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  527. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  528. for(j = 0; j < 4; j++){
  529. d = last[3];
  530. for(i = 0; i < 4; i++){
  531. Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
  532. last[i] = Y[i];
  533. }
  534. ctx->D[j] = last[3] - d;
  535. Y += Ystride;
  536. Yo += oYstride;
  537. }
  538. }
  539. static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  540. {
  541. int i, j;
  542. int mx, my;
  543. TM2_INIT_POINTERS_2();
  544. mx = GET_TOK(ctx, TM2_MOT);
  545. my = GET_TOK(ctx, TM2_MOT);
  546. Yo += my * oYstride + mx;
  547. Uo += (my >> 1) * oUstride + (mx >> 1);
  548. Vo += (my >> 1) * oVstride + (mx >> 1);
  549. /* copy chroma */
  550. for(j = 0; j < 2; j++){
  551. for(i = 0; i < 2; i++){
  552. U[i] = Uo[i];
  553. V[i] = Vo[i];
  554. }
  555. U += Ustride; V += Vstride;
  556. Uo += oUstride; 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. /* copy luma */
  563. for(j = 0; j < 4; j++){
  564. for(i = 0; i < 4; i++){
  565. Y[i] = Yo[i];
  566. }
  567. Y += Ystride;
  568. Yo += oYstride;
  569. }
  570. /* calculate deltas */
  571. Y -= Ystride * 4;
  572. ctx->D[0] = Y[3] - last[3];
  573. ctx->D[1] = Y[3 + Ystride] - Y[3];
  574. ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
  575. ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
  576. for(i = 0; i < 4; i++)
  577. last[i] = Y[i + Ystride * 3];
  578. }
  579. static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
  580. {
  581. int i, j;
  582. int bw, bh;
  583. int type;
  584. int keyframe = 1;
  585. uint8_t *Y, *U, *V;
  586. int *src;
  587. bw = ctx->avctx->width >> 2;
  588. bh = ctx->avctx->height >> 2;
  589. for(i = 0; i < TM2_NUM_STREAMS; i++)
  590. ctx->tok_ptrs[i] = 0;
  591. if (ctx->tok_lens[TM2_TYPE]<bw*bh){
  592. av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
  593. return -1;
  594. }
  595. memset(ctx->last, 0, 4 * bw * sizeof(int));
  596. memset(ctx->clast, 0, 4 * bw * sizeof(int));
  597. for(j = 0; j < bh; j++) {
  598. memset(ctx->D, 0, 4 * sizeof(int));
  599. memset(ctx->CD, 0, 4 * sizeof(int));
  600. for(i = 0; i < bw; i++) {
  601. type = GET_TOK(ctx, TM2_TYPE);
  602. switch(type) {
  603. case TM2_HI_RES:
  604. tm2_hi_res_block(ctx, p, i, j);
  605. break;
  606. case TM2_MED_RES:
  607. tm2_med_res_block(ctx, p, i, j);
  608. break;
  609. case TM2_LOW_RES:
  610. tm2_low_res_block(ctx, p, i, j);
  611. break;
  612. case TM2_NULL_RES:
  613. tm2_null_res_block(ctx, p, i, j);
  614. break;
  615. case TM2_UPDATE:
  616. tm2_update_block(ctx, p, i, j);
  617. keyframe = 0;
  618. break;
  619. case TM2_STILL:
  620. tm2_still_block(ctx, p, i, j);
  621. keyframe = 0;
  622. break;
  623. case TM2_MOTION:
  624. tm2_motion_block(ctx, p, i, j);
  625. keyframe = 0;
  626. break;
  627. default:
  628. av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
  629. }
  630. }
  631. }
  632. /* copy data from our buffer to AVFrame */
  633. Y = p->data[0];
  634. src = (ctx->cur?ctx->Y2:ctx->Y1);
  635. for(j = 0; j < ctx->avctx->height; j++){
  636. for(i = 0; i < ctx->avctx->width; i++){
  637. Y[i] = clip_uint8(*src++);
  638. }
  639. Y += p->linesize[0];
  640. }
  641. U = p->data[2];
  642. src = (ctx->cur?ctx->U2:ctx->U1);
  643. for(j = 0; j < (ctx->avctx->height + 1) >> 1; j++){
  644. for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){
  645. U[i] = clip_uint8(*src++);
  646. }
  647. U += p->linesize[2];
  648. }
  649. V = p->data[1];
  650. src = (ctx->cur?ctx->V2:ctx->V1);
  651. for(j = 0; j < (ctx->avctx->height + 1) >> 1; j++){
  652. for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){
  653. V[i] = clip_uint8(*src++);
  654. }
  655. V += p->linesize[1];
  656. }
  657. return keyframe;
  658. }
  659. static int decode_frame(AVCodecContext *avctx,
  660. void *data, int *data_size,
  661. uint8_t *buf, int buf_size)
  662. {
  663. TM2Context * const l = avctx->priv_data;
  664. AVFrame * const p= (AVFrame*)&l->pic;
  665. int skip, t;
  666. p->reference = 1;
  667. if(avctx->get_buffer(avctx, p) < 0){
  668. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  669. return -1;
  670. }
  671. l->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, buf_size >> 2);
  672. skip = tm2_read_header(l, buf);
  673. if(skip == -1)
  674. return -1;
  675. t = tm2_read_stream(l, buf + skip, TM2_C_HI);
  676. if(t == -1)
  677. return -1;
  678. skip += t;
  679. t = tm2_read_stream(l, buf + skip, TM2_C_LO);
  680. if(t == -1)
  681. return -1;
  682. skip += t;
  683. t = tm2_read_stream(l, buf + skip, TM2_L_HI);
  684. if(t == -1)
  685. return -1;
  686. skip += t;
  687. t = tm2_read_stream(l, buf + skip, TM2_L_LO);
  688. if(t == -1)
  689. return -1;
  690. skip += t;
  691. t = tm2_read_stream(l, buf + skip, TM2_UPD);
  692. if(t == -1)
  693. return -1;
  694. skip += t;
  695. t = tm2_read_stream(l, buf + skip, TM2_MOT);
  696. if(t == -1)
  697. return -1;
  698. skip += t;
  699. t = tm2_read_stream(l, buf + skip, TM2_TYPE);
  700. if(t == -1)
  701. return -1;
  702. p->key_frame = tm2_decode_blocks(l, p);
  703. if(p->key_frame)
  704. p->pict_type = FF_I_TYPE;
  705. else
  706. p->pict_type = FF_P_TYPE;
  707. l->cur = !l->cur;
  708. *data_size = sizeof(AVFrame);
  709. *(AVFrame*)data = l->pic;
  710. return buf_size;
  711. }
  712. static int decode_init(AVCodecContext *avctx){
  713. TM2Context * const l = avctx->priv_data;
  714. int i;
  715. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  716. return -1;
  717. }
  718. if((avctx->width & 3) || (avctx->height & 3)){
  719. av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
  720. return -1;
  721. }
  722. l->avctx = avctx;
  723. l->pic.data[0]=NULL;
  724. avctx->has_b_frames = 0;
  725. avctx->pix_fmt = PIX_FMT_YUV420P;
  726. dsputil_init(&l->dsp, avctx);
  727. l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  728. l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  729. for(i = 0; i < TM2_NUM_STREAMS; i++) {
  730. l->tokens[i] = NULL;
  731. l->tok_lens[i] = 0;
  732. }
  733. l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  734. l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  735. l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  736. l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  737. l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  738. l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  739. l->cur = 0;
  740. return 0;
  741. }
  742. static int decode_end(AVCodecContext *avctx){
  743. TM2Context * const l = avctx->priv_data;
  744. int i;
  745. if(l->last)
  746. av_free(l->last);
  747. if(l->clast)
  748. av_free(l->clast);
  749. for(i = 0; i < TM2_NUM_STREAMS; i++)
  750. if(l->tokens[i])
  751. av_free(l->tokens[i]);
  752. if(l->Y1){
  753. av_free(l->Y1);
  754. av_free(l->U1);
  755. av_free(l->V1);
  756. av_free(l->Y2);
  757. av_free(l->U2);
  758. av_free(l->V2);
  759. }
  760. return 0;
  761. }
  762. AVCodec truemotion2_decoder = {
  763. "truemotion2",
  764. CODEC_TYPE_VIDEO,
  765. CODEC_ID_TRUEMOTION2,
  766. sizeof(TM2Context),
  767. decode_init,
  768. NULL,
  769. decode_end,
  770. decode_frame,
  771. CODEC_CAP_DR1,
  772. };