annotate mp3lameaudio.c @ 632:f5e68f32069f libavcodec

dont discard first frame patch by (Lennert Buytenhek <buytenh at math dot leidenuniv dot nl>)
author michaelni
date Sun, 01 Sep 2002 07:40:31 +0000
parents 718a22dc121f
children 9a78dac52f4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
1 /*
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
2 * Interface to libmp3lame for mp3 encoding
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
3 * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
4 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
5 * This library is free software; you can redistribute it and/or
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
7 * License as published by the Free Software Foundation; either
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
8 * version 2 of the License, or (at your option) any later version.
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
9 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
10 * This library is distributed in the hope that it will be useful,
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
13 * Lesser General Public License for more details.
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
14 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
16 * License along with this library; if not, write to the Free Software
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
18 */
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
19
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
20 #include "avcodec.h"
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
21 #include "mpegaudio.h"
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
22 #include <lame/lame.h>
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
23
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
24 typedef struct Mp3AudioContext {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
25 lame_global_flags *gfp;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
26 int stereo;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
27 } Mp3AudioContext;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
28
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
29
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
30 static int MP3lame_encode_init(AVCodecContext *avctx)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
31 {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
32 Mp3AudioContext *s = avctx->priv_data;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
33
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
34 if (avctx->channels > 2)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
35 return -1;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
36
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
37 s->stereo = avctx->channels > 1 ? 1 : 0;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
38
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
39 if ((s->gfp = lame_init()) == NULL)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
40 goto err;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
41 lame_set_in_samplerate(s->gfp, avctx->sample_rate);
261
d6521bbbab5e - Bug fix on output sample rate for lame MP3 encoding.
pulento
parents: 259
diff changeset
42 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
43 lame_set_num_channels(s->gfp, avctx->channels);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
44 /* lame 3.91 dies on quality != 5 */
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
45 lame_set_quality(s->gfp, 5);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
46 /* lame 3.91 doesn't work in mono */
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
47 lame_set_mode(s->gfp, JOINT_STEREO);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
48 lame_set_brate(s->gfp, avctx->bit_rate/1000);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
49 if (lame_init_params(s->gfp) < 0)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
50 goto err_close;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
51
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
52 avctx->frame_size = MPA_FRAME_SIZE;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
53 avctx->key_frame = 1;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
54
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
55 return 0;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
56
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
57 err_close:
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
58 lame_close(s->gfp);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
59 err:
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
60 return -1;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
61 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
62
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
63 int MP3lame_encode_frame(AVCodecContext *avctx,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
64 unsigned char *frame, int buf_size, void *data)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
65 {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
66 Mp3AudioContext *s = avctx->priv_data;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
67 int num;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
68
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
69 /* lame 3.91 dies on '1-channel interleaved' data */
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
70 if (s->stereo) {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
71 num = lame_encode_buffer_interleaved(s->gfp, data,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
72 MPA_FRAME_SIZE, frame, buf_size);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
73 } else {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
74 num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
75 frame, buf_size);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
76 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
77
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
78 return num;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
79 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
80
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
81 int MP3lame_encode_close(AVCodecContext *avctx)
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
82 {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
83 Mp3AudioContext *s = avctx->priv_data;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
84
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
85 lame_close(s->gfp);
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
86 return 0;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
87 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
88
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
89
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
90 AVCodec mp3lame_encoder = {
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
91 "mp3",
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
92 CODEC_TYPE_AUDIO,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
93 CODEC_ID_MP3LAME,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
94 sizeof(Mp3AudioContext),
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
95 MP3lame_encode_init,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
96 MP3lame_encode_frame,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
97 MP3lame_encode_close
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
98 };