annotate libmp3lame.c @ 7459:283eeda62184 libavcodec

Modify av_audio_convert() to use AVAudioConvert context struct; add av_audio_convert_alloc() and av_audio_convert_free() support functions.
author pross
date Fri, 01 Aug 2008 13:53:18 +0000
parents 3c616a62a446
children ea663d76fcfa
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 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 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
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg 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
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
15 * Lesser General Public License for more details.
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
16 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
20 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
21
1106
1e39f273ecd6 per file doxy
michaelni
parents: 926
diff changeset
22 /**
1e39f273ecd6 per file doxy
michaelni
parents: 926
diff changeset
23 * @file mp3lameaudio.c
1e39f273ecd6 per file doxy
michaelni
parents: 926
diff changeset
24 * Interface to libmp3lame for mp3 encoding.
1e39f273ecd6 per file doxy
michaelni
parents: 926
diff changeset
25 */
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
26
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
27 #include "avcodec.h"
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
28 #include "mpegaudio.h"
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
29 #include <lame/lame.h>
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
30
5395
87d908152cb7 increase buffer size of LAME MP3 encoder
gpoirier
parents: 5127
diff changeset
31 #define BUFFER_SIZE (7200 + MPA_FRAME_SIZE + MPA_FRAME_SIZE/4)
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
32 typedef struct Mp3AudioContext {
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
33 lame_global_flags *gfp;
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
34 int stereo;
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
35 uint8_t buffer[BUFFER_SIZE];
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
36 int buffer_index;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
37 } Mp3AudioContext;
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
38
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6346
diff changeset
39 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
40 {
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
41 Mp3AudioContext *s = avctx->priv_data;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
42
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
43 if (avctx->channels > 2)
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
44 return -1;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
45
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
46 s->stereo = avctx->channels > 1 ? 1 : 0;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
47
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
48 if ((s->gfp = lame_init()) == NULL)
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
49 goto err;
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
50 lame_set_in_samplerate(s->gfp, avctx->sample_rate);
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
51 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
52 lame_set_num_channels(s->gfp, avctx->channels);
7457
3c616a62a446 Use compression level to set mp3lame quality option.
benoit
parents: 7451
diff changeset
53 if(avctx->compression_level == FF_COMPRESSION_DEFAULT) {
3c616a62a446 Use compression level to set mp3lame quality option.
benoit
parents: 7451
diff changeset
54 lame_set_quality(s->gfp, 5);
3c616a62a446 Use compression level to set mp3lame quality option.
benoit
parents: 7451
diff changeset
55 } else {
3c616a62a446 Use compression level to set mp3lame quality option.
benoit
parents: 7451
diff changeset
56 lame_set_quality(s->gfp, avctx->compression_level);
3c616a62a446 Use compression level to set mp3lame quality option.
benoit
parents: 7451
diff changeset
57 }
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
58 /* lame 3.91 doesn't work in mono */
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
59 lame_set_mode(s->gfp, JOINT_STEREO);
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
60 lame_set_brate(s->gfp, avctx->bit_rate/1000);
2841
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 2522
diff changeset
61 if(avctx->flags & CODEC_FLAG_QSCALE) {
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 2522
diff changeset
62 lame_set_brate(s->gfp, 0);
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 2522
diff changeset
63 lame_set_VBR(s->gfp, vbr_default);
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 2522
diff changeset
64 lame_set_VBR_q(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 2522
diff changeset
65 }
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
66 lame_set_bWriteVbrTag(s->gfp,0);
6346
11a44fc273a5 Add 'reservoir' flag to lavc's flags2 controlling whether or not codes may
andoma
parents: 5395
diff changeset
67 lame_set_disable_reservoir(s->gfp, avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR ? 0 : 1);
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
68 if (lame_init_params(s->gfp) < 0)
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
69 goto err_close;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
70
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
71 avctx->frame_size = lame_get_framesize(s->gfp);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
72
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
73 avctx->coded_frame= avcodec_alloc_frame();
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
74 avctx->coded_frame->key_frame= 1;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
75
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
76 return 0;
259
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 err_close:
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
79 lame_close(s->gfp);
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
80 err:
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
81 return -1;
259
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
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
84 static const int sSampleRates[3] = {
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
85 44100, 48000, 32000
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
86 };
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
87
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
88 static const int sBitRates[2][3][15] = {
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
89 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
90 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
91 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
92 },
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
93 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
94 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
95 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
96 },
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
97 };
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
98
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
99 static const int sSamplesPerFrame[2][3] =
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
100 {
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
101 { 384, 1152, 1152 },
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
102 { 384, 1152, 576 }
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
103 };
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
104
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
105 static const int sBitsPerSlot[3] = {
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
106 32,
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
107 8,
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
108 8
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
109 };
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
110
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
111 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
112 {
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4170
diff changeset
113 uint32_t header = AV_RB32(data);
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
114 int layerID = 3 - ((header >> 17) & 0x03);
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
115 int bitRateID = ((header >> 12) & 0x0f);
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
116 int sampleRateID = ((header >> 10) & 0x03);
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
117 int bitsPerSlot = sBitsPerSlot[layerID];
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
118 int isPadded = ((header >> 9) & 0x01);
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
119 static int const mode_tab[4]= {2,3,1,0};
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
120 int mode= mode_tab[(header >> 19) & 0x03];
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
121 int mpeg_id= mode>0;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
122 int temp0, temp1, bitRate;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
123
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
124 if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
125 return -1;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
126 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
127
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
128 if(!samplesPerFrame) samplesPerFrame= &temp0;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
129 if(!sampleRate ) sampleRate = &temp1;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
130
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
131 // *isMono = ((header >> 6) & 0x03) == 0x03;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
132
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
133 *sampleRate = sSampleRates[sampleRateID]>>mode;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
134 bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
135 *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
136 //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
137
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
138 return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
139 }
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
140
4170
f97a2081b5b1 make some symbols static
mru
parents: 4119
diff changeset
141 static int MP3lame_encode_frame(AVCodecContext *avctx,
f97a2081b5b1 make some symbols static
mru
parents: 4119
diff changeset
142 unsigned char *frame, int buf_size, void *data)
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
143 {
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
144 Mp3AudioContext *s = avctx->priv_data;
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
145 int len;
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
146 int lame_result;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
147
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
148 /* lame 3.91 dies on '1-channel interleaved' data */
2091
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
149
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
150 if(data){
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
151 if (s->stereo) {
1941
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
152 lame_result = lame_encode_buffer_interleaved(
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
153 s->gfp,
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
154 data,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
155 avctx->frame_size,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
156 s->buffer + s->buffer_index,
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
157 BUFFER_SIZE - s->buffer_index
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
158 );
2091
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
159 } else {
1941
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
160 lame_result = lame_encode_buffer(
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
161 s->gfp,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
162 data,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
163 data,
1941
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
164 avctx->frame_size,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
165 s->buffer + s->buffer_index,
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
166 BUFFER_SIZE - s->buffer_index
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
167 );
2091
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
168 }
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
169 }else{
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
170 lame_result= lame_encode_flush(
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
171 s->gfp,
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
172 s->buffer + s->buffer_index,
2091
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
173 BUFFER_SIZE - s->buffer_index
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
174 );
1941
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
175 }
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
176
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
177 if(lame_result==-1) {
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
178 /* output buffer too small */
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
179 av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index);
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
180 return 0;
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
181 }
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
182
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
183 s->buffer_index += lame_result;
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
184
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
185 if(s->buffer_index<4)
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
186 return 0;
1871
9457292b0b65 disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents: 1448
diff changeset
187
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
188 len= mp3len(s->buffer, NULL, NULL);
1941
81a1a5be13aa fixed buffering for low bitrates patch by (Hauke Duden <H.NS.Duden at gmx dot net>)
michael
parents: 1913
diff changeset
189 //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index);
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
190 if(len <= s->buffer_index){
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
191 memcpy(frame, s->buffer, len);
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
192 s->buffer_index -= len;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
193
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
194 memmove(s->buffer, s->buffer+len, s->buffer_index);
5127
4dbe6578f811 misc spelling fixes
diego
parents: 5103
diff changeset
195 //FIXME fix the audio codec API, so we do not need the memcpy()
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
196 /*for(i=0; i<len; i++){
1871
9457292b0b65 disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents: 1448
diff changeset
197 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
198 }*/
1913
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
199 return len;
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
200 }else
486236d25f89 split stream into valid mp3 frames, at least flv & nut absolutely need this, but probably most other formats too
michael
parents: 1871
diff changeset
201 return 0;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
202 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
203
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6346
diff changeset
204 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
205 {
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
206 Mp3AudioContext *s = avctx->priv_data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
207
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
208 av_freep(&avctx->coded_frame);
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
209
4119
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
210 lame_close(s->gfp);
85438e10d72d reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents: 3947
diff changeset
211 return 0;
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
212 }
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
213
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
214
5103
1deb3e53da27 Consistent naming and lib prefixes for x264, xvid and mp3lame within the code.
diego
parents: 5101
diff changeset
215 AVCodec libmp3lame_encoder = {
1deb3e53da27 Consistent naming and lib prefixes for x264, xvid and mp3lame within the code.
diego
parents: 5101
diff changeset
216 "libmp3lame",
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
217 CODEC_TYPE_AUDIO,
1448
317ba7ab73bd CODEC_ID_MP3LAME is obsolete
bellard
parents: 1106
diff changeset
218 CODEC_ID_MP3,
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
219 sizeof(Mp3AudioContext),
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
220 MP3lame_encode_init,
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
221 MP3lame_encode_frame,
2091
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
222 MP3lame_encode_close,
02a4fd7c606c flush audio encoder buffers at the end
michael
parents: 1941
diff changeset
223 .capabilities= CODEC_CAP_DELAY,
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7040
diff changeset
224 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
225 .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
259
cc6292eacba6 - Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff changeset
226 };