Mercurial > libavcodec.hg
annotate mp3lameaudio.c @ 1922:0ed2d7ecd1e9 libavcodec
fix obnoxious ogg_packet passing from encoder to muxer
author | michael |
---|---|
date | Sun, 04 Apr 2004 14:39:20 +0000 |
parents | 486236d25f89 |
children | 81a1a5be13aa |
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 |
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
|
29 #define BUFFER_SIZE (2*MPA_FRAME_SIZE) |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
30 typedef struct Mp3AudioContext { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
31 lame_global_flags *gfp; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
32 int stereo; |
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
|
33 uint8_t buffer[BUFFER_SIZE]; |
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
|
34 int buffer_index; |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
35 } Mp3AudioContext; |
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 static int MP3lame_encode_init(AVCodecContext *avctx) |
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 Mp3AudioContext *s = avctx->priv_data; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
40 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
41 if (avctx->channels > 2) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
42 return -1; |
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 s->stereo = avctx->channels > 1 ? 1 : 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
45 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
46 if ((s->gfp = lame_init()) == NULL) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
47 goto err; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
48 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
|
49 lame_set_out_samplerate(s->gfp, avctx->sample_rate); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
50 lame_set_num_channels(s->gfp, avctx->channels); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
51 /* lame 3.91 dies on quality != 5 */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
52 lame_set_quality(s->gfp, 5); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
53 /* lame 3.91 doesn't work in mono */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
54 lame_set_mode(s->gfp, JOINT_STEREO); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
55 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
|
56 lame_set_bWriteVbrTag(s->gfp,0); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
57 if (lame_init_params(s->gfp) < 0) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
58 goto err_close; |
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 avctx->frame_size = MPA_FRAME_SIZE; |
926 | 61 |
62 avctx->coded_frame= avcodec_alloc_frame(); | |
63 avctx->coded_frame->key_frame= 1; | |
259
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 return 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
66 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
67 err_close: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
68 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
69 err: |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
70 return -1; |
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 |
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
|
73 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
|
74 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
|
75 }; |
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
|
76 |
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
|
77 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
|
78 { { 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
|
79 { 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
|
80 { 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
|
81 }, |
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
|
82 { { 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
|
83 { 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
|
84 { 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
|
85 }, |
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 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
|
89 { |
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 { 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
|
91 { 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
|
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 |
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 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
|
95 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
|
96 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
|
97 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
|
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 |
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 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
|
101 { |
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 uint8_t *dataTmp = (uint8_t *)data; |
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 uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 |
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 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
|
115 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
|
116 } |
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 |
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 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
|
119 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
|
120 |
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 // *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
|
122 |
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 *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
|
124 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
|
125 *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
|
126 //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, 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
|
127 |
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 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
|
129 } |
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 |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
131 int MP3lame_encode_frame(AVCodecContext *avctx, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
132 unsigned char *frame, int buf_size, void *data) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
133 { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
134 Mp3AudioContext *s = avctx->priv_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
|
135 int len, i; |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
136 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
137 /* lame 3.91 dies on '1-channel interleaved' data */ |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
138 if (s->stereo) { |
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
|
139 s->buffer_index += lame_encode_buffer_interleaved( |
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 s->gfp, |
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
|
141 data, |
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
|
142 MPA_FRAME_SIZE, |
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
|
143 s->buffer + 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
|
144 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
|
145 ); |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
146 } else { |
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
|
147 s->buffer_index += lame_encode_buffer( |
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
|
148 s->gfp, |
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
|
149 data, |
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
|
150 data, |
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
|
151 MPA_FRAME_SIZE, |
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
|
152 s->buffer + 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
|
153 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
|
154 ); |
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
|
155 } |
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
|
156 if(s->buffer_index<4) |
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 return 0; |
1871
9457292b0b65
disable lames leading zero "feature", fixes a few more flv encoding issues
michael
parents:
1448
diff
changeset
|
158 |
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
|
159 len= mp3len(s->buffer, NULL, NULL); |
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
|
160 //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", MPA_FRAME_SIZE, 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
|
161 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
|
162 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
|
163 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
|
164 |
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
|
165 memmove(s->buffer, s->buffer+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
|
166 //FIXME fix the audio codec API, so we dont need the memcpy() |
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 //FIXME fix the audio codec API, so we can output multiple packets if we have them |
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
|
168 /*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
|
169 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
|
170 }*/ |
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
|
171 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
|
172 }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
|
173 return 0; |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
174 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
175 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
176 int MP3lame_encode_close(AVCodecContext *avctx) |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
177 { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
178 Mp3AudioContext *s = avctx->priv_data; |
926 | 179 |
180 av_freep(&avctx->coded_frame); | |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
181 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
182 lame_close(s->gfp); |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
183 return 0; |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
184 } |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
185 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
186 |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
187 AVCodec mp3lame_encoder = { |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
188 "mp3", |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
189 CODEC_TYPE_AUDIO, |
1448 | 190 CODEC_ID_MP3, |
259
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
191 sizeof(Mp3AudioContext), |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
192 MP3lame_encode_init, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
193 MP3lame_encode_frame, |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
194 MP3lame_encode_close |
cc6292eacba6
- Added MP3 encoding through libmp3lame contributed by Lennert Buytenhek.
pulento
parents:
diff
changeset
|
195 }; |