]> git.sesse.net Git - ffmpeg/blob - libavcodec/bintext.c
Merge remote-tracking branch 'qatar/master'
[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 "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 supported\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->x = s->y = 0;
135     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
136                             FF_BUFFER_HINTS_PRESERVE |
137                             FF_BUFFER_HINTS_REUSABLE;
138     if (avctx->reget_buffer(avctx, &s->frame)) {
139         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
140         return -1;
141     }
142     s->frame.pict_type           = FF_I_TYPE;
143     s->frame.palette_has_changed = 1;
144     memcpy(s->frame.data[1], s->palette, 16 * 4);
145
146     if (avctx->codec_id == CODEC_ID_XBIN) {
147         while (buf + 2 < buf_end) {
148             int i,c,a;
149             int type  = *buf >> 6;
150             int count = (*buf & 0x3F) + 1;
151             buf++;
152             switch (type) {
153             case 0: //no compression
154                 for (i = 0; i < count && buf + 1 < buf_end; i++) {
155                     draw_char(avctx, buf[0], buf[1]);
156                     buf += 2;
157                 }
158                 break;
159             case 1: //character compression
160                 c = *buf++;
161                 for (i = 0; i < count && buf < buf_end; i++)
162                     draw_char(avctx, c, *buf++);
163                 break;
164             case 2: //attribute compression
165                 a = *buf++;
166                 for (i = 0; i < count && buf < buf_end; i++)
167                     draw_char(avctx, *buf++, a);
168                 break;
169             case 3: //character/attribute compression
170                 c = *buf++;
171                 a = *buf++;
172                 for (i = 0; i < count && buf < buf_end; i++)
173                     draw_char(avctx, c, a);
174                 break;
175             }
176         }
177     } else if (avctx->codec_id == CODEC_ID_IDF) {
178         while (buf + 2 < buf_end) {
179             if (AV_RL16(buf) == 1) {
180                int i;
181                if (buf + 6 > buf_end)
182                    break;
183                for (i = 0; i < buf[2]; i++)
184                    draw_char(avctx, buf[4], buf[5]);
185                buf += 6;
186             } else {
187                draw_char(avctx, buf[0], buf[1]);
188                buf += 2;
189             }
190         }
191     } else {
192         while (buf + 1 < buf_end) {
193             draw_char(avctx, buf[0], buf[1]);
194             buf += 2;
195         }
196     }
197
198     *data_size = sizeof(AVFrame);
199     *(AVFrame*)data = s->frame;
200     return buf_size;
201 }
202
203 static av_cold int decode_end(AVCodecContext *avctx)
204 {
205     XbinContext *s = avctx->priv_data;
206
207     if (s->frame.data[0])
208         avctx->release_buffer(avctx, &s->frame);
209
210     return 0;
211 }
212
213 AVCodec ff_bintext_decoder = {
214     "bintext",
215     AVMEDIA_TYPE_VIDEO,
216     CODEC_ID_BINTEXT,
217     sizeof(XbinContext),
218     decode_init,
219     NULL,
220     decode_end,
221     decode_frame,
222     CODEC_CAP_DR1,
223     .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
224 };
225
226 AVCodec ff_xbin_decoder = {
227     "xbin",
228     AVMEDIA_TYPE_VIDEO,
229     CODEC_ID_XBIN,
230     sizeof(XbinContext),
231     decode_init,
232     NULL,
233     decode_end,
234     decode_frame,
235     CODEC_CAP_DR1,
236     .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
237 };
238
239 AVCodec ff_idf_decoder = {
240     "idf",
241     AVMEDIA_TYPE_VIDEO,
242     CODEC_ID_IDF,
243     sizeof(XbinContext),
244     decode_init,
245     NULL,
246     decode_end,
247     decode_frame,
248     CODEC_CAP_DR1,
249     .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
250 };