Debian Codegraph @

lib/common/impl_audio.cpp

raw
1 #include "audiodecoder.h"
2 #include "audioencoder.h"
3 #include "utils.h"
4 #include "avm_output.h"
5 #include <string.h>
6
7 //#include <stdio.h>
8
9 AVM_BEGIN_NAMESPACE;
10
11 IAudioDecoder::IAudioDecoder(const CodecInfo& info, const WAVEFORMATEX* inw)
12 :m_Info(info), m_pFormat(0)
13 {
14 size_t sz = sizeof(WAVEFORMATEX) + inw->cbSize;
15 m_pFormat = (WAVEFORMATEX*) new char[sz];
16 memcpy(m_pFormat, inw, sz);
17
18 m_uiBytesPerSec = m_pFormat->wBitsPerSample;
19 switch (m_uiBytesPerSec)
20 {
21 case 0:
22 case 2: // IMC
23 case 4: // IMA_ADPCM
24 m_uiBytesPerSec = 16;
25 break;
26 }
27
28 switch (m_pFormat->wFormatTag)
29 {
30 case 6: // A-Law 8 -> 16b
31 case 7: // u-Law 8 -> 16b
32 m_uiBytesPerSec = 16;
33 break;
34 }
35
36 m_uiBytesPerSec = ((m_uiBytesPerSec + 7) / 8)
37 * m_pFormat->nSamplesPerSec * m_pFormat->nChannels;
38 }
39
40 IAudioDecoder::~IAudioDecoder()
41 {
42 delete[] m_pFormat;
43 }
44
45 void IAudioDecoder::Flush()
46 {
47 }
48
49 const CodecInfo& IAudioDecoder::GetCodecInfo() const
50 {
51 return m_Info;
52 }
53
54 size_t IAudioDecoder::GetMinSize() const
55 {
56 return (m_pFormat->nBlockAlign > 2) ? m_pFormat->nBlockAlign : 2;
57 }
58
59 int IAudioDecoder::GetOutputFormat(WAVEFORMATEX* destfmt) const
60 {
61 if (!destfmt)
62 return -1;
63
64 // couple tricks here
65 switch (m_pFormat->wBitsPerSample)
66 {
67 case 0:
68 case 2: // IMC
69 case 4: // IMA_ADPCM
70 destfmt->wBitsPerSample = 16;
71 break;
72 default:
73 destfmt->wBitsPerSample = m_pFormat->wBitsPerSample;
74 }
75
76 destfmt->nSamplesPerSec = m_pFormat->nSamplesPerSec;
77 destfmt->nChannels = m_pFormat->nChannels;
78 if (destfmt->nChannels > 2)
79 destfmt->nChannels = 2; // AC3 5 channel
80
81 switch (m_pFormat->wFormatTag)
82 {
83 case 6: // A-Law 8 -> 16b
84 case 7: // u-Law 8 -> 16b
85 destfmt->wBitsPerSample = 16;
86 break;
87 case 0x2000:
88 if (destfmt->nSamplesPerSec > 48000)
89 destfmt->nSamplesPerSec = 48000;
90 if (m_pFormat->nAvgBytesPerSec < m_pFormat->nSamplesPerSec)
91 m_pFormat->nAvgBytesPerSec = m_pFormat->nSamplesPerSec;
92 }
93
94 destfmt->wFormatTag = WAVE_FORMAT_PCM;
95 destfmt->nBlockAlign = (uint16_t) (destfmt->nChannels * ((destfmt->wBitsPerSample + 7) / 8));
96 destfmt->nAvgBytesPerSec = destfmt->nSamplesPerSec * destfmt->nBlockAlign;
97 destfmt->cbSize = 0;
98
99 return 0;
100 }
101
102 IRtConfig* IAudioDecoder::GetRtConfig()
103 {
104 return 0;
105 }
106
107 size_t IAudioDecoder::GetSrcSize(size_t dest_size) const
108 {
109 //printf("GET SRC Bps:%d ch:%d bps:%d sps:%d align:%d avgBps:%d\n", (int)m_uiBytesPerSec, m_pFormat->nChannels,
110 // m_pFormat->wBitsPerSample, m_pFormat->nSamplesPerSec, m_pFormat->nBlockAlign, m_pFormat->nAvgBytesPerSec);
111 if (!m_uiBytesPerSec || !m_pFormat->nBlockAlign)
112 {
113 if (!m_pFormat->nSamplesPerSec)
114 return 1500;
115
116 return dest_size;
117 }
118
119 size_t frames = dest_size * m_pFormat->nAvgBytesPerSec / m_uiBytesPerSec;
120 if (frames < m_pFormat->nBlockAlign)
121 frames = m_pFormat->nBlockAlign;
122 else if (m_pFormat->nBlockAlign > 1)
123 frames -= frames % m_pFormat->nBlockAlign;
124
125 return frames;
126 }
127
128 int IAudioDecoder::SetOutputFormat(const WAVEFORMATEX* destfmt)
129 {
130 return -1;
131 }
132
133 IAudioEncoder::IAudioEncoder(const CodecInfo& info)
134 :m_Info(info)
135 {
136 }
137
138 IAudioEncoder::~IAudioEncoder()
139 {
140 }
141
142 const CodecInfo& IAudioEncoder::GetCodecInfo() const
143 {
144 return m_Info;
145 }
146
147 int IAudioEncoder::SetBitrate(int bitrate)
148 {
149 return -1;
150 }
151
152 int IAudioEncoder::SetQuality(int quality)
153 {
154 return -1;
155 }
156
157 AVM_END_NAMESPACE;