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