📄 bitstream.c
字号:
/***************************************************************************** * * XVID MPEG-4 VIDEO CODEC * - Bitstream reader/writer functions - * * Copyright (C) 2001-2002 - Peter Ross <pross@xvid.org> * * This file is part of XviD, a free MPEG-4 video encoder/decoder * * XviD is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Under section 8 of the GNU General Public License, the copyright * holders of XVID explicitly forbid distribution in the following * countries: * * - Japan * - United States of America * * Linking XviD statically or dynamically with other modules is making a * combined work based on XviD. Thus, the terms and conditions of the * GNU General Public License cover the whole combination. * * As a special exception, the copyright holders of XviD give you * permission to link XviD with independent modules that communicate with * XviD solely through the VFW1.1 and DShow interfaces, regardless of the * license terms of these independent modules, and to copy and distribute * the resulting combined work under terms of your choice, provided that * every copy of the combined work is accompanied by a complete copy of * the source code of XviD (the version of XviD used to produce the * combined work), being distributed under the terms of the GNU General * Public License plus this exception. An independent module is a module * which is not derived from or based on XviD. * * Note that people who make modified versions of XviD are not obligated * to grant this special exception for their modified versions; it is * their choice whether to do so. The GNU General Public License gives * permission to release a modified version without this exception; this * exception also makes it possible to release a modified version which * carries forward this exception. * * $Id: bitstream.c,v 1.37.2.1 2003/07/28 12:39:27 edgomez Exp $ * ****************************************************************************/#include "bitstream.h"#include "zigzag.h"#include "../quant/quant_matrix.h"/***************************************************************************** * Functions ****************************************************************************/static uint32_t __inlinelog2bin(uint32_t value){/* Changed by Chenm001 */#if !defined(_MSC_VER) int n = 0; while (value) { value >>= 1; n++; } return n;#else __asm { bsr eax, value inc eax }#endif}static const uint32_t intra_dc_threshold_table[] = { 32, /* never use */ 13, 15, 17, 19, 21, 23, 1,};voidbs_get_matrix(Bitstream * bs, uint8_t * matrix){ int i = 0; int last, value = 0; do { last = value; value = BitstreamGetBits(bs, 8); matrix[scan_tables[0][i++]] = value; } while (value != 0 && i < 64); i--; /* fix little bug at coeff not full */ while (i < 64) { matrix[scan_tables[0][i++]] = last; }}/* for PVOP addbits == fcode - 1 *//* for BVOP addbits == max(fcode,bcode) - 1 *//* returns mbpos */intread_video_packet_header(Bitstream *bs, const int addbits, int * quant){ int nbits; int mbnum; int hec; nbits = NUMBITS_VP_RESYNC_MARKER + addbits; BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); BitstreamSkip(bs, nbits); DPRINTF(DPRINTF_STARTCODE, "<video_packet_header>"); /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { */ /* hec */ /* vop_width */ /* marker_bit */ /* vop_height */ /* marker_bit */ /*} */ mbnum = BitstreamGetBits(bs, 9); DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); /* if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) */ *quant = BitstreamGetBits(bs, 5); DPRINTF(DPRINTF_HEADER, "quant %i", *quant); /* if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) */ hec = BitstreamGetBit(bs); DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); /* if (hec) */ /* .. decoder hec-header ... */ return mbnum;}/*decode headersreturns coding_type, or -1 if error*/#define VIDOBJ_START_CODE_MASK 0x0000001f#define VIDOBJLAY_START_CODE_MASK 0x0000000fintBitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold){ uint32_t vol_ver_id; static uint32_t time_increment_resolution; uint32_t coding_type; uint32_t start_code; uint32_t time_incr = 0; int32_t time_increment = 0; do { BitstreamByteAlign(bs); start_code = BitstreamShowBits(bs, 32); if (start_code == VISOBJSEQ_START_CODE) { int profile; DPRINTF(DPRINTF_STARTCODE, "<visual_object_sequence>"); BitstreamSkip(bs, 32); /* visual_object_sequence_start_code */ profile = BitstreamGetBits(bs, 8); /* profile_and_level_indication */ DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile); } else if (start_code == VISOBJSEQ_STOP_CODE) { BitstreamSkip(bs, 32); /* visual_object_sequence_stop_code */ DPRINTF(DPRINTF_STARTCODE, "</visual_object_sequence>"); } else if (start_code == VISOBJ_START_CODE) { DPRINTF(DPRINTF_STARTCODE, "<visual_object>"); BitstreamSkip(bs, 32); /* visual_object_start_code */ if (BitstreamGetBit(bs)) /* is_visual_object_identified */ { vol_ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */ DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); BitstreamSkip(bs, 3); /* visual_object_priority */ } else { vol_ver_id = 1; } if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */ { DPRINTF(DPRINTF_ERROR, "visual_object_type != video"); return -1; } BitstreamSkip(bs, 4); /* video_signal_type */ if (BitstreamGetBit(bs)) /* video_signal_type */ { DPRINTF(DPRINTF_HEADER,"+ video_signal_type"); BitstreamSkip(bs, 3); /* video_format */ BitstreamSkip(bs, 1); /* video_range */ if (BitstreamGetBit(bs)) /* color_description */ { DPRINTF(DPRINTF_HEADER,"+ color_description"); BitstreamSkip(bs, 8); /* color_primaries */ BitstreamSkip(bs, 8); /* transfer_characteristics */ BitstreamSkip(bs, 8); /* matrix_coefficients */ } } } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) { DPRINTF(DPRINTF_STARTCODE, "<video_object>"); DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); BitstreamSkip(bs, 32); /* video_object_start_code */ } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { DPRINTF(DPRINTF_STARTCODE, "<video_object_layer>"); DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); BitstreamSkip(bs, 32); /* video_object_layer_start_code */ BitstreamSkip(bs, 1); /* random_accessible_vol */ /* video_object_type_indication */ if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && BitstreamShowBits(bs, 8) != 0) /* BUGGY DIVX */ { DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", BitstreamShowBits(bs, 8)); return -1; } BitstreamSkip(bs, 8); if (BitstreamGetBit(bs)) /* is_object_layer_identifier */ { DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier"); vol_ver_id = BitstreamGetBits(bs, 4); /* video_object_layer_verid */ DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); BitstreamSkip(bs, 3); /* video_object_layer_priority */ } else { vol_ver_id = 1; } if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) /* aspect_ratio_info */ { DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); BitstreamSkip(bs, 8); /* par_width */ BitstreamSkip(bs, 8); /* par_height */ } if (BitstreamGetBit(bs)) /* vol_control_parameters */ { DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters"); BitstreamSkip(bs, 2); /* chroma_format */ dec->low_delay = (uint8_t)BitstreamGetBit(bs); /* low_delay */ DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); if (BitstreamGetBit(bs)) /* vbv_parameters */ { DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); BitstreamSkip(bs, 15); /* first_half_bitrate */ READ_MARKER(); BitstreamSkip(bs, 15); /* latter_half_bitrate */ READ_MARKER(); BitstreamSkip(bs, 15); /* first_half_vbv_buffer_size */ READ_MARKER(); BitstreamSkip(bs, 3); /* latter_half_vbv_buffer_size */ BitstreamSkip(bs, 11); /* first_half_vbv_occupancy */ READ_MARKER(); BitstreamSkip(bs, 15); /* latter_half_vbv_occupancy */ READ_MARKER(); } } dec->shape = BitstreamGetBits(bs, 2); /* video_object_layer_shape */ DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { BitstreamSkip(bs, 4); /* video_object_layer_shape_extension */ } READ_MARKER();/* *************************** for decode B-frame time *********************** */ time_increment_resolution = BitstreamGetBits(bs, 16); /* vop_time_increment_resolution */ DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution);/* time_increment_resolution--; */ if (time_increment_resolution > 0) { dec->time_inc_bits = log2bin(time_increment_resolution-1); } else { /* dec->time_inc_bits = 0; */ /* for "old" xvid compatibility, set time_inc_bits = 1 */ dec->time_inc_bits = 1; } READ_MARKER(); if (BitstreamGetBit(bs)) /* fixed_vop_rate */ { DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate"); BitstreamSkip(bs, dec->time_inc_bits); /* fixed_vop_time_increment */ } if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { uint32_t width, height; READ_MARKER(); width = BitstreamGetBits(bs, 13); /* video_object_layer_width */ READ_MARKER(); height = BitstreamGetBits(bs, 13); /* video_object_layer_height */ READ_MARKER(); DPRINTF(DPRINTF_HEADER, "width %i", width); DPRINTF(DPRINTF_HEADER, "height %i", height); /* for auto set width & height */ if (dec->width == 0) dec->width = width; if (dec->height == 0) dec->height = height; if (width != dec->width || height != dec->height) { DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); return -1; } } dec->interlacing = BitstreamGetBit(bs); DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); if (!BitstreamGetBit(bs)) /* obmc_disable */ { DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported"); /* TODO */ /* fucking divx4.02 has this enabled */ } if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) /* sprite_enable */ { DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); return -1; } if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { BitstreamSkip(bs, 1); /* sadct_disable */ } if (BitstreamGetBit(bs)) /* not_8_bit */ { DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)"); dec->quant_bits = BitstreamGetBits(bs, 4); /* quant_precision */ BitstreamSkip(bs, 4); /* bits_per_pixel */ } else { dec->quant_bits = 5; } if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { BitstreamSkip(bs, 1); /* no_gray_quant_update */ BitstreamSkip(bs, 1); /* composition_method */ BitstreamSkip(bs, 1); /* linear_composition */ } dec->quant_type = BitstreamGetBit(bs); /* quant_type */ DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type); if (dec->quant_type) { if (BitstreamGetBit(bs)) /* load_intra_quant_mat */ { uint8_t matrix[64]; DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -