// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcphuff.c
// Function: start_pass_phuff
METHODDEF(void)
start_pass_phuff (j_compress_ptr cinfo, jboolean gather_statistics)
{  
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  jboolean is_DC_band;
  int ci, tbl;
  jpeg_component_info * compptr;

  entropy->cinfo = cinfo;
  entropy->gather_statistics = gather_statistics;

  is_DC_band = (cinfo->Ss == 0);

  /* We assume jcmaster.c already validated the scan parameters. */

  /* Select execution routines */
  if (cinfo->Ah == 0) {
    if (is_DC_band)
      entropy->pub.encode_mcu = encode_mcu_DC_first;
    else
      entropy->pub.encode_mcu = encode_mcu_AC_first;
  } else {
    if (is_DC_band)
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
    else {
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
      /* AC refinement needs a correction bit buffer */
      if (entropy->bit_buffer == NULL)
	entropy->bit_buffer = (char *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      MAX_CORR_BITS * SIZEOF(char));
    }
  }
  if (gather_statistics)
    entropy->pub.finish_pass = finish_pass_gather_phuff;
  else
    entropy->pub.finish_pass = finish_pass_phuff;

  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
   * for AC coefficients.
   */
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    compptr = cinfo->cur_comp_info[ci];
    /* Initialize DC predictions to 0 */
    entropy->last_dc_val[ci] = 0;
    /* Get table index */
    if (is_DC_band) {
      if (cinfo->Ah != 0)	/* DC refinement needs no table */
	continue;
      tbl = compptr->dc_tbl_no;
    } else {
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
    }
    if (gather_statistics) {
      /* Check for invalid table index */
      /* (make_c_derived_tbl does this in the other path) */
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
      /* Allocate and zero the statistics tables */
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
      if (entropy->count_ptrs[tbl] == NULL)
	entropy->count_ptrs[tbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      257 * SIZEOF(long));
      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
    } else {
      /* Compute derived values for Huffman table */
      /* We may do this more than once for a table, but it's not expensive */
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
			      & entropy->derived_tbls[tbl]);
    }
  }

  /* Initialize AC stuff */
  entropy->EOBRUN = 0;
  entropy->BE = 0;

  /* Initialize bit buffer to empty */
  entropy->put_buffer = 0;
  entropy->put_bits = 0;

  /* Initialize restart stuff */
  entropy->restarts_to_go = cinfo->restart_interval;
  entropy->next_restart_num = 0;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x1f0(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
movl 0x2c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x19c(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x1a4(%rax)
jne 0xdf0a9
cmpl $0x0, 0x1c(%rsp)
je 0xdf097
movq 0x20(%rsp), %rax
leaq 0x26f(%rip), %rcx       # 0xdf300
movq %rcx, 0x8(%rax)
jmp 0xdf0a7
movq 0x20(%rsp), %rax
leaq 0x4bd(%rip), %rcx       # 0xdf560
movq %rcx, 0x8(%rax)
jmp 0xdf10b
cmpl $0x0, 0x1c(%rsp)
je 0xdf0c2
movq 0x20(%rsp), %rax
leaq 0x794(%rip), %rcx       # 0xdf850
movq %rcx, 0x8(%rax)
jmp 0xdf109
movq 0x20(%rsp), %rax
leaq 0x8f2(%rip), %rcx       # 0xdf9c0
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x68(%rax)
jne 0xdf107
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq *%rax
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x68(%rax)
jmp 0xdf109
jmp 0xdf10b
cmpl $0x0, 0x2c(%rsp)
je 0xdf124
movq 0x20(%rsp), %rax
leaq 0xdc2(%rip), %rcx       # 0xdfee0
movq %rcx, 0x10(%rax)
jmp 0xdf134
movq 0x20(%rsp), %rax
leaq 0xf20(%rip), %rcx       # 0xe0050
movq %rcx, 0x10(%rax)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x144(%rcx), %eax
jge 0xdf29f
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movl $0x0, 0x48(%rax,%rcx,4)
cmpl $0x0, 0x1c(%rsp)
je 0xdf1a2
movq 0x30(%rsp), %rax
cmpl $0x0, 0x1a4(%rax)
je 0xdf194
jmp 0xdf28f
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x14(%rsp)
jmp 0xdf1b6
movq 0x8(%rsp), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0x14(%rsp)
movq 0x20(%rsp), %rax
movl %ecx, 0x58(%rax)
cmpl $0x0, 0x2c(%rsp)
je 0xdf266
cmpl $0x0, 0x14(%rsp)
jl 0xdf1cf
cmpl $0x4, 0x14(%rsp)
jl 0xdf1ff
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x32, 0x28(%rax)
movl 0x14(%rsp), %ecx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
cmpq $0x0, 0x98(%rax,%rcx,8)
jne 0xdf246
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x808, %edx            # imm = 0x808
callq *%rax
movq %rax, %rdx
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, 0x98(%rax,%rcx,8)
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq 0x98(%rax,%rcx,8), %rdi
xorl %esi, %esi
movl $0x808, %edx            # imm = 0x808
callq 0xf280
jmp 0xdf28d
movq 0x30(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl 0x14(%rsp), %edx
movq 0x20(%rsp), %rcx
addq $0x78, %rcx
movslq 0x14(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
callq 0xdd420
jmp 0xdf28f
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xdf13c
movq 0x20(%rsp), %rax
movl $0x0, 0x5c(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x60(%rax)
movq 0x20(%rsp), %rax
movq $0x0, 0x30(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x38(%rax)
movq 0x30(%rsp), %rax
movl 0x118(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x70(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x74(%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)