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.

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