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

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