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.

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