Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/media/libjpeg/jcphuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcphuff.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1995-1997, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2015, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains Huffman entropy encoding routines for progressive JPEG.
12
 *
13
 * We do not support output suspension in this module, since the library
14
 * currently does not allow multiple-scan files to be written with output
15
 * suspension.
16
 */
17
18
#define JPEG_INTERNALS
19
#include "jinclude.h"
20
#include "jpeglib.h"
21
#include "jchuff.h"             /* Declarations shared with jchuff.c */
22
23
#ifdef C_PROGRESSIVE_SUPPORTED
24
25
/* Expanded entropy encoder object for progressive Huffman encoding. */
26
27
typedef struct {
28
  struct jpeg_entropy_encoder pub; /* public fields */
29
30
  /* Mode flag: TRUE for optimization, FALSE for actual data output */
31
  boolean gather_statistics;
32
33
  /* Bit-level coding status.
34
   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
35
   */
36
  JOCTET *next_output_byte;     /* => next byte to write in buffer */
37
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
38
  size_t put_buffer;            /* current bit-accumulation buffer */
39
  int put_bits;                 /* # of bits now in it */
40
  j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
41
42
  /* Coding status for DC components */
43
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
44
45
  /* Coding status for AC components */
46
  int ac_tbl_no;                /* the table number of the single component */
47
  unsigned int EOBRUN;          /* run length of EOBs */
48
  unsigned int BE;              /* # of buffered correction bits before MCU */
49
  char *bit_buffer;             /* buffer for correction bits (1 per char) */
50
  /* packing correction bits tightly would save some space but cost time... */
51
52
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
53
  int next_restart_num;         /* next restart number to write (0-7) */
54
55
  /* Pointers to derived tables (these workspaces have image lifespan).
56
   * Since any one scan codes only DC or only AC, we only need one set
57
   * of tables, not one for DC and one for AC.
58
   */
59
  c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
60
61
  /* Statistics tables for optimization; again, one set is enough */
62
  long *count_ptrs[NUM_HUFF_TBLS];
63
} phuff_entropy_encoder;
64
65
typedef phuff_entropy_encoder *phuff_entropy_ptr;
66
67
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
68
 * buffer can hold.  Larger sizes may slightly improve compression, but
69
 * 1000 is already well into the realm of overkill.
70
 * The minimum safe size is 64 bits.
71
 */
72
73
0
#define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
74
75
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
76
 * We assume that int right shift is unsigned if JLONG right shift is,
77
 * which should be safe.
78
 */
79
80
#ifdef RIGHT_SHIFT_IS_UNSIGNED
81
#define ISHIFT_TEMPS    int ishift_temp;
82
#define IRIGHT_SHIFT(x,shft)  \
83
        ((ishift_temp = (x)) < 0 ? \
84
         (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
85
         (ishift_temp >> (shft)))
86
#else
87
#define ISHIFT_TEMPS
88
0
#define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
89
#endif
90
91
/* Forward declarations */
92
METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo,
93
                                        JBLOCKROW *MCU_data);
94
METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo,
95
                                        JBLOCKROW *MCU_data);
96
METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo,
97
                                         JBLOCKROW *MCU_data);
98
METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo,
99
                                         JBLOCKROW *MCU_data);
100
METHODDEF(void) finish_pass_phuff (j_compress_ptr cinfo);
101
METHODDEF(void) finish_pass_gather_phuff (j_compress_ptr cinfo);
102
103
104
/*
105
 * Initialize for a Huffman-compressed scan using progressive JPEG.
106
 */
107
108
METHODDEF(void)
109
start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
110
0
{
111
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
112
0
  boolean is_DC_band;
113
0
  int ci, tbl;
114
0
  jpeg_component_info *compptr;
115
0
116
0
  entropy->cinfo = cinfo;
117
0
  entropy->gather_statistics = gather_statistics;
118
0
119
0
  is_DC_band = (cinfo->Ss == 0);
120
0
121
0
  /* We assume jcmaster.c already validated the scan parameters. */
122
0
123
0
  /* Select execution routines */
124
0
  if (cinfo->Ah == 0) {
125
0
    if (is_DC_band)
126
0
      entropy->pub.encode_mcu = encode_mcu_DC_first;
127
0
    else
128
0
      entropy->pub.encode_mcu = encode_mcu_AC_first;
129
0
  } else {
130
0
    if (is_DC_band)
131
0
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
132
0
    else {
133
0
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
134
0
      /* AC refinement needs a correction bit buffer */
135
0
      if (entropy->bit_buffer == NULL)
136
0
        entropy->bit_buffer = (char *)
137
0
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
138
0
                                      MAX_CORR_BITS * sizeof(char));
139
0
    }
140
0
  }
141
0
  if (gather_statistics)
142
0
    entropy->pub.finish_pass = finish_pass_gather_phuff;
143
0
  else
144
0
    entropy->pub.finish_pass = finish_pass_phuff;
145
0
146
0
  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
147
0
   * for AC coefficients.
148
0
   */
149
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
150
0
    compptr = cinfo->cur_comp_info[ci];
151
0
    /* Initialize DC predictions to 0 */
152
0
    entropy->last_dc_val[ci] = 0;
153
0
    /* Get table index */
154
0
    if (is_DC_band) {
155
0
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
156
0
        continue;
157
0
      tbl = compptr->dc_tbl_no;
158
0
    } else {
159
0
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
160
0
    }
161
0
    if (gather_statistics) {
162
0
      /* Check for invalid table index */
163
0
      /* (make_c_derived_tbl does this in the other path) */
164
0
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
165
0
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
166
0
      /* Allocate and zero the statistics tables */
167
0
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
168
0
      if (entropy->count_ptrs[tbl] == NULL)
169
0
        entropy->count_ptrs[tbl] = (long *)
170
0
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
171
0
                                      257 * sizeof(long));
172
0
      MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long));
173
0
    } else {
174
0
      /* Compute derived values for Huffman table */
175
0
      /* We may do this more than once for a table, but it's not expensive */
176
0
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
177
0
                              & entropy->derived_tbls[tbl]);
178
0
    }
179
0
  }
180
0
181
0
  /* Initialize AC stuff */
182
0
  entropy->EOBRUN = 0;
183
0
  entropy->BE = 0;
184
0
185
0
  /* Initialize bit buffer to empty */
186
0
  entropy->put_buffer = 0;
187
0
  entropy->put_bits = 0;
188
0
189
0
  /* Initialize restart stuff */
190
0
  entropy->restarts_to_go = cinfo->restart_interval;
191
0
  entropy->next_restart_num = 0;
192
0
}
193
194
195
/* Outputting bytes to the file.
196
 * NB: these must be called only when actually outputting,
197
 * that is, entropy->gather_statistics == FALSE.
198
 */
199
200
/* Emit a byte */
201
#define emit_byte(entropy,val)  \
202
0
        { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
203
0
          if (--(entropy)->free_in_buffer == 0)  \
204
0
            dump_buffer(entropy); }
205
206
207
LOCAL(void)
208
dump_buffer (phuff_entropy_ptr entropy)
209
/* Empty the output buffer; we do not support suspension in this module. */
210
0
{
211
0
  struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
212
0
213
0
  if (! (*dest->empty_output_buffer) (entropy->cinfo))
214
0
    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
215
0
  /* After a successful buffer dump, must reset buffer pointers */
216
0
  entropy->next_output_byte = dest->next_output_byte;
217
0
  entropy->free_in_buffer = dest->free_in_buffer;
218
0
}
219
220
221
/* Outputting bits to the file */
222
223
/* Only the right 24 bits of put_buffer are used; the valid bits are
224
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
225
 * in one call, and we never retain more than 7 bits in put_buffer
226
 * between calls, so 24 bits are sufficient.
227
 */
228
229
LOCAL(void)
230
emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
231
/* Emit some bits, unless we are in gather mode */
232
0
{
233
0
  /* This routine is heavily used, so it's worth coding tightly. */
234
0
  register size_t put_buffer = (size_t) code;
235
0
  register int put_bits = entropy->put_bits;
236
0
237
0
  /* if size is 0, caller used an invalid Huffman table entry */
238
0
  if (size == 0)
239
0
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
240
0
241
0
  if (entropy->gather_statistics)
242
0
    return;                     /* do nothing if we're only getting stats */
243
0
244
0
  put_buffer &= (((size_t) 1)<<size) - 1; /* mask off any extra bits in code */
245
0
246
0
  put_bits += size;             /* new number of bits in buffer */
247
0
248
0
  put_buffer <<= 24 - put_bits; /* align incoming bits */
249
0
250
0
  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
251
0
252
0
  while (put_bits >= 8) {
253
0
    int c = (int) ((put_buffer >> 16) & 0xFF);
254
0
255
0
    emit_byte(entropy, c);
256
0
    if (c == 0xFF) {            /* need to stuff a zero byte? */
257
0
      emit_byte(entropy, 0);
258
0
    }
259
0
    put_buffer <<= 8;
260
0
    put_bits -= 8;
261
0
  }
262
0
263
0
  entropy->put_buffer = put_buffer; /* update variables */
264
0
  entropy->put_bits = put_bits;
265
0
}
266
267
268
LOCAL(void)
269
flush_bits (phuff_entropy_ptr entropy)
270
0
{
271
0
  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
272
0
  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
273
0
  entropy->put_bits = 0;
274
0
}
275
276
277
/*
278
 * Emit (or just count) a Huffman symbol.
279
 */
280
281
LOCAL(void)
282
emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
283
0
{
284
0
  if (entropy->gather_statistics)
285
0
    entropy->count_ptrs[tbl_no][symbol]++;
286
0
  else {
287
0
    c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
288
0
    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
289
0
  }
290
0
}
291
292
293
/*
294
 * Emit bits from a correction bit buffer.
295
 */
296
297
LOCAL(void)
298
emit_buffered_bits (phuff_entropy_ptr entropy, char *bufstart,
299
                    unsigned int nbits)
300
0
{
301
0
  if (entropy->gather_statistics)
302
0
    return;                     /* no real work */
303
0
304
0
  while (nbits > 0) {
305
0
    emit_bits(entropy, (unsigned int) (*bufstart), 1);
306
0
    bufstart++;
307
0
    nbits--;
308
0
  }
309
0
}
310
311
312
/*
313
 * Emit any pending EOBRUN symbol.
314
 */
315
316
LOCAL(void)
317
emit_eobrun (phuff_entropy_ptr entropy)
318
0
{
319
0
  register int temp, nbits;
320
0
321
0
  if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
322
0
    temp = entropy->EOBRUN;
323
0
    nbits = 0;
324
0
    while ((temp >>= 1))
325
0
      nbits++;
326
0
    /* safety check: shouldn't happen given limited correction-bit buffer */
327
0
    if (nbits > 14)
328
0
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
329
0
330
0
    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
331
0
    if (nbits)
332
0
      emit_bits(entropy, entropy->EOBRUN, nbits);
333
0
334
0
    entropy->EOBRUN = 0;
335
0
336
0
    /* Emit any buffered correction bits */
337
0
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
338
0
    entropy->BE = 0;
339
0
  }
340
0
}
341
342
343
/*
344
 * Emit a restart marker & resynchronize predictions.
345
 */
346
347
LOCAL(void)
348
emit_restart (phuff_entropy_ptr entropy, int restart_num)
349
0
{
350
0
  int ci;
351
0
352
0
  emit_eobrun(entropy);
353
0
354
0
  if (! entropy->gather_statistics) {
355
0
    flush_bits(entropy);
356
0
    emit_byte(entropy, 0xFF);
357
0
    emit_byte(entropy, JPEG_RST0 + restart_num);
358
0
  }
359
0
360
0
  if (entropy->cinfo->Ss == 0) {
361
0
    /* Re-initialize DC predictions to 0 */
362
0
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
363
0
      entropy->last_dc_val[ci] = 0;
364
0
  } else {
365
0
    /* Re-initialize all AC-related fields to 0 */
366
0
    entropy->EOBRUN = 0;
367
0
    entropy->BE = 0;
368
0
  }
369
0
}
370
371
372
/*
373
 * MCU encoding for DC initial scan (either spectral selection,
374
 * or first pass of successive approximation).
375
 */
376
377
METHODDEF(boolean)
378
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
379
0
{
380
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
381
0
  register int temp, temp2;
382
0
  register int nbits;
383
0
  int blkn, ci;
384
0
  int Al = cinfo->Al;
385
0
  JBLOCKROW block;
386
0
  jpeg_component_info *compptr;
387
0
  ISHIFT_TEMPS
388
0
389
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
390
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
391
0
392
0
  /* Emit restart marker if needed */
393
0
  if (cinfo->restart_interval)
394
0
    if (entropy->restarts_to_go == 0)
395
0
      emit_restart(entropy, entropy->next_restart_num);
396
0
397
0
  /* Encode the MCU data blocks */
398
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
399
0
    block = MCU_data[blkn];
400
0
    ci = cinfo->MCU_membership[blkn];
401
0
    compptr = cinfo->cur_comp_info[ci];
402
0
403
0
    /* Compute the DC value after the required point transform by Al.
404
0
     * This is simply an arithmetic right shift.
405
0
     */
406
0
    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
407
0
408
0
    /* DC differences are figured on the point-transformed values. */
409
0
    temp = temp2 - entropy->last_dc_val[ci];
410
0
    entropy->last_dc_val[ci] = temp2;
411
0
412
0
    /* Encode the DC coefficient difference per section G.1.2.1 */
413
0
    temp2 = temp;
414
0
    if (temp < 0) {
415
0
      temp = -temp;             /* temp is abs value of input */
416
0
      /* For a negative input, want temp2 = bitwise complement of abs(input) */
417
0
      /* This code assumes we are on a two's complement machine */
418
0
      temp2--;
419
0
    }
420
0
421
0
    /* Find the number of bits needed for the magnitude of the coefficient */
422
0
    nbits = 0;
423
0
    while (temp) {
424
0
      nbits++;
425
0
      temp >>= 1;
426
0
    }
427
0
    /* Check for out-of-range coefficient values.
428
0
     * Since we're encoding a difference, the range limit is twice as much.
429
0
     */
430
0
    if (nbits > MAX_COEF_BITS+1)
431
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
432
0
433
0
    /* Count/emit the Huffman-coded symbol for the number of bits */
434
0
    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
435
0
436
0
    /* Emit that number of bits of the value, if positive, */
437
0
    /* or the complement of its magnitude, if negative. */
438
0
    if (nbits)                  /* emit_bits rejects calls with size 0 */
439
0
      emit_bits(entropy, (unsigned int) temp2, nbits);
440
0
  }
441
0
442
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
443
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
444
0
445
0
  /* Update restart-interval state too */
446
0
  if (cinfo->restart_interval) {
447
0
    if (entropy->restarts_to_go == 0) {
448
0
      entropy->restarts_to_go = cinfo->restart_interval;
449
0
      entropy->next_restart_num++;
450
0
      entropy->next_restart_num &= 7;
451
0
    }
452
0
    entropy->restarts_to_go--;
453
0
  }
454
0
455
0
  return TRUE;
456
0
}
457
458
459
/*
460
 * MCU encoding for AC initial scan (either spectral selection,
461
 * or first pass of successive approximation).
462
 */
463
464
METHODDEF(boolean)
465
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
466
0
{
467
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
468
0
  register int temp, temp2;
469
0
  register int nbits;
470
0
  register int r, k;
471
0
  int Se = cinfo->Se;
472
0
  int Al = cinfo->Al;
473
0
  JBLOCKROW block;
474
0
475
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
476
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
477
0
478
0
  /* Emit restart marker if needed */
479
0
  if (cinfo->restart_interval)
480
0
    if (entropy->restarts_to_go == 0)
481
0
      emit_restart(entropy, entropy->next_restart_num);
482
0
483
0
  /* Encode the MCU data block */
484
0
  block = MCU_data[0];
485
0
486
0
  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
487
0
488
0
  r = 0;                        /* r = run length of zeros */
489
0
490
0
  for (k = cinfo->Ss; k <= Se; k++) {
491
0
    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
492
0
      r++;
493
0
      continue;
494
0
    }
495
0
    /* We must apply the point transform by Al.  For AC coefficients this
496
0
     * is an integer division with rounding towards 0.  To do this portably
497
0
     * in C, we shift after obtaining the absolute value; so the code is
498
0
     * interwoven with finding the abs value (temp) and output bits (temp2).
499
0
     */
500
0
    if (temp < 0) {
501
0
      temp = -temp;             /* temp is abs value of input */
502
0
      temp >>= Al;              /* apply the point transform */
503
0
      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
504
0
      temp2 = ~temp;
505
0
    } else {
506
0
      temp >>= Al;              /* apply the point transform */
507
0
      temp2 = temp;
508
0
    }
509
0
    /* Watch out for case that nonzero coef is zero after point transform */
510
0
    if (temp == 0) {
511
0
      r++;
512
0
      continue;
513
0
    }
514
0
515
0
    /* Emit any pending EOBRUN */
516
0
    if (entropy->EOBRUN > 0)
517
0
      emit_eobrun(entropy);
518
0
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
519
0
    while (r > 15) {
520
0
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
521
0
      r -= 16;
522
0
    }
523
0
524
0
    /* Find the number of bits needed for the magnitude of the coefficient */
525
0
    nbits = 1;                  /* there must be at least one 1 bit */
526
0
    while ((temp >>= 1))
527
0
      nbits++;
528
0
    /* Check for out-of-range coefficient values */
529
0
    if (nbits > MAX_COEF_BITS)
530
0
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
531
0
532
0
    /* Count/emit Huffman symbol for run length / number of bits */
533
0
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
534
0
535
0
    /* Emit that number of bits of the value, if positive, */
536
0
    /* or the complement of its magnitude, if negative. */
537
0
    emit_bits(entropy, (unsigned int) temp2, nbits);
538
0
539
0
    r = 0;                      /* reset zero run length */
540
0
  }
541
0
542
0
  if (r > 0) {                  /* If there are trailing zeroes, */
543
0
    entropy->EOBRUN++;          /* count an EOB */
544
0
    if (entropy->EOBRUN == 0x7FFF)
545
0
      emit_eobrun(entropy);     /* force it out to avoid overflow */
546
0
  }
547
0
548
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
549
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
550
0
551
0
  /* Update restart-interval state too */
552
0
  if (cinfo->restart_interval) {
553
0
    if (entropy->restarts_to_go == 0) {
554
0
      entropy->restarts_to_go = cinfo->restart_interval;
555
0
      entropy->next_restart_num++;
556
0
      entropy->next_restart_num &= 7;
557
0
    }
558
0
    entropy->restarts_to_go--;
559
0
  }
560
0
561
0
  return TRUE;
562
0
}
563
564
565
/*
566
 * MCU encoding for DC successive approximation refinement scan.
567
 * Note: we assume such scans can be multi-component, although the spec
568
 * is not very clear on the point.
569
 */
570
571
METHODDEF(boolean)
572
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
573
0
{
574
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
575
0
  register int temp;
576
0
  int blkn;
577
0
  int Al = cinfo->Al;
578
0
  JBLOCKROW block;
579
0
580
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
581
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
582
0
583
0
  /* Emit restart marker if needed */
584
0
  if (cinfo->restart_interval)
585
0
    if (entropy->restarts_to_go == 0)
586
0
      emit_restart(entropy, entropy->next_restart_num);
587
0
588
0
  /* Encode the MCU data blocks */
589
0
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
590
0
    block = MCU_data[blkn];
591
0
592
0
    /* We simply emit the Al'th bit of the DC coefficient value. */
593
0
    temp = (*block)[0];
594
0
    emit_bits(entropy, (unsigned int) (temp >> Al), 1);
595
0
  }
596
0
597
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
598
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
599
0
600
0
  /* Update restart-interval state too */
601
0
  if (cinfo->restart_interval) {
602
0
    if (entropy->restarts_to_go == 0) {
603
0
      entropy->restarts_to_go = cinfo->restart_interval;
604
0
      entropy->next_restart_num++;
605
0
      entropy->next_restart_num &= 7;
606
0
    }
607
0
    entropy->restarts_to_go--;
608
0
  }
609
0
610
0
  return TRUE;
611
0
}
612
613
614
/*
615
 * MCU encoding for AC successive approximation refinement scan.
616
 */
617
618
METHODDEF(boolean)
619
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
620
0
{
621
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
622
0
  register int temp;
623
0
  register int r, k;
624
0
  int EOB;
625
0
  char *BR_buffer;
626
0
  unsigned int BR;
627
0
  int Se = cinfo->Se;
628
0
  int Al = cinfo->Al;
629
0
  JBLOCKROW block;
630
0
  int absvalues[DCTSIZE2];
631
0
632
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
633
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
634
0
635
0
  /* Emit restart marker if needed */
636
0
  if (cinfo->restart_interval)
637
0
    if (entropy->restarts_to_go == 0)
638
0
      emit_restart(entropy, entropy->next_restart_num);
639
0
640
0
  /* Encode the MCU data block */
641
0
  block = MCU_data[0];
642
0
643
0
  /* It is convenient to make a pre-pass to determine the transformed
644
0
   * coefficients' absolute values and the EOB position.
645
0
   */
646
0
  EOB = 0;
647
0
  for (k = cinfo->Ss; k <= Se; k++) {
648
0
    temp = (*block)[jpeg_natural_order[k]];
649
0
    /* We must apply the point transform by Al.  For AC coefficients this
650
0
     * is an integer division with rounding towards 0.  To do this portably
651
0
     * in C, we shift after obtaining the absolute value.
652
0
     */
653
0
    if (temp < 0)
654
0
      temp = -temp;             /* temp is abs value of input */
655
0
    temp >>= Al;                /* apply the point transform */
656
0
    absvalues[k] = temp;        /* save abs value for main pass */
657
0
    if (temp == 1)
658
0
      EOB = k;                  /* EOB = index of last newly-nonzero coef */
659
0
  }
660
0
661
0
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
662
0
663
0
  r = 0;                        /* r = run length of zeros */
664
0
  BR = 0;                       /* BR = count of buffered bits added now */
665
0
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
666
0
667
0
  for (k = cinfo->Ss; k <= Se; k++) {
668
0
    if ((temp = absvalues[k]) == 0) {
669
0
      r++;
670
0
      continue;
671
0
    }
672
0
673
0
    /* Emit any required ZRLs, but not if they can be folded into EOB */
674
0
    while (r > 15 && k <= EOB) {
675
0
      /* emit any pending EOBRUN and the BE correction bits */
676
0
      emit_eobrun(entropy);
677
0
      /* Emit ZRL */
678
0
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
679
0
      r -= 16;
680
0
      /* Emit buffered correction bits that must be associated with ZRL */
681
0
      emit_buffered_bits(entropy, BR_buffer, BR);
682
0
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
683
0
      BR = 0;
684
0
    }
685
0
686
0
    /* If the coef was previously nonzero, it only needs a correction bit.
687
0
     * NOTE: a straight translation of the spec's figure G.7 would suggest
688
0
     * that we also need to test r > 15.  But if r > 15, we can only get here
689
0
     * if k > EOB, which implies that this coefficient is not 1.
690
0
     */
691
0
    if (temp > 1) {
692
0
      /* The correction bit is the next bit of the absolute value. */
693
0
      BR_buffer[BR++] = (char) (temp & 1);
694
0
      continue;
695
0
    }
696
0
697
0
    /* Emit any pending EOBRUN and the BE correction bits */
698
0
    emit_eobrun(entropy);
699
0
700
0
    /* Count/emit Huffman symbol for run length / number of bits */
701
0
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
702
0
703
0
    /* Emit output bit for newly-nonzero coef */
704
0
    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
705
0
    emit_bits(entropy, (unsigned int) temp, 1);
706
0
707
0
    /* Emit buffered correction bits that must be associated with this code */
708
0
    emit_buffered_bits(entropy, BR_buffer, BR);
709
0
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
710
0
    BR = 0;
711
0
    r = 0;                      /* reset zero run length */
712
0
  }
713
0
714
0
  if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
715
0
    entropy->EOBRUN++;          /* count an EOB */
716
0
    entropy->BE += BR;          /* concat my correction bits to older ones */
717
0
    /* We force out the EOB if we risk either:
718
0
     * 1. overflow of the EOB counter;
719
0
     * 2. overflow of the correction bit buffer during the next MCU.
720
0
     */
721
0
    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
722
0
      emit_eobrun(entropy);
723
0
  }
724
0
725
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
726
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
727
0
728
0
  /* Update restart-interval state too */
729
0
  if (cinfo->restart_interval) {
730
0
    if (entropy->restarts_to_go == 0) {
731
0
      entropy->restarts_to_go = cinfo->restart_interval;
732
0
      entropy->next_restart_num++;
733
0
      entropy->next_restart_num &= 7;
734
0
    }
735
0
    entropy->restarts_to_go--;
736
0
  }
737
0
738
0
  return TRUE;
739
0
}
740
741
742
/*
743
 * Finish up at the end of a Huffman-compressed progressive scan.
744
 */
745
746
METHODDEF(void)
747
finish_pass_phuff (j_compress_ptr cinfo)
748
0
{
749
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
750
0
751
0
  entropy->next_output_byte = cinfo->dest->next_output_byte;
752
0
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
753
0
754
0
  /* Flush out any buffered data */
755
0
  emit_eobrun(entropy);
756
0
  flush_bits(entropy);
757
0
758
0
  cinfo->dest->next_output_byte = entropy->next_output_byte;
759
0
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
760
0
}
761
762
763
/*
764
 * Finish up a statistics-gathering pass and create the new Huffman tables.
765
 */
766
767
METHODDEF(void)
768
finish_pass_gather_phuff (j_compress_ptr cinfo)
769
0
{
770
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
771
0
  boolean is_DC_band;
772
0
  int ci, tbl;
773
0
  jpeg_component_info *compptr;
774
0
  JHUFF_TBL **htblptr;
775
0
  boolean did[NUM_HUFF_TBLS];
776
0
777
0
  /* Flush out buffered data (all we care about is counting the EOB symbol) */
778
0
  emit_eobrun(entropy);
779
0
780
0
  is_DC_band = (cinfo->Ss == 0);
781
0
782
0
  /* It's important not to apply jpeg_gen_optimal_table more than once
783
0
   * per table, because it clobbers the input frequency counts!
784
0
   */
785
0
  MEMZERO(did, sizeof(did));
786
0
787
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
788
0
    compptr = cinfo->cur_comp_info[ci];
789
0
    if (is_DC_band) {
790
0
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
791
0
        continue;
792
0
      tbl = compptr->dc_tbl_no;
793
0
    } else {
794
0
      tbl = compptr->ac_tbl_no;
795
0
    }
796
0
    if (! did[tbl]) {
797
0
      if (is_DC_band)
798
0
        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
799
0
      else
800
0
        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
801
0
      if (*htblptr == NULL)
802
0
        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
803
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
804
0
      did[tbl] = TRUE;
805
0
    }
806
0
  }
807
0
}
808
809
810
/*
811
 * Module initialization routine for progressive Huffman entropy encoding.
812
 */
813
814
GLOBAL(void)
815
jinit_phuff_encoder (j_compress_ptr cinfo)
816
0
{
817
0
  phuff_entropy_ptr entropy;
818
0
  int i;
819
0
820
0
  entropy = (phuff_entropy_ptr)
821
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
822
0
                                sizeof(phuff_entropy_encoder));
823
0
  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
824
0
  entropy->pub.start_pass = start_pass_phuff;
825
0
826
0
  /* Mark tables unallocated */
827
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
828
0
    entropy->derived_tbls[i] = NULL;
829
0
    entropy->count_ptrs[i] = NULL;
830
0
  }
831
0
  entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
832
0
}
833
834
#endif /* C_PROGRESSIVE_SUPPORTED */