aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Encoder/enc_lame/24bit.cpp
blob: d1b83b1f810ea907efcc87f47f13c8a92a95066d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "MP3Coder.h"
#include <malloc.h>

static const float const_1_div_128_ = 32768.0 / 128.0;  /* 8 bit multiplier */
static const double const_1_div_2147483648_ = 32768.0 / 2147483648.0; /* 32 bit multiplier */

static void Int8_To_Float32(float *dest, void *sourceBuffer, signed int sourceStride, unsigned int count)
{
    signed char *src = (signed char*)sourceBuffer;

    while( count-- )
    {
        float samp = *src * const_1_div_128_;
        *dest = samp;

        src += sourceStride;
        dest ++;
    }
}

static void Int24_To_Float32(float *dest, void *sourceBuffer, signed int sourceStride, unsigned int count)
{
	unsigned char *src = (unsigned char*)sourceBuffer;

	while ( count-- )
	{
		signed long temp = (((long)src[0]) << 8);
		temp = temp | (((long)src[1]) << 16);
		temp = temp | (((long)src[2]) << 24);
		*dest = (float) ((double)temp * const_1_div_2147483648_);
		src += sourceStride * 3;
		dest ++;
	}
}

int AudioCoderMP3_24::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
{
	if (m_err) return -1;
	if (!hbeStream)
	{
		if (beInitStream(&beConfig, &ibuf_size_spls, &obuf_size, (PHBE_STREAM) &hbeStream) != BE_ERR_SUCCESSFUL)
		{
			m_err++;
			return -1;
		}
		ibuf_size = ibuf_size_spls * bytesPerSample;

		if (is_downmix) ibuf_size *= 2;

		bs = (char*)malloc(ibuf_size);
		bs_size = 0;
	}

	*in_used = 0;

	int needbytes = ibuf_size - bs_size;
	if (needbytes > 0 && in_avail > 0)
	{
		if (needbytes > in_avail)
			needbytes = in_avail;
		memcpy(bs + bs_size, in, needbytes);
		bs_size += needbytes;
		*in_used = needbytes;
	}

	if (!done)
	{
		if (bs_size < (int)ibuf_size) return 0;
	}

	if (out_avail < (int)obuf_size) return 0;

	int feedBytes = min(bs_size, (int)ibuf_size);
	int feedSamples = feedBytes / bytesPerSample;
	bs_size -= feedBytes;
	/*
		if (is_downmix)
		{
			int x;
			int l = feedBytes / 4;
			short *b = (short*)bs;
			for (x = 0; x < l; x ++)
			{
				b[x] = b[x * 2] / 2 + b[x * 2 + 1] / 2;
			}
		}
		*/
	DWORD dwo = 0;

	if (feedSamples)
	{
		if (mono_input)
		{
			float *float_l = (float *)alloca(sizeof(float) * feedSamples);
			switch(bytesPerSample)
			{
			case 8:
				Int8_To_Float32(float_l, bs, 1, feedSamples);
				break;
			case 24:
				Int24_To_Float32(float_l, bs, 1, feedSamples);
				break;
			}
			

			if (beEncodeChunkFloatS16NI(hbeStream, feedSamples, float_l, float_l, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
			{
				m_err++;
				return -1;
			}
		}
		else
		{
			float *float_l = (float *)alloca(sizeof(float) * feedSamples / 2);
			float *float_r = (float *)alloca(sizeof(float) * feedSamples / 2);
			switch(bytesPerSample)
			{
				case 1: // 8 bit
					Int8_To_Float32(float_l, bs, 2, feedSamples / 2);
					Int8_To_Float32(float_r, bs + 1, 2, feedSamples / 2);
				break;
				case 3: // 24 bit
					Int24_To_Float32(float_l, bs, 2, feedSamples / 2);
					Int24_To_Float32(float_r, bs + 3, 2, feedSamples / 2);
				break;
			}
			
			if (beEncodeChunkFloatS16NI(hbeStream, feedSamples / 2, float_l, float_r, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
			{
				m_err++;
				return -1;
			}
		}
	}

	if (!dwo && done == 1)
	{
		if (beDeinitStream(hbeStream, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
		{
			m_err++;
			return -1;
		}
		done++;
	}

	return dwo;
}