FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  return AVERROR_INVALIDDATA;
98 
99 
100  s->frame = av_frame_alloc();
101  if (!s->frame)
102  return AVERROR(ENOMEM);
103 
104  return 0;
105 }
106 
107 #define DEFAULT_BG_COLOR 0
108 av_unused static void hscroll(AVCodecContext *avctx)
109 {
110  XbinContext *s = avctx->priv_data;
111  if (s->y < avctx->height - s->font_height) {
112  s->y += s->font_height;
113  } else {
114  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
115  (avctx->height - s->font_height)*s->frame->linesize[0]);
116  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
118  }
119 }
120 
121 /**
122  * Draw character to screen
123  */
124 static void draw_char(AVCodecContext *avctx, int c, int a)
125 {
126  XbinContext *s = avctx->priv_data;
127  if (s->y > avctx->height - s->font_height)
128  return;
129  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
130  s->frame->linesize[0], s->font, s->font_height, c,
131  a & 0x0F, a >> 4);
132  s->x += FONT_WIDTH;
133  if (s->x > avctx->width - FONT_WIDTH) {
134  s->x = 0;
135  s->y += s->font_height;
136  }
137 }
138 
139 static int decode_frame(AVCodecContext *avctx,
140  void *data, int *got_frame,
141  AVPacket *avpkt)
142 {
143  XbinContext *s = avctx->priv_data;
144  const uint8_t *buf = avpkt->data;
145  int buf_size = avpkt->size;
146  const uint8_t *buf_end = buf+buf_size;
147  int ret;
148 
149  s->x = s->y = 0;
150  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
151  return ret;
153  s->frame->palette_has_changed = 1;
154  memcpy(s->frame->data[1], s->palette, 16 * 4);
155 
156  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
157  while (buf + 2 < buf_end) {
158  int i,c,a;
159  int type = *buf >> 6;
160  int count = (*buf & 0x3F) + 1;
161  buf++;
162  switch (type) {
163  case 0: //no compression
164  for (i = 0; i < count && buf + 1 < buf_end; i++) {
165  draw_char(avctx, buf[0], buf[1]);
166  buf += 2;
167  }
168  break;
169  case 1: //character compression
170  c = *buf++;
171  for (i = 0; i < count && buf < buf_end; i++)
172  draw_char(avctx, c, *buf++);
173  break;
174  case 2: //attribute compression
175  a = *buf++;
176  for (i = 0; i < count && buf < buf_end; i++)
177  draw_char(avctx, *buf++, a);
178  break;
179  case 3: //character/attribute compression
180  c = *buf++;
181  a = *buf++;
182  for (i = 0; i < count && buf < buf_end; i++)
183  draw_char(avctx, c, a);
184  break;
185  }
186  }
187  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
188  while (buf + 2 < buf_end) {
189  if (AV_RL16(buf) == 1) {
190  int i;
191  if (buf + 6 > buf_end)
192  break;
193  for (i = 0; i < buf[2]; i++)
194  draw_char(avctx, buf[4], buf[5]);
195  buf += 6;
196  } else {
197  draw_char(avctx, buf[0], buf[1]);
198  buf += 2;
199  }
200  }
201  } else {
202  while (buf + 1 < buf_end) {
203  draw_char(avctx, buf[0], buf[1]);
204  buf += 2;
205  }
206  }
207 
208  if ((ret = av_frame_ref(data, s->frame)) < 0)
209  return ret;
210  *got_frame = 1;
211  return buf_size;
212 }
213 
215 {
216  XbinContext *s = avctx->priv_data;
217 
218  av_frame_free(&s->frame);
219 
220  return 0;
221 }
222 
223 #if CONFIG_BINTEXT_DECODER
225  .name = "bintext",
226  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
227  .type = AVMEDIA_TYPE_VIDEO,
228  .id = AV_CODEC_ID_BINTEXT,
229  .priv_data_size = sizeof(XbinContext),
230  .init = decode_init,
231  .close = decode_end,
232  .decode = decode_frame,
233  .capabilities = AV_CODEC_CAP_DR1,
234 };
235 #endif
236 #if CONFIG_XBIN_DECODER
238  .name = "xbin",
239  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
240  .type = AVMEDIA_TYPE_VIDEO,
241  .id = AV_CODEC_ID_XBIN,
242  .priv_data_size = sizeof(XbinContext),
243  .init = decode_init,
244  .close = decode_end,
245  .decode = decode_frame,
246  .capabilities = AV_CODEC_CAP_DR1,
247 };
248 #endif
249 #if CONFIG_IDF_DECODER
251  .name = "idf",
252  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
253  .type = AVMEDIA_TYPE_VIDEO,
254  .id = AV_CODEC_ID_IDF,
255  .priv_data_size = sizeof(XbinContext),
256  .init = decode_init,
257  .close = decode_end,
258  .decode = decode_frame,
259  .capabilities = AV_CODEC_CAP_DR1,
260 };
261 #endif
Binary text decoder.
const char * s
Definition: avisynth_c.h:768
#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:218
const uint8_t * font
Definition: bintext.c:45
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#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:1431
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
const uint8_t avpriv_vga16_font[4096]
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:108
AVCodec.
Definition: avcodec.h:3408
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
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1938
static av_cold int decode_init(AVCodecContext *avctx)
Definition: bintext.c:49
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:441
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
uint8_t * data
Definition: avcodec.h:1430
CGA/EGA/VGA ROM data.
static av_cold int decode_end(AVCodecContext *avctx)
Definition: bintext.c:214
#define av_log(a,...)
#define BINTEXT_PALETTE
Definition: bintext.h:34
#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 AVERROR(e)
Definition: error.h:43
#define DEFAULT_BG_COLOR
Definition: bintext.c:107
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#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:3415
GLsizei count
Definition: opengl_enc.c:109
int palette[16]
Definition: bintext.c:42
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
int width
picture width / height.
Definition: avcodec.h:1690
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
Definition: bintext.c:124
int font_height
Definition: bintext.c:44
#define FONT_WIDTH
Definition: bintext.c:38
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:1528
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:139
main external API structure.
Definition: avcodec.h:1518
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1619
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:375
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:232
common internal api header.
static double c[64]
void * priv_data
Definition: avcodec.h:1545
#define BINTEXT_FONT
Definition: bintext.h:35
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
CGA/EGA/VGA ROM font data.
#define av_unused
Definition: attributes.h:125