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.

982 lines
29KB

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