Mercurial > mplayer.hg
annotate libmpcodecs/ae_lavc.c @ 21963:b031f5863b41
Another AVCodecTag compile fix
author | reimar |
---|---|
date | Sun, 21 Jan 2007 10:49:08 +0000 |
parents | 0e746e1646bc |
children | d9494ca70ca7 |
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) |
21820
cd705f30bb31
Include libavformat/riff.h when building with static libavformat and
reimar
parents:
21773
diff
changeset
|
32 #ifdef USE_LIBAVFORMAT_SO |
cd705f30bb31
Include libavformat/riff.h when building with static libavformat and
reimar
parents:
21773
diff
changeset
|
33 #include <ffmpeg/avformat.h> |
21944
0e746e1646bc
include the right riff.h according to USE_LIBAVFORMAT_SO|USE_LIBAVFORMAT; please stop breaking code that uses *_SO
nicodvb
parents:
21822
diff
changeset
|
34 #include <ffmpeg/riff.h> |
21820
cd705f30bb31
Include libavformat/riff.h when building with static libavformat and
reimar
parents:
21773
diff
changeset
|
35 #else |
cd705f30bb31
Include libavformat/riff.h when building with static libavformat and
reimar
parents:
21773
diff
changeset
|
36 #include "libavformat/avformat.h" |
21944
0e746e1646bc
include the right riff.h according to USE_LIBAVFORMAT_SO|USE_LIBAVFORMAT; please stop breaking code that uses *_SO
nicodvb
parents:
21822
diff
changeset
|
37 #include "libavformat/riff.h" |
21822
fc4ce8a91a2e
Include "internal" libavformat/riff.h also when dynamic libavformat is used
reimar
parents:
21820
diff
changeset
|
38 #endif |
21963 | 39 extern const AVCodecTag mp_wav_tags[]; |
17096 | 40 #endif |
15234 | 41 |
42 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
43 { | |
44 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256); | |
45 mux_a->wf->wFormatTag = lavc_param_atag; | |
46 mux_a->wf->nChannels = lavc_actx->channels; | |
47 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate; | |
48 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8); | |
19360
d4d72e5eea07
pass average bitrate from encoder to (lavf) muxer
michael
parents:
17855
diff
changeset
|
49 mux_a->avg_rate= lavc_actx->bit_rate; |
15234 | 50 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec; |
51 if(lavc_actx->block_align) | |
52 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align; | |
53 else | |
54 { | |
55 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */ | |
56 | |
57 if ((mux_a->wf->nAvgBytesPerSec * | |
58 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) | |
59 { | |
60 mux_a->h.dwScale = lavc_actx->frame_size; | |
61 mux_a->h.dwRate = lavc_actx->sample_rate; | |
62 mux_a->h.dwSampleSize = 0; // Blocksize not constant | |
63 } | |
64 else | |
21773
03bc990498fc
the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents:
21771
diff
changeset
|
65 mux_a->h.dwSampleSize = 0; |
15234 | 66 } |
21773
03bc990498fc
the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents:
21771
diff
changeset
|
67 if(mux_a->h.dwSampleSize) |
03bc990498fc
the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents:
21771
diff
changeset
|
68 mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize; |
03bc990498fc
the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents:
21771
diff
changeset
|
69 else |
03bc990498fc
the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents:
21771
diff
changeset
|
70 mux_a->wf->nBlockAlign = 1; |
15234 | 71 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000; |
72 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign; | |
73 | |
74 switch(lavc_param_atag) | |
75 { | |
76 case 0x11: /* imaadpcm */ | |
77 mux_a->wf->wBitsPerSample = 4; | |
78 mux_a->wf->cbSize = 2; | |
79 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] = | |
80 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1; | |
81 break; | |
82 case 0x55: /* mp3 */ | |
83 mux_a->wf->cbSize = 12; | |
84 mux_a->wf->wBitsPerSample = 0; /* does not apply */ | |
85 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1; | |
86 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2; | |
87 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign; | |
88 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1; | |
89 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0; | |
90 break; | |
91 default: | |
92 mux_a->wf->wBitsPerSample = 0; /* Unknown */ | |
93 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0)) | |
94 { | |
15501
7cdc07507650
wrong memcpy of extradata; 10l to whomever wrote that broken code
nicodvb
parents:
15244
diff
changeset
|
95 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size); |
15234 | 96 mux_a->wf->cbSize = lavc_actx->extradata_size; |
97 } | |
98 else | |
99 mux_a->wf->cbSize = 0; | |
100 break; | |
101 } | |
102 | |
103 // Fix allocation | |
104 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); | |
105 | |
106 encoder->input_format = AF_FORMAT_S16_NE; | |
107 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize; | |
108 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2; | |
109 | |
110 return 1; | |
111 } | |
112 | |
113 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size) | |
114 { | |
115 int n; | |
116 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
|
117 compressed_frame_size = n; |
15234 | 118 return n; |
119 } | |
120 | |
121 | |
122 static int close_lavc(audio_encoder_t *encoder) | |
123 { | |
124 compressed_frame_size = 0; | |
125 return 1; | |
126 } | |
127 | |
128 static int get_frame_size(audio_encoder_t *encoder) | |
129 { | |
17855
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
130 int sz = compressed_frame_size; |
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
131 compressed_frame_size = 0; |
4b1930c9345c
do not randomly chop up packets, this isnt allowed in almost no container
michael
parents:
17842
diff
changeset
|
132 return sz; |
15234 | 133 } |
134 | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
135 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
|
136 { |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
137 if(codec == NULL) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
138 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
139 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
140 if(! strcasecmp(codec, "mp2")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
141 return 0x50; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
142 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
143 if(! strcasecmp(codec, "mp3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
144 return 0x55; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
145 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
146 if(! strcasecmp(codec, "ac3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
147 return 0x2000; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
148 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
149 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
|
150 return 0x11; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
151 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
152 if(! strncasecmp(codec, "bonk", 4)) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
153 return 0x2048; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
154 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
155 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
156 } |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
157 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
158 |
15234 | 159 int mpae_init_lavc(audio_encoder_t *encoder) |
160 { | |
161 encoder->params.samples_per_frame = encoder->params.sample_rate; | |
162 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8; | |
163 | |
164 if(!lavc_param_acodec) | |
165 { | |
166 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName); | |
167 return 0; | |
168 } | |
169 | |
170 if(!avcodec_inited){ | |
171 avcodec_init(); | |
172 avcodec_register_all(); | |
173 avcodec_inited=1; | |
174 } | |
175 | |
176 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec); | |
177 if (!lavc_acodec) | |
178 { | |
179 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec); | |
180 return 0; | |
181 } | |
182 if(lavc_param_atag == 0) | |
183 { | |
17354 | 184 #if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO) |
15234 | 185 lavc_param_atag = codec_get_wav_tag(lavc_acodec->id); |
21771 | 186 if(!lavc_param_atag) |
187 lavc_param_atag = codec_get_tag(mp_wav_tags, lavc_acodec->id); | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
188 #else |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
189 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
|
190 #endif |
15234 | 191 if(!lavc_param_atag) |
192 { | |
193 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n"); | |
194 return 0; | |
195 } | |
196 } | |
197 | |
198 lavc_actx = avcodec_alloc_context(); | |
199 if(lavc_actx == NULL) | |
200 { | |
201 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext); | |
202 return 0; | |
203 } | |
204 | |
205 // put sample parameters | |
206 lavc_actx->channels = encoder->params.channels; | |
207 lavc_actx->sample_rate = encoder->params.sample_rate; | |
208 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000; | |
209 | |
210 | |
211 /* | |
212 * Special case for adpcm_ima_wav. | |
213 * The bitrate is only dependant on samplerate. | |
214 * We have to known frame_size and block_align in advance, | |
215 * so I just copied the code from libavcodec/adpcm.c | |
216 * | |
217 * However, ms adpcm_ima_wav uses a block_align of 2048, | |
218 * lavc defaults to 1024 | |
219 */ | |
220 if(lavc_param_atag == 0x11) { | |
221 int blkalign = 2048; | |
222 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
223 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize; | |
224 } | |
17842 | 225 if((lavc_param_audio_global_header&1) |
226 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){ | |
227 lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
228 } | |
229 if(lavc_param_audio_global_header&2){ | |
230 lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER; | |
231 } | |
15234 | 232 |
233 if(avcodec_open(lavc_actx, lavc_acodec) < 0) | |
234 { | |
235 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate); | |
236 return 0; | |
237 } | |
238 | |
239 if(lavc_param_atag == 0x11) { | |
240 lavc_actx->block_align = 2048; | |
241 lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
242 } | |
243 | |
244 encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels; | |
245 encoder->bind = bind_lavc; | |
246 encoder->get_frame_size = get_frame_size; | |
247 encoder->encode = encode_lavc; | |
248 encoder->close = close_lavc; | |
249 | |
250 return 1; | |
251 } | |
252 |