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.

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