Originally committed as revision 25164 to svn://svn.ffmpeg.org/ffmpeg/trunk
Martin Storsjö authored on 2010/09/24 06:33:29... | ... |
@@ -536,7 +536,7 @@ following image formats are supported: |
536 | 536 |
@item ADPCM Electronic Arts R2 @tab @tab X |
537 | 537 |
@item ADPCM Electronic Arts R3 @tab @tab X |
538 | 538 |
@item ADPCM Electronic Arts XAS @tab @tab X |
539 |
-@item ADPCM G.722 @tab @tab X |
|
539 |
+@item ADPCM G.722 @tab X @tab X |
|
540 | 540 |
@item ADPCM G.726 @tab X @tab X |
541 | 541 |
@item ADPCM IMA AMV @tab @tab X |
542 | 542 |
@tab Used in AMV files |
... | ... |
@@ -477,6 +477,7 @@ OBJS-$(CONFIG_ADPCM_EA_R2_DECODER) += adpcm.o |
477 | 477 |
OBJS-$(CONFIG_ADPCM_EA_R3_DECODER) += adpcm.o |
478 | 478 |
OBJS-$(CONFIG_ADPCM_EA_XAS_DECODER) += adpcm.o |
479 | 479 |
OBJS-$(CONFIG_ADPCM_G722_DECODER) += g722.o |
480 |
+OBJS-$(CONFIG_ADPCM_G722_ENCODER) += g722.o |
|
480 | 481 |
OBJS-$(CONFIG_ADPCM_G726_DECODER) += g726.o |
481 | 482 |
OBJS-$(CONFIG_ADPCM_G726_ENCODER) += g726.o |
482 | 483 |
OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER) += adpcm.o |
... | ... |
@@ -318,7 +318,7 @@ void avcodec_register_all(void) |
318 | 318 |
REGISTER_DECODER (ADPCM_EA_R2, adpcm_ea_r2); |
319 | 319 |
REGISTER_DECODER (ADPCM_EA_R3, adpcm_ea_r3); |
320 | 320 |
REGISTER_DECODER (ADPCM_EA_XAS, adpcm_ea_xas); |
321 |
- REGISTER_DECODER (ADPCM_G722, adpcm_g722); |
|
321 |
+ REGISTER_ENCDEC (ADPCM_G722, adpcm_g722); |
|
322 | 322 |
REGISTER_ENCDEC (ADPCM_G726, adpcm_g726); |
323 | 323 |
REGISTER_DECODER (ADPCM_IMA_AMV, adpcm_ima_amv); |
324 | 324 |
REGISTER_DECODER (ADPCM_IMA_DK3, adpcm_ima_dk3); |
... | ... |
@@ -1,5 +1,5 @@ |
1 | 1 |
/* |
2 |
- * G.722 ADPCM audio decoder |
|
2 |
+ * G.722 ADPCM audio encoder/decoder |
|
3 | 3 |
* |
4 | 4 |
* Copyright (c) CMU 1993 Computer Science, Speech Group |
5 | 5 |
* Chengxiang Lu and Alex Hauptmann |
... | ... |
@@ -219,6 +219,7 @@ static av_cold int g722_init(AVCodecContext * avctx) |
219 | 219 |
return 0; |
220 | 220 |
} |
221 | 221 |
|
222 |
+#if CONFIG_ADPCM_G722_DECODER |
|
222 | 223 |
static const int16_t low_inv_quant5[32] = { |
223 | 224 |
-35, -35, -2919, -2195, -1765, -1458, -1219, -1023, |
224 | 225 |
-858, -714, -587, -473, -370, -276, -190, -110, |
... | ... |
@@ -301,4 +302,84 @@ AVCodec adpcm_g722_decoder = { |
301 | 301 |
.long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"), |
302 | 302 |
.max_lowres = 1, |
303 | 303 |
}; |
304 |
+#endif |
|
305 |
+ |
|
306 |
+#if CONFIG_ADPCM_G722_ENCODER |
|
307 |
+static const int16_t low_quant[33] = { |
|
308 |
+ 35, 72, 110, 150, 190, 233, 276, 323, |
|
309 |
+ 370, 422, 473, 530, 587, 650, 714, 786, |
|
310 |
+ 858, 940, 1023, 1121, 1219, 1339, 1458, 1612, |
|
311 |
+ 1765, 1980, 2195, 2557, 2919 |
|
312 |
+}; |
|
313 |
+ |
|
314 |
+static inline void filter_samples(G722Context *c, const int16_t *samples, |
|
315 |
+ int *xlow, int *xhigh) |
|
316 |
+{ |
|
317 |
+ int xout1, xout2; |
|
318 |
+ c->prev_samples[c->prev_samples_pos++] = samples[0]; |
|
319 |
+ c->prev_samples[c->prev_samples_pos++] = samples[1]; |
|
320 |
+ apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2); |
|
321 |
+ *xlow = xout1 + xout2 >> 13; |
|
322 |
+ *xhigh = xout1 - xout2 >> 13; |
|
323 |
+ if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { |
|
324 |
+ memmove(c->prev_samples, |
|
325 |
+ c->prev_samples + c->prev_samples_pos - 22, |
|
326 |
+ 22 * sizeof(c->prev_samples[0])); |
|
327 |
+ c->prev_samples_pos = 22; |
|
328 |
+ } |
|
329 |
+} |
|
330 |
+ |
|
331 |
+static inline int encode_high(const struct G722Band *state, int xhigh) |
|
332 |
+{ |
|
333 |
+ int diff = av_clip_int16(xhigh - state->s_predictor); |
|
334 |
+ int pred = 141 * state->scale_factor >> 8; |
|
335 |
+ /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */ |
|
336 |
+ return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0); |
|
337 |
+} |
|
338 |
+ |
|
339 |
+static inline int encode_low(const struct G722Band* state, int xlow) |
|
340 |
+{ |
|
341 |
+ int diff = av_clip_int16(xlow - state->s_predictor); |
|
342 |
+ /* = diff >= 0 ? diff : -(diff + 1) */ |
|
343 |
+ int limit = diff ^ (diff >> (sizeof(diff)*8-1)); |
|
344 |
+ int i = 0; |
|
345 |
+ limit = limit + 1 << 10; |
|
346 |
+ if (limit > low_quant[8] * state->scale_factor) |
|
347 |
+ i = 9; |
|
348 |
+ while (i < 29 && limit > low_quant[i] * state->scale_factor) |
|
349 |
+ i++; |
|
350 |
+ return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i; |
|
351 |
+} |
|
352 |
+ |
|
353 |
+static int g722_encode_frame(AVCodecContext *avctx, |
|
354 |
+ uint8_t *dst, int buf_size, void *data) |
|
355 |
+{ |
|
356 |
+ G722Context *c = avctx->priv_data; |
|
357 |
+ const int16_t *samples = data; |
|
358 |
+ int i; |
|
359 |
+ |
|
360 |
+ for (i = 0; i < buf_size >> 1; i++) { |
|
361 |
+ int xlow, xhigh, ihigh, ilow; |
|
362 |
+ filter_samples(c, &samples[2*i], &xlow, &xhigh); |
|
363 |
+ ihigh = encode_high(&c->band[1], xhigh); |
|
364 |
+ ilow = encode_low(&c->band[0], xlow); |
|
365 |
+ update_high_predictor(&c->band[1], c->band[1].scale_factor * |
|
366 |
+ high_inv_quant[ihigh] >> 10, ihigh); |
|
367 |
+ update_low_predictor(&c->band[0], ilow >> 2); |
|
368 |
+ *dst++ = ihigh << 6 | ilow; |
|
369 |
+ } |
|
370 |
+ return i; |
|
371 |
+} |
|
372 |
+ |
|
373 |
+AVCodec adpcm_g722_encoder = { |
|
374 |
+ .name = "g722", |
|
375 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
376 |
+ .id = CODEC_ID_ADPCM_G722, |
|
377 |
+ .priv_data_size = sizeof(G722Context), |
|
378 |
+ .init = g722_init, |
|
379 |
+ .encode = g722_encode_frame, |
|
380 |
+ .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"), |
|
381 |
+ .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, |
|
382 |
+}; |
|
383 |
+#endif |
|
304 | 384 |
|