forked from mirrorer/libbpg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bpgenc.c
2955 lines (2670 loc) · 91.9 KB
/
bpgenc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BPG encoder
*
* Copyright (c) 2014 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <getopt.h>
#include <math.h>
#include <assert.h>
#include <png.h>
#include <jpeglib.h>
/* need this to determine whether x265 can output desired bitdepth */
#if USE_X265
#include "x265.h"
#endif
#include "bpgfmt.h"
#include "bpgenc.h"
#ifdef USE_JCTVC
# define HAVE_JCTVC 1
#else
# define HAVE_JCTVC 0
#endif
int have_jctvc = HAVE_JCTVC;
typedef uint16_t PIXEL;
static inline int clamp_pix(int a, int pixel_max)
{
if (a < 0)
return 0;
else if (a > pixel_max)
return pixel_max;
else
return a;
}
static inline int sub_mod_int(int a, int b, int m)
{
a -= b;
if (a < 0)
a += m;
return a;
}
static inline int add_mod_int(int a, int b, int m)
{
a += b;
if (a >= m)
a -= m;
return a;
}
typedef struct {
int c_shift;
int c_rnd;
int c_0_25, c_0_5, c_one;
int rgb_to_ycc[3 * 3];
int y_one;
int y_offset;
int bit_depth;
int pixel_max;
int c_center;
} ColorConvertState;
static void convert_init(ColorConvertState *s, int in_bit_depth,
int out_bit_depth, BPGColorSpaceEnum color_space,
int limited_range)
{
double k_r, k_b, mult, mult_y, mult_c;
int in_pixel_max, out_pixel_max, c_shift, i;
double rgb_to_ycc[3 * 3];
/* XXX: could use one more bit */
c_shift = 31 - out_bit_depth;
in_pixel_max = (1 << in_bit_depth) - 1;
out_pixel_max = (1 << out_bit_depth) - 1;
mult = (double)out_pixel_max * (1 << c_shift) / (double)in_pixel_max;
// printf("mult=%f c_shift=%d\n", mult, c_shift);
if (limited_range) {
mult_y = (double)(219 << (out_bit_depth - 8)) * (1 << c_shift) /
(double)in_pixel_max;
mult_c = (double)(224 << (out_bit_depth - 8)) * (1 << c_shift) /
(double)in_pixel_max;
} else {
mult_y = mult;
mult_c = mult;
}
switch(color_space) {
case BPG_CS_YCbCr:
k_r = 0.299;
k_b = 0.114;
goto convert_ycc;
case BPG_CS_YCbCr_BT709:
k_r = 0.2126;
k_b = 0.0722;
goto convert_ycc;
case BPG_CS_YCbCr_BT2020:
k_r = 0.2627;
k_b = 0.0593;
convert_ycc:
rgb_to_ycc[0] = k_r;
rgb_to_ycc[1] = 1 - k_r - k_b;
rgb_to_ycc[2] = k_b;
rgb_to_ycc[3] = -0.5 * k_r / (1 - k_b);
rgb_to_ycc[4] = -0.5 * (1 - k_r - k_b) / (1 - k_b);
rgb_to_ycc[5] = 0.5;
rgb_to_ycc[6] = 0.5;
rgb_to_ycc[7] = -0.5 * (1 - k_r - k_b) / (1 - k_r);
rgb_to_ycc[8] = -0.5 * k_b / (1 - k_r);
for(i = 0; i < 3; i++)
s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_y);
for(i = 3; i < 9; i++)
s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_c);
break;
case BPG_CS_YCgCo:
s->c_0_25 = lrint(0.25 * mult_y);
s->c_0_5 = lrint(0.5 * mult_y);
break;
default:
break;
}
s->c_one = lrint(mult);
s->c_shift = c_shift;
s->c_rnd = (1 << (c_shift - 1));
if (limited_range) {
s->y_offset = s->c_rnd + (16 << (c_shift + out_bit_depth - 8));
s->y_one = lrint(mult_y);
} else {
s->y_offset = s->c_rnd;
s->y_one = s->c_one;
}
s->bit_depth = out_bit_depth;
s->c_center = 1 << (out_bit_depth - 1);
s->pixel_max = out_pixel_max;
}
/* 8 bit input */
static void rgb24_to_ycc(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint8_t *src = src1;
int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
int pixel_max, y_offset;
c0 = s->rgb_to_ycc[0];
c1 = s->rgb_to_ycc[1];
c2 = s->rgb_to_ycc[2];
c3 = s->rgb_to_ycc[3];
c4 = s->rgb_to_ycc[4];
c5 = s->rgb_to_ycc[5];
c6 = s->rgb_to_ycc[6];
c7 = s->rgb_to_ycc[7];
c8 = s->rgb_to_ycc[8];
shift = s->c_shift;
rnd = s->c_rnd;
y_offset = s->y_offset;
center = s->c_center;
pixel_max = s->pixel_max;
for(i = 0; i < n; i++) {
r = src[0];
g = src[1];
b = src[2];
y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
y_offset) >> shift, pixel_max);
cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
rnd) >> shift) + center, pixel_max);
cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
rnd) >> shift) + center, pixel_max);
src += incr;
}
}
static void rgb24_to_rgb(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint8_t *src = src1;
int i, r, g, b, c, shift, rnd;
c = s->y_one;
shift = s->c_shift;
rnd = s->y_offset;
for(i = 0; i < n; i++) {
r = src[0];
g = src[1];
b = src[2];
y_ptr[i] = (c * g + rnd) >> shift;
cb_ptr[i] = (c * b + rnd) >> shift;
cr_ptr[i] = (c * r + rnd) >> shift;
src += incr;
}
}
static void rgb24_to_ycgco(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint8_t *src = src1;
int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
int y_offset;
c_0_25 = s->c_0_25;
c_0_5 = s->c_0_5;
rnd = s->c_rnd;
shift = s->c_shift;
pixel_max = s->pixel_max;
center = s->c_center;
y_offset = s->y_offset;
for(i = 0; i < n; i++) {
r = src[0];
g = src[1];
b = src[2];
t1 = c_0_5 * g;
t2 = c_0_25 * (r + b);
y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
pixel_max);
cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
rnd) >> shift) + center, pixel_max);
src += incr;
}
}
/* Note: used for alpha/W so no limited range */
static void gray8_to_gray(ColorConvertState *s,
PIXEL *y_ptr, const uint8_t *src, int n, int incr)
{
int i, g, c, shift, rnd;
c = s->c_one;
shift = s->c_shift;
rnd = s->c_rnd;
for(i = 0; i < n; i++) {
g = src[0];
y_ptr[i] = (c * g + rnd) >> shift;
src += incr;
}
}
static void luma8_to_gray(ColorConvertState *s,
PIXEL *y_ptr, const uint8_t *src, int n, int incr)
{
int i, g, c, shift, rnd;
c = s->y_one;
shift = s->c_shift;
rnd = s->y_offset;
for(i = 0; i < n; i++) {
g = src[0];
y_ptr[i] = (c * g + rnd) >> shift;
src += incr;
}
}
/* 16 bit input */
static void rgb48_to_ycc(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint16_t *src = src1;
int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
int pixel_max, y_offset;
c0 = s->rgb_to_ycc[0];
c1 = s->rgb_to_ycc[1];
c2 = s->rgb_to_ycc[2];
c3 = s->rgb_to_ycc[3];
c4 = s->rgb_to_ycc[4];
c5 = s->rgb_to_ycc[5];
c6 = s->rgb_to_ycc[6];
c7 = s->rgb_to_ycc[7];
c8 = s->rgb_to_ycc[8];
shift = s->c_shift;
rnd = s->c_rnd;
y_offset = s->y_offset;
center = s->c_center;
pixel_max = s->pixel_max;
for(i = 0; i < n; i++) {
r = src[0];
g = src[1];
b = src[2];
y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
y_offset) >> shift, pixel_max);
cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
rnd) >> shift) + center, pixel_max);
cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
rnd) >> shift) + center, pixel_max);
src += incr;
}
}
static void rgb48_to_ycgco(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint16_t *src = src1;
int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
int y_offset;
c_0_25 = s->c_0_25;
c_0_5 = s->c_0_5;
rnd = s->c_rnd;
y_offset = s->y_offset;
shift = s->c_shift;
pixel_max = s->pixel_max;
center = s->c_center;
for(i = 0; i < n; i++) {
r = src[0];
g = src[1];
b = src[2];
t1 = c_0_5 * g;
t2 = c_0_25 * (r + b);
y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
pixel_max);
cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
rnd) >> shift) + center, pixel_max);
src += incr;
}
}
/* Note: use for alpha/W so no limited range */
static void gray16_to_gray(ColorConvertState *s,
PIXEL *y_ptr, const uint16_t *src, int n, int incr)
{
int i, g, c, shift, rnd;
c = s->c_one;
shift = s->c_shift;
rnd = s->c_rnd;
for(i = 0; i < n; i++) {
g = src[0];
y_ptr[i] = (c * g + rnd) >> shift;
src += incr;
}
}
static void luma16_to_gray(ColorConvertState *s,
PIXEL *y_ptr, const uint16_t *src, int n, int incr)
{
int i, g, c, shift, rnd;
c = s->y_one;
shift = s->c_shift;
rnd = s->y_offset;
for(i = 0; i < n; i++) {
g = src[0];
y_ptr[i] = (c * g + rnd) >> shift;
src += incr;
}
}
static void rgb48_to_rgb(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src1, int n, int incr)
{
const uint16_t *src = src1;
luma16_to_gray(s, y_ptr, src + 1, n, incr);
luma16_to_gray(s, cb_ptr, src + 2, n, incr);
luma16_to_gray(s, cr_ptr, src + 0, n, incr);
}
typedef void RGBConvertFunc(ColorConvertState *s,
PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
const void *src, int n, int incr);
static RGBConvertFunc *rgb_to_cs[2][BPG_CS_COUNT] = {
{
rgb24_to_ycc,
rgb24_to_rgb,
rgb24_to_ycgco,
rgb24_to_ycc,
rgb24_to_ycc,
},
{
rgb48_to_ycc,
rgb48_to_rgb,
rgb48_to_ycgco,
rgb48_to_ycc,
rgb48_to_ycc,
}
};
/* val = 1.0 - val */
static void gray_one_minus(ColorConvertState *s, PIXEL *y_ptr, int n)
{
int pixel_max = s->pixel_max;
int i;
for(i = 0; i < n; i++) {
y_ptr[i] = pixel_max - y_ptr[i];
}
}
/* val = -val for chroma */
static void gray_neg_c(ColorConvertState *s, PIXEL *y_ptr, int n)
{
int pixel_max = s->pixel_max;
int i, v;
for(i = 0; i < n; i++) {
v = y_ptr[i];
if (v == 0)
v = pixel_max;
else
v = pixel_max + 1 - v;
y_ptr[i] = v;
}
}
/* decimation */
/* phase = 0 */
#define DP0TAPS2 7
#define DP0TAPS (2 * DP0TAPS + 1)
#define DP0C0 64
#define DP0C1 40
#define DP0C3 (-11)
#define DP0C5 4
#define DP0C7 (-1)
/* phase = 0.5 */
#define DP1TAPS2 5
#define DP1TAPS (2 * DP1TAPS2)
#define DP1C0 57
#define DP1C1 17
#define DP1C2 (-8)
#define DP1C3 (-4)
#define DP1C4 2
#define DTAPS_MAX 7
/* chroma aligned with luma samples */
static void decimate2p0_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
{
int n2, i, pixel_max;
pixel_max = (1 << bit_depth) - 1;
n2 = (n + 1) / 2;
for(i = 0; i < n2; i++) {
dst[i] = clamp_pix(((src[-7] + src[7]) * DP0C7 +
(src[-5] + src[5]) * DP0C5 +
(src[-3] + src[3]) * DP0C3 +
(src[-1] + src[1]) * DP0C1 +
src[0] * DP0C0 + 64) >> 7, pixel_max);
src += 2;
}
}
/* same with more precision and no saturation */
static void decimate2p0_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
{
int n2, i, shift, rnd;
shift = bit_depth - 7;
rnd = 1 << (shift - 1);
n2 = (n + 1) / 2;
for(i = 0; i < n2; i++) {
dst[i] = ((src[-7] + src[7]) * DP0C7 +
(src[-5] + src[5]) * DP0C5 +
(src[-3] + src[3]) * DP0C3 +
(src[-1] + src[1]) * DP0C1 +
src[0] * DP0C0 + rnd) >> shift;
src += 2;
}
}
/* chroma half way between luma samples */
static void decimate2p1_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
{
int n2, i, pixel_max;
pixel_max = (1 << bit_depth) - 1;
n2 = (n + 1) / 2;
for(i = 0; i < n2; i++) {
dst[i] = clamp_pix(((src[-4] + src[5]) * DP1C4 +
(src[-3] + src[4]) * DP1C3 +
(src[-2] + src[3]) * DP1C2 +
(src[-1] + src[2]) * DP1C1 +
(src[0] + src[1]) * DP1C0 + 64) >> 7, pixel_max);
src += 2;
}
}
/* same with more precision and no saturation */
static void decimate2p1_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
{
int n2, i, shift, rnd;
shift = bit_depth - 7;
rnd = 1 << (shift - 1);
n2 = (n + 1) / 2;
for(i = 0; i < n2; i++) {
dst[i] = ((src[-4] + src[5]) * DP1C4 +
(src[-3] + src[4]) * DP1C3 +
(src[-2] + src[3]) * DP1C2 +
(src[-1] + src[2]) * DP1C1 +
(src[0] + src[1]) * DP1C0 + rnd) >> shift;
src += 2;
}
}
static void decimate2_h(PIXEL *dst, PIXEL *src, int n, int bit_depth, int phase)
{
PIXEL *src1, v;
int d, i;
if (phase == 0)
d = DP0TAPS2;
else
d = DP1TAPS2;
/* add edge pixels */
src1 = malloc(sizeof(PIXEL) * (n + 2 * d));
v = src[0];
for(i = 0; i < d; i++)
src1[i] = v;
memcpy(src1 + d, src, n * sizeof(PIXEL));
v = src[n - 1];
for(i = 0; i < d; i++)
src1[d + n + i] = v;
if (phase == 0)
decimate2p0_simple(dst, src1 + d, n, bit_depth);
else
decimate2p1_simple(dst, src1 + d, n, bit_depth);
free(src1);
}
/* src1 is a temporary buffer of length n + 2 * DTAPS */
static void decimate2_h16(int16_t *dst, PIXEL *src, int n, PIXEL *src1,
int bit_depth, int phase)
{
PIXEL v;
int d, i;
if (phase == 0)
d = DP0TAPS2;
else
d = DP1TAPS2;
/* add edge pixels */
v = src[0];
for(i = 0; i < d; i++)
src1[i] = v;
memcpy(src1 + d, src, n * sizeof(PIXEL));
v = src[n - 1];
for(i = 0; i < d; i++)
src1[d + n + i] = v;
if (phase == 0)
decimate2p0_simple16(dst, src1 + d, n, bit_depth);
else
decimate2p1_simple16(dst, src1 + d, n, bit_depth);
}
static void decimate2_v(PIXEL *dst, int16_t **src, int pos, int n,
int bit_depth)
{
int16_t *src0, *src1, *src2, *src3, *src4, *src5, *srcm1, *srcm2, *srcm3, *srcm4;
int i, shift, offset, pixel_max;
pos = sub_mod_int(pos, 4, DP1TAPS);
srcm4 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
srcm3 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
srcm2 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
srcm1 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src0 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src1 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src2 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src3 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src4 = src[pos];
pos = add_mod_int(pos, 1, DP1TAPS);
src5 = src[pos];
shift = 21 - bit_depth;
offset = 1 << (shift - 1);
pixel_max = (1 << bit_depth) - 1;
for(i = 0; i < n; i++) {
dst[i] = clamp_pix(((srcm4[i] + src5[i]) * DP1C4 +
(srcm3[i] + src4[i]) * DP1C3 +
(srcm2[i] + src3[i]) * DP1C2 +
(srcm1[i] + src2[i]) * DP1C1 +
(src0[i] + src1[i]) * DP1C0 + offset) >> shift, pixel_max);
}
}
/* Note: we do the horizontal decimation first to use less CPU cache */
static void decimate2_hv(uint8_t *dst, int dst_linesize,
uint8_t *src, int src_linesize,
int w, int h, int bit_depth, int h_phase)
{
PIXEL *buf1;
int16_t *buf2[DP1TAPS];
int w2, pos, i, y, y1, y2;
w2 = (w + 1) / 2;
buf1 = malloc(sizeof(PIXEL) * (w + 2 * DTAPS_MAX));
/* init line buffer */
for(i = 0; i < DP1TAPS; i++) {
buf2[i] = malloc(sizeof(int16_t) * w2);
y = i;
if (y > DP1TAPS2)
y -= DP1TAPS;
if (y < 0) {
/* copy from first line */
memcpy(buf2[i], buf2[0], sizeof(int16_t) * w2);
} else if (y >= h) {
/* copy from last line (only happens for small height) */
memcpy(buf2[i], buf2[h - 1], sizeof(int16_t) * w2);
} else {
decimate2_h16(buf2[i], (PIXEL *)(src + src_linesize * y), w,
buf1, bit_depth, h_phase);
}
}
for(y = 0; y < h; y++) {
pos = y % DP1TAPS;
if ((y & 1) == 0) {
/* filter one line */
y2 = y >> 1;
decimate2_v((PIXEL *)(dst + y2 * dst_linesize), buf2,
pos, w2, bit_depth);
}
/* add a new line in the buffer */
y1 = y + DP1TAPS2 + 1;
pos = add_mod_int(pos, DP1TAPS2 + 1, DP1TAPS);
if (y1 >= h) {
/* copy last line */
memcpy(buf2[pos], buf2[sub_mod_int(pos, 1, DP1TAPS)],
sizeof(int16_t) * w2);
} else {
/* horizontally decimate new line */
decimate2_h16(buf2[pos], (PIXEL *)(src + src_linesize * y1), w,
buf1, bit_depth, h_phase);
}
}
for(i = 0; i < DP1TAPS; i++)
free(buf2[i]);
free(buf1);
}
static void get_plane_res(Image *img, int *pw, int *ph, int i)
{
if (img->format == BPG_FORMAT_420 && (i == 1 || i == 2)) {
*pw = (img->w + 1) / 2;
*ph = (img->h + 1) / 2;
} else if (img->format == BPG_FORMAT_422 && (i == 1 || i == 2)) {
*pw = (img->w + 1) / 2;
*ph = img->h;
} else {
*pw = img->w;
*ph = img->h;
}
}
#define W_PAD 16
Image *image_alloc(int w, int h, BPGImageFormatEnum format, int has_alpha,
BPGColorSpaceEnum color_space, int bit_depth)
{
Image *img;
int i, linesize, w1, h1, c_count;
img = malloc(sizeof(Image));
memset(img, 0, sizeof(*img));
img->w = w;
img->h = h;
img->format = format;
img->has_alpha = has_alpha;
img->bit_depth = bit_depth;
img->color_space = color_space;
img->pixel_shift = 1;
img->c_h_phase = 1;
if (img->format == BPG_FORMAT_GRAY)
c_count = 1;
else
c_count = 3;
if (has_alpha)
c_count++;
for(i = 0; i < c_count; i++) {
get_plane_res(img, &w1, &h1, i);
/* multiple of 16 pixels to add borders */
w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
linesize = w1 << img->pixel_shift;
img->data[i] = malloc(linesize * h1);
img->linesize[i] = linesize;
}
return img;
}
void image_free(Image *img)
{
int i, c_count;
if (img->format == BPG_FORMAT_GRAY)
c_count = 1;
else
c_count = 3;
if (img->has_alpha)
c_count++;
for(i = 0; i < c_count; i++)
free(img->data[i]);
free(img);
}
int image_ycc444_to_ycc422(Image *img, int h_phase)
{
uint8_t *data1;
int w1, h1, bpp, linesize1, i, y;
if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
return -1;
bpp = 2;
w1 = (img->w + 1) / 2;
w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
h1 = (img->h + (W_PAD - 1)) & ~(W_PAD - 1);
linesize1 = bpp * w1;
for(i = 1; i <= 2; i++) {
data1 = malloc(linesize1 * h1);
for(y = 0; y < img->h; y++) {
decimate2_h((PIXEL *)(data1 + y * linesize1),
(PIXEL *)(img->data[i] + y * img->linesize[i]),
img->w, img->bit_depth, h_phase);
}
free(img->data[i]);
img->data[i] = data1;
img->linesize[i] = linesize1;
}
img->format = BPG_FORMAT_422;
img->c_h_phase = h_phase;
return 0;
}
int image_ycc444_to_ycc420(Image *img, int h_phase)
{
uint8_t *data1;
int w1, h1, bpp, linesize1, i;
if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
return -1;
bpp = 2;
w1 = (img->w + 1) / 2;
h1 = (img->h + 1) / 2;
w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
linesize1 = bpp * w1;
for(i = 1; i <= 2; i++) {
data1 = malloc(linesize1 * h1);
decimate2_hv(data1, linesize1,
img->data[i], img->linesize[i],
img->w, img->h, img->bit_depth, h_phase);
free(img->data[i]);
img->data[i] = data1;
img->linesize[i] = linesize1;
}
img->format = BPG_FORMAT_420;
img->c_h_phase = h_phase;
return 0;
}
/* duplicate right and bottom samples so that the image has a width
and height multiple of cb_size (power of two) */
void image_pad(Image *img, int cb_size)
{
int w1, h1, x, y, c_count, c_w, c_h, c_w1, c_h1, h_shift, v_shift, c_idx;
PIXEL *ptr, v, *ptr1;
assert(img->pixel_shift == 1);
if (cb_size <= 1)
return;
w1 = (img->w + cb_size - 1) & ~(cb_size - 1);
h1 = (img->h + cb_size - 1) & ~(cb_size - 1);
if (img->format == BPG_FORMAT_GRAY)
c_count = 1;
else
c_count = 3;
if (img->has_alpha)
c_count++;
for(c_idx = 0; c_idx < c_count; c_idx++) {
if (img->format == BPG_FORMAT_420 &&
(c_idx == 1 || c_idx == 2)) {
h_shift = 1;
v_shift = 1;
} else if (img->format == BPG_FORMAT_422 &&
(c_idx == 1 || c_idx == 2)) {
h_shift = 1;
v_shift = 0;
} else {
h_shift = 0;
v_shift = 0;
}
c_w = (img->w + h_shift) >> h_shift;
c_h = (img->h + v_shift) >> v_shift;
c_w1 = w1 >> h_shift;
c_h1 = h1 >> v_shift;
/* pad horizontally */
for(y = 0; y < c_h; y++) {
ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
v = ptr[c_w - 1];
for(x = c_w; x < c_w1; x++) {
ptr[x] = v;
}
}
/* pad vertically */
ptr1 = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * (c_h - 1));
for(y = c_h; y < c_h1; y++) {
ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
memcpy(ptr, ptr1, c_w1 * sizeof(PIXEL));
}
}
img->w = w1;
img->h = h1;
}
/* convert the 16 bit components to 8 bits */
void image_convert16to8(Image *img)
{
int w, h, stride, y, x, c_count, i;
uint8_t *plane;
if (img->bit_depth > 8 || img->pixel_shift != 1)
return;
if (img->format == BPG_FORMAT_GRAY)
c_count = 1;
else
c_count = 3;
if (img->has_alpha)
c_count++;
for(i = 0; i < c_count; i++) {
get_plane_res(img, &w, &h, i);
stride = w;
plane = malloc(stride * h);
for(y = 0; y < h; y++) {
const uint16_t *src;
uint8_t *dst;
dst = plane + stride * y;
src = (uint16_t *)(img->data[i] + img->linesize[i] * y);
for(x = 0; x < w; x++)
dst[x] = src[x];
}
free(img->data[i]);
img->data[i] = plane;
img->linesize[i] = stride;
}
img->pixel_shift = 0;
}
Image *read_png(BPGMetaData **pmd,
FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth,
int limited_range, int premultiplied_alpha, int lossless)
{
png_structp png_ptr;
png_infop info_ptr;
int bit_depth, color_type;
Image *img;
uint8_t **rows;
int y, has_alpha, linesize, bpp;
BPGImageFormatEnum format;
ColorConvertState cvt_s, *cvt = &cvt_s;
BPGMetaData *md, **plast_md, *first_md;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (png_ptr == NULL) {
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
return NULL;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return NULL;
}
png_init_io(png_ptr, f);
png_read_info(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
switch (color_type) {
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(png_ptr);
bit_depth = 8;
break;
case PNG_COLOR_TYPE_GRAY:
case PNG_COLOR_TYPE_GRAY_ALPHA:
if (bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
bit_depth = 8;
}
break;
}
assert(bit_depth == 8 || bit_depth == 16);
if (lossless) {
if (bit_depth < out_bit_depth) {
fprintf(stderr, "HBD lossless encode of low depth input is wasteful\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return NULL;
}
if (bit_depth > out_bit_depth)
fprintf(stderr, "Input is downshifted. Might not be lossless\n");
}
#if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
if (bit_depth == 16) {
png_set_swap(png_ptr);
}
#endif
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
format = BPG_FORMAT_GRAY;
color_space = BPG_CS_YCbCr;
} else {
format = BPG_FORMAT_444;
}
has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
has_alpha = 1;
}
if (premultiplied_alpha) {
png_set_alpha_mode(png_ptr, PNG_ALPHA_ASSOCIATED, PNG_GAMMA_LINEAR);
}
img = image_alloc(png_get_image_width(png_ptr, info_ptr),
png_get_image_height(png_ptr, info_ptr),
format, has_alpha, color_space,
out_bit_depth);
img->limited_range = limited_range;
img->premultiplied_alpha = premultiplied_alpha;
rows = malloc(sizeof(rows[0]) * img->h);
if (format == BPG_FORMAT_GRAY)
bpp = (1 + has_alpha) * (bit_depth / 8);
else
bpp = (3 + has_alpha) * (bit_depth / 8);
linesize = bpp * img->w;
for (y = 0; y < img->h; y++) {
rows[y] = malloc(linesize);
}
png_read_image(png_ptr, rows);