typedecision.c
上传用户:sunbaby
上传日期:2013-05-31
资源大小:242k
文件大小:4k
源码类别:

mpeg/mp3

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *
  3. *  T264 AVC CODEC
  4. *
  5. *  Copyright(C) 2004-2005 llcc <lcgate1@yahoo.com.cn>
  6. *               2004-2005 visionany <visionany@yahoo.com.cn>
  7. *
  8. *  This program is free software ; you can redistribute it and/or modify
  9. *  it under the terms of the GNU General Public License as published by
  10. *  the Free Software Foundation ; either version 2 of the License, or
  11. *  (at your option) any later version.
  12. *
  13. *  This program is distributed in the hope that it will be useful,
  14. *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
  15. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. *  GNU General Public License for more details.
  17. *
  18. *  You should have received a copy of the GNU General Public License
  19. *  along with this program ; if not, write to the Free Software
  20. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  21. *
  22. ****************************************************************************/
  23. #include <stdio.h>
  24. #include <assert.h>
  25. #include "T264.h"
  26. #include "bitstream.h"
  27. #include "typedecision.h"
  28. #include "estimation.h"
  29. #define INTRA_THRESH 2000
  30. #define SEARCH_THRESH 1000
  31. static uint32_t
  32. MBAnalysis(T264_t* t, int32_t y, int32_t x)
  33. {
  34.     uint32_t sad;
  35.     T264_vector_t vec;
  36.     uint8_t* src;
  37.     uint8_t* dst;
  38.     // 1, we try the previous frame vector
  39.     // Attention: we rely that the buffer management method is the simplest one
  40.     vec = t->refn[1].mb[y * t->mb_stride + x].vec[0][0];
  41.     
  42.     src = t->cur.Y[0]  + (y << 4) * t->stride + (x << 4);
  43.     dst = t->refn[1].Y[0] + ((vec.y >> 2) + (y << 4)) * t->edged_stride + (x << 4) + (vec.x >> 2);
  44.     sad = t->sad[MB_16x16](src, t->stride, dst, t->edged_stride);
  45.     // 2, if sad is great than thresh, we try small_diamond_search
  46.     if (sad > SEARCH_THRESH)
  47.     {
  48.         // diamond maybe enough
  49.         T264_search_context_t context;
  50.         context.limit_x = 16;
  51.         context.limit_y = 16;
  52.         context.vec_best = vec;
  53.         context.vec = &vec;
  54.         context.mb_part = MB_16x16;
  55.         sad = small_diamond_search(t, src, dst, &context, t->stride, t->edged_stride, sad);
  56.     }
  57.     return sad;
  58. }
  59. static int32_t
  60. MEAnalysis(T264_t* t)
  61. {
  62.     int32_t i, j;
  63.     uint32_t sad = 0;
  64.     uint32_t intra_thresh = INTRA_THRESH * t->mb_height * t->mb_width;
  65.     if ((t->frame_id - t->last_i_frame_id) % t->param.idrframe == 0)
  66.         return SLICE_IDR;
  67.     if ((t->frame_id - t->last_i_frame_id) % t->param.iframe == 0)
  68.         return SLICE_I;
  69.     
  70.     // we believe that only 2 gop per second at least
  71.     if ((int32_t)(t->frame_id - t->last_i_frame_id) > (int32_t)(t->param.framerate / 2))
  72.     {
  73.         // previous has set a keyframe
  74.         for (i = 0 ; i < t->mb_height ; i ++)
  75.         {
  76.             for (j = 0 ; j < t->mb_width ; j ++)
  77.             {
  78.                 sad += MBAnalysis(t, i, j);
  79.             }
  80.         }
  81.         t->emms();
  82.     }
  83.     else
  84.     {
  85.         sad = 0;
  86.     }
  87.     if (sad > intra_thresh)
  88.         return SLICE_I;
  89.     // p, b we just only use the 
  90.     if (t->pending_bframes_num < t->param.b_num)
  91.         return SLICE_B;
  92.     return SLICE_P;
  93. }
  94. int32_t
  95. decision_slice_type(T264_t* t)
  96. {
  97.     if ((t->flags & USE_SCENEDETECT) == 0)
  98.     {
  99.         if (t->frame_no % t->param.idrframe == 0)
  100.         {
  101.             return SLICE_IDR;
  102.         }
  103.         if (t->frame_no % t->param.iframe == 0)
  104.         {
  105.             return SLICE_I;
  106.         }
  107.         else    // P or B pic
  108.         {
  109.             if (t->frame_no % (t->param.b_num + 1) == 0)
  110.             {
  111.                 return SLICE_P;
  112.             }
  113.             else
  114.             {
  115.                 return SLICE_B;
  116.             }
  117.         }
  118.     }
  119.     else
  120.     {
  121.         return MEAnalysis(t);
  122.     }
  123. }