Mercurial > libavcodec.hg
annotate mp3lameaudio.c @ 307:764aeec1320e libavcodec
msmpeg4v2 decoding (no encoding yet)
author | michaelni |
---|---|
date | Fri, 05 Apr 2002 04:09:04 +0000 |
parents | 0bae80322c6d |
children | fce0a2520551 |
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 * |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
5 * This program is free software; you can redistribute it and/or modify |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
6 * it under the terms of the GNU General Public License as published by |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
7 * the Free Software Foundation; either version 2 of the License, or |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
8 * (at your option) any later version. |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
9 * |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
10 * This program is distributed in the hope that it will be useful, |
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 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
13 * GNU General Public License for more details. |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
14 * |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
16 * along with this program; if not, write to the Free Software |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
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 <math.h> |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
22 #include "mpegaudio.h" |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
23 #include <lame/lame.h> |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
24 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
25 typedef struct Mp3AudioContext { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
26 lame_global_flags *gfp; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
27 int first_frame; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
28 int stereo; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
29 } Mp3AudioContext; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
30 |
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 static int MP3lame_encode_init(AVCodecContext *avctx) |
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 Mp3AudioContext *s = avctx->priv_data; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
35 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
36 if (avctx->channels > 2) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
37 return -1; |
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 s->first_frame = 1; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
40 s->stereo = avctx->channels > 1 ? 1 : 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
41 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
42 if ((s->gfp = lame_init()) == NULL) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
43 goto err; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
44 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
|
45 lame_set_out_samplerate(s->gfp, avctx->sample_rate); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
46 lame_set_num_channels(s->gfp, avctx->channels); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
47 /* lame 3.91 dies on quality != 5 */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
48 lame_set_quality(s->gfp, 5); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
49 /* lame 3.91 doesn't work in mono */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
50 lame_set_mode(s->gfp, JOINT_STEREO); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
51 lame_set_brate(s->gfp, avctx->bit_rate/1000); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
52 if (lame_init_params(s->gfp) < 0) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
53 goto err_close; |
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 avctx->frame_size = MPA_FRAME_SIZE; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
56 avctx->key_frame = 1; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
57 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
58 return 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
59 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
60 err_close: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
61 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
62 err: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
63 return -1; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
64 } |
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 int MP3lame_encode_frame(AVCodecContext *avctx, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
67 unsigned char *frame, int buf_size, void *data) |
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 Mp3AudioContext *s = avctx->priv_data; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
70 int num; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
71 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
72 /* lame 3.91 dies on '1-channel interleaved' data */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
73 if (s->stereo) { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
74 num = lame_encode_buffer_interleaved(s->gfp, data, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
75 MPA_FRAME_SIZE, frame, buf_size); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
76 } else { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
77 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
|
78 frame, buf_size); |
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 /* lame 3.91 outputs the first frame as garbage */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
82 if (s->first_frame) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
83 s->first_frame = num = 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
84 return num; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
85 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
86 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
87 int MP3lame_encode_close(AVCodecContext *avctx) |
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 Mp3AudioContext *s = avctx->priv_data; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
90 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
91 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
92 return 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
93 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
94 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
95 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
96 AVCodec mp3lame_encoder = { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
97 "mp3", |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
98 CODEC_TYPE_AUDIO, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
99 CODEC_ID_MP3LAME, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
100 sizeof(Mp3AudioContext), |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
101 MP3lame_encode_init, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
102 MP3lame_encode_frame, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
103 MP3lame_encode_close |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
104 }; |