Advertisement
Guest User

Untitled

a guest
Aug 16th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 64.44 KB | None | 0 0
  1. diff --git a/common/common.c b/common/common.c
  2. index a9e1c73..dd5bb07 100644
  3. --- a/common/common.c
  4. +++ b/common/common.c
  5. @@ -87,6 +87,7 @@ void x264_param_default( x264_param_t *param )
  6.      param->i_bframe_bias = 0;
  7.      param->i_bframe_pyramid = X264_B_PYRAMID_NORMAL;
  8.      param->b_interlaced = 0;
  9. +    param->b_field_encode = 0;
  10.      param->b_constrained_intra = 0;
  11.  
  12.      param->b_deblocking_filter = 1;
  13. @@ -798,13 +799,26 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
  14.      OPT("cabac-idc")
  15.          p->i_cabac_init_idc = atoi(value);
  16.      OPT("interlaced")
  17. +    {
  18.          p->b_interlaced = atobool(value);
  19. +        p->b_field_encode = 0;
  20. +    }
  21. +    OPT("field-encode")
  22. +    {
  23. +        p->b_interlaced = 0;
  24. +        p->b_field_encode = atobool(value);
  25. +    }
  26.      OPT("tff")
  27. -        p->b_interlaced = p->b_tff = atobool(value);
  28. +    {
  29. +        p->b_tff = atobool(value);
  30. +        if( !p->b_field_encode )
  31. +            p->b_interlaced = 1;
  32. +    }
  33.      OPT("bff")
  34.      {
  35. -        p->b_interlaced = atobool(value);
  36. -        p->b_tff = !p->b_interlaced;
  37. +        p->b_tff = !atobool(value);
  38. +        if( !p->b_field_encode )
  39. +            p->b_interlaced = 1;
  40.      }
  41.      OPT("constrained-intra")
  42.          p->b_constrained_intra = atobool(value);
  43. diff --git a/common/common.h b/common/common.h
  44. index 76ae8a3..ef2395b 100644
  45. --- a/common/common.h
  46. +++ b/common/common.h
  47. @@ -81,8 +81,8 @@ do {\
  48.  
  49.  #define ARRAY_SIZE(array)  (sizeof(array)/sizeof(array[0]))
  50.  
  51. -#define X264_BFRAME_MAX 16
  52. -#define X264_REF_MAX 16
  53. +#define X264_BFRAME_MAX 32
  54. +#define X264_REF_MAX 32
  55.  #define X264_THREAD_MAX 128
  56.  #define X264_LOOKAHEAD_THREAD_MAX 16
  57.  #define X264_PCM_COST (FRAME_SIZE(256*BIT_DEPTH)+16)
  58. @@ -125,11 +125,17 @@ do {\
  59.  #if HAVE_INTERLACED
  60.  #   define MB_INTERLACED h->mb.b_interlaced
  61.  #   define SLICE_MBAFF h->sh.b_mbaff
  62. +#   define SLICE_FIELD h->sh.b_field_pic
  63.  #   define PARAM_INTERLACED h->param.b_interlaced
  64. +#   define PARAM_FIELD_ENCODE h->param.b_field_encode
  65. +#   define MB_MBAFF_FIELD (SLICE_MBAFF & MB_INTERLACED)
  66.  #else
  67.  #   define MB_INTERLACED 0
  68.  #   define SLICE_MBAFF 0
  69. +#   define SLICE_FIELD 0
  70.  #   define PARAM_INTERLACED 0
  71. +#   define PARAM_FIELD_ENCODE 0
  72. +#   define MB_MBAFF_FIELD 0
  73.  #endif
  74.  
  75.  #ifdef CHROMA_FORMAT
  76. @@ -440,6 +446,7 @@ typedef struct x264_lookahead_t
  77.      int                           i_last_keyframe;
  78.      int                           i_slicetype_length;
  79.      x264_frame_t                  *last_nonb;
  80. +    x264_frame_t                  *penultimate_nonb;
  81.      x264_pthread_t                thread_handle;
  82.      x264_sync_frame_list_t        ifbuf;
  83.      x264_sync_frame_list_t        next;
  84. @@ -887,6 +894,7 @@ struct x264_t
  85.          int ref_blind_dupe; /* The index of the blind reference frame duplicate. */
  86.          int8_t deblock_ref_table[X264_REF_MAX*2+2];
  87.  #define deblock_ref_table(x) h->mb.deblock_ref_table[(x)+2]
  88. +        int     i_mvy_offset[2][X264_REF_MAX];
  89.      } mb;
  90.  
  91.      /* rate control encoding only */
  92. diff --git a/common/deblock.c b/common/deblock.c
  93. index 382eb72..ae62b8e 100644
  94. --- a/common/deblock.c
  95. +++ b/common/deblock.c
  96. @@ -343,8 +343,8 @@ static ALWAYS_INLINE void x264_macroblock_cache_load_neighbours_deblock( x264_t
  97.  
  98.      h->mb.i_neighbour = 0;
  99.      h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
  100. -    h->mb.b_interlaced = PARAM_INTERLACED && h->mb.field[h->mb.i_mb_xy];
  101. -    h->mb.i_mb_top_y = mb_y - (1 << MB_INTERLACED);
  102. +    h->mb.b_interlaced = PARAM_FIELD_ENCODE || (PARAM_INTERLACED && h->mb.field[h->mb.i_mb_xy]);
  103. +    h->mb.i_mb_top_y = mb_y - (1 << MB_MBAFF_FIELD);
  104.      h->mb.i_mb_top_xy = mb_x + h->mb.i_mb_stride*h->mb.i_mb_top_y;
  105.      h->mb.i_mb_left_xy[1] =
  106.      h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
  107. @@ -370,14 +370,14 @@ static ALWAYS_INLINE void x264_macroblock_cache_load_neighbours_deblock( x264_t
  108.      if( mb_x > 0 && (deblock_on_slice_edges ||
  109.          h->mb.slice_table[h->mb.i_mb_left_xy[0]] == h->mb.slice_table[h->mb.i_mb_xy]) )
  110.          h->mb.i_neighbour |= MB_LEFT;
  111. -    if( mb_y > MB_INTERLACED && (deblock_on_slice_edges
  112. +    if( mb_y > MB_MBAFF_FIELD && (deblock_on_slice_edges
  113.          || h->mb.slice_table[h->mb.i_mb_top_xy] == h->mb.slice_table[h->mb.i_mb_xy]) )
  114.          h->mb.i_neighbour |= MB_TOP;
  115.  }
  116.  
  117.  void x264_frame_deblock_row( x264_t *h, int mb_y )
  118.  {
  119. -    int b_interlaced = SLICE_MBAFF;
  120. +    int b_mbaff = SLICE_MBAFF;
  121.      int a = h->sh.i_alpha_c0_offset - QP_BD_OFFSET;
  122.      int b = h->sh.i_beta_offset - QP_BD_OFFSET;
  123.      int qp_thresh = 15 - X264_MIN( a, b ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
  124. @@ -387,7 +387,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  125.      int chroma_height = 16 >> CHROMA_V_SHIFT;
  126.      intptr_t uvdiff = chroma444 ? h->fdec->plane[2] - h->fdec->plane[1] : 1;
  127.  
  128. -    for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x += (~b_interlaced | mb_y)&1, mb_y ^= b_interlaced )
  129. +    for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x += (~b_mbaff | mb_y)&1, mb_y ^= b_mbaff )
  130.      {
  131.          x264_prefetch_fenc( h, h->fdec, mb_x, mb_y );
  132.          x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
  133. @@ -400,14 +400,14 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  134.          pixel *pixy = h->fdec->plane[0] + 16*mb_y*stridey  + 16*mb_x;
  135.          pixel *pixuv = h->fdec->plane[1] + chroma_height*mb_y*strideuv + 16*mb_x;
  136.  
  137. -        if( mb_y & MB_INTERLACED )
  138. +        if( mb_y & MB_MBAFF_FIELD )
  139.          {
  140.              pixy -= 15*stridey;
  141.              pixuv -= (chroma_height-1)*strideuv;
  142.          }
  143.  
  144. -        int stride2y  = stridey << MB_INTERLACED;
  145. -        int stride2uv = strideuv << MB_INTERLACED;
  146. +        int stride2y  = stridey << MB_MBAFF_FIELD;
  147. +        int stride2uv = strideuv << MB_MBAFF_FIELD;
  148.          int qp = h->mb.qp[mb_xy];
  149.          int qpc = h->chroma_qp_table[qp];
  150.          int first_edge_only = (h->mb.partition[mb_xy] == D_16x16 && !h->mb.cbp[mb_xy] && !intra_cur) || qp <= qp_thresh;
  151. @@ -446,7 +446,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  152.  
  153.          if( h->mb.i_neighbour & MB_LEFT )
  154.          {
  155. -            if( b_interlaced && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
  156. +            if( b_mbaff && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
  157.              {
  158.                  int luma_qp[2];
  159.                  int chroma_qp[2];
  160. @@ -528,7 +528,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  161.  
  162.          if( h->mb.i_neighbour & MB_TOP )
  163.          {
  164. -            if( b_interlaced && !(mb_y&1) && !MB_INTERLACED && h->mb.field[h->mb.i_mb_top_xy] )
  165. +            if( b_mbaff && !(mb_y&1) && !MB_INTERLACED && h->mb.field[h->mb.i_mb_top_xy] )
  166.              {
  167.                  int mbn_xy = mb_xy - 2 * h->mb.i_mb_stride;
  168.  
  169. @@ -567,7 +567,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
  170.                      RESET_EFFECTIVE_QP(h->mb.i_mb_top_xy);
  171.                  }
  172.  
  173. -                if( (!b_interlaced || (!MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy])) && intra_deblock )
  174. +                if( (!b_mbaff || (!MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy])) && intra_deblock && !SLICE_FIELD )
  175.                  {
  176.                      FILTER( _intra, 1, 0, qp_top, qpc_top );
  177.                  }
  178. diff --git a/common/frame.h b/common/frame.h
  179. index f95ffa9..e5fdbb1 100644
  180. --- a/common/frame.h
  181. +++ b/common/frame.h
  182. @@ -136,6 +136,7 @@ typedef struct x264_frame
  183.      float   f_weighted_cost_delta[X264_BFRAME_MAX+2];
  184.      uint32_t i_pixel_sum[3];
  185.      uint64_t i_pixel_ssd[3];
  186. +    int     b_ref_opp_field; /* set to one if field references field of opposite parity */
  187.  
  188.      /* hrd */
  189.      x264_hrd_t hrd_timing;
  190. diff --git a/common/macroblock.c b/common/macroblock.c
  191. index 8494bfe..4c9cbc7 100644
  192. --- a/common/macroblock.c
  193. +++ b/common/macroblock.c
  194. @@ -53,8 +53,8 @@ static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int h
  195.      {
  196.          int v_shift = CHROMA_V_SHIFT;
  197.          // Chroma in 4:2:0 is offset if MCing from a field of opposite parity
  198. -        if( v_shift & MB_INTERLACED & i_ref )
  199. -            mvy += (h->mb.i_mb_y & 1)*4 - 2;
  200. +        if( v_shift & MB_INTERLACED )
  201. +            mvy += h->mb.i_mvy_offset[0][i_ref];
  202.  
  203.          int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
  204.          height = 4*height >> v_shift;
  205. @@ -91,8 +91,8 @@ static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int h
  206.      else
  207.      {
  208.          int v_shift = CHROMA_V_SHIFT;
  209. -        if( v_shift & MB_INTERLACED & i_ref )
  210. -            mvy += (h->mb.i_mb_y & 1)*4 - 2;
  211. +        if( v_shift & MB_INTERLACED )
  212. +            mvy += h->mb.i_mvy_offset[1][i_ref];
  213.  
  214.          int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
  215.          h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
  216. @@ -136,10 +136,11 @@ static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int
  217.      else
  218.      {
  219.          int v_shift = CHROMA_V_SHIFT;
  220. -        if( v_shift & MB_INTERLACED & i_ref0 )
  221. -            mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
  222. -        if( v_shift & MB_INTERLACED & i_ref1 )
  223. -            mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
  224. +        if( v_shift & MB_INTERLACED )
  225. +        {
  226. +            mvy0 += h->mb.i_mvy_offset[0][i_ref0];
  227. +            mvy1 += h->mb.i_mvy_offset[1][i_ref1];
  228. +        }
  229.  
  230.          h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
  231.                           mvx0, 2*mvy0>>v_shift, 2*width, 4*height>>v_shift );
  232. @@ -254,7 +255,7 @@ int x264_macroblock_cache_allocate( x264_t *h )
  233.      h->mb.i_b8_stride = h->mb.i_mb_width * 2;
  234.      h->mb.i_b4_stride = h->mb.i_mb_width * 4;
  235.  
  236. -    h->mb.b_interlaced = PARAM_INTERLACED;
  237. +    h->mb.b_interlaced = PARAM_INTERLACED || PARAM_FIELD_ENCODE;
  238.  
  239.      PREALLOC_INIT
  240.  
  241. @@ -331,9 +332,12 @@ int x264_macroblock_cache_allocate( x264_t *h )
  242.  
  243.      for( int i = 0; i < 2; i++ )
  244.      {
  245. -        int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
  246. +        // FIXME this is probably wrong
  247. +        int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference>>PARAM_FIELD_ENCODE) ) << PARAM_INTERLACED;
  248.          if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
  249.              i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
  250. +        if( PARAM_FIELD_ENCODE )
  251. +            i_refs = X264_MIN(X264_REF_MAX, i_refs<<1);
  252.  
  253.          for( int j = !i; j < i_refs; j++ )
  254.          {
  255. @@ -487,6 +491,21 @@ void x264_macroblock_slice_init( x264_t *h )
  256.              h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
  257.          }
  258.  
  259. +    int cur_bottom = (h->fdec->i_frame & 1) ^ !h->param.b_tff;
  260. +    for( int i = 0; i < h->i_ref[0]; i++ )
  261. +    {
  262. +        int ref_bottom = (h->fref[0][i]->i_frame & 1) ^ !h->param.b_tff;
  263. +        h->mb.i_mvy_offset[0][i] = SLICE_MBAFF ? i&1 ? (h->mb.i_mb_y & 1)*4 - 2 : 0
  264. +                                   : cur_bottom && !ref_bottom ? +2 : !cur_bottom && ref_bottom ? -2 : 0;
  265. +    }
  266. +    if( h->sh.i_type == SLICE_TYPE_B )
  267. +        for( int i = 0; i < h->i_ref[1]; i++ )
  268. +        {
  269. +            int ref_bottom = (h->fref[1][i]->i_frame & 1) ^ !h->param.b_tff;
  270. +            h->mb.i_mvy_offset[1][i] = SLICE_MBAFF ? i&1 ? (h->mb.i_mb_y & 1)*4 - 2 : 0
  271. +                                       : cur_bottom && !ref_bottom ? +2 : !cur_bottom && ref_bottom ? -2 : 0;
  272. +        }
  273. +
  274.      h->mb.i_neighbour4[6] =
  275.      h->mb.i_neighbour4[9] =
  276.      h->mb.i_neighbour4[12] =
  277. @@ -1305,8 +1324,8 @@ static void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int m
  278.      /* load skip */
  279.      if( h->sh.i_type == SLICE_TYPE_B )
  280.      {
  281. -        h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
  282. -        h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
  283. +        h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_MBAFF_FIELD][MB_MBAFF_FIELD&(mb_y&1)];
  284. +        h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_MBAFF_FIELD][MB_MBAFF_FIELD&(mb_y&1)];
  285.          if( h->param.b_cabac )
  286.          {
  287.              uint8_t skipbp;
  288. diff --git a/common/mvpred.c b/common/mvpred.c
  289. index 745c8ea..b15907a 100644
  290. --- a/common/mvpred.c
  291. +++ b/common/mvpred.c
  292. @@ -187,8 +187,8 @@ static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
  293.      int mb_xy = h->mb.i_mb_xy;
  294.      int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] };
  295.      int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] };
  296. -    int preshift = MB_INTERLACED;
  297. -    int postshift = MB_INTERLACED;
  298. +    int preshift = MB_MBAFF_FIELD;
  299. +    int postshift = MB_MBAFF_FIELD;
  300.      int offset = 1;
  301.      int yshift = 1;
  302.      h->mb.i_partition = partition_col[0];
  303. @@ -257,7 +257,7 @@ static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
  304.  
  305.          int i_part_8x8 = i_mb_8x8 + x8 + (ypart>>1) * h->mb.i_b8_stride;
  306.          int i_ref1_ref = h->fref[1][0]->ref[0][i_part_8x8];
  307. -        int i_ref = (map_col_to_list0(i_ref1_ref>>preshift) << postshift) + (offset&i_ref1_ref&MB_INTERLACED);
  308. +        int i_ref = (map_col_to_list0(i_ref1_ref>>preshift) << postshift) + (offset&i_ref1_ref&MB_MBAFF_FIELD);
  309.  
  310.          if( i_ref >= 0 )
  311.          {
  312. diff --git a/common/set.h b/common/set.h
  313. index 6fb7aba..da908ba 100644
  314. --- a/common/set.h
  315. +++ b/common/set.h
  316. @@ -157,6 +157,7 @@ typedef struct
  317.  
  318.      int b_qpprime_y_zero_transform_bypass;
  319.      int i_chroma_format_idc;
  320. +    int b_half_height;
  321.  
  322.  } x264_sps_t;
  323.  
  324. diff --git a/encoder/analyse.c b/encoder/analyse.c
  325. index 9131d3d..cd58250 100644
  326. --- a/encoder/analyse.c
  327. +++ b/encoder/analyse.c
  328. @@ -3986,8 +3986,8 @@ static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
  329.              int ref = h->mb.cache.ref[l][x264_scan8[0]];
  330.              if( ref < 0 )
  331.                  continue;
  332. -            completed = h->fref[l][ ref >> MB_INTERLACED ]->orig->i_lines_completed;
  333. -            if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - MB_INTERLACED)) + h->mb.i_mb_y*16 > completed )
  334. +            completed = h->fref[l][ ref >> MB_MBAFF_FIELD ]->orig->i_lines_completed;
  335. +            if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - MB_MBAFF_FIELD)) + h->mb.i_mb_y*16 > completed )
  336.              {
  337.                  x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
  338.                  x264_log( h, X264_LOG_DEBUG, "mb type: %d \n", h->mb.i_type);
  339. diff --git a/encoder/encoder.c b/encoder/encoder.c
  340. index 1ea341b..559e9d9 100644
  341. --- a/encoder/encoder.c
  342. +++ b/encoder/encoder.c
  343. @@ -87,11 +87,17 @@ static void x264_frame_dump( x264_t *h )
  344.          x264_threadpool_wait_all( h );
  345.  
  346.      /* Write the frame in display order */
  347. -    int frame_size = FRAME_SIZE( h->param.i_height * h->param.i_width * sizeof(pixel) );
  348. -    fseek( f, (uint64_t)h->fdec->i_frame * frame_size, SEEK_SET );
  349. +    int frame_size = FRAME_SIZE( h->param.i_height * (1+PARAM_FIELD_ENCODE) * h->param.i_width * sizeof(pixel) );
  350. +    fseek( f, (uint64_t)(h->fdec->i_frame >> PARAM_FIELD_ENCODE) * frame_size, SEEK_SET );
  351. +    if( PARAM_FIELD_ENCODE && h->sh.b_bottom_field )
  352. +        fseek( f, (uint64_t)h->param.i_width, SEEK_CUR );
  353.      for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
  354.          for( int y = 0; y < h->param.i_height; y++ )
  355. +        {
  356.              fwrite( &h->fdec->plane[p][y*h->fdec->i_stride[p]], sizeof(pixel), h->param.i_width, f );
  357. +            if( PARAM_FIELD_ENCODE )
  358. +                fseek( f, (uint64_t)h->param.i_width, SEEK_CUR );
  359. +        }
  360.      if( !CHROMA444 )
  361.      {
  362.          int cw = h->param.i_width>>1;
  363. @@ -101,8 +107,26 @@ static void x264_frame_dump( x264_t *h )
  364.          {
  365.              pixel *planev = planeu + cw*ch + 16;
  366.              h->mc.plane_copy_deinterleave( planeu, cw, planev, cw, h->fdec->plane[1], h->fdec->i_stride[1], cw, ch );
  367. -            fwrite( planeu, 1, cw*ch*sizeof(pixel), f );
  368. -            fwrite( planev, 1, cw*ch*sizeof(pixel), f );
  369. +            if( PARAM_FIELD_ENCODE )
  370. +            {
  371. +                if( h->sh.b_bottom_field )
  372. +                    fseek( f, (uint64_t)(-(h->param.i_width>>1)), SEEK_CUR );
  373. +                for( int y = 0; y < ch; y++ )
  374. +                {
  375. +                    fwrite( &planeu[y*cw], sizeof(pixel), cw, f );
  376. +                    fseek( f, (uint64_t)cw, SEEK_CUR );
  377. +                }
  378. +                for( int y = 0; y < ch; y++ )
  379. +                {
  380. +                    fwrite( &planev[y*cw], sizeof(pixel), cw, f );
  381. +                    fseek( f, (uint64_t)cw, SEEK_CUR );
  382. +                }
  383. +            }
  384. +            else
  385. +            {
  386. +                fwrite( planeu, 1, cw*ch*sizeof(pixel), f );
  387. +                fwrite( planev, 1, cw*ch*sizeof(pixel), f );
  388. +            }
  389.              x264_free( planeu );
  390.          }
  391.      }
  392. @@ -127,8 +151,8 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
  393.      sh->i_frame_num = i_frame;
  394.  
  395.      sh->b_mbaff = PARAM_INTERLACED;
  396. -    sh->b_field_pic = 0;    /* no field support for now */
  397. -    sh->b_bottom_field = 0; /* not yet used */
  398. +    sh->b_field_pic = PARAM_FIELD_ENCODE;
  399. +    sh->b_bottom_field = h->fenc->i_pic_struct == PIC_STRUCT_BOTTOM;
  400.  
  401.      sh->i_idr_pic_id = i_idr_pic_id;
  402.  
  403. @@ -173,13 +197,23 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
  404.      {
  405.          if( sh->b_ref_pic_list_reordering[list] )
  406.          {
  407. -            int pred_frame_num = i_frame;
  408. +            int pred_frame_num, pic_num;
  409. +            pred_frame_num = PARAM_FIELD_ENCODE ? 2 * sh->i_frame_num + 1 : i_frame;
  410. +
  411.              for( int i = 0; i < h->i_ref[list]; i++ )
  412.              {
  413. -                int diff = h->fref[list][i]->i_frame_num - pred_frame_num;
  414. +                if( PARAM_FIELD_ENCODE )
  415. +                {
  416. +                    int same_parity = ((h->fenc->i_frame ^ h->fref[list][i]->i_frame) & 1) ^ 1;
  417. +                    pic_num = 2 * h->fref[list][i]->i_frame_num + same_parity;
  418. +                }
  419. +                else
  420. +                    pic_num = h->fref[list][i]->i_frame_num;
  421. +
  422. +                int diff = pic_num - pred_frame_num;
  423.                  sh->ref_pic_list_order[list][i].idc = ( diff > 0 );
  424.                  sh->ref_pic_list_order[list][i].arg = (abs(diff) - 1) & ((1 << sps->i_log2_max_frame_num) - 1);
  425. -                pred_frame_num = h->fref[list][i]->i_frame_num;
  426. +                pred_frame_num = pic_num;
  427.              }
  428.          }
  429.      }
  430. @@ -405,6 +439,19 @@ static void x264_encoder_thread_init( x264_t *h )
  431.  }
  432.  #endif
  433.  
  434. +static int x264_diff_pic_num( x264_t *h, x264_frame_t *src, x264_frame_t *ref )
  435. +{
  436. +    if( PARAM_FIELD_ENCODE )
  437. +    {
  438. +        int cur_pic_num = 2 * src->i_frame_num + 1;
  439. +        int same_parity = ((src->i_frame ^ ref->i_frame) & 1) ^ 1;
  440. +        int ref_pic_num = 2 * ref->i_frame_num + same_parity;
  441. +        return cur_pic_num - ref_pic_num;
  442. +    }
  443. +    else
  444. +        return src->i_frame_num - ref->i_frame_num;
  445. +}
  446. +
  447.  /****************************************************************************
  448.   *
  449.   ****************************************************************************
  450. @@ -454,8 +501,14 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  451.  
  452.  #if HAVE_INTERLACED
  453.      h->param.b_interlaced = !!PARAM_INTERLACED;
  454. +    h->param.b_field_encode = !!PARAM_FIELD_ENCODE;
  455. +    if( h->param.b_interlaced && h->param.b_field_encode )
  456. +    {
  457. +        x264_log( h, X264_LOG_ERROR, "MBAFF and field encode is invalid\n" );
  458. +        return -1;
  459. +    }
  460.  #else
  461. -    if( h->param.b_interlaced )
  462. +    if( h->param.b_interlaced || h->param.b_field_encode )
  463.      {
  464.          x264_log( h, X264_LOG_ERROR, "not compiled with interlaced support\n" );
  465.          return -1;
  466. @@ -584,6 +637,17 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  467.      }
  468.  
  469.      h->param.i_frame_packing = x264_clip3( h->param.i_frame_packing, -1, 5 );
  470. +    if( h->param.i_frame_packing == 5 && PARAM_FIELD_ENCODE )
  471. +    {
  472. +        x264_log( h, X264_LOG_ERROR, "Frame packing 5 not supported with PAFF\n" );
  473. +        return -1;
  474. +    }
  475. +
  476. +    if( PARAM_FIELD_ENCODE && h->param.i_bframe > 0 )
  477. +    {
  478. +        x264_log( h, X264_LOG_WARNING, "B-frames not supported with PAFF\n" );
  479. +        h->param.i_bframe = 0;
  480. +    }
  481.  
  482.      /* Detect default ffmpeg settings and terminate with an error. */
  483.      if( b_open )
  484. @@ -937,8 +1001,19 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  485.              h->param.b_pic_struct = 1;
  486.      }
  487.  
  488. -    h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, X264_REF_MAX );
  489. -    h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, X264_REF_MAX );
  490. +    if( PARAM_FIELD_ENCODE )
  491. +    {
  492. +        if( !(h->param.b_intra_refresh && h->param.i_frame_reference == 1) )
  493. +        {
  494. +            h->param.i_frame_reference <<= 1;
  495. +            h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, X264_REF_MAX );
  496. +        }
  497. +    }
  498. +    else
  499. +    {
  500. +        h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, X264_REF_MAX >> 1 );
  501. +        h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, X264_REF_MAX >> 1 );
  502. +    }
  503.      if( h->param.i_scenecut_threshold < 0 )
  504.          h->param.i_scenecut_threshold = 0;
  505.      h->param.analyse.i_direct_mv_pred = x264_clip3( h->param.analyse.i_direct_mv_pred, X264_DIRECT_PRED_NONE, X264_DIRECT_PRED_AUTO );
  506. @@ -947,7 +1022,19 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  507.          x264_log( h, X264_LOG_WARNING, "subme=0 + direct=temporal is not supported\n" );
  508.          h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
  509.      }
  510. +    if( PARAM_FIELD_ENCODE )
  511. +    {
  512. +        // FIXME: should this be part of the API for field encoding
  513. +        if( 1 < h->param.i_keyint_max && h->param.i_keyint_max < X264_KEYINT_MAX_INFINITE )
  514. +            h->param.i_keyint_max <<= 1;
  515. +        h->param.i_keyint_max = x264_clip3( h->param.i_keyint_max, 1, X264_KEYINT_MAX_INFINITE );
  516. +        if( 0 < h->param.i_bframe && h->param.i_bframe < X264_KEYINT_MAX_INFINITE )
  517. +            h->param.i_bframe <<= 1;
  518. +    }
  519. +
  520.      h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_MIN( X264_BFRAME_MAX, h->param.i_keyint_max-1 ) );
  521. +    if( PARAM_FIELD_ENCODE )
  522. +        h->param.i_bframe &= ~1;
  523.      h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
  524.      if( h->param.i_bframe <= 1 )
  525.          h->param.i_bframe_pyramid = X264_B_PYRAMID_NONE;
  526. @@ -978,10 +1065,16 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  527.      }
  528.      if( !h->param.i_fps_num || !h->param.i_fps_den )
  529.      {
  530. -        h->param.i_fps_num = 25;
  531. +        h->param.i_fps_num = 25*(1<<PARAM_FIELD_ENCODE);
  532.          h->param.i_fps_den = 1;
  533.      }
  534.      float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
  535. +    if( PARAM_FIELD_ENCODE )
  536. +    {
  537. +        if( h->param.i_keyint_min != X264_KEYINT_MIN_AUTO )
  538. +            if( 0 < h->param.i_keyint_min && h->param.i_keyint_min < X264_KEYINT_MAX_INFINITE )
  539. +                h->param.i_keyint_min <<= 1;
  540. +    }
  541.      if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO )
  542.          h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, fps );
  543.      h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
  544. @@ -1138,9 +1231,9 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  545.              }
  546.          }
  547.          if( h->param.analyse.i_mv_range <= 0 )
  548. -            h->param.analyse.i_mv_range = l->mv_range >> PARAM_INTERLACED;
  549. +            h->param.analyse.i_mv_range = l->mv_range >> (PARAM_INTERLACED|PARAM_FIELD_ENCODE);
  550.          else
  551. -            h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> PARAM_INTERLACED);
  552. +            h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> (PARAM_INTERLACED|PARAM_FIELD_ENCODE));
  553.      }
  554.  
  555.      h->param.analyse.i_weighted_pred = x264_clip3( h->param.analyse.i_weighted_pred, X264_WEIGHTP_NONE, X264_WEIGHTP_SMART );
  556. @@ -1174,7 +1267,7 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  557.      }
  558.      h->param.i_lookahead_threads = x264_clip3( h->param.i_lookahead_threads, 1, X264_MIN( max_sliced_threads, X264_LOOKAHEAD_THREAD_MAX ) );
  559.  
  560. -    if( PARAM_INTERLACED )
  561. +    if( PARAM_INTERLACED || PARAM_FIELD_ENCODE )
  562.      {
  563.          if( h->param.analyse.i_me_method >= X264_ME_ESA )
  564.          {
  565. @@ -1249,6 +1342,7 @@ static int x264_validate_parameters( x264_t *h, int b_open )
  566.      BOOLIFY( b_deterministic );
  567.      BOOLIFY( b_sliced_threads );
  568.      BOOLIFY( b_interlaced );
  569. +    BOOLIFY( b_field_encode );
  570.      BOOLIFY( b_intra_refresh );
  571.      BOOLIFY( b_aud );
  572.      BOOLIFY( b_repeat_headers );
  573. @@ -1462,9 +1556,10 @@ x264_t *x264_encoder_open( x264_param_t *param )
  574.      h->frames.i_delay += h->param.i_sync_lookahead;
  575.      h->frames.i_delay += h->param.b_vfr_input;
  576.      h->frames.i_bframe_delay = h->param.i_bframe ? (h->param.i_bframe_pyramid ? 2 : 1) : 0;
  577. +    // FIXME what's the bframe delay for PAFF?
  578.  
  579.      h->frames.i_max_ref0 = h->param.i_frame_reference;
  580. -    h->frames.i_max_ref1 = X264_MIN( h->sps->vui.i_num_reorder_frames, h->param.i_frame_reference );
  581. +    h->frames.i_max_ref1 = X264_MIN( h->sps->vui.i_num_reorder_frames<<PARAM_FIELD_ENCODE, h->param.i_frame_reference );
  582.      h->frames.i_max_dpb  = h->sps->vui.i_max_dec_frame_buffering;
  583.      h->frames.b_have_lowres = !h->param.rc.b_stat_read
  584.          && ( h->param.rc.i_rc_method == X264_RC_ABR
  585. @@ -1504,7 +1599,7 @@ x264_t *x264_encoder_open( x264_param_t *param )
  586.      x264_pixel_init( h->param.cpu, &h->pixf );
  587.      x264_dct_init( h->param.cpu, &h->dctf );
  588.      x264_zigzag_init( h->param.cpu, &h->zigzagf_progressive, &h->zigzagf_interlaced );
  589. -    memcpy( &h->zigzagf, PARAM_INTERLACED ? &h->zigzagf_interlaced : &h->zigzagf_progressive, sizeof(h->zigzagf) );
  590. +    memcpy( &h->zigzagf, PARAM_INTERLACED || PARAM_FIELD_ENCODE ? &h->zigzagf_interlaced : &h->zigzagf_progressive, sizeof(h->zigzagf) );
  591.      x264_mc_init( h->param.cpu, &h->mc, h->param.b_cpu_independent );
  592.      x264_quant_init( h, h->param.cpu, &h->quantf );
  593.      x264_deblock_init( h->param.cpu, &h->loopf, PARAM_INTERLACED );
  594. @@ -1990,6 +2085,19 @@ int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
  595.      return frame_size;
  596.  }
  597.  
  598. +static inline int x264_reference_distance( x264_t *h, x264_frame_t *frame )
  599. +{
  600. +    if( h->param.i_frame_packing == 5 )
  601. +        return (abs((h->fenc->i_frame^2) - (frame->i_frame^2))) +
  602. +                  ((h->fenc->i_frame&1) != (frame->i_frame&1));
  603. +    else if( PARAM_FIELD_ENCODE )
  604. +    {
  605. +        return ((h->fenc->i_frame - frame->i_frame - 1) & ~1) + ((h->fenc->i_frame - frame->i_frame) && !!frame->i_frame);
  606. +    }
  607. +    else
  608. +        return abs(h->fenc->i_frame - frame->i_frame);
  609. +}
  610. +
  611.  /* Check to see whether we have chosen a reference list ordering different
  612.   * from the standard's default. */
  613.  static inline void x264_reference_check_reorder( x264_t *h )
  614. @@ -2003,17 +2111,49 @@ static inline void x264_reference_check_reorder( x264_t *h )
  615.              return;
  616.          }
  617.      for( int list = 0; list <= (h->sh.i_type == SLICE_TYPE_B); list++ )
  618. -        for( int i = 0; i < h->i_ref[list] - 1; i++ )
  619. +    {
  620. +        if( PARAM_FIELD_ENCODE )
  621.          {
  622. -            int framenum_diff = h->fref[list][i+1]->i_frame_num - h->fref[list][i]->i_frame_num;
  623. -            int poc_diff = h->fref[list][i+1]->i_poc - h->fref[list][i]->i_poc;
  624. -            /* P and B-frames use different default orders. */
  625. -            if( h->sh.i_type == SLICE_TYPE_P ? framenum_diff > 0 : list == 1 ? poc_diff < 0 : poc_diff > 0 )
  626. +            /* check the first field in the reference list has the same parity
  627. +             * as the field to be encoded */
  628. +            if( h->i_ref[list] > 0 && ((h->fenc->i_frame ^ h->fref[list][0]->i_frame) & 1))
  629.              {
  630. +                //printf("\n here %i \n", h->fenc->i_frame);
  631.                  h->b_ref_reorder[list] = 1;
  632.                  return;
  633.              }
  634. +
  635. +            /* FIXME: is there a cleaner way of doing this */
  636. +            for( int i = 0; i < h->i_ref[list] - 1; i++ )
  637. +            {
  638. +                int dist = x264_reference_distance( h, h->fref[list][i] ) > x264_reference_distance( h, h->fref[list][i+1] );
  639. +                int poc_diff = h->fref[list][i+1]->i_poc - h->fref[list][i]->i_poc;
  640. +                //printf("\n %i %i %i %i %i \n", h->fenc->i_frame, i, dist, h->fref[list][i]->i_frame, h->fref[list][i+1]->i_frame );
  641. +                /* P and B-frames use different default orders. */
  642. +                // FIXME b-frames
  643. +                if( h->sh.i_type == SLICE_TYPE_P ? dist : list == 1 ? poc_diff < 0 : poc_diff > 0 )
  644. +                {
  645. +                    h->b_ref_reorder[list] = 1;
  646. +                    return;
  647. +                }
  648. +            }
  649. +
  650. +        }
  651. +        else
  652. +        {
  653. +            for( int i = 0; i < h->i_ref[list] - 1; i++ )
  654. +            {
  655. +                int picnum_diff = x264_diff_pic_num( h, h->fref[list][i+1], h->fref[list][i] );
  656. +                int poc_diff = h->fref[list][i+1]->i_poc - h->fref[list][i]->i_poc;
  657. +                /* P and B-frames use different default orders. */
  658. +                if( h->sh.i_type == SLICE_TYPE_P ? picnum_diff > 0 : list == 1 ? poc_diff < 0 : poc_diff > 0 )
  659. +                {
  660. +                    h->b_ref_reorder[list] = 1;
  661. +                    return;
  662. +                }
  663. +            }
  664.          }
  665. +    }
  666.  }
  667.  
  668.  /* return -1 on failure, else return the index of the new reference frame */
  669. @@ -2140,15 +2280,6 @@ static void x264_weighted_pred_init( x264_t *h )
  670.      h->sh.weight[0][2].i_denom = h->sh.weight[0][1].i_denom;
  671.  }
  672.  
  673. -static inline int x264_reference_distance( x264_t *h, x264_frame_t *frame )
  674. -{
  675. -    if( h->param.i_frame_packing == 5 )
  676. -        return abs((h->fenc->i_frame&~1) - (frame->i_frame&~1)) +
  677. -                  ((h->fenc->i_frame&1) != (frame->i_frame&1));
  678. -    else
  679. -        return abs(h->fenc->i_frame - frame->i_frame);
  680. -}
  681. -
  682.  static inline void x264_reference_build_list( x264_t *h, int i_poc )
  683.  {
  684.      int b_ok;
  685. @@ -2188,13 +2319,13 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc )
  686.  
  687.          for( int i = h->i_ref[0]-1; i >= h->i_ref[0] - h->sh.i_mmco_remove_from_end; i-- )
  688.          {
  689. -            int diff = h->i_frame_num - h->fref[0][i]->i_frame_num;
  690. +            int diff = x264_diff_pic_num( h, h->fdec, h->fref[0][i] );
  691.              h->sh.mmco[h->sh.i_mmco_command_count].i_poc = h->fref[0][i]->i_poc;
  692.              h->sh.mmco[h->sh.i_mmco_command_count++].i_difference_of_pic_nums = diff;
  693.          }
  694.      }
  695.  
  696. -    /* Order reference lists by distance from the current frame. */
  697. +    /* Order reference lists by distance from the current picture. */
  698.      for( int list = 0; list < 2; list++ )
  699.      {
  700.          h->fref_nearest[list] = h->fref[list][0];
  701. @@ -2206,6 +2337,12 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc )
  702.                  if( list ? h->fref[list][i+1]->i_poc < h->fref_nearest[list]->i_poc
  703.                           : h->fref[list][i+1]->i_poc > h->fref_nearest[list]->i_poc )
  704.                      h->fref_nearest[list] = h->fref[list][i+1];
  705. +
  706. +                printf("\n frame %i refi %i refi+1 %i dist %i %i", h->fenc->i_frame, h->fref[list][i]->i_frame,
  707. +                                                                   h->fref[list][i+1]->i_frame, x264_reference_distance( h, h->fref[list][i] ),
  708. +                                                                   x264_reference_distance( h, h->fref[list][i+1] ) );
  709. +
  710. +
  711.                  if( x264_reference_distance( h, h->fref[list][i] ) > x264_reference_distance( h, h->fref[list][i+1] ) )
  712.                  {
  713.                      XCHG( x264_frame_t*, h->fref[list][i], h->fref[list][i+1] );
  714. @@ -2224,7 +2361,7 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc )
  715.  
  716.      /* For Blu-ray compliance, don't reference frames outside of the minigop. */
  717.      if( IS_X264_TYPE_B( h->fenc->i_type ) && h->param.b_bluray_compat )
  718. -        h->i_ref[0] = X264_MIN( h->i_ref[0], IS_X264_TYPE_B( h->fref[0][0]->i_type ) + 1 );
  719. +        h->i_ref[0] = X264_MIN( h->i_ref[0], (IS_X264_TYPE_B( h->fref[0][0]->i_type ) + 1)<<PARAM_FIELD_ENCODE );
  720.  
  721.      /* add duplicates */
  722.      if( h->fenc->i_type == X264_TYPE_P )
  723. @@ -2409,8 +2546,13 @@ static inline int x264_reference_update( x264_t *h )
  724.  
  725.      /* move frame in the buffer */
  726.      x264_frame_push( h->frames.reference, h->fdec );
  727. -    if( h->frames.reference[h->sps->i_num_ref_frames] )
  728. +    if( h->frames.reference[h->sps->i_num_ref_frames<<PARAM_FIELD_ENCODE] )
  729. +    {
  730.          x264_frame_push_unused( h, x264_frame_shift( h->frames.reference ) );
  731. +        if( PARAM_FIELD_ENCODE )
  732. +            x264_frame_push_unused( h, x264_frame_shift( h->frames.reference ) ); /* 2nd field */
  733. +    }
  734. +
  735.      h->fdec = x264_frame_pop_unused( h, 1 );
  736.      if( !h->fdec )
  737.          return -1;
  738. @@ -2432,8 +2574,8 @@ static inline void x264_reference_hierarchy_reset( x264_t *h )
  739.  
  740.      /* look for delay frames -- chain must only contain frames that are disposable */
  741.      for( int i = 0; h->frames.current[i] && IS_DISPOSABLE( h->frames.current[i]->i_type ); i++ )
  742. -        b_hasdelayframe |= h->frames.current[i]->i_coded
  743. -                        != h->frames.current[i]->i_frame + h->sps->vui.i_num_reorder_frames;
  744. +        b_hasdelayframe |= (h->frames.current[i]->i_coded>>PARAM_FIELD_ENCODE)
  745. +                        != (h->frames.current[i]->i_frame>>PARAM_FIELD_ENCODE) + h->sps->vui.i_num_reorder_frames;
  746.  
  747.      /* This function must handle b-pyramid and clear frames for open-gop */
  748.      if( h->param.i_bframe_pyramid != X264_B_PYRAMID_STRICT && !b_hasdelayframe && h->frames.i_poc_last_open_gop == -1 )
  749. @@ -2448,7 +2590,7 @@ static inline void x264_reference_hierarchy_reset( x264_t *h )
  750.              || ( h->frames.reference[ref]->i_poc < h->frames.i_poc_last_open_gop
  751.              && h->sh.i_type != SLICE_TYPE_B ) )
  752.          {
  753. -            int diff = h->i_frame_num - h->frames.reference[ref]->i_frame_num;
  754. +            int diff = x264_diff_pic_num( h, h->fdec, h->frames.reference[ref] );
  755.              h->sh.mmco[h->sh.i_mmco_command_count].i_difference_of_pic_nums = diff;
  756.              h->sh.mmco[h->sh.i_mmco_command_count++].i_poc = h->frames.reference[ref]->i_poc;
  757.              x264_frame_push_unused( h, x264_frame_shift( &h->frames.reference[ref] ) );
  758. @@ -2459,7 +2601,7 @@ static inline void x264_reference_hierarchy_reset( x264_t *h )
  759.  
  760.      /* Prepare room in the dpb for the delayed display time of the later b-frame's */
  761.      if( h->param.i_bframe_pyramid )
  762. -        h->sh.i_mmco_remove_from_end = X264_MAX( ref + 2 - h->frames.i_max_dpb, 0 );
  763. +        h->sh.i_mmco_remove_from_end = X264_MAX( (ref>>PARAM_FIELD_ENCODE) + 2 - h->frames.i_max_dpb, 0 ) << PARAM_FIELD_ENCODE;
  764.  }
  765.  
  766.  static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
  767. @@ -2508,8 +2650,6 @@ static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
  768.          h->sh_backup = h->sh;
  769.      }
  770.  
  771. -    h->fdec->i_frame_num = h->sh.i_frame_num;
  772. -
  773.      if( h->sps->i_poc_type == 0 )
  774.      {
  775.          h->sh.i_poc = h->fdec->i_poc;
  776. @@ -3174,6 +3314,7 @@ int x264_encoder_invalidate_reference( x264_t *h, int64_t pts )
  777.   *       P      4   2*6
  778.   *       B      5   2*4
  779.   *       B      6   2*5
  780. + *
  781.   ****************************************************************************/
  782.  int     x264_encoder_encode( x264_t *h,
  783.                               x264_nal_t **pp_nal, int *pi_nal,
  784. @@ -3245,13 +3386,16 @@ int     x264_encoder_encode( x264_t *h,
  785.          {
  786.  #if HAVE_INTERLACED
  787.              int b_interlaced = fenc->param ? fenc->param->b_interlaced : h->param.b_interlaced;
  788. +            int b_field_encode = fenc->param ? fenc->param->b_field_encode : h->param.b_field_encode;
  789.  #else
  790.              int b_interlaced = 0;
  791. +            int b_field_encode = 0;
  792.  #endif
  793. -            if( b_interlaced )
  794. +            if( b_interlaced || b_field_encode )
  795.              {
  796.                  int b_tff = fenc->param ? fenc->param->b_tff : h->param.b_tff;
  797. -                fenc->i_pic_struct = b_tff ? PIC_STRUCT_TOP_BOTTOM : PIC_STRUCT_BOTTOM_TOP;
  798. +                fenc->i_pic_struct = b_interlaced ? b_tff ? PIC_STRUCT_TOP_BOTTOM : PIC_STRUCT_BOTTOM_TOP
  799. +                                     : (fenc->i_frame ^ !b_tff) & 1 ? PIC_STRUCT_BOTTOM : PIC_STRUCT_TOP;
  800.              }
  801.              else
  802.                  fenc->i_pic_struct = PIC_STRUCT_PROGRESSIVE;
  803. @@ -3357,7 +3501,9 @@ int     x264_encoder_encode( x264_t *h,
  804.      h->b_ref_reorder[0] =
  805.      h->b_ref_reorder[1] = 0;
  806.      h->fdec->i_poc =
  807. -    h->fenc->i_poc = 2 * ( h->fenc->i_frame - X264_MAX( h->frames.i_last_idr, 0 ) );
  808. +    h->fenc->i_poc = (2-PARAM_FIELD_ENCODE) * ( h->fenc->i_frame - X264_MAX( h->frames.i_last_idr, 0 ) );
  809. +    h->fdec->i_frame = h->fenc->i_frame;
  810. +    h->fdec->i_frame_num = h->i_frame_num;
  811.  
  812.      /* ------------------- Setup frame context ----------------------------- */
  813.      /* 5: Init data dependent of frame type */
  814. @@ -3384,15 +3530,19 @@ int     x264_encoder_encode( x264_t *h,
  815.          i_nal_type    = NAL_SLICE;
  816.          i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
  817.          h->sh.i_type = SLICE_TYPE_P;
  818. -        x264_reference_hierarchy_reset( h );
  819. -        h->frames.i_poc_last_open_gop = -1;
  820. +        if( !(h->fenc->i_coded & PARAM_FIELD_ENCODE) ) // 1st field
  821. +        {
  822. +            x264_reference_hierarchy_reset( h );
  823. +            h->frames.i_poc_last_open_gop = -1;
  824. +        }
  825.      }
  826.      else if( h->fenc->i_type == X264_TYPE_BREF )
  827.      {
  828.          i_nal_type    = NAL_SLICE;
  829.          i_nal_ref_idc = h->param.i_bframe_pyramid == X264_B_PYRAMID_STRICT ? NAL_PRIORITY_LOW : NAL_PRIORITY_HIGH;
  830.          h->sh.i_type = SLICE_TYPE_B;
  831. -        x264_reference_hierarchy_reset( h );
  832. +        if( !PARAM_FIELD_ENCODE || !(h->fenc->i_coded & 1) ) // 1st field
  833. +            x264_reference_hierarchy_reset( h );
  834.      }
  835.      else    /* B frame */
  836.      {
  837. @@ -3402,7 +3552,6 @@ int     x264_encoder_encode( x264_t *h,
  838.      }
  839.  
  840.      h->fdec->i_type = h->fenc->i_type;
  841. -    h->fdec->i_frame = h->fenc->i_frame;
  842.      h->fenc->b_kept_as_ref =
  843.      h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE && h->param.i_keyint_max > 1;
  844.  
  845. @@ -3692,7 +3841,11 @@ int     x264_encoder_encode( x264_t *h,
  846.      x264_weighted_pred_init( h );
  847.  
  848.      if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
  849. -        h->i_frame_num++;
  850. +    {
  851. +        int second_field = h->fenc->i_frame & 1;
  852. +        if( !PARAM_FIELD_ENCODE || second_field )
  853. +            h->i_frame_num++;
  854. +    }
  855.  
  856.      /* Write frame */
  857.      h->i_threadslice_start = 0;
  858. diff --git a/encoder/lookahead.c b/encoder/lookahead.c
  859. index a6e861b..97a7b67 100644
  860. --- a/encoder/lookahead.c
  861. +++ b/encoder/lookahead.c
  862. @@ -58,8 +58,13 @@ static void x264_lookahead_shift( x264_sync_frame_list_t *dst, x264_sync_frame_l
  863.  
  864.  static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
  865.  {
  866. -    if( h->lookahead->last_nonb )
  867. +    if( PARAM_FIELD_ENCODE && h->lookahead->penultimate_nonb )
  868. +        x264_frame_push_unused( h, h->lookahead->penultimate_nonb );
  869. +
  870. +    if( !PARAM_FIELD_ENCODE && h->lookahead->last_nonb )
  871.          x264_frame_push_unused( h, h->lookahead->last_nonb );
  872. +    if( PARAM_FIELD_ENCODE )
  873. +        h->lookahead->penultimate_nonb = h->lookahead->last_nonb;
  874.      h->lookahead->last_nonb = new_nonb;
  875.      new_nonb->i_reference_count++;
  876.  }
  877. @@ -67,10 +72,12 @@ static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
  878.  #if HAVE_THREAD
  879.  static void x264_lookahead_slicetype_decide( x264_t *h )
  880.  {
  881. +    //printf("\n next %i \n", h->lookahead->next.list[0]->i_frame );
  882. +
  883.      x264_stack_align( x264_slicetype_decide, h );
  884.  
  885.      x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
  886. -    int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
  887. +    int shift_frames = h->lookahead->last_nonb->i_bframes + 1;
  888.  
  889.      x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  890.      while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
  891. @@ -80,6 +87,7 @@ static void x264_lookahead_slicetype_decide( x264_t *h )
  892.      x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
  893.      x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  894.  
  895. +    // FIXME this probably will be broken
  896.      /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  897.      if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
  898.          x264_stack_align( x264_slicetype_analyse, h, shift_frames );
  899. @@ -179,6 +187,8 @@ void x264_lookahead_delete( x264_t *h )
  900.      x264_sync_frame_list_delete( &h->lookahead->next );
  901.      if( h->lookahead->last_nonb )
  902.          x264_frame_push_unused( h, h->lookahead->last_nonb );
  903. +    if( h->lookahead->penultimate_nonb )
  904. +        x264_frame_push_unused( h, h->lookahead->penultimate_nonb );
  905.      x264_sync_frame_list_delete( &h->lookahead->ofbuf );
  906.      x264_free( h->lookahead );
  907.  }
  908. @@ -232,7 +242,7 @@ void x264_lookahead_get_frames( x264_t *h )
  909.  
  910.          x264_stack_align( x264_slicetype_decide, h );
  911.          x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
  912. -        int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
  913. +        int shift_frames = h->lookahead->last_nonb->i_bframes + 1;
  914.          x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
  915.  
  916.          /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  917. diff --git a/encoder/macroblock.c b/encoder/macroblock.c
  918. index 351650a..da53c0e 100644
  919. --- a/encoder/macroblock.c
  920. +++ b/encoder/macroblock.c
  921. @@ -649,6 +649,8 @@ static ALWAYS_INLINE void x264_macroblock_encode_internal( x264_t *h, int plane_
  922.              if( chroma )
  923.              {
  924.                  int v_shift = CHROMA_V_SHIFT;
  925. +                if( v_shift && PARAM_FIELD_ENCODE )
  926. +                    mvy += h->mb.i_mvy_offset[0][0];
  927.                  int height = 16 >> v_shift;
  928.  
  929.                  /* Special case for mv0, which is (of course) very common in P-skip mode. */
  930. @@ -1016,6 +1018,10 @@ static ALWAYS_INLINE int x264_macroblock_probe_skip_internal( x264_t *h, int b_b
  931.  
  932.          if( !b_bidir )
  933.          {
  934. +            int v_shift = CHROMA_V_SHIFT;
  935. +            if( v_shift && PARAM_FIELD_ENCODE )
  936. +                mvp[1] += h->mb.i_mvy_offset[0][0];
  937. +
  938.              /* Special case for mv0, which is (of course) very common in P-skip mode. */
  939.              if( M32( mvp ) )
  940.                  h->mc.mc_chroma( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], FDEC_STRIDE,
  941. diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
  942. index f71eb5f..161295e 100644
  943. --- a/encoder/ratecontrol.c
  944. +++ b/encoder/ratecontrol.c
  945. @@ -749,7 +749,7 @@ int x264_ratecontrol_new( x264_t *h )
  946.      if( h->param.i_fps_num > 0 && h->param.i_fps_den > 0 )
  947.          rc->fps = (float) h->param.i_fps_num / h->param.i_fps_den;
  948.      else
  949. -        rc->fps = 25.0;
  950. +        rc->fps = 25.0*(1+PARAM_FIELD_ENCODE);
  951.  
  952.      if( h->param.rc.b_mb_tree )
  953.      {
  954. diff --git a/encoder/set.c b/encoder/set.c
  955. index 1a40b71..652295e 100644
  956. --- a/encoder/set.c
  957. +++ b/encoder/set.c
  958. @@ -144,7 +144,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  959.      /* extra slot with pyramid so that we don't have to override the
  960.       * order of forgetting old pictures */
  961.      sps->vui.i_max_dec_frame_buffering =
  962. -    sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
  963. +    sps->i_num_ref_frames = X264_MIN(X264_REF_MAX >> 1, X264_MAX4(param->i_frame_reference>>param->b_field_encode, 1 + sps->vui.i_num_reorder_frames,
  964.                              param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
  965.      sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
  966.      if( param->i_keyint_max == 1 )
  967. @@ -166,7 +166,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  968.      while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
  969.          sps->i_log2_max_frame_num++;
  970.  
  971. -    sps->i_poc_type = param->i_bframe || param->b_interlaced ? 0 : 2;
  972. +    sps->i_poc_type = param->i_bframe || param->b_interlaced || param->b_field_encode ? 0 : 2;
  973.      if( sps->i_poc_type == 0 )
  974.      {
  975.          int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
  976. @@ -178,8 +178,9 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  977.      sps->b_vui = 1;
  978.  
  979.      sps->b_gaps_in_frame_num_value_allowed = 0;
  980. -    sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
  981. -    if( !sps->b_frame_mbs_only )
  982. +    sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced || param->b_field_encode);
  983. +    sps->b_half_height = param->b_interlaced || param->b_fake_interlaced;
  984. +    if( sps->b_half_height )
  985.          sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
  986.      sps->b_mb_adaptive_frame_field = param->b_interlaced;
  987.      sps->b_direct8x8_inference = 1;
  988. @@ -187,7 +188,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  989.      sps->crop.i_left   = param->crop_rect.i_left;
  990.      sps->crop.i_top    = param->crop_rect.i_top;
  991.      sps->crop.i_right  = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
  992. -    sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
  993. +    sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> sps->b_half_height;
  994.      sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
  995.                    sps->crop.i_right || sps->crop.i_bottom;
  996.  
  997. @@ -241,7 +242,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
  998.      if( sps->vui.b_timing_info_present )
  999.      {
  1000.          sps->vui.i_num_units_in_tick = param->i_timebase_num;
  1001. -        sps->vui.i_time_scale = param->i_timebase_den * 2;
  1002. +        sps->vui.i_time_scale = param->i_timebase_den * (2-param->b_field_encode);
  1003.          sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
  1004.      }
  1005.  
  1006. @@ -295,7 +296,7 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
  1007.      bs_write_ue( s, sps->i_num_ref_frames );
  1008.      bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
  1009.      bs_write_ue( s, sps->i_mb_width - 1 );
  1010. -    bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
  1011. +    bs_write_ue( s, (sps->i_mb_height >> sps->b_half_height) - 1);
  1012.      bs_write1( s, sps->b_frame_mbs_only );
  1013.      if( !sps->b_frame_mbs_only )
  1014.          bs_write1( s, sps->b_mb_adaptive_frame_field );
  1015. @@ -819,8 +820,7 @@ int x264_validate_levels( x264_t *h, int verbose )
  1016.          l++;
  1017.  
  1018.      if( l->frame_size < mbs
  1019. -        || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
  1020. -        || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
  1021. +        || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width )
  1022.          ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
  1023.                 h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
  1024.      if( dpb > l->dpb )
  1025. @@ -836,6 +836,7 @@ int x264_validate_levels( x264_t *h, int verbose )
  1026.      CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
  1027.      CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
  1028.      CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
  1029. +    CHECK( "field encoding", !l->frame_only, h->param.b_field_encode );
  1030.  
  1031.      if( h->param.i_fps_den > 0 )
  1032.          CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
  1033. diff --git a/encoder/slicetype.c b/encoder/slicetype.c
  1034. index 0f4d831..d64c89c 100644
  1035. --- a/encoder/slicetype.c
  1036. +++ b/encoder/slicetype.c
  1037. @@ -859,6 +859,9 @@ static int x264_slicetype_frame_cost( x264_t *h, x264_mb_analysis_t *a,
  1038.          }
  1039.          if( do_search[1] ) fenc->lowres_mvs[1][p1-b-1][0][0] = 0;
  1040.  
  1041. +        if( b != p0 )
  1042. +            printf("\n do search %i %i %i \n", do_search[0], do_search[1], fenc->lowres_mvs[0][b-p0-1][0][0] );
  1043. +
  1044.          if( p1 != p0 )
  1045.              dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
  1046.  
  1047. @@ -1101,6 +1104,8 @@ static void x264_macroblock_tree( x264_t *h, x264_mb_analysis_t *a, x264_frame_t
  1048.  
  1049.      int i = num_frames;
  1050.  
  1051. +    printf("\n MBTREE num_frames %i \n", num_frames );
  1052. +
  1053.      if( b_intra )
  1054.          x264_slicetype_frame_cost( h, a, frames, 0, 0, 0, 0 );
  1055.  
  1056. @@ -1501,6 +1506,9 @@ void x264_slicetype_analyse( x264_t *h, int intra_minigop )
  1057.          return;
  1058.      }
  1059.  
  1060. +    if( PARAM_FIELD_ENCODE && frames[0]->i_frame ^ 1 )
  1061. +        frames[1]->i_type = X264_TYPE_P;
  1062. +
  1063.  #if HAVE_OPENCL
  1064.      x264_opencl_slicetype_prep( h, frames, num_frames, a.i_lambda );
  1065.  #endif
  1066. @@ -1640,7 +1648,7 @@ void x264_slicetype_analyse( x264_t *h, int intra_minigop )
  1067.  
  1068.  void x264_slicetype_decide( x264_t *h )
  1069.  {
  1070. -    x264_frame_t *frames[X264_BFRAME_MAX+2];
  1071. +    x264_frame_t *frames[X264_BFRAME_MAX+3];
  1072.      x264_frame_t *frm;
  1073.      int bframes;
  1074.      int brefs;
  1075. @@ -1655,7 +1663,8 @@ void x264_slicetype_decide( x264_t *h )
  1076.          if( h->param.b_vfr_input )
  1077.          {
  1078.              if( lookahead_size-- > 1 )
  1079. -                h->lookahead->next.list[i]->i_duration = 2 * (h->lookahead->next.list[i+1]->i_pts - h->lookahead->next.list[i]->i_pts);
  1080. +                h->lookahead->next.list[i]->i_duration = (2-PARAM_FIELD_ENCODE)
  1081. +                                                       * (h->lookahead->next.list[i+1]->i_pts - h->lookahead->next.list[i]->i_pts);
  1082.              else
  1083.                  h->lookahead->next.list[i]->i_duration = h->i_prev_duration;
  1084.          }
  1085. @@ -1695,6 +1704,14 @@ void x264_slicetype_decide( x264_t *h )
  1086.      for( bframes = 0, brefs = 0;; bframes++ )
  1087.      {
  1088.          frm = h->lookahead->next.list[bframes];
  1089. +
  1090. +        /* Any non-scenecut field following an I-field must be a P-field */
  1091. +        if( PARAM_FIELD_ENCODE && !IS_X264_TYPE_I( frm->i_type ) && frm->i_frame == h->lookahead->i_last_keyframe+1 )
  1092. +        {
  1093. +            frm->i_type = X264_TYPE_P;
  1094. +            frm->b_ref_opp_field = 1;
  1095. +        }
  1096. +
  1097.          if( frm->i_type == X264_TYPE_BREF && h->param.i_bframe_pyramid < X264_B_PYRAMID_NORMAL &&
  1098.              brefs == h->param.i_bframe_pyramid )
  1099.          {
  1100. @@ -1705,7 +1722,7 @@ void x264_slicetype_decide( x264_t *h )
  1101.          /* pyramid with multiple B-refs needs a big enough dpb that the preceding P-frame stays available.
  1102.             smaller dpb could be supported by smart enough use of mmco, but it's easier just to forbid it. */
  1103.          else if( frm->i_type == X264_TYPE_BREF && h->param.i_bframe_pyramid == X264_B_PYRAMID_NORMAL &&
  1104. -            brefs && h->param.i_frame_reference <= (brefs+3) )
  1105. +            brefs && (h->param.i_frame_reference>>PARAM_FIELD_ENCODE) <= (brefs+3) )
  1106.          {
  1107.              frm->i_type = X264_TYPE_B;
  1108.              x264_log( h, X264_LOG_WARNING, "B-ref at frame %d incompatible with B-pyramid %s and %d reference frames\n",
  1109. @@ -1748,16 +1765,18 @@ void x264_slicetype_decide( x264_t *h )
  1110.              frm->b_keyframe = 1;
  1111.              if( bframes > 0 )
  1112.              {
  1113. -                bframes--;
  1114. +                bframes -= 1+PARAM_FIELD_ENCODE;
  1115.                  h->lookahead->next.list[bframes]->i_type = X264_TYPE_P;
  1116. +                if( PARAM_FIELD_ENCODE )
  1117. +                    h->lookahead->next.list[bframes+1]->i_type = X264_TYPE_P;
  1118.              }
  1119.          }
  1120.  
  1121.          if( bframes == h->param.i_bframe ||
  1122. -            !h->lookahead->next.list[bframes+1] )
  1123. +            !h->lookahead->next.list[bframes+1+PARAM_FIELD_ENCODE] )
  1124.          {
  1125.              if( IS_X264_TYPE_B( frm->i_type ) )
  1126. -                x264_log( h, X264_LOG_WARNING, "specified frame type is not compatible with max B-frames\n" );
  1127. +                x264_log( h, X264_LOG_WARNING, "specified frame type is not compatible with max B-frames, frame %i\n", frm->i_frame );
  1128.              if( frm->i_type == X264_TYPE_AUTO
  1129.                  || IS_X264_TYPE_B( frm->i_type ) )
  1130.                  frm->i_type = X264_TYPE_P;
  1131. @@ -1774,7 +1793,15 @@ void x264_slicetype_decide( x264_t *h )
  1132.  
  1133.      if( bframes )
  1134.          h->lookahead->next.list[bframes-1]->b_last_minigop_bframe = 1;
  1135. -    h->lookahead->next.list[bframes]->i_bframes = bframes;
  1136. +
  1137. +    if( PARAM_FIELD_ENCODE )
  1138. +    {
  1139. +        h->lookahead->next.list[bframes+0]->i_bframes = 0;
  1140. +        if( lookahead_size > 1 )
  1141. +            h->lookahead->next.list[bframes+1]->i_bframes = bframes;
  1142. +    }
  1143. +    else
  1144. +        h->lookahead->next.list[bframes]->i_bframes = bframes;
  1145.  
  1146.      /* insert a bref into the sequence */
  1147.      if( h->param.i_bframe_pyramid && bframes > 1 && !brefs )
  1148. @@ -1792,12 +1819,30 @@ void x264_slicetype_decide( x264_t *h )
  1149.  
  1150.          x264_lowres_context_init( h, &a );
  1151.  
  1152. -        frames[0] = h->lookahead->last_nonb;
  1153. -        memcpy( &frames[1], h->lookahead->next.list, (bframes+1) * sizeof(x264_frame_t*) );
  1154. -        if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
  1155. -            p0 = bframes + 1;
  1156. -        else // P
  1157. -            p0 = 0;
  1158. +        if( PARAM_FIELD_ENCODE )
  1159. +        {
  1160. +            frames[0] = h->lookahead->penultimate_nonb;
  1161. +            frames[1] = h->lookahead->last_nonb;
  1162. +            memcpy( &frames[2], h->lookahead->next.list, (bframes+1) * sizeof(x264_frame_t*) );
  1163. +            p1 = b = 2;
  1164. +            if( frames[1] && IS_X264_TYPE_I( frames[1]->i_type ) )
  1165. +                p0 = 1;
  1166. +            else if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
  1167. +                p0 = bframes + 2;
  1168. +            else // P
  1169. +                p0 = 0;
  1170. +        }
  1171. +        else
  1172. +        {
  1173. +            frames[0] = h->lookahead->last_nonb;
  1174. +            memcpy( &frames[1], h->lookahead->next.list, (bframes+1) * sizeof(x264_frame_t*) );
  1175. +            if( IS_X264_TYPE_I( h->lookahead->next.list[bframes]->i_type ) )
  1176. +                p0 = bframes + 1;
  1177. +            else // P
  1178. +                p0 = 0;
  1179. +        }
  1180. +
  1181. +        printf("\n SD %i %i %i \n", p0, p1, b );
  1182.  
  1183.          x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
  1184.  
  1185. @@ -1835,7 +1880,7 @@ void x264_slicetype_decide( x264_t *h )
  1186.      int i_coded = h->lookahead->next.list[0]->i_frame;
  1187.      if( bframes )
  1188.      {
  1189. -        int idx_list[] = { brefs+1, 1 };
  1190. +        int idx_list[2] = { brefs+1+PARAM_FIELD_ENCODE, 1+PARAM_FIELD_ENCODE };
  1191.          for( int i = 0; i < bframes; i++ )
  1192.          {
  1193.              int idx = idx_list[h->lookahead->next.list[i]->i_type == X264_TYPE_BREF]++;
  1194. @@ -1844,6 +1889,11 @@ void x264_slicetype_decide( x264_t *h )
  1195.          }
  1196.          frames[0] = h->lookahead->next.list[bframes];
  1197.          frames[0]->i_reordered_pts = h->lookahead->next.list[0]->i_pts;
  1198. +        if( PARAM_FIELD_ENCODE )
  1199. +        {
  1200. +            frames[1] = h->lookahead->next.list[bframes+1];
  1201. +            frames[1]->i_reordered_pts = h->lookahead->next.list[1]->i_pts;
  1202. +        }
  1203.          memcpy( h->lookahead->next.list, frames, (bframes+1) * sizeof(x264_frame_t*) );
  1204.      }
  1205.  
  1206. @@ -1868,22 +1918,34 @@ int x264_rc_analyse_slice( x264_t *h )
  1207.  {
  1208.      int p0 = 0, p1, b;
  1209.      int cost;
  1210. +    x264_frame_t **frames;
  1211.      x264_emms();
  1212.  
  1213.      if( IS_X264_TYPE_I(h->fenc->i_type) )
  1214.          p1 = b = 0;
  1215.      else if( h->fenc->i_type == X264_TYPE_P )
  1216. -        p1 = b = h->fenc->i_bframes + 1;
  1217. +    {
  1218. +        p1 = b = h->fenc->i_bframes + 1+PARAM_FIELD_ENCODE;
  1219. +        if( PARAM_FIELD_ENCODE && h->fenc->b_ref_opp_field )
  1220. +        {
  1221. +            p1--;
  1222. +            b--;
  1223. +        }
  1224. +        frames = &h->fenc - b;
  1225. +    }
  1226.      else //B
  1227.      {
  1228.          p1 = (h->fref_nearest[1]->i_poc - h->fref_nearest[0]->i_poc)/2;
  1229.          b  = (h->fenc->i_poc - h->fref_nearest[0]->i_poc)/2;
  1230.      }
  1231.      /* We don't need to assign p0/p1 since we are not performing any real analysis here. */
  1232. -    x264_frame_t **frames = &h->fenc - b;
  1233. +    frames = &h->fenc - b;
  1234. +
  1235. +    printf("\n num %i ref %i p1 %i b %i \n", frames[p1]->i_frame, h->fenc->b_ref_opp_field, p1, b );
  1236.  
  1237.      /* cost should have been already calculated by x264_slicetype_decide */
  1238.      cost = frames[b]->i_cost_est[b-p0][p1-b];
  1239. +    printf("\n b-p0 %i p1-b %i \n", b-p0, p1-b );
  1240.      assert( cost >= 0 );
  1241.  
  1242.      if( h->param.rc.b_mb_tree && !h->param.rc.b_stat_read )
  1243. diff --git a/x264.c b/x264.c
  1244. index 427d06c..7599331 100644
  1245. --- a/x264.c
  1246. +++ b/x264.c
  1247. @@ -691,6 +691,7 @@ static void help( x264_param_t *defaults, int longhelp )
  1248.      H2( "      --slice-min-mbs <integer> Limit the size of each slice in macroblocks (min)\n");
  1249.      H0( "      --tff                   Enable interlaced mode (top field first)\n" );
  1250.      H0( "      --bff                   Enable interlaced mode (bottom field first)\n" );
  1251. +    H0( "      --field-encode          Enable field encoding - one frame will be encoded as two fields \n" );
  1252.      H2( "      --constrained-intra     Enable constrained intra prediction.\n" );
  1253.      H0( "      --pulldown <string>     Use soft pulldown to change frame rate\n"
  1254.          "                                  - none, 22, 32, 64, double, triple, euro (requires cfr input)\n" );
  1255. @@ -999,6 +1000,7 @@ static struct option long_options[] =
  1256.      { "tff",               no_argument, NULL, OPT_INTERLACED },
  1257.      { "bff",               no_argument, NULL, OPT_INTERLACED },
  1258.      { "no-interlaced",     no_argument, NULL, OPT_INTERLACED },
  1259. +    { "field-encode",      no_argument, NULL, OPT_INTERLACED },
  1260.      { "constrained-intra", no_argument, NULL, 0 },
  1261.      { "cabac",             no_argument, NULL, 0 },
  1262.      { "no-cabac",          no_argument, NULL, 0 },
  1263. @@ -1857,9 +1859,17 @@ static int encode( x264_param_t *param, cli_opt_t *opt )
  1264.  
  1265.      opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
  1266.  
  1267. +    if( param->b_field_encode )
  1268. +    {
  1269. +        FAIL_IF_ERROR2( param->b_vfr_input, "field encoding is not compatible with vfr\n" );
  1270. +        param->i_fps_num <<= 1;
  1271. +        param->i_height >>= 1;
  1272. +    }
  1273. +
  1274.      /* set up pulldown */
  1275. -    if( opt->i_pulldown && !param->b_vfr_input )
  1276. +    if( opt->i_pulldown )
  1277.      {
  1278. +        FAIL_IF_ERROR2( param->b_field_encode, "field encoding is not compatible with pulldown\n" );
  1279.          param->b_pulldown = 1;
  1280.          param->b_pic_struct = 1;
  1281.          pulldown = &pulldown_values[opt->i_pulldown];
  1282. @@ -1936,18 +1946,43 @@ static int encode( x264_param_t *param, cli_opt_t *opt )
  1283.              parse_qpfile( opt, &pic, i_frame + opt->i_seek );
  1284.  
  1285.          prev_dts = last_dts;
  1286. -        i_frame_size = encode_frame( h, opt->hout, &pic, &last_dts );
  1287. -        if( i_frame_size < 0 )
  1288. +
  1289. +        if( param->b_field_encode )
  1290.          {
  1291. -            b_ctrl_c = 1; /* lie to exit the loop */
  1292. -            retval = -1;
  1293. +            for( int i = 0; i < pic.img.i_plane; i++ )
  1294. +            {
  1295. +                pic.img.plane[i] += param->b_tff ? 0 : pic.img.i_stride[i]; /* first field */
  1296. +                pic.img.i_stride[i] <<= 1;
  1297. +            }
  1298.          }
  1299. -        else if( i_frame_size )
  1300. +
  1301. +        for( int i = 0; i < 1+param->b_field_encode; i++ )
  1302.          {
  1303. -            i_file += i_frame_size;
  1304. -            i_frame_output++;
  1305. -            if( i_frame_output == 1 )
  1306. -                first_dts = prev_dts = last_dts;
  1307. +            /* x264cli takes in frames but in field mode libx264 takes fields
  1308. +             * the encode loop separates the fields from the frame */
  1309. +            if( i )
  1310. +            {
  1311. +                for( int j = 0; j < pic.img.i_plane; j++ )
  1312. +                    pic.img.plane[j] += param->b_tff ? (pic.img.i_stride[j] >> 1) : -(pic.img.i_stride[j] >> 1); /* second field */
  1313. +                pic.i_pts++;
  1314. +            }
  1315. +            else
  1316. +                pic.i_pts <<= 1;
  1317. +
  1318. +            i_frame_size = encode_frame( h, opt->hout, &pic, &last_dts );
  1319. +            if( i_frame_size < 0 )
  1320. +            {
  1321. +                b_ctrl_c = 1; /* lie to exit the outer encode loop */
  1322. +                retval = -1;
  1323. +                break;
  1324. +            }
  1325. +            else if( i_frame_size )
  1326. +            {
  1327. +                i_file += i_frame_size;
  1328. +                i_frame_output++;
  1329. +                if( i_frame_output == 1 )
  1330. +                    first_dts = prev_dts = last_dts;
  1331. +            }
  1332.          }
  1333.  
  1334.          if( filter.release_frame( opt->hin, &cli_pic, i_frame + opt->i_seek ) )
  1335. @@ -1955,7 +1990,8 @@ static int encode( x264_param_t *param, cli_opt_t *opt )
  1336.  
  1337.          /* update status line (up to 1000 times per input file) */
  1338.          if( opt->b_progress && i_frame_output )
  1339. -            i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
  1340. +            i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total*(1+param->b_field_encode),
  1341. +                                       i_file, param, 2 * last_dts - prev_dts - first_dts );
  1342.      }
  1343.      /* Flush delayed frames */
  1344.      while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
  1345. @@ -1975,7 +2011,8 @@ static int encode( x264_param_t *param, cli_opt_t *opt )
  1346.                  first_dts = prev_dts = last_dts;
  1347.          }
  1348.          if( opt->b_progress && i_frame_output )
  1349. -            i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
  1350. +            i_previous = print_status( i_start, i_previous, i_frame_output, param->i_frame_total*(1+param->b_field_encode),
  1351. +                                       i_file, param, 2 * last_dts - prev_dts - first_dts );
  1352.      }
  1353.  fail:
  1354.      if( pts_warning_cnt >= MAX_PTS_WARNING && cli_log_level < X264_LOG_DEBUG )
  1355. @@ -1989,6 +2026,10 @@ fail:
  1356.      else
  1357.          duration = (double)(2 * largest_pts - second_largest_pts) * param->i_timebase_num / param->i_timebase_den;
  1358.  
  1359. +    /* simpler than trying to use exact field timestamps */
  1360. +    if( param->b_field_encode )
  1361. +        duration *= 2;
  1362. +
  1363.      i_end = x264_mdate();
  1364.      /* Erase progress indicator before printing encoding stats. */
  1365.      if( opt->b_progress )
  1366. diff --git a/x264.h b/x264.h
  1367. index b896f91..7549ea5 100644
  1368. --- a/x264.h
  1369. +++ b/x264.h
  1370. @@ -309,7 +309,7 @@ typedef struct x264_param_t
  1371.      } vui;
  1372.  
  1373.      /* Bitstream parameters */
  1374. -    int         i_frame_reference;  /* Maximum number of reference frames */
  1375. +    int         i_frame_reference;  /* Maximum number of reference pictures */
  1376.      int         i_dpb_size;         /* Force a DPB size larger than that implied by B-frames and reference frames.
  1377.                                       * Useful in combination with interactive error resilience. */
  1378.      int         i_keyint_max;       /* Force an IDR keyframe at this interval */
  1379. @@ -320,7 +320,7 @@ typedef struct x264_param_t
  1380.      int         i_bframe;   /* how many b-frame between 2 references pictures */
  1381.      int         i_bframe_adaptive;
  1382.      int         i_bframe_bias;
  1383. -    int         i_bframe_pyramid;   /* Keep some B-frames as references: 0=off, 1=strict hierarchical, 2=normal */
  1384. +    int         i_bframe_pyramid;   /* Keep some B-pictures as references: 0=off, 1=strict hierarchical, 2=normal */
  1385.      int         b_open_gop;
  1386.      int         b_bluray_compat;
  1387.      int         i_avcintra_class;
  1388. @@ -333,6 +333,7 @@ typedef struct x264_param_t
  1389.      int         i_cabac_init_idc;
  1390.  
  1391.      int         b_interlaced;
  1392. +    int         b_field_encode;     /* With field encoding, libx264 takes separate fields */
  1393.      int         b_constrained_intra;
  1394.  
  1395.      int         i_cqm_preset;
  1396. @@ -456,8 +457,8 @@ typedef struct x264_param_t
  1397.      int b_vfr_input;            /* VFR input.  If 1, use timebase and timestamps for ratecontrol purposes.
  1398.                                   * If 0, use fps only. */
  1399.      int b_pulldown;             /* use explicity set timebase for CFR */
  1400. -    uint32_t i_fps_num;
  1401. -    uint32_t i_fps_den;
  1402. +    uint32_t i_fps_num;         /* Field rate when using field encoding mode, frame rate otherwise */
  1403. +    uint32_t i_fps_den;         /* Field rate when using field encoding mode, frame rate otherwise */
  1404.      uint32_t i_timebase_num;    /* Timebase numerator */
  1405.      uint32_t i_timebase_den;    /* Timebase denominator */
  1406.  
  1407. @@ -678,7 +679,8 @@ enum pic_struct_e
  1408.  {
  1409.      PIC_STRUCT_AUTO              = 0, // automatically decide (default)
  1410.      PIC_STRUCT_PROGRESSIVE       = 1, // progressive frame
  1411. -    // "TOP" and "BOTTOM" are not supported in x264 (PAFF only)
  1412. +    PIC_STRUCT_TOP               = 2, // top field
  1413. +    PIC_STRUCT_BOTTOM            = 3, // bottom field
  1414.      PIC_STRUCT_TOP_BOTTOM        = 4, // top field followed by bottom
  1415.      PIC_STRUCT_BOTTOM_TOP        = 5, // bottom field followed by top
  1416.      PIC_STRUCT_TOP_BOTTOM_TOP    = 6, // top field, bottom field, top field repeated
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement