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