📄 g729ev_main_decoder.c
字号:
/* ITU-T G.729EV Optimization/Characterization Candidate *//* Version: 1.0.a *//* Revision Date: June 28, 2006 *//* ITU-T G.729EV Optimization/Characterization Candidate ANSI-C Source Code Copyright (c) 2006 France Telecom, Matsushita Electric, Mindspeed, Siemens AG, ETRI, VoiceAge Corp. All rights reserved*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include "stl.h"#include "G729EV_G729_defines.h"#include "G729EV_G729_DecStat.h"#include "G729EV_G729_CodStat.h"#include "G729EV_MAIN_defines.h"#include "G729EV_MAIN_filt.h"#include "G729EV_MAIN_decod.h"#include "G729EV_G729B_defines.h"/*--------------------------------------------------------------------------* * Function display_usage() * * ~~~~~~~~~~~~~~~~~~~~~~~~~ * * Display decoder usage * *--------------------------------------------------------------------------*/static void display_usage(char *argv){ printf("Usage: %s [options] <bitstream> <outputfile>\n", argv); printf("\n"); printf("Options:\n"); printf(" -quiet do not display frame counter\n"); printf(" -rXXXXX run decoder at maximal bit rate XXXXX bit/s (32000 by default)\n"); printf(" -f8 output sampled at 8 kHz\n"); printf(" -g729b_bst read and decode G.729 or G.729B bitstream\n"); printf(" -ld low-delay mode\n"); printf("\n");}/*--------------------------------------------------------------------------* * Function check_arguments() * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Check decoder arguments * *--------------------------------------------------------------------------*/static void check_arguments(int argc, char *argv[], Word16 * used_rate, /* (i/o) rate used by decoder */ Word16 * nb_bits_used, /* (i/o) number of bits used by decoder */ Word16 * frame_size, /* (i/o) frame size used by encoder */ Word16 * i, /* (o) argv index */ Word16 * quiet, /* (i/o) quiet mode flag */ Word16 * f8, /* (i/o) 8kHz sampled output flag */ Word16 * g729b_bst, /* (i/o) G.729 bitstream mode flag */ Word16 * low_delay) /* (i/o) Low-delay mode flag */{ if ((argc < 3) || (argc > 8)) { display_usage(argv[0]); exit(-1); } for(*i = (Word16) 1; (*i < argc) && (strncmp(argv[*i], "-", 1) == 0); (*i)++) { if (strcmp(argv[*i], "-quiet") == 0) *quiet = 1; else if (strcmp(argv[*i], "-f8") == 0) { *f8 = (Word16) 1; *frame_size = G729EV_MAIN_L_FRAME2; } else if (strcmp(argv[*i], "-g729b_bst") == 0) { *g729b_bst = (Word16) 1; *low_delay = (Word16) 1; } else if (strcmp(argv[*i], "-ld") == 0) { *low_delay = (Word16) 1; } else if (strncmp(argv[*i], "-r", 2) == 0) { *used_rate = (Word16) atoi(&argv[*i][2]); if (((*used_rate != 8000) && (*used_rate < 12000)) || (*used_rate > 32000) || (*used_rate % 2000 != 0)) { printf("Error: wrong bit rate\n"); exit(-1); } *nb_bits_used = (Word16) ((*used_rate) * 0.02); } else { printf("Error: unrecognized option %s\n", argv[*i]); display_usage(argv[0]); exit(-1); } } if ((*g729b_bst) && (*nb_bits_used != 0)) { *used_rate = 0; *nb_bits_used = 0; printf("Warning: information rate ignored (G729B_BST mode on)\n"); } if ((*low_delay) && (!*g729b_bst) && ((*used_rate > 12000) || (*used_rate == 0))) { *used_rate = (Word16) 12000; *nb_bits_used = (Word16) 240; printf("Warning: bit rate limited to 12000 bit/s (LOW_DELAY mode on)\n"); }}int main(int argc, char *argv[]){ FILE *pfilin; /* input file descriptor */ FILE *pfilout; /* output file descriptor */ DECSTATMAIN decStat; /* structure of decoder states variables */ Word16 tabout[G729EV_MAIN_L_FRAME]; /* table of the 16 bits ouput samples */ Word16 nb_bits_sent = 0; /* number of bits available to decoder */ Word16 nb_bits_used = 0; /* number of bits used to decode */ Word16 used_rate = 0; /* bitrate specified with "-rXXXXX" option */ Word16 frame_size = G729EV_MAIN_L_FRAME; /* number of samples per frame */ Word32 nb_frame = 0; /* frame counter */ Word16 nb; /* number of samples/bytes read in input file */ Word16 bfi; /* flag: nRcv=0 -> received frame, nRcv=1 -> frame not received */ Word16 i; Word16 itu_192_bitstream[G729EV_MAX_BITS_BST + 2]; Word16 quiet = 0; Word16 f8 = 0; /* flag for 8kHz output */ Word16 g729b_bst = 0; /* flag for g729 bitstream */ Word16 low_delay = 0; Word16 skip; Word16 *bst; Word16 rate; Word16 nclf = 0; /* not complete last frame (for G729 bitstream mode */ printf("\n"); printf(" ***** ITU-T G.729EV Decoder *****\n"); printf("\n"); printf(" Fixed-point C simulation\n"); printf(" Version 1.0.a - June 28, 2006\n"); printf("\n"); /*------------------------------- read command line and check arguments -----------------------*/ check_arguments(argc, argv, &used_rate, &nb_bits_used, &frame_size, &i, &quiet, &f8, &g729b_bst, &low_delay); /* open input file */ if (i < argc) { if ((pfilin = fopen(argv[i], "rb")) == NULL) { printf("Error: cannot open file %s\n", argv[i]); exit(-1); } i++; } else { printf("Error: no input and output files specified\n"); exit(-1); } /* open output file */ if (i < argc) { if ((pfilout = fopen(argv[i], "wb")) == NULL) { printf("Error: cannot open file %s\n", argv[i]); exit(-1); } } else { printf("Error: no output file specified\n"); exit(-1); } /* print arguments */ printf("Input file : %s\n", argv[i - 1]); printf("Output file : %s\n", argv[i]); if (used_rate != 0) printf("Used bitrate : %d\n", (int) used_rate); if (f8) printf("Sampling frequency : 8000 Hz\n"); else printf("Sampling frequency : 16000 Hz\n"); if (g729b_bst) printf("Mode : G729B_BST\n"); if (low_delay & (!g729b_bst)) printf("Mode : LOW_DELAY\n"); printf("Decoding file %s...\n", argv[i - 1]);#ifdef WMOPS Init_WMOPS_counter(); /* WMOPS counter */#endif /*------------------------------------------ main processing ---------------------------------------*/ /* init decoder */ G729EV_MAIN_InitDecoder(&decStat, f8, g729b_bst, low_delay); /* set delay for synthesis alignment */ if (f8) skip = G729EV_MAIN_DELAY / 2; else skip = G729EV_MAIN_DELAY; if (low_delay!=1) skip += frame_size; /* loop over bitstream file */ while ((nb = fread(&itu_192_bitstream[0], sizeof(itu_192_bitstream[0]), 1, pfilin)) == 1) /*reads sync word */ { fwc(); Reset_WMOPS_counter(); bst = itu_192_bitstream; bfi = 0; for(i = 0; i <= g729b_bst; i++) { /* if G729 bitstream, reads 2frames */ if ((*bst != ITU_G192_SYNCWORD) && (*bst != ITU_G192_SYNCWORD_ERASURE)) { printf("Error: wrong synchron word in bitstream\n"); exit(-1); } if (*bst == ITU_G192_SYNCWORD_ERASURE) { bfi = 1; } bst++; /* get number of bits in current frame */ if ((nb = fread(bst, sizeof(bst[0]), 1, pfilin)) != 1) /*reads number of bits */ { printf("WARNING: not complete last frame, number of bits is missing : \n"); bfi = 1; } nb_bits_sent = *bst; bst++; /* for G729 bitstream mode, determine the type of the received frame */ /* 1: speech / 2: SID / 0: not transmitted */ if (g729b_bst == 1) { if (nb_bits_sent == G729EV_G729B_RATE_8000) decStat.decStG729.ftyp[i] = 1; else if (nb_bits_sent == G729EV_G729B_RATE_SID) decStat.decStG729.ftyp[i] = 2; else if (nb_bits_sent == G729EV_G729B_RATE_0) decStat.decStG729.ftyp[i] = 0; else { printf("Error: Bad bitstream format for G729 bitstream mode\n"); exit(-1); } } /* read bitstream and check if whole frame received */ if ((nb = fread(bst, sizeof(bst[0]), nb_bits_sent, pfilin)) != nb_bits_sent) { printf("WARNING: not complete last frame, data bits are missing : \n"); bfi = 1; } bst += nb; if ((i == 0) && g729b_bst) if ((nb = fread(bst, sizeof(bst[0]), 1, pfilin)) != 1) { printf("WARNING: incomplete last 20ms frame in G729 bitstream mode: \n"); nclf = 1; i++; } } if (!nclf) { if ((nb_bits_used != 0) && (nb_bits_sent > nb_bits_used)) nb_bits_sent = nb_bits_used; if (g729b_bst == 1) nb_bits_sent = G729EV_G729B_RATE_8000 * 2; /* set rate of CELP core in DecStat */ decStat.decStG729.rate = nb_bits_sent * 50; rate = nb_bits_sent * 50; /* decode bitstream */ G729EV_MAIN_Decode(&decStat, &itu_192_bitstream[2], tabout, rate, bfi); /* Make sure output speech is aligned with input speech */ /* Dump first G729EV_MAIN_DELAY + frame_size samples samples (lookahead) */ if (skip == 0) { /* write output */ fwrite(tabout, sizeof(Word16), frame_size, pfilout); } else { if (skip <= frame_size) { fwrite(tabout, sizeof(Word16), frame_size - skip, pfilout); skip = 0; } else { skip -= frame_size; } } nb_frame++; if (g729b_bst) nb_frame++; if (quiet == 0) { if (nb_frame % 100 == 0) { printf(" frame %d \r", (int) nb_frame); fflush(stdout); } } } } if (!skip) { Word16 buf[G729EV_MAIN_DELAY + G729EV_MAIN_L_FRAME]; for(i = 0; i < G729EV_MAIN_DELAY + G729EV_MAIN_L_FRAME; i++) buf[i] = 0; fwrite(buf, sizeof(Word16), G729EV_MAIN_DELAY + G729EV_MAIN_L_FRAME, pfilout); } printf("Number of processed frames: %d\n\n", (int) nb_frame);#ifdef WMOPS fwc(); printf("Decoder complexity\n"); WMOPS_output(0); printf("\n");#endif fclose(pfilin); fclose(pfilout); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -