2531
|
1
|
|
2 #define VCODEC_DIVX4 1
|
2583
|
3 #define ACODEC_PCM 1
|
|
4 #define ACODEC_VBRMP3 2
|
2531
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <stdlib.h>
|
|
8 #include <string.h>
|
|
9 #include <signal.h>
|
|
10
|
2591
|
11 #include "config.h"
|
2531
|
12 #include "mp_msg.h"
|
|
13 #include "help_mp.h"
|
|
14
|
|
15 #include "codec-cfg.h"
|
|
16
|
|
17 #include "stream.h"
|
|
18 #include "demuxer.h"
|
|
19 #include "stheader.h"
|
|
20
|
|
21 #include "aviwrite.h"
|
|
22
|
|
23 #include "libvo/video_out.h"
|
|
24
|
2574
|
25 #include "dec_audio.h"
|
|
26 #include "dec_video.h"
|
|
27
|
2531
|
28 #include <encore2.h>
|
|
29
|
2591
|
30 #include <lame/lame.h>
|
2583
|
31
|
|
32 //--------------------------
|
|
33
|
2531
|
34 // cache2:
|
2618
|
35 static int stream_cache_size=0;
|
2531
|
36 #ifdef USE_STREAM_CACHE
|
|
37 extern int cache_fill_status;
|
|
38 #else
|
|
39 #define cache_fill_status 0
|
|
40 #endif
|
|
41
|
2618
|
42 int vcd_track=0;
|
|
43 int audio_id=-1;
|
|
44 int video_id=-1;
|
|
45 int dvdsub_id=-1;
|
|
46
|
2531
|
47 char *audio_codec=NULL; // override audio codec
|
|
48 char *video_codec=NULL; // override video codec
|
|
49 int audio_family=-1; // override audio codec family
|
|
50 int video_family=-1; // override video codec family
|
|
51
|
|
52 // audio stream skip/resync functions requires only for seeking.
|
|
53 // (they should be implemented in the audio codec layer)
|
|
54 //void skip_audio_frame(sh_audio_t *sh_audio){}
|
|
55 //void resync_audio_stream(sh_audio_t *sh_audio){}
|
|
56
|
2618
|
57 int verbose=0; // must be global!
|
2531
|
58
|
|
59 double video_time_usage=0;
|
|
60 double vout_time_usage=0;
|
|
61 static double audio_time_usage=0;
|
|
62 static int total_time_usage_start=0;
|
|
63 static int benchmark=0;
|
|
64
|
2605
|
65 // A-V sync:
|
|
66 int delay_corrected=1;
|
|
67 static float default_max_pts_correction=-1;//0.01f;
|
|
68 static float max_pts_correction=0;//default_max_pts_correction;
|
|
69 static float c_total=0;
|
|
70
|
2613
|
71 float force_fps=0;
|
|
72 float force_ofps=0; // set to 24 for inverse telecine
|
2531
|
73
|
2618
|
74 int force_srate=0;
|
|
75
|
2626
|
76 char* out_filename="test.avi";
|
|
77 char* mp3_filename=NULL;
|
|
78 char* ac3_filename=NULL;
|
|
79
|
|
80 char *out_audio_codec=NULL; // override audio codec
|
|
81 char *out_video_codec=NULL; // override video codec
|
|
82
|
2591
|
83 //#include "libmpeg2/mpeg2.h"
|
|
84 //#include "libmpeg2/mpeg2_internal.h"
|
|
85
|
2626
|
86 ENC_PARAM divx4_param;
|
|
87
|
|
88 int lame_param_quality=0; // best
|
|
89 int lame_param_vbr=vbr_default;
|
|
90 int lame_param_mode=-1; // unset
|
|
91 int lame_param_padding=-1; // unset
|
|
92 int lame_param_br=-1; // unset
|
|
93 int lame_param_ratio=-1; // unset
|
|
94
|
2618
|
95 //-------------------------- config stuff:
|
|
96
|
|
97 #include "cfgparser.h"
|
|
98
|
|
99 static int cfg_inc_verbose(struct config *conf){ ++verbose; return 0;}
|
|
100
|
|
101 static int cfg_include(struct config *conf, char *filename){
|
|
102 return parse_config_file(conf, filename);
|
|
103 }
|
|
104
|
|
105 #include "get_path.c"
|
|
106
|
|
107 #include "cfg-mplayer-def.h"
|
|
108 #include "cfg-mencoder.h"
|
|
109
|
2591
|
110 //---------------------------------------------------------------------------
|
|
111
|
|
112 // mini dummy libvo:
|
2531
|
113
|
|
114 static unsigned char* vo_image=NULL;
|
|
115 static unsigned char* vo_image_ptr=NULL;
|
|
116 static int vo_w,vo_h;
|
|
117
|
|
118 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h, int x0,int y0){
|
|
119 int y;
|
|
120 // printf("draw_slice %dx%d %d;%d\n",w,h,x,y);
|
|
121
|
|
122 // copy Y:
|
|
123 for(y=0;y<h;y++){
|
|
124 unsigned char* s=src[0]+stride[0]*(y0+y)+x0;
|
|
125 unsigned char* d=vo_image+vo_w*(y0+y)+x0;
|
|
126 memcpy(d,s,w);
|
|
127 }
|
|
128 x0>>=1;y0>>=1;
|
|
129 w>>=1;h>>=1;
|
|
130 // copy U:
|
|
131 for(y=0;y<h;y++){
|
2577
|
132 unsigned char* s=src[2]+stride[2]*(y0+y)+x0;
|
2531
|
133 unsigned char* d=vo_image+vo_w*vo_h+(vo_w>>1)*(y0+y)+x0;
|
|
134 memcpy(d,s,w);
|
|
135 }
|
|
136 // copy V:
|
|
137 for(y=0;y<h;y++){
|
|
138 unsigned char* s=src[1]+stride[1]*(y0+y)+x0;
|
|
139 unsigned char* d=vo_image+vo_w*vo_h+vo_w*vo_h/4+(vo_w>>1)*(y0+y)+x0;
|
|
140 memcpy(d,s,w);
|
|
141 }
|
|
142
|
|
143 }
|
|
144
|
|
145 static uint32_t draw_frame(uint8_t *src[]){
|
|
146 // printf("This function shouldn't be called - report bug!\n");
|
|
147 // later: add YUY2->YV12 conversion here!
|
|
148 vo_image_ptr=src[0];
|
|
149 }
|
|
150
|
|
151 vo_functions_t video_out;
|
|
152
|
2591
|
153 //---------------------------------------------------------------------------
|
|
154
|
|
155 int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
|
|
156 int size=0;
|
|
157 int eof=0;
|
|
158 while(size<total && !eof){
|
|
159 int len=total-size;
|
|
160 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
|
|
161 if(len>sh_audio->a_buffer_size) len=sh_audio->a_buffer_size;
|
|
162 if(len>sh_audio->a_buffer_len){
|
|
163 int ret=decode_audio(sh_audio,
|
|
164 &sh_audio->a_buffer[sh_audio->a_buffer_len],
|
|
165 len-sh_audio->a_buffer_len,
|
|
166 sh_audio->a_buffer_size-sh_audio->a_buffer_len);
|
|
167 if(ret>0) sh_audio->a_buffer_len+=ret; else eof=1;
|
|
168 }
|
|
169 if(len>sh_audio->a_buffer_len) len=sh_audio->a_buffer_len;
|
|
170 memcpy(buffer+size,sh_audio->a_buffer,len);
|
|
171 sh_audio->a_buffer_len-=len; size+=len;
|
|
172 if(sh_audio->a_buffer_len>0)
|
|
173 memcpy(sh_audio->a_buffer,&sh_audio->a_buffer[len],sh_audio->a_buffer_len);
|
|
174 }
|
|
175 return size;
|
|
176 }
|
|
177
|
|
178 //---------------------------------------------------------------------------
|
2531
|
179
|
|
180 static int eof=0;
|
|
181
|
|
182 static void exit_sighandler(int x){
|
|
183 eof=1;
|
|
184 }
|
|
185
|
2618
|
186 int main(int argc,char* argv[], char *envp[]){
|
2531
|
187
|
|
188 stream_t* stream=NULL;
|
|
189 demuxer_t* demuxer=NULL;
|
|
190 demux_stream_t *d_audio=NULL;
|
|
191 demux_stream_t *d_video=NULL;
|
|
192 demux_stream_t *d_dvdsub=NULL;
|
|
193 sh_audio_t *sh_audio=NULL;
|
|
194 sh_video_t *sh_video=NULL;
|
|
195 int file_format=DEMUXER_TYPE_UNKNOWN;
|
|
196 int i;
|
|
197 unsigned int out_fmt;
|
|
198
|
|
199 aviwrite_t* muxer=NULL;
|
|
200 aviwrite_stream_t* mux_a=NULL;
|
|
201 aviwrite_stream_t* mux_v=NULL;
|
|
202 FILE* muxer_f=NULL;
|
|
203
|
|
204 ENC_FRAME enc_frame;
|
|
205 ENC_RESULT enc_result;
|
|
206 void* enc_handle=NULL;
|
|
207
|
2591
|
208 lame_global_flags *lame;
|
2583
|
209
|
2581
|
210 float audio_preload=0.3;
|
|
211
|
2613
|
212 double v_pts_corr=0;
|
|
213 double v_timer_corr=0;
|
2605
|
214
|
2618
|
215 char** filenames=NULL;
|
|
216 char* filename=NULL;
|
|
217 int num_filenames;
|
|
218
|
2531
|
219 //int out_buffer_size=0x200000;
|
|
220 //unsigned char* out_buffer=malloc(out_buffer_size);
|
|
221
|
2622
|
222 mp_msg_init(MSGL_STATUS);
|
|
223
|
|
224 // check codec.conf
|
|
225 if(!parse_codec_cfg(get_path("codecs.conf"))){
|
|
226 if(!parse_codec_cfg(DATADIR"/codecs.conf")){
|
|
227 mp_msg(MSGT_MENCODER,MSGL_HINT,MSGTR_CopyCodecsConf);
|
|
228 exit(0);
|
|
229 }
|
|
230 }
|
2531
|
231
|
2618
|
232 num_filenames=parse_command_line(conf, argc, argv, envp, &filenames);
|
|
233 if(num_filenames<0) exit(1); // error parsing cmdline
|
|
234 if(!num_filenames && !vcd_track && !dvd_title){
|
|
235 printf("\nMissing filename!\n\n");
|
|
236 exit(1);
|
|
237 }
|
|
238
|
2622
|
239 mp_msg_init(verbose+MSGL_STATUS);
|
2600
|
240
|
2618
|
241 filename=(num_filenames>0)?filenames[0]:NULL;
|
|
242 stream=open_stream(filename,vcd_track,&file_format);
|
2531
|
243
|
|
244 if(!stream){
|
|
245 printf("Cannot open file/device\n");
|
|
246 exit(1);
|
|
247 }
|
|
248
|
|
249 printf("success: format: %d data: 0x%X - 0x%X\n",file_format, (int)(stream->start_pos),(int)(stream->end_pos));
|
|
250
|
2618
|
251 if(stream_cache_size) stream_enable_cache(stream,stream_cache_size*1024);
|
2531
|
252
|
2618
|
253 demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
|
2531
|
254 if(!demuxer){
|
|
255 printf("Cannot open demuxer\n");
|
|
256 exit(1);
|
|
257 }
|
|
258
|
|
259 d_audio=demuxer->audio;
|
|
260 d_video=demuxer->video;
|
|
261 d_dvdsub=demuxer->sub;
|
|
262 sh_audio=d_audio->sh;
|
|
263 sh_video=d_video->sh;
|
|
264
|
|
265 if(!video_read_properties(sh_video)){
|
|
266 printf("Couldn't read video properties\n");
|
|
267 exit(1);
|
|
268 }
|
|
269
|
2622
|
270 mp_msg(MSGT_MENCODER,MSGL_INFO,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
|
2531
|
271 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
|
|
272 sh_video->fps,sh_video->frametime
|
|
273 );
|
|
274
|
|
275
|
|
276 sh_video->codec=NULL;
|
2622
|
277 if(video_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceVideoFmt,video_family);
|
2531
|
278 while(1){
|
|
279 sh_video->codec=find_codec(sh_video->format,
|
|
280 sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,sh_video->codec,0);
|
|
281 if(!sh_video->codec){
|
|
282 if(video_family!=-1) {
|
|
283 sh_video->codec=NULL; /* re-search */
|
2622
|
284 mp_msg(MSGT_MENCODER,MSGL_WARN,MSGTR_CantFindVfmtFallback);
|
2531
|
285 video_family=-1;
|
|
286 continue;
|
|
287 }
|
2622
|
288 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindVideoCodec,sh_video->format);
|
|
289 mp_msg(MSGT_MENCODER,MSGL_HINT, MSGTR_TryUpgradeCodecsConfOrRTFM,get_path("codecs.conf"));
|
2531
|
290 exit(1);
|
|
291 }
|
|
292 if(video_codec && strcmp(sh_video->codec->name,video_codec)) continue;
|
|
293 else if(video_family!=-1 && sh_video->codec->driver!=video_family) continue;
|
|
294 break;
|
|
295 }
|
|
296
|
2622
|
297 mp_msg(MSGT_MENCODER,MSGL_INFO,"%s video codec: [%s] drv:%d (%s)\n",video_codec?"Forcing":"Detected",sh_video->codec->name,sh_video->codec->driver,sh_video->codec->info);
|
2531
|
298
|
|
299 for(i=0;i<CODECS_MAX_OUTFMT;i++){
|
|
300 out_fmt=sh_video->codec->outfmt[i];
|
|
301 if(out_fmt==0xFFFFFFFF) continue;
|
|
302 if(out_fmt==IMGFMT_YV12) break;
|
|
303 if(out_fmt==IMGFMT_I420) break;
|
|
304 if(out_fmt==IMGFMT_IYUV) break;
|
|
305 if(out_fmt==IMGFMT_YUY2) break;
|
|
306 }
|
|
307 if(i>=CODECS_MAX_OUTFMT){
|
2622
|
308 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_VOincompCodec);
|
2531
|
309 exit(1); // exit_player(MSGTR_Exit_error);
|
|
310 }
|
|
311 sh_video->outfmtidx=i;
|
|
312
|
|
313 if(out_fmt==IMGFMT_YV12 || out_fmt==IMGFMT_I420 || out_fmt==IMGFMT_IYUV){
|
|
314 vo_w=sh_video->disp_w;
|
|
315 vo_h=sh_video->disp_h;
|
|
316 vo_image=malloc(vo_w*vo_h*3/2);
|
|
317 vo_image_ptr=vo_image;
|
|
318 }
|
|
319
|
|
320 if(!init_video(sh_video)){
|
2622
|
321 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_CouldntInitVideoCodec);
|
2531
|
322 exit(1);
|
|
323 }
|
|
324
|
2581
|
325
|
|
326 if(sh_audio){
|
|
327 // Go through the codec.conf and find the best codec...
|
|
328 sh_audio->codec=NULL;
|
2622
|
329 if(audio_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceAudioFmt,audio_family);
|
2581
|
330 while(1){
|
|
331 sh_audio->codec=find_codec(sh_audio->format,NULL,sh_audio->codec,1);
|
|
332 if(!sh_audio->codec){
|
|
333 if(audio_family!=-1) {
|
|
334 sh_audio->codec=NULL; /* re-search */
|
2622
|
335 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindAfmtFallback);
|
2581
|
336 audio_family=-1;
|
|
337 continue;
|
|
338 }
|
2622
|
339 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindAudioCodec,sh_audio->format);
|
|
340 mp_msg(MSGT_MENCODER,MSGL_HINT, MSGTR_TryUpgradeCodecsConfOrRTFM,get_path("codecs.conf"));
|
2581
|
341 sh_audio=d_audio->sh=NULL;
|
|
342 break;
|
|
343 }
|
|
344 if(audio_codec && strcmp(sh_audio->codec->name,audio_codec)) continue;
|
|
345 else if(audio_family!=-1 && sh_audio->codec->driver!=audio_family) continue;
|
2622
|
346 mp_msg(MSGT_MENCODER,MSGL_INFO,"%s audio codec: [%s] drv:%d (%s)\n",audio_codec?"Forcing":"Detected",sh_audio->codec->name,sh_audio->codec->driver,sh_audio->codec->info);
|
2581
|
347 break;
|
|
348 }
|
|
349 }
|
|
350
|
|
351 if(sh_audio){
|
2622
|
352 mp_msg(MSGT_MENCODER,MSGL_V,"Initializing audio codec...\n");
|
2581
|
353 if(!init_audio(sh_audio)){
|
2622
|
354 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CouldntInitAudioCodec);
|
2581
|
355 sh_audio=d_audio->sh=NULL;
|
|
356 } else {
|
2622
|
357 mp_msg(MSGT_MENCODER,MSGL_INFO,"AUDIO: srate=%d chans=%d bps=%d sfmt=0x%X ratio: %d->%d\n",sh_audio->samplerate,sh_audio->channels,sh_audio->samplesize,
|
2581
|
358 sh_audio->sample_format,sh_audio->i_bps,sh_audio->o_bps);
|
|
359 }
|
|
360 }
|
|
361
|
|
362
|
|
363
|
2531
|
364 // set up video encoder:
|
|
365 video_out.draw_slice=draw_slice;
|
|
366 video_out.draw_frame=draw_frame;
|
|
367
|
|
368 // set up output file:
|
2626
|
369 muxer_f=fopen(out_filename,"wb");
|
2531
|
370 muxer=aviwrite_new_muxer();
|
2581
|
371
|
|
372 // ============= VIDEO ===============
|
|
373
|
2531
|
374 mux_v=aviwrite_new_stream(muxer,AVIWRITE_TYPE_VIDEO);
|
|
375
|
|
376 mux_v->buffer_size=0x200000;
|
|
377 mux_v->buffer=malloc(mux_v->buffer_size);
|
|
378
|
|
379 mux_v->source=sh_video;
|
|
380
|
|
381 mux_v->h.dwSampleSize=0; // VBR
|
|
382 mux_v->h.dwScale=10000;
|
2613
|
383 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps);
|
2531
|
384
|
2574
|
385 mux_v->codec=VCODEC_DIVX4; // 0=streamcopy
|
|
386
|
2531
|
387 switch(mux_v->codec){
|
|
388 case 0:
|
|
389 mux_v->bih=sh_video->bih;
|
|
390 break;
|
|
391 case VCODEC_DIVX4:
|
|
392 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
|
|
393 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
|
|
394 mux_v->bih->biWidth=sh_video->disp_w;
|
|
395 mux_v->bih->biHeight=sh_video->disp_h;
|
|
396 mux_v->bih->biPlanes=0;
|
|
397 mux_v->bih->biBitCount=24;
|
|
398 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x');
|
|
399 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
|
|
400 break;
|
|
401 }
|
|
402
|
2581
|
403 // ============= AUDIO ===============
|
|
404 if(sh_audio){
|
|
405
|
|
406 mux_a=aviwrite_new_stream(muxer,AVIWRITE_TYPE_AUDIO);
|
|
407
|
|
408 mux_a->buffer_size=0x100000; //16384;
|
|
409 mux_a->buffer=malloc(mux_a->buffer_size);
|
|
410
|
|
411 mux_a->source=sh_audio;
|
|
412
|
2591
|
413 //mux_a->codec=ACODEC_PCM; // 0=streamcopy
|
|
414 mux_a->codec=ACODEC_VBRMP3; // 0=streamcopy
|
2581
|
415
|
|
416 switch(mux_a->codec){
|
|
417 case 0:
|
|
418 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
|
|
419 mux_a->h.dwScale=sh_audio->audio.dwScale;
|
|
420 mux_a->h.dwRate=sh_audio->audio.dwRate;
|
|
421 mux_a->wf=sh_audio->wf;
|
|
422 break;
|
2583
|
423 case ACODEC_PCM:
|
|
424 printf("CBR PCM audio selected\n");
|
|
425 mux_a->h.dwSampleSize=2*sh_audio->channels;
|
|
426 mux_a->h.dwScale=1;
|
|
427 mux_a->h.dwRate=sh_audio->samplerate;
|
|
428 mux_a->wf=malloc(sizeof(WAVEFORMATEX));
|
|
429 mux_a->wf->nBlockAlign=mux_a->h.dwSampleSize;
|
|
430 mux_a->wf->wFormatTag=0x1; // PCM
|
|
431 mux_a->wf->nChannels=sh_audio->channels;
|
|
432 mux_a->wf->nSamplesPerSec=sh_audio->samplerate;
|
|
433 mux_a->wf->nAvgBytesPerSec=mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
|
|
434 mux_a->wf->wBitsPerSample=16;
|
|
435 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
|
|
436 break;
|
2581
|
437 case ACODEC_VBRMP3:
|
|
438 mux_a->h.dwSampleSize=0; // VBR
|
2591
|
439 mux_a->h.dwScale=4608/4;
|
2581
|
440 mux_a->h.dwRate=sh_audio->samplerate;
|
|
441 mux_a->wf=malloc(sizeof(WAVEFORMATEX));
|
|
442 mux_a->wf->wFormatTag=0x55; // MP3
|
|
443 mux_a->wf->nChannels=sh_audio->channels;
|
|
444 mux_a->wf->nSamplesPerSec=sh_audio->samplerate;
|
|
445 mux_a->wf->nAvgBytesPerSec=0;
|
|
446 mux_a->wf->nBlockAlign=1;
|
|
447 mux_a->wf->wBitsPerSample=16;
|
|
448 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
|
|
449 break;
|
|
450 }
|
|
451 }
|
|
452
|
2583
|
453 printf("Writting AVI header...\n");
|
2531
|
454 aviwrite_write_header(muxer,muxer_f);
|
|
455
|
|
456 switch(mux_v->codec){
|
|
457 case 0:
|
|
458 break;
|
|
459 case VCODEC_DIVX4:
|
|
460 // init divx4linux:
|
2626
|
461 divx4_param.x_dim=sh_video->disp_w;
|
|
462 divx4_param.y_dim=sh_video->disp_h;
|
|
463 divx4_param.framerate=(float)mux_v->h.dwRate/mux_v->h.dwScale;
|
|
464 if(!divx4_param.bitrate) divx4_param.bitrate=800000;
|
|
465 else if(divx4_param.bitrate<=16000) divx4_param.bitrate*=1000;
|
|
466 if(!divx4_param.quality) divx4_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best )
|
|
467 divx4_param.handle=NULL;
|
|
468 encore(NULL,ENC_OPT_INIT,&divx4_param,NULL);
|
|
469 enc_handle=divx4_param.handle;
|
2531
|
470 break;
|
|
471 }
|
|
472
|
2591
|
473 #if 1
|
2600
|
474 if(sh_audio)
|
2583
|
475 switch(mux_a->codec){
|
|
476 case ACODEC_VBRMP3:
|
|
477
|
|
478 lame=lame_init();
|
2591
|
479 lame_set_bWriteVbrTag(lame,0);
|
2583
|
480 lame_set_in_samplerate(lame,sh_audio->samplerate);
|
|
481 lame_set_num_channels(lame,mux_a->wf->nChannels);
|
|
482 lame_set_out_samplerate(lame,mux_a->h.dwRate);
|
2626
|
483 if(lame_param_vbr){ // VBR:
|
|
484 lame_set_VBR(lame,lame_param_vbr); // vbr mode
|
|
485 lame_set_VBR_q(lame,lame_param_quality+1); // 1 = best vbr q 6=~128k
|
|
486 if(lame_param_br>0) lame_set_VBR_mean_bitrate_kbps(lame,lame_param_br);
|
|
487 } else { // CBR:
|
|
488 lame_set_quality(lame,lame_param_quality); // 0 = best q
|
|
489 if(lame_param_br>0) lame_set_brate(lame,lame_param_br);
|
|
490 }
|
|
491 if(lame_param_mode>=0) lame_set_mode(lame,lame_param_mode); // j-st
|
|
492 if(lame_param_ratio>0) lame_set_compression_ratio(lame,lame_param_ratio);
|
2583
|
493 lame_init_params(lame);
|
2622
|
494 if(verbose){
|
2626
|
495 lame_print_config(lame);
|
|
496 lame_print_internals(lame);
|
2622
|
497 }
|
2583
|
498
|
|
499 }
|
|
500 #endif
|
|
501
|
2531
|
502 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
|
|
503 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
|
|
504 signal(SIGTERM,exit_sighandler); // kill
|
|
505
|
|
506 while(!eof){
|
|
507
|
2571
|
508 float frame_time=0;
|
2531
|
509 int blit_frame=0;
|
|
510 float a_pts=0;
|
|
511 float v_pts=0;
|
2574
|
512 unsigned char* start=NULL;
|
|
513 int in_size;
|
2613
|
514 int skip_flag=0; // 1=skip -1=duplicate
|
2531
|
515
|
2581
|
516 if(sh_audio){
|
|
517 // get audio:
|
2583
|
518 while(mux_a->timer-audio_preload<mux_v->timer){
|
2581
|
519 int len;
|
|
520 if(mux_a->h.dwSampleSize){
|
2605
|
521 // CBR - copy 0.5 sec of audio
|
2583
|
522 switch(mux_a->codec){
|
|
523 case 0: // copy
|
|
524 len=sh_audio->i_bps/2;
|
|
525 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
|
|
526 len*=mux_a->h.dwSampleSize;
|
|
527 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
|
|
528 break;
|
|
529 case ACODEC_PCM:
|
|
530 len=mux_a->h.dwSampleSize*(mux_a->h.dwRate/2);
|
2591
|
531 len=dec_audio(sh_audio,mux_a->buffer,len);
|
2583
|
532 break;
|
|
533 }
|
2581
|
534 } else {
|
2605
|
535 // VBR - encode/copy an audio frame
|
|
536 switch(mux_a->codec){
|
|
537 case 0: // copy
|
|
538 printf("not yet implemented!\n");
|
|
539 break;
|
|
540 case ACODEC_VBRMP3:
|
2591
|
541 while(mux_a->buffer_len<4){
|
|
542 unsigned char tmp[2304];
|
|
543 int len=dec_audio(sh_audio,tmp,2304);
|
|
544 if(len<=0) break; // eof
|
|
545 len=lame_encode_buffer_interleaved(lame,
|
|
546 tmp,len/4,
|
|
547 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len);
|
|
548 if(len<0) break; // error
|
|
549 mux_a->buffer_len+=len;
|
|
550 }
|
|
551 if(mux_a->buffer_len<4) break;
|
|
552 len=mp_decode_mp3_header(mux_a->buffer);
|
|
553 if(len<=0) break; // bad frame!
|
|
554 while(mux_a->buffer_len<len){
|
|
555 unsigned char tmp[2304];
|
|
556 int len=dec_audio(sh_audio,tmp,2304);
|
|
557 if(len<=0) break; // eof
|
|
558 len=lame_encode_buffer_interleaved(lame,
|
|
559 tmp,len/4,
|
|
560 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len);
|
|
561 if(len<0) break; // error
|
|
562 mux_a->buffer_len+=len;
|
|
563 }
|
2605
|
564 break;
|
|
565 }
|
2581
|
566 }
|
2583
|
567 if(len<=0) break; // EOF?
|
|
568 aviwrite_write_chunk(muxer,mux_a,muxer_f,len,0);
|
2591
|
569 if(mux_a->buffer_len>=len){
|
|
570 mux_a->buffer_len-=len;
|
|
571 memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
|
|
572 }
|
2581
|
573 }
|
|
574 }
|
|
575
|
|
576 // get video frame!
|
|
577 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps);
|
|
578 if(in_size<0){ eof=1; break; }
|
2531
|
579 sh_video->timer+=frame_time;
|
2613
|
580
|
|
581 v_timer_corr-=frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
|
2531
|
582
|
2613
|
583 // check frame duplicate/drop:
|
|
584
|
|
585 if(v_timer_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate){
|
|
586 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
|
|
587 ++skip_flag; // skip
|
|
588 } else
|
|
589 while(v_timer_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate){
|
|
590 v_timer_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate;
|
|
591 --skip_flag; // dup
|
|
592 }
|
2531
|
593
|
2613
|
594 while( (v_pts_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag>0)
|
|
595 || (v_pts_corr<=-2*(float)mux_v->h.dwScale/mux_v->h.dwRate) ){
|
|
596 v_pts_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate;
|
|
597 --skip_flag; // dup
|
|
598 }
|
|
599 if( (v_pts_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag<0)
|
|
600 || (v_pts_corr>=2*(float)mux_v->h.dwScale/mux_v->h.dwRate) )
|
|
601 if(skip_flag<=0){ // we can't skip more than 1 frame now
|
|
602 v_pts_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
|
|
603 ++skip_flag; // skip
|
|
604 }
|
|
605
|
|
606
|
|
607 if(skip_flag<=0){ // don't have to skip frame.
|
2531
|
608 switch(mux_v->codec){
|
2574
|
609 case 0:
|
|
610 mux_v->buffer=start;
|
|
611 aviwrite_write_chunk(muxer,mux_v,muxer_f,in_size,(sh_video->ds->flags&1)?0x10:0);
|
|
612 break;
|
2531
|
613 case VCODEC_DIVX4:
|
2574
|
614 blit_frame=decode_video(&video_out,sh_video,start,in_size,0);
|
|
615 if(!blit_frame){
|
|
616 // empty.
|
|
617 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0);
|
|
618 break;
|
|
619 }
|
2531
|
620 enc_frame.image=vo_image_ptr;
|
|
621 enc_frame.bitstream=mux_v->buffer;
|
|
622 enc_frame.length=mux_v->buffer_size;
|
|
623 switch(out_fmt){
|
|
624 case IMGFMT_YV12: enc_frame.colorspace=ENC_CSP_YV12; break;
|
|
625 case IMGFMT_IYUV:
|
|
626 case IMGFMT_I420: enc_frame.colorspace=ENC_CSP_I420; break;
|
|
627 case IMGFMT_YUY2: enc_frame.colorspace=ENC_CSP_YUY2; break;
|
|
628 case IMGFMT_UYVY: enc_frame.colorspace=ENC_CSP_UYVY; break;
|
|
629 case IMGFMT_RGB24:
|
|
630 case IMGFMT_BGR24:
|
|
631 enc_frame.colorspace=ENC_CSP_RGB24; break;
|
|
632 }
|
|
633 enc_frame.quant=0;
|
|
634 enc_frame.intra=0;
|
|
635 enc_frame.mvs=NULL;
|
|
636 // printf("encoding...\n");
|
|
637 encore(enc_handle,ENC_OPT_ENCODE,&enc_frame,&enc_result);
|
|
638 // printf(" len=%d key:%d qualt:%d \n",enc_frame.length,enc_result.is_key_frame,enc_result.quantizer);
|
|
639 aviwrite_write_chunk(muxer,mux_v,muxer_f,enc_frame.length,enc_result.is_key_frame?0x10:0);
|
|
640 break;
|
|
641 }
|
2613
|
642
|
|
643 if(skip_flag<0){
|
2605
|
644 // duplicate frame
|
2613
|
645 printf("\nduplicate %d frame(s)!!! \n",-skip_flag);
|
|
646 while(skip_flag<0){
|
|
647 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0);
|
|
648 ++skip_flag;
|
|
649 }
|
2605
|
650 }
|
|
651
|
|
652 } else {
|
|
653 // skip frame
|
|
654 printf("\nskip frame!!! \n");
|
2613
|
655 --skip_flag;
|
2605
|
656 }
|
|
657
|
|
658 if(sh_audio){
|
|
659 float AV_delay,x;
|
|
660 // A-V sync!
|
|
661 if(pts_from_bps){
|
|
662 unsigned int samples=(sh_audio->audio.dwSampleSize)?
|
|
663 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
|
|
664 (d_audio->pack_no); // <- used for VBR audio
|
|
665 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
|
|
666 delay_corrected=1;
|
|
667 } else {
|
|
668 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
|
|
669 a_pts=d_audio->pts;
|
|
670 if(!delay_corrected) if(a_pts) delay_corrected=1;
|
|
671 //printf("*** %5.3f ***\n",a_pts);
|
|
672 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
|
|
673 }
|
|
674 v_pts=d_video->pts;
|
|
675 // av = compensated (with out buffering delay) A-V diff
|
2613
|
676 AV_delay=(a_pts-v_pts); AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
|
2605
|
677 // compensate input video timer by av:
|
|
678 x=AV_delay*0.1f;
|
|
679 if(x<-max_pts_correction) x=-max_pts_correction; else
|
|
680 if(x> max_pts_correction) x= max_pts_correction;
|
|
681 if(default_max_pts_correction>=0)
|
|
682 max_pts_correction=default_max_pts_correction;
|
|
683 else
|
|
684 max_pts_correction=sh_video->frametime*0.10; // +-10% of time
|
|
685 // sh_video->timer-=x;
|
|
686 c_total+=x;
|
2613
|
687 v_pts_corr+=x;
|
2605
|
688
|
2613
|
689 printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
|
2605
|
690 a_pts,v_pts,a_pts-v_pts,
|
|
691 (float)(mux_a->timer-mux_v->timer),
|
2613
|
692 AV_delay, c_total, v_pts_corr );
|
2605
|
693
|
|
694 }
|
|
695
|
|
696 #if 0
|
|
697 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A:%6.1f V:%6.1f A-V:%7.3f ct:%7.3f %3d/%3d %2d%% %2d%% %4.1f%% %d%%\r",
|
|
698 a_pts,v_pts,a_pts-v_pts,c_total,
|
|
699 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
|
|
700 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
|
|
701 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
|
|
702 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
|
|
703 ,cache_fill_status
|
|
704 );
|
|
705 #endif
|
|
706
|
|
707 fflush(stdout);
|
|
708
|
2531
|
709
|
|
710
|
|
711 } // while(!eof)
|
|
712
|
2622
|
713 printf("\nWritting AVI index...\n");
|
2531
|
714 aviwrite_write_index(muxer,muxer_f);
|
2622
|
715 printf("Fixup AVI header...\n");
|
2531
|
716 fseek(muxer_f,0,SEEK_SET);
|
|
717 aviwrite_write_header(muxer,muxer_f); // update header
|
|
718 fclose(muxer_f);
|
|
719
|
2618
|
720 if(stream) free_stream(stream); // kill cache thread
|
|
721
|
2531
|
722 }
|