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