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;
66 if (!s->font_height) {
67 av_log(avctx, AV_LOG_ERROR, "invalid font height\n");
68 return AVERROR_INVALIDDATA;
75 if ((s->flags & BINTEXT_PALETTE)) {
76 for (i = 0; i < 16; i++) {
77 s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
81 for (i = 0; i < 16; i++)
82 s->palette[i] = 0xFF000000 | ff_cga_palette[i];
85 if ((s->flags & BINTEXT_FONT)) {
88 switch(s->font_height) {
90 av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
93 s->font = avpriv_cga_font;
96 s->font = avpriv_vga16_font;
100 if (avctx->width < FONT_WIDTH || avctx->height < s->font_height) {
101 av_log(avctx, AV_LOG_ERROR, "Resolution too small for font.\n");
102 return AVERROR_INVALIDDATA;
108 #define DEFAULT_BG_COLOR 0
109 av_unused static void hscroll(AVCodecContext *avctx)
111 XbinContext *s = avctx->priv_data;
112 if (s->y < avctx->height - s->font_height) {
113 s->y += s->font_height;
115 memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
116 (avctx->height - s->font_height)*s->frame->linesize[0]);
117 memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
118 DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
123 * Draw character to screen
125 static void draw_char(AVCodecContext *avctx, int c, int a)
127 XbinContext *s = avctx->priv_data;
128 if (s->y > avctx->height - s->font_height)
130 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
131 s->frame->linesize[0], s->font, s->font_height, c,
134 if (s->x > avctx->width - FONT_WIDTH) {
136 s->y += s->font_height;
140 static int decode_frame(AVCodecContext *avctx,
141 void *data, int *got_frame,
144 XbinContext *s = avctx->priv_data;
145 const uint8_t *buf = avpkt->data;
146 int buf_size = avpkt->size;
147 const uint8_t *buf_end = buf+buf_size;
150 if ((avctx->width / FONT_WIDTH) * (avctx->height / s->font_height) / 256 > buf_size)
151 return AVERROR_INVALIDDATA;
155 if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
157 s->frame->pict_type = AV_PICTURE_TYPE_I;
158 s->frame->palette_has_changed = 1;
159 memcpy(s->frame->data[1], s->palette, 16 * 4);
161 if (avctx->codec_id == AV_CODEC_ID_XBIN) {
162 while (buf + 2 < buf_end) {
164 int type = *buf >> 6;
165 int count = (*buf & 0x3F) + 1;
168 case 0: //no compression
169 for (i = 0; i < count && buf + 1 < buf_end; i++) {
170 draw_char(avctx, buf[0], buf[1]);
174 case 1: //character compression
176 for (i = 0; i < count && buf < buf_end; i++)
177 draw_char(avctx, c, *buf++);
179 case 2: //attribute compression
181 for (i = 0; i < count && buf < buf_end; i++)
182 draw_char(avctx, *buf++, a);
184 case 3: //character/attribute compression
187 for (i = 0; i < count && buf < buf_end; i++)
188 draw_char(avctx, c, a);
192 } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
193 while (buf + 2 < buf_end) {
194 if (AV_RL16(buf) == 1) {
196 if (buf + 6 > buf_end)
198 for (i = 0; i < buf[2]; i++)
199 draw_char(avctx, buf[4], buf[5]);
202 draw_char(avctx, buf[0], buf[1]);
207 while (buf + 1 < buf_end) {
208 draw_char(avctx, buf[0], buf[1]);
217 #if CONFIG_BINTEXT_DECODER
218 AVCodec ff_bintext_decoder = {
220 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
221 .type = AVMEDIA_TYPE_VIDEO,
222 .id = AV_CODEC_ID_BINTEXT,
223 .priv_data_size = sizeof(XbinContext),
225 .decode = decode_frame,
226 .capabilities = AV_CODEC_CAP_DR1,
229 #if CONFIG_XBIN_DECODER
230 AVCodec ff_xbin_decoder = {
232 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
233 .type = AVMEDIA_TYPE_VIDEO,
234 .id = AV_CODEC_ID_XBIN,
235 .priv_data_size = sizeof(XbinContext),
237 .decode = decode_frame,
238 .capabilities = AV_CODEC_CAP_DR1,
241 #if CONFIG_IDF_DECODER
242 AVCodec ff_idf_decoder = {
244 .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
245 .type = AVMEDIA_TYPE_VIDEO,
246 .id = AV_CODEC_ID_IDF,
247 .priv_data_size = sizeof(XbinContext),
249 .decode = decode_frame,
250 .capabilities = AV_CODEC_CAP_DR1,