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.

383 lines
11KB

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