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