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.

386 lines
12KB

  1. /*
  2. * CD Graphics Video Decoder
  3. * Copyright (c) 2009 Michael Tison
  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. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "internal.h"
  24. /**
  25. * @file
  26. * @brief CD Graphics Video Decoder
  27. * @author Michael Tison
  28. * @see http://wiki.multimedia.cx/index.php?title=CD_Graphics
  29. * @see http://www.ccs.neu.edu/home/bchafy/cdb/info/cdg
  30. */
  31. /// default screen sizes
  32. #define CDG_FULL_WIDTH 300
  33. #define CDG_FULL_HEIGHT 216
  34. #define CDG_DISPLAY_WIDTH 294
  35. #define CDG_DISPLAY_HEIGHT 204
  36. #define CDG_BORDER_WIDTH 6
  37. #define CDG_BORDER_HEIGHT 12
  38. /// masks
  39. #define CDG_COMMAND 0x09
  40. #define CDG_MASK 0x3F
  41. /// instruction codes
  42. #define CDG_INST_MEMORY_PRESET 1
  43. #define CDG_INST_BORDER_PRESET 2
  44. #define CDG_INST_TILE_BLOCK 6
  45. #define CDG_INST_SCROLL_PRESET 20
  46. #define CDG_INST_SCROLL_COPY 24
  47. #define CDG_INST_TRANSPARENT_COL 28
  48. #define CDG_INST_LOAD_PAL_LO 30
  49. #define CDG_INST_LOAD_PAL_HIGH 31
  50. #define CDG_INST_TILE_BLOCK_XOR 38
  51. /// data sizes
  52. #define CDG_PACKET_SIZE 24
  53. #define CDG_DATA_SIZE 16
  54. #define CDG_TILE_HEIGHT 12
  55. #define CDG_TILE_WIDTH 6
  56. #define CDG_MINIMUM_PKT_SIZE 6
  57. #define CDG_MINIMUM_SCROLL_SIZE 3
  58. #define CDG_HEADER_SIZE 8
  59. #define CDG_PALETTE_SIZE 16
  60. typedef struct CDGraphicsContext {
  61. AVFrame *frame;
  62. int hscroll;
  63. int vscroll;
  64. int transparency;
  65. } CDGraphicsContext;
  66. static av_cold int cdg_decode_init(AVCodecContext *avctx)
  67. {
  68. CDGraphicsContext *cc = avctx->priv_data;
  69. cc->frame = av_frame_alloc();
  70. if (!cc->frame)
  71. return AVERROR(ENOMEM);
  72. cc->transparency = -1;
  73. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  74. return ff_set_dimensions(avctx, CDG_FULL_WIDTH, CDG_FULL_HEIGHT);
  75. }
  76. static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
  77. {
  78. int y;
  79. int lsize = cc->frame->linesize[0];
  80. uint8_t *buf = cc->frame->data[0];
  81. int color = data[0] & 0x0F;
  82. if (!(data[1] & 0x0F)) {
  83. /// fill the top and bottom borders
  84. memset(buf, color, CDG_BORDER_HEIGHT * lsize);
  85. memset(buf + (CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT) * lsize,
  86. color, CDG_BORDER_HEIGHT * lsize);
  87. /// fill the side borders
  88. for (y = CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT; y++) {
  89. memset(buf + y * lsize, color, CDG_BORDER_WIDTH);
  90. memset(buf + CDG_FULL_WIDTH - CDG_BORDER_WIDTH + y * lsize,
  91. color, CDG_BORDER_WIDTH);
  92. }
  93. }
  94. }
  95. static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
  96. {
  97. uint8_t r, g, b;
  98. uint16_t color;
  99. int i;
  100. int array_offset = low ? 0 : 8;
  101. uint32_t *palette = (uint32_t *) cc->frame->data[1];
  102. for (i = 0; i < 8; i++) {
  103. color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
  104. r = ((color >> 8) & 0x000F) * 17;
  105. g = ((color >> 4) & 0x000F) * 17;
  106. b = ((color ) & 0x000F) * 17;
  107. palette[i + array_offset] = 0xFFU << 24 | r << 16 | g << 8 | b;
  108. if (cc->transparency >= 0)
  109. palette[cc->transparency] &= 0xFFFFFF;
  110. }
  111. cc->frame->palette_has_changed = 1;
  112. }
  113. static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
  114. {
  115. unsigned ci, ri;
  116. int color;
  117. int x, y;
  118. int ai;
  119. int stride = cc->frame->linesize[0];
  120. uint8_t *buf = cc->frame->data[0];
  121. ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
  122. ci = (data[3] & 0x3F) * CDG_TILE_WIDTH + cc->hscroll;
  123. if (ri > (CDG_FULL_HEIGHT - CDG_TILE_HEIGHT))
  124. return AVERROR(EINVAL);
  125. if (ci > (CDG_FULL_WIDTH - CDG_TILE_WIDTH))
  126. return AVERROR(EINVAL);
  127. for (y = 0; y < CDG_TILE_HEIGHT; y++) {
  128. for (x = 0; x < CDG_TILE_WIDTH; x++) {
  129. if (!((data[4 + y] >> (5 - x)) & 0x01))
  130. color = data[0] & 0x0F;
  131. else
  132. color = data[1] & 0x0F;
  133. ai = ci + x + (stride * (ri + y));
  134. if (b)
  135. color ^= buf[ai];
  136. buf[ai] = color;
  137. }
  138. }
  139. return 0;
  140. }
  141. #define UP 2
  142. #define DOWN 1
  143. #define LEFT 2
  144. #define RIGHT 1
  145. static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out,
  146. int in_tl_x, int in_tl_y, uint8_t *in,
  147. int w, int h, int stride)
  148. {
  149. int y;
  150. in += in_tl_x + in_tl_y * stride;
  151. out += out_tl_x + out_tl_y * stride;
  152. for (y = 0; y < h; y++)
  153. memcpy(out + y * stride, in + y * stride, w);
  154. }
  155. static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out,
  156. int color, int w, int h, int stride)
  157. {
  158. int y;
  159. for (y = tl_y; y < tl_y + h; y++)
  160. memset(out + tl_x + y * stride, color, w);
  161. }
  162. static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out,
  163. int in_tl_x, int in_tl_y, uint8_t *in,
  164. int color, int w, int h, int stride, int roll)
  165. {
  166. if (roll) {
  167. cdg_copy_rect_buf(out_tl_x, out_tl_y, out, in_tl_x, in_tl_y,
  168. in, w, h, stride);
  169. } else {
  170. cdg_fill_rect_preset(out_tl_x, out_tl_y, out, color, w, h, stride);
  171. }
  172. }
  173. static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data,
  174. AVFrame *new_frame, int roll_over)
  175. {
  176. int color;
  177. int hscmd, h_off, hinc, vscmd, v_off, vinc;
  178. int y;
  179. int stride = cc->frame->linesize[0];
  180. uint8_t *in = cc->frame->data[0];
  181. uint8_t *out = new_frame->data[0];
  182. color = data[0] & 0x0F;
  183. hscmd = (data[1] & 0x30) >> 4;
  184. vscmd = (data[2] & 0x30) >> 4;
  185. h_off = FFMIN(data[1] & 0x07, CDG_BORDER_WIDTH - 1);
  186. v_off = FFMIN(data[2] & 0x0F, CDG_BORDER_HEIGHT - 1);
  187. /// find the difference and save the offset for cdg_tile_block usage
  188. hinc = h_off - cc->hscroll;
  189. vinc = v_off - cc->vscroll;
  190. cc->hscroll = h_off;
  191. cc->vscroll = v_off;
  192. if (vscmd == UP)
  193. vinc -= 12;
  194. if (vscmd == DOWN)
  195. vinc += 12;
  196. if (hscmd == LEFT)
  197. hinc -= 6;
  198. if (hscmd == RIGHT)
  199. hinc += 6;
  200. if (!hinc && !vinc)
  201. return;
  202. memcpy(new_frame->data[1], cc->frame->data[1], CDG_PALETTE_SIZE * 4);
  203. for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
  204. memcpy(out + FFMAX(0, hinc) + stride * y,
  205. in + FFMAX(0, hinc) - hinc + (y - vinc) * stride,
  206. FFMIN(stride + hinc, stride));
  207. if (vinc > 0)
  208. cdg_fill_wrapper(0, 0, out,
  209. 0, CDG_FULL_HEIGHT - vinc, in, color,
  210. stride, vinc, stride, roll_over);
  211. else if (vinc < 0)
  212. cdg_fill_wrapper(0, CDG_FULL_HEIGHT + vinc, out,
  213. 0, 0, in, color,
  214. stride, -1 * vinc, stride, roll_over);
  215. if (hinc > 0)
  216. cdg_fill_wrapper(0, 0, out,
  217. CDG_FULL_WIDTH - hinc, 0, in, color,
  218. hinc, CDG_FULL_HEIGHT, stride, roll_over);
  219. else if (hinc < 0)
  220. cdg_fill_wrapper(CDG_FULL_WIDTH + hinc, 0, out,
  221. 0, 0, in, color,
  222. -1 * hinc, CDG_FULL_HEIGHT, stride, roll_over);
  223. }
  224. static int cdg_decode_frame(AVCodecContext *avctx,
  225. void *data, int *got_frame, AVPacket *avpkt)
  226. {
  227. GetByteContext gb;
  228. int buf_size = avpkt->size;
  229. int ret;
  230. uint8_t command, inst;
  231. uint8_t cdg_data[CDG_DATA_SIZE] = {0};
  232. AVFrame *frame = data;
  233. CDGraphicsContext *cc = avctx->priv_data;
  234. if (buf_size < CDG_MINIMUM_PKT_SIZE) {
  235. av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n");
  236. return AVERROR(EINVAL);
  237. }
  238. if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) {
  239. av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n");
  240. return AVERROR(EINVAL);
  241. }
  242. bytestream2_init(&gb, avpkt->data, avpkt->size);
  243. if ((ret = ff_reget_buffer(avctx, cc->frame)) < 0)
  244. return ret;
  245. if (!avctx->frame_number) {
  246. memset(cc->frame->data[0], 0, cc->frame->linesize[0] * avctx->height);
  247. memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
  248. }
  249. command = bytestream2_get_byte(&gb);
  250. inst = bytestream2_get_byte(&gb);
  251. inst &= CDG_MASK;
  252. bytestream2_skip(&gb, 2);
  253. bytestream2_get_buffer(&gb, cdg_data, sizeof(cdg_data));
  254. if ((command & CDG_MASK) == CDG_COMMAND) {
  255. switch (inst) {
  256. case CDG_INST_MEMORY_PRESET:
  257. if (!(cdg_data[1] & 0x0F))
  258. memset(cc->frame->data[0], cdg_data[0] & 0x0F,
  259. cc->frame->linesize[0] * CDG_FULL_HEIGHT);
  260. break;
  261. case CDG_INST_LOAD_PAL_LO:
  262. case CDG_INST_LOAD_PAL_HIGH:
  263. if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
  264. av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
  265. return AVERROR(EINVAL);
  266. }
  267. cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
  268. break;
  269. case CDG_INST_BORDER_PRESET:
  270. cdg_border_preset(cc, cdg_data);
  271. break;
  272. case CDG_INST_TILE_BLOCK_XOR:
  273. case CDG_INST_TILE_BLOCK:
  274. if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
  275. av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
  276. return AVERROR(EINVAL);
  277. }
  278. ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
  279. if (ret) {
  280. av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
  281. return ret;
  282. }
  283. break;
  284. case CDG_INST_SCROLL_PRESET:
  285. case CDG_INST_SCROLL_COPY:
  286. if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
  287. av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
  288. return AVERROR(EINVAL);
  289. }
  290. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  291. return ret;
  292. cdg_scroll(cc, cdg_data, frame, inst == CDG_INST_SCROLL_COPY);
  293. av_frame_unref(cc->frame);
  294. ret = av_frame_ref(cc->frame, frame);
  295. if (ret < 0)
  296. return ret;
  297. break;
  298. case CDG_INST_TRANSPARENT_COL:
  299. cc->transparency = cdg_data[0] & 0xF;
  300. break;
  301. default:
  302. break;
  303. }
  304. if (!frame->data[0]) {
  305. ret = av_frame_ref(frame, cc->frame);
  306. if (ret < 0)
  307. return ret;
  308. }
  309. *got_frame = 1;
  310. } else {
  311. *got_frame = 0;
  312. }
  313. return avpkt->size;
  314. }
  315. static av_cold int cdg_decode_end(AVCodecContext *avctx)
  316. {
  317. CDGraphicsContext *cc = avctx->priv_data;
  318. av_frame_free(&cc->frame);
  319. return 0;
  320. }
  321. AVCodec ff_cdgraphics_decoder = {
  322. .name = "cdgraphics",
  323. .long_name = NULL_IF_CONFIG_SMALL("CD Graphics video"),
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .id = AV_CODEC_ID_CDGRAPHICS,
  326. .priv_data_size = sizeof(CDGraphicsContext),
  327. .init = cdg_decode_init,
  328. .close = cdg_decode_end,
  329. .decode = cdg_decode_frame,
  330. .capabilities = AV_CODEC_CAP_DR1,
  331. };