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.

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