comparison mencoder.c @ 2583:3dce775d82cb

PCM audio
author arpi
date Wed, 31 Oct 2001 00:25:28 +0000
parents e94a20dec331
children f4b01c45b1ea
comparison
equal deleted inserted replaced
2582:1616ad8fec1b 2583:3dce775d82cb
1 1
2 #define VCODEC_DIVX4 1 2 #define VCODEC_DIVX4 1
3 #define ACODEC_VBRMP3 1 3 #define ACODEC_PCM 1
4 #define ACODEC_VBRMP3 2
4 5
5 #include <stdio.h> 6 #include <stdio.h>
6 #include <stdlib.h> 7 #include <stdlib.h>
7 #include <string.h> 8 #include <string.h>
8 #include <signal.h> 9 #include <signal.h>
24 #include "dec_video.h" 25 #include "dec_video.h"
25 26
26 #include <encore2.h> 27 #include <encore2.h>
27 28
28 #include "get_path.c" 29 #include "get_path.c"
30
31 //--------------------------
32
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
29 119
30 //-------------------------- 120 //--------------------------
31 121
32 // cache2: 122 // cache2:
33 #ifdef USE_STREAM_CACHE 123 #ifdef USE_STREAM_CACHE
48 // audio stream skip/resync functions requires only for seeking. 138 // audio stream skip/resync functions requires only for seeking.
49 // (they should be implemented in the audio codec layer) 139 // (they should be implemented in the audio codec layer)
50 //void skip_audio_frame(sh_audio_t *sh_audio){} 140 //void skip_audio_frame(sh_audio_t *sh_audio){}
51 //void resync_audio_stream(sh_audio_t *sh_audio){} 141 //void resync_audio_stream(sh_audio_t *sh_audio){}
52 142
53 int verbose=5; // must be global! 143 int verbose=1; // must be global!
54 144
55 double video_time_usage=0; 145 double video_time_usage=0;
56 double vout_time_usage=0; 146 double vout_time_usage=0;
57 static double audio_time_usage=0; 147 static double audio_time_usage=0;
58 static int total_time_usage_start=0; 148 static int total_time_usage_start=0;
131 ENC_PARAM enc_param; 221 ENC_PARAM enc_param;
132 ENC_FRAME enc_frame; 222 ENC_FRAME enc_frame;
133 ENC_RESULT enc_result; 223 ENC_RESULT enc_result;
134 void* enc_handle=NULL; 224 void* enc_handle=NULL;
135 225
226 //lame_global_flags *lame;
227
136 float audio_preload=0.3; 228 float audio_preload=0.3;
137 229
138 //int out_buffer_size=0x200000; 230 //int out_buffer_size=0x200000;
139 //unsigned char* out_buffer=malloc(out_buffer_size); 231 //unsigned char* out_buffer=malloc(out_buffer_size);
140 232
147 } 239 }
148 240
149 if(argc>1) 241 if(argc>1)
150 stream=open_stream(argv[1],0,&file_format); 242 stream=open_stream(argv[1],0,&file_format);
151 else 243 else
152 // stream=open_stream("/3d/abcug/Weird AL - Amish Paradise (MUSIC VIDEO).mpeg",0,&file_format); 244 stream=open_stream("/3d/abcug/Weird AL - Amish Paradise (MUSIC VIDEO).mpeg",0,&file_format);
153 stream=open_stream("/3d/divx/405divx_sm_v2[1].avi",0,&file_format); 245 // stream=open_stream("/3d/divx/405divx_sm_v2[1].avi",0,&file_format);
154 // stream=open_stream("/dev/cdrom",2,&file_format); // VCD track 2 246 // stream=open_stream("/dev/cdrom",2,&file_format); // VCD track 2
155 247
156 if(!stream){ 248 if(!stream){
157 printf("Cannot open file/device\n"); 249 printf("Cannot open file/device\n");
158 exit(1); 250 exit(1);
323 mux_a->buffer_size=0x100000; //16384; 415 mux_a->buffer_size=0x100000; //16384;
324 mux_a->buffer=malloc(mux_a->buffer_size); 416 mux_a->buffer=malloc(mux_a->buffer_size);
325 417
326 mux_a->source=sh_audio; 418 mux_a->source=sh_audio;
327 419
328 //mux_a->codec=ACODEC_VBRMP3; // 0=streamcopy 420 mux_a->codec=ACODEC_PCM; // 0=streamcopy
329 421
330 switch(mux_a->codec){ 422 switch(mux_a->codec){
331 case 0: 423 case 0:
332 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize; 424 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
333 mux_a->h.dwScale=sh_audio->audio.dwScale; 425 mux_a->h.dwScale=sh_audio->audio.dwScale;
334 mux_a->h.dwRate=sh_audio->audio.dwRate; 426 mux_a->h.dwRate=sh_audio->audio.dwRate;
335 mux_a->wf=sh_audio->wf; 427 mux_a->wf=sh_audio->wf;
428 break;
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
336 break; 442 break;
337 case ACODEC_VBRMP3: 443 case ACODEC_VBRMP3:
338 mux_a->h.dwSampleSize=0; // VBR 444 mux_a->h.dwSampleSize=0; // VBR
339 mux_a->h.dwScale=4608; 445 mux_a->h.dwScale=4608;
340 mux_a->h.dwRate=sh_audio->samplerate; 446 mux_a->h.dwRate=sh_audio->samplerate;
348 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm 454 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
349 break; 455 break;
350 } 456 }
351 } 457 }
352 458
459 printf("Writting AVI header...\n");
353 aviwrite_write_header(muxer,muxer_f); 460 aviwrite_write_header(muxer,muxer_f);
354 461
355 switch(mux_v->codec){ 462 switch(mux_v->codec){
356 case 0: 463 case 0:
357 break; 464 break;
375 encore(NULL,ENC_OPT_INIT,&enc_param,NULL); 482 encore(NULL,ENC_OPT_INIT,&enc_param,NULL);
376 enc_handle=enc_param.handle; 483 enc_handle=enc_param.handle;
377 break; 484 break;
378 } 485 }
379 486
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
380 signal(SIGINT,exit_sighandler); // Interrupt from keyboard 514 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
381 signal(SIGQUIT,exit_sighandler); // Quit from keyboard 515 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
382 signal(SIGTERM,exit_sighandler); // kill 516 signal(SIGTERM,exit_sighandler); // kill
383 517
384 while(!eof){ 518 while(!eof){
390 unsigned char* start=NULL; 524 unsigned char* start=NULL;
391 int in_size; 525 int in_size;
392 526
393 if(sh_audio){ 527 if(sh_audio){
394 // get audio: 528 // get audio:
395 if(mux_a->timer-audio_preload<mux_v->timer){ 529 while(mux_a->timer-audio_preload<mux_v->timer){
396 // copy 0.5 sec of audio 530 // copy 0.5 sec of audio
397 int len; 531 int len;
398 if(mux_a->h.dwSampleSize){ 532 if(mux_a->h.dwSampleSize){
399 // CBR 533 // CBR
400 len=sh_audio->i_bps/2; 534 switch(mux_a->codec){
401 len/=mux_a->h.dwSampleSize;if(len<1) len=1; 535 case 0: // copy
402 len*=mux_a->h.dwSampleSize; 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
403 // printf("%d -> ",len); 600 // printf("%d -> ",len);
404 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
405 // printf("%d \n",len); 601 // printf("%d \n",len);
406 } else { 602 } else {
407 // VBR 603 // VBR
408 printf("not yet implemented!\n"); 604 printf("not yet implemented!\n");
409 } 605 }
410 if(len>0) aviwrite_write_chunk(muxer,mux_a,muxer_f,len,0); 606 if(len<=0) break; // EOF?
607 aviwrite_write_chunk(muxer,mux_a,muxer_f,len,0);
411 } 608 }
412 } 609 }
413 610
414 // get video frame! 611 // get video frame!
415 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps); 612 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps);