📄 gethdr.c
字号:
/*
* Copyright (c) 1997-2000 by TriMedia Technologies.
*
* +------------------------------------------------------------------+
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such |
* | a license and with the inclusion of this copyright notice. This |
* | software or any other copies of this software may not be provided|
* | or otherwise made available to any other person. The ownership |
* | and title of this software is not transferred. |
* | |
* | The information in this software is subject to change without |
* | any prior notice and should not be construed as a commitment by |
* | TriMedia Technologies. |
* | |
* | this code and information is provided "as is" without any |
* | warranty of any kind, either expressed or implied, including but |
* | not limited to the implied warranties of merchantability and/or |
* | fitness for any particular purpose. |
* +------------------------------------------------------------------+
*
* Module name : mpeg_gethdr.c 1.7
*
* Last update : 13:12:00 - 99/01/08
*
* Description :
*
*/
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include "tmalTm2VdecMpeg.h"
#include <tmlib/dprintf.h>
#ifndef QUIET
#define PRINT(x) printf x
#define MY_DP(x) DP(x)
#else
#define MY_DP(x) DP(x)
#define PRINT(x) (DP(x))
#endif
#define Get_Bits(a) get_bits(livp,(a))
#define Flush_Buffer(a) flush_buffer(livp,(a))
void parse_headers (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
int slice_header (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
/* private prototypes */
static void sequence_header (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void group_of_pictures_header (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void picture_header (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void extension_and_user_data (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void sequence_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void sequence_display_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void quant_matrix_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void picture_display_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void picture_coding_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static void copyright_extension (tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph);
static int extra_bit_information (tm2MpegDecInstVars_t *livp);
static void Initialize_Sequence (tm2MpegDecInstVars_t *livp);
static void Update_Picture_Buffers (tm2MpegDecInstVars_t *livp);
extern int sequence_done;
/* zig-zag and alternate scan patterns */
static unsigned char scan[2][64]= {
{ /* Zig-Zag scan pattern */
0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
},
{ /* Alternate scan pattern */
0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
}
};
#define RESERVED -1
static double frame_rate_Table[16] =
{
0.0,
((23.0*1000.0)/1001.0),
24.0,
25.0,
((30.0*1000.0)/1001.0),
30.0,
50.0,
((60.0*1000.0)/1001.0),
60.0,
RESERVED,
RESERVED,
RESERVED,
RESERVED,
RESERVED,
RESERVED,
RESERVED
};
static void marker_bit(tm2MpegDecInstVars_t *livp, char *text)
{
Get_Bits(1);
}
void parse_headers(ptm2MpegDecInstVars_t livp, tmMPEGHdr_t *lph)
{
unsigned code;
while(1) {
code = 0x100 | get_bits(livp,8);
if(code == SEQUENCE_HEADER_CODE) {
#ifdef FILEIO
sequence_done = 0;
#endif
sequence_header(livp,lph);
}
else if(code==GROUP_START_CODE) {
group_of_pictures_header(livp,lph);
}
else if(code==PICTURE_START_CODE) {
picture_header(livp,lph);
}
else if(code==SEQUENCE_END_CODE) {
if(livp->sequenceFramenum!=0){
PRINT(("Done Seq\n"));
#ifdef SELF_CHECK
Output_Last_Frame_of_Sequence(livp);
exit(0);
#elif WRITE_TO_FILE
putlast(livp);
#else
PRINT(("Display last frame of sequence\n"));
#endif
sequence_done = 1;
return;
}
}
else {
PRINT(("Unexpected start code %x",code));
exit(1);
}
extension_and_user_data(livp,lph);
if(code==PICTURE_START_CODE) {
if(livp->ph.picture_structure==FRAME_PICTURE && livp->secondField) {
printf("odd number of field pictures\n");
livp->secondField = 0;
}
if(livp->formatChange == True){
Initialize_Sequence(livp);
livp->formatChange = False;
}
if(!livp->secondField)
Update_Picture_Buffers(livp);
if(livp->ph.picture_coding_type==D_TYPE) {
printf("Can't handle D picture!\n");
exit(1);
}
break;
}
}
} /* parse_headers() */
static void sequence_header(tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph)
{
int i,hsize,vsize;
hsize = Get_Bits(12);
vsize = Get_Bits(12);
if (hsize == 0)
exit(1);
if(((lph->horizontal_size << (livp->halfResMode == 1)) != hsize) || (lph->vertical_size != vsize)){
if((hsize <= 1920) && (vsize <= 1088)){
livp->formatChange = True;
lph->horizontal_size = hsize;
lph->vertical_size = vsize;
}
}
lph->aspect_ratio_information = Get_Bits(4);
lph->frame_rate_code = Get_Bits(4);
lph->bit_rate_value = Get_Bits(18);
marker_bit(livp,"sequence_header()");
lph->vbv_buffer_size = Get_Bits(10);
lph->constrained_parameters_flag = Get_Bits(1);
lph->MPEG2_Flag = 0;
if((lph->load_intra_quantizer_matrix = Get_Bits(1))) {
for (i=0; i<64; i++)
lph->intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
}
if((lph->load_non_intra_quantizer_matrix = Get_Bits(1))) {
for (i=0; i<64; i++)
lph->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
}
}
static void group_of_pictures_header(tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph)
{
lph->drop_flag = Get_Bits(1);
lph->hour = Get_Bits(5);
lph->minute = Get_Bits(6);
marker_bit(livp,"group_of_pictures_header()");
lph->sec = Get_Bits(6);
lph->frame = Get_Bits(6);
lph->closed_gop = Get_Bits(1);
lph->broken_link = Get_Bits(1);
}
static void picture_header(tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph)
{
int Extra_Information_Byte_Count;
lph->temporal_reference = Get_Bits(10);
lph->picture_coding_type = Get_Bits(3);
lph->vbv_delay = Get_Bits(16);
if (lph->picture_coding_type==P_TYPE || lph->picture_coding_type==B_TYPE) {
lph->full_pel_forward_vector = Get_Bits(1);
lph->forward_f_code = Get_Bits(3);
}
if (lph->picture_coding_type==B_TYPE) {
lph->full_pel_backward_vector = Get_Bits(1);
lph->backward_f_code = Get_Bits(3);
}
Extra_Information_Byte_Count =
extra_bit_information(livp);
}
int slice_header(tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph)
{
int slice_vertical_position_extension;
int slice_picture_id_enable = 0;
int slice_picture_id = 0;
int extra_information_slice = 0;
slice_vertical_position_extension =
(lph->MPEG2_Flag && lph->vertical_size>2800) ? Get_Bits(3) : 0;
lph->quantizer_scale_code = Get_Bits(5);
/* slice_id introduced in March 1995 as part of the video corridendum
(after the IS was drafted in November 1994) */
if (Get_Bits(1)) {
lph->intra_slice = Get_Bits(1);
slice_picture_id_enable = Get_Bits(1);
slice_picture_id = Get_Bits(6);
extra_information_slice = extra_bit_information(livp);
}
else
lph->intra_slice = 0;
return slice_vertical_position_extension;
}
static void extension_and_user_data(tm2MpegDecInstVars_t *livp, tmMPEGHdr_t *lph)
{
unsigned code;
code = find_start_code();
while((code==USER_DATA_START_CODE) || (code== EXTENSION_START_CODE)) {
Flush_Buffer(8);
if(code==EXTENSION_START_CODE) {
int ext_ID = Get_Bits(4);
switch (ext_ID) {
case SEQUENCE_EXTENSION_ID:
sequence_extension(livp,lph);
break;
case SEQUENCE_DISPLAY_EXTENSION_ID:
sequence_display_extension(livp,lph);
break;
case QUANT_MATRIX_EXTENSION_ID:
quant_matrix_extension(livp,lph);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -