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.

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