0
|
1 /*
|
|
2 * BPG encoder
|
|
3 *
|
|
4 * Copyright (c) 2014 Fabrice Bellard
|
|
5 *
|
|
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7 * of this software and associated documentation files (the "Software"), to deal
|
|
8 * in the Software without restriction, including without limitation the rights
|
|
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 * copies of the Software, and to permit persons to whom the Software is
|
|
11 * furnished to do so, subject to the following conditions:
|
|
12 *
|
|
13 * The above copyright notice and this permission notice shall be included in
|
|
14 * all copies or substantial portions of the Software.
|
|
15 *
|
|
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22 * THE SOFTWARE.
|
|
23 */
|
|
24 #include <stdlib.h>
|
|
25 #include <stdio.h>
|
|
26 #include <string.h>
|
|
27 #include <inttypes.h>
|
|
28 #include <getopt.h>
|
|
29 #include <math.h>
|
|
30 #include <assert.h>
|
|
31
|
|
32 #include <png.h>
|
|
33 #include <jpeglib.h>
|
|
34
|
|
35 #include "bpgenc.h"
|
|
36
|
|
37 typedef uint16_t PIXEL;
|
|
38
|
|
39 static void put_ue(uint8_t **pp, uint32_t v);
|
|
40
|
|
41 static inline int clamp_pix(int a, int pixel_max)
|
|
42 {
|
|
43 if (a < 0)
|
|
44 return 0;
|
|
45 else if (a > pixel_max)
|
|
46 return pixel_max;
|
|
47 else
|
|
48 return a;
|
|
49 }
|
|
50
|
|
51 static inline int sub_mod_int(int a, int b, int m)
|
|
52 {
|
|
53 a -= b;
|
|
54 if (a < 0)
|
|
55 a += m;
|
|
56 return a;
|
|
57 }
|
|
58
|
|
59 static inline int add_mod_int(int a, int b, int m)
|
|
60 {
|
|
61 a += b;
|
|
62 if (a >= m)
|
|
63 a -= m;
|
|
64 return a;
|
|
65 }
|
|
66
|
|
67 typedef struct {
|
|
68 int c_shift;
|
|
69 int c_rnd;
|
|
70 int c_0_25, c_0_5, c_one;
|
|
71 int rgb_to_ycc[3 * 3];
|
|
72 int y_one;
|
|
73 int y_offset;
|
|
74 int bit_depth;
|
|
75 int pixel_max;
|
|
76 int c_center;
|
|
77 } ColorConvertState;
|
|
78
|
|
79 static void convert_init(ColorConvertState *s, int in_bit_depth,
|
|
80 int out_bit_depth, BPGColorSpaceEnum color_space,
|
|
81 int limited_range)
|
|
82 {
|
|
83 double k_r, k_b, mult, mult_y, mult_c;
|
|
84 int in_pixel_max, out_pixel_max, c_shift, i;
|
|
85 double rgb_to_ycc[3 * 3];
|
|
86
|
|
87 /* XXX: could use one more bit */
|
|
88 c_shift = 31 - out_bit_depth;
|
|
89 in_pixel_max = (1 << in_bit_depth) - 1;
|
|
90 out_pixel_max = (1 << out_bit_depth) - 1;
|
|
91 mult = (double)out_pixel_max * (1 << c_shift) / (double)in_pixel_max;
|
|
92 // printf("mult=%f c_shift=%d\n", mult, c_shift);
|
|
93 if (limited_range) {
|
|
94 mult_y = (double)(219 << (out_bit_depth - 8)) * (1 << c_shift) /
|
|
95 (double)in_pixel_max;
|
|
96 mult_c = (double)(224 << (out_bit_depth - 8)) * (1 << c_shift) /
|
|
97 (double)in_pixel_max;
|
|
98 } else {
|
|
99 mult_y = mult;
|
|
100 mult_c = mult;
|
|
101 }
|
|
102 switch(color_space) {
|
|
103 case BPG_CS_YCbCr:
|
|
104 k_r = 0.299;
|
|
105 k_b = 0.114;
|
|
106 goto convert_ycc;
|
|
107
|
|
108 case BPG_CS_YCbCr_BT709:
|
|
109 k_r = 0.2126;
|
|
110 k_b = 0.0722;
|
|
111 goto convert_ycc;
|
|
112
|
|
113 case BPG_CS_YCbCr_BT2020:
|
|
114 k_r = 0.2627;
|
|
115 k_b = 0.0593;
|
|
116 convert_ycc:
|
|
117 rgb_to_ycc[0] = k_r;
|
|
118 rgb_to_ycc[1] = 1 - k_r - k_b;
|
|
119 rgb_to_ycc[2] = k_b;
|
|
120 rgb_to_ycc[3] = -0.5 * k_r / (1 - k_b);
|
|
121 rgb_to_ycc[4] = -0.5 * (1 - k_r - k_b) / (1 - k_b);
|
|
122 rgb_to_ycc[5] = 0.5;
|
|
123 rgb_to_ycc[6] = 0.5;
|
|
124 rgb_to_ycc[7] = -0.5 * (1 - k_r - k_b) / (1 - k_r);
|
|
125 rgb_to_ycc[8] = -0.5 * k_b / (1 - k_r);
|
|
126
|
|
127 for(i = 0; i < 3; i++)
|
|
128 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_y);
|
|
129 for(i = 3; i < 9; i++)
|
|
130 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_c);
|
|
131 break;
|
|
132 case BPG_CS_YCgCo:
|
|
133 s->c_0_25 = lrint(0.25 * mult_y);
|
|
134 s->c_0_5 = lrint(0.5 * mult_y);
|
|
135 break;
|
|
136 default:
|
|
137 break;
|
|
138 }
|
|
139
|
|
140 s->c_one = lrint(mult);
|
|
141 s->c_shift = c_shift;
|
|
142 s->c_rnd = (1 << (c_shift - 1));
|
|
143 if (limited_range) {
|
|
144 s->y_offset = s->c_rnd + (16 << (c_shift + out_bit_depth - 8));
|
|
145 s->y_one = lrint(mult_y);
|
|
146 } else {
|
|
147 s->y_offset = s->c_rnd;
|
|
148 s->y_one = s->c_one;
|
|
149 }
|
|
150
|
|
151 s->bit_depth = out_bit_depth;
|
|
152 s->c_center = 1 << (out_bit_depth - 1);
|
|
153 s->pixel_max = out_pixel_max;
|
|
154 }
|
|
155
|
|
156 /* 8 bit input */
|
|
157 static void rgb24_to_ycc(ColorConvertState *s,
|
|
158 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
159 const void *src1, int n, int incr)
|
|
160 {
|
|
161 const uint8_t *src = src1;
|
|
162 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
|
|
163 int pixel_max, y_offset;
|
|
164
|
|
165 c0 = s->rgb_to_ycc[0];
|
|
166 c1 = s->rgb_to_ycc[1];
|
|
167 c2 = s->rgb_to_ycc[2];
|
|
168 c3 = s->rgb_to_ycc[3];
|
|
169 c4 = s->rgb_to_ycc[4];
|
|
170 c5 = s->rgb_to_ycc[5];
|
|
171 c6 = s->rgb_to_ycc[6];
|
|
172 c7 = s->rgb_to_ycc[7];
|
|
173 c8 = s->rgb_to_ycc[8];
|
|
174 shift = s->c_shift;
|
|
175 rnd = s->c_rnd;
|
|
176 y_offset = s->y_offset;
|
|
177 center = s->c_center;
|
|
178 pixel_max = s->pixel_max;
|
|
179 for(i = 0; i < n; i++) {
|
|
180 r = src[0];
|
|
181 g = src[1];
|
|
182 b = src[2];
|
|
183 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
|
|
184 y_offset) >> shift, pixel_max);
|
|
185 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
|
|
186 rnd) >> shift) + center, pixel_max);
|
|
187 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
|
|
188 rnd) >> shift) + center, pixel_max);
|
|
189 src += incr;
|
|
190 }
|
|
191 }
|
|
192
|
|
193 static void rgb24_to_rgb(ColorConvertState *s,
|
|
194 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
195 const void *src1, int n, int incr)
|
|
196 {
|
|
197 const uint8_t *src = src1;
|
|
198 int i, r, g, b, c, shift, rnd;
|
|
199
|
|
200 c = s->y_one;
|
|
201 shift = s->c_shift;
|
|
202 rnd = s->y_offset;
|
|
203 for(i = 0; i < n; i++) {
|
|
204 r = src[0];
|
|
205 g = src[1];
|
|
206 b = src[2];
|
|
207 y_ptr[i] = (c * g + rnd) >> shift;
|
|
208 cb_ptr[i] = (c * b + rnd) >> shift;
|
|
209 cr_ptr[i] = (c * r + rnd) >> shift;
|
|
210 src += incr;
|
|
211 }
|
|
212 }
|
|
213
|
|
214 static void rgb24_to_ycgco(ColorConvertState *s,
|
|
215 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
216 const void *src1, int n, int incr)
|
|
217 {
|
|
218 const uint8_t *src = src1;
|
|
219 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
|
|
220 int y_offset;
|
|
221
|
|
222 c_0_25 = s->c_0_25;
|
|
223 c_0_5 = s->c_0_5;
|
|
224 rnd = s->c_rnd;
|
|
225 shift = s->c_shift;
|
|
226 pixel_max = s->pixel_max;
|
|
227 center = s->c_center;
|
|
228 y_offset = s->y_offset;
|
|
229 for(i = 0; i < n; i++) {
|
|
230 r = src[0];
|
|
231 g = src[1];
|
|
232 b = src[2];
|
|
233 t1 = c_0_5 * g;
|
|
234 t2 = c_0_25 * (r + b);
|
|
235 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
|
|
236 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
|
|
237 pixel_max);
|
|
238 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
|
|
239 rnd) >> shift) + center, pixel_max);
|
|
240 src += incr;
|
|
241 }
|
|
242 }
|
|
243
|
|
244 /* Note: used for alpha/W so no limited range */
|
|
245 static void gray8_to_gray(ColorConvertState *s,
|
|
246 PIXEL *y_ptr, const uint8_t *src, int n, int incr)
|
|
247 {
|
|
248 int i, g, c, shift, rnd;
|
|
249
|
|
250 c = s->c_one;
|
|
251 shift = s->c_shift;
|
|
252 rnd = s->c_rnd;
|
|
253 for(i = 0; i < n; i++) {
|
|
254 g = src[0];
|
|
255 y_ptr[i] = (c * g + rnd) >> shift;
|
|
256 src += incr;
|
|
257 }
|
|
258 }
|
|
259
|
|
260 static void luma8_to_gray(ColorConvertState *s,
|
|
261 PIXEL *y_ptr, const uint8_t *src, int n, int incr)
|
|
262 {
|
|
263 int i, g, c, shift, rnd;
|
|
264
|
|
265 c = s->y_one;
|
|
266 shift = s->c_shift;
|
|
267 rnd = s->y_offset;
|
|
268 for(i = 0; i < n; i++) {
|
|
269 g = src[0];
|
|
270 y_ptr[i] = (c * g + rnd) >> shift;
|
|
271 src += incr;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 /* 16 bit input */
|
|
276
|
|
277 static void rgb48_to_ycc(ColorConvertState *s,
|
|
278 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
279 const void *src1, int n, int incr)
|
|
280 {
|
|
281 const uint16_t *src = src1;
|
|
282 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
|
|
283 int pixel_max, y_offset;
|
|
284
|
|
285 c0 = s->rgb_to_ycc[0];
|
|
286 c1 = s->rgb_to_ycc[1];
|
|
287 c2 = s->rgb_to_ycc[2];
|
|
288 c3 = s->rgb_to_ycc[3];
|
|
289 c4 = s->rgb_to_ycc[4];
|
|
290 c5 = s->rgb_to_ycc[5];
|
|
291 c6 = s->rgb_to_ycc[6];
|
|
292 c7 = s->rgb_to_ycc[7];
|
|
293 c8 = s->rgb_to_ycc[8];
|
|
294 shift = s->c_shift;
|
|
295 rnd = s->c_rnd;
|
|
296 y_offset = s->y_offset;
|
|
297 center = s->c_center;
|
|
298 pixel_max = s->pixel_max;
|
|
299 for(i = 0; i < n; i++) {
|
|
300 r = src[0];
|
|
301 g = src[1];
|
|
302 b = src[2];
|
|
303 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
|
|
304 y_offset) >> shift, pixel_max);
|
|
305 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
|
|
306 rnd) >> shift) + center, pixel_max);
|
|
307 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
|
|
308 rnd) >> shift) + center, pixel_max);
|
|
309 src += incr;
|
|
310 }
|
|
311 }
|
|
312
|
|
313 static void rgb48_to_ycgco(ColorConvertState *s,
|
|
314 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
315 const void *src1, int n, int incr)
|
|
316 {
|
|
317 const uint16_t *src = src1;
|
|
318 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
|
|
319 int y_offset;
|
|
320
|
|
321 c_0_25 = s->c_0_25;
|
|
322 c_0_5 = s->c_0_5;
|
|
323 rnd = s->c_rnd;
|
|
324 y_offset = s->y_offset;
|
|
325 shift = s->c_shift;
|
|
326 pixel_max = s->pixel_max;
|
|
327 center = s->c_center;
|
|
328 for(i = 0; i < n; i++) {
|
|
329 r = src[0];
|
|
330 g = src[1];
|
|
331 b = src[2];
|
|
332 t1 = c_0_5 * g;
|
|
333 t2 = c_0_25 * (r + b);
|
|
334 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
|
|
335 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
|
|
336 pixel_max);
|
|
337 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
|
|
338 rnd) >> shift) + center, pixel_max);
|
|
339 src += incr;
|
|
340 }
|
|
341 }
|
|
342
|
|
343 /* Note: use for alpha/W so no limited range */
|
|
344 static void gray16_to_gray(ColorConvertState *s,
|
|
345 PIXEL *y_ptr, const uint16_t *src, int n, int incr)
|
|
346 {
|
|
347 int i, g, c, shift, rnd;
|
|
348
|
|
349 c = s->c_one;
|
|
350 shift = s->c_shift;
|
|
351 rnd = s->c_rnd;
|
|
352 for(i = 0; i < n; i++) {
|
|
353 g = src[0];
|
|
354 y_ptr[i] = (c * g + rnd) >> shift;
|
|
355 src += incr;
|
|
356 }
|
|
357 }
|
|
358
|
|
359 static void luma16_to_gray(ColorConvertState *s,
|
|
360 PIXEL *y_ptr, const uint16_t *src, int n, int incr)
|
|
361 {
|
|
362 int i, g, c, shift, rnd;
|
|
363
|
|
364 c = s->y_one;
|
|
365 shift = s->c_shift;
|
|
366 rnd = s->y_offset;
|
|
367 for(i = 0; i < n; i++) {
|
|
368 g = src[0];
|
|
369 y_ptr[i] = (c * g + rnd) >> shift;
|
|
370 src += incr;
|
|
371 }
|
|
372 }
|
|
373
|
|
374 static void rgb48_to_rgb(ColorConvertState *s,
|
|
375 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
376 const void *src1, int n, int incr)
|
|
377 {
|
|
378 const uint16_t *src = src1;
|
|
379
|
|
380 luma16_to_gray(s, y_ptr, src + 1, n, incr);
|
|
381 luma16_to_gray(s, cb_ptr, src + 2, n, incr);
|
|
382 luma16_to_gray(s, cr_ptr, src + 0, n, incr);
|
|
383 }
|
|
384
|
|
385 typedef void RGBConvertFunc(ColorConvertState *s,
|
|
386 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
|
|
387 const void *src, int n, int incr);
|
|
388
|
|
389 static RGBConvertFunc *rgb_to_cs[2][BPG_CS_COUNT] = {
|
|
390 {
|
|
391 rgb24_to_ycc,
|
|
392 rgb24_to_rgb,
|
|
393 rgb24_to_ycgco,
|
|
394 rgb24_to_ycc,
|
|
395 rgb24_to_ycc,
|
|
396 },
|
|
397 {
|
|
398 rgb48_to_ycc,
|
|
399 rgb48_to_rgb,
|
|
400 rgb48_to_ycgco,
|
|
401 rgb48_to_ycc,
|
|
402 rgb48_to_ycc,
|
|
403 }
|
|
404 };
|
|
405
|
|
406 /* val = 1.0 - val */
|
|
407 static void gray_one_minus(ColorConvertState *s, PIXEL *y_ptr, int n)
|
|
408 {
|
|
409 int pixel_max = s->pixel_max;
|
|
410 int i;
|
|
411
|
|
412 for(i = 0; i < n; i++) {
|
|
413 y_ptr[i] = pixel_max - y_ptr[i];
|
|
414 }
|
|
415 }
|
|
416
|
|
417 /* val = -val for chroma */
|
|
418 static void gray_neg_c(ColorConvertState *s, PIXEL *y_ptr, int n)
|
|
419 {
|
|
420 int pixel_max = s->pixel_max;
|
|
421 int i, v;
|
|
422
|
|
423 for(i = 0; i < n; i++) {
|
|
424 v = y_ptr[i];
|
|
425 if (v == 0)
|
|
426 v = pixel_max;
|
|
427 else
|
|
428 v = pixel_max + 1 - v;
|
|
429 y_ptr[i] = v;
|
|
430 }
|
|
431 }
|
|
432
|
|
433
|
|
434 /* decimation */
|
|
435
|
|
436 /* phase = 0 */
|
|
437 #define DP0TAPS2 7
|
|
438 #define DP0TAPS (2 * DP0TAPS + 1)
|
|
439 #define DP0C0 64
|
|
440 #define DP0C1 40
|
|
441 #define DP0C3 (-11)
|
|
442 #define DP0C5 4
|
|
443 #define DP0C7 (-1)
|
|
444
|
|
445 /* phase = 0.5 */
|
|
446 #define DP1TAPS2 5
|
|
447 #define DP1TAPS (2 * DP1TAPS2)
|
|
448 #define DP1C0 57
|
|
449 #define DP1C1 17
|
|
450 #define DP1C2 (-8)
|
|
451 #define DP1C3 (-4)
|
|
452 #define DP1C4 2
|
|
453
|
|
454 #define DTAPS_MAX 7
|
|
455
|
|
456 /* chroma aligned with luma samples */
|
|
457 static void decimate2p0_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
|
|
458 {
|
|
459 int n2, i, pixel_max;
|
|
460 pixel_max = (1 << bit_depth) - 1;
|
|
461 n2 = (n + 1) / 2;
|
|
462 for(i = 0; i < n2; i++) {
|
|
463 dst[i] = clamp_pix(((src[-7] + src[7]) * DP0C7 +
|
|
464 (src[-5] + src[5]) * DP0C5 +
|
|
465 (src[-3] + src[3]) * DP0C3 +
|
|
466 (src[-1] + src[1]) * DP0C1 +
|
|
467 src[0] * DP0C0 + 64) >> 7, pixel_max);
|
|
468 src += 2;
|
|
469 }
|
|
470 }
|
|
471
|
|
472 /* same with more precision and no saturation */
|
|
473 static void decimate2p0_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
|
|
474 {
|
|
475 int n2, i, shift, rnd;
|
|
476 shift = bit_depth - 7;
|
|
477 rnd = 1 << (shift - 1);
|
|
478 n2 = (n + 1) / 2;
|
|
479 for(i = 0; i < n2; i++) {
|
|
480 dst[i] = ((src[-7] + src[7]) * DP0C7 +
|
|
481 (src[-5] + src[5]) * DP0C5 +
|
|
482 (src[-3] + src[3]) * DP0C3 +
|
|
483 (src[-1] + src[1]) * DP0C1 +
|
|
484 src[0] * DP0C0 + rnd) >> shift;
|
|
485 src += 2;
|
|
486 }
|
|
487 }
|
|
488
|
|
489
|
|
490 /* chroma half way between luma samples */
|
|
491 static void decimate2p1_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
|
|
492 {
|
|
493 int n2, i, pixel_max;
|
|
494 pixel_max = (1 << bit_depth) - 1;
|
|
495 n2 = (n + 1) / 2;
|
|
496 for(i = 0; i < n2; i++) {
|
|
497 dst[i] = clamp_pix(((src[-4] + src[5]) * DP1C4 +
|
|
498 (src[-3] + src[4]) * DP1C3 +
|
|
499 (src[-2] + src[3]) * DP1C2 +
|
|
500 (src[-1] + src[2]) * DP1C1 +
|
|
501 (src[0] + src[1]) * DP1C0 + 64) >> 7, pixel_max);
|
|
502 src += 2;
|
|
503 }
|
|
504 }
|
|
505
|
|
506 /* same with more precision and no saturation */
|
|
507 static void decimate2p1_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
|
|
508 {
|
|
509 int n2, i, shift, rnd;
|
|
510 shift = bit_depth - 7;
|
|
511 rnd = 1 << (shift - 1);
|
|
512 n2 = (n + 1) / 2;
|
|
513 for(i = 0; i < n2; i++) {
|
|
514 dst[i] = ((src[-4] + src[5]) * DP1C4 +
|
|
515 (src[-3] + src[4]) * DP1C3 +
|
|
516 (src[-2] + src[3]) * DP1C2 +
|
|
517 (src[-1] + src[2]) * DP1C1 +
|
|
518 (src[0] + src[1]) * DP1C0 + rnd) >> shift;
|
|
519 src += 2;
|
|
520 }
|
|
521 }
|
|
522
|
|
523 static void decimate2_h(PIXEL *dst, PIXEL *src, int n, int bit_depth, int phase)
|
|
524 {
|
|
525 PIXEL *src1, v;
|
|
526 int d, i;
|
|
527
|
|
528 if (phase == 0)
|
|
529 d = DP0TAPS2;
|
|
530 else
|
|
531 d = DP1TAPS2;
|
|
532 /* add edge pixels */
|
|
533 src1 = malloc(sizeof(PIXEL) * (n + 2 * d));
|
|
534 v = src[0];
|
|
535 for(i = 0; i < d; i++)
|
|
536 src1[i] = v;
|
|
537 memcpy(src1 + d, src, n * sizeof(PIXEL));
|
|
538 v = src[n - 1];
|
|
539 for(i = 0; i < d; i++)
|
|
540 src1[d + n + i] = v;
|
|
541 if (phase == 0)
|
|
542 decimate2p0_simple(dst, src1 + d, n, bit_depth);
|
|
543 else
|
|
544 decimate2p1_simple(dst, src1 + d, n, bit_depth);
|
|
545 free(src1);
|
|
546 }
|
|
547
|
|
548 /* src1 is a temporary buffer of length n + 2 * DTAPS */
|
|
549 static void decimate2_h16(int16_t *dst, PIXEL *src, int n, PIXEL *src1,
|
|
550 int bit_depth, int phase)
|
|
551 {
|
|
552 PIXEL v;
|
|
553 int d, i;
|
|
554
|
|
555 if (phase == 0)
|
|
556 d = DP0TAPS2;
|
|
557 else
|
|
558 d = DP1TAPS2;
|
|
559 /* add edge pixels */
|
|
560 v = src[0];
|
|
561 for(i = 0; i < d; i++)
|
|
562 src1[i] = v;
|
|
563 memcpy(src1 + d, src, n * sizeof(PIXEL));
|
|
564 v = src[n - 1];
|
|
565 for(i = 0; i < d; i++)
|
|
566 src1[d + n + i] = v;
|
|
567 if (phase == 0)
|
|
568 decimate2p0_simple16(dst, src1 + d, n, bit_depth);
|
|
569 else
|
|
570 decimate2p1_simple16(dst, src1 + d, n, bit_depth);
|
|
571
|
|
572 }
|
|
573
|
|
574 static void decimate2_v(PIXEL *dst, int16_t **src, int pos, int n,
|
|
575 int bit_depth)
|
|
576 {
|
|
577 int16_t *src0, *src1, *src2, *src3, *src4, *src5, *srcm1, *srcm2, *srcm3, *srcm4;
|
|
578 int i, shift, offset, pixel_max;
|
|
579
|
|
580 pos = sub_mod_int(pos, 4, DP1TAPS);
|
|
581 srcm4 = src[pos];
|
|
582 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
583 srcm3 = src[pos];
|
|
584 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
585 srcm2 = src[pos];
|
|
586 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
587 srcm1 = src[pos];
|
|
588 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
589 src0 = src[pos];
|
|
590 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
591 src1 = src[pos];
|
|
592 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
593 src2 = src[pos];
|
|
594 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
595 src3 = src[pos];
|
|
596 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
597 src4 = src[pos];
|
|
598 pos = add_mod_int(pos, 1, DP1TAPS);
|
|
599 src5 = src[pos];
|
|
600
|
|
601 shift = 21 - bit_depth;
|
|
602 offset = 1 << (shift - 1);
|
|
603 pixel_max = (1 << bit_depth) - 1;
|
|
604 for(i = 0; i < n; i++) {
|
|
605 dst[i] = clamp_pix(((srcm4[i] + src5[i]) * DP1C4 +
|
|
606 (srcm3[i] + src4[i]) * DP1C3 +
|
|
607 (srcm2[i] + src3[i]) * DP1C2 +
|
|
608 (srcm1[i] + src2[i]) * DP1C1 +
|
|
609 (src0[i] + src1[i]) * DP1C0 + offset) >> shift, pixel_max);
|
|
610 }
|
|
611 }
|
|
612
|
|
613 /* Note: we do the horizontal decimation first to use less CPU cache */
|
|
614 static void decimate2_hv(uint8_t *dst, int dst_linesize,
|
|
615 uint8_t *src, int src_linesize,
|
|
616 int w, int h, int bit_depth, int h_phase)
|
|
617 {
|
|
618 PIXEL *buf1;
|
|
619 int16_t *buf2[DP1TAPS];
|
|
620 int w2, pos, i, y, y1, y2;
|
|
621
|
|
622 w2 = (w + 1) / 2;
|
|
623
|
|
624 buf1 = malloc(sizeof(PIXEL) * (w + 2 * DTAPS_MAX));
|
|
625 /* init line buffer */
|
|
626 for(i = 0; i < DP1TAPS; i++) {
|
|
627 buf2[i] = malloc(sizeof(int16_t) * w2);
|
|
628 y = i;
|
|
629 if (y > DP1TAPS2)
|
|
630 y -= DP1TAPS;
|
|
631 if (y < 0) {
|
|
632 /* copy from first line */
|
|
633 memcpy(buf2[i], buf2[0], sizeof(int16_t) * w2);
|
|
634 } else if (y >= h) {
|
|
635 /* copy from last line (only happens for small height) */
|
|
636 memcpy(buf2[i], buf2[h - 1], sizeof(int16_t) * w2);
|
|
637 } else {
|
|
638 decimate2_h16(buf2[i], (PIXEL *)(src + src_linesize * y), w,
|
|
639 buf1, bit_depth, h_phase);
|
|
640 }
|
|
641 }
|
|
642
|
|
643 for(y = 0; y < h; y++) {
|
|
644 pos = y % DP1TAPS;
|
|
645 if ((y & 1) == 0) {
|
|
646 /* filter one line */
|
|
647 y2 = y >> 1;
|
|
648 decimate2_v((PIXEL *)(dst + y2 * dst_linesize), buf2,
|
|
649 pos, w2, bit_depth);
|
|
650 }
|
|
651 /* add a new line in the buffer */
|
|
652 y1 = y + DP1TAPS2 + 1;
|
|
653 pos = add_mod_int(pos, DP1TAPS2 + 1, DP1TAPS);
|
|
654 if (y1 >= h) {
|
|
655 /* copy last line */
|
|
656 memcpy(buf2[pos], buf2[sub_mod_int(pos, 1, DP1TAPS)],
|
|
657 sizeof(int16_t) * w2);
|
|
658 } else {
|
|
659 /* horizontally decimate new line */
|
|
660 decimate2_h16(buf2[pos], (PIXEL *)(src + src_linesize * y1), w,
|
|
661 buf1, bit_depth, h_phase);
|
|
662 }
|
|
663 }
|
|
664
|
|
665 for(i = 0; i < DP1TAPS; i++)
|
|
666 free(buf2[i]);
|
|
667 free(buf1);
|
|
668 }
|
|
669
|
|
670 static void get_plane_res(Image *img, int *pw, int *ph, int i)
|
|
671 {
|
|
672 if (img->format == BPG_FORMAT_420 && (i == 1 || i == 2)) {
|
|
673 *pw = (img->w + 1) / 2;
|
|
674 *ph = (img->h + 1) / 2;
|
|
675 } else if (img->format == BPG_FORMAT_422 && (i == 1 || i == 2)) {
|
|
676 *pw = (img->w + 1) / 2;
|
|
677 *ph = img->h;
|
|
678 } else {
|
|
679 *pw = img->w;
|
|
680 *ph = img->h;
|
|
681 }
|
|
682 }
|
|
683
|
|
684 #define W_PAD 16
|
|
685
|
|
686 Image *image_alloc(int w, int h, BPGImageFormatEnum format, int has_alpha,
|
|
687 BPGColorSpaceEnum color_space, int bit_depth)
|
|
688 {
|
|
689 Image *img;
|
|
690 int i, linesize, w1, h1, c_count;
|
|
691
|
|
692 img = malloc(sizeof(Image));
|
|
693 memset(img, 0, sizeof(*img));
|
|
694
|
|
695 img->w = w;
|
|
696 img->h = h;
|
|
697 img->format = format;
|
|
698 img->has_alpha = has_alpha;
|
|
699 img->bit_depth = bit_depth;
|
|
700 img->color_space = color_space;
|
|
701 img->pixel_shift = 1;
|
|
702 img->c_h_phase = 1;
|
|
703
|
|
704 if (img->format == BPG_FORMAT_GRAY)
|
|
705 c_count = 1;
|
|
706 else
|
|
707 c_count = 3;
|
|
708 if (has_alpha)
|
|
709 c_count++;
|
|
710 for(i = 0; i < c_count; i++) {
|
|
711 get_plane_res(img, &w1, &h1, i);
|
|
712 /* multiple of 16 pixels to add borders */
|
|
713 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
714 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
715
|
|
716 linesize = w1 << img->pixel_shift;
|
|
717 img->data[i] = malloc(linesize * h1);
|
|
718 img->linesize[i] = linesize;
|
|
719 }
|
|
720 return img;
|
|
721 }
|
|
722
|
|
723 void image_free(Image *img)
|
|
724 {
|
|
725 int i, c_count;
|
|
726 if (img->format == BPG_FORMAT_GRAY)
|
|
727 c_count = 1;
|
|
728 else
|
|
729 c_count = 3;
|
|
730 if (img->has_alpha)
|
|
731 c_count++;
|
|
732 for(i = 0; i < c_count; i++)
|
|
733 free(img->data[i]);
|
|
734 free(img);
|
|
735 }
|
|
736
|
|
737 int image_ycc444_to_ycc422(Image *img, int h_phase)
|
|
738 {
|
|
739 uint8_t *data1;
|
|
740 int w1, h1, bpp, linesize1, i, y;
|
|
741
|
|
742 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
|
|
743 return -1;
|
|
744 bpp = 2;
|
|
745 w1 = (img->w + 1) / 2;
|
|
746 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
747 h1 = (img->h + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
748 linesize1 = bpp * w1;
|
|
749 for(i = 1; i <= 2; i++) {
|
|
750 data1 = malloc(linesize1 * h1);
|
|
751 for(y = 0; y < img->h; y++) {
|
|
752 decimate2_h((PIXEL *)(data1 + y * linesize1),
|
|
753 (PIXEL *)(img->data[i] + y * img->linesize[i]),
|
|
754 img->w, img->bit_depth, h_phase);
|
|
755 }
|
|
756 free(img->data[i]);
|
|
757 img->data[i] = data1;
|
|
758 img->linesize[i] = linesize1;
|
|
759 }
|
|
760 img->format = BPG_FORMAT_422;
|
|
761 img->c_h_phase = h_phase;
|
|
762 return 0;
|
|
763 }
|
|
764
|
|
765 int image_ycc444_to_ycc420(Image *img, int h_phase)
|
|
766 {
|
|
767 uint8_t *data1;
|
|
768 int w1, h1, bpp, linesize1, i;
|
|
769
|
|
770 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
|
|
771 return -1;
|
|
772 bpp = 2;
|
|
773 w1 = (img->w + 1) / 2;
|
|
774 h1 = (img->h + 1) / 2;
|
|
775 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
776 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
|
|
777 linesize1 = bpp * w1;
|
|
778 for(i = 1; i <= 2; i++) {
|
|
779 data1 = malloc(linesize1 * h1);
|
|
780 decimate2_hv(data1, linesize1,
|
|
781 img->data[i], img->linesize[i],
|
|
782 img->w, img->h, img->bit_depth, h_phase);
|
|
783 free(img->data[i]);
|
|
784 img->data[i] = data1;
|
|
785 img->linesize[i] = linesize1;
|
|
786 }
|
|
787 img->format = BPG_FORMAT_420;
|
|
788 img->c_h_phase = h_phase;
|
|
789 return 0;
|
|
790 }
|
|
791
|
|
792 /* duplicate right and bottom samples so that the image has a width
|
|
793 and height multiple of cb_size (power of two) */
|
|
794 void image_pad(Image *img, int cb_size)
|
|
795 {
|
|
796 int w1, h1, x, y, c_count, c_w, c_h, c_w1, c_h1, h_shift, v_shift, c_idx;
|
|
797 PIXEL *ptr, v, *ptr1;
|
|
798
|
|
799 assert(img->pixel_shift == 1);
|
|
800 if (cb_size <= 1)
|
|
801 return;
|
|
802 w1 = (img->w + cb_size - 1) & ~(cb_size - 1);
|
|
803 h1 = (img->h + cb_size - 1) & ~(cb_size - 1);
|
|
804
|
|
805 if (img->format == BPG_FORMAT_GRAY)
|
|
806 c_count = 1;
|
|
807 else
|
|
808 c_count = 3;
|
|
809 if (img->has_alpha)
|
|
810 c_count++;
|
|
811 for(c_idx = 0; c_idx < c_count; c_idx++) {
|
|
812 if (img->format == BPG_FORMAT_420 &&
|
|
813 (c_idx == 1 || c_idx == 2)) {
|
|
814 h_shift = 1;
|
|
815 v_shift = 1;
|
|
816 } else if (img->format == BPG_FORMAT_422 &&
|
|
817 (c_idx == 1 || c_idx == 2)) {
|
|
818 h_shift = 1;
|
|
819 v_shift = 0;
|
|
820 } else {
|
|
821 h_shift = 0;
|
|
822 v_shift = 0;
|
|
823 }
|
|
824
|
|
825 c_w = (img->w + h_shift) >> h_shift;
|
|
826 c_h = (img->h + v_shift) >> v_shift;
|
|
827 c_w1 = w1 >> h_shift;
|
|
828 c_h1 = h1 >> v_shift;
|
|
829
|
|
830 /* pad horizontally */
|
|
831 for(y = 0; y < c_h; y++) {
|
|
832 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
|
|
833 v = ptr[c_w - 1];
|
|
834 for(x = c_w; x < c_w1; x++) {
|
|
835 ptr[x] = v;
|
|
836 }
|
|
837 }
|
|
838
|
|
839 /* pad vertically */
|
|
840 ptr1 = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * (c_h - 1));
|
|
841 for(y = c_h; y < c_h1; y++) {
|
|
842 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
|
|
843 memcpy(ptr, ptr1, c_w1 * sizeof(PIXEL));
|
|
844 }
|
|
845 }
|
|
846 img->w = w1;
|
|
847 img->h = h1;
|
|
848 }
|
|
849
|
|
850 /* convert the 16 bit components to 8 bits */
|
|
851 void image_convert16to8(Image *img)
|
|
852 {
|
|
853 int w, h, stride, y, x, c_count, i;
|
|
854 uint8_t *plane;
|
|
855
|
|
856 if (img->bit_depth > 8 || img->pixel_shift != 1)
|
|
857 return;
|
|
858 if (img->format == BPG_FORMAT_GRAY)
|
|
859 c_count = 1;
|
|
860 else
|
|
861 c_count = 3;
|
|
862 if (img->has_alpha)
|
|
863 c_count++;
|
|
864 for(i = 0; i < c_count; i++) {
|
|
865 get_plane_res(img, &w, &h, i);
|
|
866 stride = w;
|
|
867 plane = malloc(stride * h);
|
|
868 for(y = 0; y < h; y++) {
|
|
869 const uint16_t *src;
|
|
870 uint8_t *dst;
|
|
871 dst = plane + stride * y;
|
|
872 src = (uint16_t *)(img->data[i] + img->linesize[i] * y);
|
|
873 for(x = 0; x < w; x++)
|
|
874 dst[x] = src[x];
|
|
875 }
|
|
876 free(img->data[i]);
|
|
877 img->data[i] = plane;
|
|
878 img->linesize[i] = stride;
|
|
879 }
|
|
880 img->pixel_shift = 0;
|
|
881 }
|
|
882
|
|
883 typedef struct BPGMetaData {
|
|
884 uint32_t tag;
|
|
885 uint8_t *buf;
|
|
886 int buf_len;
|
|
887 struct BPGMetaData *next;
|
|
888 } BPGMetaData;
|
|
889
|
|
890 BPGMetaData *bpg_md_alloc(uint32_t tag)
|
|
891 {
|
|
892 BPGMetaData *md;
|
|
893 md = malloc(sizeof(BPGMetaData));
|
|
894 memset(md, 0, sizeof(*md));
|
|
895 md->tag = tag;
|
|
896 return md;
|
|
897 }
|
|
898
|
|
899 void bpg_md_free(BPGMetaData *md)
|
|
900 {
|
|
901 BPGMetaData *md_next;
|
|
902
|
|
903 while (md != NULL) {
|
|
904 md_next = md->next;
|
|
905 free(md->buf);
|
|
906 free(md);
|
|
907 md = md_next;
|
|
908 }
|
|
909 }
|
|
910
|
|
911 Image *read_png(BPGMetaData **pmd,
|
|
912 FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth,
|
|
913 int limited_range, int premultiplied_alpha)
|
|
914 {
|
|
915 png_structp png_ptr;
|
|
916 png_infop info_ptr;
|
|
917 int bit_depth, color_type;
|
|
918 Image *img;
|
|
919 uint8_t **rows;
|
|
920 int y, has_alpha, linesize, bpp;
|
|
921 BPGImageFormatEnum format;
|
|
922 ColorConvertState cvt_s, *cvt = &cvt_s;
|
|
923 BPGMetaData *md, **plast_md, *first_md;
|
|
924
|
|
925 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
|
926 NULL, NULL, NULL);
|
|
927 if (png_ptr == NULL) {
|
|
928 return NULL;
|
|
929 }
|
|
930
|
|
931 info_ptr = png_create_info_struct(png_ptr);
|
|
932 if (info_ptr == NULL) {
|
|
933 png_destroy_read_struct(&png_ptr, NULL, NULL);
|
|
934 return NULL;
|
|
935 }
|
|
936
|
|
937 if (setjmp(png_jmpbuf(png_ptr))) {
|
|
938 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
939 return NULL;
|
|
940 }
|
|
941
|
|
942 png_init_io(png_ptr, f);
|
|
943
|
|
944 png_read_info(png_ptr, info_ptr);
|
|
945
|
|
946 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
|
947 color_type = png_get_color_type(png_ptr, info_ptr);
|
|
948
|
|
949 switch (color_type) {
|
|
950 case PNG_COLOR_TYPE_PALETTE:
|
|
951 png_set_palette_to_rgb(png_ptr);
|
|
952 bit_depth = 8;
|
|
953 break;
|
|
954 case PNG_COLOR_TYPE_GRAY:
|
|
955 case PNG_COLOR_TYPE_GRAY_ALPHA:
|
|
956 if (bit_depth < 8) {
|
|
957 png_set_expand_gray_1_2_4_to_8(png_ptr);
|
|
958 bit_depth = 8;
|
|
959 }
|
|
960 break;
|
|
961 }
|
|
962 assert(bit_depth == 8 || bit_depth == 16);
|
|
963
|
|
964 #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
|
|
965 if (bit_depth == 16) {
|
|
966 png_set_swap(png_ptr);
|
|
967 }
|
|
968 #endif
|
|
969
|
|
970 if (color_type == PNG_COLOR_TYPE_GRAY ||
|
|
971 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
|
|
972 format = BPG_FORMAT_GRAY;
|
|
973 color_space = BPG_CS_YCbCr;
|
|
974 } else {
|
|
975 format = BPG_FORMAT_444;
|
|
976 }
|
|
977
|
|
978 has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
|
|
979 color_type == PNG_COLOR_TYPE_RGB_ALPHA);
|
|
980
|
|
981 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
|
982 png_set_tRNS_to_alpha(png_ptr);
|
|
983 has_alpha = 1;
|
|
984 }
|
|
985
|
|
986 if (premultiplied_alpha) {
|
|
987 png_set_alpha_mode(png_ptr, PNG_ALPHA_ASSOCIATED, PNG_GAMMA_LINEAR);
|
|
988 }
|
|
989
|
|
990 img = image_alloc(png_get_image_width(png_ptr, info_ptr),
|
|
991 png_get_image_height(png_ptr, info_ptr),
|
|
992 format, has_alpha, color_space,
|
|
993 out_bit_depth);
|
|
994 img->limited_range = limited_range;
|
|
995 img->premultiplied_alpha = premultiplied_alpha;
|
|
996
|
|
997 rows = malloc(sizeof(rows[0]) * img->h);
|
|
998 if (format == BPG_FORMAT_GRAY)
|
|
999 bpp = (1 + has_alpha) * (bit_depth / 8);
|
|
1000 else
|
|
1001 bpp = (3 + has_alpha) * (bit_depth / 8);
|
|
1002 linesize = bpp * img->w;
|
|
1003 for (y = 0; y < img->h; y++) {
|
|
1004 rows[y] = malloc(linesize);
|
|
1005 }
|
|
1006
|
|
1007 png_read_image(png_ptr, rows);
|
|
1008
|
|
1009 convert_init(cvt, bit_depth, out_bit_depth, color_space, limited_range);
|
|
1010
|
|
1011 if (format != BPG_FORMAT_GRAY) {
|
|
1012 int idx;
|
|
1013 RGBConvertFunc *convert_func;
|
|
1014
|
|
1015 idx = (bit_depth == 16);
|
|
1016 convert_func = rgb_to_cs[idx][color_space];
|
|
1017
|
|
1018 for (y = 0; y < img->h; y++) {
|
|
1019 convert_func(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
|
|
1020 (PIXEL *)(img->data[1] + y * img->linesize[1]),
|
|
1021 (PIXEL *)(img->data[2] + y * img->linesize[2]),
|
|
1022 rows[y], img->w, 3 + has_alpha);
|
|
1023 if (has_alpha) {
|
|
1024 if (idx) {
|
|
1025 gray16_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]),
|
|
1026 (uint16_t *)rows[y] + 3, img->w, 4);
|
|
1027 } else {
|
|
1028 gray8_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]),
|
|
1029 rows[y] + 3, img->w, 4);
|
|
1030 }
|
|
1031 }
|
|
1032 }
|
|
1033 } else {
|
|
1034 if (bit_depth == 16) {
|
|
1035 for (y = 0; y < img->h; y++) {
|
|
1036 luma16_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
|
|
1037 (uint16_t *)rows[y], img->w, 1 + has_alpha);
|
|
1038 if (has_alpha) {
|
|
1039 gray16_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]),
|
|
1040 (uint16_t *)rows[y] + 1, img->w, 2);
|
|
1041 }
|
|
1042 }
|
|
1043 } else {
|
|
1044 for (y = 0; y < img->h; y++) {
|
|
1045 luma8_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
|
|
1046 rows[y], img->w, 1 + has_alpha);
|
|
1047 if (has_alpha) {
|
|
1048 gray8_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]),
|
|
1049 rows[y] + 1, img->w, 2);
|
|
1050 }
|
|
1051 }
|
|
1052 }
|
|
1053 }
|
|
1054
|
|
1055 for (y = 0; y < img->h; y++) {
|
|
1056 free(rows[y]);
|
|
1057 }
|
|
1058 free(rows);
|
|
1059
|
|
1060 png_read_end(png_ptr, info_ptr);
|
|
1061
|
|
1062 /* get the ICC profile if present */
|
|
1063 first_md = NULL;
|
|
1064 plast_md = &first_md;
|
|
1065 {
|
|
1066 png_charp name;
|
|
1067 int comp_type;
|
|
1068 png_bytep iccp_buf;
|
|
1069 png_uint_32 iccp_buf_len;
|
|
1070
|
|
1071 if (png_get_iCCP(png_ptr, info_ptr,
|
|
1072 &name, &comp_type, &iccp_buf, &iccp_buf_len) ==
|
|
1073 PNG_INFO_iCCP) {
|
|
1074 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP);
|
|
1075 md->buf_len = iccp_buf_len;
|
|
1076 md->buf = malloc(iccp_buf_len);
|
|
1077 memcpy(md->buf, iccp_buf, iccp_buf_len);
|
|
1078 *plast_md = md;
|
|
1079 plast_md = &md->next;
|
|
1080 }
|
|
1081 }
|
|
1082
|
|
1083 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
1084
|
|
1085 *pmd = first_md;
|
|
1086 return img;
|
|
1087 }
|
|
1088
|
|
1089 static BPGMetaData *jpeg_get_metadata(jpeg_saved_marker_ptr first_marker)
|
|
1090 {
|
|
1091 static const char app1_exif[] = "Exif";
|
|
1092 static const char app1_xmp[] = "http://ns.adobe.com/xap/1.0/";
|
|
1093 static const char app2_iccp[] = "ICC_PROFILE";
|
|
1094 jpeg_saved_marker_ptr marker;
|
|
1095 BPGMetaData *md, **plast_md, *first_md;
|
|
1096 int has_exif, has_xmp, l, iccp_chunk_count, i;
|
|
1097 jpeg_saved_marker_ptr iccp_chunks[256];
|
|
1098
|
|
1099 iccp_chunk_count = 0;
|
|
1100 has_exif = 0;
|
|
1101 has_xmp = 0;
|
|
1102 first_md = NULL;
|
|
1103 plast_md = &first_md;
|
|
1104 for (marker = first_marker; marker != NULL; marker = marker->next) {
|
|
1105 #if 0
|
|
1106 printf("marker=APP%d len=%d\n",
|
|
1107 marker->marker - JPEG_APP0, marker->data_length);
|
|
1108 #endif
|
|
1109 if (!has_exif && marker->marker == JPEG_APP0 + 1 &&
|
|
1110 marker->data_length > sizeof(app1_exif) &&
|
|
1111 !memcmp(marker->data, app1_exif, sizeof(app1_exif))) {
|
|
1112 md = bpg_md_alloc(BPG_EXTENSION_TAG_EXIF);
|
|
1113 l = sizeof(app1_exif);
|
|
1114 md->buf_len = marker->data_length - l;
|
|
1115 md->buf = malloc(md->buf_len);
|
|
1116 memcpy(md->buf, marker->data + l, md->buf_len);
|
|
1117 *plast_md = md;
|
|
1118 plast_md = &md->next;
|
|
1119 has_exif = 1;
|
|
1120 } else if (!has_xmp && marker->marker == JPEG_APP0 + 1 &&
|
|
1121 marker->data_length > sizeof(app1_xmp) &&
|
|
1122 !memcmp(marker->data, app1_xmp, sizeof(app1_xmp)) &&
|
|
1123 !has_xmp) {
|
|
1124 md = bpg_md_alloc(BPG_EXTENSION_TAG_XMP);
|
|
1125 l = sizeof(app1_xmp);
|
|
1126 md->buf_len = marker->data_length - l;
|
|
1127 md->buf = malloc(md->buf_len);
|
|
1128 memcpy(md->buf, marker->data + l, md->buf_len);
|
|
1129 *plast_md = md;
|
|
1130 plast_md = &md->next;
|
|
1131 has_xmp = 1;
|
|
1132 } else if (marker->marker == JPEG_APP0 + 2 &&
|
|
1133 marker->data_length > (sizeof(app2_iccp) + 2) &&
|
|
1134 !memcmp(marker->data, app2_iccp, sizeof(app2_iccp))) {
|
|
1135 int chunk_count, chunk_index;
|
|
1136 l = sizeof(app2_iccp);
|
|
1137 chunk_index = marker->data[l];
|
|
1138 chunk_count = marker->data[l];
|
|
1139 if (chunk_index == 0 || chunk_count == 0)
|
|
1140 continue;
|
|
1141 if (iccp_chunk_count == 0) {
|
|
1142 iccp_chunk_count = chunk_count;
|
|
1143 for(i = 0; i < chunk_count; i++) {
|
|
1144 iccp_chunks[i] = NULL;
|
|
1145 }
|
|
1146 } else {
|
|
1147 if (chunk_count != iccp_chunk_count)
|
|
1148 continue;
|
|
1149 }
|
|
1150 if (chunk_index > iccp_chunk_count)
|
|
1151 continue;
|
|
1152 iccp_chunks[chunk_index - 1] = marker;
|
|
1153 }
|
|
1154 }
|
|
1155
|
|
1156 if (iccp_chunk_count != 0) {
|
|
1157 int len, hlen, idx;
|
|
1158 /* check that no chunk are missing */
|
|
1159 len = 0;
|
|
1160 hlen = sizeof(app2_iccp) + 2;
|
|
1161 for(i = 0; i < iccp_chunk_count; i++) {
|
|
1162 if (!iccp_chunks[i])
|
|
1163 break;
|
|
1164 len += iccp_chunks[i]->data_length - hlen;
|
|
1165 }
|
|
1166 if (i == iccp_chunk_count) {
|
|
1167 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP);
|
|
1168 md->buf_len = len;
|
|
1169 md->buf = malloc(md->buf_len);
|
|
1170 idx = 0;
|
|
1171 for(i = 0; i < iccp_chunk_count; i++) {
|
|
1172 l = iccp_chunks[i]->data_length - hlen;
|
|
1173 memcpy(md->buf + idx, iccp_chunks[i]->data + hlen, l);
|
|
1174 idx += l;
|
|
1175 }
|
|
1176 assert(idx == len);
|
|
1177 *plast_md = md;
|
|
1178 plast_md = &md->next;
|
|
1179 }
|
|
1180 }
|
|
1181 return first_md;
|
|
1182 }
|
|
1183
|
|
1184 Image *read_jpeg(BPGMetaData **pmd, FILE *f,
|
|
1185 int out_bit_depth)
|
|
1186 {
|
|
1187 struct jpeg_decompress_struct cinfo;
|
|
1188 struct jpeg_error_mgr jerr;
|
|
1189 int w, h, w1, i, y_h, c_h, y, v_shift, c_w, y1, idx, c_idx, h_shift;
|
|
1190 int h1, plane_idx[4], has_alpha, has_w_plane;
|
|
1191 Image *img;
|
|
1192 BPGImageFormatEnum format;
|
|
1193 BPGColorSpaceEnum color_space;
|
|
1194 ColorConvertState cvt_s, *cvt = &cvt_s;
|
|
1195 BPGMetaData *first_md = NULL;
|
|
1196 uint32_t comp_hv;
|
|
1197
|
|
1198 cinfo.err = jpeg_std_error(&jerr);
|
|
1199 jpeg_create_decompress(&cinfo);
|
|
1200
|
|
1201 jpeg_save_markers(&cinfo, JPEG_APP0 + 1, 65535);
|
|
1202 jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 65535);
|
|
1203
|
|
1204 jpeg_stdio_src(&cinfo, f);
|
|
1205
|
|
1206 jpeg_read_header(&cinfo, TRUE);
|
|
1207
|
|
1208 cinfo.raw_data_out = TRUE;
|
|
1209 cinfo.do_fancy_upsampling = TRUE;
|
|
1210
|
|
1211 w = cinfo.image_width;
|
|
1212 h = cinfo.image_height;
|
|
1213
|
|
1214 has_w_plane = 0;
|
|
1215 comp_hv = 0;
|
|
1216 if (cinfo.num_components < 1 || cinfo.num_components > 4)
|
|
1217 goto unsupported;
|
|
1218 for(i = 0; i < cinfo.num_components; i++) {
|
|
1219 comp_hv |= cinfo.comp_info[i].h_samp_factor << (i * 8 + 4);
|
|
1220 comp_hv |= cinfo.comp_info[i].v_samp_factor << (i * 8);
|
|
1221 }
|
|
1222 switch(cinfo.jpeg_color_space) {
|
|
1223 case JCS_GRAYSCALE:
|
|
1224 if (cinfo.num_components != 1 || comp_hv != 0x11)
|
|
1225 goto unsupported;
|
|
1226 format = BPG_FORMAT_GRAY;
|
|
1227 color_space = BPG_CS_YCbCr;
|
|
1228 break;
|
|
1229 case JCS_YCbCr:
|
|
1230 if (cinfo.num_components != 3)
|
|
1231 goto unsupported;
|
|
1232 switch(comp_hv) {
|
|
1233 case 0x111111:
|
|
1234 format = BPG_FORMAT_444;
|
|
1235 break;
|
|
1236 case 0x111121:
|
|
1237 format = BPG_FORMAT_422;
|
|
1238 break;
|
|
1239 case 0x111122:
|
|
1240 format = BPG_FORMAT_420;
|
|
1241 break;
|
|
1242 default:
|
|
1243 cinfo.raw_data_out = FALSE;
|
|
1244 format = BPG_FORMAT_444;
|
|
1245 cinfo.out_color_space = JCS_YCbCr;
|
|
1246 break;
|
|
1247 }
|
|
1248 color_space = BPG_CS_YCbCr;
|
|
1249 break;
|
|
1250 case JCS_RGB:
|
|
1251 if (cinfo.num_components != 3)
|
|
1252 goto unsupported;
|
|
1253 format = BPG_FORMAT_444;
|
|
1254 color_space = BPG_CS_RGB;
|
|
1255 cinfo.raw_data_out = FALSE;
|
|
1256 cinfo.out_color_space = JCS_RGB;
|
|
1257 break;
|
|
1258 case JCS_YCCK:
|
|
1259 if (cinfo.num_components != 4)
|
|
1260 goto unsupported;
|
|
1261 switch(comp_hv) {
|
|
1262 case 0x11111111:
|
|
1263 format = BPG_FORMAT_444;
|
|
1264 color_space = BPG_CS_YCbCr;
|
|
1265 break;
|
|
1266 case 0x22111121:
|
|
1267 format = BPG_FORMAT_422;
|
|
1268 color_space = BPG_CS_YCbCr;
|
|
1269 break;
|
|
1270 case 0x22111122:
|
|
1271 format = BPG_FORMAT_420;
|
|
1272 color_space = BPG_CS_YCbCr;
|
|
1273 break;
|
|
1274 default:
|
|
1275 cinfo.raw_data_out = FALSE;
|
|
1276 format = BPG_FORMAT_444;
|
|
1277 cinfo.out_color_space = JCS_CMYK;
|
|
1278 color_space = BPG_CS_RGB;
|
|
1279 break;
|
|
1280 }
|
|
1281 has_w_plane = 1;
|
|
1282 break;
|
|
1283 case JCS_CMYK:
|
|
1284 if (cinfo.num_components != 4)
|
|
1285 goto unsupported;
|
|
1286 format = BPG_FORMAT_444;
|
|
1287 color_space = BPG_CS_RGB;
|
|
1288 has_w_plane = 1;
|
|
1289 cinfo.raw_data_out = FALSE;
|
|
1290 cinfo.out_color_space = JCS_CMYK;
|
|
1291 break;
|
|
1292 default:
|
|
1293 unsupported:
|
|
1294 fprintf(stderr, "Unsupported JPEG parameters (cs=%d n_comp=%d comp_hv=%x)\n",
|
|
1295 cinfo.jpeg_color_space, cinfo.num_components, comp_hv);
|
|
1296 img = NULL;
|
|
1297 goto the_end;
|
|
1298 }
|
|
1299
|
|
1300 v_shift = (format == BPG_FORMAT_420);
|
|
1301 h_shift = (format == BPG_FORMAT_422 || format == BPG_FORMAT_420);
|
|
1302 has_alpha = (cinfo.num_components == 4);
|
|
1303 img = image_alloc(w, h, format, has_alpha, color_space, out_bit_depth);
|
|
1304 img->has_w_plane = has_w_plane;
|
|
1305
|
|
1306 convert_init(cvt, 8, out_bit_depth, color_space, 0);
|
|
1307
|
|
1308 jpeg_start_decompress(&cinfo);
|
|
1309
|
|
1310 if (color_space == BPG_CS_RGB) {
|
|
1311 plane_idx[0] = 2;
|
|
1312 plane_idx[1] = 0;
|
|
1313 plane_idx[2] = 1;
|
|
1314 } else {
|
|
1315 plane_idx[0] = 0;
|
|
1316 plane_idx[1] = 1;
|
|
1317 plane_idx[2] = 2;
|
|
1318 }
|
|
1319 plane_idx[3] = 3;
|
|
1320
|
|
1321 if (cinfo.raw_data_out) {
|
|
1322 JSAMPROW rows[4][16];
|
|
1323 JSAMPROW *plane_pointer[4];
|
|
1324
|
|
1325 y_h = 8 * cinfo.max_v_samp_factor;
|
|
1326 if (cinfo.num_components == 1) {
|
|
1327 c_h = 0;
|
|
1328 c_w = 0;
|
|
1329 } else {
|
|
1330 c_h = 8;
|
|
1331 if (h_shift)
|
|
1332 c_w = (w + 1) / 2;
|
|
1333 else
|
|
1334 c_w = w;
|
|
1335 }
|
|
1336 w1 = (w + 15) & ~15;
|
|
1337 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
|
|
1338 if (c_idx == 1 || c_idx == 2) {
|
|
1339 h1 = c_h;
|
|
1340 } else {
|
|
1341 h1 = y_h;
|
|
1342 }
|
|
1343 for(i = 0; i < h1; i++) {
|
|
1344 rows[c_idx][i] = malloc(w1);
|
|
1345 }
|
|
1346 plane_pointer[c_idx] = rows[c_idx];
|
|
1347 }
|
|
1348
|
|
1349 while (cinfo.output_scanline < cinfo.output_height) {
|
|
1350 y = cinfo.output_scanline;
|
|
1351 jpeg_read_raw_data(&cinfo, plane_pointer, y_h);
|
|
1352
|
|
1353 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
|
|
1354 if (c_idx == 1 || c_idx == 2) {
|
|
1355 h1 = c_h;
|
|
1356 w1 = c_w;
|
|
1357 y1 = (y >> v_shift);
|
|
1358 } else {
|
|
1359 h1 = y_h;
|
|
1360 w1 = img->w;
|
|
1361 y1 = y;
|
|
1362 }
|
|
1363 idx = plane_idx[c_idx];
|
|
1364 for(i = 0; i < h1; i++) {
|
|
1365 PIXEL *ptr;
|
|
1366 ptr = (PIXEL *)(img->data[idx] +
|
|
1367 img->linesize[idx] * (y1 + i));
|
|
1368 gray8_to_gray(cvt, ptr, rows[c_idx][i], w1, 1);
|
|
1369 if (color_space == BPG_CS_YCbCr && has_w_plane) {
|
|
1370 /* negate color */
|
|
1371 if (c_idx == 0) {
|
|
1372 gray_one_minus(cvt, ptr, w1);
|
|
1373 } else if (c_idx <= 2) {
|
|
1374 gray_neg_c(cvt, ptr, w1);
|
|
1375 }
|
|
1376 }
|
|
1377 }
|
|
1378 }
|
|
1379 }
|
|
1380
|
|
1381 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
|
|
1382 if (c_idx == 1 || c_idx == 2) {
|
|
1383 h1 = c_h;
|
|
1384 } else {
|
|
1385 h1 = y_h;
|
|
1386 }
|
|
1387 for(i = 0; i < h1; i++) {
|
|
1388 free(rows[c_idx][i]);
|
|
1389 }
|
|
1390 }
|
|
1391 } else {
|
|
1392 JSAMPROW rows[1];
|
|
1393 uint8_t *buf;
|
|
1394 int c_count;
|
|
1395
|
|
1396 c_count = 3 + has_w_plane;
|
|
1397 buf = malloc(c_count * w);
|
|
1398 rows[0] = buf;
|
|
1399 while (cinfo.output_scanline < cinfo.output_height) {
|
|
1400 y = cinfo.output_scanline;
|
|
1401 jpeg_read_scanlines(&cinfo, rows, 1);
|
|
1402
|
|
1403 for(c_idx = 0; c_idx < c_count; c_idx++) {
|
|
1404 idx = plane_idx[c_idx];
|
|
1405 gray8_to_gray(cvt, (PIXEL *)(img->data[idx] +
|
|
1406 img->linesize[idx] * y),
|
|
1407 buf + c_idx, w, c_count);
|
|
1408 }
|
|
1409 }
|
|
1410 free(buf);
|
|
1411 }
|
|
1412
|
|
1413 first_md = jpeg_get_metadata(cinfo.marker_list);
|
|
1414
|
|
1415 the_end:
|
|
1416 jpeg_finish_decompress(&cinfo);
|
|
1417
|
|
1418 jpeg_destroy_decompress(&cinfo);
|
|
1419 *pmd = first_md;
|
|
1420 return img;
|
|
1421 }
|
|
1422
|
|
1423 Image *load_image(BPGMetaData **pmd, const char *infilename,
|
|
1424 BPGColorSpaceEnum color_space, int bit_depth,
|
|
1425 int limited_range, int premultiplied_alpha)
|
|
1426 {
|
|
1427 FILE *f;
|
|
1428 int is_png;
|
|
1429 Image *img;
|
|
1430 BPGMetaData *md;
|
|
1431
|
|
1432 *pmd = NULL;
|
|
1433
|
|
1434 f = fopen(infilename, "rb");
|
|
1435 if (!f)
|
|
1436 return NULL;
|
|
1437 {
|
|
1438 uint8_t buf[8];
|
|
1439 if (fread(buf, 1, 8, f) == 8 &&
|
|
1440 png_sig_cmp(buf, 0, 8) == 0)
|
|
1441 is_png = 1;
|
|
1442 else
|
|
1443 is_png = 0;
|
|
1444 fseek(f, 0, SEEK_SET);
|
|
1445 }
|
|
1446
|
|
1447 if (is_png) {
|
|
1448 img = read_png(&md, f, color_space, bit_depth, limited_range,
|
|
1449 premultiplied_alpha);
|
|
1450 } else {
|
|
1451 img = read_jpeg(&md, f, bit_depth);
|
|
1452 }
|
|
1453 fclose(f);
|
|
1454 *pmd = md;
|
|
1455 return img;
|
|
1456 }
|
|
1457
|
|
1458 void save_yuv1(Image *img, FILE *f)
|
|
1459 {
|
|
1460 int c_w, c_h, i, c_count, y;
|
|
1461
|
|
1462 if (img->format == BPG_FORMAT_GRAY)
|
|
1463 c_count = 1;
|
|
1464 else
|
|
1465 c_count = 3;
|
|
1466 for(i = 0; i < c_count; i++) {
|
|
1467 get_plane_res(img, &c_w, &c_h, i);
|
|
1468 for(y = 0; y < c_h; y++) {
|
|
1469 fwrite(img->data[i] + y * img->linesize[i],
|
|
1470 1, c_w << img->pixel_shift, f);
|
|
1471 }
|
|
1472 }
|
|
1473 }
|
|
1474
|
|
1475 void save_yuv(Image *img, const char *filename)
|
|
1476 {
|
|
1477 FILE *f;
|
|
1478
|
|
1479 f = fopen(filename, "wb");
|
|
1480 if (!f) {
|
|
1481 fprintf(stderr, "Could not open %s\n", filename);
|
|
1482 exit(1);
|
|
1483 }
|
|
1484 save_yuv1(img, f);
|
|
1485 fclose(f);
|
|
1486 }
|
|
1487
|
|
1488 /* return the position of the end of the NAL or -1 if error */
|
|
1489 static int find_nal_end(const uint8_t *buf, int buf_len)
|
|
1490 {
|
|
1491 int idx;
|
|
1492
|
|
1493 idx = 0;
|
|
1494 if (buf_len >= 4 &&
|
|
1495 buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1) {
|
|
1496 idx = 4;
|
|
1497 } else if (buf_len >= 3 &&
|
|
1498 buf[0] == 0 && buf[1] == 0 && buf[2] == 1) {
|
|
1499 idx = 3;
|
|
1500 } else {
|
|
1501 return -1;
|
|
1502 }
|
|
1503 /* NAL header */
|
|
1504 if (idx + 2 > buf_len)
|
|
1505 return -1;
|
|
1506 /* find the last byte */
|
|
1507 for(;;) {
|
|
1508 if (idx + 2 >= buf_len) {
|
|
1509 idx = buf_len;
|
|
1510 break;
|
|
1511 }
|
|
1512 if (buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 1)
|
|
1513 break;
|
|
1514 if (idx + 3 < buf_len &&
|
|
1515 buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0 && buf[idx + 3] == 1)
|
|
1516 break;
|
|
1517 idx++;
|
|
1518 }
|
|
1519 return idx;
|
|
1520 }
|
|
1521
|
|
1522 /* return the position of the end of the NAL or -1 if error */
|
|
1523 static int extract_nal(uint8_t **pnal_buf, int *pnal_len,
|
|
1524 const uint8_t *buf, int buf_len)
|
|
1525 {
|
|
1526 int idx, start, end, len;
|
|
1527 uint8_t *nal_buf;
|
|
1528 int nal_len;
|
|
1529
|
|
1530 end = find_nal_end(buf, buf_len);
|
|
1531 if (end < 0)
|
|
1532 return -1;
|
|
1533 if (buf[2] == 1)
|
|
1534 start = 3;
|
|
1535 else
|
|
1536 start = 4;
|
|
1537 len = end - start;
|
|
1538
|
|
1539 nal_buf = malloc(len);
|
|
1540 nal_len = 0;
|
|
1541 idx = start;
|
|
1542 while (idx < end) {
|
|
1543 if (idx + 2 < end && buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 3) {
|
|
1544 nal_buf[nal_len++] = 0;
|
|
1545 nal_buf[nal_len++] = 0;
|
|
1546 idx += 3;
|
|
1547 } else {
|
|
1548 nal_buf[nal_len++] = buf[idx++];
|
|
1549 }
|
|
1550 }
|
|
1551 while (idx < end) {
|
|
1552 nal_buf[nal_len++] = buf[idx++];
|
|
1553 }
|
|
1554 *pnal_buf = nal_buf;
|
|
1555 *pnal_len = nal_len;
|
|
1556 return idx;
|
|
1557 }
|
|
1558
|
|
1559 /* big endian variable length 7 bit encoding */
|
|
1560 static void put_ue(uint8_t **pp, uint32_t v)
|
|
1561 {
|
|
1562 uint8_t *p = *pp;
|
|
1563 int i, j;
|
|
1564
|
|
1565 for(i = 1; i < 5; i++) {
|
|
1566 if (v < (1 << (7 * i)))
|
|
1567 break;
|
|
1568 }
|
|
1569 for(j = i - 1; j >= 1; j--)
|
|
1570 *p++ = ((v >> (7 * j)) & 0x7f) | 0x80;
|
|
1571 *p++ = v & 0x7f;
|
|
1572 *pp = p;
|
|
1573 }
|
|
1574
|
|
1575 typedef struct {
|
|
1576 const uint8_t *buf;
|
|
1577 int idx;
|
|
1578 int buf_len;
|
|
1579 } GetBitState;
|
|
1580
|
|
1581 static void init_get_bits(GetBitState *s, const uint8_t *buf, int buf_len)
|
|
1582 {
|
|
1583 s->buf = buf;
|
|
1584 s->buf_len = buf_len;
|
|
1585 s->idx = 0;
|
|
1586 }
|
|
1587
|
|
1588 static void skip_bits(GetBitState *s, int n)
|
|
1589 {
|
|
1590 s->idx += n;
|
|
1591 }
|
|
1592
|
|
1593 /* 1 <= n <= 25. return '0' bits if past the end of the buffer. */
|
|
1594 static uint32_t get_bits(GetBitState *s, int n)
|
|
1595 {
|
|
1596 const uint8_t *buf = s->buf;
|
|
1597 int p, i;
|
|
1598 uint32_t v;
|
|
1599
|
|
1600 p = s->idx >> 3;
|
|
1601 if ((p + 3) < s->buf_len) {
|
|
1602 v = (buf[p] << 24) | (buf[p + 1] << 16) |
|
|
1603 (buf[p + 2] << 8) | buf[p + 3];
|
|
1604 } else {
|
|
1605 v = 0;
|
|
1606 for(i = 0; i < 3; i++) {
|
|
1607 if ((p + i) < s->buf_len)
|
|
1608 v |= buf[p + i] << (24 - i * 8);
|
|
1609 }
|
|
1610 }
|
|
1611 v = (v >> (32 - (s->idx & 7) - n)) & ((1 << n) - 1);
|
|
1612 s->idx += n;
|
|
1613 return v;
|
|
1614 }
|
|
1615
|
|
1616 /* 1 <= n <= 32 */
|
|
1617 static uint32_t get_bits_long(GetBitState *s, int n)
|
|
1618 {
|
|
1619 uint32_t v;
|
|
1620
|
|
1621 if (n <= 25) {
|
|
1622 v = get_bits(s, n);
|
|
1623 } else {
|
|
1624 n -= 16;
|
|
1625 v = get_bits(s, 16) << n;
|
|
1626 v |= get_bits(s, n);
|
|
1627 }
|
|
1628 return v;
|
|
1629 }
|
|
1630
|
|
1631 /* at most 32 bits are supported */
|
|
1632 static uint32_t get_ue_golomb(GetBitState *s)
|
|
1633 {
|
|
1634 int i;
|
|
1635 i = 0;
|
|
1636 for(;;) {
|
|
1637 if (get_bits(s, 1))
|
|
1638 break;
|
|
1639 i++;
|
|
1640 if (i == 32)
|
|
1641 return 0xffffffff;
|
|
1642 }
|
|
1643 if (i == 0)
|
|
1644 return 0;
|
|
1645 else
|
|
1646 return ((1 << i) | get_bits_long(s, i)) - 1;
|
|
1647 }
|
|
1648
|
|
1649 typedef struct {
|
|
1650 uint8_t *buf;
|
|
1651 int idx;
|
|
1652 } PutBitState;
|
|
1653
|
|
1654 static void init_put_bits(PutBitState *s, uint8_t *buf)
|
|
1655 {
|
|
1656 s->buf = buf;
|
|
1657 s->idx = 0;
|
|
1658 }
|
|
1659
|
|
1660 static void put_bit(PutBitState *s, int bit)
|
|
1661 {
|
|
1662 s->buf[s->idx >> 3] |= bit << (7 - (s->idx & 7));
|
|
1663 s->idx++;
|
|
1664 }
|
|
1665
|
|
1666 static void put_bits(PutBitState *s, int n, uint32_t v)
|
|
1667 {
|
|
1668 int i;
|
|
1669
|
|
1670 for(i = 0; i < n; i++) {
|
|
1671 put_bit(s, (v >> (n - 1 - i)) & 1);
|
|
1672 }
|
|
1673 }
|
|
1674
|
|
1675 static void put_ue_golomb(PutBitState *s, uint32_t v)
|
|
1676 {
|
|
1677 uint32_t a;
|
|
1678 int n;
|
|
1679
|
|
1680 v++;
|
|
1681 n = 0;
|
|
1682 a = v;
|
|
1683 while (a != 0) {
|
|
1684 a >>= 1;
|
|
1685 n++;
|
|
1686 }
|
|
1687 if (n > 1)
|
|
1688 put_bits(s, n - 1, 0);
|
|
1689 put_bits(s, n, v);
|
|
1690 }
|
|
1691
|
|
1692 typedef struct {
|
|
1693 uint8_t *buf;
|
|
1694 int size;
|
|
1695 int len;
|
|
1696 } DynBuf;
|
|
1697
|
|
1698 static void dyn_buf_init(DynBuf *s)
|
|
1699 {
|
|
1700 s->buf = NULL;
|
|
1701 s->size = 0;
|
|
1702 s->len = 0;
|
|
1703 }
|
|
1704
|
|
1705 static int dyn_buf_resize(DynBuf *s, int size)
|
|
1706 {
|
|
1707 int new_size;
|
|
1708 uint8_t *new_buf;
|
|
1709
|
|
1710 if (size <= s->size)
|
|
1711 return 0;
|
|
1712 new_size = (s->size * 3) / 2;
|
|
1713 if (new_size < size)
|
|
1714 new_size = size;
|
|
1715 new_buf = realloc(s->buf, new_size);
|
|
1716 if (!new_buf)
|
|
1717 return -1;
|
|
1718 s->buf = new_buf;
|
|
1719 s->size = new_size;
|
|
1720 return 0;
|
|
1721 }
|
|
1722
|
|
1723 /* suppress the VPS NAL and keep only the useful part of the SPS
|
|
1724 header. The decoder can rebuild a valid HEVC stream if needed. */
|
|
1725 static int build_modified_sps(uint8_t **pout_buf, int *pout_buf_len,
|
|
1726 const uint8_t *buf, int buf_len)
|
|
1727 {
|
|
1728 int nal_unit_type, nal_len, idx, i, ret, msps_buf_len;
|
|
1729 int out_buf_len, out_buf_len_max;
|
|
1730 uint8_t *nal_buf, *msps_buf, *out_buf;
|
|
1731 GetBitState gb_s, *gb = &gb_s;
|
|
1732 PutBitState pb_s, *pb = &pb_s;
|
|
1733 uint8_t *p;
|
|
1734
|
|
1735 idx = extract_nal(&nal_buf, &nal_len, buf, buf_len);
|
|
1736 if (idx < 0)
|
|
1737 return -1;
|
|
1738 if (nal_len < 2) {
|
|
1739 free(nal_buf);
|
|
1740 return -1;
|
|
1741 }
|
|
1742 nal_unit_type = (nal_buf[0] >> 1) & 0x3f;
|
|
1743 free(nal_buf);
|
|
1744 if (nal_unit_type != 32) {
|
|
1745 fprintf(stderr, "expecting VPS nal (%d)\n", nal_unit_type);
|
|
1746 return -1; /* expect VPS nal */
|
|
1747 }
|
|
1748
|
|
1749 ret = extract_nal(&nal_buf, &nal_len, buf + idx, buf_len);
|
|
1750 if (ret < 0)
|
|
1751 return -1;
|
|
1752 idx += ret;
|
|
1753 if (nal_len < 2)
|
|
1754 return -1;
|
|
1755 nal_unit_type = (nal_buf[0] >> 1) & 0x3f;
|
|
1756 if (nal_unit_type != 33) {
|
|
1757 fprintf(stderr, "expecting SPS nal (%d)\n", nal_unit_type);
|
|
1758 return -1; /* expect SPS nal */
|
|
1759 }
|
|
1760
|
|
1761 /* skip the initial part of the SPS up to and including
|
|
1762 log2_min_cb_size */
|
|
1763 {
|
|
1764 int vps_id, max_sub_layers, profile_idc, sps_id;
|
|
1765 int chroma_format_idc, width, height, bit_depth_luma, bit_depth_chroma;
|
|
1766 int log2_max_poc_lsb, sublayer_ordering_info, log2_min_cb_size;
|
|
1767 int log2_diff_max_min_coding_block_size, log2_min_tb_size;
|
|
1768 int log2_diff_max_min_transform_block_size;
|
|
1769 int max_transform_hierarchy_depth_inter;
|
|
1770 int max_transform_hierarchy_depth_intra;
|
|
1771 int scaling_list_enable_flag, amp_enabled_flag, sao_enabled;
|
|
1772 int pcm_enabled_flag, nb_st_rps;
|
|
1773 int long_term_ref_pics_present_flag, sps_strong_intra_smoothing_enable_flag, vui_present;
|
|
1774 int sps_temporal_mvp_enabled_flag;
|
|
1775 int pcm_sample_bit_depth_luma_minus1;
|
|
1776 int pcm_sample_bit_depth_chroma_minus1;
|
|
1777 int log2_min_pcm_luma_coding_block_size_minus3;
|
|
1778 int log2_diff_max_min_pcm_luma_coding_block_size;
|
|
1779 int pcm_loop_filter_disabled_flag;
|
|
1780 int sps_extension_flag, sps_range_extension_flag, sps_extension_7bits;
|
|
1781 int sps_range_extension_flags;
|
|
1782
|
|
1783 init_get_bits(gb, nal_buf, nal_len);
|
|
1784 skip_bits(gb, 16); /* nal header */
|
|
1785 vps_id = get_bits(gb, 4);
|
|
1786 if (vps_id != 0) {
|
|
1787 fprintf(stderr, "VPS id 0 expected\n");
|
|
1788 return -1;
|
|
1789 }
|
|
1790 max_sub_layers = get_bits(gb, 3);
|
|
1791 if (max_sub_layers != 0) {
|
|
1792 fprintf(stderr, "max_sub_layers == 0 expected\n");
|
|
1793 return -1;
|
|
1794 }
|
|
1795 skip_bits(gb, 1); /* temporal_id_nesting_flag */
|
|
1796 /* profile tier level */
|
|
1797 skip_bits(gb, 2); /* profile_space */
|
|
1798 skip_bits(gb, 1); /* tier_flag */
|
|
1799 profile_idc = get_bits(gb, 5);
|
|
1800 for(i = 0; i < 32; i++) {
|
|
1801 skip_bits(gb, 1); /* profile_compatibility_flag */
|
|
1802 }
|
|
1803 skip_bits(gb, 1); /* progressive_source_flag */
|
|
1804 skip_bits(gb, 1); /* interlaced_source_flag */
|
|
1805 skip_bits(gb, 1); /* non_packed_constraint_flag */
|
|
1806 skip_bits(gb, 1); /* frame_only_constraint_flag */
|
|
1807 skip_bits(gb, 44); /* XXX_reserved_zero_44 */
|
|
1808 skip_bits(gb, 8); /* level_idc */
|
|
1809
|
|
1810 sps_id = get_ue_golomb(gb);
|
|
1811 if (sps_id != 0) {
|
|
1812 fprintf(stderr, "SPS id 0 expected (%d)\n", sps_id);
|
|
1813 return -1;
|
|
1814 }
|
|
1815 chroma_format_idc = get_ue_golomb(gb);
|
|
1816 if (chroma_format_idc == 3) {
|
|
1817 get_bits(gb, 1); /* separate_colour_plane_flag */
|
|
1818 }
|
|
1819 width = get_ue_golomb(gb);
|
|
1820 height = get_ue_golomb(gb);
|
|
1821 /* pic conformance_flag */
|
|
1822 if (get_bits(gb, 1)) {
|
|
1823 get_ue_golomb(gb); /* left_offset */
|
|
1824 get_ue_golomb(gb); /* right_offset */
|
|
1825 get_ue_golomb(gb); /* top_offset */
|
|
1826 get_ue_golomb(gb); /* bottom_offset */
|
|
1827 }
|
|
1828 bit_depth_luma = get_ue_golomb(gb) + 8;
|
|
1829 bit_depth_chroma = get_ue_golomb(gb) + 8;
|
|
1830 log2_max_poc_lsb = get_ue_golomb(gb) + 4;
|
|
1831 if (log2_max_poc_lsb != 8) {
|
|
1832 fprintf(stderr, "log2_max_poc_lsb must be 8 (%d)\n", log2_max_poc_lsb);
|
|
1833 return -1;
|
|
1834 }
|
|
1835 sublayer_ordering_info = get_bits(gb, 1);
|
|
1836 get_ue_golomb(gb); /* max_dec_pic_buffering */
|
|
1837 get_ue_golomb(gb); /* num_reorder_pics */
|
|
1838 get_ue_golomb(gb); /* max_latency_increase */
|
|
1839
|
|
1840 log2_min_cb_size = get_ue_golomb(gb) + 3;
|
|
1841 log2_diff_max_min_coding_block_size = get_ue_golomb(gb);
|
|
1842 log2_min_tb_size = get_ue_golomb(gb) + 2;
|
|
1843 log2_diff_max_min_transform_block_size = get_ue_golomb(gb);
|
|
1844
|
|
1845 max_transform_hierarchy_depth_inter = get_ue_golomb(gb);
|
|
1846 max_transform_hierarchy_depth_intra = get_ue_golomb(gb);
|
|
1847 if (max_transform_hierarchy_depth_inter != max_transform_hierarchy_depth_intra) {
|
|
1848 fprintf(stderr, "max_transform_hierarchy_depth_inter must be the same as max_transform_hierarchy_depth_intra (%d %d)\n", max_transform_hierarchy_depth_inter, max_transform_hierarchy_depth_intra);
|
|
1849 return -1;
|
|
1850 }
|
|
1851
|
|
1852 scaling_list_enable_flag = get_bits(gb, 1);
|
|
1853 if (scaling_list_enable_flag != 0) {
|
|
1854 fprintf(stderr, "scaling_list_enable_flag must be 0\n");
|
|
1855 return -1;
|
|
1856 }
|
|
1857 amp_enabled_flag = get_bits(gb, 1);
|
|
1858 if (!amp_enabled_flag) {
|
|
1859 fprintf(stderr, "amp_enabled_flag must be set\n");
|
|
1860 return -1;
|
|
1861 }
|
|
1862 sao_enabled = get_bits(gb, 1);
|
|
1863 pcm_enabled_flag = get_bits(gb, 1);
|
|
1864 if (pcm_enabled_flag) {
|
|
1865 pcm_sample_bit_depth_luma_minus1 = get_bits(gb, 4);
|
|
1866 pcm_sample_bit_depth_chroma_minus1 = get_bits(gb, 4);
|
|
1867 log2_min_pcm_luma_coding_block_size_minus3 = get_ue_golomb(gb);
|
|
1868 log2_diff_max_min_pcm_luma_coding_block_size = get_ue_golomb(gb);
|
|
1869 pcm_loop_filter_disabled_flag = get_bits(gb, 1);
|
|
1870 }
|
|
1871 nb_st_rps = get_ue_golomb(gb);
|
|
1872 if (nb_st_rps != 0) {
|
|
1873 fprintf(stderr, "nb_st_rps must be 0 (%d)\n", nb_st_rps);
|
|
1874 return -1;
|
|
1875 }
|
|
1876 long_term_ref_pics_present_flag = get_bits(gb, 1);
|
|
1877 if (long_term_ref_pics_present_flag) {
|
|
1878 fprintf(stderr, "nlong_term_ref_pics_present_flag must be 0 (%d)\n", nb_st_rps);
|
|
1879 return -1;
|
|
1880 }
|
|
1881 sps_temporal_mvp_enabled_flag = get_bits(gb, 1);
|
|
1882 if (!sps_temporal_mvp_enabled_flag) {
|
|
1883 fprintf(stderr, "sps_temporal_mvp_enabled_flag must be set\n");
|
|
1884 return -1;
|
|
1885 }
|
|
1886 sps_strong_intra_smoothing_enable_flag = get_bits(gb, 1);
|
|
1887 vui_present = get_bits(gb, 1);
|
|
1888 if (vui_present) {
|
|
1889 int sar_present, sar_idx, overscan_info_present_flag;
|
|
1890 int video_signal_type_present_flag, chroma_loc_info_present_flag;
|
|
1891 int default_display_window_flag, vui_timing_info_present_flag;
|
|
1892 int vui_poc_proportional_to_timing_flag;
|
|
1893 int vui_hrd_parameters_present_flag, bitstream_restriction_flag;
|
|
1894
|
|
1895 sar_present = get_bits(gb, 1);
|
|
1896 if (sar_present) {
|
|
1897 sar_idx = get_bits(gb, 8);
|
|
1898 if (sar_idx == 255) {
|
|
1899 skip_bits(gb, 16); /* sar_num */
|
|
1900 skip_bits(gb, 16); /* sar_den */
|
|
1901 }
|
|
1902 }
|
|
1903
|
|
1904 overscan_info_present_flag = get_bits(gb, 1);
|
|
1905 if (overscan_info_present_flag) {
|
|
1906 skip_bits(gb, 1); /* overscan_appropriate_flag */
|
|
1907 }
|
|
1908
|
|
1909 video_signal_type_present_flag = get_bits(gb, 1);
|
|
1910 if (video_signal_type_present_flag) {
|
|
1911 fprintf(stderr, "video_signal_type_present_flag must be 0\n");
|
|
1912 return -1;
|
|
1913 }
|
|
1914 chroma_loc_info_present_flag = get_bits(gb, 1);
|
|
1915 if (chroma_loc_info_present_flag) {
|
|
1916 get_ue_golomb(gb);
|
|
1917 get_ue_golomb(gb);
|
|
1918 }
|
|
1919 skip_bits(gb, 1); /* neutra_chroma_indication_flag */
|
|
1920 skip_bits(gb, 1);
|
|
1921 skip_bits(gb, 1);
|
|
1922 default_display_window_flag = get_bits(gb, 1);
|
|
1923 if (default_display_window_flag) {
|
|
1924 fprintf(stderr, "default_display_window_flag must be 0\n");
|
|
1925 return -1;
|
|
1926 }
|
|
1927 vui_timing_info_present_flag = get_bits(gb, 1);
|
|
1928 if (vui_timing_info_present_flag) {
|
|
1929 skip_bits(gb, 32);
|
|
1930 skip_bits(gb, 32);
|
|
1931 vui_poc_proportional_to_timing_flag = get_bits(gb, 1);
|
|
1932 if (vui_poc_proportional_to_timing_flag) {
|
|
1933 get_ue_golomb(gb);
|
|
1934 }
|
|
1935 vui_hrd_parameters_present_flag = get_bits(gb, 1);
|
|
1936 if (vui_hrd_parameters_present_flag) {
|
|
1937 fprintf(stderr, "vui_hrd_parameters_present_flag must be 0\n");
|
|
1938 return -1;
|
|
1939 }
|
|
1940 }
|
|
1941 bitstream_restriction_flag = get_bits(gb, 1);
|
|
1942 if (bitstream_restriction_flag) {
|
|
1943 skip_bits(gb, 1);
|
|
1944 skip_bits(gb, 1);
|
|
1945 skip_bits(gb, 1);
|
|
1946 get_ue_golomb(gb);
|
|
1947 get_ue_golomb(gb);
|
|
1948 get_ue_golomb(gb);
|
|
1949 get_ue_golomb(gb);
|
|
1950 get_ue_golomb(gb);
|
|
1951 }
|
|
1952 }
|
|
1953 sps_extension_flag = get_bits(gb, 1);
|
|
1954 sps_range_extension_flag = 0;
|
|
1955 sps_range_extension_flags = 0;
|
|
1956 if (sps_extension_flag) {
|
|
1957 sps_range_extension_flag = get_bits(gb, 1);
|
|
1958 sps_extension_7bits = get_bits(gb, 7);
|
|
1959 if (sps_extension_7bits != 0) {
|
|
1960 fprintf(stderr, "sps_extension_7bits must be 0\n");
|
|
1961 return -1;
|
|
1962 }
|
|
1963 if (sps_range_extension_flag) {
|
|
1964 sps_range_extension_flags = get_bits(gb, 9);
|
|
1965 if (sps_range_extension_flags & ((1 << (8 - 3)) |
|
|
1966 (1 << (8 - 4)) |
|
|
1967 (1 << (8 - 6)) |
|
|
1968 (1 << (8 - 8)))) {
|
|
1969 fprintf(stderr, "unsupported range extensions (0x%x)\n",
|
|
1970 sps_range_extension_flags);
|
|
1971 return -1;
|
|
1972 }
|
|
1973 }
|
|
1974 }
|
|
1975
|
|
1976 /* build the modified SPS */
|
|
1977 msps_buf = malloc(nal_len + 32);
|
|
1978 memset(msps_buf, 0, nal_len + 16);
|
|
1979
|
|
1980 init_put_bits(pb, msps_buf);
|
|
1981 put_ue_golomb(pb, log2_min_cb_size - 3);
|
|
1982 put_ue_golomb(pb, log2_diff_max_min_coding_block_size);
|
|
1983 put_ue_golomb(pb, log2_min_tb_size - 2);
|
|
1984 put_ue_golomb(pb, log2_diff_max_min_transform_block_size);
|
|
1985 put_ue_golomb(pb, max_transform_hierarchy_depth_intra);
|
|
1986 put_bits(pb, 1, sao_enabled);
|
|
1987 put_bits(pb, 1, pcm_enabled_flag);
|
|
1988 if (pcm_enabled_flag) {
|
|
1989 put_bits(pb, 4, pcm_sample_bit_depth_luma_minus1);
|
|
1990 put_bits(pb, 4, pcm_sample_bit_depth_chroma_minus1);
|
|
1991 put_ue_golomb(pb, log2_min_pcm_luma_coding_block_size_minus3);
|
|
1992 put_ue_golomb(pb, log2_diff_max_min_pcm_luma_coding_block_size);
|
|
1993 put_bits(pb, 1, pcm_loop_filter_disabled_flag);
|
|
1994 }
|
|
1995 put_bits(pb, 1, sps_strong_intra_smoothing_enable_flag);
|
|
1996 put_bits(pb, 1, sps_extension_flag);
|
|
1997 if (sps_extension_flag) {
|
|
1998 put_bits(pb, 1, sps_range_extension_flag);
|
|
1999 put_bits(pb, 7, 0);
|
|
2000 if (sps_range_extension_flag) {
|
|
2001 put_bits(pb, 9, sps_range_extension_flags);
|
|
2002 }
|
|
2003 }
|
|
2004 msps_buf_len = (pb->idx + 7) >> 3;
|
|
2005
|
|
2006 out_buf_len_max = 5 + msps_buf_len;
|
|
2007 out_buf = malloc(out_buf_len_max);
|
|
2008
|
|
2009 // printf("msps_n_bits=%d\n", pb->idx);
|
|
2010 p = out_buf;
|
|
2011 put_ue(&p, msps_buf_len); /* header length */
|
|
2012
|
|
2013 memcpy(p, msps_buf, msps_buf_len);
|
|
2014 p += msps_buf_len;
|
|
2015
|
|
2016 out_buf_len = p - out_buf;
|
|
2017 free(msps_buf);
|
|
2018 free(nal_buf);
|
|
2019 }
|
|
2020 *pout_buf = out_buf;
|
|
2021 *pout_buf_len = out_buf_len;
|
|
2022 return idx;
|
|
2023 }
|
|
2024
|
|
2025 static int add_frame_duration_sei(DynBuf *out_buf, uint16_t frame_ticks)
|
|
2026 {
|
|
2027 uint8_t nal_buf[128], *q;
|
|
2028 int nut, nal_len;
|
|
2029
|
|
2030 q = nal_buf;
|
|
2031 *q++ = 0x00;
|
|
2032 *q++ = 0x00;
|
|
2033 *q++ = 0x01;
|
|
2034 nut = 39; /* prefix SEI NUT */
|
|
2035 *q++ = (nut << 1);
|
|
2036 *q++ = 1;
|
|
2037 *q++ = 0xff; /* payload_type = 257 */
|
|
2038 *q++ = 0x02;
|
|
2039 *q++ = 2; /* payload_size = 2 */
|
|
2040 *q++ = frame_ticks >> 8;
|
|
2041 *q++ = frame_ticks;
|
|
2042 *q++ = 0x80; /* extra '1' bit and align to byte */
|
|
2043 /* Note: the 0x00 0x00 b pattern with b <= 3 cannot happen, so no
|
|
2044 need to escape */
|
|
2045 nal_len = q - nal_buf;
|
|
2046 if (dyn_buf_resize(out_buf, out_buf->len + nal_len) < 0)
|
|
2047 return -1;
|
|
2048 memcpy(out_buf->buf + out_buf->len, nal_buf, nal_len);
|
|
2049 out_buf->len += nal_len;
|
|
2050 return 0;
|
|
2051 }
|
|
2052
|
|
2053 static int build_modified_hevc(uint8_t **pout_buf,
|
|
2054 const uint8_t *cbuf, int cbuf_len,
|
|
2055 const uint8_t *abuf, int abuf_len,
|
|
2056 const uint16_t *frame_duration_tab,
|
|
2057 int frame_count)
|
|
2058 {
|
|
2059 DynBuf out_buf_s, *out_buf = &out_buf_s;
|
|
2060 uint8_t *msps;
|
|
2061 const uint8_t *nal_buf;
|
|
2062 int msps_len, cidx, aidx, is_alpha, nal_len, first_nal, start, l, frame_num;
|
|
2063
|
|
2064 dyn_buf_init(out_buf);
|
|
2065
|
|
2066 /* add alpha MSPS */
|
|
2067 aidx = 0; /* avoids warning */
|
|
2068 if (abuf) {
|
|
2069 aidx = build_modified_sps(&msps, &msps_len, abuf, abuf_len);
|
|
2070 if (aidx < 0)
|
|
2071 goto fail;
|
|
2072 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0)
|
|
2073 goto fail;
|
|
2074 memcpy(out_buf->buf + out_buf->len, msps, msps_len);
|
|
2075 out_buf->len += msps_len;
|
|
2076 free(msps);
|
|
2077 }
|
|
2078
|
|
2079 /* add color MSPS */
|
|
2080 cidx = build_modified_sps(&msps, &msps_len, cbuf, cbuf_len);
|
|
2081 if (cidx < 0)
|
|
2082 goto fail;
|
|
2083 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0)
|
|
2084 goto fail;
|
|
2085 memcpy(out_buf->buf + out_buf->len, msps, msps_len);
|
|
2086 out_buf->len += msps_len;
|
|
2087 free(msps);
|
|
2088
|
|
2089 /* add the remaining NALs, alternating between alpha (if present)
|
|
2090 and color. */
|
|
2091 is_alpha = (abuf != NULL);
|
|
2092 first_nal = 1;
|
|
2093 frame_num = 0;
|
|
2094 for(;;) {
|
|
2095 if (!is_alpha) {
|
|
2096 if (cidx >= cbuf_len) {
|
|
2097 if (abuf) {
|
|
2098 fprintf(stderr, "Incorrect number of alpha NALs\n");
|
|
2099 goto fail;
|
|
2100 }
|
|
2101 break;
|
|
2102 }
|
|
2103 nal_buf = cbuf + cidx;
|
|
2104 nal_len = find_nal_end(nal_buf, cbuf_len - cidx);
|
|
2105 // printf("cidx=%d/%d nal_len=%d\n", cidx, cbuf_len, nal_len);
|
|
2106 if (nal_len < 0)
|
|
2107 goto fail;
|
|
2108 cidx += nal_len;
|
|
2109 } else {
|
|
2110 if (aidx >= abuf_len)
|
|
2111 break;
|
|
2112 nal_buf = abuf + aidx;
|
|
2113 nal_len = find_nal_end(nal_buf, abuf_len - aidx);
|
|
2114 // printf("aidx=%d/%d nal_len=%d\n", aidx, abuf_len, nal_len);
|
|
2115 if (nal_len < 0)
|
|
2116 goto fail;
|
|
2117 aidx += nal_len;
|
|
2118 }
|
|
2119 start = 3 + (nal_buf[2] == 0);
|
|
2120 if (!is_alpha) {
|
|
2121 int nut;
|
|
2122 /* add SEI NAL for the frame duration (animation case) */
|
|
2123 nut = (nal_buf[start] >> 1) & 0x3f;
|
|
2124 if ((nut <= 9 || (nut >= 16 && nut <= 21)) &&
|
|
2125 start + 2 < nal_len && (nal_buf[start + 2] & 0x80)) {
|
|
2126 int frame_ticks;
|
|
2127 assert(frame_num < frame_count);
|
|
2128 frame_ticks = frame_duration_tab[frame_num];
|
|
2129 if (frame_ticks > 1) {
|
|
2130 add_frame_duration_sei(out_buf, frame_ticks);
|
|
2131 }
|
|
2132 frame_num++;
|
|
2133 }
|
|
2134 }
|
|
2135 if (first_nal) {
|
|
2136 /* skip first start code */
|
|
2137 l = start;
|
|
2138 } else {
|
|
2139 l = 0;
|
|
2140 }
|
|
2141 if (dyn_buf_resize(out_buf, out_buf->len + nal_len - l) < 0)
|
|
2142 goto fail;
|
|
2143 // printf("add nal len=%d\n", nal_len - l);
|
|
2144 memcpy(out_buf->buf + out_buf->len, nal_buf + l, nal_len - l);
|
|
2145 if (is_alpha) {
|
|
2146 /* set nul_layer_id of alpha to '1' */
|
|
2147 out_buf->buf[out_buf->len + (start - l) + 1] |= 1 << 3;
|
|
2148 }
|
|
2149 out_buf->len += nal_len - l;
|
|
2150
|
|
2151 if (abuf) {
|
|
2152 is_alpha ^= 1;
|
|
2153 }
|
|
2154 first_nal = 0;
|
|
2155 }
|
|
2156 *pout_buf = out_buf->buf;
|
|
2157 return out_buf->len;
|
|
2158 fail:
|
|
2159 free(out_buf->buf);
|
|
2160 return -1;
|
|
2161 }
|
|
2162
|
|
2163 typedef enum {
|
|
2164 #if defined(USE_X265)
|
|
2165 HEVC_ENCODER_X265,
|
|
2166 #endif
|
|
2167 #if defined(USE_JCTVC)
|
|
2168 HEVC_ENCODER_JCTVC,
|
|
2169 #endif
|
|
2170
|
|
2171 HEVC_ENCODER_COUNT,
|
|
2172 } HEVCEncoderEnum;
|
|
2173
|
|
2174 static char *hevc_encoder_name[HEVC_ENCODER_COUNT] = {
|
|
2175 #if defined(USE_X265)
|
|
2176 "x265",
|
|
2177 #endif
|
|
2178 #if defined(USE_JCTVC)
|
|
2179 "jctvc",
|
|
2180 #endif
|
|
2181 };
|
|
2182
|
|
2183 static HEVCEncoder *hevc_encoder_tab[HEVC_ENCODER_COUNT] = {
|
|
2184 #if defined(USE_X265)
|
|
2185 &x265_hevc_encoder,
|
|
2186 #endif
|
|
2187 #if defined(USE_JCTVC)
|
|
2188 &jctvc_encoder,
|
|
2189 #endif
|
|
2190 };
|
|
2191
|
|
2192 #define IMAGE_HEADER_MAGIC 0x425047fb
|
|
2193
|
|
2194 #define DEFAULT_OUTFILENAME "out.bpg"
|
|
2195 #define DEFAULT_QP 29
|
|
2196 #define DEFAULT_BIT_DEPTH 8
|
|
2197
|
|
2198 #ifdef RExt__HIGH_BIT_DEPTH_SUPPORT
|
|
2199 #define BIT_DEPTH_MAX 14
|
|
2200 #else
|
|
2201 #define BIT_DEPTH_MAX 12
|
|
2202 #endif
|
|
2203 #define DEFAULT_COMPRESS_LEVEL 8
|
|
2204
|
|
2205
|
|
2206 typedef struct BPGEncoderContext BPGEncoderContext;
|
|
2207
|
|
2208 typedef struct BPGEncoderParameters {
|
|
2209 int qp; /* 0 ... 51 */
|
|
2210 int alpha_qp; /* -1 ... 51. -1 means same as qp */
|
|
2211 int lossless; /* true if lossless compression (qp and alpha_qp are
|
|
2212 ignored) */
|
|
2213 BPGImageFormatEnum preferred_chroma_format;
|
|
2214 int sei_decoded_picture_hash; /* 0, 1 */
|
|
2215 int compress_level; /* 1 ... 9 */
|
|
2216 int verbose;
|
|
2217 HEVCEncoderEnum encoder_type;
|
|
2218 int animated; /* 0 ... 1: if true, encode as animated image */
|
|
2219 uint16_t loop_count; /* animations: number of loops. 0=infinite */
|
|
2220 /* animations: the frame delay is a multiple of
|
|
2221 frame_delay_num/frame_delay_den seconds */
|
|
2222 uint16_t frame_delay_num;
|
|
2223 uint16_t frame_delay_den;
|
|
2224 } BPGEncoderParameters;
|
|
2225
|
|
2226 typedef int BPGEncoderWriteFunc(void *opaque, const uint8_t *buf, int buf_len);
|
|
2227
|
|
2228 struct BPGEncoderContext {
|
|
2229 BPGEncoderParameters params;
|
|
2230 BPGMetaData *first_md;
|
|
2231 HEVCEncoder *encoder;
|
|
2232 int frame_count;
|
|
2233 HEVCEncoderContext *enc_ctx;
|
|
2234 HEVCEncoderContext *alpha_enc_ctx;
|
|
2235 int frame_ticks;
|
|
2236 uint16_t *frame_duration_tab;
|
|
2237 int frame_duration_tab_size;
|
|
2238 };
|
|
2239
|
|
2240 void *mallocz(size_t size)
|
|
2241 {
|
|
2242 void *ptr;
|
|
2243 ptr = malloc(size);
|
|
2244 if (!ptr)
|
|
2245 return NULL;
|
|
2246 memset(ptr, 0, size);
|
|
2247 return ptr;
|
|
2248 }
|
|
2249
|
|
2250 BPGEncoderParameters *bpg_encoder_param_alloc(void)
|
|
2251 {
|
|
2252 BPGEncoderParameters *p;
|
|
2253 p = mallocz(sizeof(BPGEncoderParameters));
|
|
2254 if (!p)
|
|
2255 return NULL;
|
|
2256 p->qp = DEFAULT_QP;
|
|
2257 p->alpha_qp = -1;
|
|
2258 p->preferred_chroma_format = BPG_FORMAT_420;
|
|
2259 p->compress_level = DEFAULT_COMPRESS_LEVEL;
|
|
2260 p->frame_delay_num = 1;
|
|
2261 p->frame_delay_den = 25;
|
|
2262 p->loop_count = 0;
|
|
2263 return p;
|
|
2264 }
|
|
2265
|
|
2266 void bpg_encoder_param_free(BPGEncoderParameters *p)
|
|
2267 {
|
|
2268 free(p);
|
|
2269 }
|
|
2270
|
|
2271 BPGEncoderContext *bpg_encoder_open(BPGEncoderParameters *p)
|
|
2272 {
|
|
2273 BPGEncoderContext *s;
|
|
2274
|
|
2275 s = mallocz(sizeof(BPGEncoderContext));
|
|
2276 if (!s)
|
|
2277 return NULL;
|
|
2278 s->params = *p;
|
|
2279 s->encoder = hevc_encoder_tab[s->params.encoder_type];
|
|
2280 s->frame_ticks = 1;
|
|
2281 return s;
|
|
2282 }
|
|
2283
|
|
2284 void bpg_encoder_set_extension_data(BPGEncoderContext *s,
|
|
2285 BPGMetaData *md)
|
|
2286 {
|
|
2287 s->first_md = md;
|
|
2288 }
|
|
2289
|
|
2290 static int bpg_encoder_encode_trailer(BPGEncoderContext *s,
|
|
2291 BPGEncoderWriteFunc *write_func,
|
|
2292 void *opaque)
|
|
2293 {
|
|
2294 uint8_t *out_buf, *alpha_buf, *hevc_buf;
|
|
2295 int out_buf_len, alpha_buf_len, hevc_buf_len;
|
|
2296
|
|
2297 out_buf_len = s->encoder->close(s->enc_ctx, &out_buf);
|
|
2298 if (out_buf_len < 0) {
|
|
2299 fprintf(stderr, "Error while encoding picture\n");
|
|
2300 exit(1);
|
|
2301 }
|
|
2302 s->enc_ctx = NULL;
|
|
2303
|
|
2304 alpha_buf = NULL;
|
|
2305 alpha_buf_len = 0;
|
|
2306 if (s->alpha_enc_ctx) {
|
|
2307 alpha_buf_len = s->encoder->close(s->alpha_enc_ctx, &alpha_buf);
|
|
2308 if (alpha_buf_len < 0) {
|
|
2309 fprintf(stderr, "Error while encoding picture (alpha plane)\n");
|
|
2310 exit(1);
|
|
2311 }
|
|
2312 s->alpha_enc_ctx = NULL;
|
|
2313 }
|
|
2314
|
|
2315 hevc_buf = NULL;
|
|
2316 hevc_buf_len = build_modified_hevc(&hevc_buf, out_buf, out_buf_len,
|
|
2317 alpha_buf, alpha_buf_len,
|
|
2318 s->frame_duration_tab, s->frame_count);
|
|
2319 if (hevc_buf_len < 0) {
|
|
2320 fprintf(stderr, "Error while creating HEVC data\n");
|
|
2321 exit(1);
|
|
2322 }
|
|
2323 free(out_buf);
|
|
2324 free(alpha_buf);
|
|
2325
|
|
2326 if (write_func(opaque, hevc_buf, hevc_buf_len) != hevc_buf_len) {
|
|
2327 fprintf(stderr, "Error while writing HEVC data\n");
|
|
2328 exit(1);
|
|
2329 }
|
|
2330 free(hevc_buf);
|
|
2331 return 0;
|
|
2332 }
|
|
2333
|
|
2334 int bpg_encoder_set_frame_duration(BPGEncoderContext *s, int frame_ticks)
|
|
2335 {
|
|
2336 if (frame_ticks >= 1 && frame_ticks <= 65535) {
|
|
2337 s->frame_ticks = frame_ticks;
|
|
2338 return 0;
|
|
2339 } else {
|
|
2340 return -1;
|
|
2341 }
|
|
2342 }
|
|
2343
|
|
2344 /* Warning: currently 'img' is modified. When encoding animations, img
|
|
2345 = NULL indicates the end of the stream. */
|
|
2346 int bpg_encoder_encode(BPGEncoderContext *s, Image *img,
|
|
2347 BPGEncoderWriteFunc *write_func,
|
|
2348 void *opaque)
|
|
2349 {
|
|
2350 const BPGEncoderParameters *p = &s->params;
|
|
2351 Image *img_alpha;
|
|
2352 HEVCEncodeParams ep_s, *ep = &ep_s;
|
|
2353 uint8_t *extension_buf;
|
|
2354 int extension_buf_len;
|
|
2355 int cb_size, width, height;
|
|
2356
|
|
2357 if (p->animated && !img) {
|
|
2358 return bpg_encoder_encode_trailer(s, write_func, opaque);
|
|
2359 }
|
|
2360
|
|
2361 /* extract the alpha plane */
|
|
2362 if (img->has_alpha) {
|
|
2363 int c_idx;
|
|
2364
|
|
2365 img_alpha = malloc(sizeof(Image));
|
|
2366 memset(img_alpha, 0, sizeof(*img_alpha));
|
|
2367 if (img->format == BPG_FORMAT_GRAY)
|
|
2368 c_idx = 1;
|
|
2369 else
|
|
2370 c_idx = 3;
|
|
2371
|
|
2372 img_alpha->w = img->w;
|
|
2373 img_alpha->h = img->h;
|
|
2374 img_alpha->format = BPG_FORMAT_GRAY;
|
|
2375 img_alpha->has_alpha = 0;
|
|
2376 img_alpha->color_space = BPG_CS_YCbCr;
|
|
2377 img_alpha->bit_depth = img->bit_depth;
|
|
2378 img_alpha->pixel_shift = img->pixel_shift;
|
|
2379 img_alpha->data[0] = img->data[c_idx];
|
|
2380 img_alpha->linesize[0] = img->linesize[c_idx];
|
|
2381
|
|
2382 img->data[c_idx] = NULL;
|
|
2383 img->has_alpha = 0;
|
|
2384 } else {
|
|
2385 img_alpha = NULL;
|
|
2386 }
|
|
2387
|
|
2388 if (img->format == BPG_FORMAT_444 && img->color_space != BPG_CS_RGB) {
|
|
2389 if (p->preferred_chroma_format == BPG_FORMAT_420 ||
|
|
2390 p->preferred_chroma_format == BPG_FORMAT_420_VIDEO) {
|
|
2391 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_420);
|
|
2392 if (image_ycc444_to_ycc420(img, c_h_phase) != 0)
|
|
2393 goto error_convert;
|
|
2394 } else if (p->preferred_chroma_format == BPG_FORMAT_422 ||
|
|
2395 p->preferred_chroma_format == BPG_FORMAT_422_VIDEO) {
|
|
2396 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_422);
|
|
2397 if (image_ycc444_to_ycc422(img, c_h_phase) != 0) {
|
|
2398 error_convert:
|
|
2399 fprintf(stderr, "Cannot convert image\n");
|
|
2400 exit(1);
|
|
2401 }
|
|
2402 }
|
|
2403 }
|
|
2404
|
|
2405 cb_size = 8; /* XXX: should make it configurable. We assume the
|
|
2406 HEVC encoder uses the same value */
|
|
2407 width = img->w;
|
|
2408 height = img->h;
|
|
2409 image_pad(img, cb_size);
|
|
2410 if (img_alpha)
|
|
2411 image_pad(img_alpha, cb_size);
|
|
2412
|
|
2413 /* convert to the allocated pixel width to 8 bit if needed by the
|
|
2414 HEVC encoder */
|
|
2415 if (img->bit_depth == 8) {
|
|
2416 image_convert16to8(img);
|
|
2417 if (img_alpha)
|
|
2418 image_convert16to8(img_alpha);
|
|
2419 }
|
|
2420
|
|
2421 if (s->frame_count == 0) {
|
|
2422 memset(ep, 0, sizeof(*ep));
|
|
2423 ep->qp = p->qp;
|
|
2424 ep->width = img->w;
|
|
2425 ep->height = img->h;
|
|
2426 ep->chroma_format = img->format;
|
|
2427 ep->bit_depth = img->bit_depth;
|
|
2428 ep->intra_only = !p->animated;
|
|
2429 ep->lossless = p->lossless;
|
|
2430 ep->sei_decoded_picture_hash = p->sei_decoded_picture_hash;
|
|
2431 ep->compress_level = p->compress_level;
|
|
2432 ep->verbose = p->verbose;
|
|
2433
|
|
2434 s->enc_ctx = s->encoder->open(ep);
|
|
2435 if (!s->enc_ctx) {
|
|
2436 fprintf(stderr, "Error while opening encoder\n");
|
|
2437 exit(1);
|
|
2438 }
|
|
2439
|
|
2440 if (img_alpha) {
|
|
2441 if (p->alpha_qp < 0)
|
|
2442 ep->qp = p->qp;
|
|
2443 else
|
|
2444 ep->qp = p->alpha_qp;
|
|
2445 ep->chroma_format = 0;
|
|
2446
|
|
2447 s->alpha_enc_ctx = s->encoder->open(ep);
|
|
2448 if (!s->alpha_enc_ctx) {
|
|
2449 fprintf(stderr, "Error while opening alpha encoder\n");
|
|
2450 exit(1);
|
|
2451 }
|
|
2452 }
|
|
2453
|
|
2454 /* prepare the extension data */
|
|
2455 if (p->animated) {
|
|
2456 BPGMetaData *md;
|
|
2457 uint8_t buf[15], *q;
|
|
2458
|
|
2459 md = bpg_md_alloc(BPG_EXTENSION_TAG_ANIM_CONTROL);
|
|
2460 q = buf;
|
|
2461 put_ue(&q, p->loop_count);
|
|
2462 put_ue(&q, p->frame_delay_num);
|
|
2463 put_ue(&q, p->frame_delay_den);
|
|
2464 md->buf_len = q - buf;
|
|
2465 md->buf = malloc(md->buf_len);
|
|
2466 memcpy(md->buf, buf, md->buf_len);
|
|
2467 md->next = s->first_md;
|
|
2468 s->first_md = md;
|
|
2469 }
|
|
2470
|
|
2471 extension_buf = NULL;
|
|
2472 extension_buf_len = 0;
|
|
2473 if (s->first_md) {
|
|
2474 BPGMetaData *md1;
|
|
2475 int max_len;
|
|
2476 uint8_t *q;
|
|
2477
|
|
2478 max_len = 0;
|
|
2479 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) {
|
|
2480 max_len += md1->buf_len + 5 * 2;
|
|
2481 }
|
|
2482 extension_buf = malloc(max_len);
|
|
2483 q = extension_buf;
|
|
2484 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) {
|
|
2485 put_ue(&q, md1->tag);
|
|
2486 put_ue(&q, md1->buf_len);
|
|
2487 memcpy(q, md1->buf, md1->buf_len);
|
|
2488 q += md1->buf_len;
|
|
2489 }
|
|
2490 extension_buf_len = q - extension_buf;
|
|
2491
|
|
2492 bpg_md_free(s->first_md);
|
|
2493 s->first_md = NULL;
|
|
2494 }
|
|
2495
|
|
2496 {
|
|
2497 uint8_t img_header[128], *q;
|
|
2498 int v, has_alpha, has_extension, alpha2_flag, alpha1_flag, format;
|
|
2499
|
|
2500 has_alpha = (img_alpha != NULL);
|
|
2501 has_extension = (extension_buf_len > 0);
|
|
2502
|
|
2503
|
|
2504 if (has_alpha) {
|
|
2505 if (img->has_w_plane) {
|
|
2506 alpha1_flag = 0;
|
|
2507 alpha2_flag = 1;
|
|
2508 } else {
|
|
2509 alpha1_flag = 1;
|
|
2510 alpha2_flag = img->premultiplied_alpha;
|
|
2511 }
|
|
2512 } else {
|
|
2513 alpha1_flag = 0;
|
|
2514 alpha2_flag = 0;
|
|
2515 }
|
|
2516
|
|
2517 q = img_header;
|
|
2518 *q++ = (IMAGE_HEADER_MAGIC >> 24) & 0xff;
|
|
2519 *q++ = (IMAGE_HEADER_MAGIC >> 16) & 0xff;
|
|
2520 *q++ = (IMAGE_HEADER_MAGIC >> 8) & 0xff;
|
|
2521 *q++ = (IMAGE_HEADER_MAGIC >> 0) & 0xff;
|
|
2522
|
|
2523 if (img->c_h_phase == 0 && img->format == BPG_FORMAT_420)
|
|
2524 format = BPG_FORMAT_420_VIDEO;
|
|
2525 else if (img->c_h_phase == 0 && img->format == BPG_FORMAT_422)
|
|
2526 format = BPG_FORMAT_422_VIDEO;
|
|
2527 else
|
|
2528 format = img->format;
|
|
2529 v = (format << 5) | (alpha1_flag << 4) | (img->bit_depth - 8);
|
|
2530 *q++ = v;
|
|
2531 v = (img->color_space << 4) | (has_extension << 3) |
|
|
2532 (alpha2_flag << 2) | (img->limited_range << 1) |
|
|
2533 p->animated;
|
|
2534 *q++ = v;
|
|
2535 put_ue(&q, width);
|
|
2536 put_ue(&q, height);
|
|
2537
|
|
2538 put_ue(&q, 0); /* zero length means up to the end of the file */
|
|
2539 if (has_extension) {
|
|
2540 put_ue(&q, extension_buf_len); /* extension data length */
|
|
2541 }
|
|
2542
|
|
2543 write_func(opaque, img_header, q - img_header);
|
|
2544
|
|
2545 if (has_extension) {
|
|
2546 if (write_func(opaque, extension_buf, extension_buf_len) != extension_buf_len) {
|
|
2547 fprintf(stderr, "Error while writing extension data\n");
|
|
2548 exit(1);
|
|
2549 }
|
|
2550 free(extension_buf);
|
|
2551 }
|
|
2552 }
|
|
2553 }
|
|
2554
|
|
2555 /* store the frame duration */
|
|
2556 if ((s->frame_count + 1) > s->frame_duration_tab_size) {
|
|
2557 s->frame_duration_tab_size = (s->frame_duration_tab_size * 3) / 2;
|
|
2558 if (s->frame_duration_tab_size < (s->frame_count + 1))
|
|
2559 s->frame_duration_tab_size = (s->frame_count + 1);
|
|
2560 s->frame_duration_tab = realloc(s->frame_duration_tab,
|
|
2561 sizeof(s->frame_duration_tab) * s->frame_duration_tab_size);
|
|
2562 }
|
|
2563 s->frame_duration_tab[s->frame_count] = s->frame_ticks;
|
|
2564
|
|
2565 s->encoder->encode(s->enc_ctx, img);
|
|
2566
|
|
2567 if (img_alpha) {
|
|
2568 s->encoder->encode(s->alpha_enc_ctx, img_alpha);
|
|
2569 image_free(img_alpha);
|
|
2570 }
|
|
2571
|
|
2572 s->frame_count++;
|
|
2573
|
|
2574 if (!p->animated)
|
|
2575 bpg_encoder_encode_trailer(s, write_func, opaque);
|
|
2576
|
|
2577 return 0;
|
|
2578 }
|
|
2579
|
|
2580 void bpg_encoder_close(BPGEncoderContext *s)
|
|
2581 {
|
|
2582 free(s->frame_duration_tab);
|
|
2583 bpg_md_free(s->first_md);
|
|
2584 free(s);
|
|
2585 }
|
|
2586
|
|
2587 static int my_write_func(void *opaque, const uint8_t *buf, int buf_len)
|
|
2588 {
|
|
2589 FILE *f = opaque;
|
|
2590 return fwrite(buf, 1, buf_len, f);
|
|
2591 }
|
|
2592
|
|
2593 static int get_filename_num(char *buf, int buf_size, const char *str, int n)
|
|
2594 {
|
|
2595 const char *p, *r;
|
|
2596 char *q;
|
|
2597 int l, c;
|
|
2598
|
|
2599 q = buf;
|
|
2600 p = str;
|
|
2601 for(;;) {
|
|
2602 c = *p++;
|
|
2603 if (c == '\0')
|
|
2604 break;
|
|
2605 if (c == '%') {
|
|
2606 r = p - 1;
|
|
2607 l = 0;
|
|
2608 for(;;) {
|
|
2609 c = *p;
|
|
2610 if (c < '0' || c > '9')
|
|
2611 break;
|
|
2612 l = l * 10 + (c - '0');
|
|
2613 p++;
|
|
2614 }
|
|
2615 c = *p++;
|
|
2616 if (c == '%') {
|
|
2617 goto add_char;
|
|
2618 } else if (c != 'd') {
|
|
2619 return -1;
|
|
2620 }
|
|
2621 snprintf(q, buf + buf_size - q, "%0*u", l, n);
|
|
2622 q += strlen(q);
|
|
2623
|
|
2624 } else {
|
|
2625 add_char:
|
|
2626 if ((q - buf) < buf_size - 1)
|
|
2627 *q++ = c;
|
|
2628 }
|
|
2629 }
|
|
2630 *q = '\0';
|
|
2631 return 0;
|
|
2632 }
|
|
2633
|
|
2634 void help(int is_full)
|
|
2635 {
|
|
2636 char hevc_encoders[128];
|
|
2637 int i;
|
|
2638
|
|
2639 hevc_encoders[0] = '\0';
|
|
2640 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
|
|
2641 if (i != 0)
|
|
2642 strcat(hevc_encoders, " ");
|
|
2643 strcat(hevc_encoders, hevc_encoder_name[i]);
|
|
2644 }
|
|
2645
|
|
2646 printf("BPG Image Encoder version " CONFIG_BPG_VERSION "\n"
|
|
2647 "usage: bpgenc [options] infile.[jpg|png]\n"
|
|
2648 "\n"
|
|
2649 "Main options:\n"
|
|
2650 "-h show the full help (including the advanced options)\n"
|
|
2651 "-o outfile set output filename (default = %s)\n"
|
|
2652 "-q qp set quantizer parameter (smaller gives better quality,\n"
|
|
2653 " range: 0-51, default = %d)\n"
|
|
2654 "-f cfmt set the preferred chroma format (420, 422, 444,\n"
|
|
2655 " default=420)\n"
|
|
2656 "-c color_space set the preferred color space (ycbcr, rgb, ycgco,\n"
|
|
2657 " ycbcr_bt709, ycbcr_bt2020, default=ycbcr)\n"
|
|
2658 "-b bit_depth set the bit depth (8 to %d, default = %d)\n"
|
|
2659 "-lossless enable lossless mode\n"
|
|
2660 "-e encoder select the HEVC encoder (%s, default = %s)\n"
|
|
2661 "-m level select the compression level (1=fast, 9=slow, default = %d)\n"
|
|
2662 "\n"
|
|
2663 "Animation options:\n"
|
|
2664 "-a generate animations from a sequence of images. Use %%d or\n"
|
|
2665 " %%Nd (N = number of digits) in the filename to specify the\n"
|
|
2666 " image index, starting from 0 or 1.\n"
|
|
2667 "-fps N set the frame rate (default = 25)\n"
|
|
2668 "-loop N set the number of times the animation is played. 0 means\n"
|
|
2669 " infinite (default = 0)\n"
|
|
2670 "-delayfile file text file containing one number per image giving the\n"
|
|
2671 " display delay per image in centiseconds.\n"
|
|
2672 , DEFAULT_OUTFILENAME, DEFAULT_QP, BIT_DEPTH_MAX, DEFAULT_BIT_DEPTH,
|
|
2673 hevc_encoders, hevc_encoder_name[0], DEFAULT_COMPRESS_LEVEL);
|
|
2674
|
|
2675 if (is_full) {
|
|
2676 printf("\nAdvanced options:\n"
|
|
2677 "-alphaq set quantizer parameter for the alpha channel (default = same as -q value)\n"
|
|
2678 "-premul store the color with premultiplied alpha\n"
|
|
2679 "-limitedrange encode the color data with the limited range of video\n"
|
|
2680 "-hash include MD5 hash in HEVC bitstream\n"
|
|
2681 "-keepmetadata keep the metadata (from JPEG: EXIF, ICC profile, XMP, from PNG: ICC profile)\n"
|
|
2682 "-v show debug messages\n"
|
|
2683 );
|
|
2684 }
|
|
2685
|
|
2686 exit(1);
|
|
2687 }
|
|
2688
|
|
2689 struct option long_opts[] = {
|
|
2690 { "hash", no_argument },
|
|
2691 { "keepmetadata", no_argument },
|
|
2692 { "alphaq", required_argument },
|
|
2693 { "lossless", no_argument },
|
|
2694 { "limitedrange", no_argument },
|
|
2695 { "premul", no_argument },
|
|
2696 { "loop", required_argument },
|
|
2697 { "fps", required_argument },
|
|
2698 { "delayfile", required_argument },
|
|
2699 { NULL },
|
|
2700 };
|
|
2701
|
|
2702 int main(int argc, char **argv)
|
|
2703 {
|
|
2704 const char *infilename, *outfilename, *frame_delay_file;
|
|
2705 Image *img;
|
|
2706 FILE *f;
|
|
2707 int c, option_index;
|
|
2708 int keep_metadata;
|
|
2709 int bit_depth, i, limited_range, premultiplied_alpha;
|
|
2710 BPGColorSpaceEnum color_space;
|
|
2711 BPGMetaData *md;
|
|
2712 BPGEncoderContext *enc_ctx;
|
|
2713 BPGEncoderParameters *p;
|
|
2714
|
|
2715 p = bpg_encoder_param_alloc();
|
|
2716
|
|
2717 outfilename = DEFAULT_OUTFILENAME;
|
|
2718 color_space = BPG_CS_YCbCr;
|
|
2719 keep_metadata = 0;
|
|
2720 bit_depth = DEFAULT_BIT_DEPTH;
|
|
2721 limited_range = 0;
|
|
2722 premultiplied_alpha = 0;
|
|
2723 frame_delay_file = NULL;
|
|
2724
|
|
2725 for(;;) {
|
|
2726 c = getopt_long_only(argc, argv, "q:o:hf:c:vm:b:e:a", long_opts, &option_index);
|
|
2727 if (c == -1)
|
|
2728 break;
|
|
2729 switch(c) {
|
|
2730 case 0:
|
|
2731 switch(option_index) {
|
|
2732 case 0:
|
|
2733 p->sei_decoded_picture_hash = 1;
|
|
2734 break;
|
|
2735 case 1:
|
|
2736 keep_metadata = 1;
|
|
2737 break;
|
|
2738 case 2:
|
|
2739 p->alpha_qp = atoi(optarg);
|
|
2740 if (p->alpha_qp < 0 || p->alpha_qp > 51) {
|
|
2741 fprintf(stderr, "alpha_qp must be between 0 and 51\n");
|
|
2742 exit(1);
|
|
2743 }
|
|
2744 break;
|
|
2745 case 3:
|
|
2746 p->lossless = 1;
|
|
2747 color_space = BPG_CS_RGB;
|
|
2748 p->preferred_chroma_format = BPG_FORMAT_444;
|
|
2749 bit_depth = 8;
|
|
2750 limited_range = 0;
|
|
2751 break;
|
|
2752 case 4:
|
|
2753 limited_range = 1;
|
|
2754 break;
|
|
2755 case 5:
|
|
2756 premultiplied_alpha = 1;
|
|
2757 break;
|
|
2758 case 6:
|
|
2759 p->loop_count = strtoul(optarg, NULL, 0);
|
|
2760 break;
|
|
2761 case 7:
|
|
2762 p->frame_delay_num = 1;
|
|
2763 p->frame_delay_den = strtoul(optarg, NULL, 0);
|
|
2764 if (p->frame_delay_den == 0) {
|
|
2765 fprintf(stderr, "invalid frame rate\n");
|
|
2766 exit(1);
|
|
2767 }
|
|
2768 break;
|
|
2769 case 8:
|
|
2770 frame_delay_file = optarg;
|
|
2771 break;
|
|
2772 default:
|
|
2773 goto show_help;
|
|
2774 }
|
|
2775 break;
|
|
2776 case 'h':
|
|
2777 show_help:
|
|
2778 help(1);
|
|
2779 break;
|
|
2780 case 'q':
|
|
2781 p->qp = atoi(optarg);
|
|
2782 if (p->qp < 0 || p->qp > 51) {
|
|
2783 fprintf(stderr, "qp must be between 0 and 51\n");
|
|
2784 exit(1);
|
|
2785 }
|
|
2786 break;
|
|
2787 case 'o':
|
|
2788 outfilename = optarg;
|
|
2789 break;
|
|
2790 case 'f':
|
|
2791 if (!strcmp(optarg, "420")) {
|
|
2792 p->preferred_chroma_format = BPG_FORMAT_420;
|
|
2793 } else if (!strcmp(optarg, "422")) {
|
|
2794 p->preferred_chroma_format = BPG_FORMAT_422;
|
|
2795 } else if (!strcmp(optarg, "444")) {
|
|
2796 p->preferred_chroma_format = BPG_FORMAT_444;
|
|
2797 } else if (!strcmp(optarg, "422_video")) {
|
|
2798 p->preferred_chroma_format = BPG_FORMAT_422_VIDEO;
|
|
2799 } else if (!strcmp(optarg, "420_video")) {
|
|
2800 p->preferred_chroma_format = BPG_FORMAT_420_VIDEO;
|
|
2801 } else {
|
|
2802 fprintf(stderr, "Invalid chroma format\n");
|
|
2803 exit(1);
|
|
2804 }
|
|
2805 break;
|
|
2806 case 'c':
|
|
2807 if (!strcmp(optarg, "ycbcr")) {
|
|
2808 color_space = BPG_CS_YCbCr;
|
|
2809 } else if (!strcmp(optarg, "rgb")) {
|
|
2810 color_space = BPG_CS_RGB;
|
|
2811 p->preferred_chroma_format = BPG_FORMAT_444;
|
|
2812 } else if (!strcmp(optarg, "ycgco")) {
|
|
2813 color_space = BPG_CS_YCgCo;
|
|
2814 } else if (!strcmp(optarg, "ycbcr_bt709")) {
|
|
2815 color_space = BPG_CS_YCbCr_BT709;
|
|
2816 } else if (!strcmp(optarg, "ycbcr_bt2020")) {
|
|
2817 color_space = BPG_CS_YCbCr_BT2020;
|
|
2818 } else {
|
|
2819 fprintf(stderr, "Invalid color space format\n");
|
|
2820 exit(1);
|
|
2821 }
|
|
2822 break;
|
|
2823 case 'm':
|
|
2824 p->compress_level = atoi(optarg);
|
|
2825 if (p->compress_level < 1)
|
|
2826 p->compress_level = 1;
|
|
2827 else if (p->compress_level > 9)
|
|
2828 p->compress_level = 9;
|
|
2829 break;
|
|
2830 case 'b':
|
|
2831 bit_depth = atoi(optarg);
|
|
2832 if (bit_depth < 8 || bit_depth > BIT_DEPTH_MAX) {
|
|
2833 fprintf(stderr, "Invalid bit depth (range: 8 to %d)\n",
|
|
2834 BIT_DEPTH_MAX);
|
|
2835 exit(1);
|
|
2836 }
|
|
2837 break;
|
|
2838 case 'v':
|
|
2839 p->verbose++;
|
|
2840 break;
|
|
2841 case 'e':
|
|
2842 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
|
|
2843 if (!strcmp(optarg, hevc_encoder_name[i]))
|
|
2844 break;
|
|
2845 }
|
|
2846 if (i == HEVC_ENCODER_COUNT) {
|
|
2847 fprintf(stderr, "Unsupported encoder. Available ones are:");
|
|
2848 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
|
|
2849 fprintf(stderr, " %s", hevc_encoder_name[i]);
|
|
2850 }
|
|
2851 fprintf(stderr, "\n");
|
|
2852 exit(1);
|
|
2853 }
|
|
2854 p->encoder_type = i;
|
|
2855 break;
|
|
2856 case 'a':
|
|
2857 p->animated = 1;
|
|
2858 break;
|
|
2859 default:
|
|
2860 exit(1);
|
|
2861 }
|
|
2862 }
|
|
2863
|
|
2864 if (optind >= argc)
|
|
2865 help(0);
|
|
2866 infilename = argv[optind];
|
|
2867
|
|
2868 f = fopen(outfilename, "wb");
|
|
2869 if (!f) {
|
|
2870 perror(outfilename);
|
|
2871 exit(1);
|
|
2872 }
|
|
2873
|
|
2874 enc_ctx = bpg_encoder_open(p);
|
|
2875 if (!enc_ctx) {
|
|
2876 fprintf(stderr, "Could not open BPG encoder\n");
|
|
2877 exit(1);
|
|
2878 }
|
|
2879
|
|
2880 if (p->animated) {
|
|
2881 int frame_num, first_frame, frame_ticks;
|
|
2882 char filename[1024];
|
|
2883 FILE *f1;
|
|
2884
|
|
2885 if (frame_delay_file) {
|
|
2886 f1 = fopen(frame_delay_file, "r");
|
|
2887 if (!f1) {
|
|
2888 fprintf(stderr, "Could not open '%s'\n", frame_delay_file);
|
|
2889 exit(1);
|
|
2890 }
|
|
2891 } else {
|
|
2892 f1 = NULL;
|
|
2893 }
|
|
2894
|
|
2895 first_frame = 1;
|
|
2896 for(frame_num = 0; ; frame_num++) {
|
|
2897 if (get_filename_num(filename, sizeof(filename), infilename, frame_num) < 0) {
|
|
2898 fprintf(stderr, "Invalid filename syntax: '%s'\n", infilename);
|
|
2899 exit(1);
|
|
2900 }
|
|
2901 img = load_image(&md, filename, color_space, bit_depth, limited_range,
|
|
2902 premultiplied_alpha);
|
|
2903 if (!img) {
|
|
2904 if (frame_num == 0)
|
|
2905 continue; /* accept to start at 0 or 1 */
|
|
2906 if (first_frame) {
|
|
2907 fprintf(stderr, "Could not read '%s'\n", filename);
|
|
2908 exit(1);
|
|
2909 } else {
|
|
2910 break;
|
|
2911 }
|
|
2912 }
|
|
2913 frame_ticks = 1;
|
|
2914 if (f1) {
|
|
2915 float fdelay;
|
|
2916 if (fscanf(f1, "%f", &fdelay) == 1) {
|
|
2917 frame_ticks = lrint(fdelay * p->frame_delay_den / (p->frame_delay_num * 100));
|
|
2918 if (frame_ticks < 1)
|
|
2919 frame_ticks = 1;
|
|
2920 }
|
|
2921 }
|
|
2922
|
|
2923 if (p->verbose)
|
|
2924 printf("Encoding '%s' ticks=%d\n", filename, frame_ticks);
|
|
2925
|
|
2926 if (keep_metadata && first_frame) {
|
|
2927 bpg_encoder_set_extension_data(enc_ctx, md);
|
|
2928 } else {
|
|
2929 bpg_md_free(md);
|
|
2930 }
|
|
2931 bpg_encoder_set_frame_duration(enc_ctx, frame_ticks);
|
|
2932 bpg_encoder_encode(enc_ctx, img, my_write_func, f);
|
|
2933 image_free(img);
|
|
2934
|
|
2935 first_frame = 0;
|
|
2936 }
|
|
2937 if (f1)
|
|
2938 fclose(f1);
|
|
2939 /* end of stream */
|
|
2940 bpg_encoder_encode(enc_ctx, NULL, my_write_func, f);
|
|
2941 } else {
|
|
2942 img = load_image(&md, infilename, color_space, bit_depth, limited_range,
|
|
2943 premultiplied_alpha);
|
|
2944 if (!img) {
|
|
2945 fprintf(stderr, "Could not read '%s'\n", infilename);
|
|
2946 exit(1);
|
|
2947 }
|
|
2948
|
|
2949 if (!keep_metadata && md) {
|
|
2950 bpg_md_free(md);
|
|
2951 md = NULL;
|
|
2952 }
|
|
2953
|
|
2954 bpg_encoder_set_extension_data(enc_ctx, md);
|
|
2955
|
|
2956 bpg_encoder_encode(enc_ctx, img, my_write_func, f);
|
|
2957 image_free(img);
|
|
2958 }
|
|
2959
|
|
2960 fclose(f);
|
|
2961
|
|
2962 bpg_encoder_close(enc_ctx);
|
|
2963
|
|
2964 bpg_encoder_param_free(p);
|
|
2965
|
|
2966 return 0;
|
|
2967 }
|