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.

965 lines
28KB

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