-
-static av_cold int g722_init(AVCodecContext * avctx)
-{
- G722Context *c = avctx->priv_data;
-
- if (avctx->channels != 1) {
- av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
- return AVERROR_INVALIDDATA;
- }
- avctx->sample_fmt = SAMPLE_FMT_S16;
-
- switch (avctx->bits_per_coded_sample) {
- case 8:
- case 7:
- case 6:
- break;
- default:
- av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], "
- "assuming 8\n",
- avctx->bits_per_coded_sample);
- case 0:
- avctx->bits_per_coded_sample = 8;
- break;
- }
-
- c->band[0].scale_factor = 8;
- c->band[1].scale_factor = 2;
- c->prev_samples_pos = 22;
-
- if (avctx->lowres)
- avctx->sample_rate /= 2;
-
- return 0;
-}
-
-#if CONFIG_ADPCM_G722_DECODER
-static const int16_t low_inv_quant5[32] = {
- -35, -35, -2919, -2195, -1765, -1458, -1219, -1023,
- -858, -714, -587, -473, -370, -276, -190, -110,
- 2919, 2195, 1765, 1458, 1219, 1023, 858, 714,
- 587, 473, 370, 276, 190, 110, 35, -35
-};
-static const int16_t low_inv_quant6[64] = {
- -17, -17, -17, -17, -3101, -2738, -2376, -2088,
- -1873, -1689, -1535, -1399, -1279, -1170, -1072, -982,
- -899, -822, -750, -682, -618, -558, -501, -447,
- -396, -347, -300, -254, -211, -170, -130, -91,
- 3101, 2738, 2376, 2088, 1873, 1689, 1535, 1399,
- 1279, 1170, 1072, 982, 899, 822, 750, 682,
- 618, 558, 501, 447, 396, 347, 300, 254,
- 211, 170, 130, 91, 54, 17, -54, -17
-};
-
-static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5,
- low_inv_quant4 };
-
-static int g722_decode_frame(AVCodecContext *avctx, void *data,
- int *data_size, AVPacket *avpkt)
-{
- G722Context *c = avctx->priv_data;
- int16_t *out_buf = data;
- int j, out_len = 0;
- const int skip = 8 - avctx->bits_per_coded_sample;
- const int16_t *quantizer_table = low_inv_quants[skip];
- GetBitContext gb;
-
- init_get_bits(&gb, avpkt->data, avpkt->size * 8);
-
- for (j = 0; j < avpkt->size; j++) {
- int ilow, ihigh, rlow;
-
- ihigh = get_bits(&gb, 2);
- ilow = get_bits(&gb, 6 - skip);
- skip_bits(&gb, skip);
-
- rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
- + c->band[0].s_predictor, -16384, 16383);
-
- update_low_predictor(&c->band[0], ilow >> (2 - skip));
-
- if (!avctx->lowres) {
- const int dhigh = c->band[1].scale_factor *
- high_inv_quant[ihigh] >> 10;
- const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
- -16384, 16383);
- int xout1, xout2;
-
- update_high_predictor(&c->band[1], dhigh, ihigh);
-
- c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
- c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
- apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
- &xout1, &xout2);
- out_buf[out_len++] = av_clip_int16(xout1 >> 12);
- out_buf[out_len++] = av_clip_int16(xout2 >> 12);
- if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
- memmove(c->prev_samples,
- c->prev_samples + c->prev_samples_pos - 22,
- 22 * sizeof(c->prev_samples[0]));
- c->prev_samples_pos = 22;
- }
- } else
- out_buf[out_len++] = rlow;
- }
- *data_size = out_len << 1;
- return avpkt->size;
-}
-
-AVCodec adpcm_g722_decoder = {
- .name = "g722",
- .type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_ADPCM_G722,
- .priv_data_size = sizeof(G722Context),
- .init = g722_init,
- .decode = g722_decode_frame,
- .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
- .max_lowres = 1,
-};
-#endif
-
-#if CONFIG_ADPCM_G722_ENCODER
-static const int16_t low_quant[33] = {
- 35, 72, 110, 150, 190, 233, 276, 323,
- 370, 422, 473, 530, 587, 650, 714, 786,
- 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
- 1765, 1980, 2195, 2557, 2919
-};
-
-static inline void filter_samples(G722Context *c, const int16_t *samples,
- int *xlow, int *xhigh)
-{
- int xout1, xout2;
- c->prev_samples[c->prev_samples_pos++] = samples[0];
- c->prev_samples[c->prev_samples_pos++] = samples[1];
- apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
- *xlow = xout1 + xout2 >> 13;
- *xhigh = xout1 - xout2 >> 13;
- if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
- memmove(c->prev_samples,
- c->prev_samples + c->prev_samples_pos - 22,
- 22 * sizeof(c->prev_samples[0]));
- c->prev_samples_pos = 22;
- }
-}
-
-static inline int encode_high(const struct G722Band *state, int xhigh)
-{
- int diff = av_clip_int16(xhigh - state->s_predictor);
- int pred = 141 * state->scale_factor >> 8;
- /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
- return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
-}
-
-static inline int encode_low(const struct G722Band* state, int xlow)
-{
- int diff = av_clip_int16(xlow - state->s_predictor);
- /* = diff >= 0 ? diff : -(diff + 1) */
- int limit = diff ^ (diff >> (sizeof(diff)*8-1));
- int i = 0;
- limit = limit + 1 << 10;
- if (limit > low_quant[8] * state->scale_factor)
- i = 9;
- while (i < 29 && limit > low_quant[i] * state->scale_factor)
- i++;
- return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
-}
-
-static int g722_encode_frame(AVCodecContext *avctx,
- uint8_t *dst, int buf_size, void *data)
-{
- G722Context *c = avctx->priv_data;
- const int16_t *samples = data;
- int i;
-
- for (i = 0; i < buf_size >> 1; i++) {
- int xlow, xhigh, ihigh, ilow;
- filter_samples(c, &samples[2*i], &xlow, &xhigh);
- ihigh = encode_high(&c->band[1], xhigh);
- ilow = encode_low(&c->band[0], xlow);
- update_high_predictor(&c->band[1], c->band[1].scale_factor *
- high_inv_quant[ihigh] >> 10, ihigh);
- update_low_predictor(&c->band[0], ilow >> 2);
- *dst++ = ihigh << 6 | ilow;
- }
- return i;
-}
-
-AVCodec adpcm_g722_encoder = {
- .name = "g722",
- .type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_ADPCM_G722,
- .priv_data_size = sizeof(G722Context),
- .init = g722_init,
- .encode = g722_encode_frame,
- .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
- .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
-};
-#endif
-