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