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"
37 typedef struct XbinContext {
46 static av_cold int decode_init(AVCodecContext *avctx)
48 XbinContext *s = avctx->priv_data;
52 avctx->pix_fmt = AV_PIX_FMT_PAL8;
55 s->font_height = p[0];
58 if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
59 + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
60 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
61 return AVERROR_INVALIDDATA;
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);
74 for (i = 0; i < 16; i++)
75 s->palette[i] = 0xFF000000 | ff_cga_palette[i];
78 if ((s->flags & BINTEXT_FONT)) {
81 switch(s->font_height) {
83 av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
86 s->font = avpriv_cga_font;
89 s->font = avpriv_vga16_font;
97 #define DEFAULT_BG_COLOR 0
98 av_unused static void hscroll(AVCodecContext *avctx)
100 XbinContext *s = avctx->priv_data;
101 if (s->y < avctx->height - s->font_height) {
102 s->y += s->font_height;
104 memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
105 (avctx->height - s->font_height)*s->frame.linesize[0]);
106 memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
107 DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
114 * Draw character to screen
116 static void draw_char(AVCodecContext *avctx, int c, int a)
118 XbinContext *s = avctx->priv_data;
119 if (s->y > avctx->height - s->font_height)
121 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
122 s->frame.linesize[0], s->font, s->font_height, c,
125 if (s->x > avctx->width - FONT_WIDTH) {
127 s->y += s->font_height;
131 static int decode_frame(AVCodecContext *avctx,
132 void *data, int *got_frame,
135 XbinContext *s = avctx->priv_data;
136 const uint8_t *buf = avpkt->data;
137 int buf_size = avpkt->size;
138 const uint8_t *buf_end = buf+buf_size;
141 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
142 FF_BUFFER_HINTS_PRESERVE |
143 FF_BUFFER_HINTS_REUSABLE;
144 if (avctx->reget_buffer(avctx, &s->frame)) {
145 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
148 s->frame.pict_type = AV_PICTURE_TYPE_I;
149 s->frame.palette_has_changed = 1;
150 memcpy(s->frame.data[1], s->palette, 16 * 4);
152 if (avctx->codec_id == AV_CODEC_ID_XBIN) {
153 while (buf + 2 < buf_end) {
155 int type = *buf >> 6;
156 int count = (*buf & 0x3F) + 1;
159 case 0: //no compression
160 for (i = 0; i < count && buf + 1 < buf_end; i++) {
161 draw_char(avctx, buf[0], buf[1]);
165 case 1: //character compression
167 for (i = 0; i < count && buf < buf_end; i++)
168 draw_char(avctx, c, *buf++);
170 case 2: //attribute compression
172 for (i = 0; i < count && buf < buf_end; i++)
173 draw_char(avctx, *buf++, a);
175 case 3: //character/attribute compression
178 for (i = 0; i < count && buf < buf_end; i++)
179 draw_char(avctx, c, a);
183 } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
184 while (buf + 2 < buf_end) {
185 if (AV_RL16(buf) == 1) {
187 if (buf + 6 > buf_end)
189 for (i = 0; i < buf[2]; i++)
190 draw_char(avctx, buf[4], buf[5]);
193 draw_char(avctx, buf[0], buf[1]);
198 while (buf + 1 < buf_end) {
199 draw_char(avctx, buf[0], buf[1]);
205 *(AVFrame*)data = s->frame;
209 static av_cold int decode_end(AVCodecContext *avctx)
211 XbinContext *s = avctx->priv_data;
213 if (s->frame.data[0])
214 avctx->release_buffer(avctx, &s->frame);
219 #if CONFIG_BINTEXT_DECODER
220 AVCodec ff_bintext_decoder = {
222 .type = AVMEDIA_TYPE_VIDEO,
223 .id = AV_CODEC_ID_BINTEXT,
224 .priv_data_size = sizeof(XbinContext),
227 .decode = decode_frame,
228 .capabilities = CODEC_CAP_DR1,
229 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
232 #if CONFIG_XBIN_DECODER
233 AVCodec ff_xbin_decoder = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .id = AV_CODEC_ID_XBIN,
237 .priv_data_size = sizeof(XbinContext),
240 .decode = decode_frame,
241 .capabilities = CODEC_CAP_DR1,
242 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
245 #if CONFIG_IDF_DECODER
246 AVCodec ff_idf_decoder = {
248 .type = AVMEDIA_TYPE_VIDEO,
249 .id = AV_CODEC_ID_IDF,
250 .priv_data_size = sizeof(XbinContext),
253 .decode = decode_frame,
254 .capabilities = CODEC_CAP_DR1,
255 .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),