             break;
         }
         
-        gpujpeg_huffman_gpu_encoder_decompose(temp, nbits, temp2);
-
-        // Emit Huffman symbol for run length / number of bits
-        int i = (r << 4) + nbits;
-        gpujpeg_huffman_gpu_encoder_emit_bits(out_bits, data_compressed, d_table_ac->size[i], d_table_ac->code[i]);
-
-        // Write Category offset
-        gpujpeg_huffman_gpu_encoder_emit_bits(out_bits, data_compressed, nbits, temp2);
+        gpujpeg_huffman_gpu_encode_value(cword_size, cword_value, r, value, d_table_ac);
+        gpujpeg_huffman_gpu_encoder_emit_bits(out_bits, data_compressed, cword_size, cword_value);
     }
 
     return 0;

}




/**

    int temp = data[0].x - dc;
    dc = data[0].x;
    
    int cword_size, cword_value;
    gpujpeg_huffman_gpu_encode_value(cword_size, cword_value, 0, temp, d_table_dc);
    gpujpeg_huffman_gpu_encoder_emit_bits(out_bits, data_compressed, cword_size, cword_value);
    
    // Encode the AC coefficients per section F.1.2.2 (r = run length of zeros)
    for ( int k = 1; k < 64; k++ ) 
    {
        const int value = data[k].x;
        const int r = data[k].y;

        // if last coefficient is zero
