📄 scenecut.c
字号:
/*COPYRIGHT, LICENSE AND WARRANTY INFORMATIONThis software module has been originally developed by Nokia Corporation. Provided that a person, entity or a company willing to use the Software (hereinafter Licensee) comply with all the terms and conditions of this Statement and subject to the limitations set forth in this Statement Nokia grants to such Licensee a non-exclusive, sub-licensable, worldwide, limited license under copyrights owned by Nokia to use the Software for the sole purpose of creating, manufacturing, selling, marketing, or distributing (including the right to make modifications to the Software) a fully compliant decoder implementation (hereinafter "Decoder") of ITU-T Recommendation H.264 / ISO/IEC International Standard 14496-10 and an encoder implementation producing output that is decodable with the Decoder.Nokia retains the ownership of copyrights to the Software. There is no patent nor other intellectual property right of Nokia licensed under this Statement (except the copyright license above). Licensee hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if patent licenses are required, it is their responsibility to acquire the license before utilizing the Software.The license by Nokia is subject to that the Licensee grants to Nokia the non-exclusive, worldwide, royalty-free, perpetual and irrevocable covenant that the Licensee(s) shall not bring a suit before any court or administrative agency or otherwise assert a claim for infringement under the Licensee intellectual property rights that, but for a license, would be infringed by the Software against (a) Nokia or Nokia's Affiliate; or (b) other recipient of a license and covenant not to sue with respect to the Software from Nokia; or (c) contractor, customer or distributor of a party listed above in a or b, which suit or claim is related to the Software or use thereof.The Licensee(s) further agrees to grant a reciprocal license to Nokia (as granted by Nokia to the Licensee(s) on the modifications made by Licensee(s) to the Software. THE SOFTWARE IS PROVIDED "AS IS" AND THE ORIGINAL DEVELOPER DISCLAIMS ANY AND ALL WARRANTIES WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. THOSE INTENDING TO USE THE SOFTWARE ARE EXPRESSLY ADVISED THAT ITS USE MAY INFRINGE EXISTING PATENTS AND BE SUBJECT TO ROYALTY PAYMENTS TO PATENT OWNERS. ANYONE USING THE SOFTWARE ON THE BASIS OF THIS LICENSE AGREES TO OBTAIN THE NECESSARY PERMISSIONS FROM ANY AND ALL APPLICABLE PATENT OWNERS FOR SUCH USE.IN NO EVENT SHALL THE ORIGINAL DEVELOPER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This copyright, license and warranty information notice must be retained in all copies and derivative works of the Software or substantial portions thereof.*/#include <stdio.h>#include <string.h>#include "scenecut.h"#ifdef PERFORMANCE#include "timer.h"#endif/* * Define local function prototypes. */static int32 Diff_of_Q_Histograms(u_int8 *ptr1, u_int8 *ptr2, int32 fr_size);#ifdef PERFORMANCEstatic int histStart = 0, histStop = 0, diffStart = 0, diffStop = 0; extern int histTotal, diffTotal;#endif/* * * scdSceneCut * * Parameters: * ycurr current frame * ucurr * vcurr * yprev previous frame * uprev * vprev * sizex width of a luminanceframe in pixels * sizey height of a luminanceframe in pixels * subfactor Chrominance subsampling factor (4:2:0 = 4, 4:2:2 = 2 etc.) * * Function: * Examines if there is a scene cut between frames fr1 and fr2 * using local function Sum_of_Q_Histograms * * Returns: * 1, if there is a scene cut, 0 otherwise * */int32 scdSceneCut(u_int8 *ycurr, u_int8 *ucurr, u_int8 *vcurr, u_int8 *yprev, u_int8 *uprev, u_int8 *vprev, int32 sizex, int32 sizey, int32 subfactor, int *sumy, int *sumuv){ int32 threshold_y1, threshold_y2, threshold_uv, sum_y = 0, sum_uv = 0; /* compare y-frames */ sum_y = Diff_of_Q_Histograms(ycurr, yprev, sizex*sizey); *sumy = SUMY = sum_y; *sumuv = 0; /* there is definitely a scene cut if half of the colour is changed the abs sum of difference of histograms is two times the size of the frames */ threshold_y1 = QCIF_THRESHOLD_Y_HIGH; /* adapt the threshold */ threshold_y1 = (int32)( (float32)threshold_y1 * ((float32)(sizex*sizey)/25344.0) ); if (sum_y > threshold_y1) { return 1; } else { threshold_y2 = QCIF_THRESHOLD_Y_LOW; /* adapt the threshold */ threshold_y2 = (int32)( (float32)threshold_y2 * ((float32)(sizex*sizey)/25344.0) ); if (sum_y > threshold_y2) { /* compare v-frames */ *sumuv = sum_uv = Diff_of_Q_Histograms(vcurr, vprev, sizey*sizex/subfactor); threshold_uv = QCIF_THRESHOLD_UV; /* adapt the threshold */ threshold_uv = (int32)( (float32)threshold_uv * ( (float32)(sizex*sizey*4)/(float32)(subfactor*25344) )); /* if sum_y is in a upper half of the gray area, sum_uv can be lower than usually */ if ((sum_uv > threshold_uv) || ((sum_y > (threshold_y1 + threshold_y2)/2)&& (sum_uv > threshold_uv*4/5))) { return 1; } else { /* compare u-frames */ sum_uv = Diff_of_Q_Histograms(ucurr, uprev, sizey*sizex/subfactor); /* if sum_y is in a upper half of the gray area, sum_uv can be lower than usually */ if ((sum_uv > threshold_uv) || ((sum_y > (threshold_y1 + threshold_y2)/2)&& (sum_uv > threshold_uv*4/5))) { return 1; } } } } return 0;}/* * * Diff_of_Q_Histograms * * Parameters: * ptr1 y-, u- or v-component of current frame * ptr2 y-, u- or v-component of previous frame * fr_size sizex*sizey of components * * Function: * Calculates histograms of 32 bin of both ptr's, calculates their * difference and sums the absolute values of the difference * * Returns: * sum of absolute difference of quantized (32 bin) histograms * */static int32 Diff_of_Q_Histograms(u_int8 *ptr1, u_int8 *ptr2, int32 fr_size){ int16 i; int32 j, s = 0, hist1[32], hist2[32]; memset( hist1, 0, 32*sizeof(int) ); memset( hist2, 0, 32*sizeof(int) );#ifdef PERFORMANCE histStart = tmrGetTime();#endif /* calculate histograms */ for(j = 0; j < fr_size; j++) { hist1[(int) ptr1[j] >> 3]++; hist2[(int) ptr2[j] >> 3]++; }#ifdef PERFORMANCE histStop = tmrGetTime(); histTotal += histStop - histStart; diffStart = tmrGetTime();#endif /* calculate the difference */ for (i = 0; i < 32; i++) s += abs(hist2[i] - hist1[i]);#ifdef PERFORMANCE diffStop = tmrGetTime(); diffTotal += diffStop - diffStart;#endif return s;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -