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

📄 stream.cpp

📁 AVS是中国自己推出的视频图像音频编解码标准。包中是AVS 源代码
💻 CPP
字号:
/*************************************************************************
 AVS1-P2视频解码器源码
 版权所有:联合信源数字音视频技术(北京)有限公司, (c) 2005-2006 

 AVS1-P2 Video Decoder Source Code
 (c) Copyright, NSCC All Rights Reserved, 2005-2006
 *************************************************************************
 Distributed under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*************************************************************************/
/*************************************************************************
  文件名称:	stream.cpp
  描    述: 与码流处理相关的一些函数
*************************************************************************/
/*************************************************************************
  Revision History
  data          Modification                                    Author
  2005-2-8      Created                                          jthou
 *************************************************************************/
#include "define.h"
#include "stream.h"
#include "vlc.h"
#include "global.h"
#include "macroblock.h"
#include "loopfilter.h"

AVS_DWORD SeqenceHeader(const AVS_BYTE* pbData, AVS_DWORD dwDataLen, SEQENCEINFO* Info)
{
  if(dwDataLen < 18)
    return AVS_NOT_ENOUGH_DATA;
  DWORD Left;
  FindNextPicOrEndStartCode(pbData, dwDataLen, &Left);
  const AVS_BYTE* pbCurrent = pbData;
  AVS_DWORD   bitOffset = 32;
  AVS_DWORD profile_id            = read_bits(pbCurrent, &bitOffset, 8);
  AVS_DWORD level_id              = read_bits(pbCurrent, &bitOffset, 8);
  AVS_BOOL  progressive_sequence  = read_bits(pbCurrent, &bitOffset, 1);
  AVS_DWORD horizontal_size       = read_bits(pbCurrent, &bitOffset, 14);
  AVS_DWORD vertical_size         = read_bits(pbCurrent, &bitOffset, 14);
  AVS_DWORD chromat_fromat        = read_bits(pbCurrent, &bitOffset, 2);
  AVS_DWORD sample_precision      = read_bits(pbCurrent, &bitOffset, 3);
  AVS_DWORD aspect_ratio          = read_bits(pbCurrent, &bitOffset, 4);
  AVS_DWORD frame_rate_code       = read_bits(pbCurrent, &bitOffset, 4);
  AVS_DWORD bit_rate_lower        = read_bits(pbCurrent, &bitOffset, 18);
  AVS_BOOL  market_bit            = read_bits(pbCurrent, &bitOffset, 1);
  AVS_DWORD bit_rate_upper        = read_bits(pbCurrent, &bitOffset, 12);
  AVS_BOOL  low_dlay              = read_bits(pbCurrent, &bitOffset, 1);
  
  Info->dwProfile = profile_id;
  Info->dwLevel = level_id;
  Info->bProgressive = progressive_sequence;
  Info->dwWidth = horizontal_size;
  Info->dwHeight = vertical_size;
  Info->fFrameRate = fPictureRates[frame_rate_code];
  Info->dwChromaFormat = chromat_fromat;
  Info->dwAspectRatio = aspect_ratio;
  Info->bLowDelay = low_dlay;

  Info->dwMbWidth = (horizontal_size+15)/16;
  Info->dwMbHeight = (vertical_size+15)/16; //??? 
  return dwDataLen-Left;
}

AVS_DWORD SliceHeader(const AVS_BYTE* pbData, AVS_DWORD dwDataLen, AVS_DWORD dwMbIndex, AVS_DWORD* pdwBitOffset, STREAMINFO* StrmInfo)
{
  const AVS_BYTE* pbCurrent = pbData;
  AVS_DWORD dwLeft = dwDataLen;
  AVS_DWORD MbWidth = StrmInfo->SeqInfo.dwMbWidth;
  AVS_DWORD MbHeight = StrmInfo->SeqInfo.dwMbHeight;
  AVS_BOOL  slice_weighting_flag = FALSE;
  
  AVS_DWORD dwMbNum = MbWidth * MbHeight;
  AVS_DWORD BitOffset = 0;
  AVS_DWORD mb_skip_run = 0;

  //AVS_DWORD mb_line_num = u(pbCurrent, &dwBitOffset, dwLeft, 8);
  AVS_DWORD slice_vertical_position_extention = 0;
  if(dwMbIndex == 0)
  {
    if(StrmInfo->SeqInfo.dwWidth > 2800)
      slice_vertical_position_extention = read_bits(pbCurrent, &BitOffset, 3);
    if(!StrmInfo->ImgInfo.bFixedPictureQp) //?
    {
      StrmInfo->SlcInfo.bFixedSliceQp = read_bits(pbCurrent, &BitOffset,1);
      StrmInfo->SlcInfo.dwSliceQp = read_bits(pbCurrent, &BitOffset, 6);
    }
  }

  if(StrmInfo->ImgInfo.dwImageType != I_IMG || 
    (StrmInfo->ImgInfo.bPictureStructure == 0 && dwMbIndex >= MbWidth*MbHeight/2 ))
  {
//    if(StrmInfo->ImgInfo.bSkipModeFlag)
//      mb_skip_run = ue(pbCurrent, &BitOffset, dwLeft);  
    slice_weighting_flag = read_bits(pbCurrent, &BitOffset, 1);
    
    if(slice_weighting_flag)
    {
      for(AVS_INT i=0; i<2; i++)
      {
        StrmInfo->SlcInfo.LumaScale[i] = read_bits(pbCurrent, &BitOffset, 8);
        StrmInfo->SlcInfo.LumaShift[i] = read_bits(pbCurrent, &BitOffset, 8);
        BitOffset += 1; // marker_bit
        StrmInfo->SlcInfo.ChromaScale[i] = read_bits(pbCurrent, &BitOffset, 8);
        StrmInfo->SlcInfo.ChromaShift[i] = read_bits(pbCurrent, &BitOffset, 8);
      }
      
      StrmInfo->SlcInfo.bMbWeightingFlag = read_bits(pbCurrent, &BitOffset, 8);
    }
  }

  *pdwBitOffset += BitOffset;

  return AVS_NOERROR;
}

AVS_DWORD GetImgHeaderInfo(const AVS_BYTE* pbData,AVS_DWORD dwDataLen, AVS_DWORD* dwBitoffset, STREAMINFO* Info)
{
  const AVS_BYTE* pbCurrent = pbData;
  AVS_DWORD   bitOffset = 32;
  AVS_DWORD dwCode = *(AVS_DWORD*)pbCurrent;
  if(DWORD_SWAP(dwCode) == I_FRAME_START_CODE)
  {
    AVS_DWORD bbv_dwlay = read_bits(pbCurrent, &bitOffset, 16);
    AVS_BOOL  time_code_flag = read_bits(pbCurrent, &bitOffset, 1);
    AVS_DWORD time_code = 0;
    if(time_code_flag == 1)
      time_code = read_bits(pbCurrent, &bitOffset, 24);//u(pbData, &bitOffset, dwDataLen, 24);
    
    AVS_DWORD picture_distance = read_bits(pbCurrent, &bitOffset, 8);

    AVS_DWORD bbv_check_times = 0;
    if(Info->SeqInfo.bLowDelay)
      bbv_check_times = ue(pbData, &bitOffset, dwDataLen);
    
    AVS_BOOL progressive_frame = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL picture_structure = FALSE;
    if(progressive_frame == 0)
      picture_structure = read_bits(pbCurrent, &bitOffset, 1);
    else 
      picture_structure = 1;
    
    AVS_BOOL top_field_first = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL repeat_first_field = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL fixed_picture_qp = read_bits(pbCurrent, &bitOffset, 1);
    AVS_DWORD picture_qp = read_bits(pbCurrent, &bitOffset, 6);
    AVS_BOOL skip_mode_flag = 0;
    if(progressive_frame == 0)
    {
      if(picture_structure == 0)
        skip_mode_flag = read_bits(pbCurrent, &bitOffset, 1);
    }
    bitOffset += 4; //reserved_bits
    AVS_DWORD alpha_c_offset = 0;
    AVS_DWORD beta_offset = 0;
    AVS_BOOL loop_filter_disable = read_bits(pbCurrent, &bitOffset, 1);
    if(!loop_filter_disable)
    {
      AVS_BOOL loop_filter_parameter_flag = read_bits(pbCurrent, &bitOffset, 1);
      if(loop_filter_parameter_flag)
      {
        alpha_c_offset = se(pbCurrent, &bitOffset, dwDataLen);
        beta_offset = se(pbCurrent, &bitOffset, dwDataLen);
      }
    }

    IMAGEINFO* imgInfo = &(Info->ImgInfo);
    imgInfo->dwImageType = I_IMG;
    imgInfo->dwPictureDistance = picture_distance;
    imgInfo->bProgressiveFrame = progressive_frame;
    imgInfo->bPictureStructure = picture_structure;
    imgInfo->bTopFieldFirst = top_field_first;
    imgInfo->bFixedPictureQp = fixed_picture_qp;
    imgInfo->bRepeatFirstField = repeat_first_field;
    imgInfo->dwPictureQp = picture_qp;
    imgInfo->bSkipModeFlag = skip_mode_flag;
    imgInfo->bLoopFilterDisable = loop_filter_disable;
    imgInfo->iAlphaCOffset = alpha_c_offset;
    imgInfo->iBetaOffset = beta_offset;
    *dwBitoffset = bitOffset;
    return AVS_NOERROR;
  }
  else if(DWORD_SWAP(dwCode) == PB_FRAME_START_CODE)
  {
    AVS_DWORD bbv_dwlay = read_bits(pbCurrent, &bitOffset, 16);
    AVS_DWORD picture_coding_type = read_bits(pbCurrent, &bitOffset, 2);
    AVS_DWORD picture_distance = read_bits(pbCurrent, &bitOffset, 8);
    
    AVS_DWORD bbv_check_times = 0;
    if(Info->SeqInfo.bLowDelay)
      bbv_check_times = ue(pbData, &bitOffset, dwDataLen);
    
    AVS_BOOL progressive_frame = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL picture_structure = 0;
    AVS_BOOL advanced_pred_mode_disable;
    if(progressive_frame == 0)
    {
      picture_structure = read_bits(pbCurrent, &bitOffset, 1);
      if(!picture_structure)
        advanced_pred_mode_disable = read_bits(pbCurrent, &bitOffset, 1);
    }
    else
    {
      picture_structure = TRUE;
    }
    AVS_BOOL top_field_first = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL repeat_first_field = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL fixed_picture_qp = read_bits(pbCurrent, &bitOffset, 1);
    AVS_DWORD picture_qp = read_bits(pbCurrent, &bitOffset, 6);
    
    AVS_BOOL picture_reference_flag = FALSE;
    if(!(picture_coding_type==2 && picture_structure==1))
      picture_reference_flag = read_bits(pbCurrent, &bitOffset, 1);
    
    bitOffset += 4; //reserved_bits

    AVS_BOOL skip_mode_flag = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL loop_filter_disable = read_bits(pbCurrent, &bitOffset, 1);
    AVS_BOOL loop_filter_parameter_flag = FALSE;
    AVS_DWORD alpha_c_offset = 0;
    AVS_DWORD beta_offset = 0;
    if(!loop_filter_disable)
    {
      loop_filter_parameter_flag = read_bits(pbCurrent, &bitOffset, 1);
      if(loop_filter_parameter_flag)
      {
        alpha_c_offset = se(pbData, &bitOffset, dwDataLen);
        beta_offset = se(pbData, &bitOffset, dwDataLen);
      }
    }
 
    IMAGEINFO* imgInfo = &(Info->ImgInfo);
    if(picture_coding_type == 1)
      imgInfo->dwImageType = P_IMG;
    else
      imgInfo->dwImageType = B_IMG;
    imgInfo->bFixedPictureQp = fixed_picture_qp;
    imgInfo->bLoopFilterDisable = loop_filter_disable;
    imgInfo->bPictureReferenceFlag = picture_reference_flag;
    imgInfo->bPictureStructure = picture_structure;
    imgInfo->bProgressiveFrame = progressive_frame;
    imgInfo->bRepeatFirstField = repeat_first_field;
    imgInfo->bSkipModeFlag = skip_mode_flag;
    imgInfo->bTopFieldFirst = top_field_first;
    imgInfo->iAlphaCOffset = alpha_c_offset;
    imgInfo->iBetaOffset = beta_offset;
    imgInfo->dwPictureDistance = picture_distance;
    imgInfo->dwPictureQp = picture_qp;

    *dwBitoffset = bitOffset;

    return AVS_NOERROR;
  }
  else
  {
    return AVS_INVALID_PIC_START_CODE;
  }

}


/************************************************************************/
/* 函数功能:寻找下一个起始码                                           */
/************************************************************************/
AVS_BOOL FindNextStartCode(const AVS_BYTE* pbData, AVS_DWORD dwDataLen, AVS_DWORD* dwLeft)
{
  const AVS_BYTE* pbCurrent = pbData+4;
  AVS_DWORD Left = dwDataLen-4;
  
  while (Left>4 && 
    (*(AVS_DWORD *)pbCurrent & 0x00FFFFFF) != 0x00010000) 
  {
    pbCurrent ++;
    Left --;
  }

  if(Left > 4)
  {
    *dwLeft = Left;
    return TRUE;
  }
  return FALSE;
}

⌨️ 快捷键说明

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