7735
|
1 /*
|
|
2 * Nellymoser encoder
|
|
3 * This code is developed as part of Google Summer of Code 2008 Program.
|
|
4 *
|
|
5 * Copyright (c) 2008 Bartlomiej Wolowiec
|
|
6 *
|
|
7 * This file is part of FFmpeg.
|
|
8 *
|
|
9 * FFmpeg is free software; you can redistribute it and/or
|
|
10 * modify it under the terms of the GNU Lesser General Public
|
|
11 * License as published by the Free Software Foundation; either
|
|
12 * version 2.1 of the License, or (at your option) any later version.
|
|
13 *
|
|
14 * FFmpeg is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 * Lesser General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU Lesser General Public
|
|
20 * License along with FFmpeg; if not, write to the Free Software
|
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
22 */
|
|
23
|
|
24 /**
|
|
25 * @file nellymoserenc.c
|
|
26 * Nellymoser encoder
|
|
27 * by Bartlomiej Wolowiec
|
|
28 *
|
|
29 * Generic codec information: libavcodec/nellymoserdec.c
|
|
30 *
|
|
31 * Some information also from: http://www1.mplayerhq.hu/ASAO/ASAO.zip
|
|
32 * (Copyright Joseph Artsimovich and UAB "DKD")
|
|
33 *
|
|
34 * for more information about nellymoser format, visit:
|
|
35 * http://wiki.multimedia.cx/index.php?title=Nellymoser
|
|
36 */
|
|
37
|
|
38 #include "nellymoser.h"
|
|
39 #include "avcodec.h"
|
|
40 #include "dsputil.h"
|
|
41
|
|
42 #define BITSTREAM_WRITER_LE
|
|
43 #include "bitstream.h"
|
|
44
|
|
45 #define POW_TABLE_SIZE (1<<11)
|
|
46 #define POW_TABLE_OFFSET 3
|
|
47
|
|
48 typedef struct NellyMoserEncodeContext {
|
|
49 AVCodecContext *avctx;
|
|
50 int last_frame;
|
7762
|
51 DSPContext dsp;
|
|
52 MDCTContext mdct_ctx;
|
7735
|
53 } NellyMoserEncodeContext;
|
|
54
|
|
55 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
|
|
56
|
|
57 static const uint8_t sf_lut[96] = {
|
|
58 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
|
|
59 5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 13, 13, 14,
|
|
60 15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
|
|
61 27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
|
|
62 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
|
|
63 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
|
|
64 };
|
|
65
|
|
66 static const uint8_t sf_delta_lut[78] = {
|
|
67 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
|
|
68 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10, 11, 11, 12,
|
|
69 13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
|
|
70 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
|
|
71 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
|
|
72 };
|
|
73
|
|
74 static const uint8_t quant_lut[230] = {
|
|
75 0,
|
|
76
|
|
77 0, 1, 2,
|
|
78
|
|
79 0, 1, 2, 3, 4, 5, 6,
|
|
80
|
|
81 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11,
|
|
82 12, 13, 13, 13, 14,
|
|
83
|
|
84 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8,
|
|
85 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
|
|
86 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
|
|
87 30,
|
|
88
|
|
89 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
|
|
90 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,
|
|
91 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
|
|
92 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
|
|
93 21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
|
|
94 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
|
|
95 46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
|
|
96 53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
|
|
97 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
|
|
98 61, 61, 61, 61, 62,
|
|
99 };
|
|
100
|
|
101 static const float quant_lut_mul[7] = { 0.0, 0.0, 2.0, 2.0, 5.0, 12.0, 36.6 };
|
|
102 static const float quant_lut_add[7] = { 0.0, 0.0, 2.0, 7.0, 21.0, 56.0, 157.0 };
|
|
103 static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
|
|
104
|
|
105 static av_cold int encode_init(AVCodecContext *avctx)
|
|
106 {
|
|
107 NellyMoserEncodeContext *s = avctx->priv_data;
|
|
108 int i;
|
|
109
|
|
110 if (avctx->channels != 1) {
|
|
111 av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
|
|
112 return -1;
|
|
113 }
|
|
114
|
7762
|
115 if (avctx->sample_rate != 8000 && avctx->sample_rate != 11025 &&
|
|
116 avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
|
|
117 avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
|
|
118 av_log(avctx, AV_LOG_ERROR, "Nellymoser works only with 8000, 11025, 22050 and 44100 sample rate\n");
|
|
119 return -1;
|
|
120 }
|
|
121
|
7735
|
122 avctx->frame_size = NELLY_SAMPLES;
|
|
123 s->avctx = avctx;
|
|
124 ff_mdct_init(&s->mdct_ctx, 8, 0);
|
|
125 dsputil_init(&s->dsp, avctx);
|
|
126
|
|
127 /* Generate overlap window */
|
|
128 ff_sine_window_init(ff_sine_128, 128);
|
|
129 for (i = 0; i < POW_TABLE_SIZE; i++)
|
|
130 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
|
|
131
|
|
132 return 0;
|
|
133 }
|
|
134
|
|
135 static av_cold int encode_end(AVCodecContext *avctx)
|
|
136 {
|
|
137 NellyMoserEncodeContext *s = avctx->priv_data;
|
|
138
|
|
139 ff_mdct_end(&s->mdct_ctx);
|
|
140 return 0;
|
|
141 }
|
|
142
|
7762
|
143 #define find_best(val, table, LUT, LUT_add, LUT_size) \
|
|
144 best_idx = \
|
|
145 LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
|
|
146 if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
|
|
147 best_idx++;
|
|
148
|
7735
|
149 AVCodec nellymoser_encoder = {
|
|
150 .name = "nellymoser",
|
|
151 .type = CODEC_TYPE_AUDIO,
|
|
152 .id = CODEC_ID_NELLYMOSER,
|
|
153 .priv_data_size = sizeof(NellyMoserEncodeContext),
|
|
154 .init = encode_init,
|
|
155 .encode = encode_frame,
|
|
156 .close = encode_end,
|
|
157 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
|
|
158 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao Codec"),
|
|
159 };
|