00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include "avcodec.h"
00031 #include "g722.h"
00032
00033 #define FREEZE_INTERVAL 128
00034
00035 static av_cold int g722_encode_init(AVCodecContext * avctx)
00036 {
00037 G722Context *c = avctx->priv_data;
00038
00039 if (avctx->channels != 1) {
00040 av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
00041 return AVERROR_INVALIDDATA;
00042 }
00043
00044 c->band[0].scale_factor = 8;
00045 c->band[1].scale_factor = 2;
00046 c->prev_samples_pos = 22;
00047
00048 if (avctx->trellis) {
00049 int frontier = 1 << avctx->trellis;
00050 int max_paths = frontier * FREEZE_INTERVAL;
00051 int i;
00052 for (i = 0; i < 2; i++) {
00053 c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
00054 c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
00055 c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
00056 }
00057 }
00058
00059 return 0;
00060 }
00061
00062 static av_cold int g722_encode_close(AVCodecContext *avctx)
00063 {
00064 G722Context *c = avctx->priv_data;
00065 int i;
00066 for (i = 0; i < 2; i++) {
00067 av_freep(&c->paths[i]);
00068 av_freep(&c->node_buf[i]);
00069 av_freep(&c->nodep_buf[i]);
00070 }
00071 return 0;
00072 }
00073
00074 static const int16_t low_quant[33] = {
00075 35, 72, 110, 150, 190, 233, 276, 323,
00076 370, 422, 473, 530, 587, 650, 714, 786,
00077 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
00078 1765, 1980, 2195, 2557, 2919
00079 };
00080
00081 static inline void filter_samples(G722Context *c, const int16_t *samples,
00082 int *xlow, int *xhigh)
00083 {
00084 int xout1, xout2;
00085 c->prev_samples[c->prev_samples_pos++] = samples[0];
00086 c->prev_samples[c->prev_samples_pos++] = samples[1];
00087 ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
00088 *xlow = xout1 + xout2 >> 13;
00089 *xhigh = xout1 - xout2 >> 13;
00090 if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
00091 memmove(c->prev_samples,
00092 c->prev_samples + c->prev_samples_pos - 22,
00093 22 * sizeof(c->prev_samples[0]));
00094 c->prev_samples_pos = 22;
00095 }
00096 }
00097
00098 static inline int encode_high(const struct G722Band *state, int xhigh)
00099 {
00100 int diff = av_clip_int16(xhigh - state->s_predictor);
00101 int pred = 141 * state->scale_factor >> 8;
00102
00103 return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
00104 }
00105
00106 static inline int encode_low(const struct G722Band* state, int xlow)
00107 {
00108 int diff = av_clip_int16(xlow - state->s_predictor);
00109
00110 int limit = diff ^ (diff >> (sizeof(diff)*8-1));
00111 int i = 0;
00112 limit = limit + 1 << 10;
00113 if (limit > low_quant[8] * state->scale_factor)
00114 i = 9;
00115 while (i < 29 && limit > low_quant[i] * state->scale_factor)
00116 i++;
00117 return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
00118 }
00119
00120 static int g722_encode_trellis(AVCodecContext *avctx,
00121 uint8_t *dst, int buf_size, void *data)
00122 {
00123 G722Context *c = avctx->priv_data;
00124 const int16_t *samples = data;
00125 int i, j, k;
00126 int frontier = 1 << avctx->trellis;
00127 struct TrellisNode **nodes[2];
00128 struct TrellisNode **nodes_next[2];
00129 int pathn[2] = {0, 0}, froze = -1;
00130 struct TrellisPath *p[2];
00131
00132 for (i = 0; i < 2; i++) {
00133 nodes[i] = c->nodep_buf[i];
00134 nodes_next[i] = c->nodep_buf[i] + frontier;
00135 memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf));
00136 nodes[i][0] = c->node_buf[i] + frontier;
00137 nodes[i][0]->ssd = 0;
00138 nodes[i][0]->path = 0;
00139 nodes[i][0]->state = c->band[i];
00140 }
00141
00142 for (i = 0; i < buf_size; i++) {
00143 int xlow, xhigh;
00144 struct TrellisNode *next[2];
00145 int heap_pos[2] = {0, 0};
00146
00147 for (j = 0; j < 2; j++) {
00148 next[j] = c->node_buf[j] + frontier*(i & 1);
00149 memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
00150 }
00151
00152 filter_samples(c, &samples[2*i], &xlow, &xhigh);
00153
00154 for (j = 0; j < frontier && nodes[0][j]; j++) {
00155
00156
00157
00158
00159
00160 int range = j < frontier/2 ? 4 : 0;
00161 struct TrellisNode *cur_node = nodes[0][j];
00162
00163 int ilow = encode_low(&cur_node->state, xlow);
00164
00165 for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
00166 int decoded, dec_diff, pos;
00167 uint32_t ssd;
00168 struct TrellisNode* node;
00169
00170 if (k < 0)
00171 continue;
00172
00173 decoded = av_clip((cur_node->state.scale_factor *
00174 ff_g722_low_inv_quant6[k] >> 10)
00175 + cur_node->state.s_predictor, -16384, 16383);
00176 dec_diff = xlow - decoded;
00177
00178 #define STORE_NODE(index, UPDATE, VALUE)\
00179 ssd = cur_node->ssd + dec_diff*dec_diff;\
00180
00181 \
00182 if (ssd < cur_node->ssd)\
00183 continue;\
00184 if (heap_pos[index] < frontier) {\
00185 pos = heap_pos[index]++;\
00186 assert(pathn[index] < FREEZE_INTERVAL * frontier);\
00187 node = nodes_next[index][pos] = next[index]++;\
00188 node->path = pathn[index]++;\
00189 } else {\
00190
00191 \
00192 pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
00193 if (ssd >= nodes_next[index][pos]->ssd)\
00194 continue;\
00195 heap_pos[index]++;\
00196 node = nodes_next[index][pos];\
00197 }\
00198 node->ssd = ssd;\
00199 node->state = cur_node->state;\
00200 UPDATE;\
00201 c->paths[index][node->path].value = VALUE;\
00202 c->paths[index][node->path].prev = cur_node->path;\
00203
00204 \
00205 while (pos > 0) {\
00206 int parent = (pos - 1) >> 1;\
00207 if (nodes_next[index][parent]->ssd <= ssd)\
00208 break;\
00209 FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
00210 nodes_next[index][pos]);\
00211 pos = parent;\
00212 }
00213 STORE_NODE(0, ff_g722_update_low_predictor(&node->state, k >> 2), k);
00214 }
00215 }
00216
00217 for (j = 0; j < frontier && nodes[1][j]; j++) {
00218 int ihigh;
00219 struct TrellisNode *cur_node = nodes[1][j];
00220
00221
00222
00223
00224
00225 for (ihigh = 0; ihigh < 4; ihigh++) {
00226 int dhigh, decoded, dec_diff, pos;
00227 uint32_t ssd;
00228 struct TrellisNode* node;
00229
00230 dhigh = cur_node->state.scale_factor *
00231 ff_g722_high_inv_quant[ihigh] >> 10;
00232 decoded = av_clip(dhigh + cur_node->state.s_predictor,
00233 -16384, 16383);
00234 dec_diff = xhigh - decoded;
00235
00236 STORE_NODE(1, ff_g722_update_high_predictor(&node->state, dhigh, ihigh), ihigh);
00237 }
00238 }
00239
00240 for (j = 0; j < 2; j++) {
00241 FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
00242
00243 if (nodes[j][0]->ssd > (1 << 16)) {
00244 for (k = 1; k < frontier && nodes[j][k]; k++)
00245 nodes[j][k]->ssd -= nodes[j][0]->ssd;
00246 nodes[j][0]->ssd = 0;
00247 }
00248 }
00249
00250 if (i == froze + FREEZE_INTERVAL) {
00251 p[0] = &c->paths[0][nodes[0][0]->path];
00252 p[1] = &c->paths[1][nodes[1][0]->path];
00253 for (j = i; j > froze; j--) {
00254 dst[j] = p[1]->value << 6 | p[0]->value;
00255 p[0] = &c->paths[0][p[0]->prev];
00256 p[1] = &c->paths[1][p[1]->prev];
00257 }
00258 froze = i;
00259 pathn[0] = pathn[1] = 0;
00260 memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
00261 memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
00262 }
00263 }
00264
00265 p[0] = &c->paths[0][nodes[0][0]->path];
00266 p[1] = &c->paths[1][nodes[1][0]->path];
00267 for (j = i; j > froze; j--) {
00268 dst[j] = p[1]->value << 6 | p[0]->value;
00269 p[0] = &c->paths[0][p[0]->prev];
00270 p[1] = &c->paths[1][p[1]->prev];
00271 }
00272 c->band[0] = nodes[0][0]->state;
00273 c->band[1] = nodes[1][0]->state;
00274
00275 return i;
00276 }
00277
00278 static int g722_encode_frame(AVCodecContext *avctx,
00279 uint8_t *dst, int buf_size, void *data)
00280 {
00281 G722Context *c = avctx->priv_data;
00282 const int16_t *samples = data;
00283 int i;
00284
00285 if (avctx->trellis)
00286 return g722_encode_trellis(avctx, dst, buf_size, data);
00287
00288 for (i = 0; i < buf_size; i++) {
00289 int xlow, xhigh, ihigh, ilow;
00290 filter_samples(c, &samples[2*i], &xlow, &xhigh);
00291 ihigh = encode_high(&c->band[1], xhigh);
00292 ilow = encode_low(&c->band[0], xlow);
00293 ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
00294 ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
00295 ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
00296 *dst++ = ihigh << 6 | ilow;
00297 }
00298 return i;
00299 }
00300
00301 AVCodec ff_adpcm_g722_encoder = {
00302 .name = "g722",
00303 .type = AVMEDIA_TYPE_AUDIO,
00304 .id = CODEC_ID_ADPCM_G722,
00305 .priv_data_size = sizeof(G722Context),
00306 .init = g722_encode_init,
00307 .close = g722_encode_close,
00308 .encode = g722_encode_frame,
00309 .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
00310 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00311 };