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.

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