Mercurial > mplayer.hg
annotate libmpcodecs/ae_lavc.c @ 21660:ca9da45d13e9
muxers now write to output muxer->stream rather than to muxer->file
author | nicodvb |
---|---|
date | Mon, 18 Dec 2006 21:03:59 +0000 |
parents | d4d72e5eea07 |
children | 4f317822d1fc |
rev | line source |
---|---|
15234 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <inttypes.h> | |
15238 | 4 #include <unistd.h> |
15234 | 5 #include <string.h> |
15240 | 6 #include <sys/types.h> |
15234 | 7 #include "m_option.h" |
17012 | 8 #include "mp_msg.h" |
15234 | 9 #include "aviheader.h" |
10 #include "ms_hdr.h" | |
21660
ca9da45d13e9
muxers now write to output muxer->stream rather than to muxer->file
nicodvb
parents:
19360
diff
changeset
|
11 #include "stream.h" |
15234 | 12 #include "muxer.h" |
13 #include "ae_lavc.h" | |
14 #include "help_mp.h" | |
17012 | 15 #include "config.h" |
16 #include "libaf/af_format.h" | |
15234 | 17 #ifdef USE_LIBAVCODEC_SO |
18 #include <ffmpeg/avcodec.h> | |
19 #else | |
20 #include "libavcodec/avcodec.h" | |
21 #endif | |
22 | |
23 static AVCodec *lavc_acodec; | |
24 static AVCodecContext *lavc_actx; | |
25 extern char *lavc_param_acodec; | |
26 extern int lavc_param_abitrate; | |
27 extern int lavc_param_atag; | |
17842 | 28 extern int lavc_param_audio_global_header; |
15234 | 29 extern int avcodec_inited; |
30 static int compressed_frame_size = 0; | |
17354 | 31 #if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO) |
17096 | 32 extern unsigned int codec_get_wav_tag(int id); |
33 #endif | |
15234 | 34 |
35 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
36 { | |
37 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256); | |
38 mux_a->wf->wFormatTag = lavc_param_atag; | |
39 mux_a->wf->nChannels = lavc_actx->channels; | |
40 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate; | |
41 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8); | |
19360
d4d72e5eea07
pass average bitrate from encoder to (lavf) muxer
michael
parents:
17855
diff
changeset
|
42 mux_a->avg_rate= lavc_actx->bit_rate; |
15234 | 43 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec; |
44 if(lavc_actx->block_align) | |
45 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align; | |
46 else | |
47 { | |
48 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */ | |
49 | |
50 if ((mux_a->wf->nAvgBytesPerSec * | |
51 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) | |
52 { | |
53 mux_a->h.dwScale = lavc_actx->frame_size; | |
54 mux_a->h.dwRate = lavc_actx->sample_rate; | |
55 mux_a->h.dwSampleSize = 0; // Blocksize not constant | |
56 } | |
57 else | |
58 mux_a->h.dwSampleSize = mux_a->h.dwScale; | |
59 } | |
60 mux_a->wf->nBlockAlign = mux_a->h.dwScale; | |
61 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000; | |
62 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign; | |
63 | |
64 switch(lavc_param_atag) | |
65 { | |
66 case 0x11: /* imaadpcm */ | |
67 mux_a->wf->wBitsPerSample = 4; | |
68 mux_a->wf->cbSize = 2; | |
69 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] = | |
70 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1; | |
71 break; | |
72 case 0x55: /* mp3 */ | |
73 mux_a->wf->cbSize = 12; | |
74 mux_a->wf->wBitsPerSample = 0; /* does not apply */ | |
75 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1; | |
76 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2; | |
77 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign; | |
78 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1; | |
79 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0; | |
80 break; | |
81 default: | |
82 mux_a->wf->wBitsPerSample = 0; /* Unknown */ | |
83 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0)) | |
84 { | |
15501
7cdc07507650
wrong memcpy of extradata; 10l to whomever wrote that broken code
nicodvb
parents:
15244
diff
changeset
|
85 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size); |
15234 | 86 mux_a->wf->cbSize = lavc_actx->extradata_size; |
87 } | |
88 else | |
89 mux_a->wf->cbSize = 0; | |
90 break; | |
91 } | |
92 | |
93 // Fix allocation | |
94 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); | |
95 | |
96 encoder->input_format = AF_FORMAT_S16_NE; | |
97 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize; | |
98 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2; | |
99 | |
100 return 1; | |
101 } | |
102 | |
103 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size) | |
104 { | |
105 int n; | |
106 n = avcodec_encode_audio(lavc_actx, dest, size, src); | |
17855
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
107 compressed_frame_size = n; |
15234 | 108 return n; |
109 } | |
110 | |
111 | |
112 static int close_lavc(audio_encoder_t *encoder) | |
113 { | |
114 compressed_frame_size = 0; | |
115 return 1; | |
116 } | |
117 | |
118 static int get_frame_size(audio_encoder_t *encoder) | |
119 { | |
17855
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
120 int sz = compressed_frame_size; |
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
121 compressed_frame_size = 0; |
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
122 return sz; |
15234 | 123 } |
124 | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
125 static uint32_t lavc_find_atag(char *codec) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
126 { |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
127 if(codec == NULL) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
128 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
129 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
130 if(! strcasecmp(codec, "mp2")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
131 return 0x50; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
132 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
133 if(! strcasecmp(codec, "mp3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
134 return 0x55; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
135 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
136 if(! strcasecmp(codec, "ac3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
137 return 0x2000; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
138 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
139 if(! strcasecmp(codec, "adpcm_ima_wav")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
140 return 0x11; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
141 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
142 if(! strncasecmp(codec, "bonk", 4)) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
143 return 0x2048; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
144 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
145 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
146 } |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
147 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
148 |
15234 | 149 int mpae_init_lavc(audio_encoder_t *encoder) |
150 { | |
151 encoder->params.samples_per_frame = encoder->params.sample_rate; | |
152 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8; | |
153 | |
154 if(!lavc_param_acodec) | |
155 { | |
156 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName); | |
157 return 0; | |
158 } | |
159 | |
160 if(!avcodec_inited){ | |
161 avcodec_init(); | |
162 avcodec_register_all(); | |
163 avcodec_inited=1; | |
164 } | |
165 | |
166 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec); | |
167 if (!lavc_acodec) | |
168 { | |
169 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec); | |
170 return 0; | |
171 } | |
172 if(lavc_param_atag == 0) | |
173 { | |
17354 | 174 #if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO) |
15234 | 175 lavc_param_atag = codec_get_wav_tag(lavc_acodec->id); |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
176 #else |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
177 lavc_param_atag = lavc_find_atag(lavc_param_acodec); |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
178 #endif |
15234 | 179 if(!lavc_param_atag) |
180 { | |
181 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n"); | |
182 return 0; | |
183 } | |
184 } | |
185 | |
186 lavc_actx = avcodec_alloc_context(); | |
187 if(lavc_actx == NULL) | |
188 { | |
189 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext); | |
190 return 0; | |
191 } | |
192 | |
193 // put sample parameters | |
194 lavc_actx->channels = encoder->params.channels; | |
195 lavc_actx->sample_rate = encoder->params.sample_rate; | |
196 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000; | |
197 | |
198 | |
199 /* | |
200 * Special case for adpcm_ima_wav. | |
201 * The bitrate is only dependant on samplerate. | |
202 * We have to known frame_size and block_align in advance, | |
203 * so I just copied the code from libavcodec/adpcm.c | |
204 * | |
205 * However, ms adpcm_ima_wav uses a block_align of 2048, | |
206 * lavc defaults to 1024 | |
207 */ | |
208 if(lavc_param_atag == 0x11) { | |
209 int blkalign = 2048; | |
210 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
211 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize; | |
212 } | |
17842 | 213 if((lavc_param_audio_global_header&1) |
214 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){ | |
215 lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
216 } | |
217 if(lavc_param_audio_global_header&2){ | |
218 lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER; | |
219 } | |
15234 | 220 |
221 if(avcodec_open(lavc_actx, lavc_acodec) < 0) | |
222 { | |
223 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate); | |
224 return 0; | |
225 } | |
226 | |
227 if(lavc_param_atag == 0x11) { | |
228 lavc_actx->block_align = 2048; | |
229 lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
230 } | |
231 | |
232 encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels; | |
233 encoder->bind = bind_lavc; | |
234 encoder->get_frame_size = get_frame_size; | |
235 encoder->encode = encode_lavc; | |
236 encoder->close = close_lavc; | |
237 | |
238 return 1; | |
239 } | |
240 |