📄 decoder.cpp
字号:
/*//// 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) 2001-2005 Intel Corporation. All Rights Reserved.//*/#include "precomp.h"#include <stdio.h>#include <string.h>#ifndef __JPEGBASE_H__#include "jpegbase.h"#endif#ifndef __DECODER_H__#include "decoder.h"#endif#ifdef _OPENMP#include <omp.h>#endifstatic int get_num_threads(void){ int maxThreads = 1;#ifdef _OPENMP kmp_set_blocktime(0);#pragma omp parallel shared(maxThreads) {#pragma omp master { maxThreads = omp_get_num_threads(); } }#endif return maxThreads;} // get_num_threads()static void set_num_threads(int maxThreads){ maxThreads = maxThreads;#ifdef _OPENMP omp_set_num_threads(maxThreads);#endif return;} // set_num_threads()CJPEGDecoder::CJPEGDecoder(void){ Reset(); return;} // ctorCJPEGDecoder::~CJPEGDecoder(void){ Clean(); return;} // dtorvoid CJPEGDecoder::Reset(void){ m_src.pData = 0; m_src.DataLen = 0; m_jpeg_width = 0; m_jpeg_height = 0; m_jpeg_ncomp = 0; m_jpeg_precision = 8; m_jpeg_sampling = JS_444; m_jpeg_color = JC_UNKNOWN; m_jpeg_quality = 100; m_jpeg_restart_interval = 0; m_jpeg_comment_detected = 0; m_jpeg_comment = 0; m_jpeg_comment_size = 0; m_precision = 8; m_numxMCU = 0; m_numyMCU = 0; m_mcuWidth = 0; m_mcuHeight = 0; m_ccWidth = 0; m_ccHeight = 0; m_xPadding = 0; m_yPadding = 0; m_restarts_to_go = 0; m_next_restart_num = 0; m_sos_len = 0; m_curr_comp_no = 0; m_ss = 0; m_se = 0; m_al = 0; m_ah = 0; m_jpeg_mode = JPEG_BASELINE; m_dc_scan_completed = 0; m_ac_scans_completed = 0; m_scan_count = 0; m_marker = JM_NONE; m_jfif_app0_detected = 0; m_jfif_app0_major = 0; m_jfif_app0_minor = 0; m_jfif_app0_units = 0; m_jfif_app0_xDensity = 0; m_jfif_app0_yDensity = 0; m_jfif_app0_thumb_width = 0; m_jfif_app0_thumb_height = 0; m_avi1_app0_detected = 0; m_avi1_app0_polarity = 0; m_avi1_app0_reserved = 0; m_avi1_app0_field_size = 0; m_avi1_app0_field_size2 = 0; m_exif_app1_detected = 0; m_exif_app1_data_size = 0; m_exif_app1_data = 0; m_jfxx_app0_detected = 0; m_jfxx_thumbnails_type = 0; m_adobe_app14_detected = 0; m_adobe_app14_version = 0; m_adobe_app14_flags0 = 0; m_adobe_app14_flags1 = 0; m_adobe_app14_transform = 0; m_block_buffer = 0; m_nblock = 1;#ifdef __TIMING__ m_clk_dct = 0; m_clk_ss = 0; m_clk_cc = 0; m_clk_diff = 0; m_clk_huff = 0;#endif return;} // CJPEGDecoder::Reset(void)JERRCODE CJPEGDecoder::Clean(void){ int i; m_jpeg_comment_detected = 0; if(0 != m_jpeg_comment) { ippFree(m_jpeg_comment); m_jpeg_comment = 0; m_jpeg_comment_size = 0; } m_avi1_app0_detected = 0; m_avi1_app0_polarity = 0; m_avi1_app0_reserved = 0; m_avi1_app0_field_size = 0; m_avi1_app0_field_size2 = 0; m_jfif_app0_detected = 0; m_jfxx_app0_detected = 0; m_exif_app1_detected = 0; if(0 != m_exif_app1_data) { ippFree(m_exif_app1_data); m_exif_app1_data = 0; } m_adobe_app14_detected = 0; for(i = 0; i < MAX_COMPS_PER_SCAN; i++) { if(0 != m_ccomp[i].m_cc_buffer) { ippFree(m_ccomp[i].m_cc_buffer); m_ccomp[i].m_cc_buffer = 0; } if(0 != m_ccomp[i].m_ss_buffer) { ippFree(m_ccomp[i].m_ss_buffer); m_ccomp[i].m_ss_buffer = 0; } m_ccomp[i].m_curr_row = 0; m_ccomp[i].m_prev_row = 0; } for(i = 0; i < MAX_HUFF_TABLES; i++) { m_dctbl[i].Destroy(); m_actbl[i].Destroy(); } if(0 != m_block_buffer) { ippFree(m_block_buffer); m_block_buffer = 0; } m_state.Destroy(); return JPEG_OK;} // CJPEGDecoder::Clean()JERRCODE CJPEGDecoder::SetSource( Ipp8u* pSrc, int srcSize){ m_src.pData = pSrc; m_src.DataLen = srcSize; m_src.currPos = 0; return JPEG_OK;} // CJPEGDecoder::SetSource()JERRCODE CJPEGDecoder::SetDestination( Ipp8u* pDst, int dstStep, IppiSize dstSize, int dstChannels, JCOLOR dstColor, JSS dstSampling, int dstPrecision){ m_dst.p.Data8u = pDst; m_dst.lineStep = dstStep; m_dst.width = dstSize.width; m_dst.height = dstSize.height; m_dst.nChannels = dstChannels; m_dst.color = dstColor; m_dst.sampling = dstSampling; m_dst.precision = dstPrecision; return JPEG_OK;} // CJPEGDecoder::SetDestination() JERRCODE CJPEGDecoder::SetDestination( Ipp16s* pDst, int dstStep, IppiSize dstSize, int dstChannels, JCOLOR dstColor, JSS dstSampling, int dstPrecision){ m_dst.p.Data16s = pDst; m_dst.lineStep = dstStep; m_dst.width = dstSize.width; m_dst.height = dstSize.height; m_dst.nChannels = dstChannels; m_dst.color = dstColor; m_dst.sampling = dstSampling; m_dst.precision = dstPrecision; return JPEG_OK;} // CJPEGDecoder::SetDestination()JERRCODE CJPEGDecoder::_set_sampling(void){ switch(m_jpeg_ncomp) { case 1: if(m_ccomp[0].m_hsampling == 1 && m_ccomp[0].m_vsampling == 1) { m_jpeg_sampling = JS_444; } else { return JPEG_BAD_SAMPLING; } break; case 3: if(m_ccomp[0].m_hsampling == 1 && m_ccomp[0].m_vsampling == 1 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1) { m_jpeg_sampling = JS_444; } else if(m_ccomp[0].m_hsampling == 2 && m_ccomp[0].m_vsampling == 1 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1) { m_jpeg_sampling = JS_422; } else if(m_ccomp[0].m_hsampling == 1 && m_ccomp[0].m_vsampling == 2 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1) { m_jpeg_sampling = JS_244; } else if(m_ccomp[0].m_hsampling == 2 && m_ccomp[0].m_vsampling == 2 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1) { m_jpeg_sampling = JS_411; } else { m_jpeg_sampling = JS_OTHER; } break; case 4: if(m_ccomp[0].m_hsampling == 1 && m_ccomp[0].m_vsampling == 1 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1 && m_ccomp[3].m_hsampling == 1 && m_ccomp[3].m_vsampling == 1) { m_jpeg_sampling = JS_444; } else if(m_ccomp[0].m_hsampling == 2 && m_ccomp[0].m_vsampling == 1 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1 && m_ccomp[3].m_hsampling == 2 && m_ccomp[3].m_vsampling == 1) { m_jpeg_sampling = JS_422; } else if(m_ccomp[0].m_hsampling == 1 && m_ccomp[0].m_vsampling == 2 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1 && m_ccomp[3].m_hsampling == 1 && m_ccomp[3].m_vsampling == 2) { m_jpeg_sampling = JS_244; } else if(m_ccomp[0].m_hsampling == 2 && m_ccomp[0].m_vsampling == 2 && m_ccomp[1].m_hsampling == 1 && m_ccomp[1].m_vsampling == 1 && m_ccomp[2].m_hsampling == 1 && m_ccomp[2].m_vsampling == 1 && m_ccomp[3].m_hsampling == 2 && m_ccomp[3].m_vsampling == 2) { m_jpeg_sampling = JS_411; } else { m_jpeg_sampling = JS_OTHER; } break; } return JPEG_OK;} // CJPEGDecoder::_set_sampling()JERRCODE CJPEGDecoder::NextMarker(JMARKER* marker){ int c; int n = 0; for(;;) { if(m_src.currPos >= m_src.DataLen) { LOG0("Error: buffer too small"); return JPEG_BUFF_TOO_SMALL; } m_src._READ_BYTE(&c); if(c != 0xff) { do { if(m_src.currPos >= m_src.DataLen) { LOG0("Error: buffer too small"); return JPEG_BUFF_TOO_SMALL; } n++; m_src._READ_BYTE(&c); } while(c != 0xff); } do { if(m_src.currPos >= m_src.DataLen) { LOG0("Error: buffer too small"); return JPEG_BUFF_TOO_SMALL; } m_src._READ_BYTE(&c); } while(c == 0xff); if(c != 0) { *marker = (JMARKER)c; break; } } if(n != 0) { TRC1(" skip enormous bytes - ",n); } return JPEG_OK;} // CJPEGDecoder::NextMarker()JERRCODE CJPEGDecoder::SkipMarker(void){ int len; if(m_src.currPos + 2 >= m_src.DataLen) { LOG0("Error: buffer too small"); return JPEG_BUFF_TOO_SMALL; } m_src._READ_WORD(&len); m_src.currPos += len - 2; m_marker = JM_NONE; return JPEG_OK;} // CJPEGDecoder::SkipMarker()JERRCODE CJPEGDecoder::ProcessRestart(void){ JERRCODE jerr; IppStatus status; status = ippiDecodeHuffmanStateInit_JPEG_8u(m_state); if(ippStsNoErr != status) { LOG0("Error: ippiDecodeHuffmanStateInit_JPEG_8u() failed"); return JPEG_INTERNAL_ERROR; } for(int n = 0; n < m_jpeg_ncomp; n++) { m_ccomp[n].m_lastDC = 0; } jerr = ParseRST(); if(JPEG_OK != jerr) { LOG0("Error: ParseRST() failed");// return jerr; } m_restarts_to_go = m_jpeg_restart_interval; return JPEG_OK;} // CJPEGDecoder::ProcessRestart()JERRCODE CJPEGDecoder::ParseSOI(void){ TRC0("-> SOI"); m_marker = JM_NONE; return JPEG_OK;} // CJPEGDecoder::ParseSOI()JERRCODE CJPEGDecoder::ParseEOI(void){ TRC0("-> EOI"); m_marker = JM_NONE; return JPEG_OK;} // CJPEGDecoder::ParseEOI()const int APP0_JFIF_LENGTH = 14;const int APP0_JFXX_LENGTH = 6;const int APP0_AVI1_LENGTH = 14;JERRCODE CJPEGDecoder::ParseAPP0(void){ int len; TRC0("-> APP0"); if(m_src.currPos + 2 >= m_src.DataLen) { LOG0("Error: buffer too small"); return JPEG_BUFF_TOO_SMALL; } m_src._READ_WORD(&len); len -= 2; if(len >= APP0_JFIF_LENGTH && m_src.pData[m_src.currPos + 0] == 0x4a && m_src.pData[m_src.currPos + 1] == 0x46 && m_src.pData[m_src.currPos + 2] == 0x49 && m_src.pData[m_src.currPos + 3] == 0x46 && m_src.pData[m_src.currPos + 4] == 0) { // we've found JFIF APP0 marker len -= 5; m_src.currPos += 5; m_jfif_app0_detected = 1; m_src._READ_BYTE(&m_jfif_app0_major); m_src._READ_BYTE(&m_jfif_app0_minor); m_src._READ_BYTE(&m_jfif_app0_units); m_src._READ_WORD(&m_jfif_app0_xDensity); m_src._READ_WORD(&m_jfif_app0_yDensity); m_src._READ_BYTE(&m_jfif_app0_thumb_width); m_src._READ_BYTE(&m_jfif_app0_thumb_height); len -= 9; } if(len >= APP0_JFXX_LENGTH && m_src.pData[m_src.currPos + 0] == 0x4a && m_src.pData[m_src.currPos + 1] == 0x46 && m_src.pData[m_src.currPos + 2] == 0x58 && m_src.pData[m_src.currPos + 3] == 0x58 && m_src.pData[m_src.currPos + 4] == 0) { // we've found JFXX APP0 extension marker len -= 5; m_src.currPos += 5; m_jfxx_app0_detected = 1; m_src._READ_BYTE(&m_jfxx_thumbnails_type); switch(m_jfxx_thumbnails_type) { case 0x10: break; case 0x11: break; case 0x13: break; default: break; } len -= 1; } if(len >= APP0_AVI1_LENGTH && m_src.pData[m_src.currPos + 0] == 0x41 && m_src.pData[m_src.currPos + 1] == 0x56 && m_src.pData[m_src.currPos + 2] == 0x49 && m_src.pData[m_src.currPos + 3] == 0x31) { // we've found AVI1 APP0 marker len -= 4; m_src.currPos += 4; m_avi1_app0_detected = 1; m_src._READ_BYTE(&m_avi1_app0_polarity); len -= 1; if(len == 7) // old MJPEG AVI len -= 7; if(len == 9) // ODML MJPEG AVI { m_src._READ_BYTE(&m_avi1_app0_reserved); m_src._READ_DWORD(&m_avi1_app0_field_size); m_src._READ_DWORD(&m_avi1_app0_field_size2); len -= 9; } } m_src.currPos += len; m_marker = JM_NONE; return JPEG_OK;} // CJPEGDecoder::ParseAPP0()JERRCODE CJPEGDecoder::ParseAPP1(void){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -