summaryrefslogtreecommitdiffstats
path: root/WDL/lameencdec.cpp
blob: dec614b47dd2eaa496f452168b4ee50b8528092b (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
  WDL - lameencdec.cpp
  Copyright (C) 2005 and later Cockos Incorporated

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
  


*/



#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#else
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wdlcstring.h"
#include "lameencdec.h"
#include "win32_utf8.h"
#include "ptrlist.h"

#ifdef __APPLE__
  #include <Carbon/Carbon.h>
#endif
#ifndef _WIN32
  #include <dlfcn.h>
#endif

// note: calling code should include WDL/metadata.h from somewhere to implement these functions
struct ID3RawTag;
int PackID3Chunk(WDL_HeapBuf *hb, WDL_StringKeyedArray<char*> *metadata,
  bool want_embed_otherschemes, int *ixml_lenwritten, int ixml_padtolen,
  WDL_PtrList<ID3RawTag> *rawtags=NULL);
int PackApeChunk(WDL_HeapBuf *hb, WDL_StringKeyedArray<char*> *metadata);
int PackXMPChunk(WDL_HeapBuf *hb, WDL_StringKeyedArray<char*> *metadata);
bool ParseUserDefMetadata(const char *id, const char *val,
  const char **k, const char **v, int *klen, int *vlen);
bool HasScheme(const char *scheme, WDL_StringKeyedArray<char*> *metadata);


typedef enum MPEG_mode_e {
  STEREO = 0,
  JOINT_STEREO,
  DUAL_CHANNEL,   /* LAME doesn't supports this! */
  MONO,
  NOT_SET,
  MAX_INDICATOR   /* Don't use this! It's used for sanity checks. */
} MPEG_mode;

typedef void *lame_t;

static struct {
  int (*close)(lame_t);
  lame_t (*init)();
  int (*set_in_samplerate)(lame_t, int);
  int (*set_num_channels)(lame_t,int);
  int (*set_out_samplerate)(lame_t,int);
  int (*set_quality)(lame_t,int);
  int (*set_mode)(lame_t,MPEG_mode);
  int (*set_brate)(lame_t, int);
  int (*init_params)(lame_t);
  int (*get_framesize)(lame_t);

  int (*encode_buffer_float)(lame_t,
          const float     buffer_l [],       /* PCM data for left channel     */
          const float     buffer_r [],       /* PCM data for right channel    */
          const int           nsamples,      /* number of samples per channel */
          unsigned char*      mp3buf,        /* pointer to encoded MP3 stream */
          const int           mp3buf_size ); 
  int (*encode_flush)(lame_t,unsigned char*       mp3buf,  int                  size);

// these are optional
  int (*set_VBR)(lame_t, int);
  int (*set_VBR_q)(lame_t, int);
  int (*set_VBR_mean_bitrate_kbps)(lame_t, int);
  int (*set_VBR_min_bitrate_kbps)(lame_t, int);
  int (*set_VBR_max_bitrate_kbps)(lame_t, int);
  size_t (*get_lametag_frame)(lame_t, unsigned char *, size_t);
  const char *(*get_lame_version)();
  int (*set_findReplayGain)(lame_t, int);

} lame;

#if 1
#define LAME_DEBUG_LOADING(x) 
#else
#define LAME_DEBUG_LOADING(x) OutputDebugString(x)
#endif

static char s_last_dll_file[128];

static bool tryLoadDLL2(const char *name)
{
#ifdef _WIN32
  HINSTANCE dll = LoadLibrary(name);
#else
  void *dll=dlopen(name,RTLD_NOW|RTLD_LOCAL);
#endif

  if (!dll) return false;

  LAME_DEBUG_LOADING("trying to load");
  LAME_DEBUG_LOADING(name);
  int errcnt = 0;
  #ifdef _WIN32
    #define GETITEM(x) if (NULL == (*(void **)&lame.x = GetProcAddress((HINSTANCE)dll,"lame_" #x))) errcnt++;
    #define GETITEM_NP(x) if (NULL == (*(void **)&lame.x = GetProcAddress((HINSTANCE)dll, #x))) errcnt++;
  #else
    #define GETITEM(x) if (NULL == (*(void **)&lame.x = dlsym(dll,"lame_" #x))) errcnt++;
    #define GETITEM_NP(x) if (NULL == (*(void **)&lame.x = dlsym(dll,#x))) errcnt++;
  #endif
  GETITEM(close)
  GETITEM(init)
  GETITEM(set_in_samplerate)
  GETITEM(set_num_channels)
  GETITEM(set_out_samplerate)
  GETITEM(set_quality)
  GETITEM(set_mode)
  GETITEM(set_brate)
  GETITEM(init_params)
  GETITEM(get_framesize)
  GETITEM(encode_buffer_float)
  GETITEM(encode_flush)

  int errcnt2 = errcnt;
  GETITEM(set_VBR)
  GETITEM(set_VBR_q)
  GETITEM(set_VBR_mean_bitrate_kbps)
  GETITEM(set_VBR_min_bitrate_kbps)
  GETITEM(set_VBR_max_bitrate_kbps)
  GETITEM(get_lametag_frame)
  GETITEM(set_findReplayGain)
  GETITEM_NP(get_lame_version)

  #undef GETITEM   
  #undef GETITEM_NP
  if (errcnt2)
  {
    memset(&lame, 0, sizeof(lame));

#ifdef _WIN32
    FreeLibrary(dll);
#else
    dlclose(dll);
#endif
    return false;
  }

  LAME_DEBUG_LOADING("loaded normal mode");

  lstrcpyn_safe(s_last_dll_file, name, sizeof(s_last_dll_file));
#ifdef _WIN32
//  if (!strstr(name,"\\"))
  GetModuleFileName(dll,s_last_dll_file, (DWORD)sizeof(s_last_dll_file));
#else
//  if (!strstr(name,"/"))
  {
    Dl_info inf={0,};
    dladdr((void*)lame.init,&inf);
    if (inf.dli_fname)
      lstrcpyn_safe(s_last_dll_file,inf.dli_fname,sizeof(s_last_dll_file));
  }
#endif

  return true;
}



void LameEncoder::InitDLL(const char *extrapath, bool forceRetry)
{
  static int a;
  if (a<0) return;

  if (forceRetry) a=0;
  else if (a > 30) return; // give up

  a++;
  
  char me[1024];
#ifdef _WIN32
  const char *dll = "libmp3lame.dll";
#elif defined(__APPLE__)
  const char *dll = "libmp3lame.dylib";
#else
  const char *dll = "libmp3lame.so.0";
#endif

  if (extrapath)
  {
    snprintf(me,sizeof(me),"%s%c%s",extrapath,WDL_DIRCHAR,dll);
    if (tryLoadDLL2(me)) { a = -1; return; }
  }

  if (tryLoadDLL2(dll)) a=-1;

}

const char *LameEncoder::GetLibName() { return s_last_dll_file; }

const char *LameEncoder::GetInfo()
{
  static char buf[128];
  if (!CheckDLL()) return NULL;
  const char *p = lame.get_lame_version ? lame.get_lame_version() : NULL;
  if (p && *p)
  {
    snprintf(buf, sizeof(buf), "LAME %s", p);
    return buf;
  }
  return "LAME ?.??";
}

int LameEncoder::CheckDLL() // returns 1 for lame API, 2 for Blade, 0 for none
{
  InitDLL();
  if (!lame.close||
      !lame.init||
      !lame.set_in_samplerate||
      !lame.set_num_channels||
      !lame.set_out_samplerate||
      !lame.set_quality||
      !lame.set_mode||
      !lame.set_brate||
      !lame.init_params||
      !lame.get_framesize||
      !lame.encode_buffer_float||
      !lame.encode_flush) 
  {
    return 0;
  }

  return 1;

}

LameEncoder::LameEncoder(int srate, int nch, int bitrate, int stereomode, int quality,
  int vbrmethod, int vbrquality, int vbrmax, int abr, int rpgain,
  WDL_StringKeyedArray<char*> *metadata)
{
  m_lamestate=0;
  if (!CheckDLL())
  {
    errorstat=1;
    return;
  }

  errorstat=0;
  m_nch=nch;
  m_encoder_nch = stereomode == 3 ? 1 : m_nch;
  m_id3_len=m_ixml_len=0;

  m_lamestate=lame.init();
  if (!m_lamestate)
  {
    errorstat=1; 
    return;
  }

  lame.set_in_samplerate(m_lamestate, srate);
  lame.set_num_channels(m_lamestate,m_encoder_nch);
  int outrate=srate;

  int maxbr = (vbrmethod != -1 ? vbrmax : bitrate);
  if (outrate>=32000 && maxbr <= 32*m_encoder_nch) outrate/=2;

  lame.set_out_samplerate(m_lamestate,outrate);
  lame.set_quality(m_lamestate,(quality>9 ||quality<0) ? 0 : quality);
  if (m_encoder_nch == 1 || stereomode >= 0)
    lame.set_mode(m_lamestate,(MPEG_mode) (m_encoder_nch==1?3 :stereomode ));
  lame.set_brate(m_lamestate,bitrate);
  
  //int vbrmethod (-1 no vbr), int vbrquality (nVBRQuality), int vbrmax, int abr
  if (vbrmethod != -1 && lame.set_VBR)
  {
    int vm=4; // mtrh
    if (vbrmethod == 4) vm = 3; //ABR
    lame.set_VBR(m_lamestate,vm);
    
    if (lame.set_VBR_q) lame.set_VBR_q(m_lamestate,vbrquality);
    
    if (vbrmethod == 4&&lame.set_VBR_mean_bitrate_kbps)
    {
      lame.set_VBR_mean_bitrate_kbps(m_lamestate,abr);
    }
    if (lame.set_VBR_max_bitrate_kbps)
    {
      lame.set_VBR_max_bitrate_kbps(m_lamestate,vbrmax);
    }
    if (lame.set_VBR_min_bitrate_kbps)
    {
      lame.set_VBR_min_bitrate_kbps(m_lamestate,bitrate);
    }
  }
  if (rpgain>0 && lame.set_findReplayGain) lame.set_findReplayGain(m_lamestate,1);

  if (metadata && metadata->GetSize())
  {
    WDL_HeapBuf hb;
    if (PackID3Chunk(&hb, metadata, true, &m_ixml_len, 0))
    {
      outqueue.Add(hb.Get(), hb.GetSize());
      m_id3_len=hb.GetSize();
    }
    PackApeChunk(&m_apetag, metadata);
  }

  lame.init_params(m_lamestate);

  in_size_samples=lame.get_framesize(m_lamestate);

  outtmp.Resize(65536);
}

void LameEncoder::Encode(float *in, int in_spls, int spacing)
{
  if (errorstat) return;

  if (in_spls > 0)
  {
    if (m_nch > 1 && m_encoder_nch==1)
    {
      // downmix
      int x;
      int pos=0;
      int adv=2*spacing;
      for (x = 0; x < in_spls; x ++)
      {
        float f=in[pos]+in[pos+1];
        f*=16384.0f;
        spltmp[0].Add(&f,sizeof(float));
        pos+=adv;
      }
    }
    else if (m_encoder_nch > 1) // deinterleave
    {
      int x;
      int pos=0;
      int adv=2*spacing;
      for (x = 0; x < in_spls; x ++)
      {
        float f=in[pos];
        f*=32768.0f;
        spltmp[0].Add(&f,sizeof(float));

        f=in[pos+1];
        f*=32768.0f;
        spltmp[1].Add(&f,sizeof(float));

        pos+=adv;
      }
    }
    else 
    {
      int x;
      int pos=0;
      for (x = 0; x < in_spls; x ++)
      {
        float f=in[pos];
        f*=32768.0f;
        spltmp[0].Add(&f,sizeof(float));

        pos+=spacing;
      }
    }
  }
  for (;;)
  {
    int a = spltmp[0].Available()/sizeof(float);
    if (a >= in_size_samples) a = in_size_samples;
    else if (a<1 || in_spls>0) break; // not enough samples available, and not flushing

    int dwo=lame.encode_buffer_float(m_lamestate,(float *)spltmp[0].Get(),(float*)spltmp[m_encoder_nch>1].Get(), a,(unsigned char *)outtmp.Get(),outtmp.GetSize());
    outqueue.Add(outtmp.Get(),dwo);
    spltmp[0].Advance(a*sizeof(float));
    if (m_encoder_nch > 1) spltmp[1].Advance(a*sizeof(float));
  }

  if (in_spls<1)
  {
    int a=lame.encode_flush(m_lamestate,(unsigned char *)outtmp.Get(),outtmp.GetSize());
    if (a>0) outqueue.Add(outtmp.Get(),a);
  }



  spltmp[0].Compact();
  spltmp[1].Compact();

}

#ifdef _WIN32

static BOOL HasUTF8(const char *_str)
{
  const unsigned char *str = (const unsigned char *)_str;
  if (!str) return FALSE;
  while (*str) 
  {
    unsigned char c = *str;
    if (c >= 0xC2) // discard overlongs
    {
      if (c <= 0xDF && str[1] >=0x80 && str[1] <= 0xBF) return TRUE;
      else if (c <= 0xEF && str[1] >=0x80 && str[1] <= 0xBF && str[2] >=0x80 && str[2] <= 0xBF) return TRUE;
      else if (c <= 0xF4 && str[1] >=0x80 && str[1] <= 0xBF && str[2] >=0x80 && str[2] <= 0xBF) return TRUE;
    }
    str++;
  }
  return FALSE;
}
#endif


LameEncoder::~LameEncoder()
{
  if (m_lamestate)
  {
    if (m_vbrfile.Get()[0] && lame.get_lametag_frame)
    {
      unsigned char buf[16384];
      size_t a=lame.get_lametag_frame(m_lamestate,buf,sizeof(buf));
      if ((a>0 && a<=sizeof(buf)) || m_apetag.GetSize())
      {
        FILE *fp = fopenUTF8(m_vbrfile.Get(),"r+b");
        if (fp)
        {
          if (a > 0 && a <= sizeof(buf))
          {
            fseek(fp, m_id3_len, SEEK_SET);
            fwrite(buf,1,a,fp);
          }
          if (m_apetag.GetSize())
          {
            fseek(fp, 0, SEEK_END);
            fwrite(m_apetag.Get(), 1, m_apetag.GetSize(), fp);
          }
          fclose(fp);
        }
      }
    }
    lame.close(m_lamestate);
    m_lamestate=0;
  }
}