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.

601 lines
18KB

  1. /*
  2. * Duck TrueMotion 1.0 Decoder
  3. * Copyright (C) 2003 Alex Beregszaszi & Mike Melanson
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file truemotion1.c
  21. * Duck TrueMotion v1 Video Decoder by
  22. * Alex Beregszaszi (alex@fsn.hu) and
  23. * Mike Melanson (melanson@pcisys.net)
  24. *
  25. * The TrueMotion v1 decoder presently only decodes 16-bit TM1 data and
  26. * outputs RGB555 data. 24-bit TM1 data is not supported yet.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include "common.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "truemotion1data.h"
  36. typedef struct TrueMotion1Context {
  37. AVCodecContext *avctx;
  38. AVFrame frame;
  39. AVFrame prev_frame;
  40. unsigned char *buf;
  41. int size;
  42. unsigned char *mb_change_bits;
  43. int mb_change_bits_row_size;
  44. unsigned char *index_stream;
  45. int index_stream_size;
  46. int flags;
  47. int x, y, w, h;
  48. uint32_t y_predictor_table[1024];
  49. uint32_t c_predictor_table[1024];
  50. int compression;
  51. int block_type;
  52. int block_width;
  53. int block_height;
  54. int16_t ydt[8];
  55. int16_t cdt[8];
  56. int16_t fat_ydt[8];
  57. int16_t fat_cdt[8];
  58. int last_deltaset, last_vectable;
  59. unsigned int *vert_pred;
  60. } TrueMotion1Context;
  61. #define FLAG_SPRITE 32
  62. #define FLAG_KEYFRAME 16
  63. #define FLAG_INTERFRAME 8
  64. #define FLAG_INTERPOLATED 4
  65. struct frame_header {
  66. uint8_t header_size;
  67. uint8_t compression;
  68. uint8_t deltaset;
  69. uint8_t vectable;
  70. uint16_t ysize;
  71. uint16_t xsize;
  72. uint16_t checksum;
  73. uint8_t version;
  74. uint8_t header_type;
  75. uint8_t flags;
  76. uint8_t control;
  77. uint16_t xoffset;
  78. uint16_t yoffset;
  79. uint16_t width;
  80. uint16_t height;
  81. };
  82. #define ALGO_NOP 0
  83. #define ALGO_RGB16V 1
  84. #define ALGO_RGB16H 2
  85. #define ALGO_RGB24H 3
  86. /* these are the various block sizes that can occupy a 4x4 block */
  87. #define BLOCK_2x2 0
  88. #define BLOCK_2x4 1
  89. #define BLOCK_4x2 2
  90. #define BLOCK_4x4 3
  91. typedef struct comp_types {
  92. int algorithm;
  93. int block_width;
  94. int block_height;
  95. int block_type;
  96. } comp_types;
  97. /* { valid for metatype }, algorithm, num of deltas, horiz res, vert res */
  98. static comp_types compression_types[17] = {
  99. { ALGO_NOP, 0, 0, 0 },
  100. { ALGO_RGB16V, 4, 4, BLOCK_4x4 },
  101. { ALGO_RGB16H, 4, 4, BLOCK_4x4 },
  102. { ALGO_RGB16V, 4, 2, BLOCK_4x2 },
  103. { ALGO_RGB16H, 4, 2, BLOCK_4x2 },
  104. { ALGO_RGB16V, 2, 4, BLOCK_2x4 },
  105. { ALGO_RGB16H, 2, 4, BLOCK_2x4 },
  106. { ALGO_RGB16V, 2, 2, BLOCK_2x2 },
  107. { ALGO_RGB16H, 2, 2, BLOCK_2x2 },
  108. { ALGO_NOP, 4, 4, BLOCK_4x4 },
  109. { ALGO_RGB24H, 4, 4, BLOCK_4x4 },
  110. { ALGO_NOP, 4, 2, BLOCK_4x2 },
  111. { ALGO_RGB24H, 4, 2, BLOCK_4x2 },
  112. { ALGO_NOP, 2, 4, BLOCK_2x4 },
  113. { ALGO_RGB24H, 2, 4, BLOCK_2x4 },
  114. { ALGO_NOP, 2, 2, BLOCK_2x2 },
  115. { ALGO_RGB24H, 2, 2, BLOCK_2x2 }
  116. };
  117. static void select_delta_tables(TrueMotion1Context *s, int delta_table_index)
  118. {
  119. int i;
  120. if (delta_table_index > 3)
  121. return;
  122. memcpy(s->ydt, ydts[delta_table_index], 8 * sizeof(int16_t));
  123. memcpy(s->cdt, cdts[delta_table_index], 8 * sizeof(int16_t));
  124. memcpy(s->fat_ydt, fat_ydts[delta_table_index], 8 * sizeof(int16_t));
  125. memcpy(s->fat_cdt, fat_cdts[delta_table_index], 8 * sizeof(int16_t));
  126. /* Y skinny deltas need to be halved for some reason; maybe the
  127. * skinny Y deltas should be modified */
  128. for (i = 0; i < 8; i++)
  129. {
  130. /* drop the lsb before dividing by 2-- net effect: round down
  131. * when dividing a negative number (e.g., -3/2 = -2, not -1) */
  132. s->ydt[i] &= 0xFFFE;
  133. s->ydt[i] /= 2;
  134. }
  135. }
  136. #ifdef WORDS_BIGENDIAN
  137. static int make_ydt_entry(int p2, int p1, int16_t *ydt)
  138. #else
  139. static int make_ydt_entry(int p1, int p2, int16_t *ydt)
  140. #endif
  141. {
  142. int lo, hi;
  143. lo = ydt[p1];
  144. lo += (lo << 5) + (lo << 10);
  145. hi = ydt[p2];
  146. hi += (hi << 5) + (hi << 10);
  147. return ((lo + (hi << 16)) << 1);
  148. }
  149. #ifdef WORDS_BIGENDIAN
  150. static int make_cdt_entry(int p2, int p1, int16_t *cdt)
  151. #else
  152. static int make_cdt_entry(int p1, int p2, int16_t *cdt)
  153. #endif
  154. {
  155. int r, b, lo;
  156. b = cdt[p2];
  157. r = cdt[p1] << 10;
  158. lo = b + r;
  159. return ((lo + (lo << 16)) << 1);
  160. }
  161. static void gen_vector_table(TrueMotion1Context *s, uint8_t *sel_vector_table)
  162. {
  163. int len, i, j;
  164. unsigned char delta_pair;
  165. for (i = 0; i < 1024; i += 4)
  166. {
  167. len = *sel_vector_table++ / 2;
  168. for (j = 0; j < len; j++)
  169. {
  170. delta_pair = *sel_vector_table++;
  171. s->y_predictor_table[i+j] = 0xfffffffe &
  172. make_ydt_entry(delta_pair >> 4, delta_pair & 0xf, s->ydt);
  173. s->c_predictor_table[i+j] = 0xfffffffe &
  174. make_cdt_entry(delta_pair >> 4, delta_pair & 0xf, s->cdt);
  175. }
  176. s->y_predictor_table[i+(j-1)] |= 1;
  177. s->c_predictor_table[i+(j-1)] |= 1;
  178. }
  179. }
  180. /* Returns the number of bytes consumed from the bytestream. Returns -1 if
  181. * there was an error while decoding the header */
  182. static int truemotion1_decode_header(TrueMotion1Context *s)
  183. {
  184. int i;
  185. struct frame_header header;
  186. uint8_t header_buffer[128]; /* logical maximum size of the header */
  187. uint8_t *sel_vector_table;
  188. /* There is 1 change bit per 4 pixels, so each change byte represents
  189. * 32 pixels; divide width by 4 to obtain the number of change bits and
  190. * then round up to the nearest byte. */
  191. s->mb_change_bits_row_size = ((s->avctx->width >> 2) + 7) >> 3;
  192. header.header_size = ((s->buf[0] >> 5) | (s->buf[0] << 3)) & 0x7f;
  193. if (s->buf[0] < 0x10)
  194. {
  195. av_log(s->avctx, AV_LOG_ERROR, "invalid header size\n");
  196. return -1;
  197. }
  198. /* unscramble the header bytes with a XOR operation */
  199. memset(header_buffer, 0, 128);
  200. for (i = 1; i < header.header_size; i++)
  201. header_buffer[i - 1] = s->buf[i] ^ s->buf[i + 1];
  202. header.compression = header_buffer[0];
  203. header.deltaset = header_buffer[1];
  204. header.vectable = header_buffer[2];
  205. header.ysize = LE_16(&header_buffer[3]);
  206. header.xsize = LE_16(&header_buffer[5]);
  207. header.checksum = LE_16(&header_buffer[7]);
  208. header.version = header_buffer[9];
  209. header.header_type = header_buffer[10];
  210. header.flags = header_buffer[11];
  211. header.control = header_buffer[12];
  212. /* Version 2 */
  213. if (header.version >= 2)
  214. {
  215. if (header.header_type > 3)
  216. {
  217. av_log(s->avctx, AV_LOG_ERROR, "truemotion1: invalid header type\n");
  218. return -1;
  219. } else if ((header.header_type == 2) || (header.header_type == 3)) {
  220. s->flags = header.flags;
  221. if (!(s->flags & FLAG_INTERFRAME))
  222. s->flags |= FLAG_KEYFRAME;
  223. } else
  224. s->flags = FLAG_KEYFRAME;
  225. } else /* Version 1 */
  226. s->flags = FLAG_KEYFRAME;
  227. if (s->flags & FLAG_SPRITE) {
  228. s->w = header.width;
  229. s->h = header.height;
  230. s->x = header.xoffset;
  231. s->y = header.yoffset;
  232. } else {
  233. s->w = header.xsize;
  234. s->h = header.ysize;
  235. if (header.header_type < 2) {
  236. if ((s->w < 213) && (s->h >= 176))
  237. s->flags |= FLAG_INTERPOLATED;
  238. }
  239. }
  240. if (header.compression > 17) {
  241. av_log(s->avctx, AV_LOG_ERROR, "invalid compression type (%d)\n", header.compression);
  242. return -1;
  243. }
  244. if ((header.deltaset != s->last_deltaset) ||
  245. (header.vectable != s->last_vectable))
  246. select_delta_tables(s, header.deltaset);
  247. if ((header.compression & 1) && header.header_type)
  248. sel_vector_table = pc_tbl2;
  249. else {
  250. if (header.vectable < 4)
  251. sel_vector_table = tables[header.vectable - 1];
  252. else {
  253. av_log(s->avctx, AV_LOG_ERROR, "invalid vector table id (%d)\n", header.vectable);
  254. return -1;
  255. }
  256. }
  257. if ((header.deltaset != s->last_deltaset) || (header.vectable != s->last_vectable))
  258. {
  259. if (compression_types[header.compression].algorithm == ALGO_RGB24H)
  260. {
  261. av_log(s->avctx, AV_LOG_ERROR, "24bit compression not yet supported\n");
  262. }
  263. else
  264. gen_vector_table(s, sel_vector_table);
  265. }
  266. /* set up pointers to the other key data chunks */
  267. s->mb_change_bits = s->buf + header.header_size;
  268. if (s->flags & FLAG_KEYFRAME) {
  269. /* no change bits specified for a keyframe; only index bytes */
  270. s->index_stream = s->mb_change_bits;
  271. } else {
  272. /* one change bit per 4x4 block */
  273. s->index_stream = s->mb_change_bits +
  274. (s->mb_change_bits_row_size * (s->avctx->height >> 2));
  275. }
  276. s->index_stream_size = s->size - (s->index_stream - s->buf);
  277. s->last_deltaset = header.deltaset;
  278. s->last_vectable = header.vectable;
  279. s->compression = header.compression;
  280. s->block_width = compression_types[header.compression].block_width;
  281. s->block_height = compression_types[header.compression].block_height;
  282. s->block_type = compression_types[header.compression].block_type;
  283. return header.header_size;
  284. }
  285. static int truemotion1_decode_init(AVCodecContext *avctx)
  286. {
  287. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  288. s->avctx = avctx;
  289. avctx->pix_fmt = PIX_FMT_RGB555;
  290. avctx->has_b_frames = 0;
  291. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  292. /* there is a vertical predictor for each pixel in a line; each vertical
  293. * predictor is 0 to start with */
  294. s->vert_pred =
  295. (unsigned int *)av_malloc(s->avctx->width * sizeof(unsigned short));
  296. return 0;
  297. }
  298. #define GET_NEXT_INDEX() \
  299. {\
  300. if (index_stream_index >= s->index_stream_size) { \
  301. av_log(s->avctx, AV_LOG_INFO, " help! truemotion1 decoder went out of bounds\n"); \
  302. return; \
  303. } \
  304. index = s->index_stream[index_stream_index++] * 4; \
  305. }
  306. #define APPLY_C_PREDICTOR() \
  307. predictor_pair = s->c_predictor_table[index]; \
  308. horiz_pred += (predictor_pair >> 1); \
  309. if (predictor_pair & 1) { \
  310. GET_NEXT_INDEX() \
  311. if (!index) { \
  312. GET_NEXT_INDEX() \
  313. predictor_pair = s->c_predictor_table[index]; \
  314. horiz_pred += ((predictor_pair >> 1) * 5); \
  315. if (predictor_pair & 1) \
  316. GET_NEXT_INDEX() \
  317. else \
  318. index++; \
  319. } \
  320. } else \
  321. index++;
  322. #define APPLY_Y_PREDICTOR() \
  323. predictor_pair = s->y_predictor_table[index]; \
  324. horiz_pred += (predictor_pair >> 1); \
  325. if (predictor_pair & 1) { \
  326. GET_NEXT_INDEX() \
  327. if (!index) { \
  328. GET_NEXT_INDEX() \
  329. predictor_pair = s->y_predictor_table[index]; \
  330. horiz_pred += ((predictor_pair >> 1) * 5); \
  331. if (predictor_pair & 1) \
  332. GET_NEXT_INDEX() \
  333. else \
  334. index++; \
  335. } \
  336. } else \
  337. index++;
  338. #define OUTPUT_PIXEL_PAIR() \
  339. *current_pixel_pair = *vert_pred + horiz_pred; \
  340. *vert_pred++ = *current_pixel_pair++; \
  341. prev_pixel_pair++;
  342. static void truemotion1_decode_16bit(TrueMotion1Context *s)
  343. {
  344. int y;
  345. int pixels_left; /* remaining pixels on this line */
  346. unsigned int predictor_pair;
  347. unsigned int horiz_pred;
  348. unsigned int *vert_pred;
  349. unsigned int *current_pixel_pair;
  350. unsigned int *prev_pixel_pair;
  351. unsigned char *current_line = s->frame.data[0];
  352. unsigned char *prev_line = s->prev_frame.data[0];
  353. int keyframe = s->flags & FLAG_KEYFRAME;
  354. /* these variables are for managing the stream of macroblock change bits */
  355. unsigned char *mb_change_bits = s->mb_change_bits;
  356. unsigned char mb_change_byte;
  357. unsigned char mb_change_byte_mask;
  358. int mb_change_index;
  359. /* these variables are for managing the main index stream */
  360. int index_stream_index = 0; /* yes, the index into the index stream */
  361. int index;
  362. /* clean out the line buffer */
  363. memset(s->vert_pred, 0, s->avctx->width * sizeof(unsigned short));
  364. GET_NEXT_INDEX();
  365. for (y = 0; y < s->avctx->height; y++) {
  366. /* re-init variables for the next line iteration */
  367. horiz_pred = 0;
  368. current_pixel_pair = (unsigned int *)current_line;
  369. prev_pixel_pair = (unsigned int *)prev_line;
  370. vert_pred = s->vert_pred;
  371. mb_change_index = 0;
  372. mb_change_byte = mb_change_bits[mb_change_index++];
  373. mb_change_byte_mask = 0x01;
  374. pixels_left = s->avctx->width;
  375. while (pixels_left > 0) {
  376. if (keyframe || ((mb_change_byte & mb_change_byte_mask) == 0)) {
  377. switch (y & 3) {
  378. case 0:
  379. /* if macroblock width is 2, apply C-Y-C-Y; else
  380. * apply C-Y-Y */
  381. if (s->block_width == 2) {
  382. APPLY_C_PREDICTOR();
  383. APPLY_Y_PREDICTOR();
  384. OUTPUT_PIXEL_PAIR();
  385. APPLY_C_PREDICTOR();
  386. APPLY_Y_PREDICTOR();
  387. OUTPUT_PIXEL_PAIR();
  388. } else {
  389. APPLY_C_PREDICTOR();
  390. APPLY_Y_PREDICTOR();
  391. OUTPUT_PIXEL_PAIR();
  392. APPLY_Y_PREDICTOR();
  393. OUTPUT_PIXEL_PAIR();
  394. }
  395. break;
  396. case 1:
  397. case 3:
  398. /* always apply 2 Y predictors on these iterations */
  399. APPLY_Y_PREDICTOR();
  400. OUTPUT_PIXEL_PAIR();
  401. APPLY_Y_PREDICTOR();
  402. OUTPUT_PIXEL_PAIR();
  403. break;
  404. case 2:
  405. /* this iteration might be C-Y-C-Y, Y-Y, or C-Y-Y
  406. * depending on the macroblock type */
  407. if (s->block_type == BLOCK_2x2) {
  408. APPLY_C_PREDICTOR();
  409. APPLY_Y_PREDICTOR();
  410. OUTPUT_PIXEL_PAIR();
  411. APPLY_C_PREDICTOR();
  412. APPLY_Y_PREDICTOR();
  413. OUTPUT_PIXEL_PAIR();
  414. } else if (s->block_type == BLOCK_4x2) {
  415. APPLY_C_PREDICTOR();
  416. APPLY_Y_PREDICTOR();
  417. OUTPUT_PIXEL_PAIR();
  418. APPLY_Y_PREDICTOR();
  419. OUTPUT_PIXEL_PAIR();
  420. } else {
  421. APPLY_Y_PREDICTOR();
  422. OUTPUT_PIXEL_PAIR();
  423. APPLY_Y_PREDICTOR();
  424. OUTPUT_PIXEL_PAIR();
  425. }
  426. break;
  427. }
  428. } else {
  429. /* skip (copy) four pixels, but reassign the horizontal
  430. * predictor */
  431. *current_pixel_pair = *prev_pixel_pair++;
  432. *vert_pred++ = *current_pixel_pair++;
  433. *current_pixel_pair = *prev_pixel_pair++;
  434. horiz_pred = *current_pixel_pair - *vert_pred;
  435. *vert_pred++ = *current_pixel_pair++;
  436. }
  437. if (!keyframe) {
  438. mb_change_byte_mask <<= 1;
  439. /* next byte */
  440. if (!mb_change_byte_mask) {
  441. mb_change_byte = mb_change_bits[mb_change_index++];
  442. mb_change_byte_mask = 0x01;
  443. }
  444. }
  445. pixels_left -= 4;
  446. }
  447. /* next change row */
  448. if (((y + 1) & 3) == 0)
  449. mb_change_bits += s->mb_change_bits_row_size;
  450. current_line += s->frame.linesize[0];
  451. prev_line += s->prev_frame.linesize[0];
  452. }
  453. }
  454. static int truemotion1_decode_frame(AVCodecContext *avctx,
  455. void *data, int *data_size,
  456. uint8_t *buf, int buf_size)
  457. {
  458. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  459. s->buf = buf;
  460. s->size = buf_size;
  461. s->frame.reference = 1;
  462. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  463. av_log(s->avctx, AV_LOG_ERROR, "truemotion1: get_buffer() failed\n");
  464. return -1;
  465. }
  466. /* no supplementary picture */
  467. if (buf_size == 0)
  468. return 0;
  469. if (truemotion1_decode_header(s) == -1)
  470. return -1;
  471. /* check for a do-nothing frame and copy the previous frame */
  472. if (compression_types[s->compression].algorithm == ALGO_NOP)
  473. {
  474. memcpy(s->frame.data[0], s->prev_frame.data[0],
  475. s->frame.linesize[0] * s->avctx->height);
  476. } else if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
  477. av_log(s->avctx, AV_LOG_ERROR, "24bit compression not yet supported\n");
  478. } else {
  479. truemotion1_decode_16bit(s);
  480. }
  481. if (s->prev_frame.data[0])
  482. avctx->release_buffer(avctx, &s->prev_frame);
  483. /* shuffle frames */
  484. s->prev_frame = s->frame;
  485. *data_size = sizeof(AVFrame);
  486. *(AVFrame*)data = s->frame;
  487. /* report that the buffer was completely consumed */
  488. return buf_size;
  489. }
  490. static int truemotion1_decode_end(AVCodecContext *avctx)
  491. {
  492. TrueMotion1Context *s = (TrueMotion1Context *)avctx->priv_data;
  493. /* release the last frame */
  494. if (s->prev_frame.data[0])
  495. avctx->release_buffer(avctx, &s->prev_frame);
  496. av_free(s->vert_pred);
  497. return 0;
  498. }
  499. AVCodec truemotion1_decoder = {
  500. "truemotion1",
  501. CODEC_TYPE_VIDEO,
  502. CODEC_ID_TRUEMOTION1,
  503. sizeof(TrueMotion1Context),
  504. truemotion1_decode_init,
  505. NULL,
  506. truemotion1_decode_end,
  507. truemotion1_decode_frame,
  508. CODEC_CAP_DR1,
  509. };