📄 spatialblock.c
字号:
/* * * QccPack: Quantization, compression, and coding libraries * Copyright (C) 1997-2007 James E. Fowler * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, * MA 02139, USA. * *//* * This code was written by Joe Boettcher <jbb15@msstate.edu> based on * the originally developed algorithm and code by Suxia Cui. */#include "libQccPack.h"#ifdef LIBQCCPACKSPIHT_H#define QCCVIDSPATIALBLOCK_WINDOWSIZE 15typedef struct{ int intraframe; int motion_vector_bits; int intraframe_bits;} QccVIDSpatialBlockStatistics;static void QccVIDSpatialBlockPrintStatistics(const QccVIDSpatialBlockStatistics *statistics){ int total_bits; int field_width; total_bits = statistics->motion_vector_bits + statistics->intraframe_bits; field_width = (int)ceil(log10((double)total_bits)); printf(" Frame type: "); if (statistics->intraframe) printf("I\n"); else printf("P\n"); printf(" Motion-vector bits: %*d (%5.1f%%)\n", field_width, statistics->motion_vector_bits, QccMathPercent(statistics->motion_vector_bits, total_bits)); printf(" Intraframe bits: %*d (%5.1f%%)\n", field_width, statistics->intraframe_bits, QccMathPercent(statistics->intraframe_bits, total_bits)); printf("\n");}static int QccVIDSpatialBlockMotionCompensation(QccIMGImageComponent *current_frame, QccIMGImageComponent *reference_frame, QccIMGImageComponent *horizontal_motion, QccIMGImageComponent *vertical_motion, int blocksize, int subpixel_accuracy){ int return_value; QccIMGImageComponent predicted_frame; int row, col; QccIMGImageComponentInitialize(&predicted_frame); predicted_frame.num_rows = current_frame->num_rows; predicted_frame.num_cols = current_frame->num_cols; if (QccIMGImageComponentAlloc(&predicted_frame)) { QccErrorAddMessage("(QccVIDSpatialBlockMotionCompensation): Error calling QccIMGImageComponentAlloc()"); goto Error; } if (QccVIDMotionEstimationCreateCompensatedFrame(&predicted_frame, reference_frame, horizontal_motion, vertical_motion, blocksize, subpixel_accuracy)) { QccErrorAddMessage("(QccVIDSpatialBlockMotionCompensation): Error calling QccVIDMotionEstimationCreateCompensatedFrame()"); goto Error; } for (row = 0; row < current_frame->num_rows; row++) for (col = 0; col < current_frame->num_cols; col++) current_frame->image[row][col] -= predicted_frame.image[row][col]; return_value = 0; goto Return; Error: return_value = 1; Return: QccIMGImageComponentFree(&predicted_frame); return(return_value);}static int QccVIDSpatialBlockInverseMotionCompensation(QccIMGImageComponent *current_frame, QccIMGImageComponent *reference_frame, QccIMGImageComponent *horizontal_motion, QccIMGImageComponent *vertical_motion, int blocksize, int subpixel_accuracy){ int return_value; QccIMGImageComponent predicted_frame; int row, col; QccIMGImageComponentInitialize(&predicted_frame); predicted_frame.num_rows = current_frame->num_rows; predicted_frame.num_cols = current_frame->num_cols; if (QccIMGImageComponentAlloc(&predicted_frame)) { QccErrorAddMessage("(QccVIDSpatialBlockInverseMotionCompensation): Error calling QccIMGImageComponentAlloc()"); goto Error; } if (QccVIDMotionEstimationCreateCompensatedFrame(&predicted_frame, reference_frame, horizontal_motion, vertical_motion, blocksize, subpixel_accuracy)) { QccErrorAddMessage("(QccVIDSpatialBlockInverseMotionCompensation): Error calling QccVIDMotionEstimationCreateCompensatedFrame()"); goto Error; } for (row = 0; row < current_frame->num_rows; row++) for (col = 0; col < current_frame->num_cols; col++) current_frame->image[row][col] += predicted_frame.image[row][col]; return_value = 0; goto Return; Error: return_value = 1; Return: QccIMGImageComponentFree(&predicted_frame); return(return_value);}static int QccVIDSpatialBlockEncodeHeader(QccBitBuffer *output_buffer, int num_rows, int num_cols, int start_frame_num, int end_frame_num, int blocksize, int num_levels, int target_bit_cnt){ if (QccBitBufferPutInt(output_buffer, num_rows)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, num_cols)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, start_frame_num)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, end_frame_num)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, blocksize)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, num_levels)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferPutInt(output_buffer, target_bit_cnt)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferPutInt()"); return(1); } if (QccBitBufferFlush(output_buffer)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeHeader): Error calling QccBitBufferFlush()"); return(1); } return(0);}static int QccVIDSpatialBlockDecodeFrame();static int QccVIDSpatialBlockEncodeFrame(QccIMGImageComponent *current_frame, QccIMGImageComponent *reference_frame, int blocksize, int num_levels, int subpixel_accuracy, const QccWAVWavelet *wavelet, QccBitBuffer *output_buffer, QccBitBuffer *input_buffer, QccVIDMotionVectorsTable *mvd_table, QccIMGImageComponent *motion_vector_horizontal, QccIMGImageComponent *motion_vector_vertical, int read_motion_vectors, int target_bit_cnt, QccVIDSpatialBlockStatistics *statistics, const QccFilter *filter1, const QccFilter *filter2, const QccFilter *filter3){ int return_value; int num_rows; int num_cols; int intraframe; int start_position; start_position = output_buffer->bit_cnt; num_rows = current_frame->num_rows; num_cols = current_frame->num_cols; intraframe = (motion_vector_horizontal == NULL) || (motion_vector_vertical == NULL); if (statistics != NULL) statistics->intraframe = intraframe; if (!intraframe) { if (!read_motion_vectors) { if (QccVIDMotionEstimationFullSearch(current_frame, reference_frame, motion_vector_horizontal, motion_vector_vertical, blocksize, QCCVIDSPATIALBLOCK_WINDOWSIZE, subpixel_accuracy)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Error calling QccVIDMotionEstimationFullSearch()"); goto Error; } if (QccVIDMotionVectorsEncode(motion_vector_horizontal, motion_vector_vertical, NULL, subpixel_accuracy, output_buffer)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Error calling QccVIDMotionVectorsEncode()"); goto Error; } } if (QccVIDSpatialBlockMotionCompensation(current_frame, reference_frame, motion_vector_horizontal, motion_vector_vertical, blocksize, subpixel_accuracy)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Error calling QccVIDSpatialBlockMotionCompensation()"); goto Error; } if (statistics != NULL) statistics->motion_vector_bits = output_buffer->bit_cnt - start_position; if (output_buffer->bit_cnt > start_position + target_bit_cnt) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Rate is not sufficient to store all motion vectors for frame"); goto Error; } } else if (statistics != NULL) statistics->motion_vector_bits = 0; if (QccSPIHTEncode(current_frame, NULL, output_buffer, num_levels, wavelet, NULL, start_position + target_bit_cnt, 1, NULL)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Error calling QccSPIHTEncode()"); goto Error; } if (statistics != NULL) statistics->intraframe_bits = output_buffer->bit_cnt - start_position - statistics->motion_vector_bits; if (QccBitBufferFlush(output_buffer)) { QccErrorAddMessage("(QccVIDSpatialBlockEncodeFrame): Error calling QccBitBufferFlush()"); goto Error; } if (QccVIDSpatialBlockDecodeFrame(current_frame, reference_frame, blocksize, num_levels, subpixel_accuracy, target_bit_cnt, wavelet,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -