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.

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