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.

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