]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/nellymoserenc.c
Okayed parts of nellymoserenc.c
[frescor/ffmpeg.git] / libavcodec / nellymoserenc.c
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;
51 } NellyMoserEncodeContext;
52
53 static float pow_table[POW_TABLE_SIZE];     ///< -pow(2, -i / 2048.0 - 3.0);
54
55 static const uint8_t sf_lut[96] = {
56      0,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  4,  4,
57      5,  5,  5,  6,  7,  7,  8,  8,  9, 10, 11, 11, 12, 13, 13, 14,
58     15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
59     27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
60     41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
61     54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
62 };
63
64 static const uint8_t sf_delta_lut[78] = {
65      0,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  4,  4,
66      4,  5,  5,  5,  6,  6,  7,  7,  8,  8,  9, 10, 10, 11, 11, 12,
67     13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
68     23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
69     28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
70 };
71
72 static const uint8_t quant_lut[230] = {
73      0,
74
75      0,  1,  2,
76
77      0,  1,  2,  3,  4,  5,  6,
78
79      0,  1,  1,  2,  2,  3,  3,  4,  5,  6,  7,  8,  9, 10, 11, 11,
80     12, 13, 13, 13, 14,
81
82      0,  1,  1,  2,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  8,
83      8,  9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
84     22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
85     30,
86
87      0,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3,  3,  3,
88      4,  4,  4,  5,  5,  5,  6,  6,  7,  7,  7,  8,  8,  9,  9,  9,
89     10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
90     15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
91     21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
92     33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
93     46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
94     53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
95     58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
96     61, 61, 61, 61, 62,
97 };
98
99 static const float quant_lut_mul[7] = { 0.0,  0.0,  2.0,  2.0,  5.0, 12.0,  36.6 };
100 static const float quant_lut_add[7] = { 0.0,  0.0,  2.0,  7.0, 21.0, 56.0, 157.0 };
101 static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
102
103 static av_cold int encode_init(AVCodecContext *avctx)
104 {
105     NellyMoserEncodeContext *s = avctx->priv_data;
106     int i;
107
108     if (avctx->channels != 1) {
109         av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
110         return -1;
111     }
112
113     avctx->frame_size = NELLY_SAMPLES;
114     s->avctx = avctx;
115     ff_mdct_init(&s->mdct_ctx, 8, 0);
116     dsputil_init(&s->dsp, avctx);
117
118     /* Generate overlap window */
119     ff_sine_window_init(ff_sine_128, 128);
120     for (i = 0; i < POW_TABLE_SIZE; i++)
121         pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
122
123     return 0;
124 }
125
126 static av_cold int encode_end(AVCodecContext *avctx)
127 {
128     NellyMoserEncodeContext *s = avctx->priv_data;
129
130     ff_mdct_end(&s->mdct_ctx);
131     return 0;
132 }
133
134 AVCodec nellymoser_encoder = {
135     .name = "nellymoser",
136     .type = CODEC_TYPE_AUDIO,
137     .id = CODEC_ID_NELLYMOSER,
138     .priv_data_size = sizeof(NellyMoserEncodeContext),
139     .init = encode_init,
140     .encode = encode_frame,
141     .close = encode_end,
142     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
143     .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao Codec"),
144 };