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.

261 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. typedef struct XbinContext {
  36. AVFrame *frame;
  37. int palette[16];
  38. int flags;
  39. int font_height;
  40. const uint8_t *font;
  41. int x, y;
  42. } XbinContext;
  43. static av_cold int decode_init(AVCodecContext *avctx)
  44. {
  45. XbinContext *s = avctx->priv_data;
  46. uint8_t *p;
  47. int i;
  48. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  49. p = avctx->extradata;
  50. if (p) {
  51. s->font_height = p[0];
  52. s->flags = p[1];
  53. p += 2;
  54. if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
  55. + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
  56. av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
  57. return AVERROR_INVALIDDATA;
  58. }
  59. } else {
  60. s->font_height = 8;
  61. s->flags = 0;
  62. }
  63. if ((s->flags & BINTEXT_PALETTE)) {
  64. for (i = 0; i < 16; i++) {
  65. s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
  66. p += 3;
  67. }
  68. } else {
  69. for (i = 0; i < 16; i++)
  70. s->palette[i] = 0xFF000000 | ff_cga_palette[i];
  71. }
  72. if ((s->flags & BINTEXT_FONT)) {
  73. s->font = p;
  74. } else {
  75. switch(s->font_height) {
  76. default:
  77. av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
  78. s->font_height = 8;
  79. case 8:
  80. s->font = avpriv_cga_font;
  81. break;
  82. case 16:
  83. s->font = avpriv_vga16_font;
  84. break;
  85. }
  86. }
  87. s->frame = av_frame_alloc();
  88. if (!s->frame)
  89. return AVERROR(ENOMEM);
  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. #define FONT_WIDTH 8
  106. /**
  107. * Draw character to screen
  108. */
  109. static void draw_char(AVCodecContext *avctx, int c, int a)
  110. {
  111. XbinContext *s = avctx->priv_data;
  112. if (s->y > avctx->height - s->font_height)
  113. return;
  114. ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
  115. s->frame->linesize[0], s->font, s->font_height, c,
  116. a & 0x0F, a >> 4);
  117. s->x += FONT_WIDTH;
  118. if (s->x > avctx->width - FONT_WIDTH) {
  119. s->x = 0;
  120. s->y += s->font_height;
  121. }
  122. }
  123. static int decode_frame(AVCodecContext *avctx,
  124. void *data, int *got_frame,
  125. AVPacket *avpkt)
  126. {
  127. XbinContext *s = avctx->priv_data;
  128. const uint8_t *buf = avpkt->data;
  129. int buf_size = avpkt->size;
  130. const uint8_t *buf_end = buf+buf_size;
  131. int ret;
  132. s->x = s->y = 0;
  133. if (ff_reget_buffer(avctx, s->frame) < 0) {
  134. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  135. return -1;
  136. }
  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. if ((ret = av_frame_ref(data, s->frame)) < 0)
  192. return ret;
  193. *got_frame = 1;
  194. return buf_size;
  195. }
  196. static av_cold int decode_end(AVCodecContext *avctx)
  197. {
  198. XbinContext *s = avctx->priv_data;
  199. av_frame_free(&s->frame);
  200. return 0;
  201. }
  202. #if CONFIG_BINTEXT_DECODER
  203. AVCodec ff_bintext_decoder = {
  204. .name = "bintext",
  205. .type = AVMEDIA_TYPE_VIDEO,
  206. .id = AV_CODEC_ID_BINTEXT,
  207. .priv_data_size = sizeof(XbinContext),
  208. .init = decode_init,
  209. .close = decode_end,
  210. .decode = decode_frame,
  211. .capabilities = CODEC_CAP_DR1,
  212. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  213. };
  214. #endif
  215. #if CONFIG_XBIN_DECODER
  216. AVCodec ff_xbin_decoder = {
  217. .name = "xbin",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .id = AV_CODEC_ID_XBIN,
  220. .priv_data_size = sizeof(XbinContext),
  221. .init = decode_init,
  222. .close = decode_end,
  223. .decode = decode_frame,
  224. .capabilities = CODEC_CAP_DR1,
  225. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
  226. };
  227. #endif
  228. #if CONFIG_IDF_DECODER
  229. AVCodec ff_idf_decoder = {
  230. .name = "idf",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. .id = AV_CODEC_ID_IDF,
  233. .priv_data_size = sizeof(XbinContext),
  234. .init = decode_init,
  235. .close = decode_end,
  236. .decode = decode_frame,
  237. .capabilities = CODEC_CAP_DR1,
  238. .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
  239. };
  240. #endif