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 "codec_internal.h"
31 #include "internal.h"
32 #include "xface.h"
33 
34 static int pop_integer(BigInt *b, const ProbRange *pranges)
35 {
36  uint8_t r;
37  int i;
38 
39  /* extract the last byte into r, and shift right b by 8 bits */
40  ff_big_div(b, 0, &r);
41 
42  i = 0;
43  while (r < pranges->offset || r >= pranges->range + pranges->offset) {
44  pranges++;
45  i++;
46  }
47  ff_big_mul(b, pranges->range);
48  ff_big_add(b, r - pranges->offset);
49  return i;
50 }
51 
52 static void pop_greys(BigInt *b, char *bitmap, int w, int h)
53 {
54  if (w > 3) {
55  w /= 2;
56  h /= 2;
57  pop_greys(b, bitmap, w, h);
58  pop_greys(b, bitmap + w, w, h);
59  pop_greys(b, bitmap + XFACE_WIDTH * h, w, h);
60  pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
61  } else {
63  if (w & 1) bitmap[0] = 1;
64  if (w & 2) bitmap[1] = 1;
65  if (w & 4) bitmap[XFACE_WIDTH] = 1;
66  if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
67  }
68 }
69 
70 static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
71 {
73  case XFACE_COLOR_WHITE:
74  return;
75  case XFACE_COLOR_BLACK:
76  pop_greys(b, bitmap, w, h);
77  return;
78  default:
79  w /= 2;
80  h /= 2;
81  level++;
82  decode_block(b, bitmap, w, h, level);
83  decode_block(b, bitmap + w, w, h, level);
84  decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level);
85  decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
86  return;
87  }
88 }
89 
90 typedef struct XFaceContext {
91  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
92 } XFaceContext;
93 
95 {
96  if (avctx->width || avctx->height) {
97  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
98  av_log(avctx, AV_LOG_ERROR,
99  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
100  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
101  return AVERROR(EINVAL);
102  }
103  }
104 
105  avctx->width = XFACE_WIDTH;
106  avctx->height = XFACE_HEIGHT;
107  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
108 
109  return 0;
110 }
111 
113  int *got_frame, 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 
122  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
123  return ret;
124 
125  for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
126  c = avpkt->data[i];
127 
128  /* ignore invalid digits */
129  if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
130  continue;
131 
132  if (++k > XFACE_MAX_DIGITS) {
133  av_log(avctx, AV_LOG_WARNING,
134  "Buffer is longer than expected, truncating at byte %d\n", i);
135  break;
136  }
139  }
140 
141  /* decode image and put it in bitmap */
142  memset(xface->bitmap, 0, XFACE_PIXELS);
143  buf = xface->bitmap;
144  decode_block(&b, buf, 16, 16, 0);
145  decode_block(&b, buf + 16, 16, 16, 0);
146  decode_block(&b, buf + 32, 16, 16, 0);
147  decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
148  decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
149  decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
150  decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
151  decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
152  decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
153 
154  ff_xface_generate_face(xface->bitmap, xface->bitmap);
155 
156  /* convert image from 1=black 0=white bitmap to MONOWHITE */
157  buf = frame->data[0];
158  for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
159  byte += xface->bitmap[i];
160  if (k == 7) {
161  buf[j++] = byte;
162  byte = k = 0;
163  } else {
164  k++;
165  byte <<= 1;
166  }
167  if (j == XFACE_WIDTH/8) {
168  j = 0;
169  buf += frame->linesize[0];
170  }
171  }
172 
173  *got_frame = 1;
174 
175  return avpkt->size;
176 }
177 
179  .p.name = "xface",
180  .p.long_name = NULL_IF_CONFIG_SMALL("X-face image"),
181  .p.type = AVMEDIA_TYPE_VIDEO,
182  .p.id = AV_CODEC_ID_XFACE,
183  .p.capabilities = AV_CODEC_CAP_DR1,
184  .priv_data_size = sizeof(XFaceContext),
187  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
188  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
189 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
level
uint8_t level
Definition: svq3.c:206
r
const char * r
Definition: vf_curves.c:116
AVERROR
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
XFACE_HEIGHT
#define XFACE_HEIGHT
Definition: xface.h:34
ProbRange::offset
uint8_t offset
Definition: xface.h:92
ff_big_div
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
ff_xface_decoder
const FFCodec ff_xface_decoder
Definition: xfacedec.c:178
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
b
#define b
Definition: input.c:34
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:75
ProbRange::range
uint8_t range
Definition: xface.h:91
FFCodec
Definition: codec_internal.h:112
XFACE_PRINTS
#define XFACE_PRINTS
Definition: xface.h:42
init
static int init
Definition: av_tx.c:47
pop_greys
static void pop_greys(BigInt *b, char *bitmap, int w, int h)
Definition: xfacedec.c:52
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
ProbRange
Definition: xface.h:90
XFACE_LAST_PRINT
#define XFACE_LAST_PRINT
Definition: xface.h:41
ff_big_add
void ff_big_add(BigInt *b, uint8_t a)
Add a to b storing the result in b.
Definition: xface.c:31
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_CODEC_ID_XFACE
@ AV_CODEC_ID_XFACE
Definition: codec_id.h:261
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
xface.h
XFACE_PIXELS
#define XFACE_PIXELS
Definition: xface.h:35
BigInt
Definition: xface.h:61
XFACE_WIDTH
#define XFACE_WIDTH
Definition: xface.h:33
XFACE_COLOR_WHITE
@ XFACE_COLOR_WHITE
Definition: xface.h:85
c
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
XFaceContext
Definition: xfacedec.c:90
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
byte
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:99
codec_internal.h
offset
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
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
XFACE_FIRST_PRINT
#define XFACE_FIRST_PRINT
Definition: xface.h:40
XFACE_COLOR_BLACK
@ XFACE_COLOR_BLACK
Definition: xface.h:85
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
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
Definition: filter_design.txt:264
ff_xface_probranges_2x2
const ProbRange ff_xface_probranges_2x2[16]
Definition: xface.c:137
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
ff_xface_probranges_per_level
const ProbRange ff_xface_probranges_per_level[4][3]
Definition: xface.c:129
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
bytestream.h
decode_block
static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
Definition: xfacedec.c:70
XFaceContext::bitmap
uint8_t bitmap[XFACE_PIXELS]
image used internally for decoding
Definition: xfacedec.c:91
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_xface_generate_face
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition: xface.c:286
xface_decode_init
static av_cold int xface_decode_init(AVCodecContext *avctx)
Definition: xfacedec.c:94
h
h
Definition: vp9dsp_template.c:2038
xface_decode_frame
static int xface_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: xfacedec.c:112
XFACE_MAX_DIGITS
#define XFACE_MAX_DIGITS
Definition: xface.h:50
pop_integer
static int pop_integer(BigInt *b, const ProbRange *pranges)
Definition: xfacedec.c:34
ff_big_mul
void ff_big_mul(BigInt *b, uint8_t a)
Multiply a by b storing the result in b.
Definition: xface.c:93