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

📄 h263main.c

📁 Linux下的基于intel的ipp库的h.263解码源代码
💻 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
//
//    H263 Decoder Main entry
//
******************************************************************************/
#include "samph263.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/******************************************************************************
// Function Name:   parse_cmdline_h263
//
// Description:     parse command line, currently command format:
//                  h263dec [infile] [outfile]
// Parameter:
// Input:
//          argc:   Number of arguments passed from main entry
//          argv:   Array of arguments passed from main entry
// Output:
//         param:   Pointer to structure h263_dec_param to store parameters.
// Return:
//          TRUE:   Succeeds
//         FALSE:   Number of arguments is less than 3 (argc<3)
//
// Notes:   None
******************************************************************************/
static SAMPLE_BOOL parse_cmdline_h263(int argc, char **argv, h263_dec_param *param)
{
    if(3 > argc) {
        return FALSE;
    }
    strcpy(param->in_file_name, argv[1]);
    strcpy(param->out_file_name, argv[2]);

    return TRUE;
}

/******************************************************************************
// Function Name:   print_help_info_h263
//
// Description:     print help infomation of command line
//
// Parameter:       None
// Return:          None
// Notes:           None
******************************************************************************/
static void print_help_info_h263() 
{
    //TO DO: Add Intel logo banner.
    printf("Usage: v263dec input.263 output.yuv\n");
}

/******************************************************************************
// Function Name:   output_picture_h263
//
// Description:     write current picture Y,Cb,Cr output to output file
//
// Parameter:
// Input:
//      out_file:   Pointer to output file
//   out_picture:   Pointer to structure sample_picture of current picture.
// Output:
//      out_file:   Pointer to output file
// Return:
//          None
//
// Notes:   None
******************************************************************************/
//static void output_picture_h263(FILE *out_file, sample_picture *out_picture)
void output_picture_h263(FILE *out_file, sample_picture *out_picture)
{
    int y_step  = out_picture->pic_plane_step[0];
    int cb_step = out_picture->pic_plane_step[1];
    int cr_step = out_picture->pic_plane_step[2];

    int pic_height = out_picture->pic_height;
    int pic_width  = out_picture->pic_width;
    
    unsigned char *y_plane  = out_picture->pic_plane[0];
    unsigned char *cb_plane = out_picture->pic_plane[1];
    unsigned char *cr_plane = out_picture->pic_plane[2];

    int i;

    if(NULL == out_file) {
        return;
    }

    /* Y Plane */
    for(i=0; i<pic_height; i++) {
        fwrite(y_plane, 1, pic_width, out_file);
        y_plane += y_step;
    }

    /* Cb Plane */
    for(i=0; i<pic_height/2; i++) {
        fwrite(cb_plane, 1, pic_width/2, out_file);
        cb_plane += cb_step;
    }

    /* Cr Plane */
    for(i=0; i<pic_height/2; i++) {
        fwrite(cr_plane, 1, pic_width/2, out_file);
        cr_plane += cr_step;
    }

}

/******************************************************************************
// Function Name:   init_stream_buffer_h263
//
// Description:     Allocate the stream buffer as the predefined size and 
//                  initialize the position pointer and length.
//
// Parameter:
// Input:
//        stream:   Pointer to sample_bitstream structure
// Output:
//        stream:   Pointer to sample_bitstream structure
// Return:
//  sample_status
//      [SAMPLE_STATUS_NOERR]: Succeeds
//  [SAMPLE_STATUS_NOMEM_ERR]: No enough memery 
//
// Notes:           The predefined buffer size is only set as an example, if 
//                  the size of one picture is larger than the stream buffer 
//                  size, the decoder will exit with error. 
******************************************************************************/
static sample_status init_stream_buffer_h263(sample_bitstream *stream)
{
    stream->bs_buffer = (Ipp8u*)malloc(SAMPLE_VIDEO_STREAM_BUF_SIZE);

    if(NULL == stream->bs_buffer) {
        return SAMPLE_STATUS_NOMEM_ERR;
    } 
 
    memset(stream->bs_buffer, 0, SAMPLE_VIDEO_STREAM_BUF_SIZE);

    stream->bs_bytelen       = SAMPLE_VIDEO_STREAM_BUF_SIZE;
    stream->bs_cur_byte      = stream->bs_buffer+stream->bs_bytelen;
    stream->bs_cur_bitoffset = 0;

    return SAMPLE_STATUS_NOERR;
}

/******************************************************************************
// Function Name:   load_stream_buffer_h263
//
// Description:     load stream buffer from input file, update stream length
//                  and position pointer.
//
// Parameter:
// Input:
//        stream:   Pointer to sample_bitstream structure
//     in_stream:   Pointer to input file
// Output:
//        stream:   Pointer to sample_bitstream structure
// Return:
//  sample_status:
//  [SAMPLE_STATUS_NOERR]: Succeeds
//    [SAMPLE_STATUS_ERR]: File read error occurs or zero byte is read
//
// Notes:           This function is called at the beginning of this sample
//                  application or each time the next PSC code cannot be found.
//                  If zero byte is read, it means the stream buffer cannot 
//                  hold current picture stream totally. In this condition,
//                  this function will return error code, which will cause
//                  decoder exit.
******************************************************************************/
static sample_status load_stream_buffer_h263(sample_bitstream *stream, 
                                             FILE             *in_stream)
{
    int offset    = stream->bs_cur_byte - stream->bs_buffer;
    int remain    = stream->bs_bytelen  - offset;
    int byte_read = 0;

    if(0 < remain) {
        memcpy(stream->bs_buffer, stream->bs_cur_byte, remain);
        stream->bs_cur_byte = stream->bs_buffer + remain;
    }

    byte_read = fread(stream->bs_buffer + remain, 1, 
                      SAMPLE_VIDEO_STREAM_BUF_SIZE-remain, in_stream);

    if(0 == byte_read || ferror(in_stream)) {
        return SAMPLE_STATUS_ERR;
    }

    stream->bs_bytelen  = remain + byte_read;
    stream->bs_cur_byte = stream->bs_buffer;
    
    return SAMPLE_STATUS_NOERR;
}


/******************************************************************************
// Function Name:   Main
//
// Description:     Main entry of H263 Decoder
// Command Line:    h263dec [input] [output]                 
******************************************************************************/

int main(int argc, char** argv)
{
    sample_status       ret = SAMPLE_STATUS_NOERR;
    sample_bitstream    in_stream;
    sample_picture      cur_picture, ref_picture;
    sample_picture      *out_picture;

    h263_dec_state      state;
    h263_dec_param      param;

    FILE                *in_cmp, *out_yuv;
    SAMPLE_BOOL         succeeded = TRUE;
    SAMPLE_BOOL         end = FALSE;

    /*******************************************
    // Parse Command line 
    *******************************************/
    if(!parse_cmdline_h263(argc, argv, &param)) {
        print_help_info_h263();
        return -1;
    }
    
    /*******************************************
    // Open Input/Output Files
    *******************************************/
    in_cmp  = fopen(param.in_file_name, "rb");
    if(NULL == in_cmp) {
        printf("Cannot open input file %s.\n", param.in_file_name);
        return -1;
    }

    out_yuv = fopen(param.out_file_name, "wb");
    if(NULL == out_yuv) {
        printf("Cannot open or create output file %s.\n", param.out_file_name);
        return -1;
    }

    /*******************************************
    // Initialize structures
    *******************************************/
    memset(&state, 0, sizeof(h263_dec_state));
    memset(&in_stream, 0, sizeof(sample_bitstream));
    memset(&cur_picture, 0, sizeof(sample_picture));
    memset(&ref_picture, 0, sizeof(sample_picture));
    state.cur_picture = &cur_picture;
    state.ref_picture = &ref_picture;

    /*******************************************
    // Initialize stream buffer, prepare for read
    *******************************************/
    ret = init_stream_buffer_h263(&in_stream);
    if(SAMPLE_STATUS_NOERR != ret) {
        fclose(in_cmp);
        fclose(out_yuv);
        return -1;
    }

    /*******************************************
    // Load stream buffer from input buffer
    *******************************************/
    ret = load_stream_buffer_h263(&in_stream, in_cmp);
    if(SAMPLE_STATUS_NOERR != ret) {
        free(in_stream.bs_buffer);
        fclose(in_cmp);
        fclose(out_yuv);
        return -1;
    }

    /*******************************************
    // Initialize decoder state structure 
    // and allocate work buffer memory
    *******************************************/
    ret = decoder_init_alloc_h263(&in_stream, &state);
    if(SAMPLE_STATUS_NOERR != ret) {
        free(in_stream.bs_buffer);
        fclose(in_cmp);
        fclose(out_yuv);
        return -1;
    }
    //fclose(out_yuv);
    /*******************************************
    // Loop for decoding
    *******************************************/
    printf("Source bitstream: %s\n", param.in_file_name);
    printf("Destination bitstream: %s\n\n", param.out_file_name);

    while(!end) {

        printf("Decoding No.%3d picture...\r", state.picture_index);

        ret = decode_h263(&in_stream, &state, &out_picture);

        if(SAMPLE_STATUS_SYNCNOTFOUND_ERR == ret) {

            /* Not a complete picture, load from stream buffer */
            if(!feof(in_cmp)) {
                if(SAMPLE_STATUS_NOERR != 
                   load_stream_buffer_h263(&in_stream, in_cmp)) {
                    succeeded = FALSE;
                    end = TRUE;
                }
            } else {
                /* Indicate it's last picture to be decoded */
                state.is_last_picture = 1;
            }

        } else if(SAMPLE_STATUS_NOERR == ret) {

            /* Decode OK, write output picture */
            output_picture_h263(out_yuv, out_picture);
            if(state.is_last_picture) {
                end = TRUE;
            }
            
        } else {

            /* Decode Fail, exit loop */
            succeeded = FALSE;
            end = TRUE;
        }
    }

    /*******************************************
    // Free buffers and close files
    *******************************************/
    decoder_free_h263(&state);
    free(in_stream.bs_buffer);
    fclose(in_cmp);
    fclose(out_yuv);

    /*******************************************
    // Print decode result informations
    *******************************************/
    if(succeeded) {
        printf("\n\nDecode OK, total %d pictures decoded.\n", state.picture_index);
    } else {
        printf("\n\nDecode fail at %d picture.\n", state.picture_index);
    }

    /*******************************************
    // Return
    *******************************************/
    return (ret==SAMPLE_STATUS_NOERR?0:-1);
}


/* EOF */

⌨️ 快捷键说明

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