| 1 | #include "audioencoder.h" |
| 2 | #include "avm_output.h" |
| 3 | #include "plugin.h" |
| 4 | #include "fillplugins.h" |
| 5 | |
| 6 | #include <string.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | extern "C" |
| 11 | { |
| 12 | #include "lame3.70/lame.h" |
| 13 | } |
| 14 | |
| 15 | AVM_BEGIN_NAMESPACE; |
| 16 | |
| 17 | PLUGIN_TEMP(mp3lame_audioenc) |
| 18 | |
| 19 | class MP3Encoder: public IAudioEncoder |
| 20 | { |
| 21 | lame_global_flags gf; |
| 22 | WAVEFORMATEX in_fmt; |
| 23 | |
| 24 | struct __attribute__((__packed__)) strf_mp3 |
| 25 | { |
| 26 | short wID; |
| 27 | int fdwFlags; |
| 28 | short nBlockSize; |
| 29 | short nFramesPerBlock; |
| 30 | short nCodecDelay; |
| 31 | }; |
| 32 | public: |
| 33 | MP3Encoder(const CodecInfo& info, const WAVEFORMATEX* format) |
| 34 | :IAudioEncoder(info) |
| 35 | { |
| 36 | in_fmt=*format; |
| 37 | lame_init(&gf); |
| 38 | gf.silent=1; |
| 39 | gf.padding_type=2; |
| 40 | gf.VBR=0; |
| 41 | gf.in_samplerate=format->nSamplesPerSec; |
| 42 | gf.num_channels=format->nChannels; |
| 43 | if (format->nChannels==1) |
| 44 | gf.mode=3; |
| 45 | else |
| 46 | gf.mode=1; |
| 47 | lame_init_params(&gf); |
| 48 | //int mode; /* 0,1,2,3 stereo,jstereo,dual channel,mono */ |
| 49 | } |
| 50 | virtual int SetBitrate(int bitrate) |
| 51 | { |
| 52 | gf.brate=bitrate/125; |
| 53 | lame_init_params(&gf); |
| 54 | AVM_WRITE("Lame MP3 encoder", "Setting bit rate to %d (%dkbps)\n", bitrate, gf.brate); |
| 55 | return 0; |
| 56 | } |
| 57 | virtual int SetQuality(int quality) |
| 58 | { |
| 59 | gf.quality=quality; |
| 60 | lame_init_params(&gf); |
| 61 | return 0; |
| 62 | } |
| 63 | virtual size_t GetFormat(void* extension = 0, size_t size = 0) const |
| 64 | { |
| 65 | if (!extension) |
| 66 | return 30; |
| 67 | if (size < 30) |
| 68 | return 0; |
| 69 | WAVEFORMATEX wf; |
| 70 | strf_mp3 mp3extra; |
| 71 | memcpy(&wf, &in_fmt, 18); |
| 72 | wf.wFormatTag=0x55; |
| 73 | wf.nAvgBytesPerSec=gf.brate*125; |
| 74 | wf.nBlockAlign=1; |
| 75 | wf.wBitsPerSample=0; |
| 76 | wf.cbSize=12; |
| 77 | memcpy(extension, &wf, 18); |
| 78 | |
| 79 | mp3extra.wID = 1; // |
| 80 | mp3extra.fdwFlags = 2; // These values |
| 81 | mp3extra.nBlockSize = (short)gf.framesize; |
| 82 | // based on an |
| 83 | mp3extra.nFramesPerBlock = 1; // old Usenet post!! |
| 84 | mp3extra.nCodecDelay = 1393; // |
| 85 | memcpy((char*)extension+18, &mp3extra, 12); |
| 86 | |
| 87 | return 30; |
| 88 | } |
| 89 | virtual int Start() |
| 90 | { |
| 91 | lame_init_params(&gf); |
| 92 | return 0; |
| 93 | } |
| 94 | virtual int Close(void* out_data, size_t out_size, size_t* size_read) |
| 95 | { |
| 96 | char buffer[7200]; |
| 97 | // printf("MP3Encoder::Close()\n"); |
| 98 | size_t bytes = lame_encode_finish(&gf, buffer, sizeof(buffer)); |
| 99 | if (out_size<bytes) |
| 100 | bytes = out_size; |
| 101 | if (out_data) |
| 102 | memcpy(out_data, buffer, bytes); |
| 103 | if (out_data && size_read) |
| 104 | *size_read = bytes; |
| 105 | return 0; |
| 106 | } |
| 107 | virtual int Convert(const void* in_data, size_t in_size, |
| 108 | void* out_data, size_t out_size, |
| 109 | size_t* size_read, size_t* size_written) |
| 110 | { |
| 111 | #warning FIXME 8-bit? |
| 112 | int result; |
| 113 | if (in_fmt.nChannels == 1) |
| 114 | result = lame_encode_buffer(&gf, (short*)in_data, (short*)in_data, |
| 115 | (int)in_size, (char*)out_data, (int)out_size); |
| 116 | else |
| 117 | result = lame_encode_buffer_interleaved(&gf, (short*)in_data, |
| 118 | (int)in_size, (char*)out_data, (int)out_size); |
| 119 | if(result<0)result=0; |
| 120 | if(size_read)*size_read=in_size; |
| 121 | if(size_written)*size_written=result; |
| 122 | return 0; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | static IAudioEncoder* mp3lame_CreateAudioEncoder(const CodecInfo& info, fourcc_t fourcc, const WAVEFORMATEX* fmt) |
| 127 | { |
| 128 | return new MP3Encoder(info, fmt); |
| 129 | } |
| 130 | |
| 131 | AVM_END_NAMESPACE; |
| 132 | |
| 133 | extern "C" AVMEXPORT avm::codec_plugin_t avm_codec_plugin_mp3lame_audioenc; |
| 134 | |
| 135 | avm::codec_plugin_t avm_codec_plugin_mp3lame_audioenc = |
| 136 | { |
| 137 | PLUGIN_API_VERSION, |
| 138 | |
| 139 | 0, // err |
| 140 | 0, 0, 0, 0, 0, 0, // attrs |
| 141 | avm::mp3lame_FillPlugins, |
| 142 | 0, |
| 143 | avm::mp3lame_CreateAudioEncoder, |
| 144 | 0, |
| 145 | 0, |
| 146 | }; |