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.

927 lines
28KB

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