annotate libmpcodecs/ae_toolame.c @ 15203:72bde716c1b8

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