FFmpeg
bintext.c
Go to the documentation of this file.
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"
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 {
45  int palette[16];
46  int flags;
48  const uint8_t *font;
49  int x, y;
50 } XbinContext;
51 
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;
97  break;
98  case 16:
99  s->font = avpriv_vga16_font;
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 
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  s->frame->palette_has_changed = 1;
161  memcpy(s->frame->data[1], s->palette, 16 * 4);
162 
163  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
164  while (buf + 2 < buf_end) {
165  int i,c,a;
166  int type = *buf >> 6;
167  int count = (*buf & 0x3F) + 1;
168  buf++;
169  switch (type) {
170  case 0: //no compression
171  for (i = 0; i < count && buf + 1 < buf_end; i++) {
172  draw_char(avctx, buf[0], buf[1]);
173  buf += 2;
174  }
175  break;
176  case 1: //character compression
177  c = *buf++;
178  for (i = 0; i < count && buf < buf_end; i++)
179  draw_char(avctx, c, *buf++);
180  break;
181  case 2: //attribute compression
182  a = *buf++;
183  for (i = 0; i < count && buf < buf_end; i++)
184  draw_char(avctx, *buf++, a);
185  break;
186  case 3: //character/attribute compression
187  c = *buf++;
188  a = *buf++;
189  for (i = 0; i < count && buf < buf_end; i++)
190  draw_char(avctx, c, a);
191  break;
192  }
193  }
194  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
195  while (buf + 2 < buf_end) {
196  if (AV_RL16(buf) == 1) {
197  int i;
198  if (buf + 6 > buf_end)
199  break;
200  for (i = 0; i < buf[2]; i++)
201  draw_char(avctx, buf[4], buf[5]);
202  buf += 6;
203  } else {
204  draw_char(avctx, buf[0], buf[1]);
205  buf += 2;
206  }
207  }
208  } else {
209  while (buf + 1 < buf_end) {
210  draw_char(avctx, buf[0], buf[1]);
211  buf += 2;
212  }
213  }
214 
215  *got_frame = 1;
216  return buf_size;
217 }
218 
219 #if CONFIG_BINTEXT_DECODER
220 const FFCodec ff_bintext_decoder = {
221  .p.name = "bintext",
222  CODEC_LONG_NAME("Binary text"),
223  .p.type = AVMEDIA_TYPE_VIDEO,
224  .p.id = AV_CODEC_ID_BINTEXT,
225  .priv_data_size = sizeof(XbinContext),
226  .init = decode_init,
228  .p.capabilities = AV_CODEC_CAP_DR1,
229 };
230 #endif
231 #if CONFIG_XBIN_DECODER
232 const FFCodec ff_xbin_decoder = {
233  .p.name = "xbin",
234  CODEC_LONG_NAME("eXtended BINary text"),
235  .p.type = AVMEDIA_TYPE_VIDEO,
236  .p.id = AV_CODEC_ID_XBIN,
237  .priv_data_size = sizeof(XbinContext),
238  .init = decode_init,
240  .p.capabilities = AV_CODEC_CAP_DR1,
241 };
242 #endif
243 #if CONFIG_IDF_DECODER
244 const FFCodec ff_idf_decoder = {
245  .p.name = "idf",
246  CODEC_LONG_NAME("iCEDraw text"),
247  .p.type = AVMEDIA_TYPE_VIDEO,
248  .p.id = AV_CODEC_ID_IDF,
249  .priv_data_size = sizeof(XbinContext),
250  .init = decode_init,
252  .p.capabilities = AV_CODEC_CAP_DR1,
253 };
254 #endif
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
ff_cga_palette
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:30
XbinContext::palette
int palette[16]
Definition: bintext.c:45
hscroll
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:112
av_unused
#define av_unused
Definition: attributes.h:131
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:127
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bintext.c:52
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_draw_pc_font
void ff_draw_pc_font(uint8_t *dst, int linesize, const uint8_t *font, int font_height, int ch, int fg, int bg)
Draw CGA/EGA/VGA font to 8-bit pixel buffer.
Definition: cga_data.c:46
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:528
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
DEFAULT_BG_COLOR
#define DEFAULT_BG_COLOR
Definition: bintext.c:111
XbinContext::y
int y
Definition: bintext.c:49
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:436
XbinContext::flags
int flags
Definition: bintext.c:46
XbinContext::x
int x
Definition: bintext.c:49
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:143
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
draw_char
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
Definition: bintext.c:128
BINTEXT_PALETTE
#define BINTEXT_PALETTE
Definition: bintext.h:34
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1473
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
AV_CODEC_ID_IDF
@ AV_CODEC_ID_IDF
Definition: codec_id.h:579
codec_internal.h
AV_CODEC_ID_BINTEXT
@ AV_CODEC_ID_BINTEXT
Definition: codec_id.h:577
cga_data.h
FONT_WIDTH
#define FONT_WIDTH
Definition: bintext.c:41
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
xga_font_data.h
ff_xbin_decoder
const FFCodec ff_xbin_decoder
ff_bintext_decoder
const FFCodec ff_bintext_decoder
XbinContext
Definition: bintext.c:43
avpriv_vga16_font
const uint8_t avpriv_vga16_font[4096]
Definition: xga_font_data.c:160
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:527
bintext.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AVCodecContext::height
int height
Definition: avcodec.h:598
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
XbinContext::font
const uint8_t * font
Definition: bintext.c:48
AVCodecContext
main external API structure.
Definition: avcodec.h:426
BINTEXT_FONT
#define BINTEXT_FONT
Definition: bintext.h:35
AV_CODEC_ID_XBIN
@ AV_CODEC_ID_XBIN
Definition: codec_id.h:578
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
XbinContext::font_height
int font_height
Definition: bintext.c:47
XbinContext::frame
AVFrame * frame
Definition: bintext.c:44
avpriv_cga_font
const uint8_t avpriv_cga_font[2048]
Definition: xga_font_data.c:29
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
ff_idf_decoder
const FFCodec ff_idf_decoder
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97