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.

251 lines
6.7KB

  1. /*
  2. * Binary text decoder
  3. * eXtended BINary text (XBIN) decoder
  4. * iCEDraw File decoder
  5. * Copyright (c) 2010 Peter Ross (pross@xvid.org)
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * Binary text decoder
  26. * eXtended BINary text (XBIN) decoder
  27. * iCEDraw File decoder
  28. */
  29. #include "libavutil/intreadwrite.h"
  30. #include "avcodec.h"
  31. #include "cga_data.h"
  32. #include "bintext.h"
  33. typedef struct XbinContext {
  34. AVFrame frame;
  35. int palette[16];
  36. int flags;
  37. int font_height;
  38. const uint8_t *font;
  39. int x, y;
  40. } XbinContext;
  41. static av_cold int decode_init(AVCodecContext *avctx)
  42. {
  43. XbinContext *s = avctx->priv_data;
  44. uint8_t *p;
  45. int i;
  46. avctx->pix_fmt = PIX_FMT_PAL8;
  47. p = avctx->extradata;
  48. if (p) {
  49. s->font_height = p[0];
  50. s->flags = p[1];
  51. p += 2;
  52. } else {
  53. s->font_height = 8;
  54. s->flags = 0;
  55. }
  56. if ((s->flags & BINTEXT_PALETTE)) {
  57. for (i = 0; i < 16; i++) {
  58. s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2);
  59. p += 3;
  60. }
  61. } else {
  62. for (i = 0; i < 16; i++)
  63. s->palette[i] = 0xFF000000 | ff_cga_palette[i];
  64. }
  65. if ((s->flags & BINTEXT_FONT)) {
  66. s->font = p;
  67. } else {
  68. switch(s->font_height) {
  69. default:
  70. av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
  71. s->font_height = 8;
  72. case 8:
  73. s->font = ff_cga_font;
  74. break;
  75. case 16:
  76. s->font = ff_vga16_font;
  77. break;
  78. }
  79. }
  80. return 0;
  81. }
  82. #define DEFAULT_BG_COLOR 0
  83. static void hscroll(AVCodecContext *avctx)
  84. {
  85. XbinContext *s = avctx->priv_data;
  86. if (s->y < avctx->height - s->font_height) {
  87. s->y += s->font_height;
  88. } else {
  89. memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
  90. (avctx->height - s->font_height)*s->frame.linesize[0]);
  91. memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
  92. DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
  93. }
  94. }
  95. #define FONT_WIDTH 8
  96. /**
  97. * Draw character to screen
  98. */
  99. static void draw_char(AVCodecContext *avctx, int c, int a)
  100. {
  101. XbinContext *s = avctx->priv_data;
  102. if (s->y > avctx->height - s->font_height)
  103. return;
  104. ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
  105. s->frame.linesize[0], s->font, s->font_height, c,
  106. a & 0x0F, a >> 4);
  107. s->x += FONT_WIDTH;
  108. if (s->x > avctx->width - FONT_WIDTH) {
  109. s->x = 0;
  110. s->y += s->font_height;
  111. }
  112. }
  113. static int decode_frame(AVCodecContext *avctx,
  114. void *data, int *data_size,
  115. AVPacket *avpkt)
  116. {
  117. XbinContext *s = avctx->priv_data;
  118. const uint8_t *buf = avpkt->data;
  119. int buf_size = avpkt->size;
  120. const uint8_t *buf_end = buf+buf_size;
  121. s->x = s->y = 0;
  122. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
  123. FF_BUFFER_HINTS_PRESERVE |
  124. FF_BUFFER_HINTS_REUSABLE;
  125. if (avctx->reget_buffer(avctx, &s->frame)) {
  126. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  127. return -1;
  128. }
  129. s->frame.pict_type = FF_I_TYPE;
  130. s->frame.palette_has_changed = 1;
  131. memcpy(s->frame.data[1], s->palette, 16 * 4);
  132. if (avctx->codec_id == CODEC_ID_XBIN) {
  133. while (buf + 2 < buf_end) {
  134. int i,c,a;
  135. int type = *buf >> 6;
  136. int count = (*buf & 0x3F) + 1;
  137. buf++;
  138. switch (type) {
  139. case 0: //no compression
  140. for (i = 0; i < count && buf + 1 < buf_end; i++) {
  141. draw_char(avctx, buf[0], buf[1]);
  142. buf += 2;
  143. }
  144. break;
  145. case 1: //character compression
  146. c = *buf++;
  147. for (i = 0; i < count && buf < buf_end; i++)
  148. draw_char(avctx, c, *buf++);
  149. break;
  150. case 2: //attribute compression
  151. a = *buf++;
  152. for (i = 0; i < count && buf < buf_end; i++)
  153. draw_char(avctx, *buf++, a);
  154. break;
  155. case 3: //character/attribute compression
  156. c = *buf++;
  157. a = *buf++;
  158. for (i = 0; i < count && buf < buf_end; i++)
  159. draw_char(avctx, c, a);
  160. break;
  161. }
  162. }
  163. } else if (avctx->codec_id == CODEC_ID_IDF) {
  164. while (buf + 2 < buf_end) {
  165. if (AV_RL16(buf) == 1) {
  166. int i;
  167. if (buf + 6 > buf_end)
  168. break;
  169. for (i = 0; i < buf[2]; i++)
  170. draw_char(avctx, buf[4], buf[5]);
  171. buf += 6;
  172. } else {
  173. draw_char(avctx, buf[0], buf[1]);
  174. buf += 2;
  175. }
  176. }
  177. } else {
  178. while (buf + 1 < buf_end) {
  179. draw_char(avctx, buf[0], buf[1]);
  180. buf += 2;
  181. }
  182. }
  183. *data_size = sizeof(AVFrame);
  184. *(AVFrame*)data = s->frame;
  185. return buf_size;
  186. }
  187. static av_cold int decode_end(AVCodecContext *avctx)
  188. {
  189. XbinContext *s = avctx->priv_data;
  190. if (s->frame.data[0])
  191. avctx->release_buffer(avctx, &s->frame);
  192. return 0;
  193. }
  194. AVCodec ff_bintext_decoder = {
  195. "bintext",
  196. AVMEDIA_TYPE_VIDEO,
  197. CODEC_ID_BINTEXT,
  198. sizeof(XbinContext),
  199. decode_init,
  200. NULL,
  201. decode_end,
  202. decode_frame,
  203. CODEC_CAP_DR1,
  204. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  205. };
  206. AVCodec ff_xbin_decoder = {
  207. "xbin",
  208. AVMEDIA_TYPE_VIDEO,
  209. CODEC_ID_XBIN,
  210. sizeof(XbinContext),
  211. decode_init,
  212. NULL,
  213. decode_end,
  214. decode_frame,
  215. CODEC_CAP_DR1,
  216. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
  217. };
  218. AVCodec ff_idf_decoder = {
  219. "idf",
  220. AVMEDIA_TYPE_VIDEO,
  221. CODEC_ID_IDF,
  222. sizeof(XbinContext),
  223. decode_init,
  224. NULL,
  225. decode_end,
  226. decode_frame,
  227. CODEC_CAP_DR1,
  228. .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
  229. };