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