#include "Ap4Mpeg2Parser.h"
#include "Ap4Utils.h"
/*----------------------------------------------------------------------
| debugging
+---------------------------------------------------------------------*/
//#define AP4_MPEG2_PARSER_ENABLE_DEBUG
#if defined(AP4_MPEG2_PARSER_ENABLE_DEBUG)
#define DBG_PRINTF_0(_x0) printf(_x0)
#define DBG_PRINTF_1(_x0, _x1) printf(_x0, _x1)
#define DBG_PRINTF_2(_x0, _x1, _x2) printf(_x0, _x1, _x2)
#define DBG_PRINTF_3(_x0, _x1, _x2, _x3) printf(_x0, _x1, _x2, _x3)
#define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4) printf(_x0, _x1, _x2, _x3, _x4)
#define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5) printf(_x0, _x1, _x2, _x3, _x4, _x5)
#define DBG_PRINTF_6(_x0, _x1, _x2, _x3, _x4, _x5, _x6) printf(_x0, _x1, _x2, _x3, _x4, _x5, _x6)
#define DBG_PRINTF_7(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7) printf(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7)
#else
#define DBG_PRINTF_0(_x0)
#define DBG_PRINTF_1(_x0, _x1)
#define DBG_PRINTF_2(_x0, _x1, _x2)
#define DBG_PRINTF_3(_x0, _x1, _x2, _x3)
#define DBG_PRINTF_4(_x0, _x1, _x2, _x3, _x4)
#define DBG_PRINTF_5(_x0, _x1, _x2, _x3, _x4, _x5)
#define DBG_PRINTF_6(_x0, _x1, _x2, _x3, _x4, _x5, _x6)
#define DBG_PRINTF_7(_x0, _x1, _x2, _x3, _x4, _x5, _x6, _x7)
#endif
#define AP4_MPEG2_PARSER_ENABLE_ERROR_LOG
#if defined(AP4_MPEG2_PARSER_ENABLE_ERROR_LOG)
#define ERROR_LOG(args...) \
do {\
fprintf(stderr, "\033[1;31m<ERROR! %s:%d, %s>: ", __FILE__, __LINE__, __FUNCTION__);\
fprintf(stderr, args);\
fprintf(stderr,"\033[0m");\
} while(0)
#else
#define ERROR_LOG(args...)
#endif
/*----------------------------------------------------------------------
| AP4_Mpeg2FrameParser::AP4_Mpeg2FrameParser
+---------------------------------------------------------------------*/
AP4_Mpeg2FrameParser::AP4_Mpeg2FrameParser() :
m_AccessUnitVclNalUnitCount(0),
m_TotalNalUnitCount(0),
m_TotalAccessUnitCount(0)
{
for (unsigned int i = 0; i < 2; i++) {
m_AccessUnitData[i].SetAlignUpSize(0x10000);
m_AccessUnitData[i].Reserve(0x40000);
}
m_ActiveAccessUnitIndex = 0;
m_LastIframeTotalAUCount = 0;
}
/*----------------------------------------------------------------------
| AP4_Mpeg2FrameParser::~AP4_Mpeg2FrameParser
+---------------------------------------------------------------------*/
AP4_Mpeg2FrameParser::~AP4_Mpeg2FrameParser()
{
}
/*----------------------------------------------------------------------
| AP4_Mpeg2FrameParser::Reset
+---------------------------------------------------------------------*/
void
AP4_Mpeg2FrameParser::Reset(void)
{
m_AccessUnitVclNalUnitCount = 0;
m_TotalNalUnitCount = 0;
m_TotalAccessUnitCount = 0;
m_ActiveAccessUnitIndex = 0;
m_LastIframeTotalAUCount = 0;
for (unsigned int i = 0; i < 2; i++) {
m_AccessUnitData[i].SetDataSize(0);
}
m_NalParser.Reset();
}
/*----------------------------------------------------------------------
| AP4_Mpeg2SequenceHeader::AP4_Mpeg2SequenceHeader
+---------------------------------------------------------------------*/
AP4_Mpeg2SequenceHeader::AP4_Mpeg2SequenceHeader() :
horizontal_size_value(720),
vertical_size_value(480),
aspect_ratio_information(0),
frame_rate_code(4),
bit_rate_value(0),
marker_bit(0),
vbv_buffer_size_value(0),
constrained_parameters_flag(0),
load_intra_quantiser_matrix(0),
load_non_intra_quantiser_matrix(0)
{
}
/*----------------------------------------------------------------------
| AP4_Mpeg2SequenceHeader::GetInfo
+---------------------------------------------------------------------*/
void
AP4_Mpeg2SequenceHeader::GetInfo(unsigned int& width, unsigned int& height, float& fps)
{
static float const frameRateFromCode[] =
{
0.0f, // forbidden
24000 / 1001.0f, // approx 23.976
24.0f,
25.0f,
30000 / 1001.0f, // approx 29.97
30.0f,
50.0f,
60000 / 1001.0f, // approx 59.94
60.0f,
0.0f, // reserved
0.0f, // reserved
0.0f, // reserved
0.0f, // reserved
0.0f, // reserved
0.0f, // reserved
0.0f // reserved
};
width = horizontal_size_value;
height = vertical_size_value;
fps = frameRateFromCode[frame_rate_code];
}
/*----------------------------------------------------------------------
| AP4_Mpeg2FrameParser::ParseSequenceHeader
+---------------------------------------------------------------------*/
AP4_Result
AP4_Mpeg2FrameParser::ParseSequenceHeader(const unsigned char* data,
unsigned int data_size,
AP4_Mpeg2SequenceHeader& sh)
{
sh.raw_bytes.SetData(data, data_size);
AP4_BitReader bits(data, data_size);
bits.SkipBits(8); // NAL Unit Type
sh.horizontal_size_value = bits.ReadBits(12);
sh.vertical_size_value = bits.ReadBits(12);
sh.aspect_ratio_information = bits.ReadBits(4);
sh.frame_rate_code = bits.ReadBits(4);
sh.bit_rate_value = bits.ReadBits(18);
sh.marker_bit = bits.ReadBit();
sh.vbv_buffer_size_value = bits.ReadBits(10);
sh.constrained_parameters_flag = bits.ReadBit();
sh.load_intra_quantiser_matrix = bits.ReadBit();
sh.load_non_intra_quantiser_matrix = bits.ReadBit();
return AP4_SUCCESS;
}
详细注释上述代码
最新发布