📄 vbr-g726.c
字号:
*/int main(argc, argv) int argc; char *argv[];{ G726_state encoder_state, decoder_state; long N = 16, N1 = 1, N2 = 0, cur_blk, smpno; short *tmp_buf, *inp_buf, *out_buf, reset=1; short inp_type, out_type, *rate=0; char encode = 1, decode = 1, law[4] = "A", def_rate[]="32"; int rateno=1, rate_idx; /* General-purpose, progress indication */ static char quiet=0, funny[9] = "|/-\\|/-\\";/* File variables */ char FileIn[80], FileOut[80]; FILE *Fi, *Fo; int inp, out; long start_byte;#ifdef VMS char mrs[15];#endif/* * ......... PARAMETERS FOR PROCESSING ......... */ /* GETTING OPTIONS */ if (argc < 2) display_usage(); else { while (argc > 1 && argv[1][0] == '-') if (strcmp(argv[1], "-noreset") == 0) { /* No reset */ reset = 0; /* Update argc/argv to next valid option/argument */ argv++; argc--; } else if (strcmp(argv[1], "-enc") == 0) { /* Encoder-only operation */ encode = 1; decode = 0; /* Move argv over the option to the next argument */ argv++; argc--; } else if (strcmp(argv[1], "-dec") == 0) { /*Decoder-only operation */ encode = 0; decode = 1; /* Move argv over the option to the next argument */ argv++; argc--; } else if (strcmp(argv[1], "-law") == 0) { /* Define law for operation: A, u, or linear */ switch (toupper(argv[2][0])) { case 'A': law[0] = '1'; break; case 'U': law[0] = '0'; break; case 'L': law[0] = '2'; break; default: HARAKIRI(" Invalid law (A or u)! Aborted...\n", 7); } /* Move argv over the option to the next argument */ argv+=2; argc-=2; } else if (strcmp(argv[1], "-frame") == 0) { /* Define Frame size for rate change during operation */ N = atoi(argv[2]); /* Move argv over the option to the next argument */ argv+=2; argc-=2; } else if (strcmp(argv[1], "-rate") == 0) { /*Define rate(s) for operation */ rateno = parse_rate(argv[2], &rate); if (rateno==0) { fprintf(stderr, "Invalid bitrate list: %s\n", argv[2]); exit(9); } /* Move argv over the option to the next argument */ argv+=2; argc-=2; } else if (strcmp(argv[1], "-q") == 0) { /* Don't print progress indicator */ quiet = 1; /* Move argv over the option to the next argument */ argv++; argc--; } else if (strcmp(argv[1], "-?") == 0 || strcmp(argv[1], "-help") == 0) { /* Print help */ display_usage(); } else { fprintf(stderr, "ERROR! Invalid option \"%s\" in command line\n\n", argv[1]); display_usage(); } } /* Now get regular parameters */ GET_PAR_S(1, "_Input File: .................. ", FileIn); GET_PAR_S(2, "_Output File: ................. ", FileOut); FIND_PAR_L(3, "_Block Size: .................. ", N, N); FIND_PAR_L(4, "_Starting Block: .............. ", N1, N1); FIND_PAR_L(5, "_No. of Blocks: ............... ", N2, N2); /* Uses default rate if none is given */ if (rate == 0) rateno = parse_rate(def_rate, &rate); /* Inform user of the compading law used: 0->u, 1->A, 2->linear */ fprintf(stderr, "Using %s\n", law[0] == '1'? "A-law" : ( law[0] == '0' ? "u-law" : "linear PCM")); /* Find starting byte in file */ start_byte = sizeof(short) * (long) (--N1) * (long) N; /* Check if is to process the whole file */ if (N2 == 0) { struct stat st; /* ... find the input file size ... */ stat(FileIn, &st); N2 = ceil((st.st_size - start_byte) / (double)(N * sizeof(short))); } /* Define correct data I/O types */ if (encode && decode) { inp_type = out_type = (law[0] == '2'? IS_LIN : IS_LOG); } else if (encode) { inp_type = law[0] == '2'? IS_LIN : IS_LOG; out_type = IS_ADPCM; } else { inp_type = IS_ADPCM; out_type = law[0] == '2'? IS_LIN : IS_LOG; } /* Force law to be used *by the ADPCM* to A-law, if input is linear */ if (law[0]=='2') law[0]='1';/* * ...... MEMORY ALLOCATION ......... */ if ((inp_buf = (short *) calloc(N, sizeof(short))) == NULL) HARAKIRI("Error in memory allocation!\n",1); if ((out_buf = (short *) calloc(N, sizeof(short))) == NULL) HARAKIRI("Error in memory allocation!\n",1); if ((tmp_buf = (short *) calloc(N, sizeof(short))) == NULL) HARAKIRI("Error in memory allocation!\n",1);/* * ......... FILE PREPARATION ......... */ /* Opening input file; abort if there's any problem */ if ((Fi = fopen(FileIn, "rb")) == NULL) KILL(FileIn, 2); inp = fileno(Fi); /* Creates output file */#ifdef VMS sprintf(mrs, "mrs=%d", 512);#endif if ((Fo = fopen(FileOut, WB)) == NULL) KILL(FileOut, 3); out = fileno(Fo); /* Move pointer to 1st block of interest */ if (fseek(Fi, start_byte, 0) < 0l) KILL(FileIn, 4);/* * ......... PROCESSING ACCORDING TO ITU-T G.726 ......... */ /* Reset VBR counters */ rate_idx = 0; for (cur_blk = 0; cur_blk < N2; cur_blk++) { /* Set the proper rate index */ rate_idx = cur_blk % rateno; /* Print progress flag */ if (!quiet)#ifdef DISPLAY_CURRENT_RATE fprintf(stderr, "%d-", 8 * rate[rate_idx]);#else fprintf(stderr, "%c\r", funny[cur_blk % 8]);#endif /* Read a block of samples */ if ((smpno = fread(inp_buf, sizeof(short), N, Fi)) < 0) KILL(FileIn, 5); /* Compress linear input samples */ if (inp_type == IS_LIN) { /* Compress using A-law */ alaw_compress(smpno, inp_buf, tmp_buf); /* copy temporary buffer over input buffer */ memcpy(inp_buf, tmp_buf, sizeof(short) * smpno); } /* Check if reset is needed */ reset = (reset == 1 && cur_blk == 0) ? 1 : 0; /* Carry out the desired operation */ if (encode && ! decode) G726_encode(inp_buf, out_buf, smpno, law, rate[rate_idx], reset, &encoder_state); else if (decode && !encode) G726_decode(inp_buf, out_buf, smpno, law, rate[rate_idx], reset, &decoder_state); else if (encode && decode) { G726_encode(inp_buf, tmp_buf, smpno, law, rate[rate_idx], reset, &encoder_state); G726_decode(tmp_buf, out_buf, smpno, law, rate[rate_idx], reset, &decoder_state); } /* Expand linear input samples */ if (out_type == IS_LIN) { /* Compress using A-law */ alaw_expand(smpno, out_buf, tmp_buf); /* copy temporary buffer over input buffer */ memcpy(out_buf, tmp_buf, sizeof(short) * smpno); } /* Write ADPCM output word */ if ((smpno = fwrite(out_buf, sizeof(short), smpno, Fo)) < 0) KILL(FileOut, 6); }/* * ......... FINALIZATIONS ......... */ /* Free allocated memory */ free(tmp_buf); free(out_buf); free(inp_buf); free(rate); /* Close input and output files */ fclose(Fi); fclose(Fo); /* Exit with success for non-vms systems */#ifndef VMS return (0);#endif}/* ............................. end of main() ............................. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -