truemotion2.c 25 KB

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