3 * eXtended BINary text (XBIN) decoder
5 * Copyright (c) 2010 Peter Ross (pross@xvid.org)
7 * This file is part of FFmpeg.
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.
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.
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
27 * eXtended BINary text (XBIN) decoder
28 * iCEDraw File decoder
31 #include "libavutil/intreadwrite.h"
36 typedef struct XbinContext {
45 static av_cold int decode_init(AVCodecContext *avctx)
47 XbinContext *s = avctx->priv_data;
51 avctx->pix_fmt = PIX_FMT_PAL8;
54 s->font_height = p[0];
62 if ((s->flags & BINTEXT_PALETTE)) {
63 for (i = 0; i < 16; i++) {
64 s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2);
68 for (i = 0; i < 16; i++)
69 s->palette[i] = 0xFF000000 | ff_cga_palette[i];
72 if ((s->flags & BINTEXT_FONT)) {
75 switch(s->font_height) {
77 av_log(avctx, AV_LOG_WARNING, "font height %i not support\n", s->font_height);
80 s->font = ff_cga_font;
83 s->font = ff_vga16_font;
91 #define DEFAULT_BG_COLOR 0
92 static void hscroll(AVCodecContext *avctx)
94 XbinContext *s = avctx->priv_data;
95 if (s->y < avctx->height - s->font_height) {
96 s->y += s->font_height;
98 memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
99 (avctx->height - s->font_height)*s->frame.linesize[0]);
100 memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
101 DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
108 * Draw character to screen
110 static void draw_char(AVCodecContext *avctx, int c, int a)
112 XbinContext *s = avctx->priv_data;
113 if (s->y > avctx->height - s->font_height)
115 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
116 s->frame.linesize[0], s->font, s->font_height, c,
119 if (s->x > avctx->width - FONT_WIDTH) {
121 s->y += s->font_height;
125 static int decode_frame(AVCodecContext *avctx,
126 void *data, int *data_size,
129 XbinContext *s = avctx->priv_data;
130 const uint8_t *buf = avpkt->data;
131 int buf_size = avpkt->size;
132 const uint8_t *buf_end = buf+buf_size;
134 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
135 FF_BUFFER_HINTS_PRESERVE |
136 FF_BUFFER_HINTS_REUSABLE;
137 if (avctx->reget_buffer(avctx, &s->frame)) {
138 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
141 s->frame.pict_type = FF_I_TYPE;
142 s->frame.palette_has_changed = 1;
143 memcpy(s->frame.data[1], s->palette, 16 * 4);
145 if (avctx->codec_id == CODEC_ID_XBIN) {
146 while (buf + 2 < buf_end) {
148 int type = *buf >> 6;
149 int count = (*buf & 0x3F) + 1;
152 case 0: //no compression
153 for (i = 0; i < count && buf + 1 < buf_end; i++) {
154 draw_char(avctx, buf[0], buf[1]);
158 case 1: //character compression
160 for (i = 0; i < count && buf < buf_end; i++)
161 draw_char(avctx, c, *buf++);
163 case 2: //attribute compression
165 for (i = 0; i < count && buf < buf_end; i++)
166 draw_char(avctx, *buf++, a);
168 case 3: //character/attribute compression
171 for (i = 0; i < count && buf < buf_end; i++)
172 draw_char(avctx, c, a);
176 } else if (avctx->codec_id == CODEC_ID_IDF) {
177 while (buf + 2 < buf_end) {
178 if (AV_RL16(buf) == 1) {
180 if (buf + 6 > buf_end)
182 for (i = 0; i < buf[2]; i++)
183 draw_char(avctx, buf[4], buf[5]);
186 draw_char(avctx, buf[0], buf[1]);
191 while (buf + 1 < buf_end) {
192 draw_char(avctx, buf[0], buf[1]);
197 *data_size = sizeof(AVFrame);
198 *(AVFrame*)data = s->frame;
202 static av_cold int decode_end(AVCodecContext *avctx)
204 XbinContext *s = avctx->priv_data;
206 if (s->frame.data[0])
207 avctx->release_buffer(avctx, &s->frame);
212 AVCodec ff_bintext_decoder = {
222 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
225 AVCodec ff_xbin_decoder = {
235 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
238 AVCodec ff_idf_decoder = {
248 .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),