9 #include "libmpdemux/aviheader.h"
10 #include "libaf/af_format.h"
11 #include "libmpdemux/ms_hdr.h"
12 #include "stream/stream.h"
13 #include "libmpdemux/muxer.h"
14 #include "ae_toolame.h"
15 #include "libmpdemux/mp3_hdr.h"
25 static float param_vbr
= 0;
26 static char *param_mode
= "stereo";
28 m_option_t toolameopts_conf
[] = {
29 {"br", ¶m_bitrate
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
30 {"mode", ¶m_mode
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
31 {"psy", ¶m_psy
, CONF_TYPE_INT
, CONF_RANGE
, -1, 4, NULL
},
32 {"vbr", ¶m_vbr
, CONF_TYPE_FLOAT
, CONF_RANGE
, -50, 50, NULL
},
33 {"maxvbr", ¶m_maxvbr
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
34 {"errprot", ¶m_errprot
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1, NULL
},
35 {"debug", ¶m_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000000, NULL
},
36 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
40 static int bind_toolame(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
42 mpae_toolame_ctx
*ctx
= (mpae_toolame_ctx
*) encoder
->priv
;
44 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+256);
45 mux_a
->wf
->wFormatTag
= 0x50;
46 mux_a
->wf
->nChannels
= encoder
->params
.channels
;
47 mux_a
->wf
->nSamplesPerSec
= encoder
->params
.sample_rate
;
48 mux_a
->wf
->nAvgBytesPerSec
= 125 * encoder
->params
.bitrate
;
50 if(ctx
->vbr
|| ((mux_a
->wf
->nAvgBytesPerSec
* encoder
->params
.samples_per_frame
) % mux_a
->wf
->nSamplesPerSec
))
52 mux_a
->h
.dwScale
= encoder
->params
.samples_per_frame
;
53 mux_a
->h
.dwRate
= encoder
->params
.sample_rate
;
54 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
58 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* encoder
->params
.samples_per_frame
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
59 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
60 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
;
62 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwScale
;
63 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
64 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
66 mux_a
->wf
->cbSize
= 0; //12;
67 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
68 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
69 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
70 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
71 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
72 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
75 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
77 encoder
->input_format
= AF_FORMAT_S16_NE
;
78 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
79 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
84 static int encode_toolame(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int len
, int max_size
)
86 mpae_toolame_ctx
*ctx
= (mpae_toolame_ctx
*)encoder
->priv
;
87 int ret_size
= 0, r2
, i
, nsamples
;
90 nsamples
= len
/ (2*encoder
->params
.channels
);
91 buffer
= (uint16_t *) src
;
92 for(i
= 0; i
< nsamples
; i
++)
94 ctx
->left_pcm
[i
] = buffer
[ctx
->channels
* i
];
95 ctx
->right_pcm
[i
] = buffer
[(ctx
->channels
* i
) + (ctx
->channels
- 1)];
98 toolame_encode_buffer(ctx
->toolame_ctx
, ctx
->left_pcm
, ctx
->right_pcm
, nsamples
, dest
, max_size
, &ret_size
);
99 r2
= mp_decode_mp3_header(dest
);
100 mp_msg(MSGT_MENCODER
, MSGL_DBG2
, "\nSIZE: %d, max: %d, r2: %d\n", ret_size
, max_size
, r2
);
106 int close_toolame(audio_encoder_t
*encoder
)
112 static int get_frame_size(audio_encoder_t
*encoder
)
115 if(encoder
->stream
->buffer_len
< 4)
117 sz
= mp_decode_mp3_header(encoder
->stream
->buffer
);
124 int mpae_init_toolame(audio_encoder_t
*encoder
)
127 mpae_toolame_ctx
*ctx
= NULL
;
129 if(encoder
->params
.channels
== 1)
131 mp_msg(MSGT_MENCODER
, MSGL_INFO
, "ae_toolame, 1 audio channel, forcing mono mode\n");
134 else if(encoder
->params
.channels
== 2)
136 if(! strcasecmp(param_mode
, "dual"))
137 mode
= MPG_MD_DUAL_CHANNEL
;
138 else if(! strcasecmp(param_mode
, "jstereo"))
139 mode
= MPG_MD_JOINT_STEREO
;
140 else if(! strcasecmp(param_mode
, "stereo"))
141 mode
= MPG_MD_STEREO
;
144 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "ae_toolame, unknown mode %s, exiting\n", param_mode
);
148 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "ae_toolame, Toolame can't encode > 2 channels, exiting\n");
150 ctx
= (mpae_toolame_ctx
*) calloc(1, sizeof(mpae_toolame_ctx
));
153 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "ae_toolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_toolame_ctx
));
157 ctx
->toolame_ctx
= toolame_init();
158 if(ctx
->toolame_ctx
== NULL
)
160 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "ae_toolame, couldn't initial parameters from libtoolame, exiting\n");
165 ctx
->channels
= encoder
->params
.channels
;
166 ctx
->srate
= encoder
->params
.sample_rate
;
168 if(toolame_setMode(ctx
->toolame_ctx
, mode
) != 0)
171 if(toolame_setPsymodel(ctx
->toolame_ctx
, param_psy
) != 0)
174 if(toolame_setSampleFreq(ctx
->toolame_ctx
, encoder
->params
.sample_rate
) != 0)
177 if(toolame_setBitrate(ctx
->toolame_ctx
, param_bitrate
) != 0)
181 if(toolame_setErrorProtection(ctx
->toolame_ctx
, TRUE
) != 0)
186 if(toolame_setVBR(ctx
->toolame_ctx
, TRUE
) != 0)
188 if(toolame_setVBRLevel(ctx
->toolame_ctx
, param_vbr
) != 0)
190 if(toolame_setPadding(ctx
->toolame_ctx
, FALSE
) != 0)
194 if(toolame_setVBRUpperBitrate(ctx
->toolame_ctx
, param_maxvbr
) != 0)
200 if(toolame_setVerbosity(ctx
->toolame_ctx
, param_debug
) != 0)
203 if(toolame_init_params(ctx
->toolame_ctx
) != 0)
206 ctx
->bitrate
= param_bitrate
;
207 encoder
->params
.bitrate
= ctx
->bitrate
;
208 encoder
->params
.samples_per_frame
= 1152;
210 encoder
->decode_buffer_size
= 1152 * 2 * encoder
->params
.channels
;
212 encoder
->bind
= bind_toolame
;
213 encoder
->get_frame_size
= get_frame_size
;
214 encoder
->encode
= encode_toolame
;
215 encoder
->close
= close_toolame
;