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.

247 lines
6.5KB

  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 libavcodec/xbin.c
  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 support\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. if (avctx->reget_buffer(avctx, &s->frame)) {
  122. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  123. return -1;
  124. }
  125. s->frame.pict_type = FF_I_TYPE;
  126. s->frame.palette_has_changed = 1;
  127. memcpy(s->frame.data[1], s->palette, 16 * 4);
  128. if (avctx->codec_id == CODEC_ID_XBIN) {
  129. while (buf + 2 < buf_end) {
  130. int i,c,a;
  131. int type = *buf >> 6;
  132. int count = (*buf & 0x3F) + 1;
  133. buf++;
  134. switch (type) {
  135. case 0: //no compression
  136. for (i = 0; i < count && buf + 1 < buf_end; i++) {
  137. draw_char(avctx, buf[0], buf[1]);
  138. buf += 2;
  139. }
  140. break;
  141. case 1: //character compression
  142. c = *buf++;
  143. for (i = 0; i < count && buf < buf_end; i++)
  144. draw_char(avctx, c, *buf++);
  145. break;
  146. case 2: //attribute compression
  147. a = *buf++;
  148. for (i = 0; i < count && buf < buf_end; i++)
  149. draw_char(avctx, *buf++, a);
  150. break;
  151. case 3: //character/attribute compression
  152. c = *buf++;
  153. a = *buf++;
  154. for (i = 0; i < count && buf < buf_end; i++)
  155. draw_char(avctx, c, a);
  156. break;
  157. }
  158. }
  159. } else if (avctx->codec_id == CODEC_ID_IDF) {
  160. while (buf + 2 < buf_end) {
  161. if (AV_RL16(buf) == 1) {
  162. int i;
  163. if (buf + 6 > buf_end)
  164. break;
  165. for (i = 0; i < buf[2]; i++)
  166. draw_char(avctx, buf[4], buf[5]);
  167. buf += 6;
  168. } else {
  169. draw_char(avctx, buf[0], buf[1]);
  170. buf += 2;
  171. }
  172. }
  173. } else {
  174. while (buf + 1 < buf_end) {
  175. draw_char(avctx, buf[0], buf[1]);
  176. buf += 2;
  177. }
  178. }
  179. *data_size = sizeof(AVFrame);
  180. *(AVFrame*)data = s->frame;
  181. return buf_size;
  182. }
  183. static av_cold int decode_end(AVCodecContext *avctx)
  184. {
  185. XbinContext *s = avctx->priv_data;
  186. if (s->frame.data[0])
  187. avctx->release_buffer(avctx, &s->frame);
  188. return 0;
  189. }
  190. AVCodec ff_bintext_decoder = {
  191. "xbin",
  192. AVMEDIA_TYPE_VIDEO,
  193. CODEC_ID_BINTEXT,
  194. sizeof(XbinContext),
  195. decode_init,
  196. NULL,
  197. decode_end,
  198. decode_frame,
  199. CODEC_CAP_DR1,
  200. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  201. };
  202. AVCodec ff_xbin_decoder = {
  203. "xbin",
  204. AVMEDIA_TYPE_VIDEO,
  205. CODEC_ID_XBIN,
  206. sizeof(XbinContext),
  207. decode_init,
  208. NULL,
  209. decode_end,
  210. decode_frame,
  211. CODEC_CAP_DR1,
  212. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
  213. };
  214. AVCodec ff_idf_decoder = {
  215. "idf",
  216. AVMEDIA_TYPE_VIDEO,
  217. CODEC_ID_IDF,
  218. sizeof(XbinContext),
  219. decode_init,
  220. NULL,
  221. decode_end,
  222. decode_frame,
  223. CODEC_CAP_DR1,
  224. .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
  225. };