1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "mpp_mem.h"
18 #include "mpp_debug.h"
19 #include "mpp_common.h"
20
21 #include "hal_jpege_hdr.h"
22
23 #define MAX_NUMBER_OF_COMPONENTS 3
24
25 /* JPEG markers, table B.1 */
26 enum {
27 SOI = 0xFFD8, /* Start of Image */
28 DQT = 0xFFDB, /* Define Quantization Table(s) */
29 SOF0 = 0xFFC0, /* Start of Frame */
30 DRI = 0xFFDD, /* Define Restart Interval */
31 RST0 = 0xFFD0, /* Restart marker 0 */
32 RST1 = 0xFFD1, /* Restart marker 1 */
33 RST2 = 0xFFD2, /* Restart marker 2 */
34 RST3 = 0xFFD3, /* Restart marker 3 */
35 RST4 = 0xFFD4, /* Restart marker 4 */
36 RST5 = 0xFFD5, /* Restart marker 5 */
37 RST6 = 0xFFD6, /* Restart marker 6 */
38 RST7 = 0xFFD7, /* Restart marker 7 */
39 DHT = 0xFFC4, /* Define Huffman Table(s) */
40 SOS = 0xFFDA, /* Start of Scan */
41 EOI = 0xFFD9, /* End of Image */
42 APP0 = 0xFFE0, /* APP0 Marker */
43 APP7 = 0xFFE7, /* APP7 Marker */
44 COM = 0xFFFE /* Comment marker */
45 };
46
47 enum {
48 JPEGE_NO_UNITS = 0,
49 JPEGE_DOTS_PER_INCH = 1,
50 JPEGE_DOTS_PER_CM = 2
51 };
52
53 enum {
54 JPEGE_SINGLE_MARKER,
55 JPEGE_MULTI_MARKER
56 };
57
58 static const RK_U8 zigzag[64] = {
59 0, 1, 8, 16, 9, 2, 3, 10,
60 17, 24, 32, 25, 18, 11, 4, 5,
61 12, 19, 26, 33, 40, 48, 41, 34,
62 27, 20, 13, 6, 7, 14, 21, 28,
63 35, 42, 49, 56, 57, 50, 43, 36,
64 29, 22, 15, 23, 30, 37, 44, 51,
65 58, 59, 52, 45, 38, 31, 39, 46,
66 53, 60, 61, 54, 47, 55, 62, 63
67 };
68
69 /* Mjpeg quantization tables levels 0-10 */
70 static const RK_U8 qtable_y[11][64] = {
71 {
72 80, 56, 50, 80, 120, 200, 248, 248,
73 60, 60, 72, 96, 136, 248, 248, 248,
74 72, 68, 80, 120, 200, 248, 248, 248,
75 72, 88, 112, 152, 248, 248, 248, 248,
76 92, 112, 192, 248, 248, 248, 248, 248,
77 120, 176, 248, 248, 248, 248, 248, 248,
78 152, 248, 248, 248, 248, 248, 248, 248,
79 248, 248, 248, 248, 248, 248, 248, 248
80 },
81
82 {
83 40, 28, 25, 40, 60, 100, 128, 160,
84 30, 30, 36, 48, 68, 152, 152, 144,
85 36, 34, 40, 60, 100, 144, 176, 144,
86 36, 44, 56, 76, 128, 224, 200, 160,
87 46, 56, 96, 144, 176, 248, 248, 200,
88 60, 88, 144, 160, 208, 248, 248, 232,
89 124, 160, 200, 224, 248, 248, 248, 248,
90 184, 232, 240, 248, 248, 248, 248, 248
91 },
92
93 {
94 27, 18, 17, 27, 40, 68, 88, 104,
95 20, 20, 23, 32, 44, 96, 100, 92,
96 23, 22, 27, 40, 68, 96, 116, 96,
97 23, 28, 38, 48, 88, 144, 136, 104,
98 30, 38, 62, 96, 116, 184, 176, 128,
99 40, 58, 92, 108, 136, 176, 192, 160,
100 84, 108, 136, 144, 176, 208, 200, 168,
101 120, 160, 160, 168, 192, 168, 176, 168
102 },
103
104 {
105 20, 14, 13, 20, 30, 50, 64, 76,
106 15, 15, 18, 24, 34, 76, 76, 72,
107 18, 16, 20, 30, 50, 72, 88, 72,
108 18, 21, 28, 36, 64, 112, 100, 80,
109 23, 28, 46, 72, 88, 136, 136, 96,
110 30, 44, 72, 80, 104, 136, 144, 116,
111 62, 80, 100, 112, 136, 152, 152, 128,
112 92, 116, 120, 124, 144, 128, 136, 124
113 },
114
115 {
116 16, 11, 10, 16, 24, 40, 52, 62,
117 12, 12, 14, 19, 26, 58, 60, 56,
118 14, 13, 16, 24, 40, 58, 72, 56,
119 14, 17, 22, 29, 52, 88, 80, 62,
120 18, 22, 38, 56, 68, 112, 104, 80,
121 24, 36, 56, 64, 84, 104, 116, 92,
122 50, 64, 80, 88, 104, 124, 120, 104,
123 72, 92, 96, 100, 124, 100, 104, 100
124 },
125
126 {
127 13, 9, 8, 13, 19, 32, 42, 50,
128 10, 10, 11, 15, 21, 46, 48, 44,
129 11, 10, 13, 19, 32, 46, 56, 46,
130 11, 14, 18, 23, 42, 72, 64, 50,
131 14, 18, 30, 46, 54, 88, 84, 62,
132 19, 28, 44, 52, 68, 84, 92, 76,
133 40, 52, 62, 72, 84, 100, 96, 84,
134 58, 76, 76, 80, 100, 80, 84, 80
135 },
136
137 {
138 10, 7, 6, 10, 14, 24, 31, 38,
139 7, 7, 8, 11, 16, 36, 36, 34,
140 8, 8, 10, 14, 24, 34, 42, 34,
141 8, 10, 13, 17, 32, 52, 48, 38,
142 11, 13, 22, 34, 42, 68, 62, 46,
143 14, 21, 34, 38, 50, 62, 68, 56,
144 29, 38, 48, 52, 62, 76, 72, 62,
145 44, 56, 58, 60, 68, 60, 62, 60
146 },
147
148 {
149 6, 4, 4, 6, 10, 16, 20, 24,
150 5, 5, 6, 8, 10, 23, 24, 22,
151 6, 5, 6, 10, 16, 23, 28, 22,
152 6, 7, 9, 12, 20, 36, 32, 25,
153 7, 9, 15, 22, 27, 44, 42, 31,
154 10, 14, 22, 26, 32, 42, 46, 38,
155 20, 26, 31, 36, 42, 48, 48, 40,
156 29, 38, 38, 40, 46, 40, 42, 40
157 },
158
159 {
160 3, 2, 2, 3, 5, 8, 10, 12,
161 2, 2, 3, 4, 5, 12, 12, 11,
162 3, 3, 3, 5, 8, 11, 14, 11,
163 3, 3, 4, 6, 10, 17, 16, 12,
164 4, 4, 7, 11, 14, 22, 21, 15,
165 5, 7, 11, 13, 16, 21, 23, 18,
166 10, 13, 16, 17, 21, 24, 24, 20,
167 14, 18, 19, 20, 22, 20, 21, 20
168 },
169
170 {
171 1, 1, 1, 1, 2, 3, 3, 4,
172 1, 1, 1, 1, 2, 4, 4, 4,
173 1, 1, 1, 2, 3, 4, 5, 4,
174 1, 1, 1, 2, 3, 6, 5, 4,
175 1, 1, 2, 4, 5, 7, 7, 5,
176 2, 2, 4, 4, 5, 7, 8, 6,
177 3, 4, 5, 6, 7, 8, 8, 7,
178 5, 6, 6, 7, 7, 7, 7, 7
179 },
180
181 {
182 1, 1, 1, 1, 1, 1, 1, 1,
183 1, 1, 1, 1, 1, 1, 1, 1,
184 1, 1, 1, 1, 1, 1, 1, 1,
185 1, 1, 1, 1, 1, 1, 1, 1,
186 1, 1, 1, 1, 1, 1, 1, 1,
187 1, 1, 1, 1, 1, 1, 1, 1,
188 1, 1, 1, 1, 1, 1, 1, 1,
189 1, 1, 1, 1, 1, 1, 1, 1
190 }
191 };
192
193 static const RK_U8 qtable_c[11][64] = {
194 {
195 88, 92, 120, 240, 248, 248, 248, 248,
196 92, 108, 136, 248, 248, 248, 248, 248,
197 120, 136, 248, 248, 248, 248, 248, 248,
198 240, 248, 248, 248, 248, 248, 248, 248,
199 248, 248, 248, 248, 248, 248, 248, 248,
200 248, 248, 248, 248, 248, 248, 248, 248,
201 248, 248, 248, 248, 248, 248, 248, 248,
202 248, 248, 248, 248, 248, 248, 248, 248
203 },
204
205 {
206 44, 46, 60, 120, 248, 248, 248, 248,
207 46, 54, 68, 168, 248, 248, 248, 248,
208 60, 66, 144, 248, 248, 248, 248, 248,
209 120, 168, 248, 248, 248, 248, 248, 248,
210 248, 248, 248, 248, 248, 248, 248, 248,
211 248, 248, 248, 248, 248, 248, 248, 248,
212 248, 248, 248, 248, 248, 248, 248, 248,
213 248, 248, 248, 248, 248, 248, 248, 248
214 },
215
216 {
217 28, 30, 40, 80, 168, 168, 168, 168,
218 30, 36, 44, 112, 168, 168, 168, 168,
219 40, 44, 96, 168, 168, 168, 168, 168,
220 80, 112, 168, 168, 168, 168, 168, 168,
221 168, 168, 168, 168, 168, 168, 168, 168,
222 168, 168, 168, 168, 168, 168, 168, 168,
223 168, 168, 168, 168, 168, 168, 168, 168,
224 168, 168, 168, 168, 168, 168, 168, 168
225 },
226
227 {
228 21, 23, 30, 60, 124, 124, 124, 124,
229 23, 26, 34, 84, 124, 124, 124, 124,
230 30, 34, 72, 124, 124, 124, 124, 124,
231 60, 84, 124, 124, 124, 124, 124, 124,
232 124, 124, 124, 124, 124, 124, 124, 124,
233 124, 124, 124, 124, 124, 124, 124, 124,
234 124, 124, 124, 124, 124, 124, 124, 124,
235 124, 124, 124, 124, 124, 124, 124, 124
236 },
237
238 {
239 17, 18, 24, 48, 100, 100, 100, 100,
240 18, 21, 26, 68, 100, 100, 100, 100,
241 24, 26, 56, 100, 100, 100, 100, 100,
242 48, 68, 100, 100, 100, 100, 100, 100,
243 100, 100, 100, 100, 100, 100, 100, 100,
244 100, 100, 100, 100, 100, 100, 100, 100,
245 100, 100, 100, 100, 100, 100, 100, 100,
246 100, 100, 100, 100, 100, 100, 100, 100
247 },
248
249 {
250 14, 14, 19, 38, 80, 80, 80, 80,
251 14, 17, 21, 54, 80, 80, 80, 80,
252 19, 21, 46, 80, 80, 80, 80, 80,
253 38, 54, 80, 80, 80, 80, 80, 80,
254 80, 80, 80, 80, 80, 80, 80, 80,
255 80, 80, 80, 80, 80, 80, 80, 80,
256 80, 80, 80, 80, 80, 80, 80, 80,
257 80, 80, 80, 80, 80, 80, 80, 80
258 },
259
260 {
261 10, 11, 14, 28, 60, 60, 60, 60,
262 11, 13, 16, 40, 60, 60, 60, 60,
263 14, 16, 34, 60, 60, 60, 60, 60,
264 28, 40, 60, 60, 60, 60, 60, 60,
265 60, 60, 60, 60, 60, 60, 60, 60,
266 60, 60, 60, 60, 60, 60, 60, 60,
267 60, 60, 60, 60, 60, 60, 60, 60,
268 60, 60, 60, 60, 60, 60, 60, 60
269 },
270
271 {
272 7, 7, 10, 19, 40, 40, 40, 40,
273 7, 8, 10, 26, 40, 40, 40, 40,
274 10, 10, 22, 40, 40, 40, 40, 40,
275 19, 26, 40, 40, 40, 40, 40, 40,
276 40, 40, 40, 40, 40, 40, 40, 40,
277 40, 40, 40, 40, 40, 40, 40, 40,
278 40, 40, 40, 40, 40, 40, 40, 40,
279 40, 40, 40, 40, 40, 40, 40, 40
280 },
281
282 {
283 3, 4, 5, 9, 20, 20, 20, 20,
284 4, 4, 5, 13, 20, 20, 20, 20,
285 5, 5, 11, 20, 20, 20, 20, 20,
286 9, 13, 20, 20, 20, 20, 20, 20,
287 20, 20, 20, 20, 20, 20, 20, 20,
288 20, 20, 20, 20, 20, 20, 20, 20,
289 20, 20, 20, 20, 20, 20, 20, 20,
290 20, 20, 20, 20, 20, 20, 20, 20
291 },
292
293 {
294 1, 1, 2, 3, 7, 7, 7, 7,
295 1, 1, 2, 4, 7, 7, 7, 7,
296 2, 2, 4, 7, 7, 7, 7, 7,
297 3, 4, 7, 7, 7, 7, 7, 7,
298 7, 7, 7, 7, 7, 7, 7, 7,
299 7, 7, 7, 7, 7, 7, 7, 7,
300 7, 7, 7, 7, 7, 7, 7, 7,
301 7, 7, 7, 7, 7, 7, 7, 7
302 },
303
304 {
305 1, 1, 1, 1, 1, 1, 1, 1,
306 1, 1, 1, 1, 1, 1, 1, 1,
307 1, 1, 1, 1, 1, 1, 1, 1,
308 1, 1, 1, 1, 1, 1, 1, 1,
309 1, 1, 1, 1, 1, 1, 1, 1,
310 1, 1, 1, 1, 1, 1, 1, 1,
311 1, 1, 1, 1, 1, 1, 1, 1,
312 1, 1, 1, 1, 1, 1, 1, 1
313 }
314 };
315
316 typedef struct {
317 RK_U32 Ci[MAX_NUMBER_OF_COMPONENTS];
318 RK_U32 Hi[MAX_NUMBER_OF_COMPONENTS];
319 RK_U32 Vi[MAX_NUMBER_OF_COMPONENTS];
320 RK_U32 Tqi[MAX_NUMBER_OF_COMPONENTS];
321 } JpegeColorInfo;
322
323 static const JpegeColorInfo color_info[2] = {
324 {
325 /* YUV420 */
326 { 1, 2, 3, },
327 { 2, 1, 1, },
328 { 2, 1, 1, },
329 { 0, 1, 1, },
330 },
331 {
332 /* YUV422 */
333 { 1, 2, 3, },
334 { 2, 1, 1, },
335 { 1, 1, 1, },
336 { 0, 1, 1, },
337 },
338 };
339
340 typedef struct {
341 RK_U32 val_y;
342 RK_U32 val_c;
343 } JpegeHuffmanTable;
344
345 static const JpegeHuffmanTable dc_li[16] = {
346 {0x00, 0x00},
347 {0x01, 0x03},
348 {0x05, 0x01},
349 {0x01, 0x01},
350 {0x01, 0x01},
351 {0x01, 0x01},
352 {0x01, 0x01},
353 {0x01, 0x01},
354 {0x01, 0x01},
355 {0x00, 0x01},
356 {0x00, 0x01},
357 {0x00, 0x00},
358 {0x00, 0x00},
359 {0x00, 0x00},
360 {0x00, 0x00},
361 {0x00, 0x00}
362 };
363
364 static const JpegeHuffmanTable dc_vij[12] = {
365 {0x00, 0x00},
366 {0x01, 0x01},
367 {0x02, 0x02},
368 {0x03, 0x03},
369 {0x04, 0x04},
370 {0x05, 0x05},
371 {0x06, 0x06},
372 {0x07, 0x07},
373 {0x08, 0x08},
374 {0x09, 0x09},
375 {0x0A, 0x0A},
376 {0x0B, 0x0B},
377 };
378
379 static const JpegeHuffmanTable ac_li[16] = {
380 {0x00, 0x00},
381 {0x02, 0x02},
382 {0x01, 0x01},
383 {0x03, 0x02},
384 {0x03, 0x04},
385 {0x02, 0x04},
386 {0x04, 0x03},
387 {0x03, 0x04},
388 {0x05, 0x07},
389 {0x05, 0x05},
390 {0x04, 0x04},
391 {0x04, 0x04},
392 {0x00, 0x00},
393 {0x00, 0x01},
394 {0x01, 0x02},
395 {0x7D, 0x77}
396 };
397
398 static const JpegeHuffmanTable ac_vij[162] = {
399 {0x01, 0x00},
400 {0x02, 0x01},
401 {0x03, 0x02},
402 {0x00, 0x03},
403 {0x04, 0x11},
404 {0x11, 0x04},
405 {0x05, 0x05},
406 {0x12, 0x21},
407 {0x21, 0x31},
408 {0x31, 0x06},
409 {0x41, 0x12},
410 {0x06, 0x41},
411 {0x13, 0x51},
412 {0x51, 0x07},
413 {0x61, 0x61},
414 {0x07, 0x71},
415 {0x22, 0x13},
416 {0x71, 0x22},
417 {0x14, 0x32},
418 {0x32, 0x81},
419 {0x81, 0x08},
420 {0x91, 0x14},
421 {0xA1, 0x42},
422 {0x08, 0x91},
423 {0x23, 0xA1},
424 {0x42, 0xB1},
425 {0xB1, 0xC1},
426 {0xC1, 0x09},
427 {0x15, 0x23},
428 {0x52, 0x33},
429 {0xD1, 0x52},
430 {0xF0, 0xF0},
431 {0x24, 0x15},
432 {0x33, 0x62},
433 {0x62, 0x72},
434 {0x72, 0xD1},
435 {0x82, 0x0A},
436 {0x09, 0x16},
437 {0x0A, 0x24},
438 {0x16, 0x34},
439 {0x17, 0xE1},
440 {0x18, 0x25},
441 {0x19, 0xF1},
442 {0x1A, 0x17},
443 {0x25, 0x18},
444 {0x26, 0x19},
445 {0x27, 0x1A},
446 {0x28, 0x26},
447 {0x29, 0x27},
448 {0x2A, 0x28},
449 {0x34, 0x29},
450 {0x35, 0x2A},
451 {0x36, 0x35},
452 {0x37, 0x36},
453 {0x38, 0x37},
454 {0x39, 0x38},
455 {0x3A, 0x39},
456 {0x43, 0x3A},
457 {0x44, 0x43},
458 {0x45, 0x44},
459 {0x46, 0x45},
460 {0x47, 0x46},
461 {0x48, 0x47},
462 {0x49, 0x48},
463 {0x4A, 0x49},
464 {0x53, 0x4A},
465 {0x54, 0x53},
466 {0x55, 0x54},
467 {0x56, 0x55},
468 {0x57, 0x56},
469 {0x58, 0x57},
470 {0x59, 0x58},
471 {0x5A, 0x59},
472 {0x63, 0x5A},
473 {0x64, 0x63},
474 {0x65, 0x64},
475 {0x66, 0x65},
476 {0x67, 0x66},
477 {0x68, 0x67},
478 {0x69, 0x68},
479 {0x6A, 0x69},
480 {0x73, 0x6A},
481 {0x74, 0x73},
482 {0x75, 0x74},
483 {0x76, 0x75},
484 {0x77, 0x76},
485 {0x78, 0x77},
486 {0x79, 0x78},
487 {0x7A, 0x79},
488 {0x83, 0x7A},
489 {0x84, 0x82},
490 {0x85, 0x83},
491 {0x86, 0x84},
492 {0x87, 0x85},
493 {0x88, 0x86},
494 {0x89, 0x87},
495 {0x8A, 0x88},
496 {0x92, 0x89},
497 {0x93, 0x8A},
498 {0x94, 0x92},
499 {0x95, 0x93},
500 {0x96, 0x94},
501 {0x97, 0x95},
502 {0x98, 0x96},
503 {0x99, 0x97},
504 {0x9A, 0x98},
505 {0xA2, 0x99},
506 {0xA3, 0x9A},
507 {0xA4, 0xA2},
508 {0xA5, 0xA3},
509 {0xA6, 0xA4},
510 {0xA7, 0xA5},
511 {0xA8, 0xA6},
512 {0xA9, 0xA7},
513 {0xAA, 0xA8},
514 {0xB2, 0xA9},
515 {0xB3, 0xAA},
516 {0xB4, 0xB2},
517 {0xB5, 0xB3},
518 {0xB6, 0xB4},
519 {0xB7, 0xB5},
520 {0xB8, 0xB6},
521 {0xB9, 0xB7},
522 {0xBA, 0xB8},
523 {0xC2, 0xB9},
524 {0xC3, 0xBA},
525 {0xC4, 0xC2},
526 {0xC5, 0xC3},
527 {0xC6, 0xC4},
528 {0xC7, 0xC5},
529 {0xC8, 0xC6},
530 {0xC9, 0xC7},
531 {0xCA, 0xC8},
532 {0xD2, 0xC9},
533 {0xD3, 0xCA},
534 {0xD4, 0xD2},
535 {0xD5, 0xD3},
536 {0xD6, 0xD4},
537 {0xD7, 0xD5},
538 {0xD8, 0xD6},
539 {0xD9, 0xD7},
540 {0xDA, 0xD8},
541 {0xE1, 0xD9},
542 {0xE2, 0xDA},
543 {0xE3, 0xE2},
544 {0xE4, 0xE3},
545 {0xE5, 0xE4},
546 {0xE6, 0xE5},
547 {0xE7, 0xE6},
548 {0xE8, 0xE7},
549 {0xE9, 0xE8},
550 {0xEA, 0xE9},
551 {0xF1, 0xEA},
552 {0xF2, 0xF2},
553 {0xF3, 0xF3},
554 {0xF4, 0xF4},
555 {0xF5, 0xF5},
556 {0xF6, 0xF6},
557 {0xF7, 0xF7},
558 {0xF8, 0xF8},
559 {0xF9, 0xF9},
560 {0xFA, 0xFA}
561 };
562
563 typedef struct {
564 RK_U8 *buffer; /* Pointer to first byte of stream */
565 RK_U8 *stream; /* Pointer to next byte of stream */
566 RK_U32 size; /* Byte size of stream buffer */
567 RK_U32 byteCnt; /* Byte counter */
568 RK_U32 bitCnt; /* Bit counter */
569 RK_U32 byteBuffer; /* Byte buffer */
570 RK_U32 bufferedBits; /* Amount of bits in byte buffer, [0-7] */
571 } JpegeBitsImpl;
572
jpege_bits_init(JpegeBits * ctx)573 void jpege_bits_init(JpegeBits *ctx)
574 {
575 JpegeBitsImpl *impl = mpp_malloc(JpegeBitsImpl, 1);
576 *ctx = impl;
577 }
578
jpege_bits_deinit(JpegeBits ctx)579 void jpege_bits_deinit(JpegeBits ctx)
580 {
581 if (ctx)
582 mpp_free(ctx);
583 }
584
jpege_bits_setup(JpegeBits ctx,RK_U8 * buf,RK_S32 size)585 void jpege_bits_setup(JpegeBits ctx, RK_U8 *buf, RK_S32 size)
586 {
587 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
588
589 impl->buffer = buf;
590 impl->stream = buf;
591 impl->size = size;
592 impl->byteCnt = 0;
593 impl->bitCnt = 0;
594 impl->byteBuffer = 0;
595 impl->bufferedBits = 0;
596 }
597
jpege_bits_put(JpegeBits ctx,RK_U32 value,RK_S32 number)598 void jpege_bits_put(JpegeBits ctx, RK_U32 value, RK_S32 number)
599 {
600 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
601
602 RK_U32 bits;
603 RK_U32 byteBuffer = impl->byteBuffer;
604 RK_U8 *stream = impl->stream;
605
606 /* Debug: value is too big */
607 mpp_assert(value < ((RK_U32) 1 << number));
608 mpp_assert(number < 25);
609
610 bits = number + impl->bufferedBits;
611
612 value <<= (32 - bits);
613 byteBuffer = (((RK_U32) stream[0]) << 24) | value;
614
615 while (bits > 7) {
616 *stream = (RK_U8) (byteBuffer >> 24);
617 bits -= 8;
618 byteBuffer <<= 8;
619 stream++;
620 impl->byteCnt++;
621 }
622
623 stream[0] = (RK_U8) (byteBuffer >> 24);
624 impl->stream = stream;
625 impl->bitCnt += number;
626 impl->byteBuffer = byteBuffer;
627 impl->bufferedBits = (RK_U8) bits;
628 }
629
jpege_seek_bits(JpegeBits ctx,RK_S32 len)630 void jpege_seek_bits(JpegeBits ctx, RK_S32 len)
631 {
632 JpegeBitsImpl *impl = (JpegeBitsImpl*)ctx;
633 RK_U32 bits = len;
634 RK_U8 *stream = impl->stream;
635
636 mpp_assert(bits < impl->size);
637
638 while (bits > 7) {
639 stream++;
640 impl->byteCnt++;
641 bits -= 8;
642 }
643
644 impl->stream = stream;
645 impl->bitCnt += len;
646 impl->byteBuffer = (RK_U32)stream[0];
647 impl->bufferedBits = (RK_U8) bits;
648 }
649
jpege_bits_align_byte(JpegeBits ctx)650 void jpege_bits_align_byte(JpegeBits ctx)
651 {
652 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
653
654 if (impl->bitCnt & 7)
655 jpege_bits_put(ctx, 0, 8 - (impl->bitCnt & 7));
656 }
657
jpege_bits_get_buf(JpegeBits ctx)658 RK_U8 *jpege_bits_get_buf(JpegeBits ctx)
659 {
660 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
661 return impl->buffer;
662 }
663
jpege_bits_get_bitpos(JpegeBits ctx)664 RK_S32 jpege_bits_get_bitpos(JpegeBits ctx)
665 {
666 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
667 return impl->bitCnt;
668 }
669
jpege_bits_get_bytepos(JpegeBits ctx)670 RK_S32 jpege_bits_get_bytepos(JpegeBits ctx)
671 {
672 JpegeBitsImpl *impl = (JpegeBitsImpl *)ctx;
673 return impl->byteCnt;
674 }
675
write_jpeg_comment_header(JpegeBits * bits,JpegeSyntax * syntax)676 static void write_jpeg_comment_header(JpegeBits *bits, JpegeSyntax *syntax)
677 {
678 RK_U32 i;
679 RK_U8 *data = syntax->comment_data;
680 RK_U32 length = syntax->comment_length;
681
682 /* COM */
683 jpege_bits_put(bits, COM, 16);
684
685 /* Lc */
686 jpege_bits_put(bits, 2 + length, 16);
687
688 for (i = 0; i < length; i++) {
689 /* COM data */
690 jpege_bits_put(bits, data[i], 8);
691 }
692 }
693
write_jpeg_dqt_header(JpegeBits * bits,const RK_U8 * qtables[2])694 static void write_jpeg_dqt_header(JpegeBits *bits, const RK_U8 *qtables[2])
695 {
696 RK_S32 i;
697 const RK_U8 *qtable = qtables[0];
698
699 /* DQT */
700 jpege_bits_put(bits, DQT, 16);
701 /* Lq */
702 jpege_bits_put(bits, 2 + 65, 16);
703 /* Pq */
704 jpege_bits_put(bits, 0, 4);
705 /* Tq */
706 jpege_bits_put(bits, 0, 4);
707
708 for (i = 0; i < 64; i++) {
709 /* Qk table 0 */
710 jpege_bits_put(bits, qtable[zigzag[i]], 8);
711 }
712
713 /* DQT */
714 jpege_bits_put(bits, DQT, 16);
715 /* Lq */
716 jpege_bits_put(bits, 2 + 65, 16);
717 /* Pq */
718 jpege_bits_put(bits, 0, 4);
719 /* Tq */
720 jpege_bits_put(bits, 1, 4);
721
722 qtable = qtables[1];
723
724 for (i = 0; i < 64; i++) {
725 /* Qk table 1 */
726 jpege_bits_put(bits, qtable[zigzag[i]], 8);
727 }
728 }
729
write_jpeg_SOFO_header(JpegeBits * bits,JpegeSyntax * syntax)730 static void write_jpeg_SOFO_header(JpegeBits *bits, JpegeSyntax *syntax)
731 {
732 RK_S32 i;
733 RK_U32 width = syntax->width;
734 RK_U32 height = syntax->height;
735 const JpegeColorInfo *info = &color_info[0];
736
737 /* SOF0 */
738 jpege_bits_put(bits, SOF0, 16);
739
740 /* Lf */
741 jpege_bits_put(bits, (8 + 3 * 3), 16);
742 /* P */
743 jpege_bits_put(bits, 8, 8);
744 /* Y */
745 jpege_bits_put(bits, height, 16);
746 /* X */
747 jpege_bits_put(bits, width, 16);
748 /* Nf */
749 jpege_bits_put(bits, 3, 8);
750
751 /* NOTE: only output 420 bits */
752 for (i = 0; i < 3; i++) {
753 /* Ci */
754 jpege_bits_put(bits, info->Ci[i], 8);
755 /* Hi */
756 jpege_bits_put(bits, info->Hi[i], 4);
757 /* Vi */
758 jpege_bits_put(bits, info->Vi[i], 4);
759 /* Tqi */
760 jpege_bits_put(bits, info->Tqi[i], 8);
761 }
762 }
763
write_jpeg_dht_header(JpegeBits * bits)764 static void write_jpeg_dht_header(JpegeBits *bits)
765 {
766 RK_S32 i;
767
768 /* DHT */
769 jpege_bits_put(bits, DHT, 16);
770
771 /* Huffman table for luminance DC components */
772 /* Lh */
773 jpege_bits_put(bits, 2 + ((17 * 1) + ((1 * 12))), 16);
774 /* TC */
775 jpege_bits_put(bits, 0, 4);
776 /* TH */
777 jpege_bits_put(bits, 0, 4);
778
779 for (i = 0; i < 16; i++) {
780 /* Dc_Li */
781 jpege_bits_put(bits, dc_li[i].val_y, 8);
782 }
783
784 for (i = 0; i < 12; i++) {
785 /* Dc_Vij */
786 jpege_bits_put(bits, dc_vij[i].val_y, 8);
787 }
788
789 /* DHT */
790 jpege_bits_put(bits, DHT, 16);
791
792 /* Huffman table for luminance AC components */
793 /* Lh */
794 jpege_bits_put(bits, 2 + ((17 * 1) + ((1 * 162))), 16);
795 /* TC */
796 jpege_bits_put(bits, 1, 4);
797 /* TH */
798 jpege_bits_put(bits, 0, 4);
799
800 for (i = 0; i < 16; i++) {
801 /* Ac_Li */
802 jpege_bits_put(bits, ac_li[i].val_y, 8);
803 }
804
805 for (i = 0; i < 162; i++) {
806 /* Ac_Vij */
807 jpege_bits_put(bits, ac_vij[i].val_y, 8);
808 }
809
810 /* Huffman table for chrominance DC components */
811 /* DHT */
812 jpege_bits_put(bits, DHT, 16);
813 /* Lh */
814 jpege_bits_put(bits, 2 + ((17 * 1) + ((1 * 12))), 16);
815 /* TC */
816 jpege_bits_put(bits, 0, 4);
817 /* TH */
818 jpege_bits_put(bits, 1, 4);
819
820 for (i = 0; i < 16; i++) {
821 /* Dc_Li */
822 jpege_bits_put(bits, dc_li[i].val_c, 8);
823 }
824
825 for (i = 0; i < 12; i++) {
826 /* Dc_Vij */
827 jpege_bits_put(bits, dc_vij[i].val_c, 8);
828 }
829
830 /* Huffman table for chrominance AC components */
831 /* DHT */
832 jpege_bits_put(bits, DHT, 16);
833 /* Lh */
834 jpege_bits_put(bits, 2 + ((17 * 1) + ((1 * 162))), 16);
835 /* TC */
836 jpege_bits_put(bits, 1, 4);
837 /* TH */
838 jpege_bits_put(bits, 1, 4);
839
840 for (i = 0; i < 16; i++) {
841 /* Ac_Li */
842 jpege_bits_put(bits, ac_li[i].val_c, 8);
843 }
844
845 for (i = 0; i < 162; i++) {
846 /* Ac_Vij */
847 jpege_bits_put(bits, ac_vij[i].val_c, 8);
848 }
849 }
850
write_jpeg_sos_header(JpegeBits * bits)851 static void write_jpeg_sos_header(JpegeBits *bits)
852 {
853 RK_U32 i;
854 RK_U32 Ns = 3;
855 RK_U32 Ls = (6 + (2 * Ns));
856
857 /* SOS */
858 jpege_bits_put(bits, SOS, 16);
859 /* Ls */
860 jpege_bits_put(bits, Ls, 16);
861 /* Ns */
862 jpege_bits_put(bits, Ns, 8);
863
864 for (i = 0; i < Ns; i++) {
865 /* Csj */
866 jpege_bits_put(bits, i + 1, 8);
867
868 if (i == 0) {
869 /* Tdj */
870 jpege_bits_put(bits, 0, 4);
871 /* Taj */
872 jpege_bits_put(bits, 0, 4);
873 } else {
874 /* Tdj */
875 jpege_bits_put(bits, 1, 4);
876 /* Taj */
877 jpege_bits_put(bits, 1, 4);
878 }
879 }
880
881 /* Ss */
882 jpege_bits_put(bits, 0, 8);
883 /* Se */
884 jpege_bits_put(bits, 63, 8);
885 /* Ah */
886 jpege_bits_put(bits, 0, 4);
887 /* Al */
888 jpege_bits_put(bits, 0, 4);
889 }
890
891
write_jpeg_RestartInterval(JpegeBits * bits,JpegeSyntax * syntax)892 void write_jpeg_RestartInterval(JpegeBits *bits, JpegeSyntax *syntax)
893 {
894 if (syntax->restart_ri) {
895 jpege_bits_put(bits, 0xFFDD, 16);
896 jpege_bits_put(bits, 4, 16);
897 jpege_bits_put(bits, syntax->restart_ri, 16);
898 }
899 }
900
write_jpeg_header(JpegeBits * bits,JpegeSyntax * syntax,const RK_U8 * qtables[2])901 MPP_RET write_jpeg_header(JpegeBits *bits, JpegeSyntax *syntax, const RK_U8 *qtables[2])
902 {
903 /* Com header */
904 if (syntax->comment_length)
905 write_jpeg_comment_header(bits, syntax);
906
907 /* Quant header */
908 if (!qtables[0]) {
909 if (syntax->qtable_y)
910 qtables[0] = syntax->qtable_y;
911 else
912 qtables[0] = qtable_y[syntax->quality];
913 }
914
915 if (!qtables[1]) {
916 if (syntax->qtable_c)
917 qtables[1] = syntax->qtable_c;
918 else
919 qtables[1] = qtable_c[syntax->quality];
920 }
921
922 write_jpeg_dqt_header(bits, qtables);
923
924 /* Frame header */
925 write_jpeg_SOFO_header(bits, syntax);
926
927 /* Do NOT have Restart interval */
928 write_jpeg_RestartInterval(bits, syntax);
929
930 /* Huffman header */
931 write_jpeg_dht_header(bits);
932
933 /* Scan header */
934 write_jpeg_sos_header(bits);
935
936 jpege_bits_align_byte(bits);
937 return MPP_OK;
938 }
939