FFmpeg
xfacedec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1990 James Ashton - Sydney University
3  * Copyright (c) 2012 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * X-Face decoder, based on libcompface, by James Ashton.
25  */
26 
27 #include "libavutil/pixdesc.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "xface.h"
32 
33 static int pop_integer(BigInt *b, const ProbRange *pranges)
34 {
35  uint8_t r;
36  int i;
37 
38  /* extract the last byte into r, and shift right b by 8 bits */
39  ff_big_div(b, 0, &r);
40 
41  i = 0;
42  while (r < pranges->offset || r >= pranges->range + pranges->offset) {
43  pranges++;
44  i++;
45  }
46  ff_big_mul(b, pranges->range);
47  ff_big_add(b, r - pranges->offset);
48  return i;
49 }
50 
51 static void pop_greys(BigInt *b, char *bitmap, int w, int h)
52 {
53  if (w > 3) {
54  w /= 2;
55  h /= 2;
56  pop_greys(b, bitmap, w, h);
57  pop_greys(b, bitmap + w, w, h);
58  pop_greys(b, bitmap + XFACE_WIDTH * h, w, h);
59  pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
60  } else {
62  if (w & 1) bitmap[0] = 1;
63  if (w & 2) bitmap[1] = 1;
64  if (w & 4) bitmap[XFACE_WIDTH] = 1;
65  if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
66  }
67 }
68 
69 static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
70 {
71  switch (pop_integer(b, &ff_xface_probranges_per_level[level][0])) {
72  case XFACE_COLOR_WHITE:
73  return;
74  case XFACE_COLOR_BLACK:
75  pop_greys(b, bitmap, w, h);
76  return;
77  default:
78  w /= 2;
79  h /= 2;
80  level++;
81  decode_block(b, bitmap, w, h, level);
82  decode_block(b, bitmap + w, w, h, level);
83  decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level);
84  decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
85  return;
86  }
87 }
88 
89 typedef struct XFaceContext {
90  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
91 } XFaceContext;
92 
94 {
95  if (avctx->width || avctx->height) {
96  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
97  av_log(avctx, AV_LOG_ERROR,
98  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
99  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
100  return AVERROR(EINVAL);
101  }
102  }
103 
104  avctx->width = XFACE_WIDTH;
105  avctx->height = XFACE_HEIGHT;
106  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
107 
108  return 0;
109 }
110 
112  void *data, int *got_frame,
113  AVPacket *avpkt)
114 {
115  XFaceContext *xface = avctx->priv_data;
116  int ret, i, j, k;
117  uint8_t byte;
118  BigInt b = {0};
119  char *buf;
120  int64_t c;
121  AVFrame *frame = data;
122 
123  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
124  return ret;
125 
126  for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
127  c = avpkt->data[i];
128 
129  /* ignore invalid digits */
130  if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
131  continue;
132 
133  if (++k > XFACE_MAX_DIGITS) {
134  av_log(avctx, AV_LOG_WARNING,
135  "Buffer is longer than expected, truncating at byte %d\n", i);
136  break;
137  }
139  ff_big_add(&b, c - XFACE_FIRST_PRINT);
140  }
141 
142  /* decode image and put it in bitmap */
143  memset(xface->bitmap, 0, XFACE_PIXELS);
144  buf = xface->bitmap;
145  decode_block(&b, buf, 16, 16, 0);
146  decode_block(&b, buf + 16, 16, 16, 0);
147  decode_block(&b, buf + 32, 16, 16, 0);
148  decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
149  decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
150  decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
151  decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
152  decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
153  decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
154 
155  ff_xface_generate_face(xface->bitmap, xface->bitmap);
156 
157  /* convert image from 1=black 0=white bitmap to MONOWHITE */
158  buf = frame->data[0];
159  for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
160  byte += xface->bitmap[i];
161  if (k == 7) {
162  buf[j++] = byte;
163  byte = k = 0;
164  } else {
165  k++;
166  byte <<= 1;
167  }
168  if (j == XFACE_WIDTH/8) {
169  j = 0;
170  buf += frame->linesize[0];
171  }
172  }
173 
174  *got_frame = 1;
175 
176  return avpkt->size;
177 }
178 
180  .name = "xface",
181  .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
182  .type = AVMEDIA_TYPE_VIDEO,
183  .id = AV_CODEC_ID_XFACE,
184  .priv_data_size = sizeof(XFaceContext),
188 };
static int pop_integer(BigInt *b, const ProbRange *pranges)
Definition: xfacedec.c:33
uint8_t offset
Definition: xface.h:92
static av_cold int xface_decode_init(AVCodecContext *avctx)
Definition: xfacedec.c:93
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
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
int size
Definition: avcodec.h:1481
static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
Definition: xfacedec.c:69
#define XFACE_WIDTH
Definition: xface.h:33
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1778
const ProbRange ff_xface_probranges_per_level[4][3]
Definition: xface.c:129
AVCodec.
Definition: avcodec.h:3492
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
X-Face common definitions.
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition: xface.c:286
uint8_t
#define av_cold
Definition: attributes.h:82
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
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
Definition: xface.h:61
uint8_t * data
Definition: avcodec.h:1480
void ff_big_mul(BigInt *b, uint8_t a)
Multiply a by b storing the result in b.
Definition: xface.c:93
static int xface_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xfacedec.c:111
#define av_log(a,...)
uint8_t range
Definition: xface.h:91
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:114
void ff_big_add(BigInt *b, uint8_t a)
Add a to b storing the result in b.
Definition: xface.c:31
const ProbRange ff_xface_probranges_2x2[16]
Definition: xface.c:137
const char * name
Name of the codec implementation.
Definition: avcodec.h:3499
#define b
Definition: input.c:41
int width
picture width / height.
Definition: avcodec.h:1741
uint8_t w
Definition: llviddspenc.c:38
#define XFACE_HEIGHT
Definition: xface.h:34
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
void ff_big_div(BigInt *b, uint8_t a, uint8_t *r)
Divide b by a storing the result in b and the remainder in the word pointed to by r...
Definition: xface.c:54
uint8_t bitmap[XFACE_PIXELS]
image used internally for decoding
Definition: xfacedec.c:90
#define XFACE_FIRST_PRINT
Definition: xface.h:40
Libavcodec external API header.
#define XFACE_LAST_PRINT
Definition: xface.h:41
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_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
main external API structure.
Definition: avcodec.h:1568
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1968
void * buf
Definition: avisynth_c.h:766
#define XFACE_PIXELS
Definition: xface.h:35
static void pop_greys(BigInt *b, char *bitmap, int w, int h)
Definition: xfacedec.c:51
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
uint8_t level
Definition: svq3.c:207
AVCodec ff_xface_decoder
Definition: xfacedec.c:179
#define XFACE_PRINTS
Definition: xface.h:42
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
#define XFACE_MAX_DIGITS
Definition: xface.h:50
void * priv_data
Definition: avcodec.h:1595
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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.Frame references ownership and permissions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1457