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.

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