annotate nellymoserenc.c @ 8562:187862685e70 libavcodec

Use only MpegEncContext in ff_vdpau_h264_add_data_chunk.
author cehoyos
date Sat, 10 Jan 2009 22:57:51 +0000
parents 96aadb5e4fd6
children e9d9d946f213
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 *
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
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;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
51 int bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
52 int have_saved;
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
53 DSPContext dsp;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
54 MDCTContext mdct_ctx;
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
55 DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
56 DECLARE_ALIGNED_16(float, buf[2][3 * NELLY_BUF_LEN]); ///< sample buffer
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
57 } NellyMoserEncodeContext;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
58
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
59 static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
60
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
61 static const uint8_t sf_lut[96] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
62 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
63 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
64 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
65 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
66 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
67 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
68 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
69
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
70 static const uint8_t sf_delta_lut[78] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
71 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
72 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
73 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
74 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
75 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
76 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
77
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
78 static const uint8_t quant_lut[230] = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
79 0,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
80
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
81 0, 1, 2,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
82
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
83 0, 1, 2, 3, 4, 5, 6,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
84
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
85 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
86 12, 13, 13, 13, 14,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
87
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
88 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
89 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
90 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
91 30,
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, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 61, 61, 61, 61, 62,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
103 };
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
104
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
105 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
106 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
107 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
108
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
109 void apply_mdct(NellyMoserEncodeContext *s)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
110 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
111 DECLARE_ALIGNED_16(float, in_buff[NELLY_SAMPLES]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
112
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
113 memcpy(in_buff, s->buf[s->bufsel], NELLY_BUF_LEN * sizeof(float));
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
114 s->dsp.vector_fmul(in_buff, ff_sine_128, NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
115 s->dsp.vector_fmul_reverse(in_buff + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128,
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
116 NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
117 ff_mdct_calc(&s->mdct_ctx, s->mdct_out, in_buff);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
118
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
119 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
120 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
121 NELLY_BUF_LEN);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
122 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
123 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
124
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
125 static av_cold int encode_init(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
126 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
127 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
128 int i;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
129
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
130 if (avctx->channels != 1) {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
131 av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
132 return -1;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
133 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
134
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
135 if (avctx->sample_rate != 8000 && avctx->sample_rate != 11025 &&
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
136 avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
137 avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
138 av_log(avctx, AV_LOG_ERROR, "Nellymoser works only with 8000, 11025, 22050 and 44100 sample rate\n");
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
139 return -1;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
140 }
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
141
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
142 avctx->frame_size = NELLY_SAMPLES;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
143 s->avctx = avctx;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
144 ff_mdct_init(&s->mdct_ctx, 8, 0);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
145 dsputil_init(&s->dsp, avctx);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
146
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
147 /* Generate overlap window */
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
148 ff_sine_window_init(ff_sine_128, 128);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
149 for (i = 0; i < POW_TABLE_SIZE; i++)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
150 pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
151
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
152 return 0;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
153 }
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
154
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
155 static av_cold int encode_end(AVCodecContext *avctx)
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
156 {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
157 NellyMoserEncodeContext *s = avctx->priv_data;
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
158
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
159 ff_mdct_end(&s->mdct_ctx);
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
7762
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
163 #define find_best(val, table, LUT, LUT_add, LUT_size) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
164 best_idx = \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
165 LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
166 if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
167 best_idx++;
e2573d045e79 Okayed parts of nellymoserenc.c
bwolowiec
parents: 7735
diff changeset
168
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
169 static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
170 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
171 int band, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
172 float power_candidate;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
173
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
174 //base exponent
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
175 find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
176 idx_table[0] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
177 power_idx = ff_nelly_init_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
178
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
179 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
180 power_candidate = cand[band] - power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
181 find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
182 idx_table[band] = best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
183 power_idx += ff_nelly_delta_table[best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
184 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
185 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
186
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
187 #define OPT_SIZE ((1<<15) + 3000)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
188
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
189 static inline float distance(float x, float y, int band)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
190 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
191 //return pow(fabs(x-y), 2.0);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
192 float tmp = x - y;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
193 return tmp * tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
194 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
195
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
196 static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
197 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
198 int i, j, band, best_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
199 float power_candidate, best_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
200
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
201 float opt[NELLY_BANDS][OPT_SIZE];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
202 int path[NELLY_BANDS][OPT_SIZE];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
203
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
204 for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
205 opt[0][i] = INFINITY;
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 for (i = 0; i < 64; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
209 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
210 path[0][ff_nelly_init_table[i]] = i;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
211 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
212
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
213 for (band = 1; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
214 int q, c = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
215 float tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
216 int idx_min, idx_max, idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
217 power_candidate = cand[band];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
218 for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
219 idx_min = FFMAX(0, cand[band] - q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
220 idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
221 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
222 if ( isinf(opt[band - 1][i]) )
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
223 continue;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
224 for (j = 0; j < 32; j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
225 idx = i + ff_nelly_delta_table[j];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
226 if (idx > idx_max)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
227 break;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
228 if (idx >= idx_min) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
229 tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
230 if (opt[band][idx] > tmp) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
231 opt[band][idx] = tmp;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
232 path[band][idx] = j;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
233 c = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
234 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
235 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
236 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
237 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
238 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
239 assert(c); //FIXME
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
240 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
241
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
242 best_val = INFINITY;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
243 best_idx = -1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
244 band = NELLY_BANDS - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
245 for (i = 0; i < OPT_SIZE; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
246 if (best_val > opt[band][i]) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
247 best_val = opt[band][i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
248 best_idx = i;
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 for (band = NELLY_BANDS - 1; band >= 0; band--) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
252 idx_table[band] = path[band][best_idx];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
253 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
254 best_idx -= ff_nelly_delta_table[path[band][best_idx]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
255 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
256 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
257 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
258
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
259 /**
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
260 * 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
261 * @param s encoder context
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
262 * @param output output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
263 * @param output_size size of output buffer
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
264 */
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
265 static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
266 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
267 PutBitContext pb;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
268 int i, j, band, block, best_idx, power_idx = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
269 float power_val, coeff, coeff_sum;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
270 float pows[NELLY_FILL_LEN];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
271 int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
272 float cand[NELLY_BANDS];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
273
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
274 apply_mdct(s);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
275
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
276 init_put_bits(&pb, output, output_size * 8);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
277
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
278 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
279 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
280 coeff_sum = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
281 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
282 coeff_sum += s->mdct_out[i ] * s->mdct_out[i ]
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
283 + 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
284 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
285 cand[band] =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
286 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
287 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
288
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
289 if (s->avctx->trellis) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
290 get_exponent_dynamic(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
291 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
292 get_exponent_greedy(s, cand, idx_table);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
293 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
294
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
295 i = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
296 for (band = 0; band < NELLY_BANDS; band++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
297 if (band) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
298 power_idx += ff_nelly_delta_table[idx_table[band]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
299 put_bits(&pb, 5, idx_table[band]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
300 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
301 power_idx = ff_nelly_init_table[idx_table[0]];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
302 put_bits(&pb, 6, idx_table[0]);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
303 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
304 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
305 for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
306 s->mdct_out[i] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
307 s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
308 pows[i] = power_idx;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
309 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
310 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
311
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
312 ff_nelly_get_sample_bits(pows, bits);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
313
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
314 for (block = 0; block < 2; block++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
315 for (i = 0; i < NELLY_FILL_LEN; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
316 if (bits[i] > 0) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
317 const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
318 coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
319 best_idx =
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
320 quant_lut[av_clip (
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
321 coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
322 quant_lut_offset[bits[i]],
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
323 quant_lut_offset[bits[i]+1] - 1
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
324 )];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
325 if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
326 best_idx++;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
327
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
328 put_bits(&pb, bits[i], best_idx);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
329 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
330 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
331 if (!block)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
332 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
333 }
8019
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
334
dea517f771aa nellymoserenc: flush PutBitContext after use.
ramiro
parents: 7768
diff changeset
335 flush_put_bits(&pb);
7768
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
336 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
337
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
338 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
339 {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
340 NellyMoserEncodeContext *s = avctx->priv_data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
341 int16_t *samples = data;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
342 int i;
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 (s->last_frame)
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
345 return 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
346
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
347 if (data) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
348 for (i = 0; i < avctx->frame_size; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
349 s->buf[s->bufsel][i] = samples[i];
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
350 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
351 for (; i < NELLY_SAMPLES; i++) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
352 s->buf[s->bufsel][i] = 0;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
353 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
354 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
355 if (!s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
356 s->have_saved = 1;
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 } else {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
360 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
361 s->bufsel = 1 - s->bufsel;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
362 s->last_frame = 1;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
363 }
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
364
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
365 if (s->have_saved) {
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
366 encode_block(s, frame, buf_size);
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
367 return NELLY_BLOCK_LEN;
152573e499c9 Remaining parts of Nellymoser encoder
bwolowiec
parents: 7762
diff changeset
368 }
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
7735
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
372 AVCodec nellymoser_encoder = {
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
373 .name = "nellymoser",
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
374 .type = CODEC_TYPE_AUDIO,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
375 .id = CODEC_ID_NELLYMOSER,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
376 .priv_data_size = sizeof(NellyMoserEncodeContext),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
377 .init = encode_init,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
378 .encode = encode_frame,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
379 .close = encode_end,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
380 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
381 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao Codec"),
87ec777690c9 Okayed parts of nellymoserenc.c
bwolowiec
parents:
diff changeset
382 };