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.

963 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. #define TM2_OLD_HEADER_MAGIC 0x00000100
  181. #define TM2_NEW_HEADER_MAGIC 0x00000101
  182. static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
  183. {
  184. uint32_t magic = AV_RL32(buf);
  185. switch (magic) {
  186. case TM2_OLD_HEADER_MAGIC:
  187. av_log_missing_feature(ctx->avctx, "TM2 old header", 1);
  188. return 0;
  189. case TM2_NEW_HEADER_MAGIC:
  190. return 0;
  191. default:
  192. av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. }
  196. static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
  197. int d, mb;
  198. int i, v;
  199. d = get_bits(&ctx->gb, 9);
  200. mb = get_bits(&ctx->gb, 5);
  201. if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
  202. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
  203. return -1;
  204. }
  205. for(i = 0; i < d; i++) {
  206. v = get_bits_long(&ctx->gb, mb);
  207. if(v & (1 << (mb - 1)))
  208. ctx->deltas[stream_id][i] = v - (1 << mb);
  209. else
  210. ctx->deltas[stream_id][i] = v;
  211. }
  212. for(; i < TM2_DELTAS; i++)
  213. ctx->deltas[stream_id][i] = 0;
  214. return 0;
  215. }
  216. static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, int buf_size)
  217. {
  218. int i;
  219. int skip = 0;
  220. int len, toks, pos;
  221. TM2Codes codes;
  222. GetByteContext gb;
  223. /* get stream length in dwords */
  224. bytestream2_init(&gb, buf, buf_size);
  225. len = bytestream2_get_be32(&gb);
  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 = bytestream2_get_be32(&gb);
  234. if(toks & 1) {
  235. len = bytestream2_get_be32(&gb);
  236. if(len == TM2_ESCAPE) {
  237. len = bytestream2_get_be32(&gb);
  238. }
  239. if(len > 0) {
  240. pos = bytestream2_tell(&gb);
  241. if (skip <= pos)
  242. return -1;
  243. init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
  244. if(tm2_read_deltas(ctx, stream_id) == -1)
  245. return -1;
  246. bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
  247. }
  248. }
  249. /* skip unused fields */
  250. len = bytestream2_get_be32(&gb);
  251. if(len == TM2_ESCAPE) { /* some unknown length - could be escaped too */
  252. bytestream2_skip(&gb, 8); /* unused by decoder */
  253. } else {
  254. bytestream2_skip(&gb, 4); /* unused by decoder */
  255. }
  256. pos = bytestream2_tell(&gb);
  257. if (skip <= pos)
  258. return -1;
  259. init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
  260. if(tm2_build_huff_table(ctx, &codes) == -1)
  261. return -1;
  262. bytestream2_skip(&gb, ((get_bits_count(&ctx->gb) + 31) >> 5) << 2);
  263. toks >>= 1;
  264. /* check if we have sane number of tokens */
  265. if((toks < 0) || (toks > 0xFFFFFF)){
  266. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
  267. tm2_free_codes(&codes);
  268. return -1;
  269. }
  270. ctx->tokens[stream_id] = av_realloc(ctx->tokens[stream_id], toks * sizeof(int));
  271. ctx->tok_lens[stream_id] = toks;
  272. len = bytestream2_get_be32(&gb);
  273. if(len > 0) {
  274. pos = bytestream2_tell(&gb);
  275. if (skip <= pos)
  276. return -1;
  277. init_get_bits(&ctx->gb, buf + pos, (skip - pos) * 8);
  278. for(i = 0; i < toks; i++) {
  279. if (get_bits_left(&ctx->gb) <= 0) {
  280. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of tokens: %i\n", toks);
  281. return -1;
  282. }
  283. ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
  284. if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
  285. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
  286. ctx->tokens[stream_id][i], stream_id, i);
  287. return AVERROR_INVALIDDATA;
  288. }
  289. }
  290. } else {
  291. for(i = 0; i < toks; i++) {
  292. ctx->tokens[stream_id][i] = codes.recode[0];
  293. if (stream_id <= TM2_MOT && ctx->tokens[stream_id][i] >= TM2_DELTAS) {
  294. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid delta token index %d for type %d, n=%d\n",
  295. ctx->tokens[stream_id][i], stream_id, i);
  296. return AVERROR_INVALIDDATA;
  297. }
  298. }
  299. }
  300. tm2_free_codes(&codes);
  301. return skip;
  302. }
  303. static inline int GET_TOK(TM2Context *ctx,int type) {
  304. if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
  305. 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]);
  306. return 0;
  307. }
  308. if(type <= TM2_MOT)
  309. return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
  310. return ctx->tokens[type][ctx->tok_ptrs[type]++];
  311. }
  312. /* blocks decoding routines */
  313. /* common Y, U, V pointers initialisation */
  314. #define TM2_INIT_POINTERS() \
  315. int *last, *clast; \
  316. int *Y, *U, *V;\
  317. int Ystride, Ustride, Vstride;\
  318. \
  319. Ystride = ctx->y_stride;\
  320. Vstride = ctx->uv_stride;\
  321. Ustride = ctx->uv_stride;\
  322. Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
  323. V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
  324. U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
  325. last = ctx->last + bx * 4;\
  326. clast = ctx->clast + bx * 4;
  327. #define TM2_INIT_POINTERS_2() \
  328. int *Yo, *Uo, *Vo;\
  329. int oYstride, oUstride, oVstride;\
  330. \
  331. TM2_INIT_POINTERS();\
  332. oYstride = Ystride;\
  333. oVstride = Vstride;\
  334. oUstride = Ustride;\
  335. Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
  336. Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
  337. Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
  338. /* recalculate last and delta values for next blocks */
  339. #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
  340. CD[0] = CHR[1] - last[1];\
  341. CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
  342. last[0] = (int)CHR[stride + 0];\
  343. last[1] = (int)CHR[stride + 1];}
  344. /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
  345. static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
  346. {
  347. int ct, d;
  348. int i, j;
  349. for(j = 0; j < 4; j++){
  350. ct = ctx->D[j];
  351. for(i = 0; i < 4; i++){
  352. d = deltas[i + j * 4];
  353. ct += d;
  354. last[i] += ct;
  355. Y[i] = av_clip_uint8(last[i]);
  356. }
  357. Y += stride;
  358. ctx->D[j] = ct;
  359. }
  360. }
  361. static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
  362. {
  363. int i, j;
  364. for(j = 0; j < 2; j++){
  365. for(i = 0; i < 2; i++){
  366. CD[j] += deltas[i + j * 2];
  367. last[i] += CD[j];
  368. data[i] = last[i];
  369. }
  370. data += stride;
  371. }
  372. }
  373. static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
  374. {
  375. int t;
  376. int l;
  377. int prev;
  378. if(bx > 0)
  379. prev = clast[-3];
  380. else
  381. prev = 0;
  382. t = (CD[0] + CD[1]) >> 1;
  383. l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
  384. CD[1] = CD[0] + CD[1] - t;
  385. CD[0] = t;
  386. clast[0] = l;
  387. tm2_high_chroma(data, stride, clast, CD, deltas);
  388. }
  389. static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  390. {
  391. int i;
  392. int deltas[16];
  393. TM2_INIT_POINTERS();
  394. /* hi-res chroma */
  395. for(i = 0; i < 4; i++) {
  396. deltas[i] = GET_TOK(ctx, TM2_C_HI);
  397. deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
  398. }
  399. tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
  400. tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
  401. /* hi-res luma */
  402. for(i = 0; i < 16; i++)
  403. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  404. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  405. }
  406. static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  407. {
  408. int i;
  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. /* hi-res luma */
  419. for(i = 0; i < 16; i++)
  420. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  421. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  422. }
  423. static inline void tm2_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  424. {
  425. int i;
  426. int t1, t2;
  427. int deltas[16];
  428. TM2_INIT_POINTERS();
  429. /* low-res chroma */
  430. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  431. deltas[1] = deltas[2] = deltas[3] = 0;
  432. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  433. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  434. deltas[1] = deltas[2] = deltas[3] = 0;
  435. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  436. /* low-res luma */
  437. for(i = 0; i < 16; i++)
  438. deltas[i] = 0;
  439. deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
  440. deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
  441. deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
  442. deltas[10] = GET_TOK(ctx, TM2_L_LO);
  443. if(bx > 0)
  444. last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
  445. else
  446. last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
  447. last[2] = (last[1] + last[3]) >> 1;
  448. t1 = ctx->D[0] + ctx->D[1];
  449. ctx->D[0] = t1 >> 1;
  450. ctx->D[1] = t1 - (t1 >> 1);
  451. t2 = ctx->D[2] + ctx->D[3];
  452. ctx->D[2] = t2 >> 1;
  453. ctx->D[3] = t2 - (t2 >> 1);
  454. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  455. }
  456. static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  457. {
  458. int i;
  459. int ct;
  460. int left, right, diff;
  461. int deltas[16];
  462. TM2_INIT_POINTERS();
  463. /* null chroma */
  464. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  465. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  466. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  467. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  468. /* null luma */
  469. for(i = 0; i < 16; i++)
  470. deltas[i] = 0;
  471. ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
  472. if(bx > 0)
  473. left = last[-1] - ct;
  474. else
  475. left = 0;
  476. right = last[3];
  477. diff = right - left;
  478. last[0] = left + (diff >> 2);
  479. last[1] = left + (diff >> 1);
  480. last[2] = right - (diff >> 2);
  481. last[3] = right;
  482. {
  483. int tp = left;
  484. ctx->D[0] = (tp + (ct >> 2)) - left;
  485. left += ctx->D[0];
  486. ctx->D[1] = (tp + (ct >> 1)) - left;
  487. left += ctx->D[1];
  488. ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
  489. left += ctx->D[2];
  490. ctx->D[3] = (tp + ct) - left;
  491. }
  492. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  493. }
  494. static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  495. {
  496. int i, j;
  497. TM2_INIT_POINTERS_2();
  498. /* update chroma */
  499. for(j = 0; j < 2; j++){
  500. for(i = 0; i < 2; i++){
  501. U[i] = Uo[i];
  502. V[i] = Vo[i];
  503. }
  504. U += Ustride; V += Vstride;
  505. Uo += oUstride; Vo += oVstride;
  506. }
  507. U -= Ustride * 2;
  508. V -= Vstride * 2;
  509. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  510. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  511. /* update deltas */
  512. ctx->D[0] = Yo[3] - last[3];
  513. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  514. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  515. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  516. for(j = 0; j < 4; j++){
  517. for(i = 0; i < 4; i++){
  518. Y[i] = Yo[i];
  519. last[i] = Yo[i];
  520. }
  521. Y += Ystride;
  522. Yo += oYstride;
  523. }
  524. }
  525. static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  526. {
  527. int i, j;
  528. int d;
  529. TM2_INIT_POINTERS_2();
  530. /* update chroma */
  531. for(j = 0; j < 2; j++){
  532. for(i = 0; i < 2; i++){
  533. U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
  534. V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
  535. }
  536. U += Ustride; V += Vstride;
  537. Uo += oUstride; Vo += oVstride;
  538. }
  539. U -= Ustride * 2;
  540. V -= Vstride * 2;
  541. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  542. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  543. /* update deltas */
  544. ctx->D[0] = Yo[3] - last[3];
  545. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  546. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  547. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  548. for(j = 0; j < 4; j++){
  549. d = last[3];
  550. for(i = 0; i < 4; i++){
  551. Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
  552. last[i] = Y[i];
  553. }
  554. ctx->D[j] = last[3] - d;
  555. Y += Ystride;
  556. Yo += oYstride;
  557. }
  558. }
  559. static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  560. {
  561. int i, j;
  562. int mx, my;
  563. TM2_INIT_POINTERS_2();
  564. mx = GET_TOK(ctx, TM2_MOT);
  565. my = GET_TOK(ctx, TM2_MOT);
  566. mx = av_clip(mx, -(bx * 4 + 4), ctx->avctx->width - bx * 4);
  567. my = av_clip(my, -(by * 4 + 4), ctx->avctx->height - by * 4);
  568. Yo += my * oYstride + mx;
  569. Uo += (my >> 1) * oUstride + (mx >> 1);
  570. Vo += (my >> 1) * oVstride + (mx >> 1);
  571. /* copy chroma */
  572. for(j = 0; j < 2; j++){
  573. for(i = 0; i < 2; i++){
  574. U[i] = Uo[i];
  575. V[i] = Vo[i];
  576. }
  577. U += Ustride; V += Vstride;
  578. Uo += oUstride; Vo += oVstride;
  579. }
  580. U -= Ustride * 2;
  581. V -= Vstride * 2;
  582. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  583. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  584. /* copy luma */
  585. for(j = 0; j < 4; j++){
  586. for(i = 0; i < 4; i++){
  587. Y[i] = Yo[i];
  588. }
  589. Y += Ystride;
  590. Yo += oYstride;
  591. }
  592. /* calculate deltas */
  593. Y -= Ystride * 4;
  594. ctx->D[0] = Y[3] - last[3];
  595. ctx->D[1] = Y[3 + Ystride] - Y[3];
  596. ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
  597. ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
  598. for(i = 0; i < 4; i++)
  599. last[i] = Y[i + Ystride * 3];
  600. }
  601. static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
  602. {
  603. int i, j;
  604. int w = ctx->avctx->width, h = ctx->avctx->height, bw = w >> 2, bh = h >> 2, cw = w >> 1;
  605. int type;
  606. int keyframe = 1;
  607. int *Y, *U, *V;
  608. uint8_t *dst;
  609. for(i = 0; i < TM2_NUM_STREAMS; i++)
  610. ctx->tok_ptrs[i] = 0;
  611. if (ctx->tok_lens[TM2_TYPE]<bw*bh){
  612. av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
  613. return -1;
  614. }
  615. memset(ctx->last, 0, 4 * bw * sizeof(int));
  616. memset(ctx->clast, 0, 4 * bw * sizeof(int));
  617. for(j = 0; j < bh; j++) {
  618. memset(ctx->D, 0, 4 * sizeof(int));
  619. memset(ctx->CD, 0, 4 * sizeof(int));
  620. for(i = 0; i < bw; i++) {
  621. type = GET_TOK(ctx, TM2_TYPE);
  622. switch(type) {
  623. case TM2_HI_RES:
  624. tm2_hi_res_block(ctx, p, i, j);
  625. break;
  626. case TM2_MED_RES:
  627. tm2_med_res_block(ctx, p, i, j);
  628. break;
  629. case TM2_LOW_RES:
  630. tm2_low_res_block(ctx, p, i, j);
  631. break;
  632. case TM2_NULL_RES:
  633. tm2_null_res_block(ctx, p, i, j);
  634. break;
  635. case TM2_UPDATE:
  636. tm2_update_block(ctx, p, i, j);
  637. keyframe = 0;
  638. break;
  639. case TM2_STILL:
  640. tm2_still_block(ctx, p, i, j);
  641. keyframe = 0;
  642. break;
  643. case TM2_MOTION:
  644. tm2_motion_block(ctx, p, i, j);
  645. keyframe = 0;
  646. break;
  647. default:
  648. av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
  649. }
  650. }
  651. }
  652. /* copy data from our buffer to AVFrame */
  653. Y = (ctx->cur?ctx->Y2:ctx->Y1);
  654. U = (ctx->cur?ctx->U2:ctx->U1);
  655. V = (ctx->cur?ctx->V2:ctx->V1);
  656. dst = p->data[0];
  657. for(j = 0; j < h; j++){
  658. for(i = 0; i < w; i++){
  659. int y = Y[i], u = U[i >> 1], v = V[i >> 1];
  660. dst[3*i+0] = av_clip_uint8(y + v);
  661. dst[3*i+1] = av_clip_uint8(y);
  662. dst[3*i+2] = av_clip_uint8(y + u);
  663. }
  664. /* horizontal edge extension */
  665. Y[-4] = Y[-3] = Y[-2] = Y[-1] = Y[0];
  666. Y[w + 3] = Y[w + 2] = Y[w + 1] = Y[w] = Y[w - 1];
  667. /* vertical edge extension */
  668. if (j == 0) {
  669. memcpy(Y - 4 - 1 * ctx->y_stride, Y - 4, ctx->y_stride);
  670. memcpy(Y - 4 - 2 * ctx->y_stride, Y - 4, ctx->y_stride);
  671. memcpy(Y - 4 - 3 * ctx->y_stride, Y - 4, ctx->y_stride);
  672. memcpy(Y - 4 - 4 * ctx->y_stride, Y - 4, ctx->y_stride);
  673. } else if (j == h - 1) {
  674. memcpy(Y - 4 + 1 * ctx->y_stride, Y - 4, ctx->y_stride);
  675. memcpy(Y - 4 + 2 * ctx->y_stride, Y - 4, ctx->y_stride);
  676. memcpy(Y - 4 + 3 * ctx->y_stride, Y - 4, ctx->y_stride);
  677. memcpy(Y - 4 + 4 * ctx->y_stride, Y - 4, ctx->y_stride);
  678. }
  679. Y += ctx->y_stride;
  680. if (j & 1) {
  681. /* horizontal edge extension */
  682. U[-2] = U[-1] = U[0];
  683. V[-2] = V[-1] = V[0];
  684. U[cw + 1] = U[cw] = U[cw - 1];
  685. V[cw + 1] = V[cw] = V[cw - 1];
  686. /* vertical edge extension */
  687. if (j == 1) {
  688. memcpy(U - 2 - 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
  689. memcpy(V - 2 - 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
  690. memcpy(U - 2 - 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
  691. memcpy(V - 2 - 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
  692. } else if (j == h - 1) {
  693. memcpy(U - 2 + 1 * ctx->uv_stride, U - 2, ctx->uv_stride);
  694. memcpy(V - 2 + 1 * ctx->uv_stride, V - 2, ctx->uv_stride);
  695. memcpy(U - 2 + 2 * ctx->uv_stride, U - 2, ctx->uv_stride);
  696. memcpy(V - 2 + 2 * ctx->uv_stride, V - 2, ctx->uv_stride);
  697. }
  698. U += ctx->uv_stride;
  699. V += ctx->uv_stride;
  700. }
  701. dst += p->linesize[0];
  702. }
  703. return keyframe;
  704. }
  705. static const int tm2_stream_order[TM2_NUM_STREAMS] = {
  706. TM2_C_HI, TM2_C_LO, TM2_L_HI, TM2_L_LO, TM2_UPD, TM2_MOT, TM2_TYPE
  707. };
  708. #define TM2_HEADER_SIZE 40
  709. static int decode_frame(AVCodecContext *avctx,
  710. void *data, int *got_frame,
  711. AVPacket *avpkt)
  712. {
  713. const uint8_t *buf = avpkt->data;
  714. int buf_size = avpkt->size & ~3;
  715. TM2Context * const l = avctx->priv_data;
  716. AVFrame * const p = &l->pic;
  717. int i, offset = TM2_HEADER_SIZE, t, ret;
  718. uint8_t *swbuf;
  719. swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  720. if(!swbuf){
  721. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  722. return -1;
  723. }
  724. p->reference = 1;
  725. p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  726. if(avctx->reget_buffer(avctx, p) < 0){
  727. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  728. av_free(swbuf);
  729. return -1;
  730. }
  731. l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
  732. if ((ret = tm2_read_header(l, swbuf)) < 0) {
  733. av_free(swbuf);
  734. return ret;
  735. }
  736. for(i = 0; i < TM2_NUM_STREAMS; i++){
  737. if (offset >= buf_size) {
  738. av_free(swbuf);
  739. return AVERROR_INVALIDDATA;
  740. }
  741. t = tm2_read_stream(l, swbuf + offset, tm2_stream_order[i],
  742. buf_size - offset);
  743. if(t < 0){
  744. av_free(swbuf);
  745. return t;
  746. }
  747. offset += t;
  748. }
  749. p->key_frame = tm2_decode_blocks(l, p);
  750. if(p->key_frame)
  751. p->pict_type = AV_PICTURE_TYPE_I;
  752. else
  753. p->pict_type = AV_PICTURE_TYPE_P;
  754. l->cur = !l->cur;
  755. *got_frame = 1;
  756. *(AVFrame*)data = l->pic;
  757. av_free(swbuf);
  758. return buf_size;
  759. }
  760. static av_cold int decode_init(AVCodecContext *avctx){
  761. TM2Context * const l = avctx->priv_data;
  762. int i, w = avctx->width, h = avctx->height;
  763. if((avctx->width & 3) || (avctx->height & 3)){
  764. av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
  765. return -1;
  766. }
  767. l->avctx = avctx;
  768. l->pic.data[0]=NULL;
  769. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  770. ff_dsputil_init(&l->dsp, avctx);
  771. l->last = av_malloc(4 * sizeof(*l->last) * (w >> 2));
  772. l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
  773. for(i = 0; i < TM2_NUM_STREAMS; i++) {
  774. l->tokens[i] = NULL;
  775. l->tok_lens[i] = 0;
  776. }
  777. w += 8;
  778. h += 8;
  779. l->Y1_base = av_malloc(sizeof(*l->Y1_base) * w * h);
  780. l->Y2_base = av_malloc(sizeof(*l->Y2_base) * w * h);
  781. l->y_stride = w;
  782. w = (w + 1) >> 1;
  783. h = (h + 1) >> 1;
  784. l->U1_base = av_malloc(sizeof(*l->U1_base) * w * h);
  785. l->V1_base = av_malloc(sizeof(*l->V1_base) * w * h);
  786. l->U2_base = av_malloc(sizeof(*l->U2_base) * w * h);
  787. l->V2_base = av_malloc(sizeof(*l->V1_base) * w * h);
  788. l->uv_stride = w;
  789. l->cur = 0;
  790. if (!l->Y1_base || !l->Y2_base || !l->U1_base ||
  791. !l->V1_base || !l->U2_base || !l->V2_base ||
  792. !l->last || !l->clast) {
  793. av_freep(l->Y1_base);
  794. av_freep(l->Y2_base);
  795. av_freep(l->U1_base);
  796. av_freep(l->U2_base);
  797. av_freep(l->V1_base);
  798. av_freep(l->V2_base);
  799. av_freep(l->last);
  800. av_freep(l->clast);
  801. return AVERROR(ENOMEM);
  802. }
  803. l->Y1 = l->Y1_base + l->y_stride * 4 + 4;
  804. l->Y2 = l->Y2_base + l->y_stride * 4 + 4;
  805. l->U1 = l->U1_base + l->uv_stride * 2 + 2;
  806. l->U2 = l->U2_base + l->uv_stride * 2 + 2;
  807. l->V1 = l->V1_base + l->uv_stride * 2 + 2;
  808. l->V2 = l->V2_base + l->uv_stride * 2 + 2;
  809. return 0;
  810. }
  811. static av_cold int decode_end(AVCodecContext *avctx){
  812. TM2Context * const l = avctx->priv_data;
  813. AVFrame *pic = &l->pic;
  814. int i;
  815. av_free(l->last);
  816. av_free(l->clast);
  817. for(i = 0; i < TM2_NUM_STREAMS; i++)
  818. av_free(l->tokens[i]);
  819. if(l->Y1){
  820. av_free(l->Y1_base);
  821. av_free(l->U1_base);
  822. av_free(l->V1_base);
  823. av_free(l->Y2_base);
  824. av_free(l->U2_base);
  825. av_free(l->V2_base);
  826. }
  827. if (pic->data[0])
  828. avctx->release_buffer(avctx, pic);
  829. return 0;
  830. }
  831. AVCodec ff_truemotion2_decoder = {
  832. .name = "truemotion2",
  833. .type = AVMEDIA_TYPE_VIDEO,
  834. .id = AV_CODEC_ID_TRUEMOTION2,
  835. .priv_data_size = sizeof(TM2Context),
  836. .init = decode_init,
  837. .close = decode_end,
  838. .decode = decode_frame,
  839. .capabilities = CODEC_CAP_DR1,
  840. .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0"),
  841. };