annotate nellymoserenc.c @ 11762:91b9bd17e79c libavcodec

aacenc: Trellis over scalefactors using an estimated codebook rather than every codebook. The minimal codebook to encode the band without clipping is used (as is done in the TLS).
author alexc
date Tue, 25 May 2010 18:31:55 +0000
parents 7dd2a45249a9
children fdafbcef52f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
1 /*
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
2 * Nellymoser encoder
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
3 * This code is developed as part of Google Summer of Code 2008 Program.
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
4 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
5 * Copyright (c) 2008 Bartlomiej Wolowiec
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
6 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
7 * This file is part of FFmpeg.
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
8 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
13 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
17 * Lesser General Public License for more details.
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
18 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
22 */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
23
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
24 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
25 * @file
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
26 * Nellymoser encoder
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
27 * by Bartlomiej Wolowiec
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
28 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
29 * Generic codec information: libavcodec/nellymoserdec.c
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
30 *
8465
96aadb5e4fd6 ASAO Nelly Moser implementation moved to a better place on mphq.
diego
parents: 8019
diff changeset
31 * Some information also from: http://samples.mplayerhq.hu/A-codecs/Nelly_Moser/ASAO/ASAO.zip
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
32 * (Copyright Joseph Artsimovich and UAB "DKD")
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
33 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
34 * for more information about nellymoser format, visit:
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
35 * http://wiki.multimedia.cx/index.php?title=Nellymoser
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
36 */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
37
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
38 #include "nellymoser.h"
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
39 #include "avcodec.h"
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
40 #include "dsputil.h"
11370
4b3da727d832 Move FFT parts from dsputil.h to fft.h
mru
parents: 11369
diff changeset
41 #include "fft.h"
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
42
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
43 #define BITSTREAM_WRITER_LE
9411
4cb7c65fc775 Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents: 9083
diff changeset
44 #include "put_bits.h"
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
45
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
46 #define POW_TABLE_SIZE (1<<11)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
47 #define POW_TABLE_OFFSET 3
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
48 #define OPT_SIZE ((1<<15) + 3000)
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
49
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
50 typedef struct NellyMoserEncodeContext {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
51 AVCodecContext *avctx;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
52 int last_frame;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
53 int bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
54 int have_saved;
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
55 DSPContext dsp;
10199
38ab367d4231 Merge FFTContext and MDCTContext
mru
parents: 9707
diff changeset
56 FFTContext mdct_ctx;
11369
98970e51365a Remove DECLARE_ALIGNED_{8,16} macros
mru
parents: 10961
diff changeset
57 DECLARE_ALIGNED(16, float, mdct_out)[NELLY_SAMPLES];
98970e51365a Remove DECLARE_ALIGNED_{8,16} macros
mru
parents: 10961
diff changeset
58 DECLARE_ALIGNED(16, float, in_buff)[NELLY_SAMPLES];
98970e51365a Remove DECLARE_ALIGNED_{8,16} macros
mru
parents: 10961
diff changeset
59 DECLARE_ALIGNED(16, float, buf)[2][3 * NELLY_BUF_LEN]; ///< sample buffer
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
60 float (*opt )[NELLY_BANDS];
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
61 uint8_t (*path)[NELLY_BANDS];
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
62 } NellyMoserEncodeContext;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
63
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
64 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
65
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
66 static const uint8_t sf_lut[96] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
67 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
68 5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 13, 13, 14,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
69 15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
70 27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
71 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
72 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
73 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
74
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
75 static const uint8_t sf_delta_lut[78] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
76 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
77 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10, 11, 11, 12,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
78 13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
79 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
80 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
81 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
82
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
83 static const uint8_t quant_lut[230] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
84 0,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
85
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
86 0, 1, 2,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
87
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
88 0, 1, 2, 3, 4, 5, 6,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
89
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
90 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
91 12, 13, 13, 13, 14,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
92
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
93 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
94 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
95 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
96 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
97
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
98 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
99 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
100 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
101 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
102 21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
103 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
104 46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
105 53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
106 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
107 61, 61, 61, 61, 62,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
108 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
109
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
110 static const float quant_lut_mul[7] = { 0.0, 0.0, 2.0, 2.0, 5.0, 12.0, 36.6 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
111 static const float quant_lut_add[7] = { 0.0, 0.0, 2.0, 7.0, 21.0, 56.0, 157.0 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
112 static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
113
10574
f175e643acff Mark apply_mdct() function as static; it is only used within the file.
diego
parents: 10199
diff changeset
114 static void apply_mdct(NellyMoserEncodeContext *s)
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
115 {
9707
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
116 memcpy(s->in_buff, s->buf[s->bufsel], NELLY_BUF_LEN * sizeof(float));
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
117 s->dsp.vector_fmul(s->in_buff, ff_sine_128, NELLY_BUF_LEN);
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
118 s->dsp.vector_fmul_reverse(s->in_buff + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128,
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
119 NELLY_BUF_LEN);
9707
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
120 ff_mdct_calc(&s->mdct_ctx, s->mdct_out, s->in_buff);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
121
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
122 s->dsp.vector_fmul(s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128, NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
123 s->dsp.vector_fmul_reverse(s->buf[s->bufsel] + 2 * NELLY_BUF_LEN, s->buf[1 - s->bufsel], ff_sine_128,
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
124 NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
125 ff_mdct_calc(&s->mdct_ctx, s->mdct_out + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
126 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
127
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
128 static av_cold int encode_init(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
129 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
130 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
131 int i;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
132
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
133 if (avctx->channels != 1) {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
134 av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
135 return -1;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
136 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
137
9627
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
138 if (avctx->sample_rate != 8000 && avctx->sample_rate != 16000 &&
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
139 avctx->sample_rate != 11025 &&
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
140 avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
141 avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
9627
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
142 av_log(avctx, AV_LOG_ERROR, "Nellymoser works only with 8000, 16000, 11025, 22050 and 44100 sample rate\n");
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
143 return -1;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
144 }
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
145
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
146 avctx->frame_size = NELLY_SAMPLES;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
147 s->avctx = avctx;
9658
67a20f0eb42c Support for getting (i)MDCT output multiplied by a constant scaling factor.
serge
parents: 9627
diff changeset
148 ff_mdct_init(&s->mdct_ctx, 8, 0, 1.0);
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
149 dsputil_init(&s->dsp, avctx);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
150
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
151 /* Generate overlap window */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
152 ff_sine_window_init(ff_sine_128, 128);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
153 for (i = 0; i < POW_TABLE_SIZE; i++)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
154 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
155
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
156 if (s->avctx->trellis) {
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
157 s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
158 s->path = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(uint8_t));
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
159 }
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
160
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
161 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
162 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
163
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
164 static av_cold int encode_end(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
165 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
166 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
167
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
168 ff_mdct_end(&s->mdct_ctx);
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
169
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
170 if (s->avctx->trellis) {
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
171 av_free(s->opt);
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
172 av_free(s->path);
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
173 }
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
174
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
175 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
176 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
177
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
178 #define find_best(val, table, LUT, LUT_add, LUT_size) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
179 best_idx = \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
180 LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
181 if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
182 best_idx++;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
183
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
184 static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
185 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
186 int band, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
187 float power_candidate;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
188
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
189 //base exponent
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
190 find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
191 idx_table[0] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
192 power_idx = ff_nelly_init_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
193
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
194 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
195 power_candidate = cand[band] - power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
196 find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
197 idx_table[band] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
198 power_idx += ff_nelly_delta_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
199 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
200 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
201
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
202 static inline float distance(float x, float y, int band)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
203 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
204 //return pow(fabs(x-y), 2.0);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
205 float tmp = x - y;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
206 return tmp * tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
207 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
208
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
209 static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
210 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
211 int i, j, band, best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
212 float power_candidate, best_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
213
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
214 float (*opt )[NELLY_BANDS] = s->opt ;
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
215 uint8_t(*path)[NELLY_BANDS] = s->path;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
216
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
217 for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
218 opt[0][i] = INFINITY;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
219 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
220
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
221 for (i = 0; i < 64; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
222 opt[0][ff_nelly_init_table[i]] = distance(cand[0], ff_nelly_init_table[i], 0);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
223 path[0][ff_nelly_init_table[i]] = i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
224 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
225
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
226 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
227 int q, c = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
228 float tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
229 int idx_min, idx_max, idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
230 power_candidate = cand[band];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
231 for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
232 idx_min = FFMAX(0, cand[band] - q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
233 idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
234 for (i = FFMAX(0, cand[band - 1] - q); i < FFMIN(OPT_SIZE, cand[band - 1] + q); i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
235 if ( isinf(opt[band - 1][i]) )
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
236 continue;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
237 for (j = 0; j < 32; j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
238 idx = i + ff_nelly_delta_table[j];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
239 if (idx > idx_max)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
240 break;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
241 if (idx >= idx_min) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
242 tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
243 if (opt[band][idx] > tmp) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
244 opt[band][idx] = tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
245 path[band][idx] = j;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
246 c = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
247 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
248 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
249 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
250 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
251 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
252 assert(c); //FIXME
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
253 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
254
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
255 best_val = INFINITY;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
256 best_idx = -1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
257 band = NELLY_BANDS - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
258 for (i = 0; i < OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
259 if (best_val > opt[band][i]) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
260 best_val = opt[band][i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
261 best_idx = i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
262 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
263 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
264 for (band = NELLY_BANDS - 1; band >= 0; band--) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
265 idx_table[band] = path[band][best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
266 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
267 best_idx -= ff_nelly_delta_table[path[band][best_idx]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
268 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
269 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
270 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
271
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
272 /**
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
273 * Encodes NELLY_SAMPLES samples. It assumes, that samples contains 3 * NELLY_BUF_LEN values
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
274 * @param s encoder context
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
275 * @param output output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
276 * @param output_size size of output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
277 */
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
278 static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
279 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
280 PutBitContext pb;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
281 int i, j, band, block, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
282 float power_val, coeff, coeff_sum;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
283 float pows[NELLY_FILL_LEN];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
284 int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
285 float cand[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
286
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
287 apply_mdct(s);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
288
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
289 init_put_bits(&pb, output, output_size * 8);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
290
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
291 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
292 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
293 coeff_sum = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
294 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
295 coeff_sum += s->mdct_out[i ] * s->mdct_out[i ]
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
296 + s->mdct_out[i + NELLY_BUF_LEN] * s->mdct_out[i + NELLY_BUF_LEN];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
297 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
298 cand[band] =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
299 log(FFMAX(1.0, coeff_sum / (ff_nelly_band_sizes_table[band] << 7))) * 1024.0 / M_LN2;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
300 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
301
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
302 if (s->avctx->trellis) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
303 get_exponent_dynamic(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
304 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
305 get_exponent_greedy(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
306 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
307
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
308 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
309 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
310 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
311 power_idx += ff_nelly_delta_table[idx_table[band]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
312 put_bits(&pb, 5, idx_table[band]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
313 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
314 power_idx = ff_nelly_init_table[idx_table[0]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
315 put_bits(&pb, 6, idx_table[0]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
316 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
317 power_val = pow_table[power_idx & 0x7FF] / (1 << ((power_idx >> 11) + POW_TABLE_OFFSET));
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
318 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
319 s->mdct_out[i] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
320 s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
321 pows[i] = power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
322 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
323 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
324
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
325 ff_nelly_get_sample_bits(pows, bits);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
326
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
327 for (block = 0; block < 2; block++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
328 for (i = 0; i < NELLY_FILL_LEN; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
329 if (bits[i] > 0) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
330 const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
331 coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
332 best_idx =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
333 quant_lut[av_clip (
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
334 coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
335 quant_lut_offset[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
336 quant_lut_offset[bits[i]+1] - 1
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
337 )];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
338 if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
339 best_idx++;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
340
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
341 put_bits(&pb, bits[i], best_idx);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
342 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
343 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
344 if (!block)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
345 put_bits(&pb, NELLY_HEADER_BITS + NELLY_DETAIL_BITS - put_bits_count(&pb), 0);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
346 }
8019
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
347
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
348 flush_put_bits(&pb);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
349 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
350
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
351 static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
352 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
353 NellyMoserEncodeContext *s = avctx->priv_data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
354 int16_t *samples = data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
355 int i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
356
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
357 if (s->last_frame)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
358 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
359
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
360 if (data) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
361 for (i = 0; i < avctx->frame_size; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
362 s->buf[s->bufsel][i] = samples[i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
363 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
364 for (; i < NELLY_SAMPLES; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
365 s->buf[s->bufsel][i] = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
366 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
367 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
368 if (!s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
369 s->have_saved = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
370 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
371 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
372 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
373 memset(s->buf[s->bufsel], 0, sizeof(s->buf[0][0]) * NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
374 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
375 s->last_frame = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
376 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
377
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
378 if (s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
379 encode_block(s, frame, buf_size);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
380 return NELLY_BLOCK_LEN;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
381 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
382 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
383 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
384
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
385 AVCodec nellymoser_encoder = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
386 .name = "nellymoser",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 11370
diff changeset
387 .type = AVMEDIA_TYPE_AUDIO,
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
388 .id = CODEC_ID_NELLYMOSER,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
389 .priv_data_size = sizeof(NellyMoserEncodeContext),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
390 .init = encode_init,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
391 .encode = encode_frame,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
392 .close = encode_end,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
393 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
9083
bf274494b66e Change a bunch of codec long_names to be more consistent and descriptive.
diego
parents: 8731
diff changeset
394 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
395 };