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.

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