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"
32 #include "libavutil/xga_font_data.h"
40 typedef struct XbinContext {
49 static av_cold int decode_init(AVCodecContext *avctx)
51 XbinContext *s = avctx->priv_data;
55 avctx->pix_fmt = AV_PIX_FMT_PAL8;
58 s->font_height = p[0];
61 if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
62 + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
63 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
64 return AVERROR_INVALIDDATA;
71 if ((s->flags & BINTEXT_PALETTE)) {
72 for (i = 0; i < 16; i++) {
73 s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
77 for (i = 0; i < 16; i++)
78 s->palette[i] = 0xFF000000 | ff_cga_palette[i];
81 if ((s->flags & BINTEXT_FONT)) {
84 switch(s->font_height) {
86 av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
89 s->font = avpriv_cga_font;
92 s->font = avpriv_vga16_font;
96 if (avctx->width < FONT_WIDTH || avctx->height < s->font_height)
97 return AVERROR_INVALIDDATA;
100 s->frame = av_frame_alloc();
102 return AVERROR(ENOMEM);
107 #define DEFAULT_BG_COLOR 0
108 av_unused static void hscroll(AVCodecContext *avctx)
110 XbinContext *s = avctx->priv_data;
111 if (s->y < avctx->height - s->font_height) {
112 s->y += s->font_height;
114 memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
115 (avctx->height - s->font_height)*s->frame->linesize[0]);
116 memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
117 DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
122 * Draw character to screen
124 static void draw_char(AVCodecContext *avctx, int c, int a)
126 XbinContext *s = avctx->priv_data;
127 if (s->y > avctx->height - s->font_height)
129 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
130 s->frame->linesize[0], s->font, s->font_height, c,
133 if (s->x > avctx->width - FONT_WIDTH) {
135 s->y += s->font_height;
139 static int decode_frame(AVCodecContext *avctx,
140 void *data, int *got_frame,
143 XbinContext *s = avctx->priv_data;
144 const uint8_t *buf = avpkt->data;
145 int buf_size = avpkt->size;
146 const uint8_t *buf_end = buf+buf_size;
150 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
152 s->frame->pict_type = AV_PICTURE_TYPE_I;
153 s->frame->palette_has_changed = 1;
154 memcpy(s->frame->data[1], s->palette, 16 * 4);
156 if (avctx->codec_id == AV_CODEC_ID_XBIN) {
157 while (buf + 2 < buf_end) {
159 int type = *buf >> 6;
160 int count = (*buf & 0x3F) + 1;
163 case 0: //no compression
164 for (i = 0; i < count && buf + 1 < buf_end; i++) {
165 draw_char(avctx, buf[0], buf[1]);
169 case 1: //character compression
171 for (i = 0; i < count && buf < buf_end; i++)
172 draw_char(avctx, c, *buf++);
174 case 2: //attribute compression
176 for (i = 0; i < count && buf < buf_end; i++)
177 draw_char(avctx, *buf++, a);
179 case 3: //character/attribute compression
182 for (i = 0; i < count && buf < buf_end; i++)
183 draw_char(avctx, c, a);
187 } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
188 while (buf + 2 < buf_end) {
189 if (AV_RL16(buf) == 1) {
191 if (buf + 6 > buf_end)
193 for (i = 0; i < buf[2]; i++)
194 draw_char(avctx, buf[4], buf[5]);
197 draw_char(avctx, buf[0], buf[1]);
202 while (buf + 1 < buf_end) {
203 draw_char(avctx, buf[0], buf[1]);
208 if ((ret = av_frame_ref(data, s->frame)) < 0)
214 static av_cold int decode_end(AVCodecContext *avctx)
216 XbinContext *s = avctx->priv_data;
218 av_frame_free(&s->frame);
223 #if CONFIG_BINTEXT_DECODER
224 AVCodec ff_bintext_decoder = {
226 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
227 .type = AVMEDIA_TYPE_VIDEO,
228 .id = AV_CODEC_ID_BINTEXT,
229 .priv_data_size = sizeof(XbinContext),
232 .decode = decode_frame,
233 .capabilities = AV_CODEC_CAP_DR1,
236 #if CONFIG_XBIN_DECODER
237 AVCodec ff_xbin_decoder = {
239 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
240 .type = AVMEDIA_TYPE_VIDEO,
241 .id = AV_CODEC_ID_XBIN,
242 .priv_data_size = sizeof(XbinContext),
245 .decode = decode_frame,
246 .capabilities = AV_CODEC_CAP_DR1,
249 #if CONFIG_IDF_DECODER
250 AVCodec ff_idf_decoder = {
252 .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
253 .type = AVMEDIA_TYPE_VIDEO,
254 .id = AV_CODEC_ID_IDF,
255 .priv_data_size = sizeof(XbinContext),
258 .decode = decode_frame,
259 .capabilities = AV_CODEC_CAP_DR1,