static int decode_element(AVCodecContext *avctx, void *data, int ch_index,
                          int channels)
{
    ALACContext *alac = avctx->priv_data;
    int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;
    uint32_t output_samples;
    int i, ch;
    skip_bits(&alac->gb, 4);
    skip_bits(&alac->gb, 12);
    has_size = get_bits1(&alac->gb);
    alac->extra_bits = get_bits(&alac->gb, 2) << 3;
    bps = alac->sample_size - alac->extra_bits + channels - 1;
    if (bps > 32) {
        av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", bps);
        return AVERROR_PATCHWELCOME;
    }
    is_compressed = !get_bits1(&alac->gb);
    if (has_size)
        output_samples = get_bits_long(&alac->gb, 32);
    else
        output_samples = alac->max_samples_per_frame;
    if (!output_samples || output_samples > alac->max_samples_per_frame) {
        av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n",
               output_samples);
        return AVERROR_INVALIDDATA;
    }
    if (!alac->nb_samples) {
        alac->frame.nb_samples = output_samples;
        if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
            return ret;
        }
    } else if (output_samples != alac->nb_samples) {
        av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n",
               output_samples, alac->nb_samples);
        return AVERROR_INVALIDDATA;
    }
    alac->nb_samples = output_samples;
    if (alac->sample_size > 16) {
        for (ch = 0; ch < channels; ch++)
            alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch];
    }
    if (is_compressed) {
        int16_t lpc_coefs[2][32];
        int lpc_order[2];
        int prediction_type[2];
        int lpc_quant[2];
        int rice_history_mult[2];
        decorr_shift       = get_bits(&alac->gb, 8);
        decorr_left_weight = get_bits(&alac->gb, 8);
        for (ch = 0; ch < channels; ch++) {
            prediction_type[ch]   = get_bits(&alac->gb, 4);
            lpc_quant[ch]         = get_bits(&alac->gb, 4);
            rice_history_mult[ch] = get_bits(&alac->gb, 3);
            lpc_order[ch]         = get_bits(&alac->gb, 5);
            for (i = 0; i < lpc_order[ch]; i++)
                lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
        }
        if (alac->extra_bits) {
            for (i = 0; i < alac->nb_samples; i++) {
                for (ch = 0; ch < channels; ch++)
                    alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
            }
        }
        for (ch = 0; ch < channels; ch++) {
            rice_decompress(alac, alac->predict_error_buffer[ch],
                            alac->nb_samples, bps,
                            rice_history_mult[ch] * alac->rice_history_mult / 4);
            if (prediction_type[ch] == 15) {
                lpc_prediction(alac->predict_error_buffer[ch],
                               alac->predict_error_buffer[ch],
                               alac->nb_samples, bps, NULL, 31, 0);
            } else if (prediction_type[ch] > 0) {
                av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
                       prediction_type[ch]);
            }
            lpc_prediction(alac->predict_error_buffer[ch],
                           alac->output_samples_buffer[ch], alac->nb_samples,
                           bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
        }
    } else {
        for (i = 0; i < alac->nb_samples; i++) {
            for (ch = 0; ch < channels; ch++) {
                alac->output_samples_buffer[ch][i] =
                         get_sbits_long(&alac->gb, alac->sample_size);
            }
        }
        alac->extra_bits   = 0;
        decorr_shift       = 0;
        decorr_left_weight = 0;
    }
    if (channels == 2 && decorr_left_weight) {
        decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
                           decorr_shift, decorr_left_weight);
    }
    if (alac->extra_bits) {
        append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
                          alac->extra_bits, channels, alac->nb_samples);
    }
    switch(alac->sample_size) {
    case 16: {
        for (ch = 0; ch < channels; ch++) {
            int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch];
            for (i = 0; i < alac->nb_samples; i++)
                *outbuffer++ = alac->output_samples_buffer[ch][i];
        }}
        break;
    case 24: {
        for (ch = 0; ch < channels; ch++) {
            for (i = 0; i < alac->nb_samples; i++)
                alac->output_samples_buffer[ch][i] <<= 8;
        }}
        break;
    }
    return 0;
}
['static int decode_element(AVCodecContext *avctx, void *data, int ch_index,\n                          int channels)\n{\n    ALACContext *alac = avctx->priv_data;\n    int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;\n    uint32_t output_samples;\n    int i, ch;\n    skip_bits(&alac->gb, 4);\n    skip_bits(&alac->gb, 12);\n    has_size = get_bits1(&alac->gb);\n    alac->extra_bits = get_bits(&alac->gb, 2) << 3;\n    bps = alac->sample_size - alac->extra_bits + channels - 1;\n    if (bps > 32) {\n        av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\\n", bps);\n        return AVERROR_PATCHWELCOME;\n    }\n    is_compressed = !get_bits1(&alac->gb);\n    if (has_size)\n        output_samples = get_bits_long(&alac->gb, 32);\n    else\n        output_samples = alac->max_samples_per_frame;\n    if (!output_samples || output_samples > alac->max_samples_per_frame) {\n        av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\\n",\n               output_samples);\n        return AVERROR_INVALIDDATA;\n    }\n    if (!alac->nb_samples) {\n        alac->frame.nb_samples = output_samples;\n        if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {\n            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n            return ret;\n        }\n    } else if (output_samples != alac->nb_samples) {\n        av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\\n",\n               output_samples, alac->nb_samples);\n        return AVERROR_INVALIDDATA;\n    }\n    alac->nb_samples = output_samples;\n    if (alac->sample_size > 16) {\n        for (ch = 0; ch < channels; ch++)\n            alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch];\n    }\n    if (is_compressed) {\n        int16_t lpc_coefs[2][32];\n        int lpc_order[2];\n        int prediction_type[2];\n        int lpc_quant[2];\n        int rice_history_mult[2];\n        decorr_shift       = get_bits(&alac->gb, 8);\n        decorr_left_weight = get_bits(&alac->gb, 8);\n        for (ch = 0; ch < channels; ch++) {\n            prediction_type[ch]   = get_bits(&alac->gb, 4);\n            lpc_quant[ch]         = get_bits(&alac->gb, 4);\n            rice_history_mult[ch] = get_bits(&alac->gb, 3);\n            lpc_order[ch]         = get_bits(&alac->gb, 5);\n            for (i = 0; i < lpc_order[ch]; i++)\n                lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);\n        }\n        if (alac->extra_bits) {\n            for (i = 0; i < alac->nb_samples; i++) {\n                for (ch = 0; ch < channels; ch++)\n                    alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);\n            }\n        }\n        for (ch = 0; ch < channels; ch++) {\n            rice_decompress(alac, alac->predict_error_buffer[ch],\n                            alac->nb_samples, bps,\n                            rice_history_mult[ch] * alac->rice_history_mult / 4);\n            if (prediction_type[ch] == 15) {\n                lpc_prediction(alac->predict_error_buffer[ch],\n                               alac->predict_error_buffer[ch],\n                               alac->nb_samples, bps, NULL, 31, 0);\n            } else if (prediction_type[ch] > 0) {\n                av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\\n",\n                       prediction_type[ch]);\n            }\n            lpc_prediction(alac->predict_error_buffer[ch],\n                           alac->output_samples_buffer[ch], alac->nb_samples,\n                           bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);\n        }\n    } else {\n        for (i = 0; i < alac->nb_samples; i++) {\n            for (ch = 0; ch < channels; ch++) {\n                alac->output_samples_buffer[ch][i] =\n                         get_sbits_long(&alac->gb, alac->sample_size);\n            }\n        }\n        alac->extra_bits   = 0;\n        decorr_shift       = 0;\n        decorr_left_weight = 0;\n    }\n    if (channels == 2 && decorr_left_weight) {\n        decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,\n                           decorr_shift, decorr_left_weight);\n    }\n    if (alac->extra_bits) {\n        append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,\n                          alac->extra_bits, channels, alac->nb_samples);\n    }\n    switch(alac->sample_size) {\n    case 16: {\n        for (ch = 0; ch < channels; ch++) {\n            int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch];\n            for (i = 0; i < alac->nb_samples; i++)\n                *outbuffer++ = alac->output_samples_buffer[ch][i];\n        }}\n        break;\n    case 24: {\n        for (ch = 0; ch < channels; ch++) {\n            for (i = 0; i < alac->nb_samples; i++)\n                alac->output_samples_buffer[ch][i] <<= 8;\n        }}\n        break;\n    }\n    return 0;\n}']