From 07a70caba7cb2fac69bf9541914d4bffce61d1ea Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Fri, 16 Sep 2011 04:50:26 +0200 Subject: [PATCH] Binary text decoder Decoder and demuxer for various binary text formats. Signed-off-by: Michael Niedermayer --- Changelog | 1 + doc/general.texi | 4 + libavcodec/Makefile | 3 + libavcodec/allcodecs.c | 5 + libavcodec/avcodec.h | 3 + libavcodec/bintext.c | 252 ++++++++++++++++++++++++++ libavcodec/bintext.h | 37 ++++ libavformat/Makefile | 4 + libavformat/allformats.c | 4 + libavformat/bintext.c | 376 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 689 insertions(+) create mode 100644 libavcodec/bintext.c create mode 100644 libavcodec/bintext.h create mode 100644 libavformat/bintext.c diff --git a/Changelog b/Changelog index 9a7732aacc..d3f3f6ef0d 100644 --- a/Changelog +++ b/Changelog @@ -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: diff --git a/doc/general.texi b/doc/general.texi index 9346a873df..c89f6e800c 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -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 @@ -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 @@ -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. @@ -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 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 */ diff --git a/libavformat/Makefile b/libavformat/Makefile index 3ba4b5106d..635352961b 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -21,6 +21,7 @@ OBJS-$(CONFIG_A64_MUXER) += a64.o OBJS-$(CONFIG_AAC_DEMUXER) += aacdec.o rawdec.o OBJS-$(CONFIG_AC3_DEMUXER) += ac3dec.o rawdec.o OBJS-$(CONFIG_AC3_MUXER) += rawenc.o +OBJS-$(CONFIG_ADF_DEMUXER) += bintext.o sauce.o OBJS-$(CONFIG_ADTS_MUXER) += adtsenc.o OBJS-$(CONFIG_AEA_DEMUXER) += aea.o pcm.o OBJS-$(CONFIG_AIFF_DEMUXER) += aiffdec.o riff.o pcm.o isom.o @@ -46,6 +47,7 @@ OBJS-$(CONFIG_AVS_DEMUXER) += avs.o vocdec.o voc.o OBJS-$(CONFIG_BETHSOFTVID_DEMUXER) += bethsoftvid.o OBJS-$(CONFIG_BFI_DEMUXER) += bfi.o OBJS-$(CONFIG_BINK_DEMUXER) += bink.o +OBJS-$(CONFIG_BINTEXT_DEMUXER) += bintext.o sauce.o OBJS-$(CONFIG_C93_DEMUXER) += c93.o vocdec.o voc.o OBJS-$(CONFIG_CAF_DEMUXER) += cafdec.o caf.o mov.o riff.o isom.o OBJS-$(CONFIG_CAF_MUXER) += cafenc.o caf.o riff.o isom.o @@ -100,6 +102,7 @@ OBJS-$(CONFIG_H263_MUXER) += rawenc.o OBJS-$(CONFIG_H264_DEMUXER) += h264dec.o rawdec.o OBJS-$(CONFIG_H264_MUXER) += rawenc.o OBJS-$(CONFIG_IDCIN_DEMUXER) += idcin.o +OBJS-$(CONFIG_IDF_DEMUXER) += bintext.o OBJS-$(CONFIG_IFF_DEMUXER) += iff.o OBJS-$(CONFIG_IMAGE2_DEMUXER) += img2.o OBJS-$(CONFIG_IMAGE2_MUXER) += img2.o @@ -308,6 +311,7 @@ OBJS-$(CONFIG_WTV_DEMUXER) += wtvdec.o wtv.o asfdec.o asf.o asfcry OBJS-$(CONFIG_WTV_MUXER) += wtvenc.o wtv.o asf.o riff.o OBJS-$(CONFIG_WV_DEMUXER) += wv.o apetag.o OBJS-$(CONFIG_XA_DEMUXER) += xa.o +OBJS-$(CONFIG_XBIN_DEMUXER) += bintext.o sauce.o OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o OBJS-$(CONFIG_XWMA_DEMUXER) += xwma.o riff.o OBJS-$(CONFIG_YOP_DEMUXER) += yop.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 1f0ac51e66..e725b37be5 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -51,6 +51,7 @@ void av_register_all(void) REGISTER_MUXER (A64, a64); REGISTER_DEMUXER (AAC, aac); REGISTER_MUXDEMUX (AC3, ac3); + REGISTER_DEMUXER (ADF, adf); REGISTER_MUXER (ADTS, adts); REGISTER_DEMUXER (AEA, aea); REGISTER_MUXDEMUX (AIFF, aiff); @@ -69,6 +70,7 @@ void av_register_all(void) REGISTER_DEMUXER (AVS, avs); REGISTER_DEMUXER (BETHSOFTVID, bethsoftvid); REGISTER_DEMUXER (BFI, bfi); + REGISTER_DEMUXER (BINTEXT, bintext); REGISTER_DEMUXER (BINK, bink); REGISTER_DEMUXER (C93, c93); REGISTER_MUXDEMUX (CAF, caf); @@ -103,6 +105,7 @@ void av_register_all(void) REGISTER_MUXDEMUX (H263, h263); REGISTER_MUXDEMUX (H264, h264); REGISTER_DEMUXER (IDCIN, idcin); + REGISTER_DEMUXER (IDF, idf); REGISTER_DEMUXER (IFF, iff); REGISTER_MUXDEMUX (IMAGE2, image2); REGISTER_MUXDEMUX (IMAGE2PIPE, image2pipe); @@ -229,6 +232,7 @@ void av_register_all(void) REGISTER_MUXDEMUX (WTV, wtv); REGISTER_DEMUXER (WV, wv); REGISTER_DEMUXER (XA, xa); + REGISTER_DEMUXER (XBIN, xbin); REGISTER_DEMUXER (XMV, xmv); REGISTER_DEMUXER (XWMA, xwma); REGISTER_DEMUXER (YOP, yop); diff --git a/libavformat/bintext.c b/libavformat/bintext.c new file mode 100644 index 0000000000..904fd1f519 --- /dev/null +++ b/libavformat/bintext.c @@ -0,0 +1,376 @@ +/* + * Binary text demuxer + * eXtended BINary text (XBIN) demuxer + * Artworx Data Format demuxer + * iCEDraw File demuxer + * Copyright (c) 2010 Peter Ross + * + * 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 demuxer + * eXtended BINary text (XBIN) demuxer + * Artworx Data Format demuxer + * iCEDraw File demuxer + */ + +#include "libavutil/intreadwrite.h" +#include "avformat.h" +#include "sauce.h" +#include "libavcodec/bintext.h" + +#define LINE_RATE 6000 /** characters per second */ + +typedef struct { + int chars_per_frame; + uint64_t fsize; /**< file size less metadata buffer */ +} BinDemuxContext; + +#if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER +/** + * Given filesize and width, calculate height (assume font_height of 16) + */ +static void calculate_height(AVCodecContext *avctx, uint64_t fsize) +{ + avctx->height = (fsize / ((avctx->width>>3)*2)) << 4; +} +#endif + +#if CONFIG_BINTEXT_DEMUXER +static const uint8_t next_magic[]={ + 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00 +}; + +static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize) +{ + ByteIOContext *pb = avctx->pb; + char buf[36]; + int len; + uint64_t start_pos = url_fsize(pb) - 256; + + url_fseek(pb, start_pos, SEEK_SET); + if (get_buffer(pb, buf, sizeof(next_magic)) != sizeof(next_magic)) + return -1; + if (memcmp(buf, next_magic, sizeof(next_magic))) + return -1; + if (get_byte(pb) != 0x01) + return -1; + + *fsize -= 256; + +#define GET_EFI2_META(name,size) \ + len = get_byte(pb); \ + if (len < 1 || len > size) \ + return -1; \ + if (get_buffer(pb, buf, size) == size && *buf) { \ + buf[len] = 0; \ + av_metadata_set2(&avctx->metadata, name, buf, 0); \ + } + + GET_EFI2_META("filename", 12) + GET_EFI2_META("author", 20) + GET_EFI2_META("publisher", 20) + GET_EFI2_META("title", 35) + + return 0; +} + +static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width) +{ + /** attempt to guess width */ + if (!got_width) + avctx->width = fsize > 4000 ? (160<<3) : (80<<3); +} + +static AVStream * init_stream(AVFormatContext *s, + AVFormatParameters *ap) +{ + BinDemuxContext *bin = s->priv_data; + AVStream *st = av_new_stream(s, 0); + if (!st) + return NULL; + st->codec->codec_tag = 0; + st->codec->codec_type = AVMEDIA_TYPE_VIDEO; + + if (!ap->time_base.num) { + av_set_pts_info(st, 60, 1, 25); + } else { + av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den); + } + + /* simulate tty display speed */ + bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1); + + st->codec->width = ap->width ? ap->width : (80<<3); + st->codec->height = ap->height ? ap->height : (25<<4); + return st; +} + +static int bintext_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + BinDemuxContext *bin = s->priv_data; + ByteIOContext *pb = s->pb; + + AVStream *st = init_stream(s, ap); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_id = CODEC_ID_BINTEXT; + + st->codec->extradata_size = 2; + st->codec->extradata = av_malloc(st->codec->extradata_size); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + st->codec->extradata[0] = 16; + st->codec->extradata[1] = 0; + + if (!url_is_streamed(pb)) { + int got_width = 0; + bin->fsize = url_fsize(pb); + if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0) + next_tag_read(s, &bin->fsize); + if (!ap->width) + predict_width(st->codec, bin->fsize, got_width); + if (!ap->height) + calculate_height(st->codec, bin->fsize); + url_fseek(pb, 0, SEEK_SET); + } + return 0; +}; +#endif /* CONFIG_BINTEXT_DEMUXER */ + +#if CONFIG_XBIN_DEMUXER +static int xbin_probe(AVProbeData *p) +{ + const uint8_t *d = p->buf; + + if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A && + AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 && + d[9] > 0 && d[9] <= 32) + return AVPROBE_SCORE_MAX; + return 0; +} + +static int xbin_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + BinDemuxContext *bin = s->priv_data; + ByteIOContext *pb = s->pb; + char fontheight, flags; + uint8_t *h; + + AVStream *st = init_stream(s, ap); + if (!st) + return AVERROR(ENOMEM); + + url_fskip(pb, 5); + st->codec->width = get_le16(pb)<<3; + st->codec->height = get_le16(pb); + fontheight = get_byte(pb); + st->codec->height *= fontheight; + flags = get_byte(pb); + + st->codec->extradata_size = 2; + if ((flags & BINTEXT_PALETTE)) + st->codec->extradata_size += 48; + if ((flags & BINTEXT_FONT)) + st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256); + st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT; + + h = st->codec->extradata = av_malloc(st->codec->extradata_size); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + st->codec->extradata[0] = fontheight; + st->codec->extradata[1] = flags; + if (get_buffer(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0) + return AVERROR(EIO); + + if (!url_is_streamed(pb)) { + bin->fsize = url_fsize(pb) - 9 - st->codec->extradata_size; + ff_sauce_read(s, &bin->fsize, NULL, 0); + url_fseek(pb, 9 + st->codec->extradata_size, SEEK_SET); + } + + return 0; +} +#endif /* CONFIG_XBIN_DEMUXER */ + +#if CONFIG_ADF_DEMUXER +static int adf_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + BinDemuxContext *bin = s->priv_data; + ByteIOContext *pb = s->pb; + AVStream *st; + + if (get_byte(pb) != 1) + return AVERROR_INVALIDDATA; + + st = init_stream(s, ap); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_id = CODEC_ID_BINTEXT; + + st->codec->extradata_size = 2 + 48 + 4096; + st->codec->extradata = av_malloc(st->codec->extradata_size); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + st->codec->extradata[0] = 16; + st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT; + + if (get_buffer(pb, st->codec->extradata + 2, 24) < 0) + return AVERROR(EIO); + url_fskip(pb, 144); + if (get_buffer(pb, st->codec->extradata + 2 + 24, 24) < 0) + return AVERROR(EIO); + if (get_buffer(pb, st->codec->extradata + 2 + 48, 4096) < 0) + return AVERROR(EIO); + + if (!url_is_streamed(pb)) { + int got_width = 0; + bin->fsize = url_fsize(pb) - 1 - 192 - 4096; + st->codec->width = 80<<3; + ff_sauce_read(s, &bin->fsize, &got_width, 0); + if (!ap->height) + calculate_height(st->codec, bin->fsize); + url_fseek(pb, 1 + 192 + 4096, SEEK_SET); + } + return 0; +} +#endif /* CONFIG_ADF_DEMUXER */ + +#if CONFIG_IDF_DEMUXER +static const uint8_t idf_magic[] = { + 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00 +}; + +static int idf_probe(AVProbeData *p) +{ + if (!memcmp(p->buf, idf_magic, FFMIN(sizeof(idf_magic), p->buf_size))) + return AVPROBE_SCORE_MAX; + return 0; +} + +static int idf_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + BinDemuxContext *bin = s->priv_data; + ByteIOContext *pb = s->pb; + AVStream *st; + int got_width = 0; + + if (url_is_streamed(pb)) + return AVERROR(EIO); + + st = init_stream(s, ap); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_id = CODEC_ID_IDF; + + st->codec->extradata_size = 2 + 48 + 4096; + st->codec->extradata = av_malloc(st->codec->extradata_size); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + st->codec->extradata[0] = 16; + st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT; + + url_fseek(pb, url_fsize(pb) - 4096 - 48, SEEK_SET); + + if (get_buffer(pb, st->codec->extradata + 2 + 48, 4096) < 0) + return AVERROR(EIO); + if (get_buffer(pb, st->codec->extradata + 2, 48) < 0) + return AVERROR(EIO); + + bin->fsize = url_fsize(pb) - 12 - 4096 - 48; + ff_sauce_read(s, &bin->fsize, &got_width, 0); + if (!ap->height) + calculate_height(st->codec, bin->fsize); + url_fseek(pb, 12, SEEK_SET); + return 0; +} +#endif /* CONFIG_IDF_DEMUXER */ + +static int read_packet(AVFormatContext *s, + AVPacket *pkt) +{ + BinDemuxContext *bin = s->priv_data; + + if (bin->fsize > 0) { + if (av_get_packet(s->pb, pkt, bin->fsize) < 0) + return AVERROR(EIO); + bin->fsize = -1; /* done */ + } else if (!bin->fsize) { + if (url_feof(s->pb)) + return AVERROR(EIO); + if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0) + return AVERROR(EIO); + } else { + return AVERROR(EIO); + } + + pkt->flags |= AV_PKT_FLAG_KEY; + return 0; +} + +#if CONFIG_BINTEXT_DEMUXER +AVInputFormat ff_bintext_demuxer = { + "bin", + NULL_IF_CONFIG_SMALL("Binary text"), + sizeof(BinDemuxContext), + NULL, + bintext_read_header, + read_packet, + .extensions = "bin", +}; +#endif + +#if CONFIG_XBIN_DEMUXER +AVInputFormat ff_xbin_demuxer = { + "xbin", + NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"), + sizeof(BinDemuxContext), + xbin_probe, + xbin_read_header, + read_packet, +}; +#endif + +#if CONFIG_ADF_DEMUXER +AVInputFormat ff_adf_demuxer = { + "adf", + NULL_IF_CONFIG_SMALL("Artworx Data Format"), + sizeof(BinDemuxContext), + NULL, + adf_read_header, + read_packet, + .extensions = "adf", +}; +#endif + +#if CONFIG_IDF_DEMUXER +AVInputFormat ff_idf_demuxer = { + "idf", + NULL_IF_CONFIG_SMALL("iCE Draw File"), + sizeof(BinDemuxContext), + idf_probe, + idf_read_header, + read_packet, + .extensions = "idf", +}; +#endif