/src/ffmpeg/libavcodec/xfaceenc.c
Line | Count | Source (jump to first uncovered line) |
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 "codec_internal.h" |
30 | | #include "encode.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 | 1.40M | { |
42 | 1.40M | char val, *row; |
43 | 1.40M | int x; |
44 | | |
45 | 1.40M | val = *bitmap; |
46 | 3.74M | while (h--) { |
47 | 3.12M | row = bitmap; |
48 | 3.12M | x = w; |
49 | 15.1M | while (x--) |
50 | 12.8M | if (*(row++) != val) |
51 | 782k | return 0; |
52 | 2.34M | bitmap += XFACE_WIDTH; |
53 | 2.34M | } |
54 | 625k | return 1; |
55 | 1.40M | } |
56 | | |
57 | | static int all_black(char *bitmap, int w, int h) |
58 | 3.60M | { |
59 | 3.60M | if (w > 3) { |
60 | 972k | w /= 2; |
61 | 972k | h /= 2; |
62 | 972k | return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) && |
63 | 972k | all_black(bitmap + XFACE_WIDTH * h, w, h) && |
64 | 972k | all_black(bitmap + XFACE_WIDTH * h + w, w, h)); |
65 | 2.63M | } else { |
66 | | /* at least one pixel in the 2x2 grid is non-zero */ |
67 | 2.63M | return *bitmap || *(bitmap + 1) || |
68 | 2.63M | *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1); |
69 | 2.63M | } |
70 | 3.60M | } |
71 | | |
72 | | static int all_white(char *bitmap, int w, int h) |
73 | 1.80M | { |
74 | 1.80M | return *bitmap == 0 && all_same(bitmap, w, h); |
75 | 1.80M | } |
76 | | |
77 | | typedef struct { |
78 | | ProbRange prob_ranges[XFACE_PIXELS*2]; |
79 | | int prob_ranges_idx; |
80 | | } ProbRangesQueue; |
81 | | |
82 | | static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p) |
83 | 3.15M | { |
84 | 3.15M | if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1) |
85 | 0 | return -1; |
86 | 3.15M | pq->prob_ranges[pq->prob_ranges_idx++] = *p; |
87 | 3.15M | return 0; |
88 | 3.15M | } |
89 | | |
90 | | static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h) |
91 | 1.54M | { |
92 | 1.54M | if (w > 3) { |
93 | 201k | w /= 2; |
94 | 201k | h /= 2; |
95 | 201k | push_greys(pq, bitmap, w, h); |
96 | 201k | push_greys(pq, bitmap + w, w, h); |
97 | 201k | push_greys(pq, bitmap + XFACE_WIDTH * h, w, h); |
98 | 201k | push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h); |
99 | 1.34M | } else { |
100 | 1.34M | const ProbRange *p = ff_xface_probranges_2x2 + |
101 | 1.34M | *bitmap + |
102 | 1.34M | 2 * *(bitmap + 1) + |
103 | 1.34M | 4 * *(bitmap + XFACE_WIDTH) + |
104 | 1.34M | 8 * *(bitmap + XFACE_WIDTH + 1); |
105 | 1.34M | pq_push(pq, p); |
106 | 1.34M | } |
107 | 1.54M | } |
108 | | |
109 | | static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq) |
110 | 1.80M | { |
111 | 1.80M | if (all_white(bitmap, w, h)) { |
112 | 625k | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]); |
113 | 1.18M | } else if (all_black(bitmap, w, h)) { |
114 | 743k | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]); |
115 | 743k | push_greys(pq, bitmap, w, h); |
116 | 743k | } else { |
117 | 438k | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]); |
118 | 438k | w /= 2; |
119 | 438k | h /= 2; |
120 | 438k | level++; |
121 | 438k | encode_block(bitmap, w, h, level, pq); |
122 | 438k | encode_block(bitmap + w, w, h, level, pq); |
123 | 438k | encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq); |
124 | 438k | encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq); |
125 | 438k | } |
126 | 1.80M | } |
127 | | |
128 | | static void push_integer(BigInt *b, const ProbRange *prange) |
129 | 3.15M | { |
130 | 3.15M | uint8_t r; |
131 | | |
132 | 3.15M | ff_big_div(b, prange->range, &r); |
133 | 3.15M | ff_big_mul(b, 0); |
134 | 3.15M | ff_big_add(b, r + prange->offset); |
135 | 3.15M | } |
136 | | |
137 | | static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
138 | | const AVFrame *frame, int *got_packet) |
139 | 5.99k | { |
140 | 5.99k | XFaceContext *xface = avctx->priv_data; |
141 | 5.99k | ProbRangesQueue pq = {{{ 0 }}, 0}; |
142 | 5.99k | uint8_t bitmap_copy[XFACE_PIXELS]; |
143 | 5.99k | BigInt b = {0}; |
144 | 5.99k | int i, j, k, ret = 0; |
145 | 5.99k | const uint8_t *buf; |
146 | 5.99k | uint8_t *p; |
147 | 5.99k | char intbuf[XFACE_MAX_DIGITS]; |
148 | | |
149 | 5.99k | if (avctx->width || avctx->height) { |
150 | 5.99k | if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) { |
151 | 124 | av_log(avctx, AV_LOG_ERROR, |
152 | 124 | "Size value %dx%d not supported, only accepts a size of %dx%d\n", |
153 | 124 | avctx->width, avctx->height, XFACE_WIDTH, XFACE_HEIGHT); |
154 | 124 | return AVERROR(EINVAL); |
155 | 124 | } |
156 | 5.99k | } |
157 | 5.86k | avctx->width = XFACE_WIDTH; |
158 | 5.86k | avctx->height = XFACE_HEIGHT; |
159 | | |
160 | | /* convert image from MONOWHITE to 1=black 0=white bitmap */ |
161 | 5.86k | buf = frame->data[0]; |
162 | 5.86k | i = j = 0; |
163 | 1.68M | do { |
164 | 15.2M | for (k = 0; k < 8; k++) |
165 | 13.5M | xface->bitmap[i++] = (buf[j]>>(7-k))&1; |
166 | 1.68M | if (++j == XFACE_WIDTH/8) { |
167 | 281k | buf += frame->linesize[0]; |
168 | 281k | j = 0; |
169 | 281k | } |
170 | 1.68M | } while (i < XFACE_PIXELS); |
171 | | |
172 | | /* create a copy of bitmap */ |
173 | 5.86k | memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS); |
174 | 5.86k | ff_xface_generate_face(xface->bitmap, bitmap_copy); |
175 | | |
176 | 5.86k | encode_block(xface->bitmap, 16, 16, 0, &pq); |
177 | 5.86k | encode_block(xface->bitmap + 16, 16, 16, 0, &pq); |
178 | 5.86k | encode_block(xface->bitmap + 32, 16, 16, 0, &pq); |
179 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq); |
180 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq); |
181 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq); |
182 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq); |
183 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq); |
184 | 5.86k | encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq); |
185 | | |
186 | 3.16M | while (pq.prob_ranges_idx > 0) |
187 | 3.15M | push_integer(&b, &pq.prob_ranges[--pq.prob_ranges_idx]); |
188 | | |
189 | | /* write the inverted big integer in b to intbuf */ |
190 | 5.86k | i = 0; |
191 | 5.86k | av_assert0(b.nb_words < XFACE_MAX_WORDS); |
192 | 1.15M | while (b.nb_words) { |
193 | 1.15M | uint8_t r; |
194 | 1.15M | ff_big_div(&b, XFACE_PRINTS, &r); |
195 | 1.15M | av_assert0(i < sizeof(intbuf)); |
196 | 1.15M | intbuf[i++] = r + XFACE_FIRST_PRINT; |
197 | 1.15M | } |
198 | | |
199 | 5.86k | if ((ret = ff_get_encode_buffer(avctx, pkt, i + 2, 0)) < 0) |
200 | 0 | return ret; |
201 | | |
202 | | /* revert the number, and close the buffer */ |
203 | 5.86k | p = pkt->data; |
204 | 1.15M | while (--i >= 0) |
205 | 1.15M | *(p++) = intbuf[i]; |
206 | 5.86k | *(p++) = '\n'; |
207 | 5.86k | *(p++) = 0; |
208 | | |
209 | 5.86k | *got_packet = 1; |
210 | | |
211 | 5.86k | return 0; |
212 | 5.86k | } |
213 | | |
214 | | const FFCodec ff_xface_encoder = { |
215 | | .p.name = "xface", |
216 | | CODEC_LONG_NAME("X-face image"), |
217 | | .p.type = AVMEDIA_TYPE_VIDEO, |
218 | | .p.id = AV_CODEC_ID_XFACE, |
219 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
220 | | CODEC_PIXFMTS(AV_PIX_FMT_MONOWHITE), |
221 | | .priv_data_size = sizeof(XFaceContext), |
222 | | FF_CODEC_ENCODE_CB(xface_encode_frame), |
223 | | }; |