annotate ffmpeg/libavcodec/g722enc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) CMU 1993 Computer Science, Speech Group
yading@10 3 * Chengxiang Lu and Alex Hauptmann
yading@10 4 * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
yading@10 5 * Copyright (c) 2009 Kenan Gillet
yading@10 6 * Copyright (c) 2010 Martin Storsjo
yading@10 7 *
yading@10 8 * This file is part of Libav.
yading@10 9 *
yading@10 10 * Libav is free software; you can redistribute it and/or
yading@10 11 * modify it under the terms of the GNU Lesser General Public
yading@10 12 * License as published by the Free Software Foundation; either
yading@10 13 * version 2.1 of the License, or (at your option) any later version.
yading@10 14 *
yading@10 15 * Libav is distributed in the hope that it will be useful,
yading@10 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 18 * Lesser General Public License for more details.
yading@10 19 *
yading@10 20 * You should have received a copy of the GNU Lesser General Public
yading@10 21 * License along with Libav; if not, write to the Free Software
yading@10 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 23 */
yading@10 24
yading@10 25 /**
yading@10 26 * @file
yading@10 27 * G.722 ADPCM audio encoder
yading@10 28 */
yading@10 29
yading@10 30 #include "libavutil/avassert.h"
yading@10 31 #include "avcodec.h"
yading@10 32 #include "internal.h"
yading@10 33 #include "g722.h"
yading@10 34 #include "libavutil/common.h"
yading@10 35
yading@10 36 #define FREEZE_INTERVAL 128
yading@10 37
yading@10 38 /* This is an arbitrary value. Allowing insanely large values leads to strange
yading@10 39 problems, so we limit it to a reasonable value */
yading@10 40 #define MAX_FRAME_SIZE 32768
yading@10 41
yading@10 42 /* We clip the value of avctx->trellis to prevent data type overflows and
yading@10 43 undefined behavior. Using larger values is insanely slow anyway. */
yading@10 44 #define MIN_TRELLIS 0
yading@10 45 #define MAX_TRELLIS 16
yading@10 46
yading@10 47 static av_cold int g722_encode_close(AVCodecContext *avctx)
yading@10 48 {
yading@10 49 G722Context *c = avctx->priv_data;
yading@10 50 int i;
yading@10 51 for (i = 0; i < 2; i++) {
yading@10 52 av_freep(&c->paths[i]);
yading@10 53 av_freep(&c->node_buf[i]);
yading@10 54 av_freep(&c->nodep_buf[i]);
yading@10 55 }
yading@10 56 return 0;
yading@10 57 }
yading@10 58
yading@10 59 static av_cold int g722_encode_init(AVCodecContext * avctx)
yading@10 60 {
yading@10 61 G722Context *c = avctx->priv_data;
yading@10 62 int ret;
yading@10 63
yading@10 64 if (avctx->channels != 1) {
yading@10 65 av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
yading@10 66 return AVERROR_INVALIDDATA;
yading@10 67 }
yading@10 68
yading@10 69 c->band[0].scale_factor = 8;
yading@10 70 c->band[1].scale_factor = 2;
yading@10 71 c->prev_samples_pos = 22;
yading@10 72
yading@10 73 if (avctx->trellis) {
yading@10 74 int frontier = 1 << avctx->trellis;
yading@10 75 int max_paths = frontier * FREEZE_INTERVAL;
yading@10 76 int i;
yading@10 77 for (i = 0; i < 2; i++) {
yading@10 78 c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
yading@10 79 c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
yading@10 80 c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
yading@10 81 if (!c->paths[i] || !c->node_buf[i] || !c->nodep_buf[i]) {
yading@10 82 ret = AVERROR(ENOMEM);
yading@10 83 goto error;
yading@10 84 }
yading@10 85 }
yading@10 86 }
yading@10 87
yading@10 88 if (avctx->frame_size) {
yading@10 89 /* validate frame size */
yading@10 90 if (avctx->frame_size & 1 || avctx->frame_size > MAX_FRAME_SIZE) {
yading@10 91 int new_frame_size;
yading@10 92
yading@10 93 if (avctx->frame_size == 1)
yading@10 94 new_frame_size = 2;
yading@10 95 else if (avctx->frame_size > MAX_FRAME_SIZE)
yading@10 96 new_frame_size = MAX_FRAME_SIZE;
yading@10 97 else
yading@10 98 new_frame_size = avctx->frame_size - 1;
yading@10 99
yading@10 100 av_log(avctx, AV_LOG_WARNING, "Requested frame size is not "
yading@10 101 "allowed. Using %d instead of %d\n", new_frame_size,
yading@10 102 avctx->frame_size);
yading@10 103 avctx->frame_size = new_frame_size;
yading@10 104 }
yading@10 105 } else {
yading@10 106 /* This is arbitrary. We use 320 because it's 20ms @ 16kHz, which is
yading@10 107 a common packet size for VoIP applications */
yading@10 108 avctx->frame_size = 320;
yading@10 109 }
yading@10 110 avctx->delay = 22;
yading@10 111
yading@10 112 if (avctx->trellis) {
yading@10 113 /* validate trellis */
yading@10 114 if (avctx->trellis < MIN_TRELLIS || avctx->trellis > MAX_TRELLIS) {
yading@10 115 int new_trellis = av_clip(avctx->trellis, MIN_TRELLIS, MAX_TRELLIS);
yading@10 116 av_log(avctx, AV_LOG_WARNING, "Requested trellis value is not "
yading@10 117 "allowed. Using %d instead of %d\n", new_trellis,
yading@10 118 avctx->trellis);
yading@10 119 avctx->trellis = new_trellis;
yading@10 120 }
yading@10 121 }
yading@10 122
yading@10 123 return 0;
yading@10 124 error:
yading@10 125 g722_encode_close(avctx);
yading@10 126 return ret;
yading@10 127 }
yading@10 128
yading@10 129 static const int16_t low_quant[33] = {
yading@10 130 35, 72, 110, 150, 190, 233, 276, 323,
yading@10 131 370, 422, 473, 530, 587, 650, 714, 786,
yading@10 132 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
yading@10 133 1765, 1980, 2195, 2557, 2919
yading@10 134 };
yading@10 135
yading@10 136 static inline void filter_samples(G722Context *c, const int16_t *samples,
yading@10 137 int *xlow, int *xhigh)
yading@10 138 {
yading@10 139 int xout1, xout2;
yading@10 140 c->prev_samples[c->prev_samples_pos++] = samples[0];
yading@10 141 c->prev_samples[c->prev_samples_pos++] = samples[1];
yading@10 142 ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
yading@10 143 *xlow = xout1 + xout2 >> 14;
yading@10 144 *xhigh = xout1 - xout2 >> 14;
yading@10 145 if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
yading@10 146 memmove(c->prev_samples,
yading@10 147 c->prev_samples + c->prev_samples_pos - 22,
yading@10 148 22 * sizeof(c->prev_samples[0]));
yading@10 149 c->prev_samples_pos = 22;
yading@10 150 }
yading@10 151 }
yading@10 152
yading@10 153 static inline int encode_high(const struct G722Band *state, int xhigh)
yading@10 154 {
yading@10 155 int diff = av_clip_int16(xhigh - state->s_predictor);
yading@10 156 int pred = 141 * state->scale_factor >> 8;
yading@10 157 /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
yading@10 158 return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
yading@10 159 }
yading@10 160
yading@10 161 static inline int encode_low(const struct G722Band* state, int xlow)
yading@10 162 {
yading@10 163 int diff = av_clip_int16(xlow - state->s_predictor);
yading@10 164 /* = diff >= 0 ? diff : -(diff + 1) */
yading@10 165 int limit = diff ^ (diff >> (sizeof(diff)*8-1));
yading@10 166 int i = 0;
yading@10 167 limit = limit + 1 << 10;
yading@10 168 if (limit > low_quant[8] * state->scale_factor)
yading@10 169 i = 9;
yading@10 170 while (i < 29 && limit > low_quant[i] * state->scale_factor)
yading@10 171 i++;
yading@10 172 return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
yading@10 173 }
yading@10 174
yading@10 175 static void g722_encode_trellis(G722Context *c, int trellis,
yading@10 176 uint8_t *dst, int nb_samples,
yading@10 177 const int16_t *samples)
yading@10 178 {
yading@10 179 int i, j, k;
yading@10 180 int frontier = 1 << trellis;
yading@10 181 struct TrellisNode **nodes[2];
yading@10 182 struct TrellisNode **nodes_next[2];
yading@10 183 int pathn[2] = {0, 0}, froze = -1;
yading@10 184 struct TrellisPath *p[2];
yading@10 185
yading@10 186 for (i = 0; i < 2; i++) {
yading@10 187 nodes[i] = c->nodep_buf[i];
yading@10 188 nodes_next[i] = c->nodep_buf[i] + frontier;
yading@10 189 memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf[i]));
yading@10 190 nodes[i][0] = c->node_buf[i] + frontier;
yading@10 191 nodes[i][0]->ssd = 0;
yading@10 192 nodes[i][0]->path = 0;
yading@10 193 nodes[i][0]->state = c->band[i];
yading@10 194 }
yading@10 195
yading@10 196 for (i = 0; i < nb_samples >> 1; i++) {
yading@10 197 int xlow, xhigh;
yading@10 198 struct TrellisNode *next[2];
yading@10 199 int heap_pos[2] = {0, 0};
yading@10 200
yading@10 201 for (j = 0; j < 2; j++) {
yading@10 202 next[j] = c->node_buf[j] + frontier*(i & 1);
yading@10 203 memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
yading@10 204 }
yading@10 205
yading@10 206 filter_samples(c, &samples[2*i], &xlow, &xhigh);
yading@10 207
yading@10 208 for (j = 0; j < frontier && nodes[0][j]; j++) {
yading@10 209 /* Only k >> 2 affects the future adaptive state, therefore testing
yading@10 210 * small steps that don't change k >> 2 is useless, the original
yading@10 211 * value from encode_low is better than them. Since we step k
yading@10 212 * in steps of 4, make sure range is a multiple of 4, so that
yading@10 213 * we don't miss the original value from encode_low. */
yading@10 214 int range = j < frontier/2 ? 4 : 0;
yading@10 215 struct TrellisNode *cur_node = nodes[0][j];
yading@10 216
yading@10 217 int ilow = encode_low(&cur_node->state, xlow);
yading@10 218
yading@10 219 for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
yading@10 220 int decoded, dec_diff, pos;
yading@10 221 uint32_t ssd;
yading@10 222 struct TrellisNode* node;
yading@10 223
yading@10 224 if (k < 0)
yading@10 225 continue;
yading@10 226
yading@10 227 decoded = av_clip((cur_node->state.scale_factor *
yading@10 228 ff_g722_low_inv_quant6[k] >> 10)
yading@10 229 + cur_node->state.s_predictor, -16384, 16383);
yading@10 230 dec_diff = xlow - decoded;
yading@10 231
yading@10 232 #define STORE_NODE(index, UPDATE, VALUE)\
yading@10 233 ssd = cur_node->ssd + dec_diff*dec_diff;\
yading@10 234 /* Check for wraparound. Using 64 bit ssd counters would \
yading@10 235 * be simpler, but is slower on x86 32 bit. */\
yading@10 236 if (ssd < cur_node->ssd)\
yading@10 237 continue;\
yading@10 238 if (heap_pos[index] < frontier) {\
yading@10 239 pos = heap_pos[index]++;\
yading@10 240 av_assert2(pathn[index] < FREEZE_INTERVAL * frontier);\
yading@10 241 node = nodes_next[index][pos] = next[index]++;\
yading@10 242 node->path = pathn[index]++;\
yading@10 243 } else {\
yading@10 244 /* Try to replace one of the leaf nodes with the new \
yading@10 245 * one, but not always testing the same leaf position */\
yading@10 246 pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
yading@10 247 if (ssd >= nodes_next[index][pos]->ssd)\
yading@10 248 continue;\
yading@10 249 heap_pos[index]++;\
yading@10 250 node = nodes_next[index][pos];\
yading@10 251 }\
yading@10 252 node->ssd = ssd;\
yading@10 253 node->state = cur_node->state;\
yading@10 254 UPDATE;\
yading@10 255 c->paths[index][node->path].value = VALUE;\
yading@10 256 c->paths[index][node->path].prev = cur_node->path;\
yading@10 257 /* Sift the newly inserted node up in the heap to restore \
yading@10 258 * the heap property */\
yading@10 259 while (pos > 0) {\
yading@10 260 int parent = (pos - 1) >> 1;\
yading@10 261 if (nodes_next[index][parent]->ssd <= ssd)\
yading@10 262 break;\
yading@10 263 FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
yading@10 264 nodes_next[index][pos]);\
yading@10 265 pos = parent;\
yading@10 266 }
yading@10 267 STORE_NODE(0, ff_g722_update_low_predictor(&node->state, k >> 2), k);
yading@10 268 }
yading@10 269 }
yading@10 270
yading@10 271 for (j = 0; j < frontier && nodes[1][j]; j++) {
yading@10 272 int ihigh;
yading@10 273 struct TrellisNode *cur_node = nodes[1][j];
yading@10 274
yading@10 275 /* We don't try to get any initial guess for ihigh via
yading@10 276 * encode_high - since there's only 4 possible values, test
yading@10 277 * them all. Testing all of these gives a much, much larger
yading@10 278 * gain than testing a larger range around ilow. */
yading@10 279 for (ihigh = 0; ihigh < 4; ihigh++) {
yading@10 280 int dhigh, decoded, dec_diff, pos;
yading@10 281 uint32_t ssd;
yading@10 282 struct TrellisNode* node;
yading@10 283
yading@10 284 dhigh = cur_node->state.scale_factor *
yading@10 285 ff_g722_high_inv_quant[ihigh] >> 10;
yading@10 286 decoded = av_clip(dhigh + cur_node->state.s_predictor,
yading@10 287 -16384, 16383);
yading@10 288 dec_diff = xhigh - decoded;
yading@10 289
yading@10 290 STORE_NODE(1, ff_g722_update_high_predictor(&node->state, dhigh, ihigh), ihigh);
yading@10 291 }
yading@10 292 }
yading@10 293
yading@10 294 for (j = 0; j < 2; j++) {
yading@10 295 FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
yading@10 296
yading@10 297 if (nodes[j][0]->ssd > (1 << 16)) {
yading@10 298 for (k = 1; k < frontier && nodes[j][k]; k++)
yading@10 299 nodes[j][k]->ssd -= nodes[j][0]->ssd;
yading@10 300 nodes[j][0]->ssd = 0;
yading@10 301 }
yading@10 302 }
yading@10 303
yading@10 304 if (i == froze + FREEZE_INTERVAL) {
yading@10 305 p[0] = &c->paths[0][nodes[0][0]->path];
yading@10 306 p[1] = &c->paths[1][nodes[1][0]->path];
yading@10 307 for (j = i; j > froze; j--) {
yading@10 308 dst[j] = p[1]->value << 6 | p[0]->value;
yading@10 309 p[0] = &c->paths[0][p[0]->prev];
yading@10 310 p[1] = &c->paths[1][p[1]->prev];
yading@10 311 }
yading@10 312 froze = i;
yading@10 313 pathn[0] = pathn[1] = 0;
yading@10 314 memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
yading@10 315 memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
yading@10 316 }
yading@10 317 }
yading@10 318
yading@10 319 p[0] = &c->paths[0][nodes[0][0]->path];
yading@10 320 p[1] = &c->paths[1][nodes[1][0]->path];
yading@10 321 for (j = i; j > froze; j--) {
yading@10 322 dst[j] = p[1]->value << 6 | p[0]->value;
yading@10 323 p[0] = &c->paths[0][p[0]->prev];
yading@10 324 p[1] = &c->paths[1][p[1]->prev];
yading@10 325 }
yading@10 326 c->band[0] = nodes[0][0]->state;
yading@10 327 c->band[1] = nodes[1][0]->state;
yading@10 328 }
yading@10 329
yading@10 330 static av_always_inline void encode_byte(G722Context *c, uint8_t *dst,
yading@10 331 const int16_t *samples)
yading@10 332 {
yading@10 333 int xlow, xhigh, ilow, ihigh;
yading@10 334 filter_samples(c, samples, &xlow, &xhigh);
yading@10 335 ihigh = encode_high(&c->band[1], xhigh);
yading@10 336 ilow = encode_low (&c->band[0], xlow);
yading@10 337 ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
yading@10 338 ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
yading@10 339 ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
yading@10 340 *dst = ihigh << 6 | ilow;
yading@10 341 }
yading@10 342
yading@10 343 static void g722_encode_no_trellis(G722Context *c,
yading@10 344 uint8_t *dst, int nb_samples,
yading@10 345 const int16_t *samples)
yading@10 346 {
yading@10 347 int i;
yading@10 348 for (i = 0; i < nb_samples; i += 2)
yading@10 349 encode_byte(c, dst++, &samples[i]);
yading@10 350 }
yading@10 351
yading@10 352 static int g722_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 353 const AVFrame *frame, int *got_packet_ptr)
yading@10 354 {
yading@10 355 G722Context *c = avctx->priv_data;
yading@10 356 const int16_t *samples = (const int16_t *)frame->data[0];
yading@10 357 int nb_samples, out_size, ret;
yading@10 358
yading@10 359 out_size = (frame->nb_samples + 1) / 2;
yading@10 360 if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)) < 0)
yading@10 361 return ret;
yading@10 362
yading@10 363 nb_samples = frame->nb_samples - (frame->nb_samples & 1);
yading@10 364
yading@10 365 if (avctx->trellis)
yading@10 366 g722_encode_trellis(c, avctx->trellis, avpkt->data, nb_samples, samples);
yading@10 367 else
yading@10 368 g722_encode_no_trellis(c, avpkt->data, nb_samples, samples);
yading@10 369
yading@10 370 /* handle last frame with odd frame_size */
yading@10 371 if (nb_samples < frame->nb_samples) {
yading@10 372 int16_t last_samples[2] = { samples[nb_samples], samples[nb_samples] };
yading@10 373 encode_byte(c, &avpkt->data[nb_samples >> 1], last_samples);
yading@10 374 }
yading@10 375
yading@10 376 if (frame->pts != AV_NOPTS_VALUE)
yading@10 377 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
yading@10 378 *got_packet_ptr = 1;
yading@10 379 return 0;
yading@10 380 }
yading@10 381
yading@10 382 AVCodec ff_adpcm_g722_encoder = {
yading@10 383 .name = "g722",
yading@10 384 .type = AVMEDIA_TYPE_AUDIO,
yading@10 385 .id = AV_CODEC_ID_ADPCM_G722,
yading@10 386 .priv_data_size = sizeof(G722Context),
yading@10 387 .init = g722_encode_init,
yading@10 388 .close = g722_encode_close,
yading@10 389 .encode2 = g722_encode_frame,
yading@10 390 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
yading@10 391 .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
yading@10 392 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
yading@10 393 AV_SAMPLE_FMT_NONE },
yading@10 394 };