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.

492 lines
16KB

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