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.

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