comparison ra144dec.c @ 11865:6111134a3d94 libavcodec

Split ra144.c in common code (to be shared with the future encoder) and decoder. Patch by Francesco Lavra (firstnamelastname@interfree.it)
author vitor
date Fri, 11 Jun 2010 08:01:51 +0000
parents ra144.c@8a4984c5cacc
children 0d8f35836b20
comparison
equal deleted inserted replaced
11864:7204cb7dd601 11865:6111134a3d94
1 /*
2 * Real Audio 1.0 (14.4K)
3 *
4 * Copyright (c) 2008 Vitor Sessak
5 * Copyright (c) 2003 Nick Kurshev
6 * Based on public domain decoder at http://www.honeypot.net/audio
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "libavutil/intmath.h"
26 #include "avcodec.h"
27 #include "get_bits.h"
28 #include "ra144.h"
29 #include "celp_filters.h"
30
31
32 static av_cold int ra144_decode_init(AVCodecContext * avctx)
33 {
34 RA144Context *ractx = avctx->priv_data;
35
36 ractx->avctx = avctx;
37
38 ractx->lpc_coef[0] = ractx->lpc_tables[0];
39 ractx->lpc_coef[1] = ractx->lpc_tables[1];
40
41 avctx->sample_fmt = SAMPLE_FMT_S16;
42 return 0;
43 }
44
45 static void do_output_subblock(RA144Context *ractx, const uint16_t *lpc_coefs,
46 int gval, GetBitContext *gb)
47 {
48 uint16_t buffer_a[40];
49 uint16_t *block;
50 int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
51 int gain = get_bits(gb, 8);
52 int cb1_idx = get_bits(gb, 7);
53 int cb2_idx = get_bits(gb, 7);
54 int m[3];
55
56 if (cba_idx) {
57 cba_idx += BLOCKSIZE/2 - 1;
58 copy_and_dup(buffer_a, ractx->adapt_cb, cba_idx);
59 m[0] = (irms(buffer_a) * gval) >> 12;
60 } else {
61 m[0] = 0;
62 }
63
64 m[1] = (cb1_base[cb1_idx] * gval) >> 8;
65 m[2] = (cb2_base[cb2_idx] * gval) >> 8;
66
67 memmove(ractx->adapt_cb, ractx->adapt_cb + BLOCKSIZE,
68 (BUFFERSIZE - BLOCKSIZE) * sizeof(*ractx->adapt_cb));
69
70 block = ractx->adapt_cb + BUFFERSIZE - BLOCKSIZE;
71
72 add_wav(block, gain, cba_idx, m, cba_idx? buffer_a: NULL,
73 cb1_vects[cb1_idx], cb2_vects[cb2_idx]);
74
75 memcpy(ractx->curr_sblock, ractx->curr_sblock + 40,
76 10*sizeof(*ractx->curr_sblock));
77
78 if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + 10, lpc_coefs,
79 block, BLOCKSIZE, 10, 1, 0xfff))
80 memset(ractx->curr_sblock, 0, 50*sizeof(*ractx->curr_sblock));
81 }
82
83 /** Uncompress one block (20 bytes -> 160*2 bytes). */
84 static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
85 int *data_size, AVPacket *avpkt)
86 {
87 const uint8_t *buf = avpkt->data;
88 int buf_size = avpkt->size;
89 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
90 unsigned int refl_rms[4]; // RMS of the reflection coefficients
91 uint16_t block_coefs[4][10]; // LPC coefficients of each sub-block
92 unsigned int lpc_refl[10]; // LPC reflection coefficients of the frame
93 int i, j;
94 int16_t *data = vdata;
95 unsigned int energy;
96
97 RA144Context *ractx = avctx->priv_data;
98 GetBitContext gb;
99
100 if (*data_size < 2*160)
101 return -1;
102
103 if(buf_size < 20) {
104 av_log(avctx, AV_LOG_ERROR,
105 "Frame too small (%d bytes). Truncated file?\n", buf_size);
106 *data_size = 0;
107 return buf_size;
108 }
109 init_get_bits(&gb, buf, 20 * 8);
110
111 for (i=0; i<10; i++)
112 lpc_refl[i] = lpc_refl_cb[i][get_bits(&gb, sizes[i])];
113
114 eval_coefs(ractx->lpc_coef[0], lpc_refl);
115 ractx->lpc_refl_rms[0] = rms(lpc_refl);
116
117 energy = energy_tab[get_bits(&gb, 5)];
118
119 refl_rms[0] = interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
120 refl_rms[1] = interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy,
121 t_sqrt(energy*ractx->old_energy) >> 12);
122 refl_rms[2] = interp(ractx, block_coefs[2], 3, 0, energy);
123 refl_rms[3] = rescale_rms(ractx->lpc_refl_rms[0], energy);
124
125 int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
126
127 for (i=0; i < 4; i++) {
128 do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
129
130 for (j=0; j < BLOCKSIZE; j++)
131 *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);
132 }
133
134 ractx->old_energy = energy;
135 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
136
137 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
138
139 *data_size = 2*160;
140 return 20;
141 }
142
143 AVCodec ra_144_decoder =
144 {
145 "real_144",
146 AVMEDIA_TYPE_AUDIO,
147 CODEC_ID_RA_144,
148 sizeof(RA144Context),
149 ra144_decode_init,
150 NULL,
151 NULL,
152 ra144_decode_frame,
153 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
154 };