Skip to content

Commit

Permalink
Binary text decoder
Browse files Browse the repository at this point in the history
Decoder and demuxer for various binary text formats.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
  • Loading branch information
rouseabout authored and michaelni committed Sep 16, 2011
1 parent ea8de10 commit 07a70ca
Show file tree
Hide file tree
Showing 10 changed files with 689 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog
Expand Up @@ -54,6 +54,7 @@ easier to use. The changes are:
- Optional C++ Support (needed for libstagefright)
- H.264 Decoding on Android via Stagefright
- Prores decoder
- BIN/XBIN/ADF/IDF text file decoder


version 0.8:
Expand Down
4 changes: 4 additions & 0 deletions doc/general.texi
Expand Up @@ -51,6 +51,7 @@ library:
@tab Multimedia format used in games like Mad Dog McCree.
@item 3GPP AMR @tab X @tab X
@item Apple HTTP Live Streaming @tab @tab X
@item Artworx Data Format @tab @tab X
@item ASF @tab X @tab X
@item AVI @tab X @tab X
@item AVISynth @tab @tab X
Expand All @@ -60,6 +61,7 @@ library:
@tab Audio and video format used in some games by Beam Software.
@item Bethesda Softworks VID @tab @tab X
@tab Used in some games from Bethesda Softworks.
@item Binary text @tab @tab C
@item Bink @tab @tab X
@tab Multimedia format used by many games.
@item Bitmap Brothers JV @tab @tab X
Expand Down Expand Up @@ -106,6 +108,7 @@ library:
@item GXF @tab X @tab X
@tab General eXchange Format SMPTE 360M, used by Thomson Grass Valley
playout servers.
@item iCEDraw File @tab @tab X
@item id Quake II CIN video @tab @tab X
@item id RoQ @tab X @tab X
@tab Used in Quake III, Jedi Knight 2, other computer games.
Expand Down Expand Up @@ -267,6 +270,7 @@ library:
@tab Microsoft video container used in Xbox games.
@item xWMA @tab @tab X
@tab Microsoft audio container used by XAudio 2.
@item eXtended BINary text (XBIN) @tab @tab X
@item YUV4MPEG pipe @tab X @tab X
@item Psygnosis YOP @tab @tab X
@end multitable
Expand Down
3 changes: 3 additions & 0 deletions libavcodec/Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libavcodec/allcodecs.c
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions libavcodec/avcodec.h
Expand Up @@ -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

Expand Down
252 changes: 252 additions & 0 deletions 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"),
};
37 changes: 37 additions & 0 deletions 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 */

0 comments on commit 07a70ca

Please sign in to comment.