annotate bpgenc.c @ 34:5d51fff843eb default tip

A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Mar 2020 19:18:48 +0200
parents 33594243ce31
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * BPG encoder
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 *
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Copyright (c) 2014 Fabrice Bellard
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 * of this software and associated documentation files (the "Software"), to deal
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 * in the Software without restriction, including without limitation the rights
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 * copies of the Software, and to permit persons to whom the Software is
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 * furnished to do so, subject to the following conditions:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 *
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 * The above copyright notice and this permission notice shall be included in
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 * all copies or substantial portions of the Software.
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 *
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 * THE SOFTWARE.
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 #include <stdlib.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #include <stdio.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #include <string.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #include <inttypes.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #include <getopt.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #include <math.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 #include <assert.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 #include <png.h>
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 #include <jpeglib.h>
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
34 #include <tiffio.h>
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
35 #ifdef HAVE_LIBEXIF
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
36 #include <exif-data.h>
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
37 #endif
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 #include "bpgenc.h"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
41
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
42
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 typedef uint16_t PIXEL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 static void put_ue(uint8_t **pp, uint32_t v);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 static inline int clamp_pix(int a, int pixel_max)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 if (a < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 else if (a > pixel_max)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 return pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 return a;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 static inline int sub_mod_int(int a, int b, int m)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 a -= b;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 if (a < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 a += m;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 return a;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 static inline int add_mod_int(int a, int b, int m)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 a += b;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 if (a >= m)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 a -= m;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 return a;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 typedef struct {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 int c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 int c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 int c_0_25, c_0_5, c_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 int rgb_to_ycc[3 * 3];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 int y_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 int y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 int bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 int pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 int c_center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 } ColorConvertState;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
85 static void convert_init(ColorConvertState *s, int in_bit_depth,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 int out_bit_depth, BPGColorSpaceEnum color_space,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 int limited_range)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 double k_r, k_b, mult, mult_y, mult_c;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 int in_pixel_max, out_pixel_max, c_shift, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 double rgb_to_ycc[3 * 3];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 /* XXX: could use one more bit */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 c_shift = 31 - out_bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 in_pixel_max = (1 << in_bit_depth) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 out_pixel_max = (1 << out_bit_depth) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 mult = (double)out_pixel_max * (1 << c_shift) / (double)in_pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 // printf("mult=%f c_shift=%d\n", mult, c_shift);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 if (limited_range) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
100 mult_y = (double)(219 << (out_bit_depth - 8)) * (1 << c_shift) /
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 (double)in_pixel_max;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
102 mult_c = (double)(224 << (out_bit_depth - 8)) * (1 << c_shift) /
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 (double)in_pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 mult_y = mult;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 mult_c = mult;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 switch(color_space) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 case BPG_CS_YCbCr:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 k_r = 0.299;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 k_b = 0.114;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 goto convert_ycc;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
113
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 case BPG_CS_YCbCr_BT709:
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
115 k_r = 0.2126;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 k_b = 0.0722;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 goto convert_ycc;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
118
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 case BPG_CS_YCbCr_BT2020:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 k_r = 0.2627;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 k_b = 0.0593;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 convert_ycc:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 rgb_to_ycc[0] = k_r;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 rgb_to_ycc[1] = 1 - k_r - k_b;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 rgb_to_ycc[2] = k_b;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 rgb_to_ycc[3] = -0.5 * k_r / (1 - k_b);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 rgb_to_ycc[4] = -0.5 * (1 - k_r - k_b) / (1 - k_b);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 rgb_to_ycc[5] = 0.5;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 rgb_to_ycc[6] = 0.5;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 rgb_to_ycc[7] = -0.5 * (1 - k_r - k_b) / (1 - k_r);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 rgb_to_ycc[8] = -0.5 * k_b / (1 - k_r);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
132
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 for(i = 0; i < 3; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 for(i = 3; i < 9; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_c);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 case BPG_CS_YCgCo:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 s->c_0_25 = lrint(0.25 * mult_y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 s->c_0_5 = lrint(0.5 * mult_y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 s->c_one = lrint(mult);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 s->c_shift = c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 s->c_rnd = (1 << (c_shift - 1));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 if (limited_range) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 s->y_offset = s->c_rnd + (16 << (c_shift + out_bit_depth - 8));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 s->y_one = lrint(mult_y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 s->y_offset = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 s->y_one = s->c_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 s->bit_depth = out_bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 s->c_center = 1 << (out_bit_depth - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 s->pixel_max = out_pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 /* 8 bit input */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 static void rgb24_to_ycc(ColorConvertState *s,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 const uint8_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 int pixel_max, y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 c0 = s->rgb_to_ycc[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 c1 = s->rgb_to_ycc[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 c2 = s->rgb_to_ycc[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 c3 = s->rgb_to_ycc[3];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 c4 = s->rgb_to_ycc[4];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 c5 = s->rgb_to_ycc[5];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 c6 = s->rgb_to_ycc[6];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 c7 = s->rgb_to_ycc[7];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 c8 = s->rgb_to_ycc[8];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 y_offset = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 center = s->c_center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 r = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 g = src[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 b = src[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 y_offset) >> shift, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
191 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 rnd) >> shift) + center, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
193 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 rnd) >> shift) + center, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 static void rgb24_to_rgb(ColorConvertState *s,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 const uint8_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 int i, r, g, b, c, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 c = s->y_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 rnd = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 r = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 g = src[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 b = src[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 y_ptr[i] = (c * g + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 cb_ptr[i] = (c * b + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 cr_ptr[i] = (c * r + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 static void rgb24_to_ycgco(ColorConvertState *s,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 const uint8_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 int y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 c_0_25 = s->c_0_25;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 c_0_5 = s->c_0_5;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 center = s->c_center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 y_offset = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 r = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 g = src[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 b = src[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 t1 = c_0_5 * g;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 t2 = c_0_25 * (r + b);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
242 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 rnd) >> shift) + center, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 /* Note: used for alpha/W so no limited range */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 static void gray8_to_gray(ColorConvertState *s,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 PIXEL *y_ptr, const uint8_t *src, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 int i, g, c, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 c = s->c_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 g = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 y_ptr[i] = (c * g + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 static void luma8_to_gray(ColorConvertState *s,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 PIXEL *y_ptr, const uint8_t *src, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 int i, g, c, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 c = s->y_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 rnd = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 g = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 y_ptr[i] = (c * g + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 /* 16 bit input */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
283 static void rgb48_to_ycc(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 const uint16_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 int pixel_max, y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 c0 = s->rgb_to_ycc[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 c1 = s->rgb_to_ycc[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 c2 = s->rgb_to_ycc[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 c3 = s->rgb_to_ycc[3];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 c4 = s->rgb_to_ycc[4];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 c5 = s->rgb_to_ycc[5];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 c6 = s->rgb_to_ycc[6];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 c7 = s->rgb_to_ycc[7];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 c8 = s->rgb_to_ycc[8];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 y_offset = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 center = s->c_center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 r = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 g = src[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 b = src[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b +
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 y_offset) >> shift, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
311 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 rnd) >> shift) + center, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
313 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 rnd) >> shift) + center, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
319 static void rgb48_to_ycgco(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 const uint16_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 int y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 c_0_25 = s->c_0_25;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 c_0_5 = s->c_0_5;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 y_offset = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 center = s->c_center;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 r = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 g = src[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 b = src[2];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 t1 = c_0_5 * g;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 t2 = c_0_25 * (r + b);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
341 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) +
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 rnd) >> shift) + center, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 /* Note: use for alpha/W so no limited range */
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
350 static void gray16_to_gray(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 PIXEL *y_ptr, const uint16_t *src, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 int i, g, c, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 c = s->c_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 rnd = s->c_rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 g = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 y_ptr[i] = (c * g + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
365 static void luma16_to_gray(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 PIXEL *y_ptr, const uint16_t *src, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 int i, g, c, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 c = s->y_one;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 shift = s->c_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 rnd = s->y_offset;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 g = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 y_ptr[i] = (c * g + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 src += incr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
380 static void rgb48_to_rgb(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 const void *src1, int n, int incr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 const uint16_t *src = src1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 luma16_to_gray(s, y_ptr, src + 1, n, incr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 luma16_to_gray(s, cb_ptr, src + 2, n, incr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 luma16_to_gray(s, cr_ptr, src + 0, n, incr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
391 typedef void RGBConvertFunc(ColorConvertState *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 const void *src, int n, int incr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 static RGBConvertFunc *rgb_to_cs[2][BPG_CS_COUNT] = {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 rgb24_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 rgb24_to_rgb,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 rgb24_to_ycgco,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 rgb24_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 rgb24_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 },
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 rgb48_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 rgb48_to_rgb,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 rgb48_to_ycgco,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 rgb48_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 rgb48_to_ycc,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 };
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
411
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 /* val = 1.0 - val */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 static void gray_one_minus(ColorConvertState *s, PIXEL *y_ptr, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 int pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 int i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 y_ptr[i] = pixel_max - y_ptr[i];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 /* val = -val for chroma */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 static void gray_neg_c(ColorConvertState *s, PIXEL *y_ptr, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 int pixel_max = s->pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 int i, v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 v = y_ptr[i];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 if (v == 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 v = pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 v = pixel_max + 1 - v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 y_ptr[i] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 /* decimation */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 /* phase = 0 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 #define DP0TAPS2 7
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 #define DP0TAPS (2 * DP0TAPS + 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 #define DP0C0 64
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 #define DP0C1 40
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 #define DP0C3 (-11)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 #define DP0C5 4
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 #define DP0C7 (-1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 /* phase = 0.5 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 #define DP1TAPS2 5
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 #define DP1TAPS (2 * DP1TAPS2)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 #define DP1C0 57
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 #define DP1C1 17
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 #define DP1C2 (-8)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 #define DP1C3 (-4)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 #define DP1C4 2
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 #define DTAPS_MAX 7
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 /* chroma aligned with luma samples */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 static void decimate2p0_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 int n2, i, pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 pixel_max = (1 << bit_depth) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 n2 = (n + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 for(i = 0; i < n2; i++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
469 dst[i] = clamp_pix(((src[-7] + src[7]) * DP0C7 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
470 (src[-5] + src[5]) * DP0C5 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
471 (src[-3] + src[3]) * DP0C3 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
472 (src[-1] + src[1]) * DP0C1 +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 src[0] * DP0C0 + 64) >> 7, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 src += 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 /* same with more precision and no saturation */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 static void decimate2p0_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 int n2, i, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 shift = bit_depth - 7;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 rnd = 1 << (shift - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 n2 = (n + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 for(i = 0; i < n2; i++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
486 dst[i] = ((src[-7] + src[7]) * DP0C7 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
487 (src[-5] + src[5]) * DP0C5 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
488 (src[-3] + src[3]) * DP0C3 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
489 (src[-1] + src[1]) * DP0C1 +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 src[0] * DP0C0 + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 src += 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 /* chroma half way between luma samples */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 static void decimate2p1_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 int n2, i, pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 pixel_max = (1 << bit_depth) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 n2 = (n + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 for(i = 0; i < n2; i++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
503 dst[i] = clamp_pix(((src[-4] + src[5]) * DP1C4 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
504 (src[-3] + src[4]) * DP1C3 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
505 (src[-2] + src[3]) * DP1C2 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
506 (src[-1] + src[2]) * DP1C1 +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 (src[0] + src[1]) * DP1C0 + 64) >> 7, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 src += 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 /* same with more precision and no saturation */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 static void decimate2p1_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 int n2, i, shift, rnd;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 shift = bit_depth - 7;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 rnd = 1 << (shift - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 n2 = (n + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 for(i = 0; i < n2; i++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
520 dst[i] = ((src[-4] + src[5]) * DP1C4 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
521 (src[-3] + src[4]) * DP1C3 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
522 (src[-2] + src[3]) * DP1C2 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
523 (src[-1] + src[2]) * DP1C1 +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 (src[0] + src[1]) * DP1C0 + rnd) >> shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 src += 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 static void decimate2_h(PIXEL *dst, PIXEL *src, int n, int bit_depth, int phase)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 PIXEL *src1, v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 int d, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
534 if (phase == 0)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 d = DP0TAPS2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 d = DP1TAPS2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 /* add edge pixels */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 src1 = malloc(sizeof(PIXEL) * (n + 2 * d));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 v = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 for(i = 0; i < d; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 src1[i] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 memcpy(src1 + d, src, n * sizeof(PIXEL));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 v = src[n - 1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 for(i = 0; i < d; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 src1[d + n + i] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 if (phase == 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 decimate2p0_simple(dst, src1 + d, n, bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 decimate2p1_simple(dst, src1 + d, n, bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 free(src1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 /* src1 is a temporary buffer of length n + 2 * DTAPS */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 static void decimate2_h16(int16_t *dst, PIXEL *src, int n, PIXEL *src1,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 int bit_depth, int phase)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 PIXEL v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 int d, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
561 if (phase == 0)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 d = DP0TAPS2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 d = DP1TAPS2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 /* add edge pixels */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 v = src[0];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 for(i = 0; i < d; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 src1[i] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 memcpy(src1 + d, src, n * sizeof(PIXEL));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 v = src[n - 1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 for(i = 0; i < d; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 src1[d + n + i] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 if (phase == 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 decimate2p0_simple16(dst, src1 + d, n, bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 decimate2p1_simple16(dst, src1 + d, n, bit_depth);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
577
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 static void decimate2_v(PIXEL *dst, int16_t **src, int pos, int n,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 int16_t *src0, *src1, *src2, *src3, *src4, *src5, *srcm1, *srcm2, *srcm3, *srcm4;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 int i, shift, offset, pixel_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 pos = sub_mod_int(pos, 4, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 srcm4 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 srcm3 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 srcm2 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 srcm1 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 src0 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 src1 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 src2 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 src3 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 src4 = src[pos];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 pos = add_mod_int(pos, 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 src5 = src[pos];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
606
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 shift = 21 - bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 offset = 1 << (shift - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 pixel_max = (1 << bit_depth) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 for(i = 0; i < n; i++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
611 dst[i] = clamp_pix(((srcm4[i] + src5[i]) * DP1C4 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
612 (srcm3[i] + src4[i]) * DP1C3 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
613 (srcm2[i] + src3[i]) * DP1C2 +
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
614 (srcm1[i] + src2[i]) * DP1C1 +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 (src0[i] + src1[i]) * DP1C0 + offset) >> shift, pixel_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 /* Note: we do the horizontal decimation first to use less CPU cache */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 static void decimate2_hv(uint8_t *dst, int dst_linesize,
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
621 uint8_t *src, int src_linesize,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 int w, int h, int bit_depth, int h_phase)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 PIXEL *buf1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 int16_t *buf2[DP1TAPS];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 int w2, pos, i, y, y1, y2;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
627
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 w2 = (w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 buf1 = malloc(sizeof(PIXEL) * (w + 2 * DTAPS_MAX));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 /* init line buffer */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 for(i = 0; i < DP1TAPS; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 buf2[i] = malloc(sizeof(int16_t) * w2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 y = i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 if (y > DP1TAPS2)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 y -= DP1TAPS;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 if (y < 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 /* copy from first line */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 memcpy(buf2[i], buf2[0], sizeof(int16_t) * w2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640 } else if (y >= h) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 /* copy from last line (only happens for small height) */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 memcpy(buf2[i], buf2[h - 1], sizeof(int16_t) * w2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644 decimate2_h16(buf2[i], (PIXEL *)(src + src_linesize * y), w,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 buf1, bit_depth, h_phase);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 for(y = 0; y < h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 pos = y % DP1TAPS;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 if ((y & 1) == 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652 /* filter one line */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 y2 = y >> 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654 decimate2_v((PIXEL *)(dst + y2 * dst_linesize), buf2,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655 pos, w2, bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 /* add a new line in the buffer */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658 y1 = y + DP1TAPS2 + 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 pos = add_mod_int(pos, DP1TAPS2 + 1, DP1TAPS);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 if (y1 >= h) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 /* copy last line */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662 memcpy(buf2[pos], buf2[sub_mod_int(pos, 1, DP1TAPS)],
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 sizeof(int16_t) * w2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 /* horizontally decimate new line */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 decimate2_h16(buf2[pos], (PIXEL *)(src + src_linesize * y1), w,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 buf1, bit_depth, h_phase);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 for(i = 0; i < DP1TAPS; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672 free(buf2[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 free(buf1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 static void get_plane_res(Image *img, int *pw, int *ph, int i)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678 if (img->format == BPG_FORMAT_420 && (i == 1 || i == 2)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 *pw = (img->w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 *ph = (img->h + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 } else if (img->format == BPG_FORMAT_422 && (i == 1 || i == 2)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 *pw = (img->w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 *ph = img->h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 *pw = img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 *ph = img->h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
690 #define W_PAD 16
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692 Image *image_alloc(int w, int h, BPGImageFormatEnum format, int has_alpha,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693 BPGColorSpaceEnum color_space, int bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 Image *img;
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
696 int i, c_count;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
697
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
698 if ((img = malloc(sizeof(Image))) == NULL)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
699 return NULL;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
700
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
701 memset(img, 0, sizeof(Image));
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
702
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 img->w = w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 img->h = h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 img->format = format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 img->has_alpha = has_alpha;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707 img->bit_depth = bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 img->color_space = color_space;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 img->pixel_shift = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 img->c_h_phase = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 c_count = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 c_count = 3;
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
716
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 if (has_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718 c_count++;
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
719
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 for(i = 0; i < c_count; i++) {
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
721 int w1, h1, linesize;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
722
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 get_plane_res(img, &w1, &h1, i);
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
724
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725 /* multiple of 16 pixels to add borders */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
728
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729 linesize = w1 << img->pixel_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 img->data[i] = malloc(linesize * h1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 img->linesize[i] = linesize;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733 return img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 void image_free(Image *img)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 int i, c_count;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740 c_count = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 c_count = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 if (img->has_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744 c_count++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 for(i = 0; i < c_count; i++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 free(img->data[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747 free(img);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
749
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750 int image_ycc444_to_ycc422(Image *img, int h_phase)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
751 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 uint8_t *data1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753 int w1, h1, bpp, linesize1, i, y;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 bpp = 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 w1 = (img->w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760 h1 = (img->h + (W_PAD - 1)) & ~(W_PAD - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 linesize1 = bpp * w1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 for(i = 1; i <= 2; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
763 data1 = malloc(linesize1 * h1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 for(y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 decimate2_h((PIXEL *)(data1 + y * linesize1),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 (PIXEL *)(img->data[i] + y * img->linesize[i]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767 img->w, img->bit_depth, h_phase);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
769 free(img->data[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 img->data[i] = data1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 img->linesize[i] = linesize1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773 img->format = BPG_FORMAT_422;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774 img->c_h_phase = h_phase;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
776 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778 int image_ycc444_to_ycc420(Image *img, int h_phase)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
779 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
780 uint8_t *data1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781 int w1, h1, bpp, linesize1, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 bpp = 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786 w1 = (img->w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
787 h1 = (img->h + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
789 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790 linesize1 = bpp * w1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 for(i = 1; i <= 2; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792 data1 = malloc(linesize1 * h1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
793 decimate2_hv(data1, linesize1,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794 img->data[i], img->linesize[i],
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795 img->w, img->h, img->bit_depth, h_phase);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
796 free(img->data[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 img->data[i] = data1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
798 img->linesize[i] = linesize1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
799 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800 img->format = BPG_FORMAT_420;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
801 img->c_h_phase = h_phase;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
804
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 /* duplicate right and bottom samples so that the image has a width
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 and height multiple of cb_size (power of two) */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807 void image_pad(Image *img, int cb_size)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
808 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809 int w1, h1, x, y, c_count, c_w, c_h, c_w1, c_h1, h_shift, v_shift, c_idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810 PIXEL *ptr, v, *ptr1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
811
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812 assert(img->pixel_shift == 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
813 if (cb_size <= 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
814 return;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
815 w1 = (img->w + cb_size - 1) & ~(cb_size - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
816 h1 = (img->h + cb_size - 1) & ~(cb_size - 1);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
817
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
818 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
819 c_count = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
820 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 c_count = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
822 if (img->has_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
823 c_count++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824 for(c_idx = 0; c_idx < c_count; c_idx++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
825 if (img->format == BPG_FORMAT_420 &&
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
826 (c_idx == 1 || c_idx == 2)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
827 h_shift = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
828 v_shift = 1;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
829 } else if (img->format == BPG_FORMAT_422 &&
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
830 (c_idx == 1 || c_idx == 2)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
831 h_shift = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
832 v_shift = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
833 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
834 h_shift = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
835 v_shift = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
836 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
837
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
838 c_w = (img->w + h_shift) >> h_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
839 c_h = (img->h + v_shift) >> v_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
840 c_w1 = w1 >> h_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
841 c_h1 = h1 >> v_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
842
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
843 /* pad horizontally */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
844 for(y = 0; y < c_h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
845 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
846 v = ptr[c_w - 1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
847 for(x = c_w; x < c_w1; x++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
848 ptr[x] = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852 /* pad vertically */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853 ptr1 = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * (c_h - 1));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
854 for(y = c_h; y < c_h1; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 memcpy(ptr, ptr1, c_w1 * sizeof(PIXEL));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859 img->w = w1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 img->h = h1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
862
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
863 /* convert the 16 bit components to 8 bits */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
864 void image_convert16to8(Image *img)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 int w, h, stride, y, x, c_count, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 uint8_t *plane;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869 if (img->bit_depth > 8 || img->pixel_shift != 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
870 return;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
872 c_count = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
874 c_count = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
875 if (img->has_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 c_count++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877 for(i = 0; i < c_count; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878 get_plane_res(img, &w, &h, i);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879 stride = w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 plane = malloc(stride * h);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881 for(y = 0; y < h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
882 const uint16_t *src;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 uint8_t *dst;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884 dst = plane + stride * y;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885 src = (uint16_t *)(img->data[i] + img->linesize[i] * y);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886 for(x = 0; x < w; x++)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
887 dst[x] = src[x];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
888 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
889 free(img->data[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
890 img->data[i] = plane;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891 img->linesize[i] = stride;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
892 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
893 img->pixel_shift = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
894 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
895
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
896 typedef struct BPGMetaData {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
897 uint32_t tag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
898 uint8_t *buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
899 int buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
900 struct BPGMetaData *next;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
901 } BPGMetaData;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
902
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
903 BPGMetaData *bpg_md_alloc(uint32_t tag)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905 BPGMetaData *md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
906 md = malloc(sizeof(BPGMetaData));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907 memset(md, 0, sizeof(*md));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908 md->tag = tag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909 return md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
910 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
911
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 void bpg_md_free(BPGMetaData *md)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
914 BPGMetaData *md_next;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
916 while (md != NULL) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917 md_next = md->next;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
918 free(md->buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
919 free(md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920 md = md_next;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
921 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
923
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
924 void bpg_insert_md(BPGMetaData **list, BPGMetaData *node)
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
925 {
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
926 if (*list == NULL)
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
927 {
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
928 *list = node;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
929 node->next = NULL;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
930 }
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
931 else
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
932 {
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
933 node->next = *list;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
934 *list = node;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
935 }
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
936 }
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
937
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
938 int bpg_add_md_contents(BPGMetaData **list, const int tag, const size_t len, const void *buf)
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
939 {
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
940 BPGMetaData *md;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
941
19
a9a60da06ae3 Check for NULL buffer and 0 length of data in bpg_add_md_contents().
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
942 if (buf == NULL || len == 0)
a9a60da06ae3 Check for NULL buffer and 0 length of data in bpg_add_md_contents().
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
943 return -2;
a9a60da06ae3 Check for NULL buffer and 0 length of data in bpg_add_md_contents().
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
944
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
945 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
946 char *filename = NULL, *mtype = NULL;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
947 FILE *fh;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
948
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
949 switch (tag)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
950 {
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
951 case BPG_EXTENSION_TAG_XMP: filename = "paska.xmp"; mtype = "XMP"; break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
952 case BPG_EXTENSION_TAG_EXIF: filename = "paska.exif"; mtype = "EXIF"; break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
953 case BPG_EXTENSION_TAG_ICCP: filename = "paska.iccp"; mtype = "ICCP"; break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
954 default: mtype = "???";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
955 }
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
956 printf("bpg_add_md_contents(%p, %s (%d), %d, %p)\n", list, mtype, tag, len, buf);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
957 if (filename != NULL && (fh = fopen(filename, "wb")) != NULL)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
958 {
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
959 printf("bpg_add_md_contents() writing to file '%s' ..\n", filename);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
960 fwrite(buf, len, 1, fh);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
961 fclose(fh);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
962 }
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
963 #endif
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
964
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
965 if ((md = bpg_md_alloc(tag)) == NULL)
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
966 goto err;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
967
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
968 if ((md->buf = malloc(len)) == NULL)
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
969 goto err;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
970
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
971 md->buf_len = len;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
972 memcpy(md->buf, buf, len);
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
973 bpg_insert_md(list, md);
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
974 return 0;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
975
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
976 err:
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
977 bpg_md_free(md);
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
978 return -1;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
979 }
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
980
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
981
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
982 #ifdef HAVE_LIBEXIF
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
983 typedef struct
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
984 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
985 int tag; // Note: libtiff TIFF tags for EXIF are same as EXIF tags themselves
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
986 char *name;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
987 int ifd;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
988 } TIFFTagConvertInfo;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
989
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
990
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
991 static const TIFFTagConvertInfo tiff_tag_convert_table[] =
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
992 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
993 { TIFFTAG_ORIENTATION , "Orientation" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
994 { TIFFTAG_XRESOLUTION , "XResolution" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
995 { TIFFTAG_YRESOLUTION , "YResolution" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
996 { TIFFTAG_RESOLUTIONUNIT , "ResolutionUnit" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
997 { TIFFTAG_IMAGEDESCRIPTION , "ImageDescription" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
998 { TIFFTAG_MAKE , "Make" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
999 { TIFFTAG_MODEL , "Model" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1000 { TIFFTAG_SOFTWARE , "Software" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1001 { TIFFTAG_ARTIST , "Artist" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1002 { TIFFTAG_COPYRIGHT , "Copyright" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1003 { TIFFTAG_DATETIME , "DateTime" , EXIF_IFD_0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1004 { 0 , NULL , 0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1005 };
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1006
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1007
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1008 static const TIFFTagConvertInfo exif_tag_convert_table[] =
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1009 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1010 { EXIFTAG_EXPOSURETIME , "ExposureTime" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1011 { EXIFTAG_FNUMBER , "FNumber" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1012 { EXIFTAG_EXPOSUREPROGRAM , "ExposureProgram" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1013 { EXIFTAG_SPECTRALSENSITIVITY , "Spectralsensitivity" , EXIF_IFD_EXIF },
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1014 // { EXIFTAG_ISOSPEEDRATINGS , "ISOSpeed" , EXIF_IFD_EXIF },
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1015 // { EXIFTAG_OECF , "OptoElectricConversion" , EXIF_IFD_EXIF },
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1016 // { EXIFTAG_EXIFVERSION , "ExifVersion" , EXIF_IFD_EXIF },
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1017 { EXIFTAG_DATETIMEORIGINAL , "DateTimeOriginal" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1018 { EXIFTAG_DATETIMEDIGITIZED , "DateTimeDigitized" , EXIF_IFD_EXIF },
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1019 // { EXIFTAG_COMPONENTSCONFIGURATION , "ComponentsConfiguration" , EXIF_IFD_EXIF },
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1020 // { EXIFTAG_COMPRESSEDBITSPERPIXEL , "ImageCompression" , EXIF_IFD_EXIF },
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1021 { EXIFTAG_SHUTTERSPEEDVALUE , "ShutterSpeed" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1022 { EXIFTAG_APERTUREVALUE , "Aperture" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1023 { EXIFTAG_BRIGHTNESSVALUE , "Brightness" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1024 { EXIFTAG_EXPOSUREBIASVALUE , "ExposureBias" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1025 { EXIFTAG_MAXAPERTUREVALUE , "MaxAperture" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1026 { EXIFTAG_SUBJECTDISTANCE , "SubjectDistance" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1027
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1028 { EXIFTAG_METERINGMODE , "MeteringMode" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1029 { EXIFTAG_LIGHTSOURCE , "Lightsource" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1030 { EXIFTAG_FLASH , "Flash" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1031 { EXIFTAG_FOCALLENGTH , "FocalLength" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1032 { EXIFTAG_SUBJECTAREA , "SubjectArea" , EXIF_IFD_EXIF },
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1033 // { EXIFTAG_MAKERNOTE , "MakerNote" , EXIF_IFD_EXIF },
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1034 { EXIFTAG_USERCOMMENT , "UserComment" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1035 { EXIFTAG_SUBSECTIME , "SubSecTime" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1036 { EXIFTAG_SUBSECTIMEORIGINAL , "SubSecTimeOriginal" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1037 { EXIFTAG_SUBSECTIMEDIGITIZED , "SubSecTimeDigitized" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1038
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1039 // { EXIFTAG_FLASHPIXVERSION , "FlashpixVersion" , EXIF_IFD_EXIF },
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1040 { EXIFTAG_COLORSPACE , "Colorspace" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1041 { EXIFTAG_PIXELXDIMENSION , "Validimage" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1042 { EXIFTAG_PIXELYDIMENSION , "Validimage" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1043 { EXIFTAG_RELATEDSOUNDFILE , "Relatedaudio" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1044 { EXIFTAG_FLASHENERGY , "Flashenergy" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1045 { EXIFTAG_SPATIALFREQUENCYRESPONSE , "SpatialFrequency" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1046 { EXIFTAG_FOCALPLANEXRESOLUTION , "XResolution" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1047 { EXIFTAG_FOCALPLANEYRESOLUTION , "YResolution" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1048 { EXIFTAG_FOCALPLANERESOLUTIONUNIT , "ResolutionUnit" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1049 { EXIFTAG_SUBJECTLOCATION , "SubjectLocation" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1050 { EXIFTAG_EXPOSUREINDEX , "ExposureIndex" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1051 { EXIFTAG_SENSINGMETHOD , "SensingMethod" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1052 { EXIFTAG_FILESOURCE , "FileSource" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1053 { EXIFTAG_SCENETYPE , "SceneType" , EXIF_IFD_EXIF },
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1054 // { EXIFTAG_CFAPATTERN , "CFApattern" , EXIF_IFD_EXIF },
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1055 { EXIFTAG_CUSTOMRENDERED , "CustomRendered" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1056 { EXIFTAG_EXPOSUREMODE , "ExposureMode" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1057 { EXIFTAG_WHITEBALANCE , "WhiteBalance" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1058 { EXIFTAG_DIGITALZOOMRATIO , "DigitalZoom" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1059 { EXIFTAG_FOCALLENGTHIN35MMFILM , "FocalLength35mm" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1060 { EXIFTAG_SCENECAPTURETYPE , "SceneCapture" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1061 { EXIFTAG_GAINCONTROL , "GainControl" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1062 { EXIFTAG_CONTRAST , "Contrast" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1063 { EXIFTAG_SATURATION , "Saturation" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1064 { EXIFTAG_SHARPNESS , "Sharpness" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1065 { EXIFTAG_DEVICESETTINGDESCRIPTION , "DeviceSettings" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1066 { EXIFTAG_SUBJECTDISTANCERANGE , "SubjectDistance" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1067 { EXIFTAG_IMAGEUNIQUEID , "ImageUniqueID" , EXIF_IFD_EXIF },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1068
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1069 { 0 , NULL , 0 },
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1070 };
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1071
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1072
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1073 static void tiff_tags_convert(TIFF *tif, const TIFFTagConvertInfo *table,
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1074 ExifMem *mem, ExifData *exif, int *added)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1075 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1076 const TIFFTagConvertInfo *conv;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1077
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1078 for (conv = table; conv->tag != 0 && conv->name != NULL; conv++)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1079 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1080 const TIFFField *tfd = TIFFFieldWithTag(tif, conv->tag);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1081 TIFFDataType type = TIFFFieldDataType(tfd);
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1082 int len = TIFFDataWidth(type), components;
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1083 uint8_t tmp_buf[256], *data;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1084 char *tmp_str;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1085 int have;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1086
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1087 switch (type)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1088 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1089 case TIFF_ASCII:
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1090 have = TIFFGetField(tif, conv->tag, &tmp_str) && tmp_str != NULL;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1091 if (have)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1092 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1093 data = (uint8_t *) tmp_str;
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1094 components = strlen(tmp_str) + 1;
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1095 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1096 break;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1097
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1098 case TIFF_UNDEFINED:
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1099 have = 0;
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1100 break;
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1101
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1102 default:
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1103 have = TIFFGetField(tif, conv->tag, &tmp_buf);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1104 data = tmp_buf;
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1105 components = 1;
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1106 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1107
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1108 if (have)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1109 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1110 ExifEntry *entry = exif_entry_new_mem(mem);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1111 if (entry == NULL)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1112 return;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1113
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1114 entry->data = exif_mem_alloc(mem, len * components);
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1115 if (entry->data == NULL)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1116 return;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1117
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1118 memcpy(entry->data, data, len * components);
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1119 entry->size = len;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1120 entry->tag = conv->tag;
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1121 entry->components = components;
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1122 entry->format = type;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1123
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1124 exif_content_add_entry(exif->ifd[conv->ifd], entry);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1125 (*added)++;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1126 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1127 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1128 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1129 #endif
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1130
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1131
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1132 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1133 static void tiff_tags_dump(TIFF *tif, const TIFFTagConvertInfo *table)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1134 {
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1135 const TIFFTagConvertInfo *conv;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1136
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1137 for (conv = table; conv->tag != 0 && conv->name != NULL; conv++)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1138 {
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1139 const TIFFField *tfd = TIFFFieldWithTag(tif, conv->tag);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1140 TIFFDataType type = TIFFFieldDataType(tfd);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1141 uint16_t tmp_short;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1142 float tmp_rat;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1143 char *tmp_str, *name = "?";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1144 char tmp_buf[128] = "-";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1145 int have;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1146
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1147 switch (type)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1148 {
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1149 case TIFF_SHORT:
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1150 name = "short";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1151 if ((have = TIFFGetField(tif, conv->tag, &tmp_short)))
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1152 snprintf(tmp_buf, sizeof(tmp_buf), "%d", tmp_short);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1153 break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1154 case TIFF_RATIONAL:
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1155 name = "rational";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1156 if ((have = TIFFGetField(tif, conv->tag, &tmp_rat)))
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1157 snprintf(tmp_buf, sizeof(tmp_buf), "%1.2f", tmp_rat);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1158 break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1159 case TIFF_ASCII:
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1160 name = "ascii";
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1161 if ((have = (TIFFGetField(tif, conv->tag, &tmp_str) && tmp_str != NULL)))
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1162 snprintf(tmp_buf, sizeof(tmp_buf), "'%s'", tmp_str);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1163 break;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1164
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1165 default:
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1166 have = 0;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1167 }
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1168 if (have)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1169 printf("%-20s [%-10s]: %s\n", conv->name, name, tmp_buf);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1170 }
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1171 }
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1172 #endif
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1173
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1174
22
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1175 void tiff_warning_handler(const char *module, const char *fmt, va_list ap)
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1176 {
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1177 (void) module;
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1178 (void) fmt;
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1179 (void) ap;
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1180 // ignore errors and warnings
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1181 }
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1182
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1183
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1184 Image *read_tiff(BPGMetaData **pmd, const char *filename,
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1185 BPGColorSpaceEnum color_space, int out_bit_depth,
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1186 int limited_range, int premultiplied_alpha)
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1187 {
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1188 TIFF *tif;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1189 Image *img = NULL;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1190 BPGImageFormatEnum img_format;
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1191 uint8_t *buf = NULL, *tmp_buf = NULL;
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1192 uint32_t img_width, img_height, tmp_len;
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1193 uint16_t img_depth, img_spp, img_pconfig, img_pmetric,
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1194 *buf2 = NULL, *img_colormap_r, *img_colormap_g, *img_colormap_b;
14
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1195 int img_alpha, err_spp, offs, x, y, idx;
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1196 size_t img_linesize;
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1197 ColorConvertState cvt;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1198 RGBConvertFunc *convert_func;
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1199 #ifdef HAVE_LIBEXIF
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1200 toff_t tmp_offs;
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1201 ExifMem *exif_mem = NULL;
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1202 ExifData *exif = NULL;
30
6fec76de9a8d Fix some EXIF conversion memory allocation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1203 int added = 0;
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1204 #endif
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1205
22
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1206 // Set warning handler to silence dubious warnings about unknown fields
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1207 TIFFSetWarningHandler(tiff_warning_handler);
69089d946a14 Set a dummy libtiff warning handler to silence useless warnings about
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1208
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1209 // Open and read TIFF header etc.
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1210 if ((tif = TIFFOpen(filename, "rb")) == NULL)
7
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1211 {
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1212 fprintf(stderr, "Could not open TIFF image '%s'\n", filename);
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1213 return NULL;
7
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1214 }
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1215
7
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1216 if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img_width) ||
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1217 !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img_height) ||
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1218 !TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &img_spp) ||
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1219 !TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &img_depth))
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1220 {
25
a6e6f87414ea Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1221 fprintf(stderr, "TIFF file '%s' lacks basic fields!\n", filename);
7
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1222 goto err;
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1223 }
6dc09314d44b Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1224
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1225 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &img_pconfig);
10
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1226 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img_pmetric);
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1227
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1228 // Check basics
14
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1229 err_spp = 0;
10
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1230 switch (img_pmetric)
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1231 {
12
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1232 case PHOTOMETRIC_MINISBLACK:
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1233 case PHOTOMETRIC_MINISWHITE:
12
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1234 img_format = BPG_FORMAT_GRAY;
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1235 color_space = BPG_CS_YCbCr;
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1236 if (img_spp != 1)
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1237 err_spp = 1;
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1238 break;
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1239
10
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1240 case PHOTOMETRIC_RGB:
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1241 img_format = BPG_FORMAT_444;
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1242 if (img_spp < 3)
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1243 err_spp = 1;
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1244 break;
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1245
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1246 case PHOTOMETRIC_PALETTE:
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1247 img_format = BPG_FORMAT_444;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1248 if (img_spp != 1)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1249 err_spp = 1;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1250
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1251 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &img_colormap_r, &img_colormap_g, &img_colormap_b))
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1252 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1253 fprintf(stderr, "TIFF with palette is missing colormap entry!\n");
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1254 goto err;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1255 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1256 break;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1257
10
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1258 default:
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1259 fprintf(stderr, "TIFF file has unsupported photometric interpretation %d!\n", img_pmetric);
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1260 goto err;
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1261 }
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1262
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1263 if (err_spp)
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1264 {
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1265 fprintf(stderr, "TIFF file has unsupported samples per pixel %d for format %d.\n", img_spp, img_pmetric);
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1266 goto err;
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1267 }
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1268
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1269 if (img_depth != 8 && img_depth != 16)
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1270 {
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1271 fprintf(stderr, "TIFF file has unsupported depth %d (not 8 or 16)!\n", img_depth);
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1272 goto err;
10
d04d9e3a77d0 Add checking TIFF photometric interpretation tag data.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1273 }
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1274
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1275 img_linesize = TIFFScanlineSize(tif);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1276
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1277 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1278 int img_bpp = sizeof(uint32_t) * img_depth / 8;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1279 printf(
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1280 "\nTIFF '%s'\n"
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1281 " %d x %d, spp=%d, bpp=%d, depth=%d, pmet=%d\n"
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1282 " pconfig=%d, line_size=%d, cbps=%d\n",
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1283 filename,
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1284 img_width, img_height, img_spp, img_bpp, img_depth, img_pmetric,
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1285 img_pconfig, img_linesize, img_width * img_bpp);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1286 #endif
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1287
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1288 if (img_spp > 3)
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1289 {
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1290 uint16_t img_esmp, *img_esmp_types;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1291
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1292 TIFFGetField(tif, TIFFTAG_EXTRASAMPLES, &img_esmp, &img_esmp_types);
6
690bf78c1ce9 Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
1293 img_alpha = img_esmp == 1 && img_esmp_types[0] == EXTRASAMPLE_ASSOCALPHA;
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1294 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1295 printf(" esmp=%d: ", img_esmp);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1296 for (int n = 0; n < img_esmp; n++)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1297 printf("#%d=%d%s", n + 1, img_esmp_types[n], (n < img_esmp - 1) ? ", " : "");
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1298 printf("\n");
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1299 #endif
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1300 }
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1301 else
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1302 img_alpha = 0;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1303
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1304 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1305 printf(" alpha: %s\n\n", img_alpha ? "yes" : "no");
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1306 #endif
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1307
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1308 // Allocate temporary image space
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1309 buf = (uint8_t *) _TIFFmalloc(img_linesize * img_spp);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1310 if (buf == NULL)
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1311 goto err;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1312
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1313 if (img_pmetric == PHOTOMETRIC_PALETTE)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1314 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1315 // For indexed palette images we need a secondary RGB conversion buffer
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1316 buf2 = (uint16_t *) malloc(img_width * 3 * sizeof(uint16_t));
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1317 if (buf2 == NULL)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1318 goto err;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1319
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1320 // Force 16bit depth and samples per pixel 3 as the
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1321 // TIFF colormap/palette has 16bit R, G, B entries.
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1322 img_spp = 3;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1323 img_depth = 16;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1324 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1325
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1326 // Allocate target image
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1327 img = image_alloc(img_width, img_height, img_format, img_alpha, color_space, out_bit_depth);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1328 if (img == NULL)
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1329 goto err;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1330
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1331 img->limited_range = limited_range;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1332 img->premultiplied_alpha = premultiplied_alpha;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1333
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1334 convert_init(&cvt, img_depth, out_bit_depth, color_space, limited_range);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1335
14
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1336 idx = img_depth == 16;
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1337 convert_func = rgb_to_cs[idx][color_space];
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1338
14
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1339 for (y = 0; y < img->h; y++)
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1340 {
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1341 // Read TIFF image to raster
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1342 /*
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1343 for (int n = 0; n < img_spp; n++)
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1344 if (!TIFFReadScanline(tif, buf + (img_linesize * n), y, n))
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1345 goto err;
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1346 */
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1347 if (!TIFFReadScanline(tif, buf, y, 0))
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1348 goto err;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1349
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1350 // Convert the scanline data
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1351 switch (img_pmetric)
6
690bf78c1ce9 Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
1352 {
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1353 case PHOTOMETRIC_PALETTE:
14
b0b1d299aa5d Don't use C99 style declarations here for no reason.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1354 for (x = 0, offs = 0; x < img->w; x++)
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1355 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1356 buf2[offs++] = img_colormap_r[buf[x]];
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1357 buf2[offs++] = img_colormap_g[buf[x]];
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1358 buf2[offs++] = img_colormap_b[buf[x]];
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1359 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1360 convert_func(&cvt,
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1361 (PIXEL *)(img->data[0] + y * img->linesize[0]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1362 (PIXEL *)(img->data[1] + y * img->linesize[1]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1363 (PIXEL *)(img->data[2] + y * img->linesize[2]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1364 buf2, img->w, img_spp);
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1365 break;
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1366
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1367 case PHOTOMETRIC_RGB:
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1368 convert_func(&cvt,
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1369 (PIXEL *)(img->data[0] + y * img->linesize[0]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1370 (PIXEL *)(img->data[1] + y * img->linesize[1]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1371 (PIXEL *)(img->data[2] + y * img->linesize[2]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1372 buf, img->w, img_spp);
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1373
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1374 if (img_alpha)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1375 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1376 if (idx)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1377 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1378 gray16_to_gray(&cvt,
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1379 (PIXEL *)(img->data[3] + y * img->linesize[3]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1380 ((uint16_t *) buf) + 3, img->w, 4);
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1381 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1382 else
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1383 {
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1384 gray8_to_gray(&cvt,
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1385 (PIXEL *)(img->data[3] + y * img->linesize[3]),
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1386 ((uint8_t *) buf) + 3, img->w, 4);
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1387 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1388 }
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1389 break;
12
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1390
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1391 case PHOTOMETRIC_MINISWHITE:
12
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1392 case PHOTOMETRIC_MINISBLACK:
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1393 if (img_depth == 16)
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1394 {
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1395 luma16_to_gray(&cvt,
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1396 (PIXEL *)(img->data[0] + y * img->linesize[0]),
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1397 (uint16_t *) buf, img->w, img_spp);
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1398
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1399 if (img_alpha)
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1400 {
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1401 gray16_to_gray(&cvt,
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1402 (PIXEL *)(img->data[1] + y * img->linesize[1]),
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1403 ((uint16_t *) buf) + 1, img->w, 2);
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1404 }
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1405 }
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1406 else
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1407 {
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1408 luma8_to_gray(&cvt,
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1409 (PIXEL *)(img->data[0] + y * img->linesize[0]),
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1410 (uint8_t *) buf, img->w, img_spp);
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1411
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1412 if (img_alpha)
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1413 {
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1414 gray8_to_gray(&cvt,
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1415 (PIXEL *)(img->data[1] + y * img->linesize[1]),
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1416 ((uint8_t *) buf) + 1, img->w, 2);
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1417 }
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1418 }
9b00d4206f99 Implement support for one type of grayscale TIFF.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
1419 break;
6
690bf78c1ce9 Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
1420 }
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1421 }
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1422
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1423 // Get meta data
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1424 if (TIFFGetField(tif, TIFFTAG_ICCPROFILE, &tmp_len, &tmp_buf))
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
1425 bpg_add_md_contents(pmd, BPG_EXTENSION_TAG_ICCP, tmp_len, tmp_buf);
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1426
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1427 if (TIFFGetField(tif, TIFFTAG_XMLPACKET, &tmp_len, &tmp_buf))
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
1428 bpg_add_md_contents(pmd, BPG_EXTENSION_TAG_XMP, tmp_len, tmp_buf);
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1429
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1430 #ifdef HAVE_LIBEXIF
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1431 // Due to insanity of libtiff's "API", we have to go roundabout way to
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1432 // reconstruct the EXIF data through libexif and fetching TIFF tags ..
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1433 // even though the data already supposedly exists in desired format .. :(
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1434 if ((exif_mem = exif_mem_new_default()) == NULL)
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1435 goto err;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1436
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1437 if ((exif = exif_data_new_mem(exif_mem)) == NULL)
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1438 goto err;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1439
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1440 exif_data_set_option(exif, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1441 exif_data_set_data_type(exif, EXIF_DATA_TYPE_COMPRESSED);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1442 exif_data_set_byte_order(exif, EXIF_BYTE_ORDER_INTEL);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1443
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1444 tiff_tags_convert(tif, tiff_tag_convert_table, exif_mem, exif, &added);
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1445 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1446 tiff_tags_dump(tif, tiff_tag_convert_table);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1447 #endif
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1448
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1449 if (TIFFGetField(tif, TIFFTAG_EXIFIFD, &tmp_offs) &&
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1450 TIFFReadEXIFDirectory(tif, tmp_offs))
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1451 {
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1452 tiff_tags_convert(tif, exif_tag_convert_table, exif_mem, exif, &added);
34
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1453 #ifdef TIFF_DEBUG
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1454 tiff_tags_dump(tif, exif_tag_convert_table);
5d51fff843eb A "commit dump" of random changes I've made, as I probably won't be touching this code anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1455 #endif
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1456 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1457
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1458 if (added)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1459 {
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1460 uint8_t *ex_buf = NULL;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1461 unsigned int ex_len = 0;
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1462
29
1a7b661841c3 Add exif_fix_data() before creating the EXIF data blob from TIFF tags to
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
1463 exif_data_fix(exif);
24
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1464 exif_data_save_data(exif, &ex_buf, &ex_len);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1465
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1466 // Strip off "Exif\x00\x00" from the data
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1467 if (ex_len > 6)
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1468 bpg_add_md_contents(pmd, BPG_EXTENSION_TAG_EXIF, ex_len - 6, ex_buf + 6);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1469
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1470 exif_mem_free(exif_mem, ex_buf);
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1471 }
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1472 #endif
377dc78a052d Add EXIF metadata conversion from TIFF files to BPG via libexif.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1473
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1474 err:
27
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1475 #ifdef HAVE_LIBEXIF
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1476 if (exif != NULL)
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1477 exif_data_free(exif);
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1478 #endif
4aa0611ad85a Some adjustments to EXIF support in TIFF input.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1479
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1480 if (buf != NULL)
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1481 _TIFFfree(buf);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1482
11
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1483 if (buf2 != NULL)
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1484 free(buf2);
e70eb0e6acd5 Add support for indexed palette TIFFs, at least some of them.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
1485
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1486 TIFFClose(tif);
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1487 return img;
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1488 }
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1489
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1490 Image *read_png(BPGMetaData **pmd,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1491 FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1492 int limited_range, int premultiplied_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1493 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1494 png_structp png_ptr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1495 png_infop info_ptr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1496 int bit_depth, color_type;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1497 Image *img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1498 uint8_t **rows;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1499 int y, has_alpha, linesize, bpp;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1500 BPGImageFormatEnum format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1501 ColorConvertState cvt_s, *cvt = &cvt_s;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1502
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1503 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1504 NULL, NULL, NULL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1505 if (png_ptr == NULL) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1506 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1507 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1508
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1509 info_ptr = png_create_info_struct(png_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1510 if (info_ptr == NULL) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1511 png_destroy_read_struct(&png_ptr, NULL, NULL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1512 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1513 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1514
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1515 if (setjmp(png_jmpbuf(png_ptr))) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1516 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1517 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1518 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1519
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1520 png_init_io(png_ptr, f);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1521
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1522 png_read_info(png_ptr, info_ptr);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1523
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1524 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1525 color_type = png_get_color_type(png_ptr, info_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1526
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1527 switch (color_type) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1528 case PNG_COLOR_TYPE_PALETTE:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1529 png_set_palette_to_rgb(png_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1530 bit_depth = 8;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1531 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1532 case PNG_COLOR_TYPE_GRAY:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1533 case PNG_COLOR_TYPE_GRAY_ALPHA:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1534 if (bit_depth < 8) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1535 png_set_expand_gray_1_2_4_to_8(png_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1536 bit_depth = 8;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1537 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1538 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1539 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1540 assert(bit_depth == 8 || bit_depth == 16);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1541
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1542 #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1543 if (bit_depth == 16) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1544 png_set_swap(png_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1545 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1546 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1547
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1548 if (color_type == PNG_COLOR_TYPE_GRAY ||
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1549 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1550 format = BPG_FORMAT_GRAY;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1551 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1552 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1553 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1554 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1555
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1556 has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1557 color_type == PNG_COLOR_TYPE_RGB_ALPHA);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1558
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1559 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1560 png_set_tRNS_to_alpha(png_ptr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1561 has_alpha = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1562 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1563
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1564 if (premultiplied_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1565 png_set_alpha_mode(png_ptr, PNG_ALPHA_ASSOCIATED, PNG_GAMMA_LINEAR);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1566 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1567
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1568 img = image_alloc(png_get_image_width(png_ptr, info_ptr),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1569 png_get_image_height(png_ptr, info_ptr),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1570 format, has_alpha, color_space,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1571 out_bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1572 img->limited_range = limited_range;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1573 img->premultiplied_alpha = premultiplied_alpha;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1574
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1575 rows = malloc(sizeof(rows[0]) * img->h);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1576 if (format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1577 bpp = (1 + has_alpha) * (bit_depth / 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1578 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1579 bpp = (3 + has_alpha) * (bit_depth / 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1580 linesize = bpp * img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1581 for (y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1582 rows[y] = malloc(linesize);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1583 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1584
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1585 png_read_image(png_ptr, rows);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1586
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1587 convert_init(cvt, bit_depth, out_bit_depth, color_space, limited_range);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1588
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1589 if (format != BPG_FORMAT_GRAY) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1590 int idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1591 RGBConvertFunc *convert_func;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1592
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1593 idx = (bit_depth == 16);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1594 convert_func = rgb_to_cs[idx][color_space];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1595
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1596 for (y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1597 convert_func(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1598 (PIXEL *)(img->data[1] + y * img->linesize[1]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1599 (PIXEL *)(img->data[2] + y * img->linesize[2]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1600 rows[y], img->w, 3 + has_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1601 if (has_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1602 if (idx) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1603 gray16_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1604 (uint16_t *)rows[y] + 3, img->w, 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1605 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1606 gray8_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1607 rows[y] + 3, img->w, 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1608 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1609 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1610 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1611 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1612 if (bit_depth == 16) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1613 for (y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1614 luma16_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1615 (uint16_t *)rows[y], img->w, 1 + has_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1616 if (has_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1617 gray16_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1618 (uint16_t *)rows[y] + 1, img->w, 2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1619 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1620 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1621 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1622 for (y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1623 luma8_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1624 rows[y], img->w, 1 + has_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1625 if (has_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1626 gray8_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1627 rows[y] + 1, img->w, 2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1628 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1629 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1630 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1631 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1632
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1633 for (y = 0; y < img->h; y++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1634 free(rows[y]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1635 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1636 free(rows);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1637
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1638 png_read_end(png_ptr, info_ptr);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1639
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1640 /* get the ICC profile if present */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1641 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1642 png_charp name;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1643 int comp_type;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1644 png_bytep iccp_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1645 png_uint_32 iccp_buf_len;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1646
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1647 if (png_get_iCCP(png_ptr, info_ptr,
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1648 &name, &comp_type, &iccp_buf, &iccp_buf_len) == PNG_INFO_iCCP)
17
91f27e9fdb60 Rename insert_md() and add_md_contents() to bpg_insert_md() and
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
1649 bpg_add_md_contents(pmd, BPG_EXTENSION_TAG_ICCP, iccp_buf_len, iccp_buf);
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1650 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1651
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1652 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1653
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1654 return img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1655 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1656
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1657 static BPGMetaData *jpeg_get_metadata(jpeg_saved_marker_ptr first_marker)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1658 {
26
a6e11adda0be The format of JPEG EXIF data converted to BPG had an extraneous zero byte at
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1659 static const char app1_exif[6] = "Exif\x00\x00";
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1660 static const char app1_xmp[] = "http://ns.adobe.com/xap/1.0/";
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1661 static const char app2_iccp[] = "ICC_PROFILE";
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1662 jpeg_saved_marker_ptr marker;
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1663 BPGMetaData *md_list;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1664 int has_exif, has_xmp, l, iccp_chunk_count, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1665 jpeg_saved_marker_ptr iccp_chunks[256];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1666
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1667 iccp_chunk_count = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1668 has_exif = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1669 has_xmp = 0;
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1670 md_list = NULL;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1671 for (marker = first_marker; marker != NULL; marker = marker->next) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1672 #if 0
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1673 printf("marker=APP%d len=%d\n",
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1674 marker->marker - JPEG_APP0, marker->data_length);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1675 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1676 if (!has_exif && marker->marker == JPEG_APP0 + 1 &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1677 marker->data_length > sizeof(app1_exif) &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1678 !memcmp(marker->data, app1_exif, sizeof(app1_exif))) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1679 l = sizeof(app1_exif);
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1680 bpg_add_md_contents(&md_list, BPG_EXTENSION_TAG_EXIF, marker->data_length - l, marker->data + l);
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1681 has_exif = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1682 } else if (!has_xmp && marker->marker == JPEG_APP0 + 1 &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1683 marker->data_length > sizeof(app1_xmp) &&
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1684 !memcmp(marker->data, app1_xmp, sizeof(app1_xmp)) &&
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1685 !has_xmp) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1686 l = sizeof(app1_xmp);
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1687 bpg_add_md_contents(&md_list, BPG_EXTENSION_TAG_XMP, marker->data_length - l, marker->data + l);
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1688 has_xmp = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1689 } else if (marker->marker == JPEG_APP0 + 2 &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1690 marker->data_length > (sizeof(app2_iccp) + 2) &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1691 !memcmp(marker->data, app2_iccp, sizeof(app2_iccp))) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1692 int chunk_count, chunk_index;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1693 l = sizeof(app2_iccp);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1694 chunk_index = marker->data[l];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1695 chunk_count = marker->data[l];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1696 if (chunk_index == 0 || chunk_count == 0)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1697 continue;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1698 if (iccp_chunk_count == 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1699 iccp_chunk_count = chunk_count;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1700 for(i = 0; i < chunk_count; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1701 iccp_chunks[i] = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1702 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1703 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1704 if (chunk_count != iccp_chunk_count)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1705 continue;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1706 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1707 if (chunk_index > iccp_chunk_count)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1708 continue;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1709 iccp_chunks[chunk_index - 1] = marker;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1710 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1711 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1712
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1713 if (iccp_chunk_count != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1714 int len, hlen, idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1715 /* check that no chunk are missing */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1716 len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1717 hlen = sizeof(app2_iccp) + 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1718 for(i = 0; i < iccp_chunk_count; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1719 if (!iccp_chunks[i])
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1720 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1721 len += iccp_chunks[i]->data_length - hlen;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1722 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1723 if (i == iccp_chunk_count) {
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1724 BPGMetaData *md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP);
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1725 md->buf_len = len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1726 md->buf = malloc(md->buf_len);
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1727 bpg_insert_md(&md_list, md);
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1728 for(i = 0, idx = 0; i < iccp_chunk_count; i++) {
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1729 l = iccp_chunks[i]->data_length - hlen;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1730 memcpy(md->buf + idx, iccp_chunks[i]->data + hlen, l);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1731 idx += l;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1732 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1733 assert(idx == len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1734 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1735 }
18
403c67bcb76a Use bpg_insert_md() and bpg_add_md_contents() in jpeg_get_metadata() also.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
1736 return md_list;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1737 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1738
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1739 Image *read_jpeg(BPGMetaData **pmd, FILE *f,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1740 int out_bit_depth)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1741 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1742 struct jpeg_decompress_struct cinfo;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1743 struct jpeg_error_mgr jerr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1744 int w, h, w1, i, y_h, c_h, y, v_shift, c_w, y1, idx, c_idx, h_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1745 int h1, plane_idx[4], has_alpha, has_w_plane;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1746 Image *img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1747 BPGImageFormatEnum format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1748 BPGColorSpaceEnum color_space;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1749 ColorConvertState cvt_s, *cvt = &cvt_s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1750 uint32_t comp_hv;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1751
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1752 cinfo.err = jpeg_std_error(&jerr);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1753 jpeg_create_decompress(&cinfo);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1754
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1755 jpeg_save_markers(&cinfo, JPEG_APP0 + 1, 65535);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1756 jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 65535);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1757
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1758 jpeg_stdio_src(&cinfo, f);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1759
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1760 jpeg_read_header(&cinfo, TRUE);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1761
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1762 cinfo.raw_data_out = TRUE;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1763 cinfo.do_fancy_upsampling = TRUE;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1764
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1765 w = cinfo.image_width;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1766 h = cinfo.image_height;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1767
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1768 has_w_plane = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1769 comp_hv = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1770 if (cinfo.num_components < 1 || cinfo.num_components > 4)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1771 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1772 for(i = 0; i < cinfo.num_components; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1773 comp_hv |= cinfo.comp_info[i].h_samp_factor << (i * 8 + 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1774 comp_hv |= cinfo.comp_info[i].v_samp_factor << (i * 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1775 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1776 switch(cinfo.jpeg_color_space) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1777 case JCS_GRAYSCALE:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1778 if (cinfo.num_components != 1 || comp_hv != 0x11)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1779 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1780 format = BPG_FORMAT_GRAY;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1781 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1782 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1783 case JCS_YCbCr:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1784 if (cinfo.num_components != 3)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1785 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1786 switch(comp_hv) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1787 case 0x111111:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1788 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1789 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1790 case 0x111121:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1791 format = BPG_FORMAT_422;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1792 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1793 case 0x111122:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1794 format = BPG_FORMAT_420;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1795 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1796 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1797 cinfo.raw_data_out = FALSE;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1798 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1799 cinfo.out_color_space = JCS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1800 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1801 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1802 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1803 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1804 case JCS_RGB:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1805 if (cinfo.num_components != 3)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1806 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1807 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1808 color_space = BPG_CS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1809 cinfo.raw_data_out = FALSE;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1810 cinfo.out_color_space = JCS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1811 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1812 case JCS_YCCK:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1813 if (cinfo.num_components != 4)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1814 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1815 switch(comp_hv) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1816 case 0x11111111:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1817 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1818 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1819 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1820 case 0x22111121:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1821 format = BPG_FORMAT_422;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1822 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1823 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1824 case 0x22111122:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1825 format = BPG_FORMAT_420;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1826 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1827 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1828 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1829 cinfo.raw_data_out = FALSE;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1830 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1831 cinfo.out_color_space = JCS_CMYK;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1832 color_space = BPG_CS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1833 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1834 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1835 has_w_plane = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1836 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1837 case JCS_CMYK:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1838 if (cinfo.num_components != 4)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1839 goto unsupported;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1840 format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1841 color_space = BPG_CS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1842 has_w_plane = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1843 cinfo.raw_data_out = FALSE;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1844 cinfo.out_color_space = JCS_CMYK;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1845 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1846 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1847 unsupported:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1848 fprintf(stderr, "Unsupported JPEG parameters (cs=%d n_comp=%d comp_hv=%x)\n",
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1849 cinfo.jpeg_color_space, cinfo.num_components, comp_hv);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1850 img = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1851 goto the_end;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1852 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1853
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1854 v_shift = (format == BPG_FORMAT_420);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1855 h_shift = (format == BPG_FORMAT_422 || format == BPG_FORMAT_420);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1856 has_alpha = (cinfo.num_components == 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1857 img = image_alloc(w, h, format, has_alpha, color_space, out_bit_depth);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1858 img->has_w_plane = has_w_plane;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1859
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1860 convert_init(cvt, 8, out_bit_depth, color_space, 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1861
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1862 jpeg_start_decompress(&cinfo);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1863
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1864 if (color_space == BPG_CS_RGB) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1865 plane_idx[0] = 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1866 plane_idx[1] = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1867 plane_idx[2] = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1868 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1869 plane_idx[0] = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1870 plane_idx[1] = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1871 plane_idx[2] = 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1872 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1873 plane_idx[3] = 3;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1874
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1875 if (cinfo.raw_data_out) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1876 JSAMPROW rows[4][16];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1877 JSAMPROW *plane_pointer[4];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1878
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1879 y_h = 8 * cinfo.max_v_samp_factor;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1880 if (cinfo.num_components == 1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1881 c_h = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1882 c_w = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1883 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1884 c_h = 8;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1885 if (h_shift)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1886 c_w = (w + 1) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1887 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1888 c_w = w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1889 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1890 w1 = (w + 15) & ~15;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1891 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1892 if (c_idx == 1 || c_idx == 2) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1893 h1 = c_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1894 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1895 h1 = y_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1896 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1897 for(i = 0; i < h1; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1898 rows[c_idx][i] = malloc(w1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1899 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1900 plane_pointer[c_idx] = rows[c_idx];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1901 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1902
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1903 while (cinfo.output_scanline < cinfo.output_height) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1904 y = cinfo.output_scanline;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1905 jpeg_read_raw_data(&cinfo, plane_pointer, y_h);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1906
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1907 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1908 if (c_idx == 1 || c_idx == 2) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1909 h1 = c_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1910 w1 = c_w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1911 y1 = (y >> v_shift);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1912 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1913 h1 = y_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1914 w1 = img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1915 y1 = y;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1916 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1917 idx = plane_idx[c_idx];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1918 for(i = 0; i < h1; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1919 PIXEL *ptr;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1920 ptr = (PIXEL *)(img->data[idx] +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1921 img->linesize[idx] * (y1 + i));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1922 gray8_to_gray(cvt, ptr, rows[c_idx][i], w1, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1923 if (color_space == BPG_CS_YCbCr && has_w_plane) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1924 /* negate color */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1925 if (c_idx == 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1926 gray_one_minus(cvt, ptr, w1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1927 } else if (c_idx <= 2) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1928 gray_neg_c(cvt, ptr, w1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1929 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1930 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1931 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1932 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1933 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1934
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1935 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1936 if (c_idx == 1 || c_idx == 2) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1937 h1 = c_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1938 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1939 h1 = y_h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1940 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1941 for(i = 0; i < h1; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1942 free(rows[c_idx][i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1943 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1944 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1945 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1946 JSAMPROW rows[1];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1947 uint8_t *buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1948 int c_count;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1949
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1950 c_count = 3 + has_w_plane;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1951 buf = malloc(c_count * w);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1952 rows[0] = buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1953 while (cinfo.output_scanline < cinfo.output_height) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1954 y = cinfo.output_scanline;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1955 jpeg_read_scanlines(&cinfo, rows, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1956
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1957 for(c_idx = 0; c_idx < c_count; c_idx++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1958 idx = plane_idx[c_idx];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1959 gray8_to_gray(cvt, (PIXEL *)(img->data[idx] +
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1960 img->linesize[idx] * y),
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1961 buf + c_idx, w, c_count);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1962 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1963 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1964 free(buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1965 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1966
9
bfd933ef38b4 Implement some metadata support in TIFF and clean up meta data handling otherwise.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1967 *pmd = jpeg_get_metadata(cinfo.marker_list);
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1968
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1969 the_end:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1970 jpeg_finish_decompress(&cinfo);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1971
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1972 jpeg_destroy_decompress(&cinfo);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1973 return img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1974 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1975
25
a6e6f87414ea Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1976
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1977 static const char PROBE_TIFF_MAGIC_LE[] = { 'I', 'I', 42, 0 };
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1978 static const char PROBE_TIFF_MAGIC_BE[] = { 'M', 'M', 0, 42 };
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1979 #define PROBE_BUF_SIZE 8
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
1980
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1981 Image *load_image(BPGMetaData **pmd, const char *infilename,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1982 BPGColorSpaceEnum color_space, int bit_depth,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1983 int limited_range, int premultiplied_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1984 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1985 FILE *f;
3
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1986 Image *img = NULL;
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1987 uint8_t buf[PROBE_BUF_SIZE];
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1988
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1989 *pmd = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1990
3
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1991 if ((f = fopen(infilename, "rb")) == NULL)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1992 return NULL;
3
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1993
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1994 if (fread(buf, 1, PROBE_BUF_SIZE, f) != PROBE_BUF_SIZE)
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1995 goto err;
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1996
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1997 fseek(f, 0, SEEK_SET);
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1998
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1999 if (png_sig_cmp(buf, 0, PROBE_BUF_SIZE) == 0)
8
5a1eec3c43cc Get rid of indirect metadata variable sillyness in load_image().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
2000 img = read_png(pmd, f, color_space, bit_depth, limited_range, premultiplied_alpha);
3
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
2001 else
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
2002 if (memcmp(buf, PROBE_TIFF_MAGIC_LE, sizeof(PROBE_TIFF_MAGIC_LE)) == 0 ||
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
2003 memcmp(buf, PROBE_TIFF_MAGIC_BE, sizeof(PROBE_TIFF_MAGIC_BE)) == 0)
8
5a1eec3c43cc Get rid of indirect metadata variable sillyness in load_image().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
2004 img = read_tiff(pmd, infilename, color_space, bit_depth, limited_range, premultiplied_alpha);
4
bbd61622856a Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
2005 else
8
5a1eec3c43cc Get rid of indirect metadata variable sillyness in load_image().
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
2006 img = read_jpeg(pmd, f, bit_depth);
3
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
2007
ffcd1967fb5c Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
2008 err:
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2009 fclose(f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2010 return img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2011 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2012
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2013 void save_yuv1(Image *img, FILE *f)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2014 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2015 int c_w, c_h, i, c_count, y;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2016
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2017 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2018 c_count = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2019 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2020 c_count = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2021 for(i = 0; i < c_count; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2022 get_plane_res(img, &c_w, &c_h, i);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2023 for(y = 0; y < c_h; y++) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2024 fwrite(img->data[i] + y * img->linesize[i],
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2025 1, c_w << img->pixel_shift, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2026 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2027 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2028 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2029
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2030 void save_yuv(Image *img, const char *filename)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2031 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2032 FILE *f;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2033
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2034 f = fopen(filename, "wb");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2035 if (!f) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2036 fprintf(stderr, "Could not open %s\n", filename);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2037 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2038 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2039 save_yuv1(img, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2040 fclose(f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2041 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2042
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2043 /* return the position of the end of the NAL or -1 if error */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2044 static int find_nal_end(const uint8_t *buf, int buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2045 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2046 int idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2047
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2048 idx = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2049 if (buf_len >= 4 &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2050 buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2051 idx = 4;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2052 } else if (buf_len >= 3 &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2053 buf[0] == 0 && buf[1] == 0 && buf[2] == 1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2054 idx = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2055 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2056 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2057 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2058 /* NAL header */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2059 if (idx + 2 > buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2060 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2061 /* find the last byte */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2062 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2063 if (idx + 2 >= buf_len) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2064 idx = buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2065 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2066 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2067 if (buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2068 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2069 if (idx + 3 < buf_len &&
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2070 buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0 && buf[idx + 3] == 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2071 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2072 idx++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2073 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2074 return idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2075 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2076
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2077 /* return the position of the end of the NAL or -1 if error */
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2078 static int extract_nal(uint8_t **pnal_buf, int *pnal_len,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2079 const uint8_t *buf, int buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2080 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2081 int idx, start, end, len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2082 uint8_t *nal_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2083 int nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2084
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2085 end = find_nal_end(buf, buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2086 if (end < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2087 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2088 if (buf[2] == 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2089 start = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2090 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2091 start = 4;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2092 len = end - start;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2093
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2094 nal_buf = malloc(len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2095 nal_len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2096 idx = start;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2097 while (idx < end) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2098 if (idx + 2 < end && buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 3) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2099 nal_buf[nal_len++] = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2100 nal_buf[nal_len++] = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2101 idx += 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2102 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2103 nal_buf[nal_len++] = buf[idx++];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2104 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2105 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2106 while (idx < end) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2107 nal_buf[nal_len++] = buf[idx++];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2108 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2109 *pnal_buf = nal_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2110 *pnal_len = nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2111 return idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2112 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2113
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2114 /* big endian variable length 7 bit encoding */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2115 static void put_ue(uint8_t **pp, uint32_t v)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2116 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2117 uint8_t *p = *pp;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2118 int i, j;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2119
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
2120 for (i = 1; i < 5; i++) {
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2121 if (v < (1U << (7 * i)))
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2122 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2123 }
33
33594243ce31 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
2124 for (j = i - 1; j >= 1; j--)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2125 *p++ = ((v >> (7 * j)) & 0x7f) | 0x80;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2126 *p++ = v & 0x7f;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2127 *pp = p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2128 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2129
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2130 typedef struct {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2131 const uint8_t *buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2132 int idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2133 int buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2134 } GetBitState;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2135
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2136 static void init_get_bits(GetBitState *s, const uint8_t *buf, int buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2137 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2138 s->buf = buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2139 s->buf_len = buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2140 s->idx = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2141 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2142
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2143 static void skip_bits(GetBitState *s, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2144 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2145 s->idx += n;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2146 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2147
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2148 /* 1 <= n <= 25. return '0' bits if past the end of the buffer. */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2149 static uint32_t get_bits(GetBitState *s, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2150 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2151 const uint8_t *buf = s->buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2152 int p, i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2153 uint32_t v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2154
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2155 p = s->idx >> 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2156 if ((p + 3) < s->buf_len) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2157 v = (buf[p] << 24) | (buf[p + 1] << 16) |
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2158 (buf[p + 2] << 8) | buf[p + 3];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2159 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2160 v = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2161 for(i = 0; i < 3; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2162 if ((p + i) < s->buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2163 v |= buf[p + i] << (24 - i * 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2164 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2165 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2166 v = (v >> (32 - (s->idx & 7) - n)) & ((1 << n) - 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2167 s->idx += n;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2168 return v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2169 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2170
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2171 /* 1 <= n <= 32 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2172 static uint32_t get_bits_long(GetBitState *s, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2173 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2174 uint32_t v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2175
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2176 if (n <= 25) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2177 v = get_bits(s, n);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2178 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2179 n -= 16;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2180 v = get_bits(s, 16) << n;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2181 v |= get_bits(s, n);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2182 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2183 return v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2184 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2185
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2186 /* at most 32 bits are supported */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2187 static uint32_t get_ue_golomb(GetBitState *s)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2188 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2189 int i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2190 i = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2191 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2192 if (get_bits(s, 1))
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2193 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2194 i++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2195 if (i == 32)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2196 return 0xffffffff;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2197 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2198 if (i == 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2199 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2200 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2201 return ((1 << i) | get_bits_long(s, i)) - 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2202 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2203
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2204 typedef struct {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2205 uint8_t *buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2206 int idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2207 } PutBitState;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2208
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2209 static void init_put_bits(PutBitState *s, uint8_t *buf)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2210 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2211 s->buf = buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2212 s->idx = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2213 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2214
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2215 static void put_bit(PutBitState *s, int bit)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2216 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2217 s->buf[s->idx >> 3] |= bit << (7 - (s->idx & 7));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2218 s->idx++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2219 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2220
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2221 static void put_bits(PutBitState *s, int n, uint32_t v)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2222 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2223 int i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2224
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2225 for(i = 0; i < n; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2226 put_bit(s, (v >> (n - 1 - i)) & 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2227 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2228 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2229
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2230 static void put_ue_golomb(PutBitState *s, uint32_t v)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2231 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2232 uint32_t a;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2233 int n;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2234
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2235 v++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2236 n = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2237 a = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2238 while (a != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2239 a >>= 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2240 n++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2241 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2242 if (n > 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2243 put_bits(s, n - 1, 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2244 put_bits(s, n, v);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2245 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2246
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2247 typedef struct {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2248 uint8_t *buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2249 int size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2250 int len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2251 } DynBuf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2252
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2253 static void dyn_buf_init(DynBuf *s)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2254 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2255 s->buf = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2256 s->size = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2257 s->len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2258 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2259
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2260 static int dyn_buf_resize(DynBuf *s, int size)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2261 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2262 int new_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2263 uint8_t *new_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2264
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2265 if (size <= s->size)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2266 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2267 new_size = (s->size * 3) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2268 if (new_size < size)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2269 new_size = size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2270 new_buf = realloc(s->buf, new_size);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2271 if (!new_buf)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2272 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2273 s->buf = new_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2274 s->size = new_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2275 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2276 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2277
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2278 /* suppress the VPS NAL and keep only the useful part of the SPS
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2279 header. The decoder can rebuild a valid HEVC stream if needed. */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2280 static int build_modified_sps(uint8_t **pout_buf, int *pout_buf_len,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2281 const uint8_t *buf, int buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2282 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2283 int nal_unit_type, nal_len, idx, i, ret, msps_buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2284 int out_buf_len, out_buf_len_max;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2285 uint8_t *nal_buf, *msps_buf, *out_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2286 GetBitState gb_s, *gb = &gb_s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2287 PutBitState pb_s, *pb = &pb_s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2288 uint8_t *p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2289
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2290 idx = extract_nal(&nal_buf, &nal_len, buf, buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2291 if (idx < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2292 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2293 if (nal_len < 2) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2294 free(nal_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2295 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2296 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2297 nal_unit_type = (nal_buf[0] >> 1) & 0x3f;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2298 free(nal_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2299 if (nal_unit_type != 32) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2300 fprintf(stderr, "expecting VPS nal (%d)\n", nal_unit_type);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2301 return -1; /* expect VPS nal */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2302 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2303
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2304 ret = extract_nal(&nal_buf, &nal_len, buf + idx, buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2305 if (ret < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2306 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2307 idx += ret;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2308 if (nal_len < 2)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2309 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2310 nal_unit_type = (nal_buf[0] >> 1) & 0x3f;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2311 if (nal_unit_type != 33) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2312 fprintf(stderr, "expecting SPS nal (%d)\n", nal_unit_type);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2313 return -1; /* expect SPS nal */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2314 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2315
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2316 /* skip the initial part of the SPS up to and including
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2317 log2_min_cb_size */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2318 {
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2319 int vps_id, max_sub_layers, sps_id;
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2320 int chroma_format_idc;
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2321 int log2_max_poc_lsb, log2_min_cb_size;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2322 int log2_diff_max_min_coding_block_size, log2_min_tb_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2323 int log2_diff_max_min_transform_block_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2324 int max_transform_hierarchy_depth_inter;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2325 int max_transform_hierarchy_depth_intra;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2326 int scaling_list_enable_flag, amp_enabled_flag, sao_enabled;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2327 int pcm_enabled_flag, nb_st_rps;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2328 int long_term_ref_pics_present_flag, sps_strong_intra_smoothing_enable_flag, vui_present;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2329 int sps_temporal_mvp_enabled_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2330 int pcm_sample_bit_depth_luma_minus1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2331 int pcm_sample_bit_depth_chroma_minus1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2332 int log2_min_pcm_luma_coding_block_size_minus3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2333 int log2_diff_max_min_pcm_luma_coding_block_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2334 int pcm_loop_filter_disabled_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2335 int sps_extension_flag, sps_range_extension_flag, sps_extension_7bits;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2336 int sps_range_extension_flags;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2337
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2338 init_get_bits(gb, nal_buf, nal_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2339 skip_bits(gb, 16); /* nal header */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2340 vps_id = get_bits(gb, 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2341 if (vps_id != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2342 fprintf(stderr, "VPS id 0 expected\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2343 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2344 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2345 max_sub_layers = get_bits(gb, 3);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2346 if (max_sub_layers != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2347 fprintf(stderr, "max_sub_layers == 0 expected\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2348 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2349 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2350 skip_bits(gb, 1); /* temporal_id_nesting_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2351 /* profile tier level */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2352 skip_bits(gb, 2); /* profile_space */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2353 skip_bits(gb, 1); /* tier_flag */
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2354 skip_bits(gb, 5); /* profile_idc */
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2355 for(i = 0; i < 32; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2356 skip_bits(gb, 1); /* profile_compatibility_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2357 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2358 skip_bits(gb, 1); /* progressive_source_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2359 skip_bits(gb, 1); /* interlaced_source_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2360 skip_bits(gb, 1); /* non_packed_constraint_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2361 skip_bits(gb, 1); /* frame_only_constraint_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2362 skip_bits(gb, 44); /* XXX_reserved_zero_44 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2363 skip_bits(gb, 8); /* level_idc */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2364
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2365 sps_id = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2366 if (sps_id != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2367 fprintf(stderr, "SPS id 0 expected (%d)\n", sps_id);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2368 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2369 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2370 chroma_format_idc = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2371 if (chroma_format_idc == 3) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2372 get_bits(gb, 1); /* separate_colour_plane_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2373 }
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2374 get_ue_golomb(gb); /* width = get_ue_golomb(gb); */
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2375 get_ue_golomb(gb); /* height = get_ue_golomb(gb); */
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2376 /* pic conformance_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2377 if (get_bits(gb, 1)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2378 get_ue_golomb(gb); /* left_offset */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2379 get_ue_golomb(gb); /* right_offset */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2380 get_ue_golomb(gb); /* top_offset */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2381 get_ue_golomb(gb); /* bottom_offset */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2382 }
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2383 get_ue_golomb(gb); /* bit_depth_luma = get_ue_golomb(gb) + 8; */
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2384 get_ue_golomb(gb); /* bit_depth_chroma = get_ue_golomb(gb) + 8; */
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2385 log2_max_poc_lsb = get_ue_golomb(gb) + 4;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2386 if (log2_max_poc_lsb != 8) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2387 fprintf(stderr, "log2_max_poc_lsb must be 8 (%d)\n", log2_max_poc_lsb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2388 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2389 }
32
38ae4a231581 Silence warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
2390 skip_bits(gb, 1); /* sublayer_ordering_info */
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2391 get_ue_golomb(gb); /* max_dec_pic_buffering */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2392 get_ue_golomb(gb); /* num_reorder_pics */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2393 get_ue_golomb(gb); /* max_latency_increase */
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2394
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2395 log2_min_cb_size = get_ue_golomb(gb) + 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2396 log2_diff_max_min_coding_block_size = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2397 log2_min_tb_size = get_ue_golomb(gb) + 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2398 log2_diff_max_min_transform_block_size = get_ue_golomb(gb);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2399
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2400 max_transform_hierarchy_depth_inter = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2401 max_transform_hierarchy_depth_intra = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2402 if (max_transform_hierarchy_depth_inter != max_transform_hierarchy_depth_intra) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2403 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);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2404 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2405 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2406
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2407 scaling_list_enable_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2408 if (scaling_list_enable_flag != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2409 fprintf(stderr, "scaling_list_enable_flag must be 0\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2410 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2411 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2412 amp_enabled_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2413 if (!amp_enabled_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2414 fprintf(stderr, "amp_enabled_flag must be set\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2415 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2416 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2417 sao_enabled = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2418 pcm_enabled_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2419 if (pcm_enabled_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2420 pcm_sample_bit_depth_luma_minus1 = get_bits(gb, 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2421 pcm_sample_bit_depth_chroma_minus1 = get_bits(gb, 4);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2422 log2_min_pcm_luma_coding_block_size_minus3 = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2423 log2_diff_max_min_pcm_luma_coding_block_size = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2424 pcm_loop_filter_disabled_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2425 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2426 nb_st_rps = get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2427 if (nb_st_rps != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2428 fprintf(stderr, "nb_st_rps must be 0 (%d)\n", nb_st_rps);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2429 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2430 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2431 long_term_ref_pics_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2432 if (long_term_ref_pics_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2433 fprintf(stderr, "nlong_term_ref_pics_present_flag must be 0 (%d)\n", nb_st_rps);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2434 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2435 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2436 sps_temporal_mvp_enabled_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2437 if (!sps_temporal_mvp_enabled_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2438 fprintf(stderr, "sps_temporal_mvp_enabled_flag must be set\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2439 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2440 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2441 sps_strong_intra_smoothing_enable_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2442 vui_present = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2443 if (vui_present) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2444 int sar_present, sar_idx, overscan_info_present_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2445 int video_signal_type_present_flag, chroma_loc_info_present_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2446 int default_display_window_flag, vui_timing_info_present_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2447 int vui_poc_proportional_to_timing_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2448 int vui_hrd_parameters_present_flag, bitstream_restriction_flag;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2449
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2450 sar_present = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2451 if (sar_present) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2452 sar_idx = get_bits(gb, 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2453 if (sar_idx == 255) {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2454 skip_bits(gb, 16); /* sar_num */
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2455 skip_bits(gb, 16); /* sar_den */
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2456 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2457 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2458
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2459 overscan_info_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2460 if (overscan_info_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2461 skip_bits(gb, 1); /* overscan_appropriate_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2462 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2463
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2464 video_signal_type_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2465 if (video_signal_type_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2466 fprintf(stderr, "video_signal_type_present_flag must be 0\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2467 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2468 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2469 chroma_loc_info_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2470 if (chroma_loc_info_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2471 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2472 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2473 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2474 skip_bits(gb, 1); /* neutra_chroma_indication_flag */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2475 skip_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2476 skip_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2477 default_display_window_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2478 if (default_display_window_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2479 fprintf(stderr, "default_display_window_flag must be 0\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2480 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2481 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2482 vui_timing_info_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2483 if (vui_timing_info_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2484 skip_bits(gb, 32);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2485 skip_bits(gb, 32);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2486 vui_poc_proportional_to_timing_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2487 if (vui_poc_proportional_to_timing_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2488 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2489 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2490 vui_hrd_parameters_present_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2491 if (vui_hrd_parameters_present_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2492 fprintf(stderr, "vui_hrd_parameters_present_flag must be 0\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2493 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2494 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2495 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2496 bitstream_restriction_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2497 if (bitstream_restriction_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2498 skip_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2499 skip_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2500 skip_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2501 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2502 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2503 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2504 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2505 get_ue_golomb(gb);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2506 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2507 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2508 sps_extension_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2509 sps_range_extension_flag = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2510 sps_range_extension_flags = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2511 if (sps_extension_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2512 sps_range_extension_flag = get_bits(gb, 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2513 sps_extension_7bits = get_bits(gb, 7);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2514 if (sps_extension_7bits != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2515 fprintf(stderr, "sps_extension_7bits must be 0\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2516 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2517 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2518 if (sps_range_extension_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2519 sps_range_extension_flags = get_bits(gb, 9);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2520 if (sps_range_extension_flags & ((1 << (8 - 3)) |
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2521 (1 << (8 - 4)) |
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2522 (1 << (8 - 6)) |
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2523 (1 << (8 - 8)))) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2524 fprintf(stderr, "unsupported range extensions (0x%x)\n",
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2525 sps_range_extension_flags);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2526 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2527 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2528 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2529 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2530
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2531 /* build the modified SPS */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2532 msps_buf = malloc(nal_len + 32);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2533 memset(msps_buf, 0, nal_len + 16);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2534
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2535 init_put_bits(pb, msps_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2536 put_ue_golomb(pb, log2_min_cb_size - 3);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2537 put_ue_golomb(pb, log2_diff_max_min_coding_block_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2538 put_ue_golomb(pb, log2_min_tb_size - 2);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2539 put_ue_golomb(pb, log2_diff_max_min_transform_block_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2540 put_ue_golomb(pb, max_transform_hierarchy_depth_intra);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2541 put_bits(pb, 1, sao_enabled);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2542 put_bits(pb, 1, pcm_enabled_flag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2543 if (pcm_enabled_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2544 put_bits(pb, 4, pcm_sample_bit_depth_luma_minus1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2545 put_bits(pb, 4, pcm_sample_bit_depth_chroma_minus1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2546 put_ue_golomb(pb, log2_min_pcm_luma_coding_block_size_minus3);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2547 put_ue_golomb(pb, log2_diff_max_min_pcm_luma_coding_block_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2548 put_bits(pb, 1, pcm_loop_filter_disabled_flag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2549 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2550 put_bits(pb, 1, sps_strong_intra_smoothing_enable_flag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2551 put_bits(pb, 1, sps_extension_flag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2552 if (sps_extension_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2553 put_bits(pb, 1, sps_range_extension_flag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2554 put_bits(pb, 7, 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2555 if (sps_range_extension_flag) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2556 put_bits(pb, 9, sps_range_extension_flags);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2557 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2558 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2559 msps_buf_len = (pb->idx + 7) >> 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2560
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2561 out_buf_len_max = 5 + msps_buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2562 out_buf = malloc(out_buf_len_max);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2563
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2564 // printf("msps_n_bits=%d\n", pb->idx);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2565 p = out_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2566 put_ue(&p, msps_buf_len); /* header length */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2567
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2568 memcpy(p, msps_buf, msps_buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2569 p += msps_buf_len;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2570
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2571 out_buf_len = p - out_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2572 free(msps_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2573 free(nal_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2574 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2575 *pout_buf = out_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2576 *pout_buf_len = out_buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2577 return idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2578 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2579
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2580 static int add_frame_duration_sei(DynBuf *out_buf, uint16_t frame_ticks)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2581 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2582 uint8_t nal_buf[128], *q;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2583 int nut, nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2584
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2585 q = nal_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2586 *q++ = 0x00;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2587 *q++ = 0x00;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2588 *q++ = 0x01;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2589 nut = 39; /* prefix SEI NUT */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2590 *q++ = (nut << 1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2591 *q++ = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2592 *q++ = 0xff; /* payload_type = 257 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2593 *q++ = 0x02;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2594 *q++ = 2; /* payload_size = 2 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2595 *q++ = frame_ticks >> 8;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2596 *q++ = frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2597 *q++ = 0x80; /* extra '1' bit and align to byte */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2598 /* Note: the 0x00 0x00 b pattern with b <= 3 cannot happen, so no
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2599 need to escape */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2600 nal_len = q - nal_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2601 if (dyn_buf_resize(out_buf, out_buf->len + nal_len) < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2602 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2603 memcpy(out_buf->buf + out_buf->len, nal_buf, nal_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2604 out_buf->len += nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2605 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2606 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2607
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2608 static int build_modified_hevc(uint8_t **pout_buf,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2609 const uint8_t *cbuf, int cbuf_len,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2610 const uint8_t *abuf, int abuf_len,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2611 const uint16_t *frame_duration_tab,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2612 int frame_count)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2613 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2614 DynBuf out_buf_s, *out_buf = &out_buf_s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2615 uint8_t *msps;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2616 const uint8_t *nal_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2617 int msps_len, cidx, aidx, is_alpha, nal_len, first_nal, start, l, frame_num;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2618
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2619 dyn_buf_init(out_buf);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2620
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2621 /* add alpha MSPS */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2622 aidx = 0; /* avoids warning */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2623 if (abuf) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2624 aidx = build_modified_sps(&msps, &msps_len, abuf, abuf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2625 if (aidx < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2626 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2627 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2628 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2629 memcpy(out_buf->buf + out_buf->len, msps, msps_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2630 out_buf->len += msps_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2631 free(msps);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2632 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2633
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2634 /* add color MSPS */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2635 cidx = build_modified_sps(&msps, &msps_len, cbuf, cbuf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2636 if (cidx < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2637 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2638 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2639 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2640 memcpy(out_buf->buf + out_buf->len, msps, msps_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2641 out_buf->len += msps_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2642 free(msps);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2643
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2644 /* add the remaining NALs, alternating between alpha (if present)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2645 and color. */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2646 is_alpha = (abuf != NULL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2647 first_nal = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2648 frame_num = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2649 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2650 if (!is_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2651 if (cidx >= cbuf_len) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2652 if (abuf) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2653 fprintf(stderr, "Incorrect number of alpha NALs\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2654 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2655 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2656 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2657 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2658 nal_buf = cbuf + cidx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2659 nal_len = find_nal_end(nal_buf, cbuf_len - cidx);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2660 // printf("cidx=%d/%d nal_len=%d\n", cidx, cbuf_len, nal_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2661 if (nal_len < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2662 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2663 cidx += nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2664 } else {
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2665 if (aidx >= abuf_len)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2666 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2667 nal_buf = abuf + aidx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2668 nal_len = find_nal_end(nal_buf, abuf_len - aidx);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2669 // printf("aidx=%d/%d nal_len=%d\n", aidx, abuf_len, nal_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2670 if (nal_len < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2671 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2672 aidx += nal_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2673 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2674 start = 3 + (nal_buf[2] == 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2675 if (!is_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2676 int nut;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2677 /* add SEI NAL for the frame duration (animation case) */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2678 nut = (nal_buf[start] >> 1) & 0x3f;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2679 if ((nut <= 9 || (nut >= 16 && nut <= 21)) &&
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2680 start + 2 < nal_len && (nal_buf[start + 2] & 0x80)) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2681 int frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2682 assert(frame_num < frame_count);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2683 frame_ticks = frame_duration_tab[frame_num];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2684 if (frame_ticks > 1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2685 add_frame_duration_sei(out_buf, frame_ticks);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2686 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2687 frame_num++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2688 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2689 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2690 if (first_nal) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2691 /* skip first start code */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2692 l = start;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2693 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2694 l = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2695 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2696 if (dyn_buf_resize(out_buf, out_buf->len + nal_len - l) < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2697 goto fail;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2698 // printf("add nal len=%d\n", nal_len - l);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2699 memcpy(out_buf->buf + out_buf->len, nal_buf + l, nal_len - l);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2700 if (is_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2701 /* set nul_layer_id of alpha to '1' */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2702 out_buf->buf[out_buf->len + (start - l) + 1] |= 1 << 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2703 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2704 out_buf->len += nal_len - l;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2705
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2706 if (abuf) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2707 is_alpha ^= 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2708 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2709 first_nal = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2710 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2711 *pout_buf = out_buf->buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2712 return out_buf->len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2713 fail:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2714 free(out_buf->buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2715 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2716 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2717
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2718 typedef enum {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2719 #if defined(USE_X265)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2720 HEVC_ENCODER_X265,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2721 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2722 #if defined(USE_JCTVC)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2723 HEVC_ENCODER_JCTVC,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2724 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2725
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2726 HEVC_ENCODER_COUNT,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2727 } HEVCEncoderEnum;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2728
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2729 static char *hevc_encoder_name[HEVC_ENCODER_COUNT] = {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2730 #if defined(USE_X265)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2731 "x265",
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2732 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2733 #if defined(USE_JCTVC)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2734 "jctvc",
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2735 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2736 };
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2737
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2738 static HEVCEncoder *hevc_encoder_tab[HEVC_ENCODER_COUNT] = {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2739 #if defined(USE_X265)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2740 &x265_hevc_encoder,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2741 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2742 #if defined(USE_JCTVC)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2743 &jctvc_encoder,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2744 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2745 };
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2746
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2747 #define IMAGE_HEADER_MAGIC 0x425047fb
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2748
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2749 #define DEFAULT_OUTFILENAME "out.bpg"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2750 #define DEFAULT_QP 29
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2751 #define DEFAULT_BIT_DEPTH 8
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2752
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2753 #ifdef RExt__HIGH_BIT_DEPTH_SUPPORT
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2754 #define BIT_DEPTH_MAX 14
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2755 #else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2756 #define BIT_DEPTH_MAX 12
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2757 #endif
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2758 #define DEFAULT_COMPRESS_LEVEL 8
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2759
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2760
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2761 typedef struct BPGEncoderContext BPGEncoderContext;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2762
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2763 typedef struct BPGEncoderParameters {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2764 int qp; /* 0 ... 51 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2765 int alpha_qp; /* -1 ... 51. -1 means same as qp */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2766 int lossless; /* true if lossless compression (qp and alpha_qp are
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2767 ignored) */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2768 BPGImageFormatEnum preferred_chroma_format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2769 int sei_decoded_picture_hash; /* 0, 1 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2770 int compress_level; /* 1 ... 9 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2771 int verbose;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2772 HEVCEncoderEnum encoder_type;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2773 int animated; /* 0 ... 1: if true, encode as animated image */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2774 uint16_t loop_count; /* animations: number of loops. 0=infinite */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2775 /* animations: the frame delay is a multiple of
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2776 frame_delay_num/frame_delay_den seconds */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2777 uint16_t frame_delay_num;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2778 uint16_t frame_delay_den;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2779 } BPGEncoderParameters;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2780
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2781 typedef int BPGEncoderWriteFunc(void *opaque, const uint8_t *buf, int buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2782
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2783 struct BPGEncoderContext {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2784 BPGEncoderParameters params;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2785 BPGMetaData *first_md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2786 HEVCEncoder *encoder;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2787 int frame_count;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2788 HEVCEncoderContext *enc_ctx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2789 HEVCEncoderContext *alpha_enc_ctx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2790 int frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2791 uint16_t *frame_duration_tab;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2792 int frame_duration_tab_size;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2793 };
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2794
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2795 void *mallocz(size_t size)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2796 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2797 void *ptr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2798 ptr = malloc(size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2799 if (!ptr)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2800 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2801 memset(ptr, 0, size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2802 return ptr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2803 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2804
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2805 BPGEncoderParameters *bpg_encoder_param_alloc(void)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2806 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2807 BPGEncoderParameters *p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2808 p = mallocz(sizeof(BPGEncoderParameters));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2809 if (!p)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2810 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2811 p->qp = DEFAULT_QP;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2812 p->alpha_qp = -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2813 p->preferred_chroma_format = BPG_FORMAT_420;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2814 p->compress_level = DEFAULT_COMPRESS_LEVEL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2815 p->frame_delay_num = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2816 p->frame_delay_den = 25;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2817 p->loop_count = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2818 return p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2819 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2820
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2821 void bpg_encoder_param_free(BPGEncoderParameters *p)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2822 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2823 free(p);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2824 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2825
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2826 BPGEncoderContext *bpg_encoder_open(BPGEncoderParameters *p)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2827 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2828 BPGEncoderContext *s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2829
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2830 s = mallocz(sizeof(BPGEncoderContext));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2831 if (!s)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2832 return NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2833 s->params = *p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2834 s->encoder = hevc_encoder_tab[s->params.encoder_type];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2835 s->frame_ticks = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2836 return s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2837 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2838
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2839 void bpg_encoder_set_extension_data(BPGEncoderContext *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2840 BPGMetaData *md)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2841 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2842 s->first_md = md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2843 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2844
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2845 static int bpg_encoder_encode_trailer(BPGEncoderContext *s,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2846 BPGEncoderWriteFunc *write_func,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2847 void *opaque)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2848 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2849 uint8_t *out_buf, *alpha_buf, *hevc_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2850 int out_buf_len, alpha_buf_len, hevc_buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2851
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2852 out_buf_len = s->encoder->close(s->enc_ctx, &out_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2853 if (out_buf_len < 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2854 fprintf(stderr, "Error while encoding picture\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2855 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2856 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2857 s->enc_ctx = NULL;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2858
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2859 alpha_buf = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2860 alpha_buf_len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2861 if (s->alpha_enc_ctx) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2862 alpha_buf_len = s->encoder->close(s->alpha_enc_ctx, &alpha_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2863 if (alpha_buf_len < 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2864 fprintf(stderr, "Error while encoding picture (alpha plane)\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2865 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2866 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2867 s->alpha_enc_ctx = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2868 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2869
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2870 hevc_buf = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2871 hevc_buf_len = build_modified_hevc(&hevc_buf, out_buf, out_buf_len,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2872 alpha_buf, alpha_buf_len,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2873 s->frame_duration_tab, s->frame_count);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2874 if (hevc_buf_len < 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2875 fprintf(stderr, "Error while creating HEVC data\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2876 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2877 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2878 free(out_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2879 free(alpha_buf);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2880
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2881 if (write_func(opaque, hevc_buf, hevc_buf_len) != hevc_buf_len) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2882 fprintf(stderr, "Error while writing HEVC data\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2883 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2884 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2885 free(hevc_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2886 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2887 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2888
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2889 int bpg_encoder_set_frame_duration(BPGEncoderContext *s, int frame_ticks)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2890 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2891 if (frame_ticks >= 1 && frame_ticks <= 65535) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2892 s->frame_ticks = frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2893 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2894 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2895 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2896 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2897 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2898
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2899 /* Warning: currently 'img' is modified. When encoding animations, img
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2900 = NULL indicates the end of the stream. */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2901 int bpg_encoder_encode(BPGEncoderContext *s, Image *img,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2902 BPGEncoderWriteFunc *write_func,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2903 void *opaque)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2904 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2905 const BPGEncoderParameters *p = &s->params;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2906 Image *img_alpha;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2907 HEVCEncodeParams ep_s, *ep = &ep_s;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2908 uint8_t *extension_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2909 int extension_buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2910 int cb_size, width, height;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2911
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2912 if (p->animated && !img) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2913 return bpg_encoder_encode_trailer(s, write_func, opaque);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2914 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2915
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2916 /* extract the alpha plane */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2917 if (img->has_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2918 int c_idx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2919
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2920 img_alpha = malloc(sizeof(Image));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2921 memset(img_alpha, 0, sizeof(*img_alpha));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2922 if (img->format == BPG_FORMAT_GRAY)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2923 c_idx = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2924 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2925 c_idx = 3;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2926
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2927 img_alpha->w = img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2928 img_alpha->h = img->h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2929 img_alpha->format = BPG_FORMAT_GRAY;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2930 img_alpha->has_alpha = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2931 img_alpha->color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2932 img_alpha->bit_depth = img->bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2933 img_alpha->pixel_shift = img->pixel_shift;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2934 img_alpha->data[0] = img->data[c_idx];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2935 img_alpha->linesize[0] = img->linesize[c_idx];
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2936
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2937 img->data[c_idx] = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2938 img->has_alpha = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2939 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2940 img_alpha = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2941 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2942
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2943 if (img->format == BPG_FORMAT_444 && img->color_space != BPG_CS_RGB) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2944 if (p->preferred_chroma_format == BPG_FORMAT_420 ||
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2945 p->preferred_chroma_format == BPG_FORMAT_420_VIDEO) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2946 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_420);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2947 if (image_ycc444_to_ycc420(img, c_h_phase) != 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2948 goto error_convert;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2949 } else if (p->preferred_chroma_format == BPG_FORMAT_422 ||
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2950 p->preferred_chroma_format == BPG_FORMAT_422_VIDEO) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2951 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_422);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2952 if (image_ycc444_to_ycc422(img, c_h_phase) != 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2953 error_convert:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2954 fprintf(stderr, "Cannot convert image\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2955 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2956 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2957 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2958 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2959
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2960 cb_size = 8; /* XXX: should make it configurable. We assume the
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2961 HEVC encoder uses the same value */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2962 width = img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2963 height = img->h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2964 image_pad(img, cb_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2965 if (img_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2966 image_pad(img_alpha, cb_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2967
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2968 /* convert to the allocated pixel width to 8 bit if needed by the
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2969 HEVC encoder */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2970 if (img->bit_depth == 8) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2971 image_convert16to8(img);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2972 if (img_alpha)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2973 image_convert16to8(img_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2974 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
2975
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2976 if (s->frame_count == 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2977 memset(ep, 0, sizeof(*ep));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2978 ep->qp = p->qp;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2979 ep->width = img->w;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2980 ep->height = img->h;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2981 ep->chroma_format = img->format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2982 ep->bit_depth = img->bit_depth;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2983 ep->intra_only = !p->animated;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2984 ep->lossless = p->lossless;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2985 ep->sei_decoded_picture_hash = p->sei_decoded_picture_hash;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2986 ep->compress_level = p->compress_level;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2987 ep->verbose = p->verbose;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2988
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2989 s->enc_ctx = s->encoder->open(ep);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2990 if (!s->enc_ctx) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2991 fprintf(stderr, "Error while opening encoder\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2992 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2993 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2994
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2995 if (img_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2996 if (p->alpha_qp < 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2997 ep->qp = p->qp;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2998 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2999 ep->qp = p->alpha_qp;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3000 ep->chroma_format = 0;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3001
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3002 s->alpha_enc_ctx = s->encoder->open(ep);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3003 if (!s->alpha_enc_ctx) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3004 fprintf(stderr, "Error while opening alpha encoder\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3005 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3006 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3007 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3008
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3009 /* prepare the extension data */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3010 if (p->animated) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3011 BPGMetaData *md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3012 uint8_t buf[15], *q;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3013
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3014 md = bpg_md_alloc(BPG_EXTENSION_TAG_ANIM_CONTROL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3015 q = buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3016 put_ue(&q, p->loop_count);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3017 put_ue(&q, p->frame_delay_num);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3018 put_ue(&q, p->frame_delay_den);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3019 md->buf_len = q - buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3020 md->buf = malloc(md->buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3021 memcpy(md->buf, buf, md->buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3022 md->next = s->first_md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3023 s->first_md = md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3024 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3025
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3026 extension_buf = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3027 extension_buf_len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3028 if (s->first_md) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3029 BPGMetaData *md1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3030 int max_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3031 uint8_t *q;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3032
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3033 max_len = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3034 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3035 max_len += md1->buf_len + 5 * 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3036 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3037 extension_buf = malloc(max_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3038 q = extension_buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3039 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3040 put_ue(&q, md1->tag);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3041 put_ue(&q, md1->buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3042 memcpy(q, md1->buf, md1->buf_len);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3043 q += md1->buf_len;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3044 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3045 extension_buf_len = q - extension_buf;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3046
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3047 bpg_md_free(s->first_md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3048 s->first_md = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3049 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3050
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3051 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3052 uint8_t img_header[128], *q;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3053 int v, has_alpha, has_extension, alpha2_flag, alpha1_flag, format;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3054
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3055 has_alpha = (img_alpha != NULL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3056 has_extension = (extension_buf_len > 0);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3057
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3058
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3059 if (has_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3060 if (img->has_w_plane) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3061 alpha1_flag = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3062 alpha2_flag = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3063 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3064 alpha1_flag = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3065 alpha2_flag = img->premultiplied_alpha;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3066 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3067 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3068 alpha1_flag = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3069 alpha2_flag = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3070 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3071
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3072 q = img_header;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3073 *q++ = (IMAGE_HEADER_MAGIC >> 24) & 0xff;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3074 *q++ = (IMAGE_HEADER_MAGIC >> 16) & 0xff;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3075 *q++ = (IMAGE_HEADER_MAGIC >> 8) & 0xff;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3076 *q++ = (IMAGE_HEADER_MAGIC >> 0) & 0xff;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3077
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3078 if (img->c_h_phase == 0 && img->format == BPG_FORMAT_420)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3079 format = BPG_FORMAT_420_VIDEO;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3080 else if (img->c_h_phase == 0 && img->format == BPG_FORMAT_422)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3081 format = BPG_FORMAT_422_VIDEO;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3082 else
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3083 format = img->format;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3084 v = (format << 5) | (alpha1_flag << 4) | (img->bit_depth - 8);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3085 *q++ = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3086 v = (img->color_space << 4) | (has_extension << 3) |
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3087 (alpha2_flag << 2) | (img->limited_range << 1) |
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3088 p->animated;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3089 *q++ = v;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3090 put_ue(&q, width);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3091 put_ue(&q, height);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3092
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3093 put_ue(&q, 0); /* zero length means up to the end of the file */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3094 if (has_extension) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3095 put_ue(&q, extension_buf_len); /* extension data length */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3096 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3097
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3098 write_func(opaque, img_header, q - img_header);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3099
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3100 if (has_extension) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3101 if (write_func(opaque, extension_buf, extension_buf_len) != extension_buf_len) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3102 fprintf(stderr, "Error while writing extension data\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3103 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3104 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3105 free(extension_buf);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3106 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3107 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3108 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3109
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3110 /* store the frame duration */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3111 if ((s->frame_count + 1) > s->frame_duration_tab_size) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3112 s->frame_duration_tab_size = (s->frame_duration_tab_size * 3) / 2;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3113 if (s->frame_duration_tab_size < (s->frame_count + 1))
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3114 s->frame_duration_tab_size = (s->frame_count + 1);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3115 s->frame_duration_tab = realloc(s->frame_duration_tab,
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3116 sizeof(s->frame_duration_tab) * s->frame_duration_tab_size);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3117 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3118 s->frame_duration_tab[s->frame_count] = s->frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3119
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3120 s->encoder->encode(s->enc_ctx, img);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3121
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3122 if (img_alpha) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3123 s->encoder->encode(s->alpha_enc_ctx, img_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3124 image_free(img_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3125 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3126
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3127 s->frame_count++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3128
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3129 if (!p->animated)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3130 bpg_encoder_encode_trailer(s, write_func, opaque);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3131
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3132 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3133 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3134
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3135 void bpg_encoder_close(BPGEncoderContext *s)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3136 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3137 free(s->frame_duration_tab);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3138 bpg_md_free(s->first_md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3139 free(s);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3140 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3141
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3142 static int my_write_func(void *opaque, const uint8_t *buf, int buf_len)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3143 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3144 FILE *f = opaque;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3145 return fwrite(buf, 1, buf_len, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3146 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3147
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3148 static int get_filename_num(char *buf, int buf_size, const char *str, int n)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3149 {
15
29527d65b71a Fix assigned-but-unused variable warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 14
diff changeset
3150 const char *p;
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3151 char *q;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3152 int l, c;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3153
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3154 q = buf;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3155 p = str;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3156 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3157 c = *p++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3158 if (c == '\0')
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3159 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3160 if (c == '%') {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3161 l = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3162 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3163 c = *p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3164 if (c < '0' || c > '9')
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3165 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3166 l = l * 10 + (c - '0');
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3167 p++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3168 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3169 c = *p++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3170 if (c == '%') {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3171 goto add_char;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3172 } else if (c != 'd') {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3173 return -1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3174 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3175 snprintf(q, buf + buf_size - q, "%0*u", l, n);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3176 q += strlen(q);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3177
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3178 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3179 add_char:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3180 if ((q - buf) < buf_size - 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3181 *q++ = c;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3182 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3183 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3184 *q = '\0';
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3185 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3186 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3187
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3188 void help(int is_full)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3189 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3190 char hevc_encoders[128];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3191 int i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3192
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3193 hevc_encoders[0] = '\0';
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3194 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3195 if (i != 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3196 strcat(hevc_encoders, " ");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3197 strcat(hevc_encoders, hevc_encoder_name[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3198 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3199
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3200 printf("BPG Image Encoder version " CONFIG_BPG_VERSION "\n"
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3201 "usage: bpgenc [options] infile.[jpg|png|tiff]\n"
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3202 "\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3203 "Main options:\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3204 "-h show the full help (including the advanced options)\n"
28
76a105267445 Move information about -keepmetadata under main options.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
3205 "-keepmetadata keep the metadata (from JPEG: EXIF, ICC profile, XMP,\n"
76a105267445 Move information about -keepmetadata under main options.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
3206 " from PNG: ICC profile, from TIFF: EXIF, ICC and XMP.)\n"
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3207 "-o outfile set output filename (default = %s)\n"
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3208 "-q qp set quantizer parameter (smaller gives better quality,\n"
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3209 " range: 0-51, default = %d)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3210 "-f cfmt set the preferred chroma format (420, 422, 444,\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3211 " default=420)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3212 "-c color_space set the preferred color space (ycbcr, rgb, ycgco,\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3213 " ycbcr_bt709, ycbcr_bt2020, default=ycbcr)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3214 "-b bit_depth set the bit depth (8 to %d, default = %d)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3215 "-lossless enable lossless mode\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3216 "-e encoder select the HEVC encoder (%s, default = %s)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3217 "-m level select the compression level (1=fast, 9=slow, default = %d)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3218 "\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3219 "Animation options:\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3220 "-a generate animations from a sequence of images. Use %%d or\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3221 " %%Nd (N = number of digits) in the filename to specify the\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3222 " image index, starting from 0 or 1.\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3223 "-fps N set the frame rate (default = 25)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3224 "-loop N set the number of times the animation is played. 0 means\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3225 " infinite (default = 0)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3226 "-delayfile file text file containing one number per image giving the\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3227 " display delay per image in centiseconds.\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3228 , DEFAULT_OUTFILENAME, DEFAULT_QP, BIT_DEPTH_MAX, DEFAULT_BIT_DEPTH,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3229 hevc_encoders, hevc_encoder_name[0], DEFAULT_COMPRESS_LEVEL);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3230
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3231 if (is_full) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3232 printf("\nAdvanced options:\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3233 "-alphaq set quantizer parameter for the alpha channel (default = same as -q value)\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3234 "-premul store the color with premultiplied alpha\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3235 "-limitedrange encode the color data with the limited range of video\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3236 "-hash include MD5 hash in HEVC bitstream\n"
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3237 "-v show debug messages\n"
25
a6e6f87414ea Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
3238 );
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3239 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3240
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3241 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3242 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3243
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3244 struct option long_opts[] = {
16
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3245 { "hash" , no_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3246 { "keepmetadata" , no_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3247 { "alphaq" , required_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3248 { "lossless" , no_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3249 { "limitedrange" , no_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3250 { "premul" , no_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3251 { "loop" , required_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3252 { "fps" , required_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3253 { "delayfile" , required_argument , NULL, 0 },
86b6c976ef2d Fix some warnings by adding missing field initializers for long opts.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
3254 { NULL , 0 , NULL, 0 },
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3255 };
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3256
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3257 int main(int argc, char **argv)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3258 {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3259 const char *infilename, *outfilename, *frame_delay_file;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3260 Image *img;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3261 FILE *f;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3262 int c, option_index;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3263 int keep_metadata;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3264 int bit_depth, i, limited_range, premultiplied_alpha;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3265 BPGColorSpaceEnum color_space;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3266 BPGMetaData *md;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3267 BPGEncoderContext *enc_ctx;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3268 BPGEncoderParameters *p;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3269
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3270 p = bpg_encoder_param_alloc();
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3271
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3272 outfilename = DEFAULT_OUTFILENAME;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3273 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3274 keep_metadata = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3275 bit_depth = DEFAULT_BIT_DEPTH;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3276 limited_range = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3277 premultiplied_alpha = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3278 frame_delay_file = NULL;
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3279
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3280 for(;;) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3281 c = getopt_long_only(argc, argv, "q:o:hf:c:vm:b:e:a", long_opts, &option_index);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3282 if (c == -1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3283 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3284 switch(c) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3285 case 0:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3286 switch(option_index) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3287 case 0:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3288 p->sei_decoded_picture_hash = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3289 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3290 case 1:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3291 keep_metadata = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3292 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3293 case 2:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3294 p->alpha_qp = atoi(optarg);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3295 if (p->alpha_qp < 0 || p->alpha_qp > 51) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3296 fprintf(stderr, "alpha_qp must be between 0 and 51\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3297 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3298 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3299 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3300 case 3:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3301 p->lossless = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3302 color_space = BPG_CS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3303 p->preferred_chroma_format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3304 bit_depth = 8;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3305 limited_range = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3306 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3307 case 4:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3308 limited_range = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3309 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3310 case 5:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3311 premultiplied_alpha = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3312 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3313 case 6:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3314 p->loop_count = strtoul(optarg, NULL, 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3315 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3316 case 7:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3317 p->frame_delay_num = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3318 p->frame_delay_den = strtoul(optarg, NULL, 0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3319 if (p->frame_delay_den == 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3320 fprintf(stderr, "invalid frame rate\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3321 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3322 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3323 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3324 case 8:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3325 frame_delay_file = optarg;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3326 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3327 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3328 goto show_help;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3329 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3330 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3331 case 'h':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3332 show_help:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3333 help(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3334 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3335 case 'q':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3336 p->qp = atoi(optarg);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3337 if (p->qp < 0 || p->qp > 51) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3338 fprintf(stderr, "qp must be between 0 and 51\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3339 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3340 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3341 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3342 case 'o':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3343 outfilename = optarg;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3344 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3345 case 'f':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3346 if (!strcmp(optarg, "420")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3347 p->preferred_chroma_format = BPG_FORMAT_420;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3348 } else if (!strcmp(optarg, "422")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3349 p->preferred_chroma_format = BPG_FORMAT_422;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3350 } else if (!strcmp(optarg, "444")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3351 p->preferred_chroma_format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3352 } else if (!strcmp(optarg, "422_video")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3353 p->preferred_chroma_format = BPG_FORMAT_422_VIDEO;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3354 } else if (!strcmp(optarg, "420_video")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3355 p->preferred_chroma_format = BPG_FORMAT_420_VIDEO;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3356 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3357 fprintf(stderr, "Invalid chroma format\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3358 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3359 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3360 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3361 case 'c':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3362 if (!strcmp(optarg, "ycbcr")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3363 color_space = BPG_CS_YCbCr;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3364 } else if (!strcmp(optarg, "rgb")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3365 color_space = BPG_CS_RGB;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3366 p->preferred_chroma_format = BPG_FORMAT_444;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3367 } else if (!strcmp(optarg, "ycgco")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3368 color_space = BPG_CS_YCgCo;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3369 } else if (!strcmp(optarg, "ycbcr_bt709")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3370 color_space = BPG_CS_YCbCr_BT709;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3371 } else if (!strcmp(optarg, "ycbcr_bt2020")) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3372 color_space = BPG_CS_YCbCr_BT2020;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3373 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3374 fprintf(stderr, "Invalid color space format\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3375 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3376 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3377 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3378 case 'm':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3379 p->compress_level = atoi(optarg);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3380 if (p->compress_level < 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3381 p->compress_level = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3382 else if (p->compress_level > 9)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3383 p->compress_level = 9;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3384 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3385 case 'b':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3386 bit_depth = atoi(optarg);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3387 if (bit_depth < 8 || bit_depth > BIT_DEPTH_MAX) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3388 fprintf(stderr, "Invalid bit depth (range: 8 to %d)\n",
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3389 BIT_DEPTH_MAX);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3390 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3391 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3392 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3393 case 'v':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3394 p->verbose++;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3395 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3396 case 'e':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3397 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3398 if (!strcmp(optarg, hevc_encoder_name[i]))
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3399 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3400 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3401 if (i == HEVC_ENCODER_COUNT) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3402 fprintf(stderr, "Unsupported encoder. Available ones are:");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3403 for(i = 0; i < HEVC_ENCODER_COUNT; i++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3404 fprintf(stderr, " %s", hevc_encoder_name[i]);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3405 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3406 fprintf(stderr, "\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3407 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3408 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3409 p->encoder_type = i;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3410 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3411 case 'a':
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3412 p->animated = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3413 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3414 default:
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3415 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3416 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3417 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3418
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3419 if (optind >= argc)
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3420 help(0);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3421 infilename = argv[optind];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3422
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3423 f = fopen(outfilename, "wb");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3424 if (!f) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3425 perror(outfilename);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3426 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3427 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3428
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3429 enc_ctx = bpg_encoder_open(p);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3430 if (!enc_ctx) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3431 fprintf(stderr, "Could not open BPG encoder\n");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3432 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3433 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3434
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3435 if (p->animated) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3436 int frame_num, first_frame, frame_ticks;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3437 char filename[1024];
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3438 FILE *f1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3439
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3440 if (frame_delay_file) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3441 f1 = fopen(frame_delay_file, "r");
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3442 if (!f1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3443 fprintf(stderr, "Could not open '%s'\n", frame_delay_file);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3444 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3445 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3446 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3447 f1 = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3448 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3449
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3450 first_frame = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3451 for(frame_num = 0; ; frame_num++) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3452 if (get_filename_num(filename, sizeof(filename), infilename, frame_num) < 0) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3453 fprintf(stderr, "Invalid filename syntax: '%s'\n", infilename);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3454 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3455 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3456 img = load_image(&md, filename, color_space, bit_depth, limited_range,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3457 premultiplied_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3458 if (!img) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3459 if (frame_num == 0)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3460 continue; /* accept to start at 0 or 1 */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3461 if (first_frame) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3462 fprintf(stderr, "Could not read '%s'\n", filename);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3463 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3464 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3465 break;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3466 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3467 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3468 frame_ticks = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3469 if (f1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3470 float fdelay;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3471 if (fscanf(f1, "%f", &fdelay) == 1) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3472 frame_ticks = lrint(fdelay * p->frame_delay_den / (p->frame_delay_num * 100));
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3473 if (frame_ticks < 1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3474 frame_ticks = 1;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3475 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3476 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3477
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3478 if (p->verbose)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3479 printf("Encoding '%s' ticks=%d\n", filename, frame_ticks);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3480
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3481 if (keep_metadata && first_frame) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3482 bpg_encoder_set_extension_data(enc_ctx, md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3483 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3484 bpg_md_free(md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3485 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3486 bpg_encoder_set_frame_duration(enc_ctx, frame_ticks);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3487 bpg_encoder_encode(enc_ctx, img, my_write_func, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3488 image_free(img);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3489
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3490 first_frame = 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3491 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3492 if (f1)
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3493 fclose(f1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3494 /* end of stream */
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3495 bpg_encoder_encode(enc_ctx, NULL, my_write_func, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3496 } else {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3497 img = load_image(&md, infilename, color_space, bit_depth, limited_range,
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3498 premultiplied_alpha);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3499 if (!img) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3500 fprintf(stderr, "Could not read '%s'\n", infilename);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3501 exit(1);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3502 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3503
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3504 if (!keep_metadata && md) {
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3505 bpg_md_free(md);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3506 md = NULL;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3507 }
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3508
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3509 bpg_encoder_set_extension_data(enc_ctx, md);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3510
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3511 bpg_encoder_encode(enc_ctx, img, my_write_func, f);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3512 image_free(img);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3513 }
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3514
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3515 fclose(f);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3516
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3517 bpg_encoder_close(enc_ctx);
5
524eae707ba4 Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
3518
0
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3519 bpg_encoder_param_free(p);
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3520
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3521 return 0;
772086c29cc7 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3522 }