                                | (code_word >> (code_bit_size - new_bit_count) & ((1 << new_bit_count) - 1));
         
         out_byte_ptr[0] = out_byte;
+        if(out_byte == 0xFF) {
+            out_byte_ptr[1] = 0;
+            buffer_bits += 8;
+        }
         code_bit_size -= new_bit_count;
         buffer_bits += new_bit_count;
     }

    // count of consecutive zeros before odd value (either one more than 
    // even if even is zero or none if even value itself is nonzero)
    const int zeros_before_odd = in_even || !tid ? 0 : zeros_before_even + 1;
    
    // (TODO: remove later) save leftover bits from previous iteration
    uint8_t leftover_value = *(uint8_t*)s_temp;
    
    // clear the buffer in parallel
    ((uint64_t*)s_temp)[tid] = 0;
    
    // put leftover value back
    if(0 == tid) {
        *(uint8_t*)s_temp = leftover_value;
    }
    
    
    
    
    // pointer to LUT for encoding thread's even value 
    // (only thread #0 uses DC table, others use AC table)

    struct gpujpeg_table_huffman_encoder* d_table_cbcr_ac = &gpujpeg_huffman_gpu_encoder_table_huffman[GPUJPEG_COMPONENT_CHROMINANCE][GPUJPEG_HUFFMAN_AC];
#endif
    
    int warpidx = threadIdx.x >> 5;
    int tid = threadIdx.x & 31;

//     __shared__ int s_out_all[256 * WARPS_NUM];
// 
//     uint4 * s_out  =  s_out_all + warpidx * 256;
    
    // Select Segment
    int segment_index = blockIdx.x * WARPS_NUM + warpidx;
