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.

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