⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitmux.c

📁 MPEG2/MPEG4编解码参考程序(实现了MPEG4的部分功能)
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************MPEG-4 Audio VMBit stream moduleThis software module was originally developed byBodo Teichmann (Fraunhofer Institute of Integrated Circuits tmn@iis.fhg.de)and edited byin the course of development of the MPEG-2 NBC/MPEG-4 Audio standardISO/IEC 13818-7, 14496-1,2 and 3. This software module is animplementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio toolsas specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC givesusers of the MPEG-2 NBC/MPEG-4 Audio standards free license to thissoftware module or modifications thereof for use in hardware orsoftware products claiming conformance to the MPEG-2 NBC/ MPEG-4 Audiostandards. Those intending to use this software module in hardware orsoftware products are advised that this use may infringe existingpatents. The original developer of this software module and his/hercompany, the subsequent editors and their companies, and ISO/IEC haveno liability for use of this software module or modifications thereofin an implementation. Copyright is not released for non MPEG-2NBC/MPEG-4 Audio conforming products. The original developer retainsfull right to use the code for his/her own purpose, assign or donatethe code to a third party and to inhibit third party from using thecode for non MPEG-2 NBC/MPEG-4 Audio conforming products. Thiscopyright notice must be included in all copies or derivative works.Copyright (c) 1998.$Id: bitmux.c,v 1.11 1999/07/22 15:52:19 purnhage Exp $BT    Bodo Teichmann, FhG/IIS <tmn@iis.fhg.de>**********************************************************************//* ------------------------------------------------------------------------- *//*                                                                           *//* Bitmux.c  : Handling of bistream format for AAC Raw and AAC SCALABLE     *//*                                                                           *//* ------------------------------------------------------------------------- */#include <stdio.h>#include "block.h"               /* handler, defines, enums */#include "buffersHandle.h"       /* handler, defines, enums */#include "interface.h"           /* handler, defines, enums */#include "tf_mainHandle.h"       /* handler, defines, enums */#include "bitstreamStruct.h"     /* structs */#include "nok_ltp_common.h"      /* structs */#include "bitstream.h"#include "common_m4a.h"#include "nok_bwp_enc.h"#include "nok_ltp_enc.h"#include "tns3.h"#include "aac_qc.h"#include "bitmux.h"int sort_book_numbers(int book_vector[],                      int output_book_vector[],		      int nr_of_sfb, int qdebug,		      WINDOW_SEQUENCE windowSequence,		      BsBitStream *fixed_stream,		      int write_flag,                      int num_win_groups		      ){  /*    This function inputs the vector, 'book_vector[]', which is of length SFB_NUM_MAX,    and contains the optimal huffman tables of each sfb.  It returns the vector, 'output_book_vector[]', which    has it's elements formatted for the encoded bit stream.  It's syntax is:       {sect_cb[0], length_segment[0], ... ,sect_cb[num_of_sections], length_segment[num_of_sections]}    The above syntax is true, unless there is an escape sequence.  An    escape sequence occurs when a section is longer than 2 ^ (bit_len)    long in units of scalefactor bands.  Also, the integer returned from    this function is the number of bits written in the bitstream,     'bit_count'.      This function supports both long and short blocks.  */  int i;  int repeat_counter = 1;  int bit_count = 0;  int previous;  int max, bit_len;  int sfb_per_group = nr_of_sfb/num_win_groups;  if (windowSequence == EIGHT_SHORT_SEQUENCE){    max = 7;    bit_len = 3;  }  else {  /* the windowSequence is a long,start, or stop window */    max = 31;    bit_len = 5;  }  previous = book_vector[0];  if (write_flag) {       BsPutBit(fixed_stream,book_vector[0],4);      if (qdebug > 1 )      fprintf(stderr,"\nbookNrs:\n%d = ",book_vector[0]);  }  bit_count += 4;		/* bug fix TK */  for (i=1;i<nr_of_sfb;i++) {    if( (book_vector[i] != previous) || (i%sfb_per_group==0)) {      if (write_flag) {	BsPutBit(fixed_stream,repeat_counter,bit_len);  	if (qdebug > 1 )	  fprintf(stderr,"%d\n%d = ",i,book_vector[i]);      }      bit_count += bit_len;      if (repeat_counter == max){  /* in case you need to terminate an escape sequence */	if (write_flag) BsPutBit(fixed_stream,0,bit_len);  	bit_count += bit_len;      }	      if (write_flag) BsPutBit(fixed_stream,book_vector[i],4);      bit_count += 4;		/* bug fix TK */      previous = book_vector[i];      repeat_counter=1;    }    /* if the length of the section is longer than the amount of bits available in */    /* the bitsream, "max", then start up an escape sequence */    else if (((book_vector[i] == previous) && (repeat_counter == max)) ) {       if (write_flag) {	BsPutBit(fixed_stream,repeat_counter,bit_len);  	if (qdebug > 1 )	  fprintf(stderr,"(esc)");	      }      bit_count += bit_len;      repeat_counter = 1;    }    else {      repeat_counter++;    }  }  if (write_flag) {    BsPutBit(fixed_stream,repeat_counter,bit_len);      if (qdebug > 1 )      fprintf(stderr,"%d\n",i);    }  bit_count += bit_len;  if (repeat_counter == max) {  /* special case if the last section length is an */    /* escape sequence */    if (write_flag) BsPutBit(fixed_stream,0,bit_len);      bit_count += bit_len;  }  return(bit_count);}int find_grouping_bits(int window_group_length[],		     int num_window_groups){  /* This function inputs the grouping information and outputs the seven bit      'grouping_bits' field that the NBC decoder expects.  */  int grouping_bits = 0;  int tmp[8];  int i,j;  int index=0;  for(i=0; i<num_window_groups; i++){    for (j=0; j<window_group_length[i];j++){      tmp[index++] = i;      /*      printf("tmp[%d] = %d\n",index-1,tmp[index-1]);*/    }  }  for(i=1; i<8; i++){    grouping_bits = grouping_bits << 1;    if(tmp[i] == tmp[i-1]) {      grouping_bits++;    }  }    /*  printf("grouping_bits = %d  [i=%d]\n",grouping_bits,i);*/  return(grouping_bits);}int write_scalefactor_bitstream(int nr_of_sfb,                              int scale_factors[],                              int book_vector[],                              BsBitStream *fixed_stream,                              int write_flag,                              int window_group_length[],                              int num_window_groups,                              int global_gain,                              WINDOW_SEQUENCE windowSequence,                              int noise_nrg[],                              int qdebug, int huff[13][1090][4]){  /* this function takes care of counting the number of bits necessary */  /* to encode the scalefactors.  In addition, if the write_flag == 1, */  /* then the scalefactors are written out the fixed_stream output bit */  /* stream.  it returns k, the number of bits written to the bitstream*/  int i,j,k=0;  int diff,length,codeword;  int previous_scale_factor;  int index = 0;  int sf_out = 0;  int sf_not_out = 0;  int nr_of_sfb_per_group=0;  int pns_pcm_flag = 1;  int previous_noise_nrg = global_gain;  if (windowSequence == EIGHT_SHORT_SEQUENCE) { /* short windows */    nr_of_sfb_per_group = nr_of_sfb/num_window_groups;  }  else {    nr_of_sfb_per_group = nr_of_sfb;    num_window_groups = 1;    window_group_length[0] = 1;  }  previous_scale_factor = scale_factors[0];  previous_scale_factor = global_gain;  if ( (qdebug > 2) && write_flag ) {    fprintf(stderr,"\nscalefacs :");	  }      for(j=0; j<num_window_groups; j++){    for(i=0;i<nr_of_sfb_per_group;i++) {        /* test to see if any codebooks in a group are zero */      if ( (qdebug > 1) && write_flag ) {	fprintf(stderr," %d:%d",index,scale_factors[index]);	      }      if (book_vector[index] == PNS_HCB) {        /* send PNS pseudo scalefactors  */        diff = noise_nrg[index] - previous_noise_nrg;        if (pns_pcm_flag) {          pns_pcm_flag = 0;          if (diff+PNS_PCM_OFFSET<0  ||  diff+PNS_PCM_OFFSET>=(1<<PNS_PCM_BITS))            fprintf(stderr,"%3d (PNS PCM Overflow0) *****\n", diff);          length = PNS_PCM_BITS;          codeword = diff + PNS_PCM_OFFSET;        } else {          if (diff<-60  ||  diff>60)            printf("%3d (PNS Overflow0) *****\n", diff);          length = huff[12][diff+60][2];    	  codeword = huff[12][diff+60][3];        }	k += length;        previous_noise_nrg = noise_nrg[index];	if (write_flag == 1 ) {   	  /*	  printf("  scale_factor[%d] = %d\n",index,scale_factors[index]);*/	  BsPutBit(fixed_stream,codeword,length); 	  sf_out++;	}      } else if (book_vector[index]) {        /* only send scalefactors if using non-zero codebooks ,*/	diff = scale_factors[index] - previous_scale_factor;	length = huff[12][diff+60][2];    	k+=length;	previous_scale_factor = scale_factors[index];	if (write_flag == 1 ) {   	  /*	  printf("  scale_factor[%d] = %d\n",index,scale_factors[index]);*/	  codeword = huff[12][diff+60][3];	  BsPutBit(fixed_stream,codeword,length); 	  sf_out++;	}      } else {	/*if (write_flag)           printf("  zero_flag = 1, [j=%d index=%d i=%d]\n",j,index,i);*/	sf_not_out++;      }      index++;    }  }  /*  if (write_flag) printf("sf_out = %d  sf_not_out = %d\n",sf_out,sf_not_out);*/  return(k);}int write_ics_info (int nr_of_sfb, WINDOW_SEQUENCE windowSequence, BsBitStream *fixed_stream,                     WINDOW_SHAPE window_shape, int num_window_groups, int window_group_length[],		    int reducedInfo, QC_MOD_SELECT qc_select, PRED_TYPE predictor_type,                     NOK_BW_PRED_STATUS *nok_bwp_status, NOK_LT_PRED_STATUS *nok_lt_statusLeft,		    NOK_LT_PRED_STATUS *nok_lt_statusRight, int stereo_flag){  int bit_count = 0;  int grouping_bits;  int max_sfb;  int tmpVar;  if (!reducedInfo) {    if((qc_select == AAC_QC)        ) {      /* ics_reserved_bit only for plain AAC */      BsPutBit(fixed_stream, 0, 1);       bit_count +=1;    }    /* write out WINDOW_SEQUENCE */    switch (windowSequence)       {      case EIGHT_SHORT_SEQUENCE:        BsPutBit(fixed_stream,2,2);  /* short window */        bit_count += 2;        break;      case ONLY_LONG_SEQUENCE:        BsPutBit(fixed_stream,0,2);          bit_count += 2;        break;      case   LONG_START_SEQUENCE:        BsPutBit(fixed_stream,1,2);          bit_count += 2;        break;      case  LONG_STOP_SEQUENCE:        BsPutBit(fixed_stream,3,2);          bit_count += 2;        break;      default:        CommonExit(-1,"\n unknown blocktype : %d",windowSequence);    }    /* write out WINDOW SHAPE */    switch( window_shape ) {    case WS_FHG:      tmpVar = 0;      break;    case WS_DOLBY:      tmpVar = 1;      break;    default:      CommonExit(-1,"\nunknow windowshape : %d", window_shape );    }    BsPutBit(fixed_stream, tmpVar, 1);    bit_count += 1;  } /* if (!reducedInfo) */  /* write out SCALE_FACTOR_GROUPING and MAX_SFB */  max_sfb =   nr_of_sfb/num_window_groups;  if (max_sfb*num_window_groups != nr_of_sfb)    CommonExit(-1,"\nwrong num of nr_of_sfb");  if (windowSequence == EIGHT_SHORT_SEQUENCE){    BsPutBit(fixed_stream, max_sfb, 4); /* let max_sfb_l = 14, the max number of sfb's */    bit_count += 4;    grouping_bits = find_grouping_bits(window_group_length, num_window_groups);    BsPutBit(fixed_stream, grouping_bits, 7);  /* the grouping bits */    bit_count += 7;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -