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  } else {
67  s->font_height = 8;
68  s->flags = 0;
69  }
70 
71  if ((s->flags & BINTEXT_PALETTE)) {
72  for (i = 0; i < 16; i++) {
73  s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
74  p += 3;
75  }
76  } else {
77  for (i = 0; i < 16; i++)
78  s->palette[i] = 0xFF000000 | ff_cga_palette[i];
79  }
80 
81  if ((s->flags & BINTEXT_FONT)) {
82  s->font = p;
83  } else {
84  switch(s->font_height) {
85  default:
86  av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
87  s->font_height = 8;
88  case 8:
89  s->font = avpriv_cga_font;
90  break;
91  case 16:
93  break;
94  }
95  }
96  if (avctx->width < FONT_WIDTH || avctx->height < s->font_height) {
97  av_log(avctx, AV_LOG_ERROR, "Resolution too small for font.\n");
98  return AVERROR_INVALIDDATA;
99  }
100 
101  return 0;
102 }
103 
104 #define DEFAULT_BG_COLOR 0
105 av_unused static void hscroll(AVCodecContext *avctx)
106 {
107  XbinContext *s = avctx->priv_data;
108  if (s->y < avctx->height - s->font_height) {
109  s->y += s->font_height;
110  } else {
111  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
112  (avctx->height - s->font_height)*s->frame->linesize[0]);
113  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
115  }
116 }
117 
118 /**
119  * Draw character to screen
120  */
121 static void draw_char(AVCodecContext *avctx, int c, int a)
122 {
123  XbinContext *s = avctx->priv_data;
124  if (s->y > avctx->height - s->font_height)
125  return;
126  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
127  s->frame->linesize[0], s->font, s->font_height, c,
128  a & 0x0F, a >> 4);
129  s->x += FONT_WIDTH;
130  if (s->x > avctx->width - FONT_WIDTH) {
131  s->x = 0;
132  s->y += s->font_height;
133  }
134 }
135 
136 static int decode_frame(AVCodecContext *avctx,
137  void *data, int *got_frame,
138  AVPacket *avpkt)
139 {
140  XbinContext *s = avctx->priv_data;
141  const uint8_t *buf = avpkt->data;
142  int buf_size = avpkt->size;
143  const uint8_t *buf_end = buf+buf_size;
144  int ret;
145 
146  if ((avctx->width / FONT_WIDTH) * (avctx->height / s->font_height) / 256 > buf_size)
147  return AVERROR_INVALIDDATA;
148 
149  s->frame = data;
150  s->x = s->y = 0;
151  if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
152  return ret;
154  s->frame->palette_has_changed = 1;
155  memcpy(s->frame->data[1], s->palette, 16 * 4);
156 
157  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
158  while (buf + 2 < buf_end) {
159  int i,c,a;
160  int type = *buf >> 6;
161  int count = (*buf & 0x3F) + 1;
162  buf++;
163  switch (type) {
164  case 0: //no compression
165  for (i = 0; i < count && buf + 1 < buf_end; i++) {
166  draw_char(avctx, buf[0], buf[1]);
167  buf += 2;
168  }
169  break;
170  case 1: //character compression
171  c = *buf++;
172  for (i = 0; i < count && buf < buf_end; i++)
173  draw_char(avctx, c, *buf++);
174  break;
175  case 2: //attribute compression
176  a = *buf++;
177  for (i = 0; i < count && buf < buf_end; i++)
178  draw_char(avctx, *buf++, a);
179  break;
180  case 3: //character/attribute compression
181  c = *buf++;
182  a = *buf++;
183  for (i = 0; i < count && buf < buf_end; i++)
184  draw_char(avctx, c, a);
185  break;
186  }
187  }
188  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
189  while (buf + 2 < buf_end) {
190  if (AV_RL16(buf) == 1) {
191  int i;
192  if (buf + 6 > buf_end)
193  break;
194  for (i = 0; i < buf[2]; i++)
195  draw_char(avctx, buf[4], buf[5]);
196  buf += 6;
197  } else {
198  draw_char(avctx, buf[0], buf[1]);
199  buf += 2;
200  }
201  }
202  } else {
203  while (buf + 1 < buf_end) {
204  draw_char(avctx, buf[0], buf[1]);
205  buf += 2;
206  }
207  }
208 
209  *got_frame = 1;
210  return buf_size;
211 }
212 
213 #if CONFIG_BINTEXT_DECODER
215  .name = "bintext",
216  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
217  .type = AVMEDIA_TYPE_VIDEO,
218  .id = AV_CODEC_ID_BINTEXT,
219  .priv_data_size = sizeof(XbinContext),
220  .init = decode_init,
221  .decode = decode_frame,
222  .capabilities = AV_CODEC_CAP_DR1,
223 };
224 #endif
225 #if CONFIG_XBIN_DECODER
227  .name = "xbin",
228  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
229  .type = AVMEDIA_TYPE_VIDEO,
230  .id = AV_CODEC_ID_XBIN,
231  .priv_data_size = sizeof(XbinContext),
232  .init = decode_init,
233  .decode = decode_frame,
234  .capabilities = AV_CODEC_CAP_DR1,
235 };
236 #endif
237 #if CONFIG_IDF_DECODER
239  .name = "idf",
240  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
241  .type = AVMEDIA_TYPE_VIDEO,
242  .id = AV_CODEC_ID_IDF,
243  .priv_data_size = sizeof(XbinContext),
244  .init = decode_init,
245  .decode = decode_frame,
246  .capabilities = AV_CODEC_CAP_DR1,
247 };
248 #endif
Binary text decoder.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
const uint8_t * font
Definition: bintext.c:45
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVCodec ff_bintext_decoder
int flags
Definition: bintext.c:43
int size
Definition: avcodec.h:1478
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:36
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
GLint GLenum type
Definition: opengl_enc.c:104
const uint8_t avpriv_vga16_font[4096]
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:105
AVCodec.
Definition: avcodec.h:3477
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bintext.c:49
uint8_t
#define av_cold
Definition: attributes.h:82
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
uint8_t * data
Definition: avcodec.h:1477
CGA/EGA/VGA ROM data.
#define av_log(a,...)
#define BINTEXT_PALETTE
Definition: bintext.h:34
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const uint8_t avpriv_cga_font[2048]
Definition: xga_font_data.c:29
#define DEFAULT_BG_COLOR
Definition: bintext.c:104
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3484
GLsizei count
Definition: opengl_enc.c:108
int palette[16]
Definition: bintext.c:42
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:351
int width
picture width / height.
Definition: avcodec.h:1738
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
Definition: bintext.c:121
int font_height
Definition: bintext.c:44
#define FONT_WIDTH
Definition: bintext.c:38
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodec ff_idf_decoder
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
Libavcodec external API header.
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:87
enum AVCodecID codec_id
Definition: avcodec.h:1575
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:136
main external API structure.
Definition: avcodec.h:1565
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1964
void * buf
Definition: avisynth_c.h:766
int extradata_size
Definition: avcodec.h:1667
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:425
AVFrame * frame
Definition: bintext.c:41
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:30
AVCodec ff_xbin_decoder
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
common internal api header.
void * priv_data
Definition: avcodec.h:1592
#define BINTEXT_FONT
Definition: bintext.h:35
This structure stores compressed data.
Definition: avcodec.h:1454
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
CGA/EGA/VGA ROM font data.
#define av_unused
Definition: attributes.h:125