1 /*
2 * This file is part of MPlayer.
3 *
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <inttypes.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include "config.h"
26 #include "m_option.h"
27 #include "mp_msg.h"
28 #include "libmpdemux/aviheader.h"
29 #include "libmpdemux/ms_hdr.h"
30 #include "stream/stream.h"
31 #include "libmpdemux/muxer.h"
32 #include "ae_lavc.h"
33 #include "av_helpers.h"
34 #include "ve.h"
35 #include "help_mp.h"
36 #include "av_opts.h"
37 #include "libaf/af_format.h"
38 #include "libaf/reorder_ch.h"
39 #include "libavcodec/avcodec.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavformat/avformat.h"
42 #include "libmpdemux/mp_taglists.h"
43 #include "fmt-conversion.h"
44
45 static AVCodec *lavc_acodec;
46 static AVCodecContext *lavc_actx;
47 static int compressed_frame_size = 0;
48
bind_lavc(audio_encoder_t * encoder,muxer_stream_t * mux_a)49 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a)
50 {
51 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
52 mux_a->wf->wFormatTag = lavc_param_atag;
53 mux_a->wf->nChannels = lavc_actx->channels;
54 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
55 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
56 mux_a->avg_rate= lavc_actx->bit_rate;
57 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
58 if(lavc_actx->block_align)
59 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
60 else
61 {
62 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
63
64 if ((mux_a->wf->nAvgBytesPerSec *
65 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
66 {
67 mux_a->h.dwScale = lavc_actx->frame_size;
68 mux_a->h.dwRate = lavc_actx->sample_rate;
69 mux_a->h.dwSampleSize = 0; // Blocksize not constant
70 }
71 else
72 mux_a->h.dwSampleSize = 0;
73 }
74 if(mux_a->h.dwSampleSize)
75 mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize;
76 else
77 mux_a->wf->nBlockAlign = 1;
78 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
79 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
80
81 switch(lavc_param_atag)
82 {
83 case 0x11: /* imaadpcm */
84 mux_a->wf->wBitsPerSample = 4;
85 mux_a->wf->cbSize = 2;
86 AV_WL16(mux_a->wf+1, lavc_actx->frame_size);
87 break;
88 case 0x55: /* mp3 */
89 mux_a->wf->cbSize = 12;
90 mux_a->wf->wBitsPerSample = 0; /* does not apply */
91 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
92 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
93 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
94 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
95 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
96 break;
97 default:
98 mux_a->wf->wBitsPerSample = 0; /* Unknown */
99 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
100 {
101 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size);
102 mux_a->wf->cbSize = lavc_actx->extradata_size;
103 }
104 else
105 mux_a->wf->cbSize = 0;
106 break;
107 }
108
109 // Fix allocation
110 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
111
112 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
113 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
114
115 return 1;
116 }
117
encode_lavc(audio_encoder_t * encoder,uint8_t * dest,void * src,int size,int max_size)118 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
119 {
120 int n;
121 n = lavc_encode_audio(lavc_actx, src, size, dest, max_size);
122 compressed_frame_size = n < 0 ? 0 : n;
123 return compressed_frame_size;
124 }
125
126
close_lavc(audio_encoder_t * encoder)127 static int close_lavc(audio_encoder_t *encoder)
128 {
129 compressed_frame_size = 0;
130 return 1;
131 }
132
get_frame_size(audio_encoder_t * encoder)133 static int get_frame_size(audio_encoder_t *encoder)
134 {
135 int sz = compressed_frame_size;
136 compressed_frame_size = 0;
137 return sz;
138 }
139
140
mpae_init_lavc(audio_encoder_t * encoder)141 int mpae_init_lavc(audio_encoder_t *encoder)
142 {
143 encoder->params.samples_per_frame = encoder->params.sample_rate;
144 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8;
145
146 if(!lavc_param_acodec)
147 {
148 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
149 return 0;
150 }
151
152 init_avcodec();
153
154 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
155 if (!lavc_acodec)
156 {
157 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
158 return 0;
159 }
160 if(lavc_param_atag == 0)
161 {
162 lavc_param_atag = mp_codec_id2tag(lavc_acodec->id, 0, 1);
163 if(!lavc_param_atag)
164 {
165 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
166 return 0;
167 }
168 }
169
170 lavc_actx = avcodec_alloc_context3(lavc_acodec);
171 if(lavc_actx == NULL)
172 {
173 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
174 return 0;
175 }
176
177 lavc_actx->codec_id = lavc_acodec->id;
178 // put sample parameters
179 lavc_actx->sample_fmt = AV_SAMPLE_FMT_S16;
180 if (lavc_acodec->sample_fmts) {
181 const enum AVSampleFormat *fmts;
182 lavc_actx->sample_fmt = lavc_acodec->sample_fmts[0]; // fallback to first format
183 for (fmts = lavc_acodec->sample_fmts; *fmts != AV_SAMPLE_FMT_NONE; fmts++) {
184 if (samplefmt2affmt(av_get_packed_sample_fmt(*fmts)) == encoder->params.sample_format) { // preferred format found
185 lavc_actx->sample_fmt = *fmts;
186 break;
187 }
188 }
189 }
190 encoder->input_format = samplefmt2affmt(av_get_packed_sample_fmt(lavc_actx->sample_fmt));
191 if (encoder->input_format == AF_FORMAT_UNKNOWN) {
192 mp_msg(MSGT_MENCODER,MSGL_ERR, "Audio encoder requires unknown or unsupported input format\n");
193 return 0;
194 }
195 lavc_actx->channels = encoder->params.channels;
196 lavc_actx->sample_rate = encoder->params.sample_rate;
197 lavc_actx->time_base.num = 1;
198 lavc_actx->time_base.den = encoder->params.sample_rate;
199 if(lavc_param_abitrate<1000)
200 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000;
201 else
202 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate;
203 if(lavc_param_audio_avopt){
204 if(parse_avopts(lavc_actx, lavc_param_audio_avopt) < 0){
205 mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_audio_avopt);
206 return 0;
207 }
208 }
209
210
211 /*
212 * Special case for adpcm_ima_wav.
213 * The bitrate is only dependent 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 }
225 if((lavc_param_audio_global_header&1)
226 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
227 lavc_actx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
228 }
229 if(lavc_param_audio_global_header&2){
230 lavc_actx->flags2 |= AV_CODEC_FLAG2_LOCAL_HEADER;
231 }
232
233 if(avcodec_open2(lavc_actx, lavc_acodec, NULL) < 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 *
245 av_get_bytes_per_sample(lavc_actx->sample_fmt) *
246 encoder->params.channels;
247 while (encoder->decode_buffer_size < 1024) encoder->decode_buffer_size *= 2;
248 encoder->bind = bind_lavc;
249 encoder->get_frame_size = get_frame_size;
250 encoder->encode = encode_lavc;
251 encoder->close = close_lavc;
252
253 return 1;
254 }
255