libavcodec/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 typedef struct XbinContext {
40  int palette[16];
41  int flags;
43  const uint8_t *font;
44  int x, y;
45 } XbinContext;
46 
48 {
49  XbinContext *s = avctx->priv_data;
50  uint8_t *p;
51  int i;
52 
53  avctx->pix_fmt = AV_PIX_FMT_PAL8;
54  p = avctx->extradata;
55  if (p) {
56  s->font_height = p[0];
57  s->flags = p[1];
58  p += 2;
59  if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
60  + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
61  av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
62  return AVERROR_INVALIDDATA;
63  }
64  } else {
65  s->font_height = 8;
66  s->flags = 0;
67  }
68 
69  if ((s->flags & BINTEXT_PALETTE)) {
70  for (i = 0; i < 16; i++) {
71  s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
72  p += 3;
73  }
74  } else {
75  for (i = 0; i < 16; i++)
76  s->palette[i] = 0xFF000000 | ff_cga_palette[i];
77  }
78 
79  if ((s->flags & BINTEXT_FONT)) {
80  s->font = p;
81  } else {
82  switch(s->font_height) {
83  default:
84  av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
85  s->font_height = 8;
86  case 8:
87  s->font = avpriv_cga_font;
88  break;
89  case 16:
91  break;
92  }
93  }
94 
95  s->frame = av_frame_alloc();
96  if (!s->frame)
97  return AVERROR(ENOMEM);
98 
99  return 0;
100 }
101 
102 #define DEFAULT_BG_COLOR 0
103 av_unused static void hscroll(AVCodecContext *avctx)
104 {
105  XbinContext *s = avctx->priv_data;
106  if (s->y < avctx->height - s->font_height) {
107  s->y += s->font_height;
108  } else {
109  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
110  (avctx->height - s->font_height)*s->frame->linesize[0]);
111  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
113  }
114 }
115 
116 #define FONT_WIDTH 8
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  s->x = s->y = 0;
147  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
148  return ret;
150  s->frame->palette_has_changed = 1;
151  memcpy(s->frame->data[1], s->palette, 16 * 4);
152 
153  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
154  while (buf + 2 < buf_end) {
155  int i,c,a;
156  int type = *buf >> 6;
157  int count = (*buf & 0x3F) + 1;
158  buf++;
159  switch (type) {
160  case 0: //no compression
161  for (i = 0; i < count && buf + 1 < buf_end; i++) {
162  draw_char(avctx, buf[0], buf[1]);
163  buf += 2;
164  }
165  break;
166  case 1: //character compression
167  c = *buf++;
168  for (i = 0; i < count && buf < buf_end; i++)
169  draw_char(avctx, c, *buf++);
170  break;
171  case 2: //attribute compression
172  a = *buf++;
173  for (i = 0; i < count && buf < buf_end; i++)
174  draw_char(avctx, *buf++, a);
175  break;
176  case 3: //character/attribute compression
177  c = *buf++;
178  a = *buf++;
179  for (i = 0; i < count && buf < buf_end; i++)
180  draw_char(avctx, c, a);
181  break;
182  }
183  }
184  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
185  while (buf + 2 < buf_end) {
186  if (AV_RL16(buf) == 1) {
187  int i;
188  if (buf + 6 > buf_end)
189  break;
190  for (i = 0; i < buf[2]; i++)
191  draw_char(avctx, buf[4], buf[5]);
192  buf += 6;
193  } else {
194  draw_char(avctx, buf[0], buf[1]);
195  buf += 2;
196  }
197  }
198  } else {
199  while (buf + 1 < buf_end) {
200  draw_char(avctx, buf[0], buf[1]);
201  buf += 2;
202  }
203  }
204 
205  if ((ret = av_frame_ref(data, s->frame)) < 0)
206  return ret;
207  *got_frame = 1;
208  return buf_size;
209 }
210 
212 {
213  XbinContext *s = avctx->priv_data;
214 
215  av_frame_free(&s->frame);
216 
217  return 0;
218 }
219 
220 #if CONFIG_BINTEXT_DECODER
221 AVCodec ff_bintext_decoder = {
222  .name = "bintext",
223  .type = AVMEDIA_TYPE_VIDEO,
224  .id = AV_CODEC_ID_BINTEXT,
225  .priv_data_size = sizeof(XbinContext),
226  .init = decode_init,
227  .close = decode_end,
228  .decode = decode_frame,
229  .capabilities = CODEC_CAP_DR1,
230  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
231 };
232 #endif
233 #if CONFIG_XBIN_DECODER
234 AVCodec ff_xbin_decoder = {
235  .name = "xbin",
236  .type = AVMEDIA_TYPE_VIDEO,
237  .id = AV_CODEC_ID_XBIN,
238  .priv_data_size = sizeof(XbinContext),
239  .init = decode_init,
240  .close = decode_end,
241  .decode = decode_frame,
242  .capabilities = CODEC_CAP_DR1,
243  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
244 };
245 #endif
246 #if CONFIG_IDF_DECODER
247 AVCodec ff_idf_decoder = {
248  .name = "idf",
249  .type = AVMEDIA_TYPE_VIDEO,
250  .id = AV_CODEC_ID_IDF,
251  .priv_data_size = sizeof(XbinContext),
252  .init = decode_init,
253  .close = decode_end,
254  .decode = decode_frame,
255  .capabilities = CODEC_CAP_DR1,
256  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
257 };
258 #endif
Binary text decoder.
const char * s
Definition: avisynth_c.h:668
#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:76
const uint8_t * font
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
#define AV_RB24
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_RL16
const uint8_t avpriv_vga16_font[4096]
static av_unused void hscroll(AVCodecContext *avctx)
static av_cold int decode_init(AVCodecContext *avctx)
uint8_t
#define av_cold
Definition: attributes.h:78
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
CGA/EGA/VGA ROM data.
static av_cold int decode_end(AVCodecContext *avctx)
struct XbinContext XbinContext
#define BINTEXT_PALETTE
Definition: bintext.h:34
const uint8_t avpriv_cga_font[2048]
Definition: xga_font_data.c:29
#define DEFAULT_BG_COLOR
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
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...
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
#define FONT_WIDTH
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
enum AVCodecID codec_id
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
synthesis window for stochastic i
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:30
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:95
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static double c[64]
void INT64 INT64 count
Definition: avisynth_c.h:594
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
#define BINTEXT_FONT
Definition: bintext.h:35
This structure stores compressed data.
CGA/EGA/VGA ROM font data.
#define av_unused
Definition: attributes.h:114