]> git.sesse.net Git - ffmpeg/blob - libavcodec/bintext.c
alacdec: fix packed sample output with 5.1
[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         if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
58                                      + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
59             av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
60             return AVERROR_INVALIDDATA;
61         }
62     } else {
63         s->font_height = 8;
64         s->flags = 0;
65     }
66
67     if ((s->flags & BINTEXT_PALETTE)) {
68         for (i = 0; i < 16; i++) {
69             s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
70             p += 3;
71         }
72     } else {
73         for (i = 0; i < 16; i++)
74             s->palette[i] = 0xFF000000 | ff_cga_palette[i];
75     }
76
77     if ((s->flags & BINTEXT_FONT)) {
78         s->font = p;
79     } else {
80         switch(s->font_height) {
81         default:
82             av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
83             s->font_height = 8;
84         case 8:
85             s->font = ff_cga_font;
86             break;
87         case 16:
88             s->font = ff_vga16_font;
89             break;
90         }
91     }
92
93     return 0;
94 }
95
96 #define DEFAULT_BG_COLOR 0
97 static void hscroll(AVCodecContext *avctx)
98 {
99     XbinContext *s = avctx->priv_data;
100     if (s->y < avctx->height - s->font_height) {
101         s->y += s->font_height;
102     } else {
103         memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
104             (avctx->height - s->font_height)*s->frame.linesize[0]);
105         memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
106             DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
107     }
108 }
109
110 #define FONT_WIDTH 8
111
112 /**
113  * Draw character to screen
114  */
115 static void draw_char(AVCodecContext *avctx, int c, int a)
116 {
117     XbinContext *s = avctx->priv_data;
118     if (s->y > avctx->height - s->font_height)
119         return;
120     ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
121                     s->frame.linesize[0], s->font, s->font_height, c,
122                     a & 0x0F, a >> 4);
123     s->x += FONT_WIDTH;
124     if (s->x > avctx->width - FONT_WIDTH) {
125         s->x = 0;
126         s->y += s->font_height;
127     }
128 }
129
130 static int decode_frame(AVCodecContext *avctx,
131                             void *data, int *data_size,
132                             AVPacket *avpkt)
133 {
134     XbinContext *s = avctx->priv_data;
135     const uint8_t *buf = avpkt->data;
136     int buf_size = avpkt->size;
137     const uint8_t *buf_end = buf+buf_size;
138
139     s->x = s->y = 0;
140     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
141                             FF_BUFFER_HINTS_PRESERVE |
142                             FF_BUFFER_HINTS_REUSABLE;
143     if (avctx->reget_buffer(avctx, &s->frame)) {
144         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
145         return -1;
146     }
147     s->frame.pict_type           = AV_PICTURE_TYPE_I;
148     s->frame.palette_has_changed = 1;
149     memcpy(s->frame.data[1], s->palette, 16 * 4);
150
151     if (avctx->codec_id == CODEC_ID_XBIN) {
152         while (buf + 2 < buf_end) {
153             int i,c,a;
154             int type  = *buf >> 6;
155             int count = (*buf & 0x3F) + 1;
156             buf++;
157             switch (type) {
158             case 0: //no compression
159                 for (i = 0; i < count && buf + 1 < buf_end; i++) {
160                     draw_char(avctx, buf[0], buf[1]);
161                     buf += 2;
162                 }
163                 break;
164             case 1: //character compression
165                 c = *buf++;
166                 for (i = 0; i < count && buf < buf_end; i++)
167                     draw_char(avctx, c, *buf++);
168                 break;
169             case 2: //attribute compression
170                 a = *buf++;
171                 for (i = 0; i < count && buf < buf_end; i++)
172                     draw_char(avctx, *buf++, a);
173                 break;
174             case 3: //character/attribute compression
175                 c = *buf++;
176                 a = *buf++;
177                 for (i = 0; i < count && buf < buf_end; i++)
178                     draw_char(avctx, c, a);
179                 break;
180             }
181         }
182     } else if (avctx->codec_id == CODEC_ID_IDF) {
183         while (buf + 2 < buf_end) {
184             if (AV_RL16(buf) == 1) {
185                int i;
186                if (buf + 6 > buf_end)
187                    break;
188                for (i = 0; i < buf[2]; i++)
189                    draw_char(avctx, buf[4], buf[5]);
190                buf += 6;
191             } else {
192                draw_char(avctx, buf[0], buf[1]);
193                buf += 2;
194             }
195         }
196     } else {
197         while (buf + 1 < buf_end) {
198             draw_char(avctx, buf[0], buf[1]);
199             buf += 2;
200         }
201     }
202
203     *data_size = sizeof(AVFrame);
204     *(AVFrame*)data = s->frame;
205     return buf_size;
206 }
207
208 static av_cold int decode_end(AVCodecContext *avctx)
209 {
210     XbinContext *s = avctx->priv_data;
211
212     if (s->frame.data[0])
213         avctx->release_buffer(avctx, &s->frame);
214
215     return 0;
216 }
217
218 #if CONFIG_BINTEXT_DECODER
219 AVCodec ff_bintext_decoder = {
220     .name           = "bintext",
221     .type           = AVMEDIA_TYPE_VIDEO,
222     .id             = CODEC_ID_BINTEXT,
223     .priv_data_size = sizeof(XbinContext),
224     .init           = decode_init,
225     .close          = decode_end,
226     .decode         = decode_frame,
227     .capabilities   = CODEC_CAP_DR1,
228     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
229 };
230 #endif
231 #if CONFIG_XBIN_DECODER
232 AVCodec ff_xbin_decoder = {
233     .name           = "xbin",
234     .type           = AVMEDIA_TYPE_VIDEO,
235     .id             = CODEC_ID_XBIN,
236     .priv_data_size = sizeof(XbinContext),
237     .init           = decode_init,
238     .close          = decode_end,
239     .decode         = decode_frame,
240     .capabilities   = CODEC_CAP_DR1,
241     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
242 };
243 #endif
244 #if CONFIG_IDF_DECODER
245 AVCodec ff_idf_decoder = {
246     .name           = "idf",
247     .type           = AVMEDIA_TYPE_VIDEO,
248     .id             = CODEC_ID_IDF,
249     .priv_data_size = sizeof(XbinContext),
250     .init           = decode_init,
251     .close          = decode_end,
252     .decode         = decode_frame,
253     .capabilities   = CODEC_CAP_DR1,
254     .long_name      = NULL_IF_CONFIG_SMALL("iCEDraw text"),
255 };
256 #endif