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.

501 lines
17KB

  1. /*
  2. * Quicktime Graphics (SMC) Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file smc.c
  22. * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
  23. * For more information about the SMC format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * The SMC decoder outputs PAL8 colorspace data.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include "common.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #define CPAIR 2
  36. #define CQUAD 4
  37. #define COCTET 8
  38. #define COLORS_PER_TABLE 256
  39. typedef struct SmcContext {
  40. AVCodecContext *avctx;
  41. DSPContext dsp;
  42. AVFrame frame;
  43. unsigned char *buf;
  44. int size;
  45. /* SMC color tables */
  46. unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
  47. unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
  48. unsigned char color_octets[COLORS_PER_TABLE * COCTET];
  49. } SmcContext;
  50. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  51. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  52. (((uint8_t*)(x))[1] << 16) | \
  53. (((uint8_t*)(x))[2] << 8) | \
  54. ((uint8_t*)(x))[3])
  55. #define GET_BLOCK_COUNT() \
  56. (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
  57. #define ADVANCE_BLOCK() \
  58. { \
  59. pixel_ptr += 4; \
  60. if (pixel_ptr >= width) \
  61. { \
  62. pixel_ptr = 0; \
  63. row_ptr += stride * 4; \
  64. } \
  65. total_blocks--; \
  66. if (total_blocks < 0) \
  67. { \
  68. printf("warning: block counter just went negative (this should not happen)\n"); \
  69. return; \
  70. } \
  71. }
  72. static void smc_decode_stream(SmcContext *s)
  73. {
  74. int width = s->avctx->width;
  75. int height = s->avctx->height;
  76. int stride = s->frame.linesize[0];
  77. int i;
  78. int stream_ptr = 0;
  79. int chunk_size;
  80. unsigned char opcode;
  81. int n_blocks;
  82. unsigned int color_flags;
  83. unsigned int color_flags_a;
  84. unsigned int color_flags_b;
  85. unsigned int flag_mask;
  86. unsigned char *pixels = s->frame.data[0];
  87. int image_size = height * s->frame.linesize[0];
  88. int row_ptr = 0;
  89. int pixel_ptr = 0;
  90. int pixel_x, pixel_y;
  91. int row_inc = stride - 4;
  92. int block_ptr;
  93. int prev_block_ptr;
  94. int prev_block_ptr1, prev_block_ptr2;
  95. int prev_block_flag;
  96. int total_blocks;
  97. int color_table_index; /* indexes to color pair, quad, or octet tables */
  98. int pixel;
  99. int color_pair_index = 0;
  100. int color_quad_index = 0;
  101. int color_octet_index = 0;
  102. /* make the palette available */
  103. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  104. if (s->avctx->palctrl->palette_changed) {
  105. s->frame.palette_has_changed = 1;
  106. s->avctx->palctrl->palette_changed = 0;
  107. }
  108. chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
  109. stream_ptr += 4;
  110. if (chunk_size != s->size)
  111. printf("warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
  112. chunk_size, s->size);
  113. chunk_size = s->size;
  114. total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
  115. /* traverse through the blocks */
  116. while (total_blocks) {
  117. /* sanity checks */
  118. /* make sure stream ptr hasn't gone out of bounds */
  119. if (stream_ptr > chunk_size) {
  120. printf("SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
  121. stream_ptr, chunk_size);
  122. return;
  123. }
  124. /* make sure the row pointer hasn't gone wild */
  125. if (row_ptr >= image_size) {
  126. printf("SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
  127. row_ptr, image_size);
  128. return;
  129. }
  130. opcode = s->buf[stream_ptr++];
  131. switch (opcode & 0xF0) {
  132. /* skip n blocks */
  133. case 0x00:
  134. case 0x10:
  135. n_blocks = GET_BLOCK_COUNT();
  136. while (n_blocks--) {
  137. ADVANCE_BLOCK();
  138. }
  139. break;
  140. /* repeat last block n times */
  141. case 0x20:
  142. case 0x30:
  143. n_blocks = GET_BLOCK_COUNT();
  144. /* sanity check */
  145. if ((row_ptr == 0) && (pixel_ptr == 0)) {
  146. printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n",
  147. opcode & 0xF0);
  148. break;
  149. }
  150. /* figure out where the previous block started */
  151. if (pixel_ptr == 0)
  152. prev_block_ptr1 =
  153. (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
  154. else
  155. prev_block_ptr1 = row_ptr + pixel_ptr - 4;
  156. while (n_blocks--) {
  157. block_ptr = row_ptr + pixel_ptr;
  158. prev_block_ptr = prev_block_ptr1;
  159. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  160. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  161. pixels[block_ptr++] = pixels[prev_block_ptr++];
  162. }
  163. block_ptr += row_inc;
  164. prev_block_ptr += row_inc;
  165. }
  166. ADVANCE_BLOCK();
  167. }
  168. break;
  169. /* repeat previous pair of blocks n times */
  170. case 0x40:
  171. case 0x50:
  172. n_blocks = GET_BLOCK_COUNT();
  173. n_blocks *= 2;
  174. /* sanity check */
  175. if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
  176. printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
  177. opcode & 0xF0);
  178. break;
  179. }
  180. /* figure out where the previous 2 blocks started */
  181. if (pixel_ptr == 0)
  182. prev_block_ptr1 = (row_ptr - s->avctx->width * 4) +
  183. s->avctx->width - 4 * 2;
  184. else if (pixel_ptr == 4)
  185. prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
  186. else
  187. prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
  188. if (pixel_ptr == 0)
  189. prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
  190. else
  191. prev_block_ptr2 = row_ptr + pixel_ptr - 4;
  192. prev_block_flag = 0;
  193. while (n_blocks--) {
  194. block_ptr = row_ptr + pixel_ptr;
  195. if (prev_block_flag)
  196. prev_block_ptr = prev_block_ptr2;
  197. else
  198. prev_block_ptr = prev_block_ptr1;
  199. prev_block_flag = !prev_block_flag;
  200. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  201. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  202. pixels[block_ptr++] = pixels[prev_block_ptr++];
  203. }
  204. block_ptr += row_inc;
  205. prev_block_ptr += row_inc;
  206. }
  207. ADVANCE_BLOCK();
  208. }
  209. break;
  210. /* 1-color block encoding */
  211. case 0x60:
  212. case 0x70:
  213. n_blocks = GET_BLOCK_COUNT();
  214. pixel = s->buf[stream_ptr++];
  215. while (n_blocks--) {
  216. block_ptr = row_ptr + pixel_ptr;
  217. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  218. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  219. pixels[block_ptr++] = pixel;
  220. }
  221. block_ptr += row_inc;
  222. }
  223. ADVANCE_BLOCK();
  224. }
  225. break;
  226. /* 2-color block encoding */
  227. case 0x80:
  228. case 0x90:
  229. n_blocks = (opcode & 0x0F) + 1;
  230. /* figure out which color pair to use to paint the 2-color block */
  231. if ((opcode & 0xF0) == 0x80) {
  232. /* fetch the next 2 colors from bytestream and store in next
  233. * available entry in the color pair table */
  234. for (i = 0; i < CPAIR; i++) {
  235. pixel = s->buf[stream_ptr++];
  236. color_table_index = CPAIR * color_pair_index + i;
  237. s->color_pairs[color_table_index] = pixel;
  238. }
  239. /* this is the base index to use for this block */
  240. color_table_index = CPAIR * color_pair_index;
  241. color_pair_index++;
  242. /* wraparound */
  243. if (color_pair_index == COLORS_PER_TABLE)
  244. color_pair_index = 0;
  245. } else
  246. color_table_index = CPAIR * s->buf[stream_ptr++];
  247. while (n_blocks--) {
  248. color_flags = BE_16(&s->buf[stream_ptr]);
  249. stream_ptr += 2;
  250. flag_mask = 0x8000;
  251. block_ptr = row_ptr + pixel_ptr;
  252. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  253. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  254. if (color_flags & flag_mask)
  255. pixel = color_table_index + 1;
  256. else
  257. pixel = color_table_index;
  258. flag_mask >>= 1;
  259. pixels[block_ptr++] = s->color_pairs[pixel];
  260. }
  261. block_ptr += row_inc;
  262. }
  263. ADVANCE_BLOCK();
  264. }
  265. break;
  266. /* 4-color block encoding */
  267. case 0xA0:
  268. case 0xB0:
  269. n_blocks = (opcode & 0x0F) + 1;
  270. /* figure out which color quad to use to paint the 4-color block */
  271. if ((opcode & 0xF0) == 0xA0) {
  272. /* fetch the next 4 colors from bytestream and store in next
  273. * available entry in the color quad table */
  274. for (i = 0; i < CQUAD; i++) {
  275. pixel = s->buf[stream_ptr++];
  276. color_table_index = CQUAD * color_quad_index + i;
  277. s->color_quads[color_table_index] = pixel;
  278. }
  279. /* this is the base index to use for this block */
  280. color_table_index = CQUAD * color_quad_index;
  281. color_quad_index++;
  282. /* wraparound */
  283. if (color_quad_index == COLORS_PER_TABLE)
  284. color_quad_index = 0;
  285. } else
  286. color_table_index = CQUAD * s->buf[stream_ptr++];
  287. while (n_blocks--) {
  288. color_flags = BE_32(&s->buf[stream_ptr]);
  289. stream_ptr += 4;
  290. /* flag mask actually acts as a bit shift count here */
  291. flag_mask = 30;
  292. block_ptr = row_ptr + pixel_ptr;
  293. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  294. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  295. pixel = color_table_index +
  296. ((color_flags >> flag_mask) & 0x03);
  297. flag_mask -= 2;
  298. pixels[block_ptr++] = s->color_quads[pixel];
  299. }
  300. block_ptr += row_inc;
  301. }
  302. ADVANCE_BLOCK();
  303. }
  304. break;
  305. /* 8-color block encoding */
  306. case 0xC0:
  307. case 0xD0:
  308. n_blocks = (opcode & 0x0F) + 1;
  309. /* figure out which color octet to use to paint the 8-color block */
  310. if ((opcode & 0xF0) == 0xC0) {
  311. /* fetch the next 8 colors from bytestream and store in next
  312. * available entry in the color octet table */
  313. for (i = 0; i < COCTET; i++) {
  314. pixel = s->buf[stream_ptr++];
  315. color_table_index = COCTET * color_octet_index + i;
  316. s->color_octets[color_table_index] = pixel;
  317. }
  318. /* this is the base index to use for this block */
  319. color_table_index = COCTET * color_octet_index;
  320. color_octet_index++;
  321. /* wraparound */
  322. if (color_octet_index == COLORS_PER_TABLE)
  323. color_octet_index = 0;
  324. } else
  325. color_table_index = COCTET * s->buf[stream_ptr++];
  326. while (n_blocks--) {
  327. /*
  328. For this input of 6 hex bytes:
  329. 01 23 45 67 89 AB
  330. Mangle it to this output:
  331. flags_a = xx012456, flags_b = xx89A37B
  332. */
  333. /* build the color flags */
  334. color_flags_a = color_flags_b = 0;
  335. color_flags_a =
  336. (s->buf[stream_ptr + 0] << 16) |
  337. ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
  338. ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
  339. ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
  340. ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
  341. color_flags_b =
  342. (s->buf[stream_ptr + 4] << 16) |
  343. ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
  344. ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
  345. ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
  346. (s->buf[stream_ptr + 5] & 0x0F);
  347. stream_ptr += 6;
  348. color_flags = color_flags_a;
  349. /* flag mask actually acts as a bit shift count here */
  350. flag_mask = 21;
  351. block_ptr = row_ptr + pixel_ptr;
  352. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  353. /* reload flags at third row (iteration pixel_y == 2) */
  354. if (pixel_y == 2) {
  355. color_flags = color_flags_b;
  356. flag_mask = 21;
  357. }
  358. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  359. pixel = color_table_index +
  360. ((color_flags >> flag_mask) & 0x07);
  361. flag_mask -= 3;
  362. pixels[block_ptr++] = s->color_octets[pixel];
  363. }
  364. block_ptr += row_inc;
  365. }
  366. ADVANCE_BLOCK();
  367. }
  368. break;
  369. /* 16-color block encoding (every pixel is a different color) */
  370. case 0xE0:
  371. n_blocks = (opcode & 0x0F) + 1;
  372. while (n_blocks--) {
  373. block_ptr = row_ptr + pixel_ptr;
  374. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  375. for (pixel_x = 0; pixel_x < 4; pixel_x++) {
  376. pixels[block_ptr++] = s->buf[stream_ptr++];
  377. }
  378. block_ptr += row_inc;
  379. }
  380. ADVANCE_BLOCK();
  381. }
  382. break;
  383. case 0xF0:
  384. printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n");
  385. break;
  386. }
  387. }
  388. }
  389. static int smc_decode_init(AVCodecContext *avctx)
  390. {
  391. SmcContext *s = (SmcContext *)avctx->priv_data;
  392. s->avctx = avctx;
  393. avctx->pix_fmt = PIX_FMT_PAL8;
  394. avctx->has_b_frames = 0;
  395. dsputil_init(&s->dsp, avctx);
  396. s->frame.data[0] = NULL;
  397. return 0;
  398. }
  399. static int smc_decode_frame(AVCodecContext *avctx,
  400. void *data, int *data_size,
  401. uint8_t *buf, int buf_size)
  402. {
  403. SmcContext *s = (SmcContext *)avctx->priv_data;
  404. /* no supplementary picture */
  405. if (buf_size == 0)
  406. return 0;
  407. s->buf = buf;
  408. s->size = buf_size;
  409. s->frame.reference = 1;
  410. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  411. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  412. if (avctx->reget_buffer(avctx, &s->frame)) {
  413. printf ("reget_buffer() failed\n");
  414. return -1;
  415. }
  416. smc_decode_stream(s);
  417. *data_size = sizeof(AVFrame);
  418. *(AVFrame*)data = s->frame;
  419. /* always report that the buffer was completely consumed */
  420. return buf_size;
  421. }
  422. static int smc_decode_end(AVCodecContext *avctx)
  423. {
  424. SmcContext *s = (SmcContext *)avctx->priv_data;
  425. if (s->frame.data[0])
  426. avctx->release_buffer(avctx, &s->frame);
  427. return 0;
  428. }
  429. AVCodec smc_decoder = {
  430. "smc",
  431. CODEC_TYPE_VIDEO,
  432. CODEC_ID_SMC,
  433. sizeof(SmcContext),
  434. smc_decode_init,
  435. NULL,
  436. smc_decode_end,
  437. smc_decode_frame,
  438. CODEC_CAP_DR1,
  439. };