truemotion1.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * Duck TrueMotion 1.0 Decoder
  3. * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
  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 truemotion1.c
  23. * Duck TrueMotion v1 Video Decoder by
  24. * Alex Beregszaszi (alex@fsn.hu) and
  25. * Mike Melanson (melanson@pcisys.net)
  26. *
  27. * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and
  28. * outputs RGB555 (or RGB565) data. 24-bit TM1 data is not supported yet.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "common.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #include "truemotion1data.h"
  38. typedef struct TrueMotion1Context {
  39. AVCodecContext *avctx;
  40. AVFrame frame;
  41. AVFrame prev_frame;
  42. uint8_t *buf;
  43. int size;
  44. uint8_t *mb_change_bits;
  45. int mb_change_bits_row_size;
  46. uint8_t *index_stream;
  47. int index_stream_size;
  48. int flags;
  49. int x, y, w, h;
  50. uint32_t y_predictor_table[1024];
  51. uint32_t c_predictor_table[1024];
  52. uint32_t fat_y_predictor_table[1024];
  53. uint32_t fat_c_predictor_table[1024];
  54. int compression;
  55. int block_type;
  56. int block_width;
  57. int block_height;
  58. int16_t ydt[8];
  59. int16_t cdt[8];
  60. int16_t fat_ydt[8];
  61. int16_t fat_cdt[8];
  62. int last_deltaset, last_vectable;
  63. unsigned int *vert_pred;
  64. } TrueMotion1Context;
  65. #define FLAG_SPRITE 32
  66. #define FLAG_KEYFRAME 16
  67. #define FLAG_INTERFRAME 8
  68. #define FLAG_INTERPOLATED 4
  69. struct frame_header {
  70. uint8_t header_size;
  71. uint8_t compression;
  72. uint8_t deltaset;
  73. uint8_t vectable;
  74. uint16_t ysize;
  75. uint16_t xsize;
  76. uint16_t checksum;
  77. uint8_t version;
  78. uint8_t header_type;
  79. uint8_t flags;
  80. uint8_t control;
  81. uint16_t xoffset;
  82. uint16_t yoffset;
  83. uint16_t width;
  84. uint16_t height;
  85. };
  86. #define ALGO_NOP 0
  87. #define ALGO_RGB16V 1
  88. #define ALGO_RGB16H 2
  89. #define ALGO_RGB24H 3
  90. /* these are the various block sizes that can occupy a 4x4 block */
  91. #define BLOCK_2x2 0
  92. #define BLOCK_2x4 1
  93. #define BLOCK_4x2 2
  94. #define BLOCK_4x4 3
  95. typedef struct comp_types {
  96. int algorithm;
  97. int block_width; // vres
  98. int block_height; // hres
  99. int block_type;
  100. } comp_types;
  101. /* { valid for metatype }, algorithm, num of deltas, vert res, horiz res */
  102. static comp_types compression_types[17] = {
  103. { ALGO_NOP, 0, 0, 0 },
  104. { ALGO_RGB16V, 4, 4, BLOCK_4x4 },
  105. { ALGO_RGB16H, 4, 4, BLOCK_4x4 },
  106. { ALGO_RGB16V, 4, 2, BLOCK_4x2 },
  107. { ALGO_RGB16H, 4, 2, BLOCK_4x2 },
  108. { ALGO_RGB16V, 2, 4, BLOCK_2x4 },
  109. { ALGO_RGB16H, 2, 4, BLOCK_2x4 },
  110. { ALGO_RGB16V, 2, 2, BLOCK_2x2 },
  111. { ALGO_RGB16H, 2, 2, BLOCK_2x2 },
  112. { ALGO_NOP, 4, 4, BLOCK_4x4 },
  113. { ALGO_RGB24H, 4, 4, BLOCK_4x4 },
  114. { ALGO_NOP, 4, 2, BLOCK_4x2 },
  115. { ALGO_RGB24H, 4, 2, BLOCK_4x2 },
  116. { ALGO_NOP, 2, 4, BLOCK_2x4 },
  117. { ALGO_RGB24H, 2, 4, BLOCK_2x4 },
  118. { ALGO_NOP, 2, 2, BLOCK_2x2 },
  119. { ALGO_RGB24H, 2, 2, BLOCK_2x2 }
  120. };
  121. static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
  122. {
  123. int i;
  124. if (delta_table_index > 3)
  125. return;
  126. memcpy(s->ydt, ydts[delta_table_index], 8 * sizeof(int16_t));
  127. memcpy(s->cdt, cdts[delta_table_index], 8 * sizeof(int16_t));
  128. memcpy(s->fat_ydt, fat_ydts[delta_table_index], 8 * sizeof(int16_t));
  129. memcpy(s->fat_cdt, fat_cdts[delta_table_index], 8 * sizeof(int16_t));
  130. /* Y skinny deltas need to be halved for some reason; maybe the
  131. * skinny Y deltas should be modified */
  132. for (i = 0; i < 8; i++)
  133. {
  134. /* drop the lsb before dividing by 2-- net effect: round down
  135. * when dividing a negative number (e.g., -3/2 = -2, not -1) */
  136. s->ydt[i] &= 0xFFFE;
  137. s->ydt[i] /= 2;
  138. }
  139. }
  140. #ifdef WORDS_BIGENDIAN
  141. static int make_ydt15_entry(int p2, int p1, int16_t *ydt)
  142. #else
  143. static int make_ydt15_entry(int p1, int p2, int16_t *ydt)
  144. #endif
  145. {
  146. int lo, hi;
  147. lo = ydt[p1];
  148. lo += (lo << 5) + (lo << 10);
  149. hi = ydt[p2];
  150. hi += (hi << 5) + (hi << 10);
  151. return ((lo + (hi << 16)) << 1);
  152. }
  153. #ifdef WORDS_BIGENDIAN
  154. static int make_cdt15_entry(int p2, int p1, int16_t *cdt)
  155. #else
  156. static int make_cdt15_entry(int p1, int p2, int16_t *cdt)
  157. #endif
  158. {
  159. int r, b, lo;
  160. b = cdt[p2];
  161. r = cdt[p1] << 10;
  162. lo = b + r;
  163. return ((lo + (lo << 16)) << 1);
  164. }
  165. #ifdef WORDS_BIGENDIAN
  166. static int make_ydt16_entry(int p2, int p1, int16_t *ydt)
  167. #else
  168. static int make_ydt16_entry(int p1, int p2, int16_t *ydt)
  169. #endif
  170. {
  171. int lo, hi;
  172. lo = ydt[p1];
  173. lo += (lo << 6) + (lo << 11);
  174. hi = ydt[p2];
  175. hi += (hi << 6) + (hi << 11);
  176. return ((lo + (hi << 16)) << 1);
  177. }
  178. #ifdef WORDS_BIGENDIAN
  179. static int make_cdt16_entry(int p2, int p1, int16_t *cdt)
  180. #else
  181. static int make_cdt16_entry(int p1, int p2, int16_t *cdt)
  182. #endif
  183. {
  184. int r, b, lo;
  185. b = cdt[p2];
  186. r = cdt[p1] << 11;
  187. lo = b + r;
  188. return ((lo + (lo << 16)) << 1);
  189. }
  190. #ifdef WORDS_BIGENDIAN
  191. static int make_ydt24_entry(int p2, int p1, int16_t *ydt)
  192. #else
  193. static int make_ydt24_entry(int p1, int p2, int16_t *ydt)
  194. #endif
  195. {
  196. int lo, hi;
  197. lo = ydt[p1];
  198. hi = ydt[p2];
  199. return ((lo + (hi << 8) + (hi << 16)) << 1);
  200. }
  201. #ifdef WORDS_BIGENDIAN
  202. static int make_cdt24_entry(int p2, int p1, int16_t *cdt)
  203. #else
  204. static int make_cdt24_entry(int p1, int p2, int16_t *cdt)
  205. #endif
  206. {
  207. int r, b;
  208. b = cdt[p2];
  209. r = cdt[p1]<<16;
  210. return ((b+r) << 1);
  211. }
  212. static void gen_vector_table15(TrueMotion1Context *s, const uint8_t *sel_vector_table)
  213. {
  214. int len, i, j;
  215. unsigned char delta_pair;
  216. for (i = 0; i < 1024; i += 4)
  217. {
  218. len = *sel_vector_table++ / 2;
  219. for (j = 0; j < len; j++)
  220. {
  221. delta_pair = *sel_vector_table++;
  222. s->y_predictor_table[i+j] = 0xfffffffe &
  223. make_ydt15_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
  224. s->c_predictor_table[i+j] = 0xfffffffe &
  225. make_cdt15_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
  226. }
  227. s->y_predictor_table[i+(j-1)] |= 1;
  228. s->c_predictor_table[i+(j-1)] |= 1;
  229. }
  230. }
  231. static void gen_vector_table16(TrueMotion1Context *s, const uint8_t *sel_vector_table)
  232. {
  233. int len, i, j;
  234. unsigned char delta_pair;
  235. for (i = 0; i < 1024; i += 4)
  236. {
  237. len = *sel_vector_table++ / 2;
  238. for (j = 0; j < len; j++)
  239. {
  240. delta_pair = *sel_vector_table++;
  241. s->y_predictor_table[i+j] = 0xfffffffe &
  242. make_ydt16_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
  243. s->c_predictor_table[i+j] = 0xfffffffe &
  244. make_cdt16_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
  245. }
  246. s->y_predictor_table[i+(j-1)] |= 1;
  247. s->c_predictor_table[i+(j-1)] |= 1;
  248. }
  249. }
  250. static void gen_vector_table24(TrueMotion1Context *s, const uint8_t *sel_vector_table)
  251. {
  252. int len, i, j;
  253. unsigned char delta_pair;
  254. for (i = 0; i < 1024; i += 4)
  255. {
  256. len = *sel_vector_table++ / 2;
  257. for (j = 0; j < len; j++)
  258. {
  259. delta_pair = *sel_vector_table++;
  260. s->y_predictor_table[i+j] = 0xfffffffe &
  261. make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
  262. s->c_predictor_table[i+j] = 0xfffffffe &
  263. make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
  264. s->fat_y_predictor_table[i+j] = 0xfffffffe &
  265. make_ydt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_ydt);
  266. s->fat_c_predictor_table[i+j] = 0xfffffffe &
  267. make_cdt24_entry(delta_pair >> 4, delta_pair & 0xf, s->fat_cdt);
  268. }
  269. s->y_predictor_table[i+(j-1)] |= 1;
  270. s->c_predictor_table[i+(j-1)] |= 1;
  271. s->fat_y_predictor_table[i+(j-1)] |= 1;
  272. s->fat_c_predictor_table[i+(j-1)] |= 1;
  273. }
  274. }
  275. /* Returns the number of bytes consumed from the bytestream. Returns -1 if
  276. * there was an error while decoding the header */
  277. static int truemotion1_decode_header(TrueMotion1Context *s)
  278. {
  279. int i;
  280. struct frame_header header;
  281. uint8_t header_buffer[128]; /* logical maximum size of the header */
  282. const uint8_t *sel_vector_table;
  283. /* There is 1 change bit per 4 pixels, so each change byte represents
  284. * 32 pixels; divide width by 4 to obtain the number of change bits and
  285. * then round up to the nearest byte. */
  286. s->mb_change_bits_row_size = ((s->avctx->width >> 2) + 7) >> 3;
  287. header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
  288. if (s->buf[0] < 0x10)
  289. {
  290. av_log(s->avctx, AV_LOG_ERROR, "invalid header size (%d)\n", s->buf[0]);
  291. return -1;
  292. }
  293. /* unscramble the header bytes with a XOR operation */
  294. memset(header_buffer, 0, 128);
  295. for (i = 1; i < header.header_size; i++)
  296. header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
  297. header.compression = header_buffer[0];
  298. header.deltaset = header_buffer[1];
  299. header.vectable = header_buffer[2];
  300. header.ysize = AV_RL16(&header_buffer[3]);
  301. header.xsize = AV_RL16(&header_buffer[5]);
  302. header.checksum = AV_RL16(&header_buffer[7]);
  303. header.version = header_buffer[9];
  304. header.header_type = header_buffer[10];
  305. header.flags = header_buffer[11];
  306. header.control = header_buffer[12];
  307. /* Version 2 */
  308. if (header.version >= 2)
  309. {
  310. if (header.header_type > 3)
  311. {
  312. av_log(s->avctx, AV_LOG_ERROR, "invalid header type (%d)\n", header.header_type);
  313. return -1;
  314. } else if ((header.header_type == 2) || (header.header_type == 3)) {
  315. s->flags = header.flags;
  316. if (!(s->flags & FLAG_INTERFRAME))
  317. s->flags |= FLAG_KEYFRAME;
  318. } else
  319. s->flags = FLAG_KEYFRAME;
  320. } else /* Version 1 */
  321. s->flags = FLAG_KEYFRAME;
  322. if (s->flags & FLAG_SPRITE) {
  323. av_log(s->avctx, AV_LOG_INFO, "SPRITE frame found, please report the sample to the developers\n");
  324. /* FIXME header.width, height, xoffset and yoffset aren't initialized */
  325. #if 0
  326. s->w = header.width;
  327. s->h = header.height;
  328. s->x = header.xoffset;
  329. s->y = header.yoffset;
  330. #else
  331. return -1;
  332. #endif
  333. } else {
  334. s->w = header.xsize;
  335. s->h = header.ysize;
  336. if (header.header_type < 2) {
  337. if ((s->w < 213) && (s->h >= 176))
  338. {
  339. s->flags |= FLAG_INTERPOLATED;
  340. av_log(s->avctx, AV_LOG_INFO, "INTERPOLATION selected, please report the sample to the developers\n");
  341. }
  342. }
  343. }
  344. if (header.compression > 17) {
  345. av_log(s->avctx, AV_LOG_ERROR, "invalid compression type (%d)\n", header.compression);
  346. return -1;
  347. }
  348. if ((header.deltaset != s->last_deltaset) ||
  349. (header.vectable != s->last_vectable))
  350. select_delta_tables(s, header.deltaset);
  351. if ((header.compression & 1) && header.header_type)
  352. sel_vector_table = pc_tbl2;
  353. else {
  354. if (header.vectable < 4)
  355. sel_vector_table = tables[header.vectable - 1];
  356. else {
  357. av_log(s->avctx, AV_LOG_ERROR, "invalid vector table id (%d)\n", header.vectable);
  358. return -1;
  359. }
  360. }
  361. // FIXME: where to place this ?!?!
  362. if (compression_types[header.compression].algorithm == ALGO_RGB24H)
  363. s->avctx->pix_fmt = PIX_FMT_RGB32;
  364. else
  365. s->avctx->pix_fmt = PIX_FMT_RGB555; // RGB565 is supported as well
  366. if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
  367. {
  368. if (compression_types[header.compression].algorithm == ALGO_RGB24H)
  369. gen_vector_table24(s, sel_vector_table);
  370. else
  371. if (s->avctx->pix_fmt == PIX_FMT_RGB555)
  372. gen_vector_table15(s, sel_vector_table);
  373. else
  374. gen_vector_table16(s, sel_vector_table);
  375. }
  376. /* set up pointers to the other key data chunks */
  377. s->mb_change_bits = s->buf + header.header_size;
  378. if (s->flags & FLAG_KEYFRAME) {
  379. /* no change bits specified for a keyframe; only index bytes */
  380. s->index_stream = s->mb_change_bits;
  381. } else {
  382. /* one change bit per 4x4 block */
  383. s->index_stream = s->mb_change_bits +
  384. (s->mb_change_bits_row_size * (s->avctx->height >> 2));
  385. }
  386. s->index_stream_size = s->size - (s->index_stream - s->buf);
  387. s->last_deltaset = header.deltaset;
  388. s->last_vectable = header.vectable;
  389. s->compression = header.compression;
  390. s->block_width = compression_types[header.compression].block_width;
  391. s->block_height = compression_types[header.compression].block_height;
  392. s->block_type = compression_types[header.compression].block_type;
  393. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  394. av_log(s->avctx, AV_LOG_INFO, "tables: %d / %d c:%d %dx%d t:%d %s%s%s%s\n",
  395. s->last_deltaset, s->last_vectable, s->compression, s->block_width,
  396. s->block_height, s->block_type,
  397. s->flags & FLAG_KEYFRAME ? " KEY" : "",
  398. s->flags & FLAG_INTERFRAME ? " INTER" : "",
  399. s->flags & FLAG_SPRITE ? " SPRITE" : "",
  400. s->flags & FLAG_INTERPOLATED ? " INTERPOL" : "");
  401. return header.header_size;
  402. }
  403. static int truemotion1_decode_init(AVCodecContext *avctx)
  404. {
  405. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  406. s->avctx = avctx;
  407. // FIXME: it may change ?
  408. // if (avctx->bits_per_sample == 24)
  409. // avctx->pix_fmt = PIX_FMT_RGB24;
  410. // else
  411. // avctx->pix_fmt = PIX_FMT_RGB555;
  412. avctx->has_b_frames = 0;
  413. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  414. /* there is a vertical predictor for each pixel in a line; each vertical
  415. * predictor is 0 to start with */
  416. s->vert_pred =
  417. (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned int));
  418. return 0;
  419. }
  420. /*
  421. Block decoding order:
  422. dxi: Y-Y
  423. dxic: Y-C-Y
  424. dxic2: Y-C-Y-C
  425. hres,vres,i,i%vres (0 < i < 4)
  426. 2x2 0: 0 dxic2
  427. 2x2 1: 1 dxi
  428. 2x2 2: 0 dxic2
  429. 2x2 3: 1 dxi
  430. 2x4 0: 0 dxic2
  431. 2x4 1: 1 dxi
  432. 2x4 2: 2 dxi
  433. 2x4 3: 3 dxi
  434. 4x2 0: 0 dxic
  435. 4x2 1: 1 dxi
  436. 4x2 2: 0 dxic
  437. 4x2 3: 1 dxi
  438. 4x4 0: 0 dxic
  439. 4x4 1: 1 dxi
  440. 4x4 2: 2 dxi
  441. 4x4 3: 3 dxi
  442. */
  443. #define GET_NEXT_INDEX() \
  444. {\
  445. if (index_stream_index >= s->index_stream_size) { \
  446. av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
  447. return; \
  448. } \
  449. index = s->index_stream[index_stream_index++] * 4; \
  450. }
  451. #define APPLY_C_PREDICTOR() \
  452. predictor_pair = s->c_predictor_table[index]; \
  453. horiz_pred += (predictor_pair >> 1); \
  454. if (predictor_pair & 1) { \
  455. GET_NEXT_INDEX() \
  456. if (!index) { \
  457. GET_NEXT_INDEX() \
  458. predictor_pair = s->c_predictor_table[index]; \
  459. horiz_pred += ((predictor_pair >> 1) * 5); \
  460. if (predictor_pair & 1) \
  461. GET_NEXT_INDEX() \
  462. else \
  463. index++; \
  464. } \
  465. } else \
  466. index++;
  467. #define APPLY_C_PREDICTOR_24() \
  468. predictor_pair = s->c_predictor_table[index]; \
  469. horiz_pred += (predictor_pair >> 1); \
  470. if (predictor_pair & 1) { \
  471. GET_NEXT_INDEX() \
  472. if (!index) { \
  473. GET_NEXT_INDEX() \
  474. predictor_pair = s->fat_c_predictor_table[index]; \
  475. horiz_pred += (predictor_pair >> 1); \
  476. if (predictor_pair & 1) \
  477. GET_NEXT_INDEX() \
  478. else \
  479. index++; \
  480. } \
  481. } else \
  482. index++;
  483. #define APPLY_Y_PREDICTOR() \
  484. predictor_pair = s->y_predictor_table[index]; \
  485. horiz_pred += (predictor_pair >> 1); \
  486. if (predictor_pair & 1) { \
  487. GET_NEXT_INDEX() \
  488. if (!index) { \
  489. GET_NEXT_INDEX() \
  490. predictor_pair = s->y_predictor_table[index]; \
  491. horiz_pred += ((predictor_pair >> 1) * 5); \
  492. if (predictor_pair & 1) \
  493. GET_NEXT_INDEX() \
  494. else \
  495. index++; \
  496. } \
  497. } else \
  498. index++;
  499. #define APPLY_Y_PREDICTOR_24() \
  500. predictor_pair = s->y_predictor_table[index]; \
  501. horiz_pred += (predictor_pair >> 1); \
  502. if (predictor_pair & 1) { \
  503. GET_NEXT_INDEX() \
  504. if (!index) { \
  505. GET_NEXT_INDEX() \
  506. predictor_pair = s->fat_y_predictor_table[index]; \
  507. horiz_pred += (predictor_pair >> 1); \
  508. if (predictor_pair & 1) \
  509. GET_NEXT_INDEX() \
  510. else \
  511. index++; \
  512. } \
  513. } else \
  514. index++;
  515. #define OUTPUT_PIXEL_PAIR() \
  516. *current_pixel_pair = *vert_pred + horiz_pred; \
  517. *vert_pred++ = *current_pixel_pair++; \
  518. prev_pixel_pair++;
  519. static void truemotion1_decode_16bit(TrueMotion1Context *s)
  520. {
  521. int y;
  522. int pixels_left; /* remaining pixels on this line */
  523. unsigned int predictor_pair;
  524. unsigned int horiz_pred;
  525. unsigned int *vert_pred;
  526. unsigned int *current_pixel_pair;
  527. unsigned int *prev_pixel_pair;
  528. unsigned char *current_line = s->frame.data[0];
  529. unsigned char *prev_line = s->prev_frame.data[0];
  530. int keyframe = s->flags & FLAG_KEYFRAME;
  531. /* these variables are for managing the stream of macroblock change bits */
  532. unsigned char *mb_change_bits = s->mb_change_bits;
  533. unsigned char mb_change_byte;
  534. unsigned char mb_change_byte_mask;
  535. int mb_change_index;
  536. /* these variables are for managing the main index stream */
  537. int index_stream_index = 0; /* yes, the index into the index stream */
  538. int index;
  539. /* clean out the line buffer */
  540. memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
  541. GET_NEXT_INDEX();
  542. for (y = 0; y < s->avctx->height; y++) {
  543. /* re-init variables for the next line iteration */
  544. horiz_pred = 0;
  545. current_pixel_pair = (unsigned int *)current_line;
  546. prev_pixel_pair = (unsigned int *)prev_line;
  547. vert_pred = s->vert_pred;
  548. mb_change_index = 0;
  549. mb_change_byte = mb_change_bits[mb_change_index++];
  550. mb_change_byte_mask = 0x01;
  551. pixels_left = s->avctx->width;
  552. while (pixels_left > 0) {
  553. if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
  554. switch (y & 3) {
  555. case 0:
  556. /* if macroblock width is 2, apply C-Y-C-Y; else
  557. * apply C-Y-Y */
  558. if (s->block_width == 2) {
  559. APPLY_C_PREDICTOR();
  560. APPLY_Y_PREDICTOR();
  561. OUTPUT_PIXEL_PAIR();
  562. APPLY_C_PREDICTOR();
  563. APPLY_Y_PREDICTOR();
  564. OUTPUT_PIXEL_PAIR();
  565. } else {
  566. APPLY_C_PREDICTOR();
  567. APPLY_Y_PREDICTOR();
  568. OUTPUT_PIXEL_PAIR();
  569. APPLY_Y_PREDICTOR();
  570. OUTPUT_PIXEL_PAIR();
  571. }
  572. break;
  573. case 1:
  574. case 3:
  575. /* always apply 2 Y predictors on these iterations */
  576. APPLY_Y_PREDICTOR();
  577. OUTPUT_PIXEL_PAIR();
  578. APPLY_Y_PREDICTOR();
  579. OUTPUT_PIXEL_PAIR();
  580. break;
  581. case 2:
  582. /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
  583. * depending on the macroblock type */
  584. if (s->block_type == BLOCK_2x2) {
  585. APPLY_C_PREDICTOR();
  586. APPLY_Y_PREDICTOR();
  587. OUTPUT_PIXEL_PAIR();
  588. APPLY_C_PREDICTOR();
  589. APPLY_Y_PREDICTOR();
  590. OUTPUT_PIXEL_PAIR();
  591. } else if (s->block_type == BLOCK_4x2) {
  592. APPLY_C_PREDICTOR();
  593. APPLY_Y_PREDICTOR();
  594. OUTPUT_PIXEL_PAIR();
  595. APPLY_Y_PREDICTOR();
  596. OUTPUT_PIXEL_PAIR();
  597. } else {
  598. APPLY_Y_PREDICTOR();
  599. OUTPUT_PIXEL_PAIR();
  600. APPLY_Y_PREDICTOR();
  601. OUTPUT_PIXEL_PAIR();
  602. }
  603. break;
  604. }
  605. } else {
  606. /* skip (copy) four pixels, but reassign the horizontal
  607. * predictor */
  608. *current_pixel_pair = *prev_pixel_pair++;
  609. *vert_pred++ = *current_pixel_pair++;
  610. *current_pixel_pair = *prev_pixel_pair++;
  611. horiz_pred = *current_pixel_pair - *vert_pred;
  612. *vert_pred++ = *current_pixel_pair++;
  613. }
  614. if (!keyframe) {
  615. mb_change_byte_mask <<= 1;
  616. /* next byte */
  617. if (!mb_change_byte_mask) {
  618. mb_change_byte = mb_change_bits[mb_change_index++];
  619. mb_change_byte_mask = 0x01;
  620. }
  621. }
  622. pixels_left -= 4;
  623. }
  624. /* next change row */
  625. if (((y + 1) & 3) == 0)
  626. mb_change_bits += s->mb_change_bits_row_size;
  627. current_line += s->frame.linesize[0];
  628. prev_line += s->prev_frame.linesize[0];
  629. }
  630. }
  631. static void truemotion1_decode_24bit(TrueMotion1Context *s)
  632. {
  633. int y;
  634. int pixels_left; /* remaining pixels on this line */
  635. unsigned int predictor_pair;
  636. unsigned int horiz_pred;
  637. unsigned int *vert_pred;
  638. unsigned int *current_pixel_pair;
  639. unsigned int *prev_pixel_pair;
  640. unsigned char *current_line = s->frame.data[0];
  641. unsigned char *prev_line = s->prev_frame.data[0];
  642. int keyframe = s->flags & FLAG_KEYFRAME;
  643. /* these variables are for managing the stream of macroblock change bits */
  644. unsigned char *mb_change_bits = s->mb_change_bits;
  645. unsigned char mb_change_byte;
  646. unsigned char mb_change_byte_mask;
  647. int mb_change_index;
  648. /* these variables are for managing the main index stream */
  649. int index_stream_index = 0; /* yes, the index into the index stream */
  650. int index;
  651. /* clean out the line buffer */
  652. memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned int));
  653. GET_NEXT_INDEX();
  654. for (y = 0; y < s->avctx->height; y++) {
  655. /* re-init variables for the next line iteration */
  656. horiz_pred = 0;
  657. current_pixel_pair = (unsigned int *)current_line;
  658. prev_pixel_pair = (unsigned int *)prev_line;
  659. vert_pred = s->vert_pred;
  660. mb_change_index = 0;
  661. mb_change_byte = mb_change_bits[mb_change_index++];
  662. mb_change_byte_mask = 0x01;
  663. pixels_left = s->avctx->width;
  664. while (pixels_left > 0) {
  665. if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
  666. switch (y & 3) {
  667. case 0:
  668. /* if macroblock width is 2, apply C-Y-C-Y; else
  669. * apply C-Y-Y */
  670. if (s->block_width == 2) {
  671. APPLY_C_PREDICTOR_24();
  672. APPLY_Y_PREDICTOR_24();
  673. OUTPUT_PIXEL_PAIR();
  674. APPLY_C_PREDICTOR_24();
  675. APPLY_Y_PREDICTOR_24();
  676. OUTPUT_PIXEL_PAIR();
  677. } else {
  678. APPLY_C_PREDICTOR_24();
  679. APPLY_Y_PREDICTOR_24();
  680. OUTPUT_PIXEL_PAIR();
  681. APPLY_Y_PREDICTOR_24();
  682. OUTPUT_PIXEL_PAIR();
  683. }
  684. break;
  685. case 1:
  686. case 3:
  687. /* always apply 2 Y predictors on these iterations */
  688. APPLY_Y_PREDICTOR_24();
  689. OUTPUT_PIXEL_PAIR();
  690. APPLY_Y_PREDICTOR_24();
  691. OUTPUT_PIXEL_PAIR();
  692. break;
  693. case 2:
  694. /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
  695. * depending on the macroblock type */
  696. if (s->block_type == BLOCK_2x2) {
  697. APPLY_C_PREDICTOR_24();
  698. APPLY_Y_PREDICTOR_24();
  699. OUTPUT_PIXEL_PAIR();
  700. APPLY_C_PREDICTOR_24();
  701. APPLY_Y_PREDICTOR_24();
  702. OUTPUT_PIXEL_PAIR();
  703. } else if (s->block_type == BLOCK_4x2) {
  704. APPLY_C_PREDICTOR_24();
  705. APPLY_Y_PREDICTOR_24();
  706. OUTPUT_PIXEL_PAIR();
  707. APPLY_Y_PREDICTOR_24();
  708. OUTPUT_PIXEL_PAIR();
  709. } else {
  710. APPLY_Y_PREDICTOR_24();
  711. OUTPUT_PIXEL_PAIR();
  712. APPLY_Y_PREDICTOR_24();
  713. OUTPUT_PIXEL_PAIR();
  714. }
  715. break;
  716. }
  717. } else {
  718. /* skip (copy) four pixels, but reassign the horizontal
  719. * predictor */
  720. *current_pixel_pair = *prev_pixel_pair++;
  721. *vert_pred++ = *current_pixel_pair++;
  722. *current_pixel_pair = *prev_pixel_pair++;
  723. horiz_pred = *current_pixel_pair - *vert_pred;
  724. *vert_pred++ = *current_pixel_pair++;
  725. }
  726. if (!keyframe) {
  727. mb_change_byte_mask <<= 1;
  728. /* next byte */
  729. if (!mb_change_byte_mask) {
  730. mb_change_byte = mb_change_bits[mb_change_index++];
  731. mb_change_byte_mask = 0x01;
  732. }
  733. }
  734. pixels_left -= 4;
  735. }
  736. /* next change row */
  737. if (((y + 1) & 3) == 0)
  738. mb_change_bits += s->mb_change_bits_row_size;
  739. current_line += s->frame.linesize[0];
  740. prev_line += s->prev_frame.linesize[0];
  741. }
  742. }
  743. static int truemotion1_decode_frame(AVCodecContext *avctx,
  744. void *data, int *data_size,
  745. uint8_t *buf, int buf_size)
  746. {
  747. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  748. s->buf = buf;
  749. s->size = buf_size;
  750. if (truemotion1_decode_header(s) == -1)
  751. return -1;
  752. s->frame.reference = 1;
  753. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  754. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  755. return -1;
  756. }
  757. /* check for a do-nothing frame and copy the previous frame */
  758. if (compression_types[s->compression].algorithm == ALGO_NOP)
  759. {
  760. memcpy(s->frame.data[0], s->prev_frame.data[0],
  761. s->frame.linesize[0] * s->avctx->height);
  762. } else if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
  763. truemotion1_decode_24bit(s);
  764. } else {
  765. truemotion1_decode_16bit(s);
  766. }
  767. if (s->prev_frame.data[0])
  768. avctx->release_buffer(avctx, &s->prev_frame);
  769. /* shuffle frames */
  770. s->prev_frame = s->frame;
  771. *data_size = sizeof(AVFrame);
  772. *(AVFrame*)data = s->frame;
  773. /* report that the buffer was completely consumed */
  774. return buf_size;
  775. }
  776. static int truemotion1_decode_end(AVCodecContext *avctx)
  777. {
  778. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  779. /* release the last frame */
  780. if (s->prev_frame.data[0])
  781. avctx->release_buffer(avctx, &s->prev_frame);
  782. av_free(s->vert_pred);
  783. return 0;
  784. }
  785. AVCodec truemotion1_decoder = {
  786. "truemotion1",
  787. CODEC_TYPE_VIDEO,
  788. CODEC_ID_TRUEMOTION1,
  789. sizeof(TrueMotion1Context),
  790. truemotion1_decode_init,
  791. NULL,
  792. truemotion1_decode_end,
  793. truemotion1_decode_frame,
  794. CODEC_CAP_DR1,
  795. };