]> git.sesse.net Git - ffmpeg/blob - libavcodec/bintext.c
Merge commit 'ad5bbc408637cffd4cc2ba990abef529cf5fa6a3'
[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
100     s->frame = av_frame_alloc();
101     if (!s->frame)
102         return AVERROR(ENOMEM);
103
104     return 0;
105 }
106
107 #define DEFAULT_BG_COLOR 0
108 av_unused static void hscroll(AVCodecContext *avctx)
109 {
110     XbinContext *s = avctx->priv_data;
111     if (s->y < avctx->height - s->font_height) {
112         s->y += s->font_height;
113     } else {
114         memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
115             (avctx->height - s->font_height)*s->frame->linesize[0]);
116         memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
117             DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
118     }
119 }
120
121 /**
122  * Draw character to screen
123  */
124 static void draw_char(AVCodecContext *avctx, int c, int a)
125 {
126     XbinContext *s = avctx->priv_data;
127     if (s->y > avctx->height - s->font_height)
128         return;
129     ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
130                     s->frame->linesize[0], s->font, s->font_height, c,
131                     a & 0x0F, a >> 4);
132     s->x += FONT_WIDTH;
133     if (s->x > avctx->width - FONT_WIDTH) {
134         s->x = 0;
135         s->y += s->font_height;
136     }
137 }
138
139 static int decode_frame(AVCodecContext *avctx,
140                             void *data, int *got_frame,
141                             AVPacket *avpkt)
142 {
143     XbinContext *s = avctx->priv_data;
144     const uint8_t *buf = avpkt->data;
145     int buf_size = avpkt->size;
146     const uint8_t *buf_end = buf+buf_size;
147     int ret;
148
149     s->x = s->y = 0;
150     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
151         return ret;
152     s->frame->pict_type           = AV_PICTURE_TYPE_I;
153     s->frame->palette_has_changed = 1;
154     memcpy(s->frame->data[1], s->palette, 16 * 4);
155
156     if (avctx->codec_id == AV_CODEC_ID_XBIN) {
157         while (buf + 2 < buf_end) {
158             int i,c,a;
159             int type  = *buf >> 6;
160             int count = (*buf & 0x3F) + 1;
161             buf++;
162             switch (type) {
163             case 0: //no compression
164                 for (i = 0; i < count && buf + 1 < buf_end; i++) {
165                     draw_char(avctx, buf[0], buf[1]);
166                     buf += 2;
167                 }
168                 break;
169             case 1: //character compression
170                 c = *buf++;
171                 for (i = 0; i < count && buf < buf_end; i++)
172                     draw_char(avctx, c, *buf++);
173                 break;
174             case 2: //attribute compression
175                 a = *buf++;
176                 for (i = 0; i < count && buf < buf_end; i++)
177                     draw_char(avctx, *buf++, a);
178                 break;
179             case 3: //character/attribute compression
180                 c = *buf++;
181                 a = *buf++;
182                 for (i = 0; i < count && buf < buf_end; i++)
183                     draw_char(avctx, c, a);
184                 break;
185             }
186         }
187     } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
188         while (buf + 2 < buf_end) {
189             if (AV_RL16(buf) == 1) {
190                int i;
191                if (buf + 6 > buf_end)
192                    break;
193                for (i = 0; i < buf[2]; i++)
194                    draw_char(avctx, buf[4], buf[5]);
195                buf += 6;
196             } else {
197                draw_char(avctx, buf[0], buf[1]);
198                buf += 2;
199             }
200         }
201     } else {
202         while (buf + 1 < buf_end) {
203             draw_char(avctx, buf[0], buf[1]);
204             buf += 2;
205         }
206     }
207
208     if ((ret = av_frame_ref(data, s->frame)) < 0)
209         return ret;
210     *got_frame      = 1;
211     return buf_size;
212 }
213
214 static av_cold int decode_end(AVCodecContext *avctx)
215 {
216     XbinContext *s = avctx->priv_data;
217
218     av_frame_free(&s->frame);
219
220     return 0;
221 }
222
223 #if CONFIG_BINTEXT_DECODER
224 AVCodec ff_bintext_decoder = {
225     .name           = "bintext",
226     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
227     .type           = AVMEDIA_TYPE_VIDEO,
228     .id             = AV_CODEC_ID_BINTEXT,
229     .priv_data_size = sizeof(XbinContext),
230     .init           = decode_init,
231     .close          = decode_end,
232     .decode         = decode_frame,
233     .capabilities   = AV_CODEC_CAP_DR1,
234 };
235 #endif
236 #if CONFIG_XBIN_DECODER
237 AVCodec ff_xbin_decoder = {
238     .name           = "xbin",
239     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
240     .type           = AVMEDIA_TYPE_VIDEO,
241     .id             = AV_CODEC_ID_XBIN,
242     .priv_data_size = sizeof(XbinContext),
243     .init           = decode_init,
244     .close          = decode_end,
245     .decode         = decode_frame,
246     .capabilities   = AV_CODEC_CAP_DR1,
247 };
248 #endif
249 #if CONFIG_IDF_DECODER
250 AVCodec ff_idf_decoder = {
251     .name           = "idf",
252     .long_name      = NULL_IF_CONFIG_SMALL("iCEDraw text"),
253     .type           = AVMEDIA_TYPE_VIDEO,
254     .id             = AV_CODEC_ID_IDF,
255     .priv_data_size = sizeof(XbinContext),
256     .init           = decode_init,
257     .close          = decode_end,
258     .decode         = decode_frame,
259     .capabilities   = AV_CODEC_CAP_DR1,
260 };
261 #endif