annotate libmpcodecs/ae_twolame.c @ 35253:48db1b241757

Try to do something sensible when malloc fails instead of continuing which is sure to crash.
author reimar
date Mon, 05 Nov 2012 20:45:40 +0000
parents d332ea379205
children 389d43c448b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30421
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
1 /*
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
2 * This file is part of MPlayer.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
3 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
5 * it under the terms of the GNU General Public License as published by
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
7 * (at your option) any later version.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
8 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
12 * GNU General Public License for more details.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
13 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
14 * You should have received a copy of the GNU General Public License along
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
17 */
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
18
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
19 #include <stdio.h>
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
20 #include <stdlib.h>
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
21 #include <inttypes.h>
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
22 #include <unistd.h>
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
23 #include <string.h>
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
24 #include <sys/types.h>
31437
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
25 #include <twolame.h>
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
26
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
27 #include "m_option.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16482
diff changeset
28 #include "mp_msg.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
29 #include "libmpdemux/aviheader.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16482
diff changeset
30 #include "libaf/af_format.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
31 #include "libmpdemux/ms_hdr.h"
22600
3c2b4a866c6a Add explicit location for headers from the stream/ directory.
diego
parents: 21660
diff changeset
32 #include "stream/stream.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
33 #include "libmpdemux/muxer.h"
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
34 #include "ae_twolame.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16482
diff changeset
35 #include "libmpdemux/mp3_hdr.h"
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
36
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
37
31437
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
38 typedef struct {
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
39 twolame_options *twolame_ctx;
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
40 int vbr;
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
41 } mpae_twolame_ctx;
327eedc724c5 Move mpae_twolame_ctx structure to ae_twolame.c.
diego
parents: 30955
diff changeset
42
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
43 static int
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
44 param_bitrate = 192,
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
45 param_psy = 3,
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
46 param_maxvbr = 0,
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
47 param_errprot = 0,
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
48 param_debug = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
49
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
50 static float param_vbr = 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
51 static char *param_mode = "stereo";
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
52
30955
4e59a7aebadb Mark encoder-related m_option_t struct arrays as const.
diego
parents: 30741
diff changeset
53 const m_option_t twolameopts_conf[] = {
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
54 {"br", &param_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
55 {"mode", &param_mode, CONF_TYPE_STRING, 0, 0, 0, NULL},
15362
4ff00aa141ef updated psycho model range; made a parameter file-static in ae_toolame.c
nicodvb
parents: 15359
diff changeset
56 {"psy", &param_psy, CONF_TYPE_INT, CONF_RANGE, -1, 4, NULL},
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
57 {"vbr", &param_vbr, CONF_TYPE_FLOAT, CONF_RANGE, -50, 50, NULL},
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
58 {"maxvbr", &param_maxvbr, CONF_TYPE_INT, 0, 0, 0, NULL},
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
59 {"errprot", &param_errprot, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL},
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
60 {"debug", &param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
61 {NULL, NULL, 0, 0, 0, 0, NULL}
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
62 };
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
63
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
64
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
65 static int bind_twolame(audio_encoder_t *encoder, muxer_stream_t *mux_a)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
66 {
27843
39d9ff988bc0 Avoid pointless casting of void*
reimar
parents: 22601
diff changeset
67 mpae_twolame_ctx *ctx = encoder->priv;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
68
32121
d332ea379205 Replace sizeof(WAVEFORMATEX) occurrences.
reimar
parents: 31437
diff changeset
69 mux_a->wf = malloc(sizeof(*mux_a->wf)+256);
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
70 mux_a->wf->wFormatTag = 0x50;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
71 mux_a->wf->nChannels = encoder->params.channels;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
72 mux_a->wf->nSamplesPerSec = encoder->params.sample_rate;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
73 mux_a->wf->nAvgBytesPerSec = encoder->params.bitrate / 8;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
74
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
75 if(ctx->vbr || ((mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame) % mux_a->wf->nSamplesPerSec))
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
76 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
77 mux_a->h.dwScale = encoder->params.samples_per_frame;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
78 mux_a->h.dwRate = encoder->params.sample_rate;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
79 mux_a->h.dwSampleSize = 0; // Blocksize not constant
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
80 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
81 else
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
82 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
83 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * encoder->params.samples_per_frame)/ mux_a->wf->nSamplesPerSec; /* for cbr */
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
84 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
85 mux_a->h.dwSampleSize = mux_a->h.dwScale;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
86 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
87 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
88 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
89 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
90
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
91 mux_a->wf->cbSize = 0; //12;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
92 mux_a->wf->wBitsPerSample = 0; /* does not apply */
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
93 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
94 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
95 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
96 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
97 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
98
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
99 // Fix allocation
32121
d332ea379205 Replace sizeof(WAVEFORMATEX) occurrences.
reimar
parents: 31437
diff changeset
100 mux_a->wf = realloc(mux_a->wf, sizeof(*mux_a->wf)+mux_a->wf->cbSize);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
101
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
102 encoder->input_format = AF_FORMAT_S16_NE;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
103 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
104 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
105
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
106 return 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
107 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
108
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
109 static int encode_twolame(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
110 {
27843
39d9ff988bc0 Avoid pointless casting of void*
reimar
parents: 22601
diff changeset
111 mpae_twolame_ctx *ctx = encoder->priv;
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
112 int ret_size = 0, r2;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
113
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
114 len /= (2*encoder->params.channels);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
115 ret_size = twolame_encode_buffer_interleaved(ctx->twolame_ctx, src, len, dest, max_size);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
116 r2 = mp_decode_mp3_header(dest);
16482
82698fb46c73 reduced verbosity
nicodvb
parents: 15362
diff changeset
117 mp_msg(MSGT_MENCODER, MSGL_DBG2, "\nSIZE: %d, max: %d, r2: %d\n", ret_size, max_size, r2);
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
118 if(r2 > 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
119 ret_size = r2;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
120 return ret_size;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
121 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
122
30741
3cf4f49c2e87 Make close_twolame() static, only used in ae_twolame.c.
cehoyos
parents: 30633
diff changeset
123 static int close_twolame(audio_encoder_t *encoder)
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
124 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
125 free(encoder->priv);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
126 return 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
127 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
128
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
129 static int get_frame_size(audio_encoder_t *encoder)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
130 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
131 int sz;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
132 if(encoder->stream->buffer_len < 4)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
133 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
134 sz = mp_decode_mp3_header(encoder->stream->buffer);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
135 if(sz <= 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
136 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
137 return sz;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
138 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
139
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
140
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
141 int mpae_init_twolame(audio_encoder_t *encoder)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
142 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
143 int mode;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
144 mpae_twolame_ctx *ctx = NULL;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
145
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
146 if(encoder->params.channels == 1)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
147 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
148 mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_twolame, 1 audio channel, forcing mono mode\n");
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
149 mode = TWOLAME_MONO;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
150 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
151 else if(encoder->params.channels == 2)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
152 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
153 if(! strcasecmp(param_mode, "dual"))
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
154 mode = TWOLAME_DUAL_CHANNEL;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
155 else if(! strcasecmp(param_mode, "jstereo"))
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
156 mode = TWOLAME_JOINT_STEREO;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
157 else if(! strcasecmp(param_mode, "stereo"))
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
158 mode = TWOLAME_STEREO;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
159 else
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
160 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
161 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, unknown mode %s, exiting\n", param_mode);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
162 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
163 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
164 else
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
165 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, Twolame can't encode > 2 channels, exiting\n");
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
166
27843
39d9ff988bc0 Avoid pointless casting of void*
reimar
parents: 22601
diff changeset
167 ctx = calloc(1, sizeof(mpae_twolame_ctx));
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
168 if(ctx == NULL)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
169 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
170 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_twolame_ctx));
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
171 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
172 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
173
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
174 ctx->twolame_ctx = twolame_init();
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
175 if(ctx->twolame_ctx == NULL)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
176 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
177 mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't initial parameters from libtwolame, exiting\n");
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
178 free(ctx);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
179 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
180 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
181 ctx->vbr = 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
182
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
183 if(twolame_set_num_channels(ctx->twolame_ctx, encoder->params.channels) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
184 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
185 if(twolame_set_mode(ctx->twolame_ctx, mode) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
186 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
187
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
188 if(twolame_set_in_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
189 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
190
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
191 if(twolame_set_out_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
192 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
193
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
194 if(encoder->params.sample_rate < 32000)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
195 twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG2);
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
196 else
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
197 twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG1);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
198
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
199 if(twolame_set_psymodel(ctx->twolame_ctx, param_psy) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
200 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
201
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
202 if(twolame_set_bitrate(ctx->twolame_ctx, param_bitrate) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
203 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
204
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
205 if(param_errprot)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
206 if(twolame_set_error_protection(ctx->twolame_ctx, TRUE) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
207 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
208
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
209 if(param_vbr != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
210 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
211 if(twolame_set_VBR(ctx->twolame_ctx, TRUE) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
212 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
213 if(twolame_set_VBR_q(ctx->twolame_ctx, param_vbr) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
214 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
215 if(twolame_set_padding(ctx->twolame_ctx, FALSE) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
216 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
217 if(param_maxvbr)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
218 {
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
219 if(twolame_set_VBR_max_bitrate_kbps(ctx->twolame_ctx, param_maxvbr) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
220 return 0;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
221 }
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
222 ctx->vbr = 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
223 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
224
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
225 if(twolame_set_verbosity(ctx->twolame_ctx, param_debug) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
226 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
227
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
228 if(twolame_init_params(ctx->twolame_ctx) != 0)
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
229 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
230
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
231 encoder->params.bitrate = param_bitrate * 1000;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
232 encoder->params.samples_per_frame = 1152;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
233 encoder->priv = ctx;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
234 encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
235
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
236 encoder->bind = bind_twolame;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
237 encoder->get_frame_size = get_frame_size;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
238 encoder->encode = encode_twolame;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
239 encoder->close = close_twolame;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27843
diff changeset
240
15359
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
241 return 1;
af245d15f502 added twolame mp2 audio encoder
nicodvb
parents:
diff changeset
242 }