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 
31 typedef struct XFaceContext {
32  AVClass *class;
33  uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
34  int max_line_len; ///< max line length for compressed data
35  int set_header; ///< set X-Face header in the output
36 } XFaceContext;
37 
38 static int all_same(char *bitmap, int w, int h)
39 {
40  char val, *row;
41  int x;
42 
43  val = *bitmap;
44  while (h--) {
45  row = bitmap;
46  x = w;
47  while (x--)
48  if (*(row++) != val)
49  return 0;
50  bitmap += XFACE_WIDTH;
51  }
52  return 1;
53 }
54 
55 static int all_black(char *bitmap, int w, int h)
56 {
57  if (w > 3) {
58  w /= 2;
59  h /= 2;
60  return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) &&
61  all_black(bitmap + XFACE_WIDTH * h, w, h) &&
62  all_black(bitmap + XFACE_WIDTH * h + w, w, h));
63  } else {
64  /* at least one pixel in the 2x2 grid is non-zero */
65  return *bitmap || *(bitmap + 1) ||
66  *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1);
67  }
68 }
69 
70 static int all_white(char *bitmap, int w, int h)
71 {
72  return *bitmap == 0 && all_same(bitmap, w, h);
73 }
74 
75 typedef struct {
76  const ProbRange *prob_ranges[XFACE_PIXELS*2];
79 
80 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
81 {
82  if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
83  return -1;
84  pq->prob_ranges[pq->prob_ranges_idx++] = p;
85  return 0;
86 }
87 
88 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
89 {
90  if (w > 3) {
91  w /= 2;
92  h /= 2;
93  push_greys(pq, bitmap, w, h);
94  push_greys(pq, bitmap + w, w, h);
95  push_greys(pq, bitmap + XFACE_WIDTH * h, w, h);
96  push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h);
97  } else {
99  *bitmap +
100  2 * *(bitmap + 1) +
101  4 * *(bitmap + XFACE_WIDTH) +
102  8 * *(bitmap + XFACE_WIDTH + 1);
103  pq_push(pq, p);
104  }
105 }
106 
107 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
108 {
109  if (all_white(bitmap, w, h)) {
111  } else if (all_black(bitmap, w, h)) {
113  push_greys(pq, bitmap, w, h);
114  } else {
116  w /= 2;
117  h /= 2;
118  level++;
119  encode_block(bitmap, w, h, level, pq);
120  encode_block(bitmap + w, w, h, level, pq);
121  encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq);
122  encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq);
123  }
124 }
125 
127 {
128  avctx->coded_frame = avcodec_alloc_frame();
129  if (!avctx->coded_frame)
130  return AVERROR(ENOMEM);
132 
133  return 0;
134 }
135 
136 static void push_integer(BigInt *b, const ProbRange *prange)
137 {
138  uint8_t r;
139 
140  ff_big_div(b, prange->range, &r);
141  ff_big_mul(b, 0);
142  ff_big_add(b, r + prange->offset);
143 }
144 
146  const AVFrame *frame, int *got_packet)
147 {
148  XFaceContext *xface = avctx->priv_data;
149  ProbRangesQueue pq = {{ 0 }, 0};
150  uint8_t bitmap_copy[XFACE_PIXELS];
151  BigInt b = {0};
152  int i, j, k, ret = 0;
153  const uint8_t *buf;
154  uint8_t *p;
155  char intbuf[XFACE_MAX_DIGITS];
156 
157  if (avctx->width || avctx->height) {
158  if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
159  av_log(avctx, AV_LOG_ERROR,
160  "Size value %dx%d not supported, only accepts a size of %dx%d\n",
161  avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT);
162  return AVERROR(EINVAL);
163  }
164  }
165  avctx->width = XFACE_WIDTH;
166  avctx->height = XFACE_HEIGHT;
167 
168  /* convert image from MONOWHITE to 1=black 0=white bitmap */
169  buf = frame->data[0];
170  i = j = 0;
171  do {
172  for (k = 0; k < 8; k++)
173  xface->bitmap[i++] = (buf[j]>>(7-k))&1;
174  if (++j == XFACE_WIDTH/8) {
175  buf += frame->linesize[0];
176  j = 0;
177  }
178  } while (i < XFACE_PIXELS);
179 
180  /* create a copy of bitmap */
181  memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
182  ff_xface_generate_face(xface->bitmap, bitmap_copy);
183 
184  encode_block(xface->bitmap, 16, 16, 0, &pq);
185  encode_block(xface->bitmap + 16, 16, 16, 0, &pq);
186  encode_block(xface->bitmap + 32, 16, 16, 0, &pq);
187  encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq);
188  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq);
189  encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq);
190  encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq);
191  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq);
192  encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq);
193 
194  while (pq.prob_ranges_idx > 0)
196 
197  /* write the inverted big integer in b to intbuf */
198  i = 0;
199  while (b.nb_words) {
200  uint8_t r;
201  ff_big_div(&b, XFACE_PRINTS, &r);
202  intbuf[i++] = r + XFACE_FIRST_PRINT;
203  }
204 
205  if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
206  return ret;
207 
208  /* revert the number, and close the buffer */
209  p = pkt->data;
210  while (--i >= 0)
211  *(p++) = intbuf[i];
212  *(p++) = '\n';
213  *(p++) = 0;
214 
215  pkt->flags |= AV_PKT_FLAG_KEY;
216  *got_packet = 1;
217 
218  return 0;
219 }
220 
222 {
223  av_freep(&avctx->coded_frame);
224 
225  return 0;
226 }
227 
229  .name = "xface",
230  .type = AVMEDIA_TYPE_VIDEO,
231  .id = AV_CODEC_ID_XFACE,
232  .priv_data_size = sizeof(XFaceContext),
235  .encode2 = xface_encode_frame,
236  .pix_fmts = (const enum PixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
237  .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
238 };
static int all_white(char *bitmap, int w, int h)
Definition: xfaceenc.c:70
static int all_same(char *bitmap, int w, int h)
Definition: xfaceenc.c:38
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
int range
Definition: xface.h:87
int max_line_len
max line length for compressed data
Definition: xfaceenc.c:34
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int xface_encode_close(AVCodecContext *avctx)
Definition: xfaceenc.c:221
#define XFACE_WIDTH
Definition: xface.h:30
const ProbRange ff_xface_probranges_per_level[4][3]
Definition: xface.c:124
X-Face common definitions.
output residual component w
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
Definition: xfaceenc.c:107
void ff_xface_generate_face(uint8_t *dst, uint8_t *const src)
Definition: xface.c:281
int prob_ranges_idx
Definition: xfaceenc.c:77
uint8_t
#define av_cold
Definition: attributes.h:78
static AVPacket pkt
Definition: demuxing.c:56
static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: xfaceenc.c:145
#define b
Definition: input.c:42
AVCodec ff_xface_encoder
Definition: xfaceenc.c:228
Definition: xface.h:57
uint8_t * data
void ff_big_mul(BigInt *b, uint8_t a)
Multiply a by b storing the result in b.
Definition: xface.c:90
struct XFaceContext XFaceContext
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
frame
Definition: stft.m:14
Discrete Time axis x
static int all_black(char *bitmap, int w, int h)
Definition: xfaceenc.c:55
static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
Definition: xfaceenc.c:88
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * r
Definition: vf_curves.c:94
void ff_big_add(BigInt *b, uint8_t a)
Add a to b storing the result in b.
Definition: xface.c:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const ProbRange ff_xface_probranges_2x2[16]
Definition: xface.c:132
const char * name
Name of the codec implementation.
external API header
int flags
A combination of AV_PKT_FLAG values.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
static void push_integer(BigInt *b, const ProbRange *prange)
Definition: xfaceenc.c:136
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
#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:51
FIXME Range Coding of cr are level
Definition: snow.txt:367
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
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:126
#define XFACE_FIRST_PRINT
Definition: xface.h:37
for k
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
#define XFACE_PIXELS
Definition: xface.h:32
Describe the class of an AVClass context structure.
Definition: log.h:50
synthesis window for stochastic i
int offset
Definition: xface.h:88
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define XFACE_PRINTS
Definition: xface.h:39
common internal api header.
const ProbRange * prob_ranges[XFACE_PIXELS *2]
Definition: xfaceenc.c:76
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:77
static int pq_push(ProbRangesQueue *pq, const ProbRange *p)
Definition: xfaceenc.c:80
#define XFACE_MAX_DIGITS
Definition: xface.h:47
int nb_words
Definition: xface.h:58
int set_header
set X-Face header in the output
Definition: xfaceenc.c:35
This structure stores compressed data.