truemotion1.c 27 KB

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