FFmpeg  4.1.5
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:
97  break;
98  }
99  }
100  if (avctx->width < FONT_WIDTH || avctx->height < s->font_height)
101  return AVERROR_INVALIDDATA;
102 
103  return 0;
104 }
105 
106 #define DEFAULT_BG_COLOR 0
107 av_unused static void hscroll(AVCodecContext *avctx)
108 {
109  XbinContext *s = avctx->priv_data;
110  if (s->y < avctx->height - s->font_height) {
111  s->y += s->font_height;
112  } else {
113  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
114  (avctx->height - s->font_height)*s->frame->linesize[0]);
115  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
117  }
118 }
119 
120 /**
121  * Draw character to screen
122  */
123 static void draw_char(AVCodecContext *avctx, int c, int a)
124 {
125  XbinContext *s = avctx->priv_data;
126  if (s->y > avctx->height - s->font_height)
127  return;
128  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
129  s->frame->linesize[0], s->font, s->font_height, c,
130  a & 0x0F, a >> 4);
131  s->x += FONT_WIDTH;
132  if (s->x > avctx->width - FONT_WIDTH) {
133  s->x = 0;
134  s->y += s->font_height;
135  }
136 }
137 
138 static int decode_frame(AVCodecContext *avctx,
139  void *data, int *got_frame,
140  AVPacket *avpkt)
141 {
142  XbinContext *s = avctx->priv_data;
143  const uint8_t *buf = avpkt->data;
144  int buf_size = avpkt->size;
145  const uint8_t *buf_end = buf+buf_size;
146  int ret;
147 
148  if ((avctx->width / FONT_WIDTH) * (avctx->height / s->font_height) / 256 > buf_size)
149  return AVERROR_INVALIDDATA;
150 
151  s->frame = data;
152  s->x = s->y = 0;
153  if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
154  return ret;
156  s->frame->palette_has_changed = 1;
157  memcpy(s->frame->data[1], s->palette, 16 * 4);
158 
159  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
160  while (buf + 2 < buf_end) {
161  int i,c,a;
162  int type = *buf >> 6;
163  int count = (*buf & 0x3F) + 1;
164  buf++;
165  switch (type) {
166  case 0: //no compression
167  for (i = 0; i < count && buf + 1 < buf_end; i++) {
168  draw_char(avctx, buf[0], buf[1]);
169  buf += 2;
170  }
171  break;
172  case 1: //character compression
173  c = *buf++;
174  for (i = 0; i < count && buf < buf_end; i++)
175  draw_char(avctx, c, *buf++);
176  break;
177  case 2: //attribute compression
178  a = *buf++;
179  for (i = 0; i < count && buf < buf_end; i++)
180  draw_char(avctx, *buf++, a);
181  break;
182  case 3: //character/attribute compression
183  c = *buf++;
184  a = *buf++;
185  for (i = 0; i < count && buf < buf_end; i++)
186  draw_char(avctx, c, a);
187  break;
188  }
189  }
190  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
191  while (buf + 2 < buf_end) {
192  if (AV_RL16(buf) == 1) {
193  int i;
194  if (buf + 6 > buf_end)
195  break;
196  for (i = 0; i < buf[2]; i++)
197  draw_char(avctx, buf[4], buf[5]);
198  buf += 6;
199  } else {
200  draw_char(avctx, buf[0], buf[1]);
201  buf += 2;
202  }
203  }
204  } else {
205  while (buf + 1 < buf_end) {
206  draw_char(avctx, buf[0], buf[1]);
207  buf += 2;
208  }
209  }
210 
211  *got_frame = 1;
212  return buf_size;
213 }
214 
215 #if CONFIG_BINTEXT_DECODER
217  .name = "bintext",
218  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
219  .type = AVMEDIA_TYPE_VIDEO,
220  .id = AV_CODEC_ID_BINTEXT,
221  .priv_data_size = sizeof(XbinContext),
222  .init = decode_init,
223  .decode = decode_frame,
224  .capabilities = AV_CODEC_CAP_DR1,
225 };
226 #endif
227 #if CONFIG_XBIN_DECODER
229  .name = "xbin",
230  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
231  .type = AVMEDIA_TYPE_VIDEO,
232  .id = AV_CODEC_ID_XBIN,
233  .priv_data_size = sizeof(XbinContext),
234  .init = decode_init,
235  .decode = decode_frame,
236  .capabilities = AV_CODEC_CAP_DR1,
237 };
238 #endif
239 #if CONFIG_IDF_DECODER
241  .name = "idf",
242  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
243  .type = AVMEDIA_TYPE_VIDEO,
244  .id = AV_CODEC_ID_IDF,
245  .priv_data_size = sizeof(XbinContext),
246  .init = decode_init,
247  .decode = decode_frame,
248  .capabilities = AV_CODEC_CAP_DR1,
249 };
250 #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:226
const uint8_t * font
Definition: bintext.c:45
#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:1446
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
#define AV_RL16
Definition: intreadwrite.h:42
const uint8_t avpriv_vga16_font[4096]
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:107
AVCodec.
Definition: avcodec.h:3424
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1634
const char data[16]
Definition: mxf.c:91
uint8_t * data
Definition: avcodec.h:1445
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:258
#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:106
#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:3431
int palette[16]
Definition: bintext.c:42
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
int width
picture width / height.
Definition: avcodec.h:1706
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
Definition: bintext.c:123
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.
enum AVCodecID codec_id
Definition: avcodec.h:1543
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:138
main external API structure.
Definition: avcodec.h:1533
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1635
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:383
cl_device_type type
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:240
common internal api header.
static double c[64]
void * priv_data
Definition: avcodec.h:1560
void INT64 INT64 count
Definition: avisynth_c.h:690
#define BINTEXT_FONT
Definition: bintext.h:35
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
CGA/EGA/VGA ROM font data.
#define av_unused
Definition: attributes.h:125