00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/xga_font_data.h"
00033 #include "avcodec.h"
00034 #include "cga_data.h"
00035 #include "bintext.h"
00036
00037 typedef struct XbinContext {
00038 AVFrame frame;
00039 int palette[16];
00040 int flags;
00041 int font_height;
00042 const uint8_t *font;
00043 int x, y;
00044 } XbinContext;
00045
00046 static av_cold int decode_init(AVCodecContext *avctx)
00047 {
00048 XbinContext *s = avctx->priv_data;
00049 uint8_t *p;
00050 int i;
00051
00052 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00053 p = avctx->extradata;
00054 if (p) {
00055 s->font_height = p[0];
00056 s->flags = p[1];
00057 p += 2;
00058 if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
00059 + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
00060 av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
00061 return AVERROR_INVALIDDATA;
00062 }
00063 } else {
00064 s->font_height = 8;
00065 s->flags = 0;
00066 }
00067
00068 if ((s->flags & BINTEXT_PALETTE)) {
00069 for (i = 0; i < 16; i++) {
00070 s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
00071 p += 3;
00072 }
00073 } else {
00074 for (i = 0; i < 16; i++)
00075 s->palette[i] = 0xFF000000 | ff_cga_palette[i];
00076 }
00077
00078 if ((s->flags & BINTEXT_FONT)) {
00079 s->font = p;
00080 } else {
00081 switch(s->font_height) {
00082 default:
00083 av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
00084 s->font_height = 8;
00085 case 8:
00086 s->font = avpriv_cga_font;
00087 break;
00088 case 16:
00089 s->font = avpriv_vga16_font;
00090 break;
00091 }
00092 }
00093
00094 return 0;
00095 }
00096
00097 #define DEFAULT_BG_COLOR 0
00098 av_unused static void hscroll(AVCodecContext *avctx)
00099 {
00100 XbinContext *s = avctx->priv_data;
00101 if (s->y < avctx->height - s->font_height) {
00102 s->y += s->font_height;
00103 } else {
00104 memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
00105 (avctx->height - s->font_height)*s->frame.linesize[0]);
00106 memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
00107 DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
00108 }
00109 }
00110
00111 #define FONT_WIDTH 8
00112
00116 static void draw_char(AVCodecContext *avctx, int c, int a)
00117 {
00118 XbinContext *s = avctx->priv_data;
00119 if (s->y > avctx->height - s->font_height)
00120 return;
00121 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
00122 s->frame.linesize[0], s->font, s->font_height, c,
00123 a & 0x0F, a >> 4);
00124 s->x += FONT_WIDTH;
00125 if (s->x > avctx->width - FONT_WIDTH) {
00126 s->x = 0;
00127 s->y += s->font_height;
00128 }
00129 }
00130
00131 static int decode_frame(AVCodecContext *avctx,
00132 void *data, int *got_frame,
00133 AVPacket *avpkt)
00134 {
00135 XbinContext *s = avctx->priv_data;
00136 const uint8_t *buf = avpkt->data;
00137 int buf_size = avpkt->size;
00138 const uint8_t *buf_end = buf+buf_size;
00139
00140 s->x = s->y = 0;
00141 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00142 FF_BUFFER_HINTS_PRESERVE |
00143 FF_BUFFER_HINTS_REUSABLE;
00144 if (avctx->reget_buffer(avctx, &s->frame)) {
00145 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00146 return -1;
00147 }
00148 s->frame.pict_type = AV_PICTURE_TYPE_I;
00149 s->frame.palette_has_changed = 1;
00150 memcpy(s->frame.data[1], s->palette, 16 * 4);
00151
00152 if (avctx->codec_id == AV_CODEC_ID_XBIN) {
00153 while (buf + 2 < buf_end) {
00154 int i,c,a;
00155 int type = *buf >> 6;
00156 int count = (*buf & 0x3F) + 1;
00157 buf++;
00158 switch (type) {
00159 case 0:
00160 for (i = 0; i < count && buf + 1 < buf_end; i++) {
00161 draw_char(avctx, buf[0], buf[1]);
00162 buf += 2;
00163 }
00164 break;
00165 case 1:
00166 c = *buf++;
00167 for (i = 0; i < count && buf < buf_end; i++)
00168 draw_char(avctx, c, *buf++);
00169 break;
00170 case 2:
00171 a = *buf++;
00172 for (i = 0; i < count && buf < buf_end; i++)
00173 draw_char(avctx, *buf++, a);
00174 break;
00175 case 3:
00176 c = *buf++;
00177 a = *buf++;
00178 for (i = 0; i < count && buf < buf_end; i++)
00179 draw_char(avctx, c, a);
00180 break;
00181 }
00182 }
00183 } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
00184 while (buf + 2 < buf_end) {
00185 if (AV_RL16(buf) == 1) {
00186 int i;
00187 if (buf + 6 > buf_end)
00188 break;
00189 for (i = 0; i < buf[2]; i++)
00190 draw_char(avctx, buf[4], buf[5]);
00191 buf += 6;
00192 } else {
00193 draw_char(avctx, buf[0], buf[1]);
00194 buf += 2;
00195 }
00196 }
00197 } else {
00198 while (buf + 1 < buf_end) {
00199 draw_char(avctx, buf[0], buf[1]);
00200 buf += 2;
00201 }
00202 }
00203
00204 *got_frame = 1;
00205 *(AVFrame*)data = s->frame;
00206 return buf_size;
00207 }
00208
00209 static av_cold int decode_end(AVCodecContext *avctx)
00210 {
00211 XbinContext *s = avctx->priv_data;
00212
00213 if (s->frame.data[0])
00214 avctx->release_buffer(avctx, &s->frame);
00215
00216 return 0;
00217 }
00218
00219 #if CONFIG_BINTEXT_DECODER
00220 AVCodec ff_bintext_decoder = {
00221 .name = "bintext",
00222 .type = AVMEDIA_TYPE_VIDEO,
00223 .id = AV_CODEC_ID_BINTEXT,
00224 .priv_data_size = sizeof(XbinContext),
00225 .init = decode_init,
00226 .close = decode_end,
00227 .decode = decode_frame,
00228 .capabilities = CODEC_CAP_DR1,
00229 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
00230 };
00231 #endif
00232 #if CONFIG_XBIN_DECODER
00233 AVCodec ff_xbin_decoder = {
00234 .name = "xbin",
00235 .type = AVMEDIA_TYPE_VIDEO,
00236 .id = AV_CODEC_ID_XBIN,
00237 .priv_data_size = sizeof(XbinContext),
00238 .init = decode_init,
00239 .close = decode_end,
00240 .decode = decode_frame,
00241 .capabilities = CODEC_CAP_DR1,
00242 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
00243 };
00244 #endif
00245 #if CONFIG_IDF_DECODER
00246 AVCodec ff_idf_decoder = {
00247 .name = "idf",
00248 .type = AVMEDIA_TYPE_VIDEO,
00249 .id = AV_CODEC_ID_IDF,
00250 .priv_data_size = sizeof(XbinContext),
00251 .init = decode_init,
00252 .close = decode_end,
00253 .decode = decode_frame,
00254 .capabilities = CODEC_CAP_DR1,
00255 .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
00256 };
00257 #endif