annotate nellymoserenc.c @ 10314:ab687351bfef libavcodec

WMA: store level_table as floats, use type punning for sign flip in decode
author mru
date Tue, 29 Sep 2009 10:38:34 +0000
parents 38ab367d4231
children f175e643acff
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 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8465
diff changeset
25 * @file libavcodec/nellymoserenc.c
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"
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
41
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
42 #define BITSTREAM_WRITER_LE
9411
4cb7c65fc775 Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents: 9083
diff changeset
43 #include "put_bits.h"
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
44
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
45 #define POW_TABLE_SIZE (1<<11)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
46 #define POW_TABLE_OFFSET 3
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
47 #define OPT_SIZE ((1<<15) + 3000)
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
48
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
49 typedef struct NellyMoserEncodeContext {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
50 AVCodecContext *avctx;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
51 int last_frame;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
52 int bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
53 int have_saved;
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
54 DSPContext dsp;
10199
38ab367d4231 Merge FFTContext and MDCTContext
mru
parents: 9707
diff changeset
55 FFTContext mdct_ctx;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
56 DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]);
9707
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
57 DECLARE_ALIGNED_16(float, in_buff[NELLY_SAMPLES]);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
58 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
59 float (*opt )[NELLY_BANDS];
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
60 uint8_t (*path)[NELLY_BANDS];
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
61 } NellyMoserEncodeContext;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
62
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
63 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
64
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
65 static const uint8_t sf_lut[96] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
66 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
67 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
68 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
69 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
70 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
71 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
72 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
73
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
74 static const uint8_t sf_delta_lut[78] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
75 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
76 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
77 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
78 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
79 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
80 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
81
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
82 static const uint8_t quant_lut[230] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
83 0,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
84
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
85 0, 1, 2,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
86
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
87 0, 1, 2, 3, 4, 5, 6,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
88
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
89 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
90 12, 13, 13, 13, 14,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
91
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
92 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
93 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
94 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
95 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
96
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 61, 61, 61, 61, 62,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
107 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
108
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
109 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
110 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
111 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
112
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
113 void apply_mdct(NellyMoserEncodeContext *s)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
114 {
9707
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
115 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
116 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
117 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
118 NELLY_BUF_LEN);
9707
b69723e55653 Move a DECLARE_ALIGNED_16 variable in the Nellymoser encoder from the stack
reimar
parents: 9658
diff changeset
119 ff_mdct_calc(&s->mdct_ctx, s->mdct_out, s->in_buff);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
120
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
121 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
122 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
123 NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
124 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
125 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
126
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
127 static av_cold int encode_init(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
128 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
129 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
130 int i;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
131
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
132 if (avctx->channels != 1) {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
133 av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
134 return -1;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
135 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
136
9627
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
137 if (avctx->sample_rate != 8000 && avctx->sample_rate != 16000 &&
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
138 avctx->sample_rate != 11025 &&
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
139 avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
140 avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
9627
b10d954527a2 Support 16K samplerate in Nellymoser.
diego
parents: 9411
diff changeset
141 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
142 return -1;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
143 }
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
144
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
145 avctx->frame_size = NELLY_SAMPLES;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
146 s->avctx = avctx;
9658
67a20f0eb42c Support for getting (i)MDCT output multiplied by a constant scaling factor.
serge
parents: 9627
diff changeset
147 ff_mdct_init(&s->mdct_ctx, 8, 0, 1.0);
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
148 dsputil_init(&s->dsp, avctx);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
149
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
150 /* Generate overlap window */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
151 ff_sine_window_init(ff_sine_128, 128);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
152 for (i = 0; i < POW_TABLE_SIZE; i++)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
153 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
154
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
155 if (s->avctx->trellis) {
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
156 s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
157 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
158 }
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
159
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
160 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
161 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
162
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
163 static av_cold int encode_end(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
164 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
165 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
166
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
167 ff_mdct_end(&s->mdct_ctx);
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
168
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
169 if (s->avctx->trellis) {
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
170 av_free(s->opt);
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
171 av_free(s->path);
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
172 }
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
173
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
174 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
175 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
176
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
177 #define find_best(val, table, LUT, LUT_add, LUT_size) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
178 best_idx = \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
179 LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
180 if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
181 best_idx++;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
182
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
183 static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
184 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
185 int band, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
186 float power_candidate;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
187
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
188 //base exponent
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
189 find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
190 idx_table[0] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
191 power_idx = ff_nelly_init_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
192
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
193 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
194 power_candidate = cand[band] - power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
195 find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
196 idx_table[band] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
197 power_idx += ff_nelly_delta_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
198 }
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 static inline float distance(float x, float y, int band)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
202 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
203 //return pow(fabs(x-y), 2.0);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
204 float tmp = x - y;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
205 return tmp * tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
206 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
207
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
208 static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
209 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
210 int i, j, band, best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
211 float power_candidate, best_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
212
8731
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
213 float (*opt )[NELLY_BANDS] = s->opt ;
6a1ef00ca991 Allocate trellis tables on heap only when needed.
banan
parents: 8718
diff changeset
214 uint8_t(*path)[NELLY_BANDS] = s->path;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
215
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
216 for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
217 opt[0][i] = INFINITY;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
218 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
219
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
220 for (i = 0; i < 64; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
221 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
222 path[0][ff_nelly_init_table[i]] = i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
223 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
224
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
225 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
226 int q, c = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
227 float tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
228 int idx_min, idx_max, idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
229 power_candidate = cand[band];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
230 for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
231 idx_min = FFMAX(0, cand[band] - q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
232 idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
233 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
234 if ( isinf(opt[band - 1][i]) )
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
235 continue;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
236 for (j = 0; j < 32; j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
237 idx = i + ff_nelly_delta_table[j];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
238 if (idx > idx_max)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
239 break;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
240 if (idx >= idx_min) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
241 tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
242 if (opt[band][idx] > tmp) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
243 opt[band][idx] = tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
244 path[band][idx] = j;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
245 c = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
246 }
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 assert(c); //FIXME
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
252 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
253
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
254 best_val = INFINITY;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
255 best_idx = -1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
256 band = NELLY_BANDS - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
257 for (i = 0; i < OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
258 if (best_val > opt[band][i]) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
259 best_val = opt[band][i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
260 best_idx = i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
261 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
262 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
263 for (band = NELLY_BANDS - 1; band >= 0; band--) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
264 idx_table[band] = path[band][best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
265 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
266 best_idx -= ff_nelly_delta_table[path[band][best_idx]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
267 }
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 * 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
273 * @param s encoder context
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
274 * @param output output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
275 * @param output_size size of output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
276 */
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
277 static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
278 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
279 PutBitContext pb;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
280 int i, j, band, block, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
281 float power_val, coeff, coeff_sum;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
282 float pows[NELLY_FILL_LEN];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
283 int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
284 float cand[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
285
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
286 apply_mdct(s);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
287
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
288 init_put_bits(&pb, output, output_size * 8);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
289
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
290 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
291 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
292 coeff_sum = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
293 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
294 coeff_sum += s->mdct_out[i ] * s->mdct_out[i ]
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
295 + 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
296 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
297 cand[band] =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
298 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
299 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
300
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
301 if (s->avctx->trellis) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
302 get_exponent_dynamic(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
303 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
304 get_exponent_greedy(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
305 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
306
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
307 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
308 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
309 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
310 power_idx += ff_nelly_delta_table[idx_table[band]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
311 put_bits(&pb, 5, idx_table[band]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
312 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
313 power_idx = ff_nelly_init_table[idx_table[0]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
314 put_bits(&pb, 6, idx_table[0]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
315 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
316 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
317 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
318 s->mdct_out[i] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
319 s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
320 pows[i] = power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
321 }
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 ff_nelly_get_sample_bits(pows, bits);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
325
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
326 for (block = 0; block < 2; block++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
327 for (i = 0; i < NELLY_FILL_LEN; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
328 if (bits[i] > 0) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
329 const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
330 coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
331 best_idx =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
332 quant_lut[av_clip (
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
333 coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
334 quant_lut_offset[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
335 quant_lut_offset[bits[i]+1] - 1
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
336 )];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
337 if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
338 best_idx++;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
339
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
340 put_bits(&pb, bits[i], best_idx);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
341 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
342 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
343 if (!block)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
344 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
345 }
8019
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
346
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
347 flush_put_bits(&pb);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
348 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
349
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
350 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
351 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
352 NellyMoserEncodeContext *s = avctx->priv_data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
353 int16_t *samples = data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
354 int i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
355
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
356 if (s->last_frame)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
357 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
358
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
359 if (data) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
360 for (i = 0; i < avctx->frame_size; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
361 s->buf[s->bufsel][i] = samples[i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
362 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
363 for (; i < NELLY_SAMPLES; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
364 s->buf[s->bufsel][i] = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
365 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
366 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
367 if (!s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
368 s->have_saved = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
369 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
370 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
371 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
372 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
373 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
374 s->last_frame = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
375 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
376
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
377 if (s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
378 encode_block(s, frame, buf_size);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
379 return NELLY_BLOCK_LEN;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
380 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
381 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
382 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
383
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
384 AVCodec nellymoser_encoder = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
385 .name = "nellymoser",
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
386 .type = CODEC_TYPE_AUDIO,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
387 .id = CODEC_ID_NELLYMOSER,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
388 .priv_data_size = sizeof(NellyMoserEncodeContext),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
389 .init = encode_init,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
390 .encode = encode_frame,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
391 .close = encode_end,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
392 .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
393 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
394 };