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