Line | Branch | Exec | Source |
---|---|---|---|
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 | 75946 | static int all_same(char *bitmap, int w, int h) | |
41 | { | ||
42 | char val, *row; | ||
43 | int x; | ||
44 | |||
45 | 75946 | val = *bitmap; | |
46 |
2/2✓ Branch 0 taken 124163 times.
✓ Branch 1 taken 26850 times.
|
151013 | while (h--) { |
47 | 124163 | row = bitmap; | |
48 | 124163 | x = w; | |
49 |
2/2✓ Branch 0 taken 284650 times.
✓ Branch 1 taken 75067 times.
|
359717 | while (x--) |
50 |
2/2✓ Branch 0 taken 49096 times.
✓ Branch 1 taken 235554 times.
|
284650 | if (*(row++) != val) |
51 | 49096 | return 0; | |
52 | 75067 | bitmap += XFACE_WIDTH; | |
53 | } | ||
54 | 26850 | return 1; | |
55 | } | ||
56 | |||
57 | 222970 | static int all_black(char *bitmap, int w, int h) | |
58 | { | ||
59 |
2/2✓ Branch 0 taken 53583 times.
✓ Branch 1 taken 169387 times.
|
222970 | if (w > 3) { |
60 | 53583 | w /= 2; | |
61 | 53583 | h /= 2; | |
62 |
4/4✓ Branch 2 taken 30014 times.
✓ Branch 3 taken 6008 times.
✓ Branch 4 taken 23589 times.
✓ Branch 5 taken 6425 times.
|
119619 | return (all_black(bitmap, w, h) && all_black(bitmap + w, w, h) && |
63 |
4/4✓ Branch 0 taken 36022 times.
✓ Branch 1 taken 17561 times.
✓ Branch 3 taken 16920 times.
✓ Branch 4 taken 6669 times.
|
119619 | all_black(bitmap + XFACE_WIDTH * h, w, h) && |
64 | 23589 | all_black(bitmap + XFACE_WIDTH * h + w, w, h)); | |
65 | } else { | ||
66 | /* at least one pixel in the 2x2 grid is non-zero */ | ||
67 |
2/2✓ Branch 0 taken 60564 times.
✓ Branch 1 taken 44853 times.
|
105417 | return *bitmap || *(bitmap + 1) || |
68 |
6/6✓ Branch 0 taken 105417 times.
✓ Branch 1 taken 63970 times.
✓ Branch 2 taken 38518 times.
✓ Branch 3 taken 22046 times.
✓ Branch 4 taken 12315 times.
✓ Branch 5 taken 26203 times.
|
274804 | *(bitmap + XFACE_WIDTH) || *(bitmap + XFACE_WIDTH + 1); |
69 | } | ||
70 | } | ||
71 | |||
72 | 106612 | static int all_white(char *bitmap, int w, int h) | |
73 | { | ||
74 |
4/4✓ Branch 0 taken 75946 times.
✓ Branch 1 taken 30666 times.
✓ Branch 3 taken 26850 times.
✓ Branch 4 taken 49096 times.
|
106612 | return *bitmap == 0 && all_same(bitmap, w, h); |
75 | } | ||
76 | |||
77 | typedef struct { | ||
78 | ProbRange prob_ranges[XFACE_PIXELS*2]; | ||
79 | int prob_ranges_idx; | ||
80 | } ProbRangesQueue; | ||
81 | |||
82 | 193204 | static inline int pq_push(ProbRangesQueue *pq, const ProbRange *p) | |
83 | { | ||
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 193204 times.
|
193204 | if (pq->prob_ranges_idx >= XFACE_PIXELS * 2 - 1) |
85 | ✗ | return -1; | |
86 | 193204 | pq->prob_ranges[pq->prob_ranges_idx++] = *p; | |
87 | 193204 | return 0; | |
88 | } | ||
89 | |||
90 | 97603 | static void push_greys(ProbRangesQueue *pq, char *bitmap, int w, int h) | |
91 | { | ||
92 |
2/2✓ Branch 0 taken 11011 times.
✓ Branch 1 taken 86592 times.
|
97603 | if (w > 3) { |
93 | 11011 | w /= 2; | |
94 | 11011 | h /= 2; | |
95 | 11011 | push_greys(pq, bitmap, w, h); | |
96 | 11011 | push_greys(pq, bitmap + w, w, h); | |
97 | 11011 | push_greys(pq, bitmap + XFACE_WIDTH * h, w, h); | |
98 | 11011 | push_greys(pq, bitmap + XFACE_WIDTH * h + w, w, h); | |
99 | } else { | ||
100 | 86592 | const ProbRange *p = ff_xface_probranges_2x2 + | |
101 | 86592 | *bitmap + | |
102 | 86592 | 2 * *(bitmap + 1) + | |
103 | 86592 | 4 * *(bitmap + XFACE_WIDTH) + | |
104 | 86592 | 8 * *(bitmap + XFACE_WIDTH + 1); | |
105 | 86592 | pq_push(pq, p); | |
106 | } | ||
107 | 97603 | } | |
108 | |||
109 | 106612 | static void encode_block(char *bitmap, int w, int h, int level, ProbRangesQueue *pq) | |
110 | { | ||
111 |
2/2✓ Branch 1 taken 26850 times.
✓ Branch 2 taken 79762 times.
|
106612 | if (all_white(bitmap, w, h)) { |
112 | 26850 | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_WHITE]); | |
113 |
2/2✓ Branch 1 taken 53559 times.
✓ Branch 2 taken 26203 times.
|
79762 | } else if (all_black(bitmap, w, h)) { |
114 | 53559 | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_BLACK]); | |
115 | 53559 | push_greys(pq, bitmap, w, h); | |
116 | } else { | ||
117 | 26203 | pq_push(pq, &ff_xface_probranges_per_level[level][XFACE_COLOR_GREY]); | |
118 | 26203 | w /= 2; | |
119 | 26203 | h /= 2; | |
120 | 26203 | level++; | |
121 | 26203 | encode_block(bitmap, w, h, level, pq); | |
122 | 26203 | encode_block(bitmap + w, w, h, level, pq); | |
123 | 26203 | encode_block(bitmap + h * XFACE_WIDTH, w, h, level, pq); | |
124 | 26203 | encode_block(bitmap + w + h * XFACE_WIDTH, w, h, level, pq); | |
125 | } | ||
126 | 106612 | } | |
127 | |||
128 | 193204 | static void push_integer(BigInt *b, const ProbRange *prange) | |
129 | { | ||
130 | uint8_t r; | ||
131 | |||
132 | 193204 | ff_big_div(b, prange->range, &r); | |
133 | 193204 | ff_big_mul(b, 0); | |
134 | 193204 | ff_big_add(b, r + prange->offset); | |
135 | 193204 | } | |
136 | |||
137 | 200 | static int xface_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
138 | const AVFrame *frame, int *got_packet) | ||
139 | { | ||
140 | 200 | XFaceContext *xface = avctx->priv_data; | |
141 | 200 | ProbRangesQueue pq = {{{ 0 }}, 0}; | |
142 | uint8_t bitmap_copy[XFACE_PIXELS]; | ||
143 | 200 | BigInt b = {0}; | |
144 | 200 | int i, j, k, ret = 0; | |
145 | const uint8_t *buf; | ||
146 | uint8_t *p; | ||
147 | char intbuf[XFACE_MAX_DIGITS]; | ||
148 | |||
149 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
200 | if (avctx->width || avctx->height) { |
150 |
2/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
|
200 | 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); | |
155 | } | ||
156 | } | ||
157 | 200 | avctx->width = XFACE_WIDTH; | |
158 | 200 | avctx->height = XFACE_HEIGHT; | |
159 | |||
160 | /* convert image from MONOWHITE to 1=black 0=white bitmap */ | ||
161 | 200 | buf = frame->data[0]; | |
162 | 200 | i = j = 0; | |
163 | do { | ||
164 |
2/2✓ Branch 0 taken 460800 times.
✓ Branch 1 taken 57600 times.
|
518400 | for (k = 0; k < 8; k++) |
165 | 460800 | xface->bitmap[i++] = (buf[j]>>(7-k))&1; | |
166 |
2/2✓ Branch 0 taken 9600 times.
✓ Branch 1 taken 48000 times.
|
57600 | if (++j == XFACE_WIDTH/8) { |
167 | 9600 | buf += frame->linesize[0]; | |
168 | 9600 | j = 0; | |
169 | } | ||
170 |
2/2✓ Branch 0 taken 57400 times.
✓ Branch 1 taken 200 times.
|
57600 | } while (i < XFACE_PIXELS); |
171 | |||
172 | /* create a copy of bitmap */ | ||
173 | 200 | memcpy(bitmap_copy, xface->bitmap, XFACE_PIXELS); | |
174 | 200 | ff_xface_generate_face(xface->bitmap, bitmap_copy); | |
175 | |||
176 | 200 | encode_block(xface->bitmap, 16, 16, 0, &pq); | |
177 | 200 | encode_block(xface->bitmap + 16, 16, 16, 0, &pq); | |
178 | 200 | encode_block(xface->bitmap + 32, 16, 16, 0, &pq); | |
179 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 16, 16, 16, 0, &pq); | |
180 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 16 + 16, 16, 16, 0, &pq); | |
181 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 16 + 32, 16, 16, 0, &pq); | |
182 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 32, 16, 16, 0, &pq); | |
183 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 32 + 16, 16, 16, 0, &pq); | |
184 | 200 | encode_block(xface->bitmap + XFACE_WIDTH * 32 + 32, 16, 16, 0, &pq); | |
185 | |||
186 |
2/2✓ Branch 0 taken 193204 times.
✓ Branch 1 taken 200 times.
|
193404 | while (pq.prob_ranges_idx > 0) |
187 | 193204 | push_integer(&b, &pq.prob_ranges[--pq.prob_ranges_idx]); | |
188 | |||
189 | /* write the inverted big integer in b to intbuf */ | ||
190 | 200 | i = 0; | |
191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | av_assert0(b.nb_words < XFACE_MAX_WORDS); |
192 |
2/2✓ Branch 0 taken 67510 times.
✓ Branch 1 taken 200 times.
|
67710 | while (b.nb_words) { |
193 | uint8_t r; | ||
194 | 67510 | ff_big_div(&b, XFACE_PRINTS, &r); | |
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67510 times.
|
67510 | av_assert0(i < sizeof(intbuf)); |
196 | 67510 | intbuf[i++] = r + XFACE_FIRST_PRINT; | |
197 | } | ||
198 | |||
199 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
|
200 | if ((ret = ff_get_encode_buffer(avctx, pkt, i + 2, 0)) < 0) |
200 | ✗ | return ret; | |
201 | |||
202 | /* revert the number, and close the buffer */ | ||
203 | 200 | p = pkt->data; | |
204 |
2/2✓ Branch 0 taken 67510 times.
✓ Branch 1 taken 200 times.
|
67710 | while (--i >= 0) |
205 | 67510 | *(p++) = intbuf[i]; | |
206 | 200 | *(p++) = '\n'; | |
207 | 200 | *(p++) = 0; | |
208 | |||
209 | 200 | *got_packet = 1; | |
210 | |||
211 | 200 | return 0; | |
212 | } | ||
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 | .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), | ||
223 | }; | ||
224 |