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.

266 lines
7.8KB

  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. s->frame = av_frame_alloc();
  95. if (!s->frame)
  96. return AVERROR(ENOMEM);
  97. return 0;
  98. }
  99. #define DEFAULT_BG_COLOR 0
  100. av_unused static void hscroll(AVCodecContext *avctx)
  101. {
  102. XbinContext *s = avctx->priv_data;
  103. if (s->y < avctx->height - s->font_height) {
  104. s->y += s->font_height;
  105. } else {
  106. memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
  107. (avctx->height - s->font_height)*s->frame->linesize[0]);
  108. memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
  109. DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
  110. }
  111. }
  112. /**
  113. * Draw character to screen
  114. */
  115. static void draw_char(AVCodecContext *avctx, int c, int a)
  116. {
  117. XbinContext *s = avctx->priv_data;
  118. if (s->y > avctx->height - s->font_height)
  119. return;
  120. ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
  121. s->frame->linesize[0], s->font, s->font_height, c,
  122. a & 0x0F, a >> 4);
  123. s->x += FONT_WIDTH;
  124. if (s->x > avctx->width - FONT_WIDTH) {
  125. s->x = 0;
  126. s->y += s->font_height;
  127. }
  128. }
  129. static int decode_frame(AVCodecContext *avctx,
  130. void *data, int *got_frame,
  131. AVPacket *avpkt)
  132. {
  133. XbinContext *s = avctx->priv_data;
  134. const uint8_t *buf = avpkt->data;
  135. int buf_size = avpkt->size;
  136. const uint8_t *buf_end = buf+buf_size;
  137. int ret;
  138. s->x = s->y = 0;
  139. if ((ret = ff_reget_buffer(avctx, s->frame)) < 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. if ((ret = av_frame_ref(data, s->frame)) < 0)
  196. return ret;
  197. *got_frame = 1;
  198. return buf_size;
  199. }
  200. static av_cold int decode_end(AVCodecContext *avctx)
  201. {
  202. XbinContext *s = avctx->priv_data;
  203. av_frame_free(&s->frame);
  204. return 0;
  205. }
  206. #if CONFIG_BINTEXT_DECODER
  207. AVCodec ff_bintext_decoder = {
  208. .name = "bintext",
  209. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  210. .type = AVMEDIA_TYPE_VIDEO,
  211. .id = AV_CODEC_ID_BINTEXT,
  212. .priv_data_size = sizeof(XbinContext),
  213. .init = decode_init,
  214. .close = decode_end,
  215. .decode = decode_frame,
  216. .capabilities = AV_CODEC_CAP_DR1,
  217. };
  218. #endif
  219. #if CONFIG_XBIN_DECODER
  220. AVCodec ff_xbin_decoder = {
  221. .name = "xbin",
  222. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .id = AV_CODEC_ID_XBIN,
  225. .priv_data_size = sizeof(XbinContext),
  226. .init = decode_init,
  227. .close = decode_end,
  228. .decode = decode_frame,
  229. .capabilities = AV_CODEC_CAP_DR1,
  230. };
  231. #endif
  232. #if CONFIG_IDF_DECODER
  233. AVCodec ff_idf_decoder = {
  234. .name = "idf",
  235. .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
  236. .type = AVMEDIA_TYPE_VIDEO,
  237. .id = AV_CODEC_ID_IDF,
  238. .priv_data_size = sizeof(XbinContext),
  239. .init = decode_init,
  240. .close = decode_end,
  241. .decode = decode_frame,
  242. .capabilities = AV_CODEC_CAP_DR1,
  243. };
  244. #endif