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.

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