]> git.sesse.net Git - ffmpeg/blob - libavcodec/bintext.c
Check for invalid/corrupted bitstream in sun raster decoder.
[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 libavcodec/xbin.c
26  * Binary text decoder
27  * eXtended BINary text (XBIN) decoder
28  * iCEDraw File decoder
29  */
30
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "cga_data.h"
34 #include "bintext.h"
35
36 typedef struct XbinContext {
37     AVFrame frame;
38     int palette[16];
39     int flags;
40     int font_height;
41     const uint8_t *font;
42     int x, y;
43 } XbinContext;
44
45 static av_cold int decode_init(AVCodecContext *avctx)
46 {
47     XbinContext *s = avctx->priv_data;
48     uint8_t *p;
49     int i;
50
51     avctx->pix_fmt = PIX_FMT_PAL8;
52     p = avctx->extradata;
53     if (p) {
54         s->font_height = p[0];
55         s->flags = p[1];
56         p += 2;
57     } else {
58         s->font_height = 8;
59         s->flags = 0;
60     }
61
62     if ((s->flags & BINTEXT_PALETTE)) {
63         for (i = 0; i < 16; i++) {
64             s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2);
65             p += 3;
66         }
67     } else {
68         for (i = 0; i < 16; i++)
69             s->palette[i] = 0xFF000000 | ff_cga_palette[i];
70     }
71
72     if ((s->flags & BINTEXT_FONT)) {
73         s->font = p;
74     } else {
75         switch(s->font_height) {
76         default:
77             av_log(avctx, AV_LOG_WARNING, "font height %i not support\n", s->font_height);
78             s->font_height = 8;
79         case 8:
80             s->font = ff_cga_font;
81             break;
82         case 16:
83             s->font = ff_vga16_font;
84             break;
85         }
86     }
87
88     return 0;
89 }
90
91 #define DEFAULT_BG_COLOR 0
92 static void hscroll(AVCodecContext *avctx)
93 {
94     XbinContext *s = avctx->priv_data;
95     if (s->y < avctx->height - s->font_height) {
96         s->y += s->font_height;
97     } else {
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]);
102     }
103 }
104
105 #define FONT_WIDTH 8
106
107 /**
108  * Draw character to screen
109  */
110 static void draw_char(AVCodecContext *avctx, int c, int a)
111 {
112     XbinContext *s = avctx->priv_data;
113     if (s->y > avctx->height - s->font_height)
114         return;
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,
117                     a & 0x0F, a >> 4);
118     s->x += FONT_WIDTH;
119     if (s->x > avctx->width - FONT_WIDTH) {
120         s->x = 0;
121         s->y += s->font_height;
122     }
123 }
124
125 static int decode_frame(AVCodecContext *avctx,
126                             void *data, int *data_size,
127                             AVPacket *avpkt)
128 {
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;
133
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");
139         return -1;
140     }
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);
144
145     if (avctx->codec_id == CODEC_ID_XBIN) {
146         while (buf + 2 < buf_end) {
147             int i,c,a;
148             int type  = *buf >> 6;
149             int count = (*buf & 0x3F) + 1;
150             buf++;
151             switch (type) {
152             case 0: //no compression
153                 for (i = 0; i < count && buf + 1 < buf_end; i++) {
154                     draw_char(avctx, buf[0], buf[1]);
155                     buf += 2;
156                 }
157                 break;
158             case 1: //character compression
159                 c = *buf++;
160                 for (i = 0; i < count && buf < buf_end; i++)
161                     draw_char(avctx, c, *buf++);
162                 break;
163             case 2: //attribute compression
164                 a = *buf++;
165                 for (i = 0; i < count && buf < buf_end; i++)
166                     draw_char(avctx, *buf++, a);
167                 break;
168             case 3: //character/attribute compression
169                 c = *buf++;
170                 a = *buf++;
171                 for (i = 0; i < count && buf < buf_end; i++)
172                     draw_char(avctx, c, a);
173                 break;
174             }
175         }
176     } else if (avctx->codec_id == CODEC_ID_IDF) {
177         while (buf + 2 < buf_end) {
178             if (AV_RL16(buf) == 1) {
179                int i;
180                if (buf + 6 > buf_end)
181                    break;
182                for (i = 0; i < buf[2]; i++)
183                    draw_char(avctx, buf[4], buf[5]);
184                buf += 6;
185             } else {
186                draw_char(avctx, buf[0], buf[1]);
187                buf += 2;
188             }
189         }
190     } else {
191         while (buf + 1 < buf_end) {
192             draw_char(avctx, buf[0], buf[1]);
193             buf += 2;
194         }
195     }
196
197     *data_size = sizeof(AVFrame);
198     *(AVFrame*)data = s->frame;
199     return buf_size;
200 }
201
202 static av_cold int decode_end(AVCodecContext *avctx)
203 {
204     XbinContext *s = avctx->priv_data;
205
206     if (s->frame.data[0])
207         avctx->release_buffer(avctx, &s->frame);
208
209     return 0;
210 }
211
212 AVCodec ff_bintext_decoder = {
213     "bintext",
214     AVMEDIA_TYPE_VIDEO,
215     CODEC_ID_BINTEXT,
216     sizeof(XbinContext),
217     decode_init,
218     NULL,
219     decode_end,
220     decode_frame,
221     CODEC_CAP_DR1,
222     .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
223 };
224
225 AVCodec ff_xbin_decoder = {
226     "xbin",
227     AVMEDIA_TYPE_VIDEO,
228     CODEC_ID_XBIN,
229     sizeof(XbinContext),
230     decode_init,
231     NULL,
232     decode_end,
233     decode_frame,
234     CODEC_CAP_DR1,
235     .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
236 };
237
238 AVCodec ff_idf_decoder = {
239     "idf",
240     AVMEDIA_TYPE_VIDEO,
241     CODEC_ID_IDF,
242     sizeof(XbinContext),
243     decode_init,
244     NULL,
245     decode_end,
246     decode_frame,
247     CODEC_CAP_DR1,
248     .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
249 };