⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 meutility.c

📁 Nokia H.264/AVC Encoder/Decoder Usage Manual
💻 C
📖 第 1 页 / 共 3 页
字号:
/*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 <limits.h>#include "vlcutility.h"#include "transform.h"#define MAX_DIFF        255#define ABS_DIFF(a, b)  ((absDiff+MAX_DIFF)[(int)(a) - (int)(b)])static const u_int8 absDiff[2*MAX_DIFF+1] = {  255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,  239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,  223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,  207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,  191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,  175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,  159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,  143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,  127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,  111,110,109,108,107,106,105,104,103,102,101,100, 99, 98, 97, 96,   95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80,   79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64,   63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48,   47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,   31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,   15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0,    1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,   49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,   65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,   81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,   97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,112,  113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,  129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,  145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,  161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,  177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,  193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,  209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,  225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,  241,242,243,244,245,246,247,248,249,250,251,252,253,254,255};int findSad2_16x(u_int8 *orig, u_int8* ref, int w, int blkHeight, int sad, int bestSad){  int j;  j = 0;  do {    sad += ABS_DIFF(orig[j*MBK_SIZE+0], ref[j*2*w+0]);    sad += ABS_DIFF(orig[j*MBK_SIZE+1], ref[j*2*w+2]);    sad += ABS_DIFF(orig[j*MBK_SIZE+2], ref[j*2*w+4]);    sad += ABS_DIFF(orig[j*MBK_SIZE+3], ref[j*2*w+6]);    sad += ABS_DIFF(orig[j*MBK_SIZE+4], ref[j*2*w+8]);    sad += ABS_DIFF(orig[j*MBK_SIZE+5], ref[j*2*w+10]);    sad += ABS_DIFF(orig[j*MBK_SIZE+6], ref[j*2*w+12]);    sad += ABS_DIFF(orig[j*MBK_SIZE+7], ref[j*2*w+14]);    sad += ABS_DIFF(orig[j*MBK_SIZE+8], ref[j*2*w+16]);    sad += ABS_DIFF(orig[j*MBK_SIZE+9], ref[j*2*w+18]);    sad += ABS_DIFF(orig[j*MBK_SIZE+10], ref[j*2*w+20]);    sad += ABS_DIFF(orig[j*MBK_SIZE+11], ref[j*2*w+22]);    sad += ABS_DIFF(orig[j*MBK_SIZE+12], ref[j*2*w+24]);    sad += ABS_DIFF(orig[j*MBK_SIZE+13], ref[j*2*w+26]);    sad += ABS_DIFF(orig[j*MBK_SIZE+14], ref[j*2*w+28]);    sad += ABS_DIFF(orig[j*MBK_SIZE+15], ref[j*2*w+30]);    j++;  } while (sad < bestSad && j < blkHeight);  return sad;}int findSad2_8x(u_int8 *orig, u_int8* ref, int w, int blkHeight, int sad, int bestSad){  int j;  j = 0;  do {    sad += ABS_DIFF(orig[j*MBK_SIZE+0], ref[j*2*w+0]);    sad += ABS_DIFF(orig[j*MBK_SIZE+1], ref[j*2*w+2]);    sad += ABS_DIFF(orig[j*MBK_SIZE+2], ref[j*2*w+4]);    sad += ABS_DIFF(orig[j*MBK_SIZE+3], ref[j*2*w+6]);    sad += ABS_DIFF(orig[j*MBK_SIZE+4], ref[j*2*w+8]);    sad += ABS_DIFF(orig[j*MBK_SIZE+5], ref[j*2*w+10]);    sad += ABS_DIFF(orig[j*MBK_SIZE+6], ref[j*2*w+12]);    sad += ABS_DIFF(orig[j*MBK_SIZE+7], ref[j*2*w+14]);    j++;  } while (sad < bestSad && j < blkHeight);  return sad;}int findSAD2(u_int8 *orig, u_int8 *ref, int w, int blkWidth,             int blkHeight,  int vecX, int vecY, int sad, int bestSad){  int j;  ref = & ref[(vecY >> 1) * w + (vecX >> 1)];  switch (blkWidth) {  case 4:    j = 0;    do {      sad += ABS_DIFF(orig[j*MBK_SIZE+0], ref[j*2*w+0]);      sad += ABS_DIFF(orig[j*MBK_SIZE+1], ref[j*2*w+2]);      sad += ABS_DIFF(orig[j*MBK_SIZE+2], ref[j*2*w+4]);      sad += ABS_DIFF(orig[j*MBK_SIZE+3], ref[j*2*w+6]);      j++;    } while (sad < bestSad && j < blkHeight);    break;  case 8:    sad = findSad2_8x(orig, ref, w, blkHeight, sad, bestSad);    break;  default:  // Block size == 16    sad = findSad2_16x(orig, ref, w, blkHeight, sad, bestSad);    break;  }  return sad;}#define findSAD2_4x(orig, ref, w, blkHeight, sad, bestSad, j)  {    \    (j) = 0;                                                        \    do {                                                            \      (sad) += ABS_DIFF((orig)[j*MBK_SIZE+0], (ref)[j*2*(w)+0]);    \      (sad) += ABS_DIFF((orig)[j*MBK_SIZE+1], (ref)[j*2*(w)+2]);    \      (sad) += ABS_DIFF((orig)[j*MBK_SIZE+2], (ref)[j*2*(w)+4]);    \      (sad) += ABS_DIFF((orig)[j*MBK_SIZE+3], (ref)[j*2*(w)+6]);    \      j ++;                                                         \    } while ((sad) < (bestSad) && j < (blkHeight));                 \  }static void halfPixelDiff(u_int8 *orig, u_int8 *ref, int w,                           int blk[BLK_SIZE][BLK_SIZE]){  blk[0][0] = orig[0* MBK_SIZE + 0] - ref[0 * w + 0];  blk[0][1] = orig[0* MBK_SIZE + 1] - ref[0 * w + 2];  blk[0][2] = orig[0* MBK_SIZE + 2] - ref[0 * w + 4];  blk[0][3] = orig[0* MBK_SIZE + 3] - ref[0 * w + 6];  blk[1][0] = orig[1* MBK_SIZE + 0] - ref[2 * w + 0];  blk[1][1] = orig[1* MBK_SIZE + 1] - ref[2 * w + 2];  blk[1][2] = orig[1* MBK_SIZE + 2] - ref[2 * w + 4];  blk[1][3] = orig[1* MBK_SIZE + 3] - ref[2 * w + 6];  blk[2][0] = orig[2* MBK_SIZE + 0] - ref[4 * w + 0];  blk[2][1] = orig[2* MBK_SIZE + 1] - ref[4 * w + 2];  blk[2][2] = orig[2* MBK_SIZE + 2] - ref[4 * w + 4];  blk[2][3] = orig[2* MBK_SIZE + 3] - ref[4 * w + 6];  blk[3][0] = orig[3* MBK_SIZE + 0] - ref[6 * w + 0];  blk[3][1] = orig[3* MBK_SIZE + 1] - ref[6 * w + 2];  blk[3][2] = orig[3* MBK_SIZE + 2] - ref[6 * w + 4];  blk[3][3] = orig[3* MBK_SIZE + 3] - ref[6 * w + 6];}int quarterPixelDiffSAD4x4(u_int8 orig[BLK_SIZE][MBK_SIZE],                           u_int8 *ref, u_int8 *ref2, int w){  int sad;  sad  = abs(orig[0][0] - ((ref[0 * w + 0]+ref2[0 * w + 0]+1)>>1));  sad += abs(orig[0][1] - ((ref[0 * w + 2]+ref2[0 * w + 2]+1)>>1));  sad += abs(orig[0][2] - ((ref[0 * w + 4]+ref2[0 * w + 4]+1)>>1));  sad += abs(orig[0][3] - ((ref[0 * w + 6]+ref2[0 * w + 6]+1)>>1));  sad += abs(orig[1][0] - ((ref[2 * w + 0]+ref2[2 * w + 0]+1)>>1));  sad += abs(orig[1][1] - ((ref[2 * w + 2]+ref2[2 * w + 2]+1)>>1));  sad += abs(orig[1][2] - ((ref[2 * w + 4]+ref2[2 * w + 4]+1)>>1));  sad += abs(orig[1][3] - ((ref[2 * w + 6]+ref2[2 * w + 6]+1)>>1));  sad += abs(orig[2][0] - ((ref[4 * w + 0]+ref2[4 * w + 0]+1)>>1));  sad += abs(orig[2][1] - ((ref[4 * w + 2]+ref2[4 * w + 2]+1)>>1));  sad += abs(orig[2][2] - ((ref[4 * w + 4]+ref2[4 * w + 4]+1)>>1));  sad += abs(orig[2][3] - ((ref[4 * w + 6]+ref2[4 * w + 6]+1)>>1));  sad += abs(orig[3][0] - ((ref[6 * w + 0]+ref2[6 * w + 0]+1)>>1));  sad += abs(orig[3][1] - ((ref[6 * w + 2]+ref2[6 * w + 2]+1)>>1));  sad += abs(orig[3][2] - ((ref[6 * w + 4]+ref2[6 * w + 4]+1)>>1));  sad += abs(orig[3][3] - ((ref[6 * w + 6]+ref2[6 * w + 6]+1)>>1));  return sad;}int quarterPixelDiffSATD4x4(u_int8 orig[BLK_SIZE][MBK_SIZE],                            u_int8 *ref, u_int8 *ref2, int w){  int tmp[4][4];  int a, b, c, d;  int e;  int f;  int i;  int sad;  /*   *  A = a + b + c + d   *  B = a + b - c - d   *  C = a - b - c + d   *  D = a - b + c - d   *   *  e = a + d   *  f = b + c   *  A = e + f   *  C = e - f   *  e = a - d   *  f = b - c   *  B = e + f   *  D = e - f   */  /* Interpolation, difference and horizontal transform */  for (i = 0; i < 4; i++, ref+=2*w, ref2+=2*w) {    a = orig[i][0] - ((ref[0] + ref2[0] + 1) >> 1);    b = orig[i][1] - ((ref[2] + ref2[2] + 1) >> 1);    c = orig[i][2] - ((ref[4] + ref2[4] + 1) >> 1);    d = orig[i][3] - ((ref[6] + ref2[6] + 1) >> 1);    e = a + d;    f = b + c;    tmp[i][0] = e + f;    tmp[i][2] = e - f;    e = a - d;    f = b - c;    tmp[i][1] = e + f;    tmp[i][3] = e - f;  }  /* Vertical transform and SATD */  for (sad = 0, i = 0; i < 4; i++) {    e = tmp[0][i] + tmp[3][i];    f = tmp[1][i] + tmp[2][i];    sad += abs(e + f);    sad += abs(e - f);    e = tmp[0][i] - tmp[3][i];    f = tmp[1][i] - tmp[2][i];    sad += abs(e + f);    sad += abs(e - f);  }  return sad;}int16 bufOffs[4][4][2];void calculateBufOffs(int bufWidth){  int dx, dy;  const int8 offs[4][4][2][2] = {    {{{0,0},{0,0}}, {{0,0},{1,0}}, {{0,0},{0,0}}, {{0,0},{1,0}}},    {{{0,0},{0,1}}, {{1,0},{0,1}}, {{0,0},{0,1}}, {{0,0},{1,1}}},    {{{0,0},{0,0}}, {{0,0},{1,0}}, {{0,0},{0,0}}, {{0,0},{1,0}}},    {{{0,0},{0,1}}, {{0,0},{1,1}}, {{0,0},{0,1}}, {{1,0},{0,1}}}  };  for (dy = 0; dy < 4; dy ++)    for (dx = 0; dx < 4; dx ++)    {      bufOffs[dy][dx][0] = (int16) (offs[dy][dx][0][1] * bufWidth + offs[dy][dx][0][0]);      bufOffs[dy][dx][1] = (int16) (offs[dy][dx][1][1] * bufWidth + offs[dy][dx][1][0]);    }}int findSATD4(u_int8 *orig, u_int8 *ref, int w, int blkWidth,              int blkHeight,  int x, int y, int sad, int bestSad, int hadamard){  int i, j;  int blk[BLK_SIZE][BLK_SIZE];  ref += (y >> 1) * w + (x >> 1);  if (((x | y) & 1) == 0)  {    for (j = 0; j < blkHeight; j+=BLK_SIZE) {      for (i = 0; i < blkWidth; i+=BLK_SIZE) {        halfPixelDiff(orig + i, ref + 2 * i, w, blk);        if (hadamard)           sad += traHadamard4x4(blk)>>1;        else          sad += simpleSad4x4(blk);        if (sad >= bestSad)          return sad;      }      orig += BLK_SIZE*MBK_SIZE;      ref  += BLK_SIZE*2*w;    }  }  else {    //  : always here except ctr pos     // must be quarter pel    ////    int dx, dy;    u_int8 *ref2;    dx = x & 3;    dy = y & 3;    ref2 = ref;    ref  += bufOffs[dy][dx][0];    ref2 += bufOffs[dy][dx][1];    for (j = 0; j < blkHeight; j+=BLK_SIZE) {      for (i = 0; i < blkWidth; i+=BLK_SIZE) {        if (hadamard)           sad += quarterPixelDiffSATD4x4((u_int8 (*)[MBK_SIZE]) (orig + i),                                          ref + 2 * i, ref2 + 2 * i, w) >> 1;        else          // : must be here since hadamard is off          sad += quarterPixelDiffSAD4x4((u_int8 (*)[MBK_SIZE]) (orig + i),                                        ref + 2 * i, ref2 + 2 * i, w);        if (sad >= bestSad)          return sad;      }      orig += BLK_SIZE*MBK_SIZE;      ref  += BLK_SIZE*2*w;      ref2 += BLK_SIZE*2*w;    }  }  return sad;}/* * * findSAD4Blk: * * Parameters: *      orig            Pointer to the original pixels *      ref             Pointer to the reference pixels *      w               Width of the reference buffer *      blkWidth        Width of block *      blkHeight       Height of block *      x               Motion vector, x component *      y               Motion vector, y component *      blkSadsBuf      Store the SAD of 4x4 block *      bufOffs         Pointer offsets * * Function: *      Calculate the sum of absolute *      of differences between the original pixels and reference pixels.  *      SAD is calculated in 4x4 blocks. The SADs of each *      4x4 block are also return in the buffer. The buffer store the SADs *      of 4x4 blocks in raster order. So when "j" moves to the next row, *      the sad buffer pointer is incremented by 4. * * Returns: *      Total SAD for the partition. * */int findSAD4Blk(u_int8 *orig, u_int8 *ref, int w, int blkWidth,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -