annotate nellymoserenc.c @ 7735:87ec777690c9 libavcodec

Okayed parts of nellymoserenc.c
author bwolowiec
date Fri, 29 Aug 2008 21:35:57 +0000
parents
children e2573d045e79
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 /**
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
25 * @file nellymoserenc.c
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 *
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
31 * Some information also from: http://www1.mplayerhq.hu/ASAO/ASAO.zip
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
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
43 #include "bitstream.h"
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
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
47
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
48 typedef struct NellyMoserEncodeContext {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
49 AVCodecContext *avctx;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
50 int last_frame;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
51 } NellyMoserEncodeContext;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
52
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
53 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
54
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
55 static const uint8_t sf_lut[96] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
56 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
57 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
58 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
59 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
60 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
61 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
62 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
63
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
64 static const uint8_t sf_delta_lut[78] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
65 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
66 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
67 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
68 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
69 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
70 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
71
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
72 static const uint8_t quant_lut[230] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
73 0,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
74
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
75 0, 1, 2,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
76
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
77 0, 1, 2, 3, 4, 5, 6,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
78
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
79 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
80 12, 13, 13, 13, 14,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
81
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
82 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
83 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
84 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
85 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
86
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 61, 61, 61, 61, 62,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
97 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
98
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
99 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
100 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
101 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
102
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
103 static av_cold int encode_init(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
104 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
105 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
106 int i;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
107
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
108 if (avctx->channels != 1) {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
109 av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
110 return -1;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
111 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
112
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
113 avctx->frame_size = NELLY_SAMPLES;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
114 s->avctx = avctx;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
115 ff_mdct_init(&s->mdct_ctx, 8, 0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
116 dsputil_init(&s->dsp, avctx);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
117
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
118 /* Generate overlap window */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
119 ff_sine_window_init(ff_sine_128, 128);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
120 for (i = 0; i < POW_TABLE_SIZE; i++)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
121 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
122
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
123 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
124 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
125
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
126 static av_cold int encode_end(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
127 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
128 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
129
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
130 ff_mdct_end(&s->mdct_ctx);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
131 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
132 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
133
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
134 AVCodec nellymoser_encoder = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
135 .name = "nellymoser",
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
136 .type = CODEC_TYPE_AUDIO,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
137 .id = CODEC_ID_NELLYMOSER,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
138 .priv_data_size = sizeof(NellyMoserEncodeContext),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
139 .init = encode_init,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
140 .encode = encode_frame,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
141 .close = encode_end,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
142 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
143 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao Codec"),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
144 };