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 "libavutil/intreadwrite.h"
33 #include "avcodec.h"
34 #include "cga_data.h"
35 #include "bintext.h"
36 #include "internal.h"
37 
38 #define FONT_WIDTH 8
39 
40 typedef struct XbinContext {
42  int palette[16];
43  int flags;
45  const uint8_t *font;
46  int x, y;
47 } XbinContext;
48 
50 {
51  XbinContext *s = avctx->priv_data;
52  uint8_t *p;
53  int i;
54 
55  avctx->pix_fmt = AV_PIX_FMT_PAL8;
56  p = avctx->extradata;
57  if (p) {
58  s->font_height = p[0];
59  s->flags = p[1];
60  p += 2;
61  if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
62  + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
63  av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
64  return AVERROR_INVALIDDATA;
65  }
66  if (!s->font_height) {
67  av_log(avctx, AV_LOG_ERROR, "invalid font height\n");
68  return AVERROR_INVALIDDATA;
69  }
70  } else {
71  s->font_height = 8;
72  s->flags = 0;
73  }
74 
75  if ((s->flags & BINTEXT_PALETTE)) {
76  for (i = 0; i < 16; i++) {
77  s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
78  p += 3;
79  }
80  } else {
81  for (i = 0; i < 16; i++)
82  s->palette[i] = 0xFF000000 | ff_cga_palette[i];
83  }
84 
85  if ((s->flags & BINTEXT_FONT)) {
86  s->font = p;
87  } else {
88  switch(s->font_height) {
89  default:
90  av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
91  s->font_height = 8;
92  case 8:
93  s->font = avpriv_cga_font;
94  break;
95  case 16:
96  s->font = avpriv_vga16_font;
97  break;
98  }
99  }
100  if (avctx->width < FONT_WIDTH || avctx->height < s->font_height) {
101  av_log(avctx, AV_LOG_ERROR, "Resolution too small for font.\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  return 0;
106 }
107 
108 #define DEFAULT_BG_COLOR 0
109 av_unused static void hscroll(AVCodecContext *avctx)
110 {
111  XbinContext *s = avctx->priv_data;
112  if (s->y < avctx->height - s->font_height) {
113  s->y += s->font_height;
114  } else {
115  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
116  (avctx->height - s->font_height)*s->frame->linesize[0]);
117  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
118  DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
119  }
120 }
121 
122 /**
123  * Draw character to screen
124  */
125 static void draw_char(AVCodecContext *avctx, int c, int a)
126 {
127  XbinContext *s = avctx->priv_data;
128  if (s->y > avctx->height - s->font_height)
129  return;
130  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
131  s->frame->linesize[0], s->font, s->font_height, c,
132  a & 0x0F, a >> 4);
133  s->x += FONT_WIDTH;
134  if (s->x > avctx->width - FONT_WIDTH) {
135  s->x = 0;
136  s->y += s->font_height;
137  }
138 }
139 
140 static int decode_frame(AVCodecContext *avctx,
141  void *data, int *got_frame,
142  AVPacket *avpkt)
143 {
144  XbinContext *s = avctx->priv_data;
145  const uint8_t *buf = avpkt->data;
146  int buf_size = avpkt->size;
147  const uint8_t *buf_end = buf+buf_size;
148  int ret;
149 
150  if ((avctx->width / FONT_WIDTH) * (avctx->height / s->font_height) / 256 > buf_size)
151  return AVERROR_INVALIDDATA;
152 
153  s->frame = data;
154  s->x = s->y = 0;
155  if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
156  return ret;
157  s->frame->pict_type = AV_PICTURE_TYPE_I;
158  s->frame->palette_has_changed = 1;
159  memcpy(s->frame->data[1], s->palette, 16 * 4);
160 
161  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
162  while (buf + 2 < buf_end) {
163  int i,c,a;
164  int type = *buf >> 6;
165  int count = (*buf & 0x3F) + 1;
166  buf++;
167  switch (type) {
168  case 0: //no compression
169  for (i = 0; i < count && buf + 1 < buf_end; i++) {
170  draw_char(avctx, buf[0], buf[1]);
171  buf += 2;
172  }
173  break;
174  case 1: //character compression
175  c = *buf++;
176  for (i = 0; i < count && buf < buf_end; i++)
177  draw_char(avctx, c, *buf++);
178  break;
179  case 2: //attribute compression
180  a = *buf++;
181  for (i = 0; i < count && buf < buf_end; i++)
182  draw_char(avctx, *buf++, a);
183  break;
184  case 3: //character/attribute compression
185  c = *buf++;
186  a = *buf++;
187  for (i = 0; i < count && buf < buf_end; i++)
188  draw_char(avctx, c, a);
189  break;
190  }
191  }
192  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
193  while (buf + 2 < buf_end) {
194  if (AV_RL16(buf) == 1) {
195  int i;
196  if (buf + 6 > buf_end)
197  break;
198  for (i = 0; i < buf[2]; i++)
199  draw_char(avctx, buf[4], buf[5]);
200  buf += 6;
201  } else {
202  draw_char(avctx, buf[0], buf[1]);
203  buf += 2;
204  }
205  }
206  } else {
207  while (buf + 1 < buf_end) {
208  draw_char(avctx, buf[0], buf[1]);
209  buf += 2;
210  }
211  }
212 
213  *got_frame = 1;
214  return buf_size;
215 }
216 
217 #if CONFIG_BINTEXT_DECODER
219  .name = "bintext",
220  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
221  .type = AVMEDIA_TYPE_VIDEO,
222  .id = AV_CODEC_ID_BINTEXT,
223  .priv_data_size = sizeof(XbinContext),
224  .init = decode_init,
225  .decode = decode_frame,
226  .capabilities = AV_CODEC_CAP_DR1,
227 };
228 #endif
229 #if CONFIG_XBIN_DECODER
231  .name = "xbin",
232  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
233  .type = AVMEDIA_TYPE_VIDEO,
234  .id = AV_CODEC_ID_XBIN,
235  .priv_data_size = sizeof(XbinContext),
236  .init = decode_init,
237  .decode = decode_frame,
238  .capabilities = AV_CODEC_CAP_DR1,
239 };
240 #endif
241 #if CONFIG_IDF_DECODER
243  .name = "idf",
244  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
245  .type = AVMEDIA_TYPE_VIDEO,
246  .id = AV_CODEC_ID_IDF,
247  .priv_data_size = sizeof(XbinContext),
248  .init = decode_init,
249  .decode = decode_frame,
250  .capabilities = AV_CODEC_CAP_DR1,
251 };
252 #endif
AVCodec
AVCodec.
Definition: codec.h:190
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
ff_cga_palette
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:30
XbinContext::palette
int palette[16]
Definition: bintext.c:42
hscroll
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:109
ff_idf_decoder
AVCodec ff_idf_decoder
av_unused
#define av_unused
Definition: attributes.h:131
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
data
const char data[16]
Definition: mxf.c:91
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bintext.c:49
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_xbin_decoder
AVCodec ff_xbin_decoder
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:176
av_cold
#define av_cold
Definition: attributes.h:90
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
DEFAULT_BG_COLOR
#define DEFAULT_BG_COLOR
Definition: bintext.c:108
XbinContext::y
int y
Definition: bintext.c:46
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:90
ff_bintext_decoder
AVCodec ff_bintext_decoder
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:536
XbinContext::flags
int flags
Definition: bintext.c:43
XbinContext::x
int x
Definition: bintext.c:46
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:125
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:1854
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
AV_CODEC_ID_IDF
@ AV_CODEC_ID_IDF
Definition: codec_id.h:544
AV_CODEC_ID_BINTEXT
@ AV_CODEC_ID_BINTEXT
Definition: codec_id.h:542
cga_data.h
FONT_WIDTH
#define FONT_WIDTH
Definition: bintext.c:38
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
XbinContext
Definition: bintext.c:40
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:627
bintext.h
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:140
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
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
XbinContext::font
const uint8_t * font
Definition: bintext.c:45
AVCodecContext
main external API structure.
Definition: avcodec.h:526
BINTEXT_FONT
#define BINTEXT_FONT
Definition: bintext.h:35
AV_CODEC_ID_XBIN
@ AV_CODEC_ID_XBIN
Definition: codec_id.h:543
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
XbinContext::font_height
int font_height
Definition: bintext.c:44
XbinContext::frame
AVFrame * frame
Definition: bintext.c:41
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:332
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:93