pjpeg.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "pjpeg.h"
29 
30 #include "image_u8.h"
31 #include "image_u8x3.h"
32 
33 // https://www.w3.org/Graphics/JPEG/itu-t81.pdf
34 
35 void pjpeg_idct_2D_double(int32_t in[64], uint8_t *out, uint32_t outstride);
36 void pjpeg_idct_2D_u32(int32_t in[64], uint8_t *out, uint32_t outstride);
37 void pjpeg_idct_2D_nanojpeg(int32_t in[64], uint8_t *out, uint32_t outstride);
38 
40 {
41  uint8_t nbits; // how many bits should we actually consume?
42  uint8_t code; // what is the symbol that was encoded? (not actually a DCT coefficient; see encoding)
43 };
44 
46 {
47  int error;
48 
49  uint32_t width, height;
50  uint8_t *in;
51  uint32_t inlen;
52 
53  uint32_t flags;
54 
55  // to decode, we load the next 16 bits of input (generally more
56  // than we need). We then look up in our code book how many bits
57  // we have actually consumed. For example, if there was a code
58  // whose bit sequence was "0", the first 32768 entries would all
59  // be copies of {.bits=1, .value=XX}; no matter what the following
60  // 15 bits are, we would get the correct decode.
61  //
62  // Can be up to 8 tables; computed as (ACDC * 2 + htidx)
63  struct pjpeg_huffman_code huff_codes[4][65536];
64  int huff_codes_present[4];
65 
66  uint8_t qtab[4][64];
67 
70 
73  int reset_next; // What reset marker do we expect next? (add 0xd0)
74 
75  int debug;
76 };
77 
78 // from K.3.3.1 (page 158)
79 static uint8_t mjpeg_dht[] = { // header
80  0xFF,0xC4,0x01,0xA2,
81 
83  // luminance dc coefficients.
84  // DC table 0
85  0x00,
86  // code lengths
87  0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
88  // values
89  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
90 
92  // chrominance DC coefficents
93  // DC table 1
94  0x01,
95  // code lengths
96  0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
97  // values
98  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
99 
101  // luminance AC coefficients
102  // AC table 0
103  0x10,
104  // code lengths
105  0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,
106  // codes
107  0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,
108  0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,
109  0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,
110  0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
111  0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
112  0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,
113  0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,
114  0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,
115  0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
116  0xF8,0xF9,0xFA,
117 
119  // chrominance DC coefficients
120  // DC table 1
121  0x11,
122  // code lengths
123  0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,0x02,0x77,
124  // values
125  0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
126  0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,
127  0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,
128  0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
129  0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
130  0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
131  0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
132  0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,
133  0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
134  0xF9,0xFA
135 };
136 
137 static inline uint8_t max_u8(uint8_t a, uint8_t b)
138 {
139  return a > b ? a : b;
140 }
141 
142 // order of coefficients in each DC block
143 static const char ZZ[64] = { 0, 1, 8, 16, 9, 2, 3, 10,
144  17, 24, 32, 25, 18, 11, 4, 5,
145  12, 19, 26, 33, 40, 48, 41, 34,
146  27, 20, 13, 6, 7, 14, 21, 28,
147  35, 42, 49, 56, 57, 50, 43, 36,
148  29, 22, 15, 23, 30, 37, 44, 51,
149  58, 59, 52, 45, 38, 31, 39, 46,
150  53, 60, 61, 54, 47, 55, 62, 63 };
151 
152 
153 
155 {
156  uint8_t *in;
157  uint32_t inpos;
158  uint32_t inlen;
159 
160  uint32_t bits; // the low order bits contain the next nbits_avail bits.
161 
162  int nbits_avail; // how many bits in 'bits' (left aligned) are valid?
163 
164  int error;
165 };
166 
167 // ensure that at least 'nbits' of data is available in the bit decoder.
168 static inline void bd_ensure(struct bit_decoder *bd, int nbits)
169 {
170  while (bd->nbits_avail < nbits) {
171 
172  if (bd->inpos >= bd->inlen) {
173  printf("hallucinating 1s!\n");
174  // we hit end of stream hallucinate an infinite stream of 1s
175  bd->bits = (bd->bits << 8) | 0xff;
176  bd->nbits_avail += 8;
177  continue;
178  }
179 
180  uint8_t nextbyte = bd->in[bd->inpos];
181  bd->inpos++;
182 
183  if (nextbyte == 0xff && bd->inpos < bd->inlen && bd->in[bd->inpos] == 0x00) {
184  // a stuffed byte
185  nextbyte = 0xff;
186  bd->inpos++;
187  }
188 
189  // it's an ordinary byte
190  bd->bits = (bd->bits << 8) | nextbyte;
191  bd->nbits_avail += 8;
192  }
193 }
194 
195 static inline uint32_t bd_peek_bits(struct bit_decoder *bd, int nbits)
196 {
197  bd_ensure(bd, nbits);
198 
199  return (bd->bits >> (bd->nbits_avail - nbits)) & ((1 << nbits) - 1);
200 }
201 
202 static inline uint32_t bd_consume_bits(struct bit_decoder *bd, int nbits)
203 {
204  assert(nbits < 32);
205 
206  bd_ensure(bd, nbits);
207 
208  uint32_t v = (bd->bits >> (bd->nbits_avail - nbits)) & ((1 << nbits) - 1);
209 
210  bd->nbits_avail -= nbits;
211 
212  return v;
213 }
214 
215 // discard without regard for byte stuffing!
216 static inline void bd_discard_bytes(struct bit_decoder *bd, int nbytes)
217 {
218  assert(bd->nbits_avail == 0);
219  bd->inpos += nbytes;
220 }
221 
222 static inline int bd_has_more(struct bit_decoder *bd)
223 {
224  return bd->nbits_avail > 0 || bd->inpos < bd->inlen;
225 }
226 
227 // throw away up to 7 bits of data so that the next data returned
228 // began on a byte boundary.
229 static inline void bd_discard_to_byte_boundary(struct bit_decoder *bd)
230 {
231  bd->nbits_avail -= (bd->nbits_avail & 7);
232 }
233 
234 static inline uint32_t bd_get_offset(struct bit_decoder *bd)
235 {
236  return bd->inpos - bd->nbits_avail / 8;
237 }
238 
240 {
241  // XXX TODO Include sanity check that this is actually a JPG
242 
243  struct bit_decoder bd;
244  memset(&bd, 0, sizeof(struct bit_decoder));
245  bd.in = pjd->in;
246  bd.inpos = 0;
247  bd.inlen = pjd->inlen;
248 
249  int marker_sync_skipped = 0;
250  int marker_sync_skipped_from_offset = 0;
251 
252  while (bd_has_more(&bd)) {
253 
254  uint32_t marker_offset = bd_get_offset(&bd);
255 
256  // Look for the 0xff that signifies the beginning of a marker
258 
259  while (bd_consume_bits(&bd, 8) != 0xff) {
260  if (marker_sync_skipped == 0)
261  marker_sync_skipped_from_offset = marker_offset;
262  marker_sync_skipped++;
263  continue;
264  }
265 
266  if (marker_sync_skipped) {
267  printf("%08x: skipped %04x bytes\n", marker_sync_skipped_from_offset, marker_sync_skipped);
268  marker_sync_skipped = 0;
269  }
270 
271  uint8_t marker = bd_consume_bits(&bd, 8);
272 
273 // printf("marker %08x : %02x\n", marker_offset, marker);
274 
275  switch (marker) {
276 
277  case 0xd8: // start of image. Great, continue.
278  continue;
279 
280  // below are the markers that A) we don't care about
281  // that B) encode length as two bytes.
282  //
283  // Note: Other unknown fields should not be added since
284  // we should be able to skip over them by looking for
285  // the next marker byte.
286  case 0xe0: // JFIF header.
287  case 0xe1: // EXIF header (Yuck: Payload may contain 0xff 0xff!)
288  case 0xe2: // ICC Profile. (Yuck: payload may contain 0xff 0xff!)
289  case 0xe6: // some other common header
290  case 0xfe: // Comment
291  {
292  uint16_t length = bd_consume_bits(&bd, 16);
293  bd_discard_bytes(&bd, length - 2);
294  continue;
295  }
296 
297  case 0xdb: { // DQT Define Quantization Table
298  uint16_t length = bd_consume_bits(&bd, 16);
299 
300  if (((length-2) % 65) != 0)
301  return PJPEG_ERR_DQT;
302 
303  // can contain multiple DQTs
304  for (int offset = 0; offset < length - 2; offset += 65) {
305 
306  // pq: quant table element precision. 0=8bit, 1=16bit.
307  // tq: quant table destination id.
308  uint8_t pqtq = bd_consume_bits(&bd, 8);
309 
310  if ((pqtq & 0xf0) != 0 || (pqtq & 0x0f) >= 4)
311  return PJPEG_ERR_DQT;
312 
313  uint8_t id = pqtq & 3;
314 
315  for (int i = 0; i < 64; i++)
316  pjd->qtab[id][i] = bd_consume_bits(&bd, 8);
317  }
318 
319  break;
320  }
321 
322  case 0xc0: { // SOF, non-differential, huffman, baseline
323  uint16_t length = bd_consume_bits(&bd, 16);
324  (void) length;
325 
326  uint8_t p = bd_consume_bits(&bd, 8); // precision
327  if (p != 8)
328  return PJPEG_ERR_SOF;
329 
330  pjd->height = bd_consume_bits(&bd, 16);
331  pjd->width = bd_consume_bits(&bd, 16);
332 
333 // printf("%d x %d\n", pjd->height, pjd->width);
334 
335  int nf = bd_consume_bits(&bd, 8); // # image components
336 
337  if (nf < 1 || nf > 3)
338  return PJPEG_ERR_SOF;
339 
340  pjd->ncomponents = nf;
341  pjd->components = calloc(nf, sizeof(struct pjpeg_component));
342 
343  for (int i = 0; i < nf; i++) {
344  // comp. identifier
345  pjd->components[i].id = bd_consume_bits(&bd, 8);
346 
347  // horiz/vert sampling
348  pjd->components[i].hv = bd_consume_bits(&bd, 8);
349  pjd->components[i].scaley = pjd->components[i].hv & 0x0f;
350  pjd->components[i].scalex = pjd->components[i].hv >> 4;
351 
352  // which quant table?
353  pjd->components[i].tq = bd_consume_bits(&bd, 8);
354  }
355  break;
356  }
357 
358  case 0xc1: // SOF, non-differential, huffman, extended DCT
359  case 0xc2: // SOF, non-differential, huffman, progressive DCT
360  case 0xc3: // SOF, non-differential, huffman, lossless
361  case 0xc5: // SOF, differential, huffman, baseline DCT
362  case 0xc6: // SOF, differential, huffman, progressive
363  case 0xc7: // SOF, differential, huffman, lossless
364  case 0xc8: // reserved
365  case 0xc9: // SOF, non-differential, arithmetic, extended
366  case 0xca: // SOF, non-differential, arithmetic, progressive
367  case 0xcb: // SOF, non-differential, arithmetic, lossless
368  case 0xcd: // SOF, differential, arithmetic, sequential
369  case 0xce: // SOF, differential, arithmetic, progressive
370  case 0xcf: // SOF, differential, arithmetic, lossless
371  {
372  printf("pjepg.c: unsupported JPEG type %02x\n", marker);
373  return PJEPG_ERR_UNSUPPORTED;
374  }
375 
376  case 0xc4: { // DHT Define Huffman Tables
377  // [ED: the encoding of these tables is really quite
378  // clever!]
379  uint16_t length = bd_consume_bits(&bd, 16);
380  length = length - 2;
381 
382  while (length > 0) {
383  uint8_t TcTh = bd_consume_bits(&bd, 8);
384  length--;
385  uint8_t Tc = (TcTh >> 4);
386  int Th = TcTh & 0x0f; // which index are we using?
387 
388  if (Tc >= 2 || Th >= 2)
389  // Tc must be either AC=1 or DC=0.
390  // Th must be less than 2
391  return PJPEG_ERR_DHT;
392 
393  int htidx = Tc*2 + Th;
394 
395  uint8_t L[17]; // how many symbols of each bit length?
396  L[0] = 0; // no 0 bit codes :)
397  for (int nbits = 1; nbits <= 16; nbits++) {
398  L[nbits] = bd_consume_bits(&bd, 8);
399  length -= L[nbits];
400  }
401  length -= 16;
402 
403  uint32_t code_pos = 0;
404 
405  for (int nbits = 1; nbits <= 16; nbits++) {
406  int nvalues = L[nbits];
407 
408  // how many entries will we fill?
409  // (a 1 bit code will fill 32768, a 2 bit code 16384, ...)
410  uint32_t ncodes = (1 << (16 - nbits));
411 
412  // consume the values...
413  for (int vi = 0; vi < nvalues; vi++) {
414  uint8_t code = bd_consume_bits(&bd, 8);
415 
416  if (code_pos + ncodes > 0xffff)
417  return PJPEG_ERR_DHT;
418 
419  for (int ci = 0; ci < ncodes; ci++) {
420  pjd->huff_codes[htidx][code_pos].nbits = nbits;
421  pjd->huff_codes[htidx][code_pos].code = code;
422  code_pos++;
423  }
424  }
425  }
426  pjd->huff_codes_present[htidx] = 1;
427  }
428  break;
429  }
430 
431  // a sequentially-encoded JPG has one SOS segment. A
432  // progressive JPG will have multiple SOS segments.
433  case 0xda: { // Start Of Scan (SOS)
434 
435  // Note that this marker frame (and its encoded
436  // length) does NOT include the bitstream that
437  // follows.
438 
439  uint16_t length = bd_consume_bits(&bd, 16);
440  (void) length;
441 
442  // number of components in this scan
443  uint8_t ns = bd_consume_bits(&bd, 8);
444 
445  // for each component, what is the index into our pjd->components[] array?
446  uint8_t comp_idx[ns];
447  memset(comp_idx, 0, ns*sizeof(uint8_t));
448 
449  for (int i = 0; i < ns; i++) {
450  // component name
451  uint8_t cs = bd_consume_bits(&bd, 8);
452 
453  int found = 0;
454  for (int j = 0; j < pjd->ncomponents; j++) {
455 
456  if (cs == pjd->components[j].id) {
457  // which huff tables will we use for
458  // DC (high 4 bits) and AC (low 4 bits)
459  pjd->components[j].tda = bd_consume_bits(&bd, 8);
460  comp_idx[i] = j;
461  found = 1;
462  break;
463  }
464  }
465 
466  if (!found)
467  return PJPEG_ERR_SOS;
468  }
469 
470  // start of spectral selection. baseline == 0
471  uint8_t ss = bd_consume_bits(&bd, 8);
472 
473  // end of spectral selection. baseline == 0x3f
474  uint8_t se = bd_consume_bits(&bd, 8);
475 
476  // successive approximation bits. baseline == 0
477  uint8_t Ahl = bd_consume_bits(&bd, 8);
478 
479  if (ss != 0 || se != 0x3f || Ahl != 0x00)
480  return PJPEG_ERR_SOS;
481 
482  // compute the dimensions of each MCU in pixels
483  int maxmcux = 0, maxmcuy = 0;
484  for (int i = 0; i < ns; i++) {
485  struct pjpeg_component *comp = &pjd->components[comp_idx[i]];
486 
487  maxmcux = max_u8(maxmcux, comp->scalex * 8);
488  maxmcuy = max_u8(maxmcuy, comp->scaley * 8);
489  }
490 
491  // how many MCU blocks are required to encode the whole image?
492  int mcus_x = (pjd->width + maxmcux - 1) / maxmcux;
493  int mcus_y = (pjd->height + maxmcuy - 1) / maxmcuy;
494 
495  if (0)
496  printf("Image has %d x %d MCU blocks, each %d x %d pixels\n",
497  mcus_x, mcus_y, maxmcux, maxmcuy);
498 
499  // allocate output storage
500  for (int i = 0; i < ns; i++) {
501  struct pjpeg_component *comp = &pjd->components[comp_idx[i]];
502  comp->width = mcus_x * comp->scalex * 8;
503  comp->height = mcus_y * comp->scaley * 8;
504  comp->stride = comp->width;
505 
506  int alignment = 32;
507  if ((comp->stride % alignment) != 0)
508  comp->stride += alignment - (comp->stride % alignment);
509 
510  comp->data = calloc(comp->height * comp->stride, 1);
511  }
512 
513 
514  // each component has its own DC prediction
515  int32_t dcpred[ns];
516  memset(dcpred, 0, sizeof(dcpred));
517 
518  pjd->reset_count = 0;
519 
520  for (int mcu_y = 0; mcu_y < mcus_y; mcu_y++) {
521  for (int mcu_x = 0; mcu_x < mcus_x; mcu_x++) {
522 
523  // the next two bytes in the input stream
524  // should be 0xff 0xdN, where N is the next
525  // reset counter.
526  //
527  // Our bit decoder may have already shifted
528  // these into the buffer. Consequently, we
529  // want to use our bit decoding functions to
530  // check for the marker. But we must first
531  // discard any fractional bits left.
532  if (pjd->reset_interval > 0 && pjd->reset_count == pjd->reset_interval) {
533 
534  // RST markers are byte-aligned, so force
535  // the bit-decoder to the next byte
536  // boundary.
538 
539  while (1) {
540  int32_t value = bd_consume_bits(&bd, 8);
541  if (bd.inpos > bd.inlen)
542  return PJPEG_ERR_EOF;
543  if (value == 0xff)
544  break;
545  printf("RST SYNC\n");
546  }
547 
548  int32_t marker = bd_consume_bits(&bd, 8);
549 
550 // printf("%04x: RESET? %02x\n", *bd.inpos, marker);
551  if (marker != (0xd0 + pjd->reset_next))
552  return PJPEG_ERR_RESET;
553 
554  pjd->reset_count = 0;
555  pjd->reset_next = (pjd->reset_next + 1) & 0x7;
556 
557  memset(dcpred, 0, sizeof(dcpred));
558  }
559 
560  for (int nsidx = 0; nsidx < ns; nsidx++) {
561 
562  struct pjpeg_component *comp = &pjd->components[comp_idx[nsidx]];
563 
564  int32_t block[64];
565 
566  int qtabidx = comp->tq; // which quant table?
567 
568  for (int sby = 0; sby < comp->scaley; sby++) {
569  for (int sbx = 0; sbx < comp->scalex; sbx++) {
570  // decode block for component nsidx
571  memset(block, 0, sizeof(block));
572 
573  int dc_huff_table_idx = comp->tda >> 4;
574  int ac_huff_table_idx = 2 + (comp->tda & 0x0f);
575 
576  if (!pjd->huff_codes_present[dc_huff_table_idx] ||
577  !pjd->huff_codes_present[ac_huff_table_idx])
578  return PJPEG_ERR_MISSING_DHT; // probably an MJPEG.
579 
580 
581  if (1) {
582  // do DC coefficient
583  uint32_t next16 = bd_peek_bits(&bd, 16);
584  struct pjpeg_huffman_code *huff_code = &pjd->huff_codes[dc_huff_table_idx][next16];
585  bd_consume_bits(&bd, huff_code->nbits);
586 
587  int ssss = huff_code->code & 0x0f; // ssss == number of additional bits to read
588  int32_t value = bd_consume_bits(&bd, ssss);
589 
590  // if high bit is clear, it's negative
591  if ((value & (1 << (ssss-1))) == 0)
592  value += ((-1) << ssss) + 1;
593 
594  dcpred[nsidx] += value;
595  block[0] = dcpred[nsidx] * pjd->qtab[qtabidx][0];
596  }
597 
598  if (1) {
599  // do AC coefficients
600  for (int coeff = 1; coeff < 64; coeff++) {
601 
602  uint32_t next16 = bd_peek_bits(&bd, 16);
603 
604  struct pjpeg_huffman_code *huff_code = &pjd->huff_codes[ac_huff_table_idx][next16];
605  bd_consume_bits(&bd, huff_code->nbits);
606 
607  if (huff_code->code == 0) {
608  break; // EOB
609  }
610 
611  int rrrr = huff_code->code >> 4; // run length of zeros
612  int ssss = huff_code->code & 0x0f;
613 
614  int32_t value = bd_consume_bits(&bd, ssss);
615 
616  // if high bit is clear, it's negative
617  if ((value & (1 << (ssss-1))) == 0)
618  value += ((-1) << ssss) + 1;
619 
620  coeff += rrrr;
621 
622  block[(int) ZZ[coeff]] = value * pjd->qtab[qtabidx][coeff];
623  }
624  }
625 
626  // do IDCT
627 
628  // output block's upper-left
629  // coordinate (in pixels) is
630  // (comp_x, comp_y).
631  uint32_t comp_x = (mcu_x * comp->scalex + sbx) * 8;
632  uint32_t comp_y = (mcu_y * comp->scaley + sby) * 8;
633  uint32_t dataidx = comp_y * comp->stride + comp_x;
634 
635 // pjpeg_idct_2D_u32(block, &comp->data[dataidx], comp->stride);
636  pjpeg_idct_2D_nanojpeg(block, &comp->data[dataidx], comp->stride);
637  }
638  }
639  }
640 
641  pjd->reset_count++;
642 // printf("%04x: reset count %d / %d\n", pjd->inpos, pjd->reset_count, pjd->reset_interval);
643 
644  }
645  }
646 
647  break;
648  }
649 
650  case 0xd9: { // EOI End of Image
651  goto got_end_of_image;
652  }
653 
654  case 0xdd: { // Define Restart Interval
655  uint16_t length = bd_consume_bits(&bd, 16);
656  if (length != 4)
657  return PJPEG_ERR_DRI;
658 
659  // reset interval measured in the number of MCUs
660  pjd->reset_interval = bd_consume_bits(&bd, 16);
661 
662  break;
663  }
664 
665  default: {
666  printf("pjepg: Unknown marker %02x at offset %04x\n", marker, marker_offset);
667 
668  // try to skip it.
669  uint16_t length = bd_consume_bits(&bd, 16);
670  bd_discard_bytes(&bd, length - 2);
671  continue;
672  }
673  } // switch (marker)
674  } // while inpos < inlen
675 
676  got_end_of_image:
677 
678  return PJPEG_OKAY;
679 }
680 
682 {
683  if (!pj)
684  return;
685 
686  for (int i = 0; i < pj->ncomponents; i++)
687  free(pj->components[i].data);
688  free(pj->components);
689 
690  free(pj);
691 }
692 
693 
694 // just grab the first component.
696 {
697  assert(pj->ncomponents > 0);
698 
699  pjpeg_component_t *comp = &pj->components[0];
700 
701  assert(comp->width >= pj->width && comp->height >= pj->height);
702 
703  image_u8_t *im = image_u8_create(pj->width, pj->height);
704  for (int y = 0; y < im->height; y++)
705  memcpy(&im->buf[y*im->stride], &comp->data[y*comp->stride], pj->width);
706 
707  return im;
708 }
709 
710 static inline uint8_t clampd(double v)
711 {
712  if (v < 0)
713  return 0;
714  if (v > 255)
715  return 255;
716 
717  return (uint8_t) v;
718 }
719 
720 static inline uint8_t clamp_u8(int32_t v)
721 {
722  if (v < 0)
723  return 0;
724  if (v > 255)
725  return 255;
726  return v;
727 }
728 
729 // color conversion formulas taken from JFIF spec v 1.02
731 {
732  assert(pj->ncomponents == 3);
733 
734  pjpeg_component_t *Y = &pj->components[0];
735  pjpeg_component_t *Cb = &pj->components[1];
736  pjpeg_component_t *Cr = &pj->components[2];
737 
738  int Cb_factor_y = Y->height / Cb->height;
739  int Cb_factor_x = Y->width / Cb->width;
740 
741  int Cr_factor_y = Y->height / Cr->height;
742  int Cr_factor_x = Y->width / Cr->width;
743 
744  image_u8x3_t *im = image_u8x3_create(pj->width, pj->height);
745 
746  if (Cr_factor_y == 1 && Cr_factor_x == 1 && Cb_factor_y == 1 && Cb_factor_x == 1) {
747 
748  for (int y = 0; y < pj->height; y++) {
749  for (int x = 0; x < pj->width; x++) {
750  int32_t y_val = Y->data[y*Y->stride + x] * 65536;
751  int32_t cb_val = Cb->data[y*Cb->stride + x] - 128;
752  int32_t cr_val = Cr->data[y*Cr->stride + x] - 128;
753 
754  int32_t r_val = y_val + 91881 * cr_val;
755  int32_t g_val = y_val + -22554 * cb_val - 46802 * cr_val;
756  int32_t b_val = y_val + 116130 * cb_val;
757 
758  im->buf[y*im->stride + 3*x + 0 ] = clamp_u8(r_val >> 16);
759  im->buf[y*im->stride + 3*x + 1 ] = clamp_u8(g_val >> 16);
760  im->buf[y*im->stride + 3*x + 2 ] = clamp_u8(b_val >> 16);
761  }
762  }
763  } else if (Cb_factor_y == Cr_factor_y && Cb_factor_x == Cr_factor_x) {
764  for (int by = 0; by < pj->height / Cb_factor_y; by++) {
765  for (int bx = 0; bx < pj->width / Cb_factor_x; bx++) {
766 
767  int32_t cb_val = Cb->data[by*Cb->stride + bx] - 128;
768  int32_t cr_val = Cr->data[by*Cr->stride + bx] - 128;
769 
770  int32_t r0 = 91881 * cr_val;
771  int32_t g0 = -22554 * cb_val - 46802 * cr_val;
772  int32_t b0 = 116130 * cb_val;
773 
774  for (int dy = 0; dy < Cb_factor_y; dy++) {
775  int y = by*Cb_factor_y + dy;
776 
777  for (int dx = 0; dx < Cb_factor_x; dx++) {
778  int x = bx*Cb_factor_x + dx;
779 
780  int32_t y_val = Y->data[y*Y->stride + x] * 65536;
781 
782  int32_t r_val = r0 + y_val;
783  int32_t g_val = g0 + y_val;
784  int32_t b_val = b0 + y_val;
785 
786  im->buf[y*im->stride + 3*x + 0 ] = clamp_u8(r_val >> 16);
787  im->buf[y*im->stride + 3*x + 1 ] = clamp_u8(g_val >> 16);
788  im->buf[y*im->stride + 3*x + 2 ] = clamp_u8(b_val >> 16);
789  }
790  }
791  }
792  }
793  } else {
794 
795  for (int y = 0; y < pj->height; y++) {
796  for (int x = 0; x < pj->width; x++) {
797  int32_t y_val = Y->data[y*Y->stride + x];
798  int32_t cb_val = Cb->data[(y / Cb_factor_y)*Cb->stride + (x / Cb_factor_x)] - 128;
799  int32_t cr_val = Cr->data[(y / Cr_factor_y)*Cr->stride + (x / Cr_factor_x)] - 128;
800 
801  uint8_t r_val = clampd(y_val + 1.402 * cr_val);
802  uint8_t g_val = clampd(y_val - 0.34414 * cb_val - 0.71414 * cr_val);
803  uint8_t b_val = clampd(y_val + 1.772 * cb_val);
804 
805  im->buf[y*im->stride + 3*x + 0 ] = r_val;
806  im->buf[y*im->stride + 3*x + 1 ] = g_val;
807  im->buf[y*im->stride + 3*x + 2 ] = b_val;
808  }
809  }
810  }
811 
812  return im;
813 }
814 
816 // returns NULL if file loading fails.
817 pjpeg_t *pjpeg_create_from_file(const char *path, uint32_t flags, int *error)
818 {
819  FILE *f = fopen(path, "r");
820  if (f == NULL)
821  return NULL;
822 
823  fseek(f, 0, SEEK_END);
824  long buflen = ftell(f);
825 
826  uint8_t *buf = malloc(buflen);
827  fseek(f, 0, SEEK_SET);
828  int res = fread(buf, 1, buflen, f);
829  fclose(f);
830  if (res != buflen) {
831  free(buf);
832  if (error)
833  *error = PJPEG_ERR_FILE;
834  return NULL;
835  }
836 
837  pjpeg_t *pj = pjpeg_create_from_buffer(buf, buflen, flags, error);
838 
839  free(buf);
840  return pj;
841 }
842 
843 pjpeg_t *pjpeg_create_from_buffer(uint8_t *buf, int buflen, uint32_t flags, int *error)
844 {
845  struct pjpeg_decode_state pjd;
846  memset(&pjd, 0, sizeof(pjd));
847 
848  if (flags & PJPEG_MJPEG) {
849  pjd.in = mjpeg_dht;
850  pjd.inlen = sizeof(mjpeg_dht);
851  int result = pjpeg_decode_buffer(&pjd);
852  assert(result == 0);
853  }
854 
855  pjd.in = buf;
856  pjd.inlen = buflen;
857  pjd.flags = flags;
858 
859  int result = pjpeg_decode_buffer(&pjd);
860  if (error)
861  *error = result;
862 
863  if (result) {
864  for (int i = 0; i < pjd.ncomponents; i++)
865  free(pjd.components[i].data);
866  free(pjd.components);
867 
868  return NULL;
869  }
870 
871  pjpeg_t *pj = calloc(1, sizeof(pjpeg_t));
872 
873  pj->width = pjd.width;
874  pj->height = pjd.height;
875  pj->ncomponents = pjd.ncomponents;
876  pj->components = pjd.components;
877 
878  return pj;
879 }
uint32_t stride
Definition: pjpeg.h:41
int error
Definition: pjpeg.c:164
uint8_t code
Definition: pjpeg.c:42
int nbits_avail
Definition: pjpeg.c:162
uint8_t id
Definition: pjpeg.h:49
uint32_t width
Definition: pjpeg.h:64
uint8_t scaley
Definition: pjpeg.h:51
uint32_t width
Definition: pjpeg.h:37
uint8_t qtab[4][64]
Definition: pjpeg.c:66
void pjpeg_idct_2D_double(int32_t in[64], uint8_t *out, uint32_t outstride)
Definition: pjpeg-idct.c:235
static const char ZZ[64]
Definition: pjpeg.c:143
int huff_codes_present[4]
Definition: pjpeg.c:64
void pjpeg_destroy(pjpeg_t *pj)
Definition: pjpeg.c:681
uint8_t * in
Definition: pjpeg.c:156
image_u8_t * image_u8_create(unsigned int width, unsigned int height)
Definition: image_u8.c:59
pjpeg_component_t * components
Definition: pjpeg.h:67
Definition: pjpeg.h:59
uint8_t nbits
Definition: pjpeg.c:41
static uint32_t bd_consume_bits(struct bit_decoder *bd, int nbits)
Definition: pjpeg.c:202
const int32_t height
Definition: image_types.h:14
static void bd_discard_to_byte_boundary(struct bit_decoder *bd)
Definition: pjpeg.c:229
pjpeg_t * pjpeg_create_from_file(const char *path, uint32_t flags, int *error)
Definition: pjpeg.c:817
static uint32_t bd_get_offset(struct bit_decoder *bd)
Definition: pjpeg.c:234
uint32_t inpos
Definition: pjpeg.c:157
static uint8_t max_u8(uint8_t a, uint8_t b)
Definition: pjpeg.c:137
uint8_t * buf
Definition: image_types.h:27
struct pjpeg_huffman_code huff_codes[4][65536]
Definition: pjpeg.c:63
const int32_t stride
Definition: image_types.h:15
uint32_t inlen
Definition: pjpeg.c:158
void pjpeg_idct_2D_nanojpeg(int32_t in[64], uint8_t *out, uint32_t outstride)
Definition: pjpeg-idct.c:374
static uint8_t clamp_u8(int32_t v)
Definition: pjpeg.c:720
image_u8x3_t * pjpeg_to_u8x3_baseline(pjpeg_t *pj)
Definition: pjpeg.c:730
uint32_t height
Definition: pjpeg.h:37
uint32_t inlen
Definition: pjpeg.c:51
static void bd_discard_bytes(struct bit_decoder *bd, int nbytes)
Definition: pjpeg.c:216
const int32_t stride
Definition: image_types.h:25
static int bd_has_more(struct bit_decoder *bd)
Definition: pjpeg.c:222
static void bd_ensure(struct bit_decoder *bd, int nbits)
Definition: pjpeg.c:168
int reset_interval
Definition: pjpeg.c:71
uint32_t height
Definition: pjpeg.h:64
uint8_t * buf
Definition: image_types.h:17
image_u8x3_t * image_u8x3_create(unsigned int width, unsigned int height)
Definition: image_u8x3.c:37
pjpeg_t * pjpeg_create_from_buffer(uint8_t *buf, int buflen, uint32_t flags, int *error)
Definition: pjpeg.c:843
uint8_t hv
Definition: pjpeg.h:50
pjpeg_component_t * components
Definition: pjpeg.c:69
static uint8_t clampd(double v)
Definition: pjpeg.c:710
uint8_t * data
Definition: pjpeg.h:44
static uint8_t mjpeg_dht[]
Definition: pjpeg.c:79
uint8_t tda
Definition: pjpeg.h:55
int ncomponents
Definition: pjpeg.h:66
image_u8_t * pjpeg_to_u8_baseline(pjpeg_t *pj)
Definition: pjpeg.c:695
uint32_t bits
Definition: pjpeg.c:160
static int pjpeg_decode_buffer(struct pjpeg_decode_state *pjd)
Definition: pjpeg.c:239
uint8_t scalex
Definition: pjpeg.h:51
uint8_t tq
Definition: pjpeg.h:52
static uint32_t bd_peek_bits(struct bit_decoder *bd, int nbits)
Definition: pjpeg.c:195
uint8_t * in
Definition: pjpeg.c:50
uint32_t height
Definition: pjpeg.c:49
uint32_t flags
Definition: pjpeg.c:53
uint32_t width
Definition: pjpeg.c:49
void pjpeg_idct_2D_u32(int32_t in[64], uint8_t *out, uint32_t outstride)
Definition: pjpeg-idct.c:173


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32