FFmpeg
xfaceenc.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 encoder, based on libcompface, by James Ashton.
25  */
26 
27 #include "xface.h"
28 #include "avcodec.h"
29 #include "encode.h"
30 #include "internal.h"
31 #include "libavutil/avassert.h"
32 
33 typedef struct XFaceContext {
34  AVClass *class;
35  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
36  int max_line_len; ///< max line length for compressed data
37  int set_header; ///< set X-Face header in the output
38 } XFaceContext;
39 
40 static int all_same(char *bitmap, int w, int h)
41 {
42  char val, *row;
43  int x;
44 
45  val = *bitmap;
46  while (h--) {
47  row = bitmap;
48  x = w;
49  while (x--)
50  if (*(row++) != val)
51  return 0;
52  bitmap += XFACE_WIDTH;
53  }
54  return 1;
55 }
56 
57 static int all_black(char *bitmap, int w, int h)
58 {
59  if (w > 3) {
60  w /= 2;
61  h /= 2;
62  return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
63  all_black(bitmap + XFACE_WIDTH * h, w, h) &&
64  all_black(bitmap + XFACE_WIDTH * h + w, w, h));
65  } else {
66  /* at least one pixel in the 2x2 grid is non-zero */
67  return *bitmap || *(bitmap + 1) ||
68  *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
69  }
70 }
71 
72 static int all_white(char *bitmap, int w, int h)
73 {
74  return *bitmap == 0 && all_same(bitmap, w, h);
75 }
76 
77 typedef struct {
78  ProbRange prob_ranges[XFACE_PIXELS*2];
81 
82 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
83 {
84  if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
85  return -1;
86  pq->prob_ranges[pq->prob_ranges_idx++] = *p;
87  return 0;
88 }
89 
90 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
91 {
92  if (w > 3) {
93  w /= 2;
94  h /= 2;
95  push_greys(pq, bitmap, w, h);
96  push_greys(pq, bitmap + w, w, h);
97  push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
98  push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
99  } else {
101  *bitmap +
102  2 * *(bitmap + 1) +
103  4 * *(bitmap + XFACE_WIDTH) +
104  8 * *(bitmap + XFACE_WIDTH + 1);
105  pq_push(pq, p);
106  }
107 }
108 
109 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
110 {
111  if (all_white(bitmap, w, h)) {
113  } else if (all_black(bitmap, w, h)) {
115  push_greys(pq, bitmap, w, h);
116  } else {
118  w /= 2;
119  h /= 2;
120  level++;
121  encode_block(bitmap, w, h, level, pq);
122  encode_block(bitmap + w, w, h, level, pq);
123  encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
124  encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
125  }
126 }
127 
128 static void push_integer(BigInt *b, const ProbRange *prange)
129 {
130  uint8_t r;
131 
132  ff_big_div(b, prange->range, &r);
133  ff_big_mul(b, 0);
134  ff_big_add(b, r + prange->offset);
135 }
136 
138  const AVFrame *frame, int *got_packet)
139 {
140  XFaceContext *xface = avctx->priv_data;
141  ProbRangesQueue pq = {{{ 0 }}, 0};
142  uint8_t bitmap_copy[XFACE_PIXELS];
143  BigInt b = {0};
144  int i, j, k, ret = 0;
145  const uint8_t *buf;
146  uint8_t *p;
147  char intbuf[XFACE_MAX_DIGITS];
148 
149  if (avctx->width || avctx->height) {
150  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
151  av_log(avctx, AV_LOG_ERROR,
152  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
153  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
154  return AVERROR(EINVAL);
155  }
156  }
157  avctx->width = XFACE_WIDTH;
158  avctx->height = XFACE_HEIGHT;
159 
160  /* convert image from MONOWHITE to 1=black 0=white bitmap */
161  buf = frame->data[0];
162  i = j = 0;
163  do {
164  for (k = 0; k < 8; k++)
165  xface->bitmap[i++] = (buf[j]>>(7-k))&1;
166  if (++j == XFACE_WIDTH/8) {
167  buf += frame->linesize[0];
168  j = 0;
169  }
170  } while (i < XFACE_PIXELS);
171 
172  /* create a copy of bitmap */
173  memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
174  ff_xface_generate_face(xface->bitmap, bitmap_copy);
175 
176  encode_block(xface->bitmap, 16, 16, 0, &pq);
177  encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
178  encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
179  encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
180  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
181  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
182  encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
183  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
184  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
185 
186  while (pq.prob_ranges_idx > 0)
188 
189  /* write the inverted big integer in b to intbuf */
190  i = 0;
191  av_assert0(b.nb_words < XFACE_MAX_WORDS);
192  while (b.nb_words) {
193  uint8_t r;
194  ff_big_div(&b, XFACE_PRINTS, &r);
195  av_assert0(i < sizeof(intbuf));
196  intbuf[i++] = r + XFACE_FIRST_PRINT;
197  }
198 
199  if ((ret = ff_get_encode_buffer(avctx, pkt, i + 2, 0)) < 0)
200  return ret;
201 
202  /* revert the number, and close the buffer */
203  p = pkt->data;
204  while (--i >= 0)
205  *(p++) = intbuf[i];
206  *(p++) = '\n';
207  *(p++) = 0;
208 
210  *got_packet = 1;
211 
212  return 0;
213 }
214 
216  .name = "xface",
217  .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
218  .type = AVMEDIA_TYPE_VIDEO,
219  .id = AV_CODEC_ID_XFACE,
220  .capabilities = AV_CODEC_CAP_DR1,
221  .priv_data_size = sizeof(XFaceContext),
222  .encode2 = xface_encode_frame,
224 };
AVCodec
AVCodec.
Definition: codec.h:202
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
level
uint8_t level
Definition: svq3.c:204
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
ProbRangesQueue::prob_ranges
ProbRange prob_ranges[XFACE_PIXELS *2]
Definition: xfaceenc.c:78
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
encode.h
b
#define b
Definition: input.c:40
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
XFACE_PRINTS
#define XFACE_PRINTS
Definition: xface.h:42
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
xface_encode_frame
static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: xfaceenc.c:137
pq_push
static int pq_push(ProbRangesQueue *pq, const ProbRange *p)
Definition: xfaceenc.c:82
ProbRange
Definition: xface.h:90
val
static double val(void *priv, double ch)
Definition: aeval.c:76
avassert.h
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
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_CODEC_ID_XFACE
@ AV_CODEC_ID_XFACE
Definition: codec_id.h:261
xface.h
XFACE_PIXELS
#define XFACE_PIXELS
Definition: xface.h:35
BigInt
Definition: xface.h:61
ProbRangesQueue::prob_ranges_idx
int prob_ranges_idx
Definition: xfaceenc.c:79
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
all_black
static int all_black(char *bitmap, int w, int h)
Definition: xfaceenc.c:57
all_same
static int all_same(char *bitmap, int w, int h)
Definition: xfaceenc.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
XFACE_WIDTH
#define XFACE_WIDTH
Definition: xface.h:33
ff_xface_encoder
const AVCodec ff_xface_encoder
Definition: xfaceenc.c:215
XFACE_COLOR_WHITE
@ XFACE_COLOR_WHITE
Definition: xface.h:85
XFaceContext
Definition: xfacedec.c:89
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
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
XFACE_COLOR_GREY
@ XFACE_COLOR_GREY
Definition: xface.h:85
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
XFaceContext::set_header
int set_header
set X-Face header in the output
Definition: xfaceenc.c:37
i
int i
Definition: input.c:406
ProbRangesQueue
Definition: xfaceenc.c:77
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:209
AVCodecContext::height
int height
Definition: avcodec.h:556
avcodec.h
XFaceContext::max_line_len
int max_line_len
max line length for compressed data
Definition: xfaceenc.c:36
ret
ret
Definition: filter_design.txt:187
XFACE_MAX_WORDS
#define XFACE_MAX_WORDS
Definition: xface.h:57
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
encode_block
static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
Definition: xfaceenc.c:109
AVCodecContext
main external API structure.
Definition: avcodec.h:383
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:78
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
push_integer
static void push_integer(BigInt *b, const ProbRange *prange)
Definition: xfaceenc.c:128
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
XFaceContext::bitmap
uint8_t bitmap[XFACE_PIXELS]
image used internally for decoding
Definition: xfacedec.c:90
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_xface_generate_face
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition: xface.c:286
h
h
Definition: vp9dsp_template.c:2038
XFACE_MAX_DIGITS
#define XFACE_MAX_DIGITS
Definition: xface.h:50
all_white
static int all_white(char *bitmap, int w, int h)
Definition: xfaceenc.c:72
push_greys
static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
Definition: xfaceenc.c:90
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