summaryrefslogtreecommitdiff
path: root/src/liblame_wrapper.cc
blob: d74233e379b04c71b0e9ccb6f6cc81c525057413 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            liblame_wrapper.cc
 *
 *  Sat Jul  2 11:11:34 CEST 2005
 *  Copyright  2005 Bent Bisballe
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of MIaV.
 *
 *  MIaV is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  MIaV is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MIaV; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

/*
 * $Id$
 */

/*
 * $Log$
 * Revision 1.4  2005/07/09 16:23:15  deva
 * Added audio.
 *
 * Revision 1.3  2005/07/07 12:42:19  deva
 * *** empty log message ***
 *
 * Revision 1.2  2005/07/05 23:15:16  deva
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/02 11:39:51  deva
 * Added some audiocode.
 * Moved libfame code out of mov_encoder
 *
 */

#include <config.h>
#include "liblame_wrapper.h"

LibLAMEWrapper::LibLAMEWrapper(Info *i)
{
  info = i;

  // Init library.
  if( (gfp = lame_init()) == NULL) {
    info->error("LAME initialization failed (due to malloc failure!)");
    return;
  }

	lame_set_in_samplerate(gfp, SAMPLE_RATE);
	lame_set_out_samplerate(gfp, SAMPLE_RATE);
	lame_set_num_channels(gfp, CHANNELS);
	// lame 3.91 dies on quality != 5
	lame_set_quality(gfp, 3);
	// lame 3.91 doesn't work in mono
	lame_set_mode(gfp, JOINT_STEREO);
	lame_set_brate(gfp, 112);
  lame_set_strict_ISO(gfp, 1);
  lame_set_num_samples(gfp, SAMPLES);

  lame_set_copyright(gfp, 0);       // is there a copyright on the encoded data?
  lame_set_original(gfp, 1);        // is the encoded data on the original media?
  lame_set_error_protection(gfp, 0);// add 2 byte CRC protection to each frame?
  lame_set_padding_type(gfp, PAD_NO); // PAD_NO, PAD_ALL, PAD_ADJUST, PAD_MAX_INDICATOR 
                                    // 0 = do not pad frames
                                    // 1 = always pad frames
                                    // 2 = adjust padding to satisfy bit rate
  lame_set_extension(gfp, 0);       // private extension bit

  info->info("Lame version %d", lame_get_version(gfp));

	if (lame_init_params(gfp) < 0) {
    info->error("LAME parameter initialization failed.");
    return;
  }

  lame_init_bitstream(gfp);

  audio_buffer[0] = new int16_t[AUDIO_BUFFER_SIZE];
  audio_buffer[1] = new int16_t[AUDIO_BUFFER_SIZE];

  // And now for the dv decoder!
  decoder = NULL;
}

LibLAMEWrapper::~LibLAMEWrapper()
{
  lame_close(gfp);

  delete audio_buffer[0];
  delete audio_buffer[1];
}

Frame *LibLAMEWrapper::encode(Frame *dvframe)
{

  if(!decoder) {
    decoder = dv_decoder_new(FALSE/*this value is unused*/, FALSE, FALSE);
    decoder->quality = DV_QUALITY_BEST;
    
    dv_parse_header(decoder, dvframe->data);
  
    decoder->system = e_dv_system_625_50;  // PAL lines, PAL framerate
    decoder->sampling = e_dv_sample_422;  // 4 bytes y, 2 bytes u, 2 bytes v
    decoder->std = e_dv_std_iec_61834;
    decoder->num_dif_seqs = 12;
  }

  /**
   * Decode audio using libdv
   */
  dv_decode_full_audio( decoder, dvframe->data, audio_buffer );

  /**
   * input pcm data, output (maybe) mp3 frames.
   * This routine handles all buffering, resampling and filtering for you.
   * 
   * The required mp3buf_size can be computed from num_samples, 
   * samplerate and encoding rate, but here is a worst case estimate:
   *
   * return code     number of bytes output in mp3buffer.  can be 0 
   *                 if return code = -1:  mp3buffer was too small
   *
   * mp3buf_size in bytes = 1.25*num_samples + 7200
   *
   * I think a tighter bound could be:  (mt, March 2000)
   * MPEG1:
   *    num_samples*(bitrate/8)/samplerate + 4*1152*(bitrate/8)/samplerate + 512
   * MPEG2:
   *    num_samples*(bitrate/8)/samplerate + 4*576*(bitrate/8)/samplerate + 256
   *
   * but test first if you use that!
   *
   * set mp3buf_size = 0 and LAME will not check if mp3buf_size is
   * large enough.
   *
   * NOTE:
   * if gfp->num_channels=2, but gfp->mode = 3 (mono), the L & R channels
   * will be averaged into the L channel before encoding only the L channel
   * This will overwrite the data in buffer_l[] and buffer_r[].
   * 
   */
  Frame* audio_frame = new Frame(NULL, (int)(1.25 * SAMPLES + 7200));

  const short int    *buffer_l = audio_buffer[0];   // PCM data for left channel
  const short int    *buffer_r = audio_buffer[1];   // PCM data for right channel
  const int           nsamples = SAMPLES;      // number of samples per channel
  unsigned char*      mp3buf = audio_frame->data;        // pointer to encoded MP3 stream
  const int           mp3buf_size = audio_frame->size;   // number of valid octets in this

  int val;
  val = lame_encode_buffer(gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3buf_size);
  // val = lame_encode_mp3_frame(gfp, buffer_l, buffer_r, mp3buf, mp3buf_size);
  
  //  info->info("Framenr: %d", lame_get_frameNum(gfp));

  if(val < 0) {
    switch(val) {
    case -1:  // mp3buf was too small
      info->error("Lame encoding failed, mp3buf was too small.");
      break;
    case -2:  // malloc() problem
      info->error("Lame encoding failed, due to malloc() problem.");
      break;
    case -3:  // lame_init_params() not called
      info->error("Lame encoding failed, lame_init_params() not called.");
      break;
    case -4:  // psycho acoustic problems 
      info->error("Lame encoding failed, due to psycho acoustic problems.");
      break;
    default:
      info->error("Lame encoding failed, due to unknown error.");
      break;
    }
  }

  audio_frame->size = val;

  return audio_frame;
}