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.

897 lines
27KB

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