From 07a70caba7cb2fac69bf9541914d4bffce61d1ea Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Fri, 16 Sep 2011 04:50:26 +0200 Subject: Binary text decoder Decoder and demuxer for various binary text formats. Signed-off-by: Michael Niedermayer --- libavcodec/Makefile | 3 + libavcodec/allcodecs.c | 5 + libavcodec/avcodec.h | 3 + libavcodec/bintext.c | 252 +++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/bintext.h | 37 ++++++++ 5 files changed, 300 insertions(+) create mode 100644 libavcodec/bintext.c create mode 100644 libavcodec/bintext.h (limited to 'libavcodec') diff --git a/libavcodec/Makefile b/libavcodec/Makefile index fe1ff2a4dd..408317ebfc 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -96,6 +96,7 @@ OBJS-$(CONFIG_BFI_DECODER) += bfi.o OBJS-$(CONFIG_BINK_DECODER) += bink.o binkdsp.o OBJS-$(CONFIG_BINKAUDIO_DCT_DECODER) += binkaudio.o wma.o OBJS-$(CONFIG_BINKAUDIO_RDFT_DECODER) += binkaudio.o wma.o +OBJS-$(CONFIG_BINTEXT_DECODER) += bintext.o cga_data.o OBJS-$(CONFIG_BMP_DECODER) += bmp.o msrledec.o OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o OBJS-$(CONFIG_C93_DECODER) += c93.o @@ -188,6 +189,7 @@ OBJS-$(CONFIG_H264_VAAPI_HWACCEL) += vaapi_h264.o OBJS-$(CONFIG_HUFFYUV_DECODER) += huffyuv.o OBJS-$(CONFIG_HUFFYUV_ENCODER) += huffyuv.o OBJS-$(CONFIG_IDCIN_DECODER) += idcinvideo.o +OBJS-$(CONFIG_IDF_DECODER) += bintext.o cga_data.o OBJS-$(CONFIG_IFF_BYTERUN1_DECODER) += iff.o OBJS-$(CONFIG_IFF_ILBM_DECODER) += iff.o OBJS-$(CONFIG_IMC_DECODER) += imc.o @@ -440,6 +442,7 @@ OBJS-$(CONFIG_WS_SND1_DECODER) += ws-snd1.o OBJS-$(CONFIG_XAN_DPCM_DECODER) += dpcm.o OBJS-$(CONFIG_XAN_WC3_DECODER) += xan.o OBJS-$(CONFIG_XAN_WC4_DECODER) += xxan.o +OBJS-$(CONFIG_XBIN_DECODER) += bintext.o cga_data.o OBJS-$(CONFIG_XL_DECODER) += xl.o OBJS-$(CONFIG_XSUB_DECODER) += xsubdec.o OBJS-$(CONFIG_XSUB_ENCODER) += xsubenc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 8734f5d974..309d762f81 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -393,6 +393,11 @@ void avcodec_register_all(void) REGISTER_ENCODER (LIBXAVS, libxavs); REGISTER_ENCODER (LIBXVID, libxvid); + /* text */ + REGISTER_DECODER (BINTEXT, bintext); + REGISTER_DECODER (XBIN, xbin); + REGISTER_DECODER (IDF, idf); + /* parsers */ REGISTER_PARSER (AAC, aac); REGISTER_PARSER (AAC_LATM, aac_latm); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 4ce084c574..f8f4f69ef6 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -361,6 +361,9 @@ enum CodecID { /* other specific kind of codecs (generally used for attachments) */ CODEC_ID_FIRST_UNKNOWN = 0x18000, ///< A dummy ID pointing at the start of various fake codecs. CODEC_ID_TTF= 0x18000, + CODEC_ID_BINTEXT, + CODEC_ID_XBIN, + CODEC_ID_IDF, CODEC_ID_PROBE= 0x19000, ///< codec_id is not known (like CODEC_ID_NONE) but lavf should attempt to identify it diff --git a/libavcodec/bintext.c b/libavcodec/bintext.c new file mode 100644 index 0000000000..0726d4689d --- /dev/null +++ b/libavcodec/bintext.c @@ -0,0 +1,252 @@ +/* + * Binary text decoder + * eXtended BINary text (XBIN) decoder + * iCEDraw File decoder + * Copyright (c) 2010 Peter Ross (pross@xvid.org) + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file libavcodec/xbin.c + * Binary text decoder + * eXtended BINary text (XBIN) decoder + * iCEDraw File decoder + */ + +#include "libavutil/intreadwrite.h" +#include "avcodec.h" +#include "cga_data.h" +#include "bintext.h" + +typedef struct XbinContext { + AVFrame frame; + int palette[16]; + int flags; + int font_height; + const uint8_t *font; + int x, y; +} XbinContext; + +static av_cold int decode_init(AVCodecContext *avctx) +{ + XbinContext *s = avctx->priv_data; + uint8_t *p; + int i; + + avctx->pix_fmt = PIX_FMT_PAL8; + s->frame.reference = 1; + if (avctx->get_buffer(avctx, &s->frame)) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + p = avctx->extradata; + if (p) { + s->font_height = p[0]; + s->flags = p[1]; + p += 2; + } else { + s->font_height = 8; + s->flags = 0; + } + + if ((s->flags & BINTEXT_PALETTE)) { + for (i = 0; i < 16; i++) { + s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2); + p += 3; + } + } else { + for (i = 0; i < 16; i++) + s->palette[i] = 0xFF000000 | ff_cga_palette[i]; + } + + if ((s->flags & BINTEXT_FONT)) { + s->font = p; + } else { + switch(s->font_height) { + default: + av_log(avctx, AV_LOG_WARNING, "font height %i not support\n", s->font_height); + s->font_height = 8; + case 8: + s->font = ff_cga_font; + break; + case 16: + s->font = ff_vga16_font; + break; + } + } + + return 0; +} + +#define DEFAULT_BG_COLOR 0 +static void hscroll(AVCodecContext *avctx) +{ + XbinContext *s = avctx->priv_data; + if (s->y < avctx->height - s->font_height) { + s->y += s->font_height; + } else { + memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0], + (avctx->height - s->font_height)*s->frame.linesize[0]); + memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0], + DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]); + } +} + +#define FONT_WIDTH 8 + +/** + * Draw character to screen + */ +static void draw_char(AVCodecContext *avctx, int c, int a) +{ + XbinContext *s = avctx->priv_data; + if (s->y > avctx->height - s->font_height) + return; + ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x, + s->frame.linesize[0], s->font, s->font_height, c, + a & 0x0F, a >> 4); + s->x += FONT_WIDTH; + if (s->x >= avctx->width) { + s->x = 0; + s->y += s->font_height; + } +} + +static int decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt) +{ + XbinContext *s = avctx->priv_data; + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + const uint8_t *buf_end = buf+buf_size; + + if (avctx->reget_buffer(avctx, &s->frame)) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + s->frame.pict_type = FF_I_TYPE; + s->frame.palette_has_changed = 1; + memcpy(s->frame.data[1], s->palette, 16 * 4); + + if (avctx->codec_id == CODEC_ID_XBIN) { + while (buf + 2 < buf_end) { + int i,c,a; + int type = *buf >> 6; + int count = (*buf & 0x3F) + 1; + buf++; + switch (type) { + case 0: //no compression + for (i = 0; i < count && buf + 1 < buf_end; i++) { + draw_char(avctx, buf[0], buf[1]); + buf += 2; + } + break; + case 1: //character compression + c = *buf++; + for (i = 0; i < count && buf < buf_end; i++) + draw_char(avctx, c, *buf++); + break; + case 2: //attribute compression + a = *buf++; + for (i = 0; i < count && buf < buf_end; i++) + draw_char(avctx, *buf++, a); + break; + case 3: //character/attribute compression + c = *buf++; + a = *buf++; + for (i = 0; i < count && buf < buf_end; i++) + draw_char(avctx, c, a); + break; + } + } + } else if (avctx->codec_id == CODEC_ID_IDF) { + while (buf + 2 < buf_end) { + if (AV_RL16(buf) == 1) { + int i; + if (buf + 6 > buf_end) + break; + for (i = 0; i < buf[2]; i++) + draw_char(avctx, buf[4], buf[5]); + buf += 6; + } else { + draw_char(avctx, buf[0], buf[1]); + buf += 2; + } + } + } else { + while (buf + 1 < buf_end) { + draw_char(avctx, buf[0], buf[1]); + buf += 2; + } + } + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = s->frame; + return buf_size; +} + +static av_cold int decode_end(AVCodecContext *avctx) +{ + XbinContext *s = avctx->priv_data; + + if (s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); + + return 0; +} + +AVCodec ff_bintext_decoder = { + "xbin", + AVMEDIA_TYPE_VIDEO, + CODEC_ID_BINTEXT, + sizeof(XbinContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("Binary text"), +}; + +AVCodec ff_xbin_decoder = { + "xbin", + AVMEDIA_TYPE_VIDEO, + CODEC_ID_XBIN, + sizeof(XbinContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"), +}; + +AVCodec ff_idf_decoder = { + "idf", + AVMEDIA_TYPE_VIDEO, + CODEC_ID_IDF, + sizeof(XbinContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"), +}; diff --git a/libavcodec/bintext.h b/libavcodec/bintext.h new file mode 100644 index 0000000000..ea834a00e3 --- /dev/null +++ b/libavcodec/bintext.h @@ -0,0 +1,37 @@ +/* + * Binary text decoder + * Copyright (c) 2010 Peter Ross (pross@xvid.org) + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Binary text decoder + */ + +#ifndef AVCODEC_BINTEXT_H +#define AVCODEC_BINTEXT_H + +/* flag values passed between avformat and avcodec; + * while these are identical to the XBIN flags, they are are also used + * for the BINTEXT and IDF decoders. + */ +#define BINTEXT_PALETTE 0x1 +#define BINTEXT_FONT 0x2 + +#endif /* AVCODEC_BINTEXT_H */ -- cgit v1.2.3