truemotion2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. /**
  23. * @file truemotion2.c
  24. * Duck TrueMotion2 decoder.
  25. */
  26. #include "avcodec.h"
  27. #include "common.h"
  28. #include "bitstream.h"
  29. #include "dsputil.h"
  30. #define TM2_ESCAPE 0x80000000
  31. #define TM2_DELTAS 64
  32. /* Huffman-coded streams of different types of blocks */
  33. enum TM2_STREAMS{ TM2_C_HI = 0, TM2_C_LO, TM2_L_HI, TM2_L_LO,
  34. TM2_UPD, TM2_MOT, TM2_TYPE, TM2_NUM_STREAMS};
  35. /* Block types */
  36. enum TM2_BLOCKS{ TM2_HI_RES = 0, TM2_MED_RES, TM2_LOW_RES, TM2_NULL_RES,
  37. TM2_UPDATE, TM2_STILL, TM2_MOTION};
  38. typedef struct TM2Context{
  39. AVCodecContext *avctx;
  40. AVFrame pic;
  41. GetBitContext gb;
  42. DSPContext dsp;
  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. if(code->recode)
  170. av_free(code->recode);
  171. if(code->vlc.table)
  172. 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, uint8_t *buf)
  181. {
  182. uint32_t magic;
  183. uint8_t *obuf;
  184. int length;
  185. obuf = buf;
  186. magic = AV_RL32(buf);
  187. buf += 4;
  188. if(magic == 0x00000100) { /* old header */
  189. /* av_log (ctx->avctx, AV_LOG_ERROR, "TM2 old header: not implemented (yet)\n"); */
  190. return 40;
  191. } else if(magic == 0x00000101) { /* new header */
  192. int w, h, size, flags, xr, yr;
  193. length = AV_RL32(buf);
  194. buf += 4;
  195. init_get_bits(&ctx->gb, buf, 32 * 8);
  196. size = get_bits_long(&ctx->gb, 31);
  197. h = get_bits(&ctx->gb, 15);
  198. w = get_bits(&ctx->gb, 15);
  199. flags = get_bits_long(&ctx->gb, 31);
  200. yr = get_bits(&ctx->gb, 9);
  201. xr = get_bits(&ctx->gb, 9);
  202. return 40;
  203. } else {
  204. av_log (ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
  205. return -1;
  206. }
  207. return (buf - obuf);
  208. }
  209. static int tm2_read_deltas(TM2Context *ctx, int stream_id) {
  210. int d, mb;
  211. int i, v;
  212. d = get_bits(&ctx->gb, 9);
  213. mb = get_bits(&ctx->gb, 5);
  214. if((d < 1) || (d > TM2_DELTAS) || (mb < 1) || (mb > 32)) {
  215. av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect delta table: %i deltas x %i bits\n", d, mb);
  216. return -1;
  217. }
  218. for(i = 0; i < d; i++) {
  219. v = get_bits_long(&ctx->gb, mb);
  220. if(v & (1 << (mb - 1)))
  221. ctx->deltas[stream_id][i] = v - (1 << mb);
  222. else
  223. ctx->deltas[stream_id][i] = v;
  224. }
  225. for(; i < TM2_DELTAS; i++)
  226. ctx->deltas[stream_id][i] = 0;
  227. return 0;
  228. }
  229. static int tm2_read_stream(TM2Context *ctx, uint8_t *buf, int stream_id) {
  230. int i;
  231. int cur = 0;
  232. int skip = 0;
  233. int len, toks;
  234. TM2Codes codes;
  235. /* get stream length in dwords */
  236. len = AV_RB32(buf); buf += 4; cur += 4;
  237. skip = len * 4 + 4;
  238. if(len == 0)
  239. return 4;
  240. toks = AV_RB32(buf); buf += 4; cur += 4;
  241. if(toks & 1) {
  242. len = AV_RB32(buf); buf += 4; cur += 4;
  243. if(len == TM2_ESCAPE) {
  244. len = AV_RB32(buf); buf += 4; cur += 4;
  245. }
  246. if(len > 0) {
  247. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  248. if(tm2_read_deltas(ctx, stream_id) == -1)
  249. return -1;
  250. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  251. cur += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  252. }
  253. }
  254. /* skip unused fields */
  255. if(AV_RB32(buf) == TM2_ESCAPE) {
  256. buf += 4; cur += 4; /* some unknown length - could be escaped too */
  257. }
  258. buf += 4; cur += 4;
  259. buf += 4; cur += 4; /* unused by decoder */
  260. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  261. if(tm2_build_huff_table(ctx, &codes) == -1)
  262. return -1;
  263. buf += ((get_bits_count(&ctx->gb) + 31) >> 5) << 2;
  264. cur += ((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 = AV_RB32(buf); buf += 4; cur += 4;
  275. if(len > 0) {
  276. init_get_bits(&ctx->gb, buf, (skip - cur) * 8);
  277. for(i = 0; i < toks; i++)
  278. ctx->tokens[stream_id][i] = tm2_get_token(&ctx->gb, &codes);
  279. } else {
  280. for(i = 0; i < toks; i++)
  281. ctx->tokens[stream_id][i] = codes.recode[0];
  282. }
  283. tm2_free_codes(&codes);
  284. return skip;
  285. }
  286. static inline int GET_TOK(TM2Context *ctx,int type) {
  287. if(ctx->tok_ptrs[type] >= ctx->tok_lens[type]) {
  288. 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]);
  289. return 0;
  290. }
  291. if(type <= TM2_MOT)
  292. return ctx->deltas[type][ctx->tokens[type][ctx->tok_ptrs[type]++]];
  293. return ctx->tokens[type][ctx->tok_ptrs[type]++];
  294. }
  295. /* blocks decoding routines */
  296. /* common Y, U, V pointers initialisation */
  297. #define TM2_INIT_POINTERS() \
  298. int *last, *clast; \
  299. int *Y, *U, *V;\
  300. int Ystride, Ustride, Vstride;\
  301. \
  302. Ystride = ctx->avctx->width;\
  303. Vstride = (ctx->avctx->width + 1) >> 1;\
  304. Ustride = (ctx->avctx->width + 1) >> 1;\
  305. Y = (ctx->cur?ctx->Y2:ctx->Y1) + by * 4 * Ystride + bx * 4;\
  306. V = (ctx->cur?ctx->V2:ctx->V1) + by * 2 * Vstride + bx * 2;\
  307. U = (ctx->cur?ctx->U2:ctx->U1) + by * 2 * Ustride + bx * 2;\
  308. last = ctx->last + bx * 4;\
  309. clast = ctx->clast + bx * 4;
  310. #define TM2_INIT_POINTERS_2() \
  311. int *Yo, *Uo, *Vo;\
  312. int oYstride, oUstride, oVstride;\
  313. \
  314. TM2_INIT_POINTERS();\
  315. oYstride = Ystride;\
  316. oVstride = Vstride;\
  317. oUstride = Ustride;\
  318. Yo = (ctx->cur?ctx->Y1:ctx->Y2) + by * 4 * oYstride + bx * 4;\
  319. Vo = (ctx->cur?ctx->V1:ctx->V2) + by * 2 * oVstride + bx * 2;\
  320. Uo = (ctx->cur?ctx->U1:ctx->U2) + by * 2 * oUstride + bx * 2;
  321. /* recalculate last and delta values for next blocks */
  322. #define TM2_RECALC_BLOCK(CHR, stride, last, CD) {\
  323. CD[0] = (CHR[1] - 128) - last[1];\
  324. CD[1] = (int)CHR[stride + 1] - (int)CHR[1];\
  325. last[0] = (int)CHR[stride + 0] - 128;\
  326. last[1] = (int)CHR[stride + 1] - 128;}
  327. /* common operations - add deltas to 4x4 block of luma or 2x2 blocks of chroma */
  328. static inline void tm2_apply_deltas(TM2Context *ctx, int* Y, int stride, int *deltas, int *last)
  329. {
  330. int ct, d;
  331. int i, j;
  332. for(j = 0; j < 4; j++){
  333. ct = ctx->D[j];
  334. for(i = 0; i < 4; i++){
  335. d = deltas[i + j * 4];
  336. ct += d;
  337. last[i] += ct;
  338. Y[i] = av_clip_uint8(last[i]);
  339. }
  340. Y += stride;
  341. ctx->D[j] = ct;
  342. }
  343. }
  344. static inline void tm2_high_chroma(int *data, int stride, int *last, int *CD, int *deltas)
  345. {
  346. int i, j;
  347. for(j = 0; j < 2; j++){
  348. for(i = 0; i < 2; i++){
  349. CD[j] += deltas[i + j * 2];
  350. last[i] += CD[j];
  351. data[i] = last[i] + 128;
  352. }
  353. data += stride;
  354. }
  355. }
  356. static inline void tm2_low_chroma(int *data, int stride, int *clast, int *CD, int *deltas, int bx)
  357. {
  358. int t;
  359. int l;
  360. int prev;
  361. if(bx > 0)
  362. prev = clast[-3];
  363. else
  364. prev = 0;
  365. t = (CD[0] + CD[1]) >> 1;
  366. l = (prev - CD[0] - CD[1] + clast[1]) >> 1;
  367. CD[1] = CD[0] + CD[1] - t;
  368. CD[0] = t;
  369. clast[0] = l;
  370. tm2_high_chroma(data, stride, clast, CD, deltas);
  371. }
  372. static inline void tm2_hi_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  373. {
  374. int i;
  375. int deltas[16];
  376. TM2_INIT_POINTERS();
  377. /* hi-res chroma */
  378. for(i = 0; i < 4; i++) {
  379. deltas[i] = GET_TOK(ctx, TM2_C_HI);
  380. deltas[i + 4] = GET_TOK(ctx, TM2_C_HI);
  381. }
  382. tm2_high_chroma(U, Ustride, clast, ctx->CD, deltas);
  383. tm2_high_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas + 4);
  384. /* hi-res luma */
  385. for(i = 0; i < 16; i++)
  386. deltas[i] = GET_TOK(ctx, TM2_L_HI);
  387. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  388. }
  389. static inline void tm2_med_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  390. {
  391. int i;
  392. int deltas[16];
  393. TM2_INIT_POINTERS();
  394. /* low-res chroma */
  395. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  396. deltas[1] = deltas[2] = deltas[3] = 0;
  397. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  398. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  399. deltas[1] = deltas[2] = deltas[3] = 0;
  400. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  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_low_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  407. {
  408. int i;
  409. int t1, t2;
  410. int deltas[16];
  411. TM2_INIT_POINTERS();
  412. /* low-res chroma */
  413. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  414. deltas[1] = deltas[2] = deltas[3] = 0;
  415. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  416. deltas[0] = GET_TOK(ctx, TM2_C_LO);
  417. deltas[1] = deltas[2] = deltas[3] = 0;
  418. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  419. /* low-res luma */
  420. for(i = 0; i < 16; i++)
  421. deltas[i] = 0;
  422. deltas[ 0] = GET_TOK(ctx, TM2_L_LO);
  423. deltas[ 2] = GET_TOK(ctx, TM2_L_LO);
  424. deltas[ 8] = GET_TOK(ctx, TM2_L_LO);
  425. deltas[10] = GET_TOK(ctx, TM2_L_LO);
  426. if(bx > 0)
  427. last[0] = (last[-1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3] + last[1]) >> 1;
  428. else
  429. last[0] = (last[1] - ctx->D[0] - ctx->D[1] - ctx->D[2] - ctx->D[3])>> 1;
  430. last[2] = (last[1] + last[3]) >> 1;
  431. t1 = ctx->D[0] + ctx->D[1];
  432. ctx->D[0] = t1 >> 1;
  433. ctx->D[1] = t1 - (t1 >> 1);
  434. t2 = ctx->D[2] + ctx->D[3];
  435. ctx->D[2] = t2 >> 1;
  436. ctx->D[3] = t2 - (t2 >> 1);
  437. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  438. }
  439. static inline void tm2_null_res_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  440. {
  441. int i;
  442. int ct;
  443. int left, right, diff;
  444. int deltas[16];
  445. TM2_INIT_POINTERS();
  446. /* null chroma */
  447. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  448. tm2_low_chroma(U, Ustride, clast, ctx->CD, deltas, bx);
  449. deltas[0] = deltas[1] = deltas[2] = deltas[3] = 0;
  450. tm2_low_chroma(V, Vstride, clast + 2, ctx->CD + 2, deltas, bx);
  451. /* null luma */
  452. for(i = 0; i < 16; i++)
  453. deltas[i] = 0;
  454. ct = ctx->D[0] + ctx->D[1] + ctx->D[2] + ctx->D[3];
  455. if(bx > 0)
  456. left = last[-1] - ct;
  457. else
  458. left = 0;
  459. right = last[3];
  460. diff = right - left;
  461. last[0] = left + (diff >> 2);
  462. last[1] = left + (diff >> 1);
  463. last[2] = right - (diff >> 2);
  464. last[3] = right;
  465. {
  466. int tp = left;
  467. ctx->D[0] = (tp + (ct >> 2)) - left;
  468. left += ctx->D[0];
  469. ctx->D[1] = (tp + (ct >> 1)) - left;
  470. left += ctx->D[1];
  471. ctx->D[2] = ((tp + ct) - (ct >> 2)) - left;
  472. left += ctx->D[2];
  473. ctx->D[3] = (tp + ct) - left;
  474. }
  475. tm2_apply_deltas(ctx, Y, Ystride, deltas, last);
  476. }
  477. static inline void tm2_still_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  478. {
  479. int i, j;
  480. TM2_INIT_POINTERS_2();
  481. /* update chroma */
  482. for(j = 0; j < 2; j++){
  483. for(i = 0; i < 2; i++){
  484. U[i] = Uo[i];
  485. V[i] = Vo[i];
  486. }
  487. U += Ustride; V += Vstride;
  488. Uo += oUstride; Vo += oVstride;
  489. }
  490. U -= Ustride * 2;
  491. V -= Vstride * 2;
  492. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  493. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  494. /* update deltas */
  495. ctx->D[0] = Yo[3] - last[3];
  496. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  497. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  498. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  499. for(j = 0; j < 4; j++){
  500. for(i = 0; i < 4; i++){
  501. Y[i] = Yo[i];
  502. last[i] = Yo[i];
  503. }
  504. Y += Ystride;
  505. Yo += oYstride;
  506. }
  507. }
  508. static inline void tm2_update_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  509. {
  510. int i, j;
  511. int d;
  512. TM2_INIT_POINTERS_2();
  513. /* update chroma */
  514. for(j = 0; j < 2; j++){
  515. for(i = 0; i < 2; i++){
  516. U[i] = Uo[i] + GET_TOK(ctx, TM2_UPD);
  517. V[i] = Vo[i] + GET_TOK(ctx, TM2_UPD);
  518. }
  519. U += Ustride; V += Vstride;
  520. Uo += oUstride; Vo += oVstride;
  521. }
  522. U -= Ustride * 2;
  523. V -= Vstride * 2;
  524. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  525. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  526. /* update deltas */
  527. ctx->D[0] = Yo[3] - last[3];
  528. ctx->D[1] = Yo[3 + oYstride] - Yo[3];
  529. ctx->D[2] = Yo[3 + oYstride * 2] - Yo[3 + oYstride];
  530. ctx->D[3] = Yo[3 + oYstride * 3] - Yo[3 + oYstride * 2];
  531. for(j = 0; j < 4; j++){
  532. d = last[3];
  533. for(i = 0; i < 4; i++){
  534. Y[i] = Yo[i] + GET_TOK(ctx, TM2_UPD);
  535. last[i] = Y[i];
  536. }
  537. ctx->D[j] = last[3] - d;
  538. Y += Ystride;
  539. Yo += oYstride;
  540. }
  541. }
  542. static inline void tm2_motion_block(TM2Context *ctx, AVFrame *pic, int bx, int by)
  543. {
  544. int i, j;
  545. int mx, my;
  546. TM2_INIT_POINTERS_2();
  547. mx = GET_TOK(ctx, TM2_MOT);
  548. my = GET_TOK(ctx, TM2_MOT);
  549. Yo += my * oYstride + mx;
  550. Uo += (my >> 1) * oUstride + (mx >> 1);
  551. Vo += (my >> 1) * oVstride + (mx >> 1);
  552. /* copy chroma */
  553. for(j = 0; j < 2; j++){
  554. for(i = 0; i < 2; i++){
  555. U[i] = Uo[i];
  556. V[i] = Vo[i];
  557. }
  558. U += Ustride; V += Vstride;
  559. Uo += oUstride; Vo += oVstride;
  560. }
  561. U -= Ustride * 2;
  562. V -= Vstride * 2;
  563. TM2_RECALC_BLOCK(U, Ustride, clast, ctx->CD);
  564. TM2_RECALC_BLOCK(V, Vstride, (clast + 2), (ctx->CD + 2));
  565. /* copy luma */
  566. for(j = 0; j < 4; j++){
  567. for(i = 0; i < 4; i++){
  568. Y[i] = Yo[i];
  569. }
  570. Y += Ystride;
  571. Yo += oYstride;
  572. }
  573. /* calculate deltas */
  574. Y -= Ystride * 4;
  575. ctx->D[0] = Y[3] - last[3];
  576. ctx->D[1] = Y[3 + Ystride] - Y[3];
  577. ctx->D[2] = Y[3 + Ystride * 2] - Y[3 + Ystride];
  578. ctx->D[3] = Y[3 + Ystride * 3] - Y[3 + Ystride * 2];
  579. for(i = 0; i < 4; i++)
  580. last[i] = Y[i + Ystride * 3];
  581. }
  582. static int tm2_decode_blocks(TM2Context *ctx, AVFrame *p)
  583. {
  584. int i, j;
  585. int bw, bh;
  586. int type;
  587. int keyframe = 1;
  588. uint8_t *Y, *U, *V;
  589. int *src;
  590. bw = ctx->avctx->width >> 2;
  591. bh = ctx->avctx->height >> 2;
  592. for(i = 0; i < TM2_NUM_STREAMS; i++)
  593. ctx->tok_ptrs[i] = 0;
  594. if (ctx->tok_lens[TM2_TYPE]<bw*bh){
  595. av_log(ctx->avctx,AV_LOG_ERROR,"Got %i tokens for %i blocks\n",ctx->tok_lens[TM2_TYPE],bw*bh);
  596. return -1;
  597. }
  598. memset(ctx->last, 0, 4 * bw * sizeof(int));
  599. memset(ctx->clast, 0, 4 * bw * sizeof(int));
  600. for(j = 0; j < bh; j++) {
  601. memset(ctx->D, 0, 4 * sizeof(int));
  602. memset(ctx->CD, 0, 4 * sizeof(int));
  603. for(i = 0; i < bw; i++) {
  604. type = GET_TOK(ctx, TM2_TYPE);
  605. switch(type) {
  606. case TM2_HI_RES:
  607. tm2_hi_res_block(ctx, p, i, j);
  608. break;
  609. case TM2_MED_RES:
  610. tm2_med_res_block(ctx, p, i, j);
  611. break;
  612. case TM2_LOW_RES:
  613. tm2_low_res_block(ctx, p, i, j);
  614. break;
  615. case TM2_NULL_RES:
  616. tm2_null_res_block(ctx, p, i, j);
  617. break;
  618. case TM2_UPDATE:
  619. tm2_update_block(ctx, p, i, j);
  620. keyframe = 0;
  621. break;
  622. case TM2_STILL:
  623. tm2_still_block(ctx, p, i, j);
  624. keyframe = 0;
  625. break;
  626. case TM2_MOTION:
  627. tm2_motion_block(ctx, p, i, j);
  628. keyframe = 0;
  629. break;
  630. default:
  631. av_log(ctx->avctx, AV_LOG_ERROR, "Skipping unknown block type %i\n", type);
  632. }
  633. }
  634. }
  635. /* copy data from our buffer to AVFrame */
  636. Y = p->data[0];
  637. src = (ctx->cur?ctx->Y2:ctx->Y1);
  638. for(j = 0; j < ctx->avctx->height; j++){
  639. for(i = 0; i < ctx->avctx->width; i++){
  640. Y[i] = av_clip_uint8(*src++);
  641. }
  642. Y += p->linesize[0];
  643. }
  644. U = p->data[2];
  645. src = (ctx->cur?ctx->U2:ctx->U1);
  646. for(j = 0; j < (ctx->avctx->height + 1) >> 1; j++){
  647. for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){
  648. U[i] = av_clip_uint8(*src++);
  649. }
  650. U += p->linesize[2];
  651. }
  652. V = p->data[1];
  653. src = (ctx->cur?ctx->V2:ctx->V1);
  654. for(j = 0; j < (ctx->avctx->height + 1) >> 1; j++){
  655. for(i = 0; i < (ctx->avctx->width + 1) >> 1; i++){
  656. V[i] = av_clip_uint8(*src++);
  657. }
  658. V += p->linesize[1];
  659. }
  660. return keyframe;
  661. }
  662. static int decode_frame(AVCodecContext *avctx,
  663. void *data, int *data_size,
  664. uint8_t *buf, int buf_size)
  665. {
  666. TM2Context * const l = avctx->priv_data;
  667. AVFrame * const p= (AVFrame*)&l->pic;
  668. int skip, t;
  669. p->reference = 1;
  670. p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  671. if(avctx->reget_buffer(avctx, p) < 0){
  672. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  673. return -1;
  674. }
  675. l->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, buf_size >> 2);
  676. skip = tm2_read_header(l, buf);
  677. if(skip == -1)
  678. return -1;
  679. t = tm2_read_stream(l, buf + skip, TM2_C_HI);
  680. if(t == -1)
  681. return -1;
  682. skip += t;
  683. t = tm2_read_stream(l, buf + skip, TM2_C_LO);
  684. if(t == -1)
  685. return -1;
  686. skip += t;
  687. t = tm2_read_stream(l, buf + skip, TM2_L_HI);
  688. if(t == -1)
  689. return -1;
  690. skip += t;
  691. t = tm2_read_stream(l, buf + skip, TM2_L_LO);
  692. if(t == -1)
  693. return -1;
  694. skip += t;
  695. t = tm2_read_stream(l, buf + skip, TM2_UPD);
  696. if(t == -1)
  697. return -1;
  698. skip += t;
  699. t = tm2_read_stream(l, buf + skip, TM2_MOT);
  700. if(t == -1)
  701. return -1;
  702. skip += t;
  703. t = tm2_read_stream(l, buf + skip, TM2_TYPE);
  704. if(t == -1)
  705. return -1;
  706. p->key_frame = tm2_decode_blocks(l, p);
  707. if(p->key_frame)
  708. p->pict_type = FF_I_TYPE;
  709. else
  710. p->pict_type = FF_P_TYPE;
  711. l->cur = !l->cur;
  712. *data_size = sizeof(AVFrame);
  713. *(AVFrame*)data = l->pic;
  714. return buf_size;
  715. }
  716. static int decode_init(AVCodecContext *avctx){
  717. TM2Context * const l = avctx->priv_data;
  718. int i;
  719. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  720. return -1;
  721. }
  722. if((avctx->width & 3) || (avctx->height & 3)){
  723. av_log(avctx, AV_LOG_ERROR, "Width and height must be multiple of 4\n");
  724. return -1;
  725. }
  726. l->avctx = avctx;
  727. l->pic.data[0]=NULL;
  728. avctx->pix_fmt = PIX_FMT_YUV420P;
  729. dsputil_init(&l->dsp, avctx);
  730. l->last = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  731. l->clast = av_malloc(4 * sizeof(int) * (avctx->width >> 2));
  732. for(i = 0; i < TM2_NUM_STREAMS; i++) {
  733. l->tokens[i] = NULL;
  734. l->tok_lens[i] = 0;
  735. }
  736. l->Y1 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  737. l->U1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  738. l->V1 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  739. l->Y2 = av_malloc(sizeof(int) * avctx->width * avctx->height);
  740. l->U2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  741. l->V2 = av_malloc(sizeof(int) * ((avctx->width + 1) >> 1) * ((avctx->height + 1) >> 1));
  742. l->cur = 0;
  743. return 0;
  744. }
  745. static int decode_end(AVCodecContext *avctx){
  746. TM2Context * const l = avctx->priv_data;
  747. int i;
  748. if(l->last)
  749. av_free(l->last);
  750. if(l->clast)
  751. av_free(l->clast);
  752. for(i = 0; i < TM2_NUM_STREAMS; i++)
  753. if(l->tokens[i])
  754. av_free(l->tokens[i]);
  755. if(l->Y1){
  756. av_free(l->Y1);
  757. av_free(l->U1);
  758. av_free(l->V1);
  759. av_free(l->Y2);
  760. av_free(l->U2);
  761. av_free(l->V2);
  762. }
  763. return 0;
  764. }
  765. AVCodec truemotion2_decoder = {
  766. "truemotion2",
  767. CODEC_TYPE_VIDEO,
  768. CODEC_ID_TRUEMOTION2,
  769. sizeof(TM2Context),
  770. decode_init,
  771. NULL,
  772. decode_end,
  773. decode_frame,
  774. CODEC_CAP_DR1,
  775. };