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
7.4KB

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