FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
30 #include "libavutil/avassert.h"
31 
32 typedef struct XFaceContext {
33  AVClass *class;
34  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
35  int max_line_len; ///< max line length for compressed data
36  int set_header; ///< set X-Face header in the output
37 } XFaceContext;
38 
39 static int all_same(char *bitmap, int w, int h)
40 {
41  char val, *row;
42  int x;
43 
44  val = *bitmap;
45  while (h--) {
46  row = bitmap;
47  x = w;
48  while (x--)
49  if (*(row++) != val)
50  return 0;
51  bitmap += XFACE_WIDTH;
52  }
53  return 1;
54 }
55 
56 static int all_black(char *bitmap, int w, int h)
57 {
58  if (w > 3) {
59  w /= 2;
60  h /= 2;
61  return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
62  all_black(bitmap + XFACE_WIDTH * h, w, h) &&
63  all_black(bitmap + XFACE_WIDTH * h + w, w, h));
64  } else {
65  /* at least one pixel in the 2x2 grid is non-zero */
66  return *bitmap || *(bitmap + 1) ||
67  *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
68  }
69 }
70 
71 static int all_white(char *bitmap, int w, int h)
72 {
73  return *bitmap == 0 && all_same(bitmap, w, h);
74 }
75 
76 typedef struct {
77  ProbRange prob_ranges[XFACE_PIXELS*2];
80 
81 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
82 {
83  if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
84  return -1;
85  pq->prob_ranges[pq->prob_ranges_idx++] = *p;
86  return 0;
87 }
88 
89 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
90 {
91  if (w > 3) {
92  w /= 2;
93  h /= 2;
94  push_greys(pq, bitmap, w, h);
95  push_greys(pq, bitmap + w, w, h);
96  push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
97  push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
98  } else {
100  *bitmap +
101  2 * *(bitmap + 1) +
102  4 * *(bitmap + XFACE_WIDTH) +
103  8 * *(bitmap + XFACE_WIDTH + 1);
104  pq_push(pq, p);
105  }
106 }
107 
108 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
109 {
110  if (all_white(bitmap, w, h)) {
112  } else if (all_black(bitmap, w, h)) {
114  push_greys(pq, bitmap, w, h);
115  } else {
117  w /= 2;
118  h /= 2;
119  level++;
120  encode_block(bitmap, w, h, level, pq);
121  encode_block(bitmap + w, w, h, level, pq);
122  encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
123  encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
124  }
125 }
126 
128 {
129  avctx->coded_frame = av_frame_alloc();
130  if (!avctx->coded_frame)
131  return AVERROR(ENOMEM);
133 
134  return 0;
135 }
136 
137 static void push_integer(BigInt *b, const ProbRange *prange)
138 {
139  uint8_t r;
140 
141  ff_big_div(b, prange->range, &r);
142  ff_big_mul(b, 0);
143  ff_big_add(b, r + prange->offset);
144 }
145 
147  const AVFrame *frame, int *got_packet)
148 {
149  XFaceContext *xface = avctx->priv_data;
150  ProbRangesQueue pq = {{{ 0 }}, 0};
151  uint8_t bitmap_copy[XFACE_PIXELS];
152  BigInt b = {0};
153  int i, j, k, ret = 0;
154  const uint8_t *buf;
155  uint8_t *p;
156  char intbuf[XFACE_MAX_DIGITS];
157 
158  if (avctx->width || avctx->height) {
159  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
160  av_log(avctx, AV_LOG_ERROR,
161  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
162  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
163  return AVERROR(EINVAL);
164  }
165  }
166  avctx->width = XFACE_WIDTH;
167  avctx->height = XFACE_HEIGHT;
168 
169  /* convert image from MONOWHITE to 1=black 0=white bitmap */
170  buf = frame->data[0];
171  i = j = 0;
172  do {
173  for (k = 0; k < 8; k++)
174  xface->bitmap[i++] = (buf[j]>>(7-k))&1;
175  if (++j == XFACE_WIDTH/8) {
176  buf += frame->linesize[0];
177  j = 0;
178  }
179  } while (i < XFACE_PIXELS);
180 
181  /* create a copy of bitmap */
182  memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
183  ff_xface_generate_face(xface->bitmap, bitmap_copy);
184 
185  encode_block(xface->bitmap, 16, 16, 0, &pq);
186  encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
187  encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
188  encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
189  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
190  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
191  encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
192  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
193  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
194 
195  while (pq.prob_ranges_idx > 0)
197 
198  /* write the inverted big integer in b to intbuf */
199  i = 0;
201  while (b.nb_words) {
202  uint8_t r;
203  ff_big_div(&b, XFACE_PRINTS, &r);
204  av_assert0(i < sizeof(intbuf));
205  intbuf[i++] = r + XFACE_FIRST_PRINT;
206  }
207 
208  if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
209  return ret;
210 
211  /* revert the number, and close the buffer */
212  p = pkt->data;
213  while (--i >= 0)
214  *(p++) = intbuf[i];
215  *(p++) = '\n';
216  *(p++) = 0;
217 
218  pkt->flags |= AV_PKT_FLAG_KEY;
219  *got_packet = 1;
220 
221  return 0;
222 }
223 
225 {
226  av_frame_free(&avctx->coded_frame);
227 
228  return 0;
229 }
230 
232  .name = "xface",
233  .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
234  .type = AVMEDIA_TYPE_VIDEO,
235  .id = AV_CODEC_ID_XFACE,
236  .priv_data_size = sizeof(XFaceContext),
238  .close = xface_encode_close,
239  .encode2 = xface_encode_frame,
240  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
241 };
static int all_white(char *bitmap, int w, int h)
Definition: xfaceenc.c:71
uint8_t offset
Definition: xface.h:89
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
const char const char void * val
Definition: avisynth_c.h:634
static int all_same(char *bitmap, int w, int h)
Definition: xfaceenc.c:39
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
int max_line_len
max line length for compressed data
Definition: xfaceenc.c:35
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2745
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int xface_encode_close(AVCodecContext *avctx)
Definition: xfaceenc.c:224
const char * b
Definition: vf_curves.c:109
#define XFACE_WIDTH
Definition: xface.h:30
const ProbRange ff_xface_probranges_per_level[4][3]
Definition: xface.c:129
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3181
X-Face common definitions.
static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
Definition: xfaceenc.c:108
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition: xface.c:286
int prob_ranges_idx
Definition: xfaceenc.c:78
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: xfaceenc.c:146
AVCodec ff_xface_encoder
Definition: xfaceenc.c:231
static AVFrame * frame
Definition: xface.h:58
uint8_t * data
Definition: avcodec.h:1162
void ff_big_mul(BigInt *b, uint8_t a)
Multiply a by b storing the result in b.
Definition: xface.c:93
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1208
static int all_black(char *bitmap, int w, int h)
Definition: xfaceenc.c:56
uint8_t range
Definition: xface.h:88
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
Definition: xfaceenc.c:89
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define XFACE_MAX_WORDS
Definition: xface.h:54
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
const char * r
Definition: vf_curves.c:107
void ff_big_add(BigInt *b, uint8_t a)
Add a to b storing the result in b.
Definition: xface.c:31
simple assert() macros that are a bit more flexible than ISO C assert().
const ProbRange ff_xface_probranges_2x2[16]
Definition: xface.c:137
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1168
static void push_integer(BigInt *b, const ProbRange *prange)
Definition: xfaceenc.c:137
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
#define XFACE_HEIGHT
Definition: xface.h:31
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
static av_cold int xface_encode_init(AVCodecContext *avctx)
Definition: xfaceenc.c:127
#define XFACE_FIRST_PRINT
Definition: xface.h:37
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
void * buf
Definition: avisynth_c.h:553
#define XFACE_PIXELS
Definition: xface.h:32
Describe the class of an AVClass context structure.
Definition: log.h:67
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
uint8_t level
Definition: svq3.c:150
ProbRange prob_ranges[XFACE_PIXELS *2]
Definition: xfaceenc.c:77
#define XFACE_PRINTS
Definition: xface.h:39
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:72
static int pq_push(ProbRangesQueue *pq, const ProbRange *p)
Definition: xfaceenc.c:81
#define XFACE_MAX_DIGITS
Definition: xface.h:47
void * priv_data
Definition: avcodec.h:1283
int nb_words
Definition: xface.h:59
int set_header
set X-Face header in the output
Definition: xfaceenc.c:36
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139