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

📄 h263pars.c

📁 基于intel ipp的h263_decoder
💻 C
字号:
/******************************************************************************
//               INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2003 Intel Corporation. All Rights Reserved.
//
//  Description:
//    Intel(R) Integrated Performance Primitives Sample Code H263 Decoder
//
//  Function List:
//    parse_picture_header_h263()
//    parse_gob_header_h263()
//    parse_mb_header_h263()
//
******************************************************************************/
#include "samph263.h"
   
/******************************************************************************
// Function Name:   parse_picture_header_h263
//
// Description:     Parse picture header informations from input stream.
//
// Parameter:
// Input:
//        stream:   Pointer to sample_bitstream structure holding input stream
//         state:   Pointer to h263_dec_state structure holding decode status
// Output:
//        stream:   Pointer to sample_bitstream structure holding input stream
//                  Stream position pointer is updated to next available byte
//         state:   Pointer to h263_dec_state structure holding decode status
//
// Return:
// sample_status:   
//  [SAMPLE_STATUS_NOERR]:            Succeeds
//  [SAMPLE_STATUS_BITSTREAM_ERR]:    Stream parsing error occurs
//  [SAMPLE_STATUS_NOTSUPPORTED_ERR]: Stream syntax is not supported.
//
// Notes:   
//                  1) Currently any syntax indicating using annex will return 
//                     [SAMPLE_STATUS_NOTSUPPORTED_ERR]. 
//                  2) PSUPP is not supported but discarded.
******************************************************************************/
sample_status parse_picture_header_h263(sample_bitstream  *stream, 
                                        h263_dec_state    *state)
{
    unsigned int data_reg = 0;
    
    /* Read PSC -- should be byte aligned */
    if(!STREAM_CHECK_BYTE_ALIGN(stream)) {
        STREAM_BYTE_ALIGN(stream);
    }
    if(!read_stream_bits(stream, H263_STREAM_PSC_LEN, &data_reg)
        || H263_STREAM_PSC != data_reg)  {
        return SAMPLE_STATUS_BITSTREAM_ERR; 
    }

    /* Read TR */
    data_reg = 0;
    if(!read_stream_bits(stream, H263_STREAM_TR_LEN, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    state->tr = (int)data_reg;

    /* PTYPE */
    data_reg = 0;
    if(!read_stream_bits(stream, H263_STREAM_PTYPE_LEN, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    
    if(0x2 != ((data_reg>>(H263_STREAM_PTYPE_LEN-2))&0x3)) {
        /* First 2 bits of PTYPE should be 10 */
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }

    /* skip Bit 3-5 of PTYPE */

    /* PTYPE bit 6~8: Source format */
    state->source_format = (data_reg>>(H263_STREAM_PTYPE_LEN-8))&7;
    if(H263_SOURCE_FORBIDDEN == state->source_format) {
        /* 0 is forbidden */
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    if(H263_SOURCE_CIF < state->source_format) {
        /* PLUSPTYPE not supported */
        return SAMPLE_STATUS_NOTSUPPORTED_ERR;
    }

    /* PTYPE bit 9: Code Type (INTRA/INTER) */
    state->coding_type = (data_reg>> (H263_STREAM_PTYPE_LEN-9)) & 0x1;

    /* PTYPE bit 10~13: Annex D/E/F/G, not supported */
    if((data_reg>>(H263_STREAM_PTYPE_LEN-13)) & 0xf) {
        return SAMPLE_STATUS_NOTSUPPORTED_ERR;
    }

    /* Read PQUANT */
    data_reg = 0;
    if(!read_stream_bits(stream, H263_STREAM_PQUANT_LEN, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    state->pquant = (int)data_reg;

    /* Read CPM: CPM==1 not supported */
    data_reg = 0;
    if(!read_stream_bits(stream, H263_STREAM_CPM_LEN, &data_reg)) {
        return SAMPLE_STATUS_NOTSUPPORTED_ERR;
    }
        
    /* Read PEI */
    do {
        data_reg = 0;
        if(!read_stream_bits(stream, H263_STREAM_PEI_LEN, &data_reg)) {
            return SAMPLE_STATUS_BITSTREAM_ERR;
        }

        if(1 == data_reg) {
            /* Discard PSUPP: not supported */
            if(!read_stream_bits(stream, H263_STREAM_PSUPP_LEN, &data_reg)) {
                return SAMPLE_STATUS_BITSTREAM_ERR;
            }
        }

    } while(1 == data_reg);

    return SAMPLE_STATUS_NOERR;
}

/******************************************************************************
// Function Name:   parse_gob_header_h263
//
// Description:     Parse GOB header informations from input stream
//
// Parameter:
// Input:
//        stream:   Pointer to sample_bitstream structure holding input stream
//         state:   Pointer to h263_dec_state structure holding decode status
// Output:
//        stream:   Pointer to sample_bitstream structure holding input stream
//                  Stream position pointer is updated to next available byte
//         state:   Pointer to h263_dec_state structure holding decode status
//
// Return:
// sample_status:   
//  [SAMPLE_STATUS_NOERR]:            Succeeds
//  [SAMPLE_STATUS_BITSTREAM_ERR]:    Stream parse error occurs
//  [SAMPLE_STATUS_NOTSUPPORTED_ERR]: Stream syntax is not supported
//
// Notes:   None
******************************************************************************/
sample_status parse_gob_header_h263(sample_bitstream  *stream, 
                                    h263_dec_state    *state)
{
    unsigned int data_reg = 0;

    /* Read GBSC */
    if(!read_stream_bits(stream, H263_STREAM_GBSC_LEN, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }

    if(H263_STREAM_GBSC != data_reg) {
        if(!STREAM_CHECK_BYTE_ALIGN(stream)) {
            /* Discard GSTUF */
            STREAM_BYTE_ALIGN(stream);
            if(!read_stream_bits(stream, H263_STREAM_GBSC_LEN, &data_reg) 
                || H263_STREAM_GBSC != data_reg) {
                return SAMPLE_STATUS_SYNCNOTFOUND_ERR;
            }
        } else {
            return SAMPLE_STATUS_SYNCNOTFOUND_ERR;
        }
    }

    /* Read GN */
    if(!read_stream_bits(stream, H263_STREAM_GN_LEN, &data_reg)||0==data_reg) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    
    /* No GSBI, GSBI only exists when CPM=1, not supported here */

    /* Skip GFID */
    if(!skip_stream_bits(stream, H263_STREAM_GFID_LEN)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }

    /* Read GQUANT */
    if(!read_stream_bits(stream,H263_STREAM_GQUANT_LEN, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    state->quant = data_reg;

    return SAMPLE_STATUS_NOERR;
}

/******************************************************************************
// Function Name:   parse_mb_header_h263
//
// Description:     Parse MB header informations from input stream
//
// Parameter:
// Input:
//        stream:   Pointer to sample_bitstream structure holding input stream
//         state:   Pointer to h263_dec_state structure holding decode status
// Output:
//        stream:   Pointer to sample_bitstream structure holding input stream
//                  Stream position pointer is updated to next available byte
//         state:   Pointer to h263_dec_state structure holding decode status
//
// Return:
// sample_status:   
//  [SAMPLE_STATUS_NOERR]:            Succeeds
//  [SAMPLE_STATUS_BITSTREAM_ERR]:    Stream parsing error occurs
//  [SAMPLE_STATUS_NOTSUPPORTED_ERR]: Stream syntax is not supported by 
//									  current sample decoder
//
// Notes:   None
******************************************************************************/
sample_status parse_mb_header_h263(sample_bitstream    *stream, 
                                   h263_dec_state      *state)
{
    unsigned int data_reg = 0;
    short mcbpc = 0, cbpy = 0;

    /* Read COD -- only exists in non-I Picture  */
    if(H263_I_PICTURE != state->coding_type) {

        if(!read_stream_bits(stream, H263_STREAM_COD_LEN, &data_reg)) {
            return SAMPLE_STATUS_BITSTREAM_ERR;
        }

        state->cod = data_reg;

        if(state->cod) { /* Not Coded */
            return SAMPLE_STATUS_NOERR;
        }
    }

    /* Read MCBPC */
    data_reg = 0;
    if(H263_I_PICTURE == state->coding_type) {  

        /* I-Picture */
        if(!preview_stream_bits(stream, 6, &data_reg)) {
            return SAMPLE_STATUS_BITSTREAM_ERR;
        }

        mcbpc = mcbpc_tbl_ipic_h263[data_reg];

        if(data_reg == 0) {

            /* Stuffing bits */
            if(!skip_stream_bits(stream, 9)) {
                return SAMPLE_STATUS_BITSTREAM_ERR;
            }

        } else {
            skip_stream_bits(stream, MCBPC_NUMBITS(mcbpc));
        }

    } else {

        /* P-Picture */
        if(!preview_stream_bits(stream, 9, &data_reg)) {
            return SAMPLE_STATUS_BITSTREAM_ERR;
        }

        if(0 == data_reg) {
            /* More than 9 bits, continue reading */
            skip_stream_bits(stream, 9);
            if(!preview_stream_bits(stream, 4, &data_reg)) {
                return SAMPLE_STATUS_BITSTREAM_ERR;
            }
            mcbpc = mcbpc_tbl2_ppic_h263[data_reg];

            if(mcbpc == 0) {  /* Invalid code */
                return SAMPLE_STATUS_BITSTREAM_ERR;
            }

            skip_stream_bits(stream, MCBPC_NUMBITS(mcbpc)-9);

        } else {
            mcbpc = mcbpc_tbl1_ppic_h263[data_reg];
            skip_stream_bits(stream, MCBPC_NUMBITS(mcbpc));
        }
    }

    state->mb_type = MCBPC_MBTYPE(mcbpc);
    state->cbpc    = MCBPC_CBPC(mcbpc);
    if(H263_STUFFING == state->mb_type) {
        /* MB-type is stuffing, no more header info */
        return SAMPLE_STATUS_NOERR;
    }

    /* MODB, CBPB not supported */

    /* CBPY */
    data_reg = 0;
    if(!preview_stream_bits(stream, 6, &data_reg)) {
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    cbpy = cbpy_tbl_h263[data_reg];
    if(0 == cbpy)  { /* Invalid Code */
        return SAMPLE_STATUS_BITSTREAM_ERR;
    }
    if(H263_INTRA == state->mb_type || H263_INTRA_Q == state->mb_type) {
        state->cbpy = CBPY_INTRA(cbpy);
    } else {
        state->cbpy = CBPY_INTER(cbpy);
    }
    skip_stream_bits(stream, CBPY_NUMBITS(cbpy));

    /* DQUANT -- only exists in +Q mode */
    if(H263_INTER_Q == state->mb_type || H263_INTRA_Q  == state->mb_type || 
       H263_INTER4V_Q == state->mb_type) {
        data_reg = 0;
        if(!read_stream_bits(stream, H263_STREAM_DQUANT_LEN, &data_reg)) {
            return SAMPLE_STATUS_BITSTREAM_ERR;
        }
        state->dquant = dquant_mb_h263[data_reg];
    }

    return SAMPLE_STATUS_NOERR;
}


/* EOF */

⌨️ 快捷键说明

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