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 "avcodec.h"
28 #include "codec_internal.h"
29 #include "decode.h"
30 #include "xface.h"
31 
32 static int pop_integer(BigInt *b, const ProbRange *pranges)
33 {
34  uint8_t r;
35  int i;
36 
37  /* extract the last byte into r, and shift right b by 8 bits */
38  ff_big_div(b, 0, &r);
39 
40  i = 0;
41  while (r < pranges->offset || r >= pranges->range + pranges->offset) {
42  pranges++;
43  i++;
44  }
45  ff_big_mul(b, pranges->range);
46  ff_big_add(b, r - pranges->offset);
47  return i;
48 }
49 
50 static void pop_greys(BigInt *b, char *bitmap, int w, int h)
51 {
52  if (w > 3) {
53  w /= 2;
54  h /= 2;
55  pop_greys(b, bitmap, w, h);
56  pop_greys(b, bitmap + w, w, h);
57  pop_greys(b, bitmap + XFACE_WIDTH * h, w, h);
58  pop_greys(b, bitmap + XFACE_WIDTH * h + w, w, h);
59  } else {
61  if (w & 1) bitmap[0] = 1;
62  if (w & 2) bitmap[1] = 1;
63  if (w & 4) bitmap[XFACE_WIDTH] = 1;
64  if (w & 8) bitmap[XFACE_WIDTH + 1] = 1;
65  }
66 }
67 
68 static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
69 {
71  case XFACE_COLOR_WHITE:
72  return;
73  case XFACE_COLOR_BLACK:
74  pop_greys(b, bitmap, w, h);
75  return;
76  default:
77  w /= 2;
78  h /= 2;
79  level++;
80  decode_block(b, bitmap, w, h, level);
81  decode_block(b, bitmap + w, w, h, level);
82  decode_block(b, bitmap + h * XFACE_WIDTH, w, h, level);
83  decode_block(b, bitmap + w + h * XFACE_WIDTH, w, h, level);
84  return;
85  }
86 }
87 
88 typedef struct XFaceContext {
89  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
90 } XFaceContext;
91 
93 {
94  if (avctx->width || avctx->height) {
95  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
96  av_log(avctx, AV_LOG_ERROR,
97  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
98  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
99  return AVERROR(EINVAL);
100  }
101  }
102 
103  avctx->width = XFACE_WIDTH;
104  avctx->height = XFACE_HEIGHT;
105  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
106 
107  return 0;
108 }
109 
111  int *got_frame, AVPacket *avpkt)
112 {
113  XFaceContext *xface = avctx->priv_data;
114  int ret, i, j, k;
115  uint8_t byte;
116  BigInt b = {0};
117  char *buf;
118  int64_t c;
119 
120  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
121  return ret;
122 
123  for (i = 0, k = 0; i < avpkt->size && avpkt->data[i]; i++) {
124  c = avpkt->data[i];
125 
126  /* ignore invalid digits */
127  if (c < XFACE_FIRST_PRINT || c > XFACE_LAST_PRINT)
128  continue;
129 
130  if (++k > XFACE_MAX_DIGITS) {
131  av_log(avctx, AV_LOG_WARNING,
132  "Buffer is longer than expected, truncating at byte %d\n", i);
133  break;
134  }
137  }
138 
139  /* decode image and put it in bitmap */
140  memset(xface->bitmap, 0, XFACE_PIXELS);
141  buf = xface->bitmap;
142  decode_block(&b, buf, 16, 16, 0);
143  decode_block(&b, buf + 16, 16, 16, 0);
144  decode_block(&b, buf + 32, 16, 16, 0);
145  decode_block(&b, buf + XFACE_WIDTH * 16, 16, 16, 0);
146  decode_block(&b, buf + XFACE_WIDTH * 16 + 16, 16, 16, 0);
147  decode_block(&b, buf + XFACE_WIDTH * 16 + 32, 16, 16, 0);
148  decode_block(&b, buf + XFACE_WIDTH * 32 , 16, 16, 0);
149  decode_block(&b, buf + XFACE_WIDTH * 32 + 16, 16, 16, 0);
150  decode_block(&b, buf + XFACE_WIDTH * 32 + 32, 16, 16, 0);
151 
152  ff_xface_generate_face(xface->bitmap, xface->bitmap);
153 
154  /* convert image from 1=black 0=white bitmap to MONOWHITE */
155  buf = frame->data[0];
156  for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
157  byte += xface->bitmap[i];
158  if (k == 7) {
159  buf[j++] = byte;
160  byte = k = 0;
161  } else {
162  k++;
163  byte <<= 1;
164  }
165  if (j == XFACE_WIDTH/8) {
166  j = 0;
167  buf += frame->linesize[0];
168  }
169  }
170 
171  *got_frame = 1;
172 
173  return avpkt->size;
174 }
175 
177  .p.name = "xface",
178  CODEC_LONG_NAME("X-face image"),
179  .p.type = AVMEDIA_TYPE_VIDEO,
180  .p.id = AV_CODEC_ID_XFACE,
181  .p.capabilities = AV_CODEC_CAP_DR1,
182  .priv_data_size = sizeof(XFaceContext),
185  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
186 };
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:204
r
const char * r
Definition: vf_curves.c:126
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:176
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:374
b
#define b
Definition: input.c:41
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:119
XFACE_PRINTS
#define XFACE_PRINTS
Definition: xface.h:42
pop_greys
static void pop_greys(BigInt *b, char *bitmap, int w, int h)
Definition: xfacedec.c:50
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
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:265
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
xface.h
XFACE_PIXELS
#define XFACE_PIXELS
Definition: xface.h:35
BigInt
Definition: xface.h:61
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:363
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
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:88
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1420
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
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
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
AVCodecContext::height
int height
Definition: avcodec.h:571
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
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:398
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:425
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:571
decode_block
static void decode_block(BigInt *b, char *bitmap, int w, int h, int level)
Definition: xfacedec.c:68
XFaceContext::bitmap
uint8_t bitmap[XFACE_PIXELS]
image used internally for decoding
Definition: xfacedec.c:89
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:92
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:110
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:32
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