static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,

                              ScanTable *intra_scantable,

                              uint16_t luma_intra_matrix[64],

                              uint16_t chroma_intra_matrix[64],

                              int hsample[3])

{

    int i, j, size;

    uint8_t *ptr;

    MpegEncContext *s = avctx->priv_data;



    if (avctx->codec_id != AV_CODEC_ID_LJPEG) {

        int matrix_count = 1 + !!memcmp(luma_intra_matrix,

                                        chroma_intra_matrix,

                                        sizeof(luma_intra_matrix[0]) * 64);

    if (s->force_duplicated_matrix)

        matrix_count = 2;

    /* quant matrixes */

    put_marker(p, DQT);

    put_bits(p, 16, 2 + matrix_count * (1 + 64));

    put_bits(p, 4, 0); /* 8 bit precision */

    put_bits(p, 4, 0); /* table 0 */

    for(i=0;i<64;i++) {

        j = intra_scantable->permutated[i];

        put_bits(p, 8, luma_intra_matrix[j]);

    }



        if (matrix_count > 1) {

            put_bits(p, 4, 0); /* 8 bit precision */

            put_bits(p, 4, 1); /* table 1 */

            for(i=0;i<64;i++) {

                j = intra_scantable->permutated[i];

                put_bits(p, 8, chroma_intra_matrix[j]);

            }

        }

    }



    if(avctx->active_thread_type & FF_THREAD_SLICE){

        put_marker(p, DRI);

        put_bits(p, 16, 4);

        put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);

    }



    /* huffman table */

    put_marker(p, DHT);

    flush_put_bits(p);

    ptr = put_bits_ptr(p);

    put_bits(p, 16, 0); /* patched later */

    size = 2;



    // Only MJPEG can have a variable Huffman variable. All other

    // formats use the default Huffman table.

    if (s->out_format == FMT_MJPEG && s->huffman == HUFFMAN_TABLE_OPTIMAL) {

        size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,

                                  s->mjpeg_ctx->val_dc_luminance);

        size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,

                                  s->mjpeg_ctx->val_dc_chrominance);



        size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,

                                  s->mjpeg_ctx->val_ac_luminance);

        size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,

                                  s->mjpeg_ctx->val_ac_chrominance);

    } else {

        size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,

                                  avpriv_mjpeg_val_dc);

        size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,

                                  avpriv_mjpeg_val_dc);



        size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,

                                  avpriv_mjpeg_val_ac_luminance);

        size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,

                                  avpriv_mjpeg_val_ac_chrominance);

    }

    AV_WB16(ptr, size);

}

static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,

                              ScanTable *intra_scantable,

                              uint16_t luma_intra_matrix[64],

                              uint16_t chroma_intra_matrix[64],

                              int hsample[3])

{

    int i, j, size;

    uint8_t *ptr;

    MpegEncContext *s = avctx->priv_data;



    if (avctx->codec_id != AV_CODEC_ID_LJPEG) {

        int matrix_count = 1 + !!memcmp(luma_intra_matrix,

                                        chroma_intra_matrix,

                                        sizeof(luma_intra_matrix[0]) * 64);

    if (s->force_duplicated_matrix)

        matrix_count = 2;

    

    put_marker(p, DQT);

    put_bits(p, 16, 2 + matrix_count * (1 + 64));

    put_bits(p, 4, 0); 

    put_bits(p, 4, 0); 

    for(i=0;i<64;i++) {

        j = intra_scantable->permutated[i];

        put_bits(p, 8, luma_intra_matrix[j]);

    }



        if (matrix_count > 1) {

            put_bits(p, 4, 0); 

            put_bits(p, 4, 1); 

            for(i=0;i<64;i++) {

                j = intra_scantable->permutated[i];

                put_bits(p, 8, chroma_intra_matrix[j]);

            }

        }

    }



    if(avctx->active_thread_type & FF_THREAD_SLICE){

        put_marker(p, DRI);

        put_bits(p, 16, 4);

        put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);

    }



    

    put_marker(p, DHT);

    flush_put_bits(p);

    ptr = put_bits_ptr(p);

    put_bits(p, 16, 0); 

    size = 2;



    

    

    if (s->out_format == FMT_MJPEG && s->huffman == HUFFMAN_TABLE_OPTIMAL) {

        size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,

                                  s->mjpeg_ctx->val_dc_luminance);

        size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,

                                  s->mjpeg_ctx->val_dc_chrominance);



        size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,

                                  s->mjpeg_ctx->val_ac_luminance);

        size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,

                                  s->mjpeg_ctx->val_ac_chrominance);

    } else {

        size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,

                                  avpriv_mjpeg_val_dc);

        size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,

                                  avpriv_mjpeg_val_dc);



        size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,

                                  avpriv_mjpeg_val_ac_luminance);

        size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,

                                  avpriv_mjpeg_val_ac_chrominance);

    }

    AV_WB16(ptr, size);

}

static void FUNC_0(AVCodecContext *VAR_0, PutBitContext *VAR_1,

                              ScanTable *VAR_2,

                              uint16_t VAR_3[64],

                              uint16_t VAR_4[64],

                              int VAR_5[3])

{

    int VAR_6, VAR_7, VAR_8;

    uint8_t *ptr;

    MpegEncContext *s = VAR_0->priv_data;



    if (VAR_0->codec_id != AV_CODEC_ID_LJPEG) {

        int VAR_9 = 1 + !!memcmp(VAR_3,

                                        VAR_4,

                                        sizeof(VAR_3[0]) * 64);

    if (s->force_duplicated_matrix)

        VAR_9 = 2;

    

    put_marker(VAR_1, DQT);

    put_bits(VAR_1, 16, 2 + VAR_9 * (1 + 64));

    put_bits(VAR_1, 4, 0); 

    put_bits(VAR_1, 4, 0); 

    for(VAR_6=0;VAR_6<64;VAR_6++) {

        VAR_7 = VAR_2->permutated[VAR_6];

        put_bits(VAR_1, 8, VAR_3[VAR_7]);

    }



        if (VAR_9 > 1) {

            put_bits(VAR_1, 4, 0); 

            put_bits(VAR_1, 4, 1); 

            for(VAR_6=0;VAR_6<64;VAR_6++) {

                VAR_7 = VAR_2->permutated[VAR_6];

                put_bits(VAR_1, 8, VAR_4[VAR_7]);

            }

        }

    }



    if(VAR_0->active_thread_type & FF_THREAD_SLICE){

        put_marker(VAR_1, DRI);

        put_bits(VAR_1, 16, 4);

        put_bits(VAR_1, 16, (VAR_0->width-1)/(8*VAR_5[0]) + 1);

    }



    

    put_marker(VAR_1, DHT);

    flush_put_bits(VAR_1);

    ptr = put_bits_ptr(VAR_1);

    put_bits(VAR_1, 16, 0); 

    VAR_8 = 2;



    

    

    if (s->out_format == FMT_MJPEG && s->huffman == HUFFMAN_TABLE_OPTIMAL) {

        VAR_8 += put_huffman_table(VAR_1, 0, 0, s->mjpeg_ctx->bits_dc_luminance,

                                  s->mjpeg_ctx->val_dc_luminance);

        VAR_8 += put_huffman_table(VAR_1, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,

                                  s->mjpeg_ctx->val_dc_chrominance);



        VAR_8 += put_huffman_table(VAR_1, 1, 0, s->mjpeg_ctx->bits_ac_luminance,

                                  s->mjpeg_ctx->val_ac_luminance);

        VAR_8 += put_huffman_table(VAR_1, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,

                                  s->mjpeg_ctx->val_ac_chrominance);

    } else {

        VAR_8 += put_huffman_table(VAR_1, 0, 0, avpriv_mjpeg_bits_dc_luminance,

                                  avpriv_mjpeg_val_dc);

        VAR_8 += put_huffman_table(VAR_1, 0, 1, avpriv_mjpeg_bits_dc_chrominance,

                                  avpriv_mjpeg_val_dc);



        VAR_8 += put_huffman_table(VAR_1, 1, 0, avpriv_mjpeg_bits_ac_luminance,

                                  avpriv_mjpeg_val_ac_luminance);

        VAR_8 += put_huffman_table(VAR_1, 1, 1, avpriv_mjpeg_bits_ac_chrominance,

                                  avpriv_mjpeg_val_ac_chrominance);

    }

    AV_WB16(ptr, VAR_8);

}

static void FUNC_0(AVCodecContext *VAR_0, PutBitContext *VAR_1,
ScanTable *VAR_2,
uint16_t VAR_3[64],
uint16_t VAR_4[64],
int VAR_5[3])
{ int VAR_6, VAR_7, VAR_8; uint8_t *ptr; MpegEncContext *s = VAR_0->priv_data; if (VAR_0->codec_id != AV_CODEC_ID_LJPEG) { int VAR_9 = 1 + !!memcmp(VAR_3,
VAR_4,
sizeof(VAR_3[0]) * 64); if (s->force_duplicated_matrix)
VAR_9 = 2; put_marker(VAR_1, DQT); put_bits(VAR_1, 16, 2 + VAR_9 * (1 + 64)); put_bits(VAR_1, 4, 0); put_bits(VAR_1, 4, 0); for(VAR_6=0;VAR_6<64;VAR_6++) { VAR_7 = VAR_2->permutated[VAR_6]; put_bits(VAR_1, 8, VAR_3[VAR_7]); } if (VAR_9 > 1) { put_bits(VAR_1, 4, 0); put_bits(VAR_1, 4, 1); for(VAR_6=0;VAR_6<64;VAR_6++) { VAR_7 = VAR_2->permutated[VAR_6]; put_bits(VAR_1, 8, VAR_4[VAR_7]); } } } if(VAR_0->active_thread_type & FF_THREAD_SLICE){ put_marker(VAR_1, DRI); put_bits(VAR_1, 16, 4); put_bits(VAR_1, 16, (VAR_0->width-1)/(8*VAR_5[0]) + 1); } put_marker(VAR_1, DHT); flush_put_bits(VAR_1); ptr = put_bits_ptr(VAR_1); put_bits(VAR_1, 16, 0); VAR_8 = 2; if (s->out_format == FMT_MJPEG && s->huffman == HUFFMAN_TABLE_OPTIMAL) { VAR_8 += put_huffman_table(VAR_1, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
s->mjpeg_ctx->val_dc_luminance); VAR_8 += put_huffman_table(VAR_1, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
s->mjpeg_ctx->val_dc_chrominance); VAR_8 += put_huffman_table(VAR_1, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
s->mjpeg_ctx->val_ac_luminance); VAR_8 += put_huffman_table(VAR_1, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
s->mjpeg_ctx->val_ac_chrominance); } else { VAR_8 += put_huffman_table(VAR_1, 0, 0, avpriv_mjpeg_bits_dc_luminance,
avpriv_mjpeg_val_dc); VAR_8 += put_huffman_table(VAR_1, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
avpriv_mjpeg_val_dc); VAR_8 += put_huffman_table(VAR_1, 1, 0, avpriv_mjpeg_bits_ac_luminance,
avpriv_mjpeg_val_ac_luminance); VAR_8 += put_huffman_table(VAR_1, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
avpriv_mjpeg_val_ac_chrominance); } AV_WB16(ptr, VAR_8); }