13425
|
1 #include "m_option.h"
|
|
2 #include "../mp_msg.h"
|
|
3 #include <inttypes.h>
|
|
4 #include "ae_toolame.h"
|
|
5
|
|
6
|
|
7 static int
|
|
8 param_bitrate = 192,
|
|
9 param_srate = 48000,
|
|
10 param_psy = 3,
|
|
11 param_maxvbr = 192,
|
|
12 param_errprot = 0,
|
|
13 param_debug = 0;
|
|
14
|
|
15 float param_vbr = 0;
|
|
16 static char *param_mode = "stereo";
|
|
17
|
|
18 m_option_t toolameopts_conf[] = {
|
|
19 {"br", ¶m_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
20 {"mode", ¶m_mode, CONF_TYPE_STRING, 0, 0, 0, NULL},
|
|
21 {"psy", ¶m_psy, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
|
|
22 {"vbr", ¶m_vbr, CONF_TYPE_FLOAT, CONF_RANGE, 0, 50, NULL},
|
|
23 {"maxvbr", ¶m_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
24 {"errprot", ¶m_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL},
|
|
25 {"debug", ¶m_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
|
|
26 {NULL, NULL, 0, 0, 0, 0, NULL}
|
|
27 };
|
|
28
|
|
29
|
|
30 mpae_toolame_ctx *mpae_init_toolame(int channels, int srate)
|
|
31 {
|
|
32 int mode;
|
|
33 mpae_toolame_ctx *ctx = NULL;
|
|
34
|
|
35 if(channels == 1)
|
|
36 {
|
|
37 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_toolame, 1 audio channel, forcing mono mode\n");
|
|
38 mode = MPG_MD_MONO;
|
|
39 }
|
|
40 else if(channels == 2)
|
|
41 {
|
|
42 if(! strcasecmp(param_mode, "dual"))
|
|
43 mode = MPG_MD_DUAL_CHANNEL;
|
|
44 else if(! strcasecmp(param_mode, "jstereo"))
|
|
45 mode = MPG_MD_JOINT_STEREO;
|
|
46 else if(! strcasecmp(param_mode, "stereo"))
|
|
47 mode = MPG_MD_STEREO;
|
|
48 else
|
|
49 {
|
|
50 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, unknown mode %s, exiting\n", param_mode);
|
|
51 }
|
|
52 }
|
|
53 else
|
|
54 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, Toolame can't encode > 2 channels, exiting\n");
|
|
55
|
|
56 ctx = (mpae_toolame_ctx *) calloc(1, sizeof(mpae_toolame_ctx));
|
|
57 if(ctx == NULL)
|
|
58 {
|
|
59 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_toolame_ctx));
|
|
60 return NULL;
|
|
61 }
|
|
62
|
|
63 ctx->toolame_ctx = toolame_init();
|
|
64 if(ctx->toolame_ctx == NULL)
|
|
65 {
|
|
66 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_toolame, couldn't initial parameters from libtoolame, exiting\n");
|
|
67 free(ctx);
|
|
68 return NULL;
|
|
69 }
|
|
70 ctx->channels = channels;
|
|
71 ctx->srate = srate;
|
|
72
|
|
73 if(toolame_setMode(ctx->toolame_ctx, mode) != 0)
|
|
74 return NULL;
|
|
75
|
|
76 if(toolame_setPsymodel(ctx->toolame_ctx, param_psy) != 0)
|
|
77 return NULL;
|
|
78
|
|
79 if(toolame_setSampleFreq(ctx->toolame_ctx, srate) != 0)
|
|
80 return NULL;
|
|
81
|
|
82 if(toolame_setBitrate(ctx->toolame_ctx, param_bitrate) != 0)
|
|
83 return NULL;
|
|
84
|
|
85 if(param_errprot)
|
|
86 if(toolame_setErrorProtection(ctx->toolame_ctx, TRUE) != 0)
|
|
87 return NULL;
|
|
88
|
|
89 if(param_vbr > 0)
|
|
90 {
|
|
91 if(toolame_setVBR(ctx->toolame_ctx, TRUE) != 0)
|
|
92 return NULL;
|
|
93 if(toolame_setVBRLevel(ctx->toolame_ctx, param_maxvbr) != 0)
|
|
94 return NULL;
|
|
95 if(toolame_setPadding(ctx->toolame_ctx, FALSE) != 0)
|
|
96 return NULL;
|
|
97 if(toolame_setVBRUpperBitrate(ctx->toolame_ctx, param_maxvbr) != 0)
|
|
98 return NULL;
|
|
99 }
|
|
100
|
|
101 if(toolame_setVerbosity(ctx->toolame_ctx, param_debug) != 0)
|
|
102 return NULL;
|
|
103
|
|
104 if(toolame_init_params(ctx->toolame_ctx) != 0)
|
|
105 return NULL;
|
|
106
|
|
107 ctx->bitrate = param_bitrate;
|
|
108
|
|
109 return ctx;
|
|
110 }
|
|
111
|
|
112
|
|
113 int mpae_encode_toolame(mpae_toolame_ctx *ctx, uint8_t *dest, int nsamples, void *src, int max_size)
|
|
114 {
|
|
115 int ret_size = 0, i;
|
|
116 int16_t *buffer;
|
|
117
|
|
118 buffer = (uint16_t *) src;
|
|
119 for(i = 0; i < nsamples; i++)
|
|
120 {
|
|
121 ctx->left_pcm[i] = buffer[2 * i];
|
|
122 ctx->right_pcm[i] = buffer[2 * i + (ctx->channels - 1)];
|
|
123 }
|
|
124
|
|
125 toolame_encode_buffer(ctx->toolame_ctx, ctx->left_pcm, ctx->right_pcm, nsamples, dest, max_size, &ret_size);
|
|
126
|
|
127 return ret_size;
|
|
128 }
|