Mercurial > libavcodec.hg
annotate mp3lameaudio.c @ 1893:779bdf5063ce libavcodec
1000000l (using uninitalized variable for initalizing bits per MV table)
author | michael |
---|---|
date | Tue, 16 Mar 2004 16:57:27 +0000 |
parents | 9457292b0b65 |
children | 486236d25f89 |
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 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
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 | 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 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
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 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
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 */ |
1106 | 19 |
20 /** | |
21 * @file mp3lameaudio.c | |
22 * Interface to libmp3lame for mp3 encoding. | |
23 */ | |
259
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 #include "avcodec.h" |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
26 #include "mpegaudio.h" |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
27 #include <lame/lame.h> |
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 typedef struct Mp3AudioContext { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
30 lame_global_flags *gfp; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
31 int stereo; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
32 } Mp3AudioContext; |
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 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
35 static int MP3lame_encode_init(AVCodecContext *avctx) |
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 Mp3AudioContext *s = avctx->priv_data; |
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 (avctx->channels > 2) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
40 return -1; |
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 s->stereo = avctx->channels > 1 ? 1 : 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
43 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
44 if ((s->gfp = lame_init()) == NULL) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
45 goto err; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
46 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
|
47 lame_set_out_samplerate(s->gfp, avctx->sample_rate); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
48 lame_set_num_channels(s->gfp, avctx->channels); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
49 /* lame 3.91 dies on quality != 5 */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
50 lame_set_quality(s->gfp, 5); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
51 /* lame 3.91 doesn't work in mono */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
52 lame_set_mode(s->gfp, JOINT_STEREO); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
53 lame_set_brate(s->gfp, avctx->bit_rate/1000); |
1871
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
54 lame_set_bWriteVbrTag(s->gfp,0); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
55 if (lame_init_params(s->gfp) < 0) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
56 goto err_close; |
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 avctx->frame_size = MPA_FRAME_SIZE; |
926 | 59 |
60 avctx->coded_frame= avcodec_alloc_frame(); | |
61 avctx->coded_frame->key_frame= 1; | |
259
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 return 0; |
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 err_close: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
66 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
67 err: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
68 return -1; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
69 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
70 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
71 int MP3lame_encode_frame(AVCodecContext *avctx, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
72 unsigned char *frame, int buf_size, void *data) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
73 { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
74 Mp3AudioContext *s = avctx->priv_data; |
1871
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
75 int num, i; |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
76 //av_log(avctx, AV_LOG_DEBUG, "%X %d %X\n", (int)frame, buf_size, (int)data); |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
77 // if(data==NULL) |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
78 // return lame_encode_flush(s->gfp, frame, buf_size); |
259
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 /* lame 3.91 dies on '1-channel interleaved' data */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
81 if (s->stereo) { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
82 num = lame_encode_buffer_interleaved(s->gfp, data, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
83 MPA_FRAME_SIZE, frame, buf_size); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
84 } else { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
85 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
|
86 frame, buf_size); |
1871
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
87 |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
88 /*av_log(avctx, AV_LOG_DEBUG, "in:%d out:%d\n", MPA_FRAME_SIZE, num); |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
89 for(i=0; i<num; i++){ |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
90 av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]); |
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
91 }*/ |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
92 } |
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 return num; |
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 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
97 int MP3lame_encode_close(AVCodecContext *avctx) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
98 { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
99 Mp3AudioContext *s = avctx->priv_data; |
926 | 100 |
101 av_freep(&avctx->coded_frame); | |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
102 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
103 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
104 return 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
105 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
106 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
107 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
108 AVCodec mp3lame_encoder = { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
109 "mp3", |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
110 CODEC_TYPE_AUDIO, |
1448 | 111 CODEC_ID_MP3, |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
112 sizeof(Mp3AudioContext), |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
113 MP3lame_encode_init, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
114 MP3lame_encode_frame, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
115 MP3lame_encode_close |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
116 }; |