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