📄 mp4dbuff.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: Stream buffer processing functions of MPEG-4 video decoder
// sample code for Intel(R) Integrated Performance Primitives.
// Functions List:
// get_bits_mpeg4()
// load_video_buffer()
// rewind_buffer_mpeg4()
// init_input_video_buffer()
// release_input_video_buffer()
******************************************************************************/
#include <stdlib.h>
#include <memory.h>
#include "sampmp4.h"
/******************************************************************************
// Name: rewind_buffer_mpeg4
// Description: Skip N bits back in video buffer
// Input Arguments:
// stream_buf - Pointer to the source compressed video bitstream
// n_bits - Number of bits to skip back
//
// Output Arguments:
// stream_buf - Pointer to the updated source video stream after
// the N bits is skipped back
//
// Returns:
// None
******************************************************************************/
void rewind_buffer_mpeg4 (sample_bitstream *stream_buf, int n_bits)
{
int n_bytes;
n_bytes = (n_bits >> 3);
n_bits -= (n_bytes << 3);
if (stream_buf->bs_cur_bitoffset < n_bits) {
stream_buf->bs_cur_byte -= 1;
stream_buf->bs_cur_bitoffset += 8;
}
stream_buf->bs_cur_byte -= n_bytes;
stream_buf->bs_cur_bitoffset -= n_bits;
}
/******************************************************************************
// Name: get_bits_mpeg4
// Description: Read N bits from video buffer
// Input Arguments:
// stream_buf - Pointer to the source compressed video bitstream
// n_bits - Number of bits to read, i.e., N. (N <= 32)
//
// Output Arguments:
// stream_buf - Pointer to the updated source video stream after
// the N bits is taken out
//
// Returns:
// the value of the N bits in big endian
******************************************************************************/
Ipp32u get_bits_mpeg4 (sample_bitstream *stream_buf, int n_bits)
{
Ipp32u code = 0;
int n_head_bits, n_tail_bits, n_bytes;
n_head_bits = stream_buf->bs_cur_bitoffset;
n_bytes = (n_head_bits + n_bits + 7) / 8;
n_tail_bits = n_bytes * 8 - n_head_bits - n_bits;
while (n_bytes--) {
code = (code << 8) | (*(stream_buf->bs_cur_byte)++);
}
code = (code >> n_tail_bits) & bits_mask_tbl[n_bits];
if (n_tail_bits) {
stream_buf->bs_cur_byte--;
stream_buf->bs_cur_bitoffset = 8 - n_tail_bits;
}
else {
stream_buf->bs_cur_bitoffset = 0;
}
return code;
}
/***************************************************************************
// Name: release_input_video_buffer
// Description: Release the input video bitstream buffer
//
// Input Arguments:
// stream_buf Pointer to input video bitstream buffer
//
// Output Arguments:
// stream_buf Pointer to released video bitstream buffer
//
// Returns:
// SAMPLE_STATUS_NOERR If succeeds
*****************************************************************************/
sample_status release_input_video_buffer (sample_bitstream *stream_buf)
{
if (stream_buf->bs_buffer) {
free(stream_buf->bs_buffer);
stream_buf->bs_buffer = NULL;
}
return SAMPLE_STATUS_NOERR;
}
/***************************************************************************
// Name: init_input_video_buffer
// Description: Initialize the input video bitstream buffer
//
// Input Arguments:
// stream_buf Pointer to input video bitstream buffer
//
// Output Arguments:
// stream_buf Pointer to initialized video bitstream buffer
//
// Returns:
// SAMPLE_STATUS_NOERR If succeeds
// SAMPLE_STATUS_NOMEM_ERR If memory allocation fails
*****************************************************************************/
sample_status init_input_video_buffer (sample_bitstream *stream_buf)
{
/* reset the struct to zeroes */
memset((void*)stream_buf, 0, sizeof(sample_bitstream));
/* initialize the input video bitstream buffer which is large enough
// to store two frames of raw data at least to reduce reload times,
// note to allocate extra 3 bytes for the final sync code insertion */
stream_buf->bs_buffer = (Ipp8u*)malloc(SAMPLE_VIDEO_STREAM_BUF_SIZE
+ MPEG4_SC_LEN);
if (NULL == stream_buf->bs_buffer) {
/* memory allocation fails */
return SAMPLE_STATUS_NOMEM_ERR;
}
else {
/* reset the newly allocated buffer to zeroes */
memset (stream_buf->bs_buffer, 0, SAMPLE_VIDEO_STREAM_BUF_SIZE
+ MPEG4_SC_LEN);
}
/* set current pointer to the end of buffer for there's no data loaded
// at beginning */
stream_buf->bs_cur_byte = stream_buf->bs_buffer
+ SAMPLE_VIDEO_STREAM_BUF_SIZE;
stream_buf->bs_cur_bitoffset = 0;
stream_buf->bs_bytelen = 0;
return SAMPLE_STATUS_NOERR;
}
/***************************************************************************
// Name: load_video_buffer
// Description: Load input video bitstream into buffer
//
// Input Arguments:
// stream_buf Pointer to the video bitstream buffer
// fpin Pointer to the input file stream
//
// Output Arguments:
// stream_buf Pointer to updated video bitstream buffer
//
// Returns:
// SAMPLE_STATUS_NOERR If succeeds
// SAMPLE_STATUS_ERR If no data read from the file stream
*****************************************************************************/
sample_status load_video_buffer(sample_bitstream *stream_buf, FILE *fpin)
{
int offset;
int remain_len;
offset = stream_buf->bs_cur_byte - stream_buf->bs_buffer;
remain_len = SAMPLE_VIDEO_STREAM_BUF_SIZE - offset;
/* round the buffer */
if (0 != remain_len) {
memcpy (stream_buf->bs_buffer, stream_buf->bs_cur_byte, remain_len);
}
/* fill up the buffer by loading new data into the buffer */
stream_buf->bs_bytelen = remain_len + fread(stream_buf->bs_buffer
+ remain_len, 1, offset, fpin);
/* no new data loaded */
if (stream_buf->bs_bytelen == remain_len) {
return SAMPLE_STATUS_ERR;
}
stream_buf->bs_cur_byte = stream_buf->bs_buffer;
return SAMPLE_STATUS_NOERR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -