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.

496 lines
16KB

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