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.

253 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. s->frame.reference = 1;
  48. if (avctx->get_buffer(avctx, &s->frame)) {
  49. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  50. return -1;
  51. }
  52. p = avctx->extradata;
  53. if (p) {
  54. s->font_height = p[0];
  55. s->flags = p[1];
  56. p += 2;
  57. } else {
  58. s->font_height = 8;
  59. s->flags = 0;
  60. }
  61. if ((s->flags & BINTEXT_PALETTE)) {
  62. for (i = 0; i < 16; i++) {
  63. s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2);
  64. p += 3;
  65. }
  66. } else {
  67. for (i = 0; i < 16; i++)
  68. s->palette[i] = 0xFF000000 | ff_cga_palette[i];
  69. }
  70. if ((s->flags & BINTEXT_FONT)) {
  71. s->font = p;
  72. } else {
  73. switch(s->font_height) {
  74. default:
  75. av_log(avctx, AV_LOG_WARNING, "font height %i not support\n", s->font_height);
  76. s->font_height = 8;
  77. case 8:
  78. s->font = ff_cga_font;
  79. break;
  80. case 16:
  81. s->font = ff_vga16_font;
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
  87. #define DEFAULT_BG_COLOR 0
  88. static void hscroll(AVCodecContext *avctx)
  89. {
  90. XbinContext *s = avctx->priv_data;
  91. if (s->y < avctx->height - s->font_height) {
  92. s->y += s->font_height;
  93. } else {
  94. memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
  95. (avctx->height - s->font_height)*s->frame.linesize[0]);
  96. memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
  97. DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
  98. }
  99. }
  100. #define FONT_WIDTH 8
  101. /**
  102. * Draw character to screen
  103. */
  104. static void draw_char(AVCodecContext *avctx, int c, int a)
  105. {
  106. XbinContext *s = avctx->priv_data;
  107. if (s->y > avctx->height - s->font_height)
  108. return;
  109. ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
  110. s->frame.linesize[0], s->font, s->font_height, c,
  111. a & 0x0F, a >> 4);
  112. s->x += FONT_WIDTH;
  113. if (s->x >= avctx->width) {
  114. s->x = 0;
  115. s->y += s->font_height;
  116. }
  117. }
  118. static int decode_frame(AVCodecContext *avctx,
  119. void *data, int *data_size,
  120. AVPacket *avpkt)
  121. {
  122. XbinContext *s = avctx->priv_data;
  123. const uint8_t *buf = avpkt->data;
  124. int buf_size = avpkt->size;
  125. const uint8_t *buf_end = buf+buf_size;
  126. if (avctx->reget_buffer(avctx, &s->frame)) {
  127. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  128. return -1;
  129. }
  130. s->frame.pict_type = FF_I_TYPE;
  131. s->frame.palette_has_changed = 1;
  132. memcpy(s->frame.data[1], s->palette, 16 * 4);
  133. if (avctx->codec_id == CODEC_ID_XBIN) {
  134. while (buf + 2 < buf_end) {
  135. int i,c,a;
  136. int type = *buf >> 6;
  137. int count = (*buf & 0x3F) + 1;
  138. buf++;
  139. switch (type) {
  140. case 0: //no compression
  141. for (i = 0; i < count && buf + 1 < buf_end; i++) {
  142. draw_char(avctx, buf[0], buf[1]);
  143. buf += 2;
  144. }
  145. break;
  146. case 1: //character compression
  147. c = *buf++;
  148. for (i = 0; i < count && buf < buf_end; i++)
  149. draw_char(avctx, c, *buf++);
  150. break;
  151. case 2: //attribute compression
  152. a = *buf++;
  153. for (i = 0; i < count && buf < buf_end; i++)
  154. draw_char(avctx, *buf++, a);
  155. break;
  156. case 3: //character/attribute compression
  157. c = *buf++;
  158. a = *buf++;
  159. for (i = 0; i < count && buf < buf_end; i++)
  160. draw_char(avctx, c, a);
  161. break;
  162. }
  163. }
  164. } else if (avctx->codec_id == CODEC_ID_IDF) {
  165. while (buf + 2 < buf_end) {
  166. if (AV_RL16(buf) == 1) {
  167. int i;
  168. if (buf + 6 > buf_end)
  169. break;
  170. for (i = 0; i < buf[2]; i++)
  171. draw_char(avctx, buf[4], buf[5]);
  172. buf += 6;
  173. } else {
  174. draw_char(avctx, buf[0], buf[1]);
  175. buf += 2;
  176. }
  177. }
  178. } else {
  179. while (buf + 1 < buf_end) {
  180. draw_char(avctx, buf[0], buf[1]);
  181. buf += 2;
  182. }
  183. }
  184. *data_size = sizeof(AVFrame);
  185. *(AVFrame*)data = s->frame;
  186. return buf_size;
  187. }
  188. static av_cold int decode_end(AVCodecContext *avctx)
  189. {
  190. XbinContext *s = avctx->priv_data;
  191. if (s->frame.data[0])
  192. avctx->release_buffer(avctx, &s->frame);
  193. return 0;
  194. }
  195. AVCodec ff_bintext_decoder = {
  196. "xbin",
  197. AVMEDIA_TYPE_VIDEO,
  198. CODEC_ID_BINTEXT,
  199. sizeof(XbinContext),
  200. decode_init,
  201. NULL,
  202. decode_end,
  203. decode_frame,
  204. CODEC_CAP_DR1,
  205. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  206. };
  207. AVCodec ff_xbin_decoder = {
  208. "xbin",
  209. AVMEDIA_TYPE_VIDEO,
  210. CODEC_ID_XBIN,
  211. sizeof(XbinContext),
  212. decode_init,
  213. NULL,
  214. decode_end,
  215. decode_frame,
  216. CODEC_CAP_DR1,
  217. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
  218. };
  219. AVCodec ff_idf_decoder = {
  220. "idf",
  221. AVMEDIA_TYPE_VIDEO,
  222. CODEC_ID_IDF,
  223. sizeof(XbinContext),
  224. decode_init,
  225. NULL,
  226. decode_end,
  227. decode_frame,
  228. CODEC_CAP_DR1,
  229. .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
  230. };