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.
31 typedef struct XFaceContext {
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
38 static int all_same(char *bitmap, int w, int h)
50 bitmap += XFACE_WIDTH;
55 static int all_black(char *bitmap, int w, int h)
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));
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);
70 static int all_white(char *bitmap, int w, int h)
72 return *bitmap == 0 && all_same(bitmap, w, h);
76 const ProbRange *prob_ranges[XFACE_PIXELS*2];
80 static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p)
82 if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1)
84 pq->prob_ranges[pq->prob_ranges_idx++] = p;
88 static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h)
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);
98 const ProbRange *p = ff_xface_probranges_2x2 +
101 4 * *(bitmap + XFACE_WIDTH) +
102 8 * *(bitmap + XFACE_WIDTH + 1);
107 static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq)
109 if (all_white(bitmap, w, h)) {
110 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]);
111 } else if (all_black(bitmap, w, h)) {
112 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]);
113 push_greys(pq, bitmap, w, h);
115 pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]);
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);
126 static av_cold int xface_encode_init(AVCodecContext *avctx)
128 avctx->coded_frame = av_frame_alloc();
129 if (!avctx->coded_frame)
130 return AVERROR(ENOMEM);
131 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
136 static void push_integer(BigInt *b, const ProbRange *prange)
140 ff_big_div(b, prange->range, &r);
142 ff_big_add(b, r + prange->offset);
145 static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
146 const AVFrame *frame, int *got_packet)
148 XFaceContext *xface = avctx->priv_data;
149 ProbRangesQueue pq = {{ 0 }, 0};
150 uint8_t bitmap_copy[XFACE_PIXELS];
152 int i, j, k, ret = 0;
155 char intbuf[XFACE_MAX_DIGITS];
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);
165 avctx->width = XFACE_WIDTH;
166 avctx->height = XFACE_HEIGHT;
168 /* convert image from MONOWHITE to 1=black 0=white bitmap */
169 buf = frame->data[0];
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];
178 } while (i < XFACE_PIXELS);
180 /* create a copy of bitmap */
181 memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS);
182 ff_xface_generate_face(xface->bitmap, bitmap_copy);
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);
194 while (pq.prob_ranges_idx > 0)
195 push_integer(&b, pq.prob_ranges[--pq.prob_ranges_idx]);
197 /* write the inverted big integer in b to intbuf */
201 ff_big_div(&b, XFACE_PRINTS, &r);
202 intbuf[i++] = r + XFACE_FIRST_PRINT;
205 if ((ret = ff_alloc_packet2(avctx, pkt, i+2)) < 0)
208 /* revert the number, and close the buffer */
215 pkt->flags |= AV_PKT_FLAG_KEY;
221 static av_cold int xface_encode_close(AVCodecContext *avctx)
223 av_freep(&avctx->coded_frame);
228 AVCodec ff_xface_encoder = {
230 .long_name = NULL_IF_CONFIG_SMALL("X-face image"),
231 .type = AVMEDIA_TYPE_VIDEO,
232 .id = AV_CODEC_ID_XFACE,
233 .priv_data_size = sizeof(XFaceContext),
234 .init = xface_encode_init,
235 .close = xface_encode_close,
236 .encode2 = xface_encode_frame,
237 .pix_fmts = (const enum PixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },