| 1 | /* |
| 2 | * Plugin for lame encoding |
| 3 | * |
| 4 | * this version loads the library in runtime |
| 5 | * might look a bit complicated but has few advantages - you do not have |
| 6 | * to have libmp3lame installed on your system while you still could build |
| 7 | * this working plugin |
| 8 | */ |
| 9 | |
| 10 | #include "audioencoder.h" |
| 11 | #include "fillplugins.h" |
| 12 | #include "plugin.h" |
| 13 | #include "utils.h" |
| 14 | #include "avm_output.h" |
| 15 | |
| 16 | #include <dlfcn.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "lame.h" |
| 22 | |
| 23 | AVM_BEGIN_NAMESPACE; |
| 24 | |
| 25 | PLUGIN_TEMP(mp3lamebin_audioenc); |
| 26 | |
| 27 | static const char mp3lamename[] = "libmp3lame.so.0"; |
| 28 | static const char MODULE[] = "Lame mp3 Encoder"; |
| 29 | |
| 30 | class LameEncoder: public IAudioEncoder |
| 31 | { |
| 32 | void* handle; |
| 33 | lame_global_flags* gf; |
| 34 | WAVEFORMATEX in_fmt; |
| 35 | int brate; |
| 36 | char m_Error[128]; |
| 37 | |
| 38 | /* lame calls */ |
| 39 | lame_global_flags* CDECL (*p_lame_init)(void); |
| 40 | int CDECL (*p_lame_init_params)(lame_global_flags *); |
| 41 | int CDECL (*p_lame_encode_buffer)(lame_global_flags* gfp, |
| 42 | const short int buffer_l[], |
| 43 | const short int buffer_r[], |
| 44 | const int nsamples, |
| 45 | unsigned char* mp3buf, |
| 46 | const int mp3buf_size); |
| 47 | int CDECL (*p_lame_encode_buffer_interleaved)(lame_global_flags* gfp, |
| 48 | short int pcm[], |
| 49 | int num_samples, |
| 50 | unsigned char* mp3buf, |
| 51 | int mp3buf_size); |
| 52 | int CDECL (*p_lame_encode_finish)(lame_global_flags* gfp, |
| 53 | unsigned char* mp3buf, |
| 54 | int size); |
| 55 | int CDECL (*p_lame_get_framesize)(const lame_global_flags *); |
| 56 | int CDECL (*p_lame_get_size_mp3buffer)( const lame_global_flags*); |
| 57 | int CDECL (*p_lame_get_brate)(const lame_global_flags *); |
| 58 | int CDECL (*p_lame_get_VBR_mean_bitrate_kbps)( const lame_global_flags*); |
| 59 | void CDECL (*p_lame_print_config)(const lame_global_flags*); |
| 60 | void CDECL (*p_lame_print_internals)(const lame_global_flags *); |
| 61 | int CDECL (*p_lame_set_bWriteVbrTag)(lame_global_flags *, int); |
| 62 | int CDECL (*p_lame_set_padding_type)(lame_global_flags *, Padding_type); |
| 63 | int CDECL (*p_lame_set_VBR)(lame_global_flags *, vbr_mode); |
| 64 | int CDECL (*p_lame_set_VBR_q)(lame_global_flags *, int); |
| 65 | int CDECL (*p_lame_set_VBR_mean_bitrate_kbps)(lame_global_flags *, int); |
| 66 | int CDECL (*p_lame_set_VBR_min_bitrate_kbps)(lame_global_flags *, int); |
| 67 | int CDECL (*p_lame_set_VBR_max_bitrate_kbps)(lame_global_flags *, int); |
| 68 | int CDECL (*p_lame_set_in_samplerate)(lame_global_flags *, int); |
| 69 | int CDECL (*p_lame_set_num_channels)(lame_global_flags *, int); |
| 70 | int CDECL (*p_lame_set_mode)(lame_global_flags *, MPEG_mode); |
| 71 | int CDECL (*p_lame_set_brate)(lame_global_flags *, int); |
| 72 | int CDECL (*p_lame_set_quality)(lame_global_flags *, int); |
| 73 | |
| 74 | public: |
| 75 | LameEncoder(const CodecInfo& info, const WAVEFORMATEX* format) |
| 76 | :IAudioEncoder(info), handle(0) |
| 77 | { |
| 78 | m_Error[0] = 0; |
| 79 | in_fmt = *format; |
| 80 | } |
| 81 | |
| 82 | int init() |
| 83 | { |
| 84 | handle = dlopen(mp3lamename, RTLD_LAZY); |
| 85 | |
| 86 | if (!handle) |
| 87 | { |
| 88 | sprintf(m_Error, "Lame library %s could not be opened: %s\n" |
| 89 | "If you want to use this plugin - install lame library\n" |
| 90 | "on your system - see README for more details\n", |
| 91 | mp3lamename, dlerror()); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | // resolve all needed function calls |
| 96 | #define d(a, b) p_lame_ ## a = b dlsymm( "lame_" #a ) |
| 97 | d(init, (lame_global_flags * CDECL (*)(void))); |
| 98 | d(init_params, (int CDECL (*)(lame_global_flags *))); |
| 99 | d(print_config, (void CDECL (*)(const lame_global_flags *))); |
| 100 | d(print_internals, (void CDECL (*)(const lame_global_flags *))); |
| 101 | d(set_bWriteVbrTag, (int CDECL (*)(lame_global_flags *, int))); |
| 102 | d(set_padding_type, (int CDECL (*)(lame_global_flags *, Padding_type))); |
| 103 | d(set_VBR, (int CDECL (*)(lame_global_flags *, vbr_mode))); |
| 104 | d(set_VBR_q, (int CDECL (*)(lame_global_flags *, int))); |
| 105 | d(set_VBR_mean_bitrate_kbps, (int CDECL (*)(lame_global_flags *, int))); |
| 106 | d(set_VBR_min_bitrate_kbps, (int CDECL (*)(lame_global_flags *, int))); |
| 107 | d(set_VBR_max_bitrate_kbps, (int CDECL (*)(lame_global_flags *, int))); |
| 108 | d(set_in_samplerate, (int CDECL (*)(lame_global_flags *, int))); |
| 109 | d(set_num_channels, (int CDECL (*)(lame_global_flags *, int))); |
| 110 | d(set_mode, (int CDECL (*)(lame_global_flags *, MPEG_mode))); |
| 111 | d(set_brate, (int CDECL (*)(lame_global_flags *, int))); |
| 112 | d(set_quality, (int CDECL (*)(lame_global_flags *, int))); |
| 113 | d(get_framesize, (int CDECL (*)(const lame_global_flags *))); |
| 114 | d(get_size_mp3buffer, (int CDECL (*)(const lame_global_flags *))); |
| 115 | d(get_brate, (int CDECL (*)(const lame_global_flags *))); |
| 116 | d(get_VBR_mean_bitrate_kbps, (int CDECL (*)(const lame_global_flags *))); |
| 117 | d(encode_buffer_interleaved, (int CDECL (*)(lame_global_flags*, |
| 118 | short int pcm[], |
| 119 | int, unsigned char*, int))); |
| 120 | d(encode_finish, (int CDECL (*)(lame_global_flags*, unsigned char*, int))); |
| 121 | d(encode_buffer, (int CDECL (*)(lame_global_flags*, const short int buffer_l[], |
| 122 | const short int buffer_r[], |
| 123 | const int, unsigned char*, const int))); |
| 124 | #undef d |
| 125 | if (m_Error[0] != 0) |
| 126 | return -1; |
| 127 | |
| 128 | gf = p_lame_init(); |
| 129 | p_lame_set_bWriteVbrTag(gf, 0); |
| 130 | p_lame_set_padding_type(gf, PAD_ADJUST); |
| 131 | p_lame_set_in_samplerate(gf, in_fmt.nSamplesPerSec); |
| 132 | p_lame_set_num_channels(gf, in_fmt.nChannels); |
| 133 | |
| 134 | #if 1 |
| 135 | p_lame_set_VBR(gf, vbr_off); |
| 136 | #else |
| 137 | p_lame_set_VBR(gf, vbr_mtrh); |
| 138 | p_lame_set_VBR_q(gf, 7); |
| 139 | p_lame_set_bWriteVbrTag(gf, 1); |
| 140 | p_lame_set_VBR_max_bitrate_kbps(gf, 64); |
| 141 | p_lame_set_quality(gf, 9); |
| 142 | #endif |
| 143 | |
| 144 | if (in_fmt.nChannels == 1) |
| 145 | p_lame_set_mode(gf, MONO); |
| 146 | else |
| 147 | p_lame_set_mode(gf, JOINT_STEREO); |
| 148 | /* 0,1,2,3 stereo,jstereo,dual channel,mono */ |
| 149 | |
| 150 | p_lame_init_params(gf); |
| 151 | |
| 152 | //p_lame_print_config(gf); |
| 153 | //p_lame_print_internals(gf); |
| 154 | |
| 155 | AVM_WRITE(MODULE, "initialized\n"); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | ~LameEncoder() |
| 160 | { |
| 161 | if (handle) |
| 162 | dlclose(handle); |
| 163 | } |
| 164 | |
| 165 | virtual int Convert(const void* in_data, size_t in_size, |
| 166 | void* out_data, size_t out_size, |
| 167 | size_t* size_read, size_t* size_written) |
| 168 | { |
| 169 | #warning FIXME 8-bit? |
| 170 | int result; |
| 171 | |
| 172 | //printf("LAMEGETBUG %d\n", p_lame_get_size_mp3buffer(gf)); |
| 173 | if (in_fmt.nChannels == 1) |
| 174 | result = p_lame_encode_buffer(gf, (short*)in_data, |
| 175 | (short*)in_data, (int)in_size, |
| 176 | (unsigned char*)out_data, (int)out_size); |
| 177 | else |
| 178 | result = p_lame_encode_buffer_interleaved(gf, (short*)in_data, (int)in_size, |
| 179 | (unsigned char*)out_data, (int)out_size); |
| 180 | //printf("LAMEGETBUG2 b:%d i:%d s:%d\n", p_lame_get_size_mp3buffer(gf), in_size, result); |
| 181 | if (result < 0) |
| 182 | result = 0; |
| 183 | if (size_read) |
| 184 | *size_read = in_size; |
| 185 | if (size_written) |
| 186 | *size_written = result; |
| 187 | return 0; |
| 188 | } |
| 189 | virtual size_t GetFormat(void* extension = 0, size_t size = 0) const |
| 190 | { |
| 191 | struct __attribute__((__packed__)) strf_mp3 |
| 192 | { |
| 193 | uint16_t wID; |
| 194 | uint32_t fdwFlags; |
| 195 | uint16_t nBlockSize; |
| 196 | uint16_t nFramesPerBlock; |
| 197 | uint16_t nCodecDelay; |
| 198 | }; |
| 199 | |
| 200 | if (!extension) |
| 201 | return 30; |
| 202 | if (size < 30) |
| 203 | return 0; |
| 204 | |
| 205 | int ibrate = p_lame_get_brate(gf); |
| 206 | memset(extension, 0, size); |
| 207 | WAVEFORMATEX wf; |
| 208 | strf_mp3 mp3extra; |
| 209 | memcpy(&wf, &in_fmt, sizeof(wf)); |
| 210 | wf.wFormatTag = 0x55; |
| 211 | wf.nAvgBytesPerSec = ibrate * 125; |
| 212 | wf.nBlockAlign = 1; |
| 213 | wf.wBitsPerSample = 0; |
| 214 | wf.cbSize = 12; |
| 215 | memcpy(extension, &wf, sizeof(wf)); |
| 216 | |
| 217 | //p_lame_init_params(gf); |
| 218 | avm_set_le16(&mp3extra.wID, 1); // |
| 219 | avm_set_le32(&mp3extra.fdwFlags, 2); // These values based |
| 220 | avm_set_le16(&mp3extra.nBlockSize, (uint16_t)p_lame_get_framesize(gf)); |
| 221 | avm_set_le16(&mp3extra.nFramesPerBlock, 1); // on an old Usenet post!! |
| 222 | avm_set_le16(&mp3extra.nCodecDelay, 1393); // 0x571 for F-IIS MP3 Codec |
| 223 | |
| 224 | memcpy((uint8_t*)extension + sizeof(wf), &mp3extra, 12); |
| 225 | |
| 226 | AVM_WRITE(MODULE, "GetFormat() %d\n", ibrate); |
| 227 | return 30; |
| 228 | } |
| 229 | virtual int Start() |
| 230 | { |
| 231 | //p_lame_init_params(gf); |
| 232 | AVM_WRITE(MODULE, "Start() ch: %d freq: %d\n", |
| 233 | in_fmt.nChannels, in_fmt.nSamplesPerSec); |
| 234 | p_lame_init_params(gf); |
| 235 | return 0; |
| 236 | } |
| 237 | virtual int SetBitrate(int bitrate) |
| 238 | { |
| 239 | brate = bitrate / 125; |
| 240 | AVM_WRITE(MODULE, "SetBitrate(%d) %dkbps\n", bitrate, brate); |
| 241 | return p_lame_set_brate(gf, brate); |
| 242 | } |
| 243 | virtual int SetQuality(int quality) |
| 244 | { |
| 245 | AVM_WRITE(MODULE, "SetQuality(%d)\n", quality); |
| 246 | return p_lame_set_quality(gf, quality); |
| 247 | } |
| 248 | //virtual int SetVBR(int min, int max); |
| 249 | const char* getError() { return m_Error; } |
| 250 | private: |
| 251 | int Close(void* out_data, size_t out_size, size_t* size_read) |
| 252 | { |
| 253 | uint8_t buffer[7200]; |
| 254 | size_t bytes = p_lame_encode_finish(gf, buffer, sizeof(buffer)); |
| 255 | if (out_size < bytes) |
| 256 | bytes = out_size; |
| 257 | if (out_data) |
| 258 | memcpy(out_data, buffer, bytes); |
| 259 | if (out_data && size_read) |
| 260 | *size_read = bytes; |
| 261 | |
| 262 | AVM_WRITE(MODULE, "average %d kbps", p_lame_get_VBR_mean_bitrate_kbps(gf)); |
| 263 | return 0; |
| 264 | } |
| 265 | void* dlsymm(const char* symbol, bool fatal = true) |
| 266 | { |
| 267 | if (m_Error[0] != 0) |
| 268 | return 0; |
| 269 | |
| 270 | void* f = dlsym(handle, symbol); |
| 271 | if (!f && fatal) |
| 272 | sprintf(m_Error, "function '%s' can't be resolved\n", symbol); |
| 273 | return f; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | // plugin part |
| 278 | static IAudioEncoder* mp3lamebin_CreateAudioEncoder(const CodecInfo& info, fourcc_t fourcc, const WAVEFORMATEX* fmt) |
| 279 | { |
| 280 | LameEncoder* e = new LameEncoder(info, fmt); |
| 281 | if (e) |
| 282 | { |
| 283 | if (e->init() == 0) |
| 284 | return e; |
| 285 | mp3lamebin_audioenc_error_set(e->getError()); |
| 286 | delete e; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | AVM_END_NAMESPACE; |
| 292 | |
| 293 | extern "C" avm::codec_plugin_t avm_codec_plugin_mp3lamebin_audioenc; |
| 294 | |
| 295 | avm::codec_plugin_t avm_codec_plugin_mp3lamebin_audioenc = |
| 296 | { |
| 297 | PLUGIN_API_VERSION, |
| 298 | |
| 299 | 0, // err |
| 300 | 0, 0, 0, 0, 0, 0, // attrs |
| 301 | avm::mp3lamebin_FillPlugins, |
| 302 | 0, |
| 303 | avm::mp3lamebin_CreateAudioEncoder, |
| 304 | 0, |
| 305 | 0, |
| 306 | }; |