FFmpeg  3.4.9
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 
104  s->frame = av_frame_alloc();
105  if (!s->frame)
106  return AVERROR(ENOMEM);
107 
108  return 0;
109 }
110 
111 #define DEFAULT_BG_COLOR 0
112 av_unused static void hscroll(AVCodecContext *avctx)
113 {
114  XbinContext *s = avctx->priv_data;
115  if (s->y < avctx->height - s->font_height) {
116  s->y += s->font_height;
117  } else {
118  memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
119  (avctx->height - s->font_height)*s->frame->linesize[0]);
120  memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
122  }
123 }
124 
125 /**
126  * Draw character to screen
127  */
128 static void draw_char(AVCodecContext *avctx, int c, int a)
129 {
130  XbinContext *s = avctx->priv_data;
131  if (s->y > avctx->height - s->font_height)
132  return;
133  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
134  s->frame->linesize[0], s->font, s->font_height, c,
135  a & 0x0F, a >> 4);
136  s->x += FONT_WIDTH;
137  if (s->x > avctx->width - FONT_WIDTH) {
138  s->x = 0;
139  s->y += s->font_height;
140  }
141 }
142 
143 static int decode_frame(AVCodecContext *avctx,
144  void *data, int *got_frame,
145  AVPacket *avpkt)
146 {
147  XbinContext *s = avctx->priv_data;
148  const uint8_t *buf = avpkt->data;
149  int buf_size = avpkt->size;
150  const uint8_t *buf_end = buf+buf_size;
151  int ret;
152 
153  s->x = s->y = 0;
154  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
155  return ret;
157  s->frame->palette_has_changed = 1;
158  memcpy(s->frame->data[1], s->palette, 16 * 4);
159 
160  if (avctx->codec_id == AV_CODEC_ID_XBIN) {
161  while (buf + 2 < buf_end) {
162  int i,c,a;
163  int type = *buf >> 6;
164  int count = (*buf & 0x3F) + 1;
165  buf++;
166  switch (type) {
167  case 0: //no compression
168  for (i = 0; i < count && buf + 1 < buf_end; i++) {
169  draw_char(avctx, buf[0], buf[1]);
170  buf += 2;
171  }
172  break;
173  case 1: //character compression
174  c = *buf++;
175  for (i = 0; i < count && buf < buf_end; i++)
176  draw_char(avctx, c, *buf++);
177  break;
178  case 2: //attribute compression
179  a = *buf++;
180  for (i = 0; i < count && buf < buf_end; i++)
181  draw_char(avctx, *buf++, a);
182  break;
183  case 3: //character/attribute compression
184  c = *buf++;
185  a = *buf++;
186  for (i = 0; i < count && buf < buf_end; i++)
187  draw_char(avctx, c, a);
188  break;
189  }
190  }
191  } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
192  while (buf + 2 < buf_end) {
193  if (AV_RL16(buf) == 1) {
194  int i;
195  if (buf + 6 > buf_end)
196  break;
197  for (i = 0; i < buf[2]; i++)
198  draw_char(avctx, buf[4], buf[5]);
199  buf += 6;
200  } else {
201  draw_char(avctx, buf[0], buf[1]);
202  buf += 2;
203  }
204  }
205  } else {
206  while (buf + 1 < buf_end) {
207  draw_char(avctx, buf[0], buf[1]);
208  buf += 2;
209  }
210  }
211 
212  if ((ret = av_frame_ref(data, s->frame)) < 0)
213  return ret;
214  *got_frame = 1;
215  return buf_size;
216 }
217 
219 {
220  XbinContext *s = avctx->priv_data;
221 
222  av_frame_free(&s->frame);
223 
224  return 0;
225 }
226 
227 #if CONFIG_BINTEXT_DECODER
228 AVCodec ff_bintext_decoder = {
229  .name = "bintext",
230  .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
231  .type = AVMEDIA_TYPE_VIDEO,
232  .id = AV_CODEC_ID_BINTEXT,
233  .priv_data_size = sizeof(XbinContext),
234  .init = decode_init,
235  .close = decode_end,
236  .decode = decode_frame,
237  .capabilities = AV_CODEC_CAP_DR1,
238 };
239 #endif
240 #if CONFIG_XBIN_DECODER
241 AVCodec ff_xbin_decoder = {
242  .name = "xbin",
243  .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
244  .type = AVMEDIA_TYPE_VIDEO,
245  .id = AV_CODEC_ID_XBIN,
246  .priv_data_size = sizeof(XbinContext),
247  .init = decode_init,
248  .close = decode_end,
249  .decode = decode_frame,
250  .capabilities = AV_CODEC_CAP_DR1,
251 };
252 #endif
253 #if CONFIG_IDF_DECODER
254 AVCodec ff_idf_decoder = {
255  .name = "idf",
256  .long_name = NULL_IF_CONFIG_SMALL("iCEDraw text"),
257  .type = AVMEDIA_TYPE_VIDEO,
258  .id = AV_CODEC_ID_IDF,
259  .priv_data_size = sizeof(XbinContext),
260  .init = decode_init,
261  .close = decode_end,
262  .decode = decode_frame,
263  .capabilities = AV_CODEC_CAP_DR1,
264 };
265 #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:201
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
int flags
Definition: bintext.c:43
int size
Definition: avcodec.h:1680
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
#define AV_RL16
Definition: intreadwrite.h:42
const uint8_t avpriv_vga16_font[4096]
static av_unused void hscroll(AVCodecContext *avctx)
Definition: bintext.c:112
AVCodec.
Definition: avcodec.h:3739
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:1718
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:150
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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:395
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1679
CGA/EGA/VGA ROM data.
static av_cold int decode_end(AVCodecContext *avctx)
Definition: bintext.c:218
#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:111
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
int palette[16]
Definition: bintext.c:42
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
int width
picture width / height.
Definition: avcodec.h:1948
static void draw_char(AVCodecContext *avctx, int c, int a)
Draw character to screen.
Definition: bintext.c:128
int font_height
Definition: bintext.c:44
#define FONT_WIDTH
Definition: bintext.c:38
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:1778
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: bintext.c:143
main external API structure.
Definition: avcodec.h:1761
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1877
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:358
AVFrame * frame
Definition: bintext.c:41
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:30
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
common internal api header.
static double c[64]
void * priv_data
Definition: avcodec.h:1803
void INT64 INT64 count
Definition: avisynth_c.h:690
#define BINTEXT_FONT
Definition: bintext.h:35
This structure stores compressed data.
Definition: avcodec.h:1656
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
CGA/EGA/VGA ROM font data.
#define av_unused
Definition: attributes.h:125