2 * Copyright (c) 1990 James Ashton - Sydney University
3 * Copyright (c) 2012 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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
24 * X-Face encoder, based on libcompface, by James Ashton.
29 #include "codec_internal.h"
31 #include "libavutil/avassert.h"
33 typedef struct XFaceContext {
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
40 static int all_same(char *bitmap, int w, int h)
52 bitmap += XFACE_WIDTH;
57 static int all_black(char *bitmap, int w, int h)
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));
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);
72 static int all_white(char *bitmap, int w, int h)
74 return *bitmap == 0 && all_same(bitmap, w, h);
78 ProbRange prob_ranges[XFACE_PIXELS*2];
82 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
84 if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
86 pq->prob_ranges[pq->prob_ranges_idx++] = *p;
90 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
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);
100 const ProbRange *p = ff_xface_probranges_2x2 +
103 4 * *(bitmap + XFACE_WIDTH) +
104 8 * *(bitmap + XFACE_WIDTH + 1);
109 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
111 if (all_white(bitmap, w, h)) {
112 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]);
113 } else if (all_black(bitmap, w, h)) {
114 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]);
115 push_greys(pq, bitmap, w, h);
117 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]);
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);
128 static void push_integer(BigInt *b, const ProbRange *prange)
132 ff_big_div(b, prange->range, &r);
134 ff_big_add(b, r + prange->offset);
137 static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
138 const AVFrame *frame, int *got_packet)
140 XFaceContext *xface = avctx->priv_data;
141 ProbRangesQueue pq = {{{ 0 }}, 0};
142 uint8_t bitmap_copy[XFACE_PIXELS];
144 int i, j, k, ret = 0;
147 char intbuf[XFACE_MAX_DIGITS];
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);
157 avctx->width = XFACE_WIDTH;
158 avctx->height = XFACE_HEIGHT;
160 /* convert image from MONOWHITE to 1=black 0=white bitmap */
161 buf = frame->data[0];
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];
170 } while (i < XFACE_PIXELS);
172 /* create a copy of bitmap */
173 memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
174 ff_xface_generate_face(xface->bitmap, bitmap_copy);
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);
186 while (pq.prob_ranges_idx > 0)
187 push_integer(&b, &pq.prob_ranges[--pq.prob_ranges_idx]);
189 /* write the inverted big integer in b to intbuf */
191 av_assert0(b.nb_words < XFACE_MAX_WORDS);
194 ff_big_div(&b, XFACE_PRINTS, &r);
195 av_assert0(i < sizeof(intbuf));
196 intbuf[i++] = r + XFACE_FIRST_PRINT;
199 if ((ret = ff_get_encode_buffer(avctx, pkt, i + 2, 0)) < 0)
202 /* revert the number, and close the buffer */
214 const FFCodec ff_xface_encoder = {
216 .p.long_name = NULL_IF_CONFIG_SMALL("X-face image"),
217 .p.type = AVMEDIA_TYPE_VIDEO,
218 .p.id = AV_CODEC_ID_XFACE,
219 .p.capabilities = AV_CODEC_CAP_DR1,
220 .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
221 .priv_data_size = sizeof(XFaceContext),
222 FF_CODEC_ENCODE_CB(xface_encode_frame),