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