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.

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