OpenCores
URL https://opencores.org/ocsvn/oc-h264-encoder/oc-h264-encoder/trunk

Subversion Repositories oc-h264-encoder

[/] [oc-h264-encoder/] [trunk/] [x264/] [patches/] [x264-e381f6d-or32-or1ksim-with-fp-1.1.patch] - Blame information for rev 50

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 julius
diff -Naur --exclude=.git --exclude=.gitignore x264/common/common.c x264-or/common/common.c
2
--- x264/common/common.c        2009-10-25 17:41:22.000000000 +0100
3
+++ x264-or/common/common.c     2009-10-28 15:05:29.000000000 +0100
4
@@ -764,8 +764,10 @@
5
     align_buf = malloc( i_size );
6
 #elif defined( HAVE_MALLOC_H )
7
     align_buf = memalign( 16, i_size );
8
+    //fprintf(stderr, "memalign, result addr: 0x%.8x\n", (unsigned int) align_buf);
9
 #else
10
     uint8_t *buf = malloc( i_size + 15 + sizeof(void **) + sizeof(int) );
11
+    //fprintf(stderr, "malloc, result addr: 0x%.8x\n", (unsigned int) buf);
12
     if( buf )
13
     {
14
         align_buf = buf + 15 + sizeof(void **) + sizeof(int);
15
diff -Naur --exclude=.git --exclude=.gitignore x264/common/common.h x264-or/common/common.h
16
--- x264/common/common.h        2009-10-25 17:41:22.000000000 +0100
17
+++ x264-or/common/common.h     2009-11-10 22:42:24.000000000 +0100
18
@@ -51,7 +51,8 @@
19
 #define X264_BFRAME_MAX 16
20
 #define X264_THREAD_MAX 128
21
 #define X264_PCM_COST (386*8)
22
-#define X264_LOOKAHEAD_MAX 250
23
+//#define X264_LOOKAHEAD_MAX 250
24
+#define X264_LOOKAHEAD_MAX 140
25
 // arbitrary, but low because SATD scores are 1/4 normal
26
 #define X264_LOOKAHEAD_QP 12
27
 
28
@@ -260,7 +261,7 @@
29
  */
30
 #define X264_SCAN8_SIZE (6*8)
31
 #define X264_SCAN8_0 (4+1*8)
32
-
33
+/* this array indicates the position in the */
34
 static const int x264_scan8[16+2*4+3] =
35
 {
36
     /* Luma */
37
@@ -518,7 +519,9 @@
38
             /* space for p_fenc and p_fdec */
39
 #define FENC_STRIDE 16
40
 #define FDEC_STRIDE 32
41
-            ALIGNED_16( uint8_t fenc_buf[24*FENC_STRIDE] );
42
+         /* comment regarding FDEC_STRIDE: the catch with fdec is that the chroma blocks must be aligned to 8 and the luma blocks must be aligned to 16 but we need to have pixels on the left side for prediction so we have a lot of padding on the left side. so for luma: X X X X X X X X X X X X X X X L _ _ _ _ _ ... where X are junk, L is the left pixel, _ are normal pixels, so 32 wide total */
43
+
44
+         ALIGNED_16( uint8_t fenc_buf[24*FENC_STRIDE] ); /* FENC stores both Y, U, and V, 16x16 + 8x8 + 8x8 = 24 * 16*/
45
             ALIGNED_16( uint8_t fdec_buf[27*FDEC_STRIDE] );
46
 
47
             /* i4x4 and i8x8 backup data, for skipping the encode stage when possible */
48
@@ -551,9 +554,29 @@
49
 
50
             /* pointer over mb of the references */
51
             int i_fref[2];
52
-            uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
53
-            uint16_t *p_integral[2][16];
54
-
55
+            uint8_t *p_fref[2][32][4+2]; /* last: 4hpel pointers, 2 chroma pointers: lN (normal), lH (horizontally interpolated), lV (vertically interpolated), lHV (center position), cU (chroma U), cV (chroma V)
56
+                                           Important!: This is our post-motion compensation frame data, stored in half-pixel (hpel) resolution.
57
+                                           During motion compensation, hpel data is calculated via  6-tap FIR filter (very similar to Lanczos). It is an even filter (even function) so every input pixel corresponds to 4 output pixels
58
+                                           one of those 4 is equal to the input, so the data contains one fullpel pixel, one H, one V, one C (aka HV)
59
+                                           From this data, qpel is calculated via interpolation, which is easy and quick to do.
60
+                                           F 0 H 1 F
61
+                                           2 3 4 5 6
62
+                                           V 7 C 8 V
63
+                                           9 A B C D
64
+                                           F _ H _ F
65
+                                           Here 0 is the average of F and H
66
+                                           2 is the average of F and V
67
+                                           3 is the average of H and V
68
+                                           4 is the average of H and C
69
+                                           etc.
70
+                                           So each qpel position is the result of averaging two known hpel positions.*/
71
+         uint16_t *p_integral[2][16]; /* pointers to "integral": for each position X,Y, it's the sum of values in an 8x8 block with that point X,Y as the upper left of that block
72
+                                         Discussion on this value:
73
+                                         < pengvado> Dark_Shikari: "sum of values in an 8x8 block" is what I would call "dc". the variable name comes because it used to be an indefinite integral, i.e. the sum of everything above and to the left of that point.
74
+                                         < pengvado> but if you're only interested in 16x16, 16x8, 8x16, and 8x8, then caching 8x8 dcs is strictly faster
75
+                                      */
76
+
77
+
78
             /* fref stride */
79
             int     i_stride[3];
80
         } pic;
81
diff -Naur --exclude=.git --exclude=.gitignore x264/common/frame.c x264-or/common/frame.c
82
--- x264/common/frame.c 2009-10-25 17:41:10.000000000 +0100
83
+++ x264-or/common/frame.c      2009-10-28 15:05:29.000000000 +0100
84
@@ -31,6 +31,8 @@
85
     x264_frame_t *frame;
86
     int i, j;
87
 
88
+    //V(fprintf(stderr, "x264_frame_new\n"));
89
+
90
     int i_mb_count = h->mb.i_mb_count;
91
     int i_stride, i_width, i_lines;
92
     int i_padv = PADV << h->param.b_interlaced;
93
@@ -981,11 +983,14 @@
94
 
95
 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
96
 {
97
+  //V(fprintf(stderr, "x264_frame_pop_unused\n"));
98
     x264_frame_t *frame;
99
     if( h->frames.unused[b_fdec][0] )
100
         frame = x264_frame_pop( h->frames.unused[b_fdec] );
101
     else
102
         frame = x264_frame_new( h, b_fdec );
103
+
104
+    //V(fprintf(stderr, "x264_frame_pop_unused: frame ptr = 0x%.8x\n", (unsigned long) frame));
105
     if( !frame )
106
         return NULL;
107
     frame->b_last_minigop_bframe = 0;
108
diff -Naur --exclude=.git --exclude=.gitignore x264/common/macroblock.c x264-or/common/macroblock.c
109
--- x264/common/macroblock.c    2009-10-25 17:41:22.000000000 +0100
110
+++ x264-or/common/macroblock.c 2009-11-09 13:02:31.000000000 +0100
111
@@ -118,7 +118,7 @@
112
 
113
     int i_count = 0;
114
 
115
-    if( i_refc == -2 )
116
+    if( i_refc == -2 )/* -2 = unavailable */
117
     {
118
         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
119
         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
120
diff -Naur --exclude=.git --exclude=.gitignore x264/common/mdate.c x264-or/common/mdate.c
121
--- x264/common/mdate.c 2009-10-25 17:41:10.000000000 +0100
122
+++ x264-or/common/mdate.c      2009-10-28 15:05:29.000000000 +0100
123
@@ -32,9 +32,12 @@
124
 int64_t x264_mdate( void )
125
 {
126
 #ifndef __MINGW32__
127
+  /* --jb
128
     struct timeval tv_date;
129
     gettimeofday( &tv_date, NULL );
130
     return( (int64_t) tv_date.tv_sec * 1000000 + (int64_t) tv_date.tv_usec );
131
+  */
132
+  return 0;
133
 #else
134
     struct _timeb tb;
135
     _ftime(&tb);
136
diff -Naur --exclude=.git --exclude=.gitignore x264/common/pixel.c x264-or/common/pixel.c
137
--- x264/common/pixel.c 2009-10-25 17:41:10.000000000 +0100
138
+++ x264-or/common/pixel.c      2009-10-28 15:05:29.000000000 +0100
139
@@ -22,6 +22,7 @@
140
  *****************************************************************************/
141
 
142
 #include "common.h"
143
+#include "x264.h"
144
 
145
 #ifdef HAVE_MMX
146
 #   include "x86/pixel.h"
147
@@ -40,6 +41,7 @@
148
 /****************************************************************************
149
  * pixel_sad_WxH
150
  ****************************************************************************/
151
+/*  V(fprintf(stderr, "pixel_sad %d x %d, *pix1: 0x%.8x *pix2: 0x%.8x i_stride_pix1: %d i_stride_pix2: %d\n", lx, ly, (unsigned long) pix1, (unsigned long) pix2, i_stride_pix1, i_stride_pix2)); \*/
152
 #define PIXEL_SAD_C( name, lx, ly ) \
153
 static int name( uint8_t *pix1, int i_stride_pix1,  \
154
                  uint8_t *pix2, int i_stride_pix2 ) \
155
@@ -387,6 +389,7 @@
156
 
157
 /****************************************************************************
158
  * pixel_sad_x4
159
+ * Debug printf :   V(fprintf(stderr, "x264_pixel_sadx4 size : *fenc: 0x%.8x *pix0: 0x%.8x *pix1: 0x%.8x *pix2: 0x%.8x *pix3: 0x%.8x i_stride: %d  *scores: 0x%.8x\n", (unsigned long) fenc, (unsigned long) pix0, (unsigned long) pix1, (unsigned long)pix2, (unsigned long)pix3, i_stride, (unsigned long) scores)); \
160
  ****************************************************************************/
161
 #define SAD_X( size ) \
162
 static void x264_pixel_sad_x3_##size( uint8_t *fenc, uint8_t *pix0, uint8_t *pix1, uint8_t *pix2, int i_stride, int scores[3] )\
163
diff -Naur --exclude=.git --exclude=.gitignore x264/configure x264-or/configure
164
--- x264/configure      2009-10-25 17:41:10.000000000 +0100
165
+++ x264-or/configure   2009-10-28 15:05:29.000000000 +0100
166
@@ -29,7 +29,7 @@
167
     rm -f conftest.c
168
     [ -n "$1" ] && echo "#include <$1>" > conftest.c
169
     echo "int main () { $3 return 0; }" >> conftest.c
170
-    $CC conftest.c $CFLAGS $LDFLAGS $2 -o conftest 2>$DEVNULL
171
+    $CC conftest.c $CFLAGS $LDFLAGS $2 -o conftest #2>$DEVNULL
172
 }
173
 
174
 as_check() {
175
diff -Naur --exclude=.git --exclude=.gitignore x264/encoder/analyse.c x264-or/encoder/analyse.c
176
--- x264/encoder/analyse.c      2009-10-25 17:41:10.000000000 +0100
177
+++ x264-or/encoder/analyse.c   2009-11-09 13:11:23.000000000 +0100
178
@@ -27,6 +27,7 @@
179
 #include <unistd.h>
180
 
181
 #include "common/common.h"
182
+#include "x264.h"
183
 #include "common/cpu.h"
184
 #include "macroblock.h"
185
 #include "me.h"
186
@@ -34,6 +35,7 @@
187
 #include "analyse.h"
188
 #include "rdo.c"
189
 
190
+
191
 typedef struct
192
 {
193
     /* 16x16 */
194
@@ -233,40 +235,75 @@
195
     5, 3, 3, 1
196
 };
197
 
198
+extern const float x264_analyse_init_log2_array[]; //jb
199
+
200
 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
201
 
202
 static uint16_t x264_cost_ref[92][3][33];
203
 static x264_pthread_mutex_t cost_ref_mutex = X264_PTHREAD_MUTEX_INITIALIZER;
204
 
205
+/*
206
+float log2f( float n )
207
+{
208
+// log(n)/log(2) is log2.
209
+//return log10( n ) / log10( 2 );
210
+  return (float) (log( n ) / _M_LOG2_E);
211
+}
212
+*/
213
 int x264_analyse_init_costs( x264_t *h, int qp )
214
 {
215
     int i, j;
216
     int lambda = x264_lambda_tab[qp];
217
+    float tmp_f,lambda_f = lambda;
218
+
219
     if( h->cost_mv[lambda] )
220
         return 0;
221
+
222
+    fprintf( stderr,
223
+            "x264_analyse_init_costs: lambda (= x264_lambda_tab[%d])  = %d\n",
224
+            qp, lambda);
225
+
226
     /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
227
     CHECKED_MALLOC( h->cost_mv[lambda], (4*4*2048 + 1) * sizeof(uint16_t) );
228
+
229
     h->cost_mv[lambda] += 2*4*2048;
230
+
231
+    /* Computationally expensive thing: logs, floating point math, much of -- jb*/
232
     for( i = 0; i <= 2*4*2048; i++ )
233
-    {
234
-        h->cost_mv[lambda][-i] =
235
-        h->cost_mv[lambda][i]  = lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
236
-    }
237
+      {
238
+
239
+         //h->cost_mv[lambda][i]  = lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
240
+         //h->cost_mv[lambda][i]  = lambda * (log2(i+1)*2 + 0.718f + !!i) + .5f;
241
+
242
+       // precalulated array, doing the above few calculations for us
243
+       tmp_f = lambda_f * x264_analyse_init_log2_array[i];
244
+       tmp_f += .5f;
245
+       h->cost_mv[lambda][-i] =
246
+         h->cost_mv[lambda][i]  = tmp_f;
247
+
248
+
249
+      }
250
     x264_pthread_mutex_lock( &cost_ref_mutex );
251
+
252
     for( i = 0; i < 3; i++ )
253
-        for( j = 0; j < 33; j++ )
254
-            x264_cost_ref[lambda][i][j] = i ? lambda * bs_size_te( i, j ) : 0;
255
+      for( j = 0; j < 33; j++ )
256
+       x264_cost_ref[lambda][i][j] = i ? lambda * bs_size_te( i, j ) : 0;
257
+
258
     x264_pthread_mutex_unlock( &cost_ref_mutex );
259
+
260
+    /* for now, disable -- jb
261
     if( h->param.analyse.i_me_method >= X264_ME_ESA && !h->cost_mv_fpel[lambda][0] )
262
-    {
263
+      {
264
+       printf("analyse_init_costs: h->param.analyse.i_me_method >= X264_ME_ESA && !h->cost_mv_fpel[lambda][0]\n");
265
         for( j=0; j<4; j++ )
266
-        {
267
+         {
268
             CHECKED_MALLOC( h->cost_mv_fpel[lambda][j], (4*2048 + 1) * sizeof(uint16_t) );
269
             h->cost_mv_fpel[lambda][j] += 2*2048;
270
             for( i = -2*2048; i < 2*2048; i++ )
271
-                h->cost_mv_fpel[lambda][j][i] = h->cost_mv[lambda][i*4+j];
272
+             h->cost_mv_fpel[lambda][j][i] = h->cost_mv[lambda][i*4+j];
273
         }
274
     }
275
+    */
276
     return 0;
277
 fail:
278
     return -1;
279
@@ -288,6 +325,7 @@
280
 /* initialize an array of lambda*nbits for all possible mvs */
281
 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
282
 {
283
+  //V(fprintf(stderr, "x264_mb_analyse_load_costs() called\n"));
284
     a->p_cost_mv = h->cost_mv[a->i_lambda];
285
     a->p_cost_ref0 = x264_cost_ref[a->i_lambda][x264_clip3(h->sh.i_num_ref_idx_l0_active-1,0,2)];
286
     a->p_cost_ref1 = x264_cost_ref[a->i_lambda][x264_clip3(h->sh.i_num_ref_idx_l1_active-1,0,2)];
287
@@ -1161,6 +1199,7 @@
288
     (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
289
     (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
290
 
291
+
292
 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
293
     (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
294
     (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
295
@@ -1183,20 +1222,35 @@
296
 
297
     /* 16x16 Search on all ref frame */
298
     m.i_pixel = PIXEL_16x16;
299
-    m.p_cost_mv = a->p_cost_mv;
300
-    LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
301
+    m.p_cost_mv = a->p_cost_mv; /* where a->p_cost_mv = h->cost_mv[a->i_lambda] from x264_mb_analyse_init(). a->i_lambda related to qp of MB, and h->cost_mv[] calculated at beginning in x264_mb_analyse_local_costs() */
302
+    /* LOAD_FENC(m, src, xoff, yoff) :
303
+       h->mb.pic.p_fenc: pointer over mb of the frame to be compressed, i think array of 3 pointers, one each to Y, Cb, Cr data.
304
+       x264_me_t m is loaded with appropriate stride variables, and pointers to the appropriate pixels
305
+       Essentially loads the me struct with stride and offsets, this is done in a macro because it's  convenient
306
+       because the many partitions can use the same macro to setup x264_me_t struct
307
+     */
308
+    LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
309
+    //printf("x264_mb_analyse_inter_p16x16: m.i_stride[0]: %d\n",m.i_stride[0]);
310
+    //printf("x264_mb_analyse_inter_p16x16: m.i_stride[1]: %d\n",m.i_stride[1]);
311
+    //usually m.i_stride[0] is 416 and m.i_stride[1] is 208  -- jb
312
 
313
     a->l0.me16x16.cost = INT_MAX;
314
+
315
+    //printf("x264_mb_analyse_inter_p16x16: h->mb.pic.i_fref[0] = %d\n",h->mb.pic.i_fref[0]);
316
+
317
+    // Usually just the single reference frame here
318
     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
319
     {
320
-        const int i_ref_cost = REF_COST( 0, i_ref );
321
-        i_halfpel_thresh -= i_ref_cost;
322
+      const int i_ref_cost = REF_COST( 0, i_ref ); /* looks in a->p_cost_ref0/1[], which is set in x264_mb_analyse_load_costs() */
323
+      i_halfpel_thresh -= i_ref_cost;
324
         m.i_ref_cost = i_ref_cost;
325
         m.i_ref = i_ref;
326
 
327
         /* search with ref */
328
-        LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
329
-        x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
330
+       /*          m,             src,           list, ref, xoff, yoff */
331
+        LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref,  0 ,   0 );
332
+       /*                        h,i_list, i_ref, int16_t mvp[2] */
333
+        x264_mb_predict_mv_16x16( h,     0, i_ref, m.mvp         );
334
         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
335
         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
336
 
337
diff -Naur --exclude=.git --exclude=.gitignore x264/encoder/analyse_gen_init_array.sh x264-or/encoder/analyse_gen_init_array.sh
338
--- x264/encoder/analyse_gen_init_array.sh      1970-01-01 01:00:00.000000000 +0100
339
+++ x264-or/encoder/analyse_gen_init_array.sh   2009-10-28 15:05:29.000000000 +0100
340
@@ -0,0 +1,32 @@
341
+#!/bin/bash
342
+# Pre-generate an array of numbers according to the algorithm from analyse_init_costs()
343
+# to speedup calculation. Here we calculate the stuff in brackets:
344
+# lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
345
+outfile=analyse_init_log2.c
346
+
347
+echo; echo "Generating analyse_init_log2_array[] to $outfile"
348
+echo "This may take a minute or two..."
349
+
350
+#Size of the array: 4*2*2048
351
+numvals=16384
352
+
353
+rm  -f $outfile
354
+
355
+echo "const float x264_analyse_init_log2_array[] = {" >> $outfile
356
+
357
+n=0
358
+
359
+    for num in `seq 1 $numvals`;
360
+    do
361
+# Use bc to do the calculation, scale down to the output to one decimal place
362
+       newnum=`echo "a=((l($num)/l(2))*2)+1.718; scale=1; a=(a*2)/2; a" | bc -l`
363
+       echo -n $newnum >> $outfile
364
+       if [ $num -lt $numvals ]; then echo -n ", " >> $outfile; fi;
365
+       # Newline every 16 values
366
+       let "n = $num % 16"
367
+       if [ $n -eq 0 ]; then echo >> $outfile; fi;
368
+    done
369
+echo "};" >> $outfile
370
+
371
+echo "finished"
372
+echo
373
diff -Naur --exclude=.git --exclude=.gitignore x264/encoder/encoder.c x264-or/encoder/encoder.c
374
--- x264/encoder/encoder.c      2009-10-25 17:41:22.000000000 +0100
375
+++ x264-or/encoder/encoder.c   2009-10-28 15:05:29.000000000 +0100
376
@@ -62,6 +62,9 @@
377
 
378
 static void x264_frame_dump( x264_t *h )
379
 {
380
+  //printf("frame_dump()\n");
381
+  return;
382
+
383
     FILE *f = fopen( h->param.psz_dump_yuv, "r+b" );
384
     int i, y;
385
     if( !f )
386
@@ -301,6 +304,7 @@
387
     if( ( h->param.b_cabac && (h->cabac.p_end - h->cabac.p < 2500) )
388
      || ( h->out.bs.p_end - h->out.bs.p < 2500 ) )
389
     {
390
+      //V(fprintf(stderr, "x264_bitstream_check_buffer: h->out.bs.p = 0x%.8x, h->out.bs.p_end = 0x%.8x\n",h->out.bs.p_end, h->out.bs.p ));
391
         intptr_t delta;
392
         int i;
393
 
394
@@ -695,6 +699,8 @@
395
 static void mbcmp_init( x264_t *h )
396
 {
397
     int satd = !h->mb.b_lossless && h->param.analyse.i_subpel_refine > 1;
398
+    //V(if (satd)fprintf(stderr, "macroblock compare functions. satd enabled\n"));
399
+    //V(if (!satd)fprintf(stderr, "macroblock compare functions. satd not enabled\n"));
400
     memcpy( h->pixf.mbcmp, satd ? h->pixf.satd : h->pixf.sad_aligned, sizeof(h->pixf.mbcmp) );
401
     memcpy( h->pixf.mbcmp_unaligned, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.mbcmp_unaligned) );
402
     h->pixf.intra_mbcmp_x3_16x16 = satd ? h->pixf.intra_satd_x3_16x16 : h->pixf.intra_sad_x3_16x16;
403
@@ -750,15 +756,14 @@
404
     x264_t *h;
405
     char buf[1000], *p;
406
     int i, qp, i_slicetype_length;
407
-
408
+
409
     CHECKED_MALLOCZERO( h, sizeof(x264_t) );
410
-
411
+
412
     /* Create a copy of param */
413
     memcpy( &h->param, param, sizeof(x264_param_t) );
414
-
415
     if( param->param_free )
416
         param->param_free( param );
417
-
418
+
419
     if( x264_validate_parameters( h ) < 0 )
420
         goto fail;
421
 
422
@@ -772,7 +777,7 @@
423
         h->param.rc.psz_stat_in = strdup( h->param.rc.psz_stat_in );
424
 
425
     x264_set_aspect_ratio( h, param, 1 );
426
-
427
+
428
     x264_reduce_fraction( &h->param.i_fps_num, &h->param.i_fps_den );
429
 
430
     /* Init x264_t */
431
@@ -831,8 +836,8 @@
432
     h->i_ref0 = 0;
433
     h->i_ref1 = 0;
434
 
435
-    x264_rdo_init();
436
-
437
+    x264_rdo_init();
438
+
439
     /* init CPU functions */
440
     x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
441
     x264_predict_8x8c_init( h->param.cpu, h->predict_8x8c );
442
@@ -870,17 +875,25 @@
443
         p += sprintf( p, " none!" );
444
     x264_log( h, X264_LOG_INFO, "%s\n", buf );
445
 
446
+    /* // should skip during cycle accurate -- jb    */
447
+    // Check the different quantisation points and their costs
448
     for( qp = h->param.rc.i_qp_min; qp <= h->param.rc.i_qp_max; qp++ )
449
+      {
450
+       //printf("%d\n", qp);
451
         if( x264_analyse_init_costs( h, qp ) )
452
-            goto fail;
453
+         goto fail;
454
+      }
455
     if( x264_analyse_init_costs( h, X264_LOOKAHEAD_QP ) )
456
         goto fail;
457
     if( h->cost_mv[1][2013] != 24 )
458
-    {
459
+      {
460
+       //fprintf(stderr, "h->cost_mv[1][2013] = %d\n",h->cost_mv[1][2013]);
461
         x264_log( h, X264_LOG_ERROR, "MV cost test failed: x264 has been miscompiled!\n" );
462
         goto fail;
463
     }
464
 
465
+    //exit(0); // jb
466
+
467
     h->out.i_nal = 0;
468
     h->out.i_bitstream = X264_MAX( 1000000, h->param.i_width * h->param.i_height * 4
469
         * ( h->param.rc.i_rc_method == X264_RC_ABR ? pow( 0.95, h->param.rc.i_qp_min )
470
@@ -908,16 +921,17 @@
471
         if( x264_macroblock_cache_init( h->thread[i] ) < 0 )
472
             goto fail;
473
     }
474
-
475
+
476
     if( x264_lookahead_init( h, i_slicetype_length ) )
477
         goto fail;
478
 
479
     if( x264_ratecontrol_new( h ) < 0 )
480
         goto fail;
481
-
482
+
483
+    /* // skip this for now, it's a fopen() call we won't really do, plus it's an option we'll never need --jb
484
     if( h->param.psz_dump_yuv )
485
     {
486
-        /* create or truncate the reconstructed video file */
487
+    //create or truncate the reconstructed video file
488
         FILE *f = fopen( h->param.psz_dump_yuv, "w" );
489
         if( f )
490
             fclose( f );
491
@@ -927,6 +941,8 @@
492
             goto fail;
493
         }
494
     }
495
+    */
496
+
497
 
498
     x264_log( h, X264_LOG_INFO, "profile %s, level %d.%d\n",
499
         h->sps->i_profile_idc == PROFILE_BASELINE ? "Baseline" :
500
@@ -1307,6 +1323,8 @@
501
     int slice_max_size = h->param.i_slice_max_size > 0 ? (h->param.i_slice_max_size-3-NALU_OVERHEAD)*8 : INT_MAX;
502
     int starting_bits = bs_pos(&h->out.bs);
503
 
504
+    //V(fprintf(stderr, "x264_slice_write\n"));
505
+
506
     /* Slice */
507
     x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );
508
 
509
@@ -1320,6 +1338,7 @@
510
         /* init cabac */
511
         x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );
512
         x264_cabac_encode_init ( &h->cabac, h->out.bs.p, h->out.bs.p_end );
513
+       //V(fprintf(stderr, "\tcabac inited\n"));
514
     }
515
     h->mb.i_last_qp = h->sh.i_qp;
516
     h->mb.i_last_dqp = 0;
517
@@ -1362,7 +1381,10 @@
518
         x264_macroblock_encode( h );
519
 
520
         if( x264_bitstream_check_buffer( h ) )
521
+         {
522
+           //V(fprintf(stderr, "\tx264_bitstream_check_buffer failed\n"));
523
             return -1;
524
+         }
525
 
526
         if( h->param.b_cabac )
527
         {
528
@@ -1522,6 +1544,8 @@
529
         x264_fdec_filter_row( h, h->sps->i_mb_height );
530
     }
531
 
532
+    //V(fprintf(stderr, "x264_slice_write ok\n"));
533
+
534
     return 0;
535
 }
536
 
537
@@ -1625,8 +1649,8 @@
538
     int     i_nal_type, i;
539
     int     i_nal_ref_idc;
540
 
541
-    int   i_global_qp;
542
 
543
+    int   i_global_qp;
544
     if( h->param.i_threads > 1)
545
     {
546
         int i = ++h->i_thread_phase;
547
@@ -1653,6 +1677,7 @@
548
     /* no data out */
549
     *pi_nal = 0;
550
     *pp_nal = NULL;
551
+
552
 
553
     /* ------------------- Setup new frame from picture -------------------- */
554
     if( pic_in != NULL )
555
@@ -1663,7 +1688,10 @@
556
             return -1;
557
 
558
         if( x264_frame_copy_picture( h, fenc, pic_in ) < 0 )
559
+         {
560
+           //V(fprintf(stderr, "x264_frame_copy_picture returned >0\n"));
561
             return -1;
562
+         }
563
 
564
         if( h->param.i_width != 16 * h->sps->i_mb_width ||
565
             h->param.i_height != 16 * h->sps->i_mb_height )
566
@@ -1684,13 +1712,15 @@
567
 
568
         /* 2: Place the frame into the queue for its slice type decision */
569
         x264_lookahead_put_frame( h, fenc );
570
-
571
+
572
+       //V(fprintf(stderr, "x264_encoder_encode setup new frame from picture\n"));
573
+
574
         if( h->frames.i_input <= h->frames.i_delay + 1 - h->param.i_threads )
575
         {
576
             /* Nothing yet to encode, waiting for filling of buffers */
577
             pic_out->i_type = X264_TYPE_AUTO;
578
             return 0;
579
-        }
580
+        }
581
     }
582
     else
583
     {
584
@@ -1873,7 +1903,10 @@
585
     }
586
     else
587
         if( (intptr_t)x264_slices_write( h ) )
588
+         {
589
+           //V(fprintf(stderr, "x264_encoder_encode(): x264_slices_write() failed\n"));
590
             return -1;
591
+         }
592
 
593
     return x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );
594
 }
595
diff -Naur --exclude=.git --exclude=.gitignore x264/encoder/me.c x264-or/encoder/me.c
596
--- x264/encoder/me.c   2009-10-25 17:41:10.000000000 +0100
597
+++ x264-or/encoder/me.c        2009-11-10 13:36:34.000000000 +0100
598
@@ -33,17 +33,17 @@
599
  * the subme=8,9 values are much higher because any amount of satd search makes
600
  * up its time by reducing the number of qpel-rd iterations. */
601
 static const int subpel_iterations[][4] =
602
-   {{0,0,0,0},
603
-    {1,1,0,0},
604
-    {0,1,1,0},
605
-    {0,2,1,0},
606
-    {0,2,1,1},
607
-    {0,2,1,2},
608
-    {0,0,2,2},
609
-    {0,0,2,2},
610
-    {0,0,4,10},
611
-    {0,0,4,10},
612
-    {0,0,4,10}};
613
+  {{0,0,0,0},
614
+   {1,1,0,0},
615
+   {0,1,1,0},
616
+   {0,2,1,0},
617
+   {0,2,1,1},
618
+   {0,2,1,2},
619
+   {0,0,2,2},
620
+   {0,0,2,2},
621
+   {0,0,4,10},
622
+   {0,0,4,10},
623
+   {0,0,4,10}};
624
 
625
 /* (x-1)%6 */
626
 static const int mod6m1[8] = {5,0,1,2,3,4,5,0};
627
@@ -53,1108 +53,1114 @@
628
 
629
 static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel );
630
 
631
-#define BITS_MVD( mx, my )\
632
-    (p_cost_mvx[(mx)<<2] + p_cost_mvy[(my)<<2])
633
+#define BITS_MVD( mx, my )                     \
634
+  (p_cost_mvx[(mx)<<2] + p_cost_mvy[(my)<<2])
635
 
636
-#define COST_MV( mx, my )\
637
-{\
638
-    int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE,\
639
-                   &p_fref[(my)*stride+(mx)], stride )\
640
-             + BITS_MVD(mx,my);\
641
-    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );\
642
-}
643
-
644
-#define COST_MV_HPEL( mx, my ) \
645
-{ \
646
-    int stride2 = 16; \
647
+#define COST_MV( mx, my )                                              \
648
+  {                                                                    \
649
+    int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE,          \
650
+                                        &p_fref[(my)*stride+(mx)], stride ) \
651
+      + BITS_MVD(mx,my);                                               \
652
+    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );                      \
653
+  }
654
+
655
+#define COST_MV_HPEL( mx, my )                                         \
656
+  {                                                                    \
657
+    int stride2 = 16;                                                  \
658
     uint8_t *src = h->mc.get_ref( pix, &stride2, m->p_fref, stride, mx, my, bw, bh ); \
659
     int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, src, stride2 ) \
660
-             + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
661
-    COPY3_IF_LT( bpred_cost, cost, bpred_mx, mx, bpred_my, my ); \
662
-}
663
-
664
-#define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )\
665
-{\
666
-    uint8_t *pix_base = p_fref + bmx + bmy*stride;\
667
-    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
668
-        pix_base + (m0x) + (m0y)*stride,\
669
-        pix_base + (m1x) + (m1y)*stride,\
670
-        pix_base + (m2x) + (m2y)*stride,\
671
-        stride, costs );\
672
-    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
673
-    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
674
-    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
675
-}
676
-
677
-#define COST_MV_X4_DIR( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, costs )\
678
-{\
679
-    uint8_t *pix_base = p_fref + bmx + bmy*stride;\
680
-    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
681
-        pix_base + (m0x) + (m0y)*stride,\
682
-        pix_base + (m1x) + (m1y)*stride,\
683
-        pix_base + (m2x) + (m2y)*stride,\
684
-        pix_base + (m3x) + (m3y)*stride,\
685
-        stride, costs );\
686
-    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
687
-    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
688
-    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
689
-    (costs)[3] += BITS_MVD( bmx+(m3x), bmy+(m3y) );\
690
-}
691
-
692
-#define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\
693
-{\
694
-    uint8_t *pix_base = p_fref + omx + omy*stride;\
695
-    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
696
-        pix_base + (m0x) + (m0y)*stride,\
697
-        pix_base + (m1x) + (m1y)*stride,\
698
-        pix_base + (m2x) + (m2y)*stride,\
699
-        pix_base + (m3x) + (m3y)*stride,\
700
-        stride, costs );\
701
-    costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );\
702
-    costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );\
703
-    costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );\
704
-    costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );\
705
-    COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );\
706
-    COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );\
707
-    COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );\
708
-    COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\
709
-}
710
-
711
-#define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )\
712
-{\
713
-    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
714
-        p_fref + (m0x) + (m0y)*stride,\
715
-        p_fref + (m1x) + (m1y)*stride,\
716
-        p_fref + (m2x) + (m2y)*stride,\
717
-        stride, costs );\
718
-    costs[0] += p_cost_mvx[(m0x)<<2]; /* no cost_mvy */\
719
-    costs[1] += p_cost_mvx[(m1x)<<2];\
720
-    costs[2] += p_cost_mvx[(m2x)<<2];\
721
-    COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\
722
-    COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\
723
-    COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\
724
-}
725
+      + p_cost_mvx[ mx ] + p_cost_mvy[ my ];                           \
726
+    COPY3_IF_LT( bpred_cost, cost, bpred_mx, mx, bpred_my, my );       \
727
+  }
728
+
729
+#define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )          \
730
+  {                                                                    \
731
+    uint8_t *pix_base = p_fref + bmx + bmy*stride;                     \
732
+    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,                               \
733
+                                pix_base + (m0x) + (m0y)*stride,       \
734
+                                pix_base + (m1x) + (m1y)*stride,       \
735
+                                pix_base + (m2x) + (m2y)*stride,       \
736
+                                stride, costs );                       \
737
+    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );                    \
738
+    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );                    \
739
+    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );                    \
740
+  }
741
+
742
+#define COST_MV_X4_DIR( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, costs )        \
743
+  {                                                                    \
744
+    uint8_t *pix_base = p_fref + bmx + bmy*stride;                     \
745
+    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,                               \
746
+                                pix_base + (m0x) + (m0y)*stride,       \
747
+                                pix_base + (m1x) + (m1y)*stride,       \
748
+                                pix_base + (m2x) + (m2y)*stride,       \
749
+                                pix_base + (m3x) + (m3y)*stride,       \
750
+                                stride, costs );                       \
751
+    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );                    \
752
+    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );                    \
753
+    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );                    \
754
+    (costs)[3] += BITS_MVD( bmx+(m3x), bmy+(m3y) );                    \
755
+  }
756
+
757
+#define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )           \
758
+  {                                                                    \
759
+    uint8_t *pix_base = p_fref + omx + omy*stride;                     \
760
+    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,                               \
761
+                                pix_base + (m0x) + (m0y)*stride,       \
762
+                                pix_base + (m1x) + (m1y)*stride,       \
763
+                                pix_base + (m2x) + (m2y)*stride,       \
764
+                                pix_base + (m3x) + (m3y)*stride,       \
765
+                                stride, costs );                       \
766
+    costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );                      \
767
+    costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );                      \
768
+    costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );                      \
769
+    costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );                      \
770
+    COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );    \
771
+    COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );    \
772
+    COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );    \
773
+    COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );    \
774
+  }
775
+
776
+#define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )         \
777
+  {                                                            \
778
+    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,                       \
779
+                                p_fref + (m0x) + (m0y)*stride, \
780
+                                p_fref + (m1x) + (m1y)*stride, \
781
+                                p_fref + (m2x) + (m2y)*stride, \
782
+                                stride, costs );               \
783
+    costs[0] += p_cost_mvx[(m0x)<<2]; /* no cost_mvy */                \
784
+    costs[1] += p_cost_mvx[(m1x)<<2];                          \
785
+    costs[2] += p_cost_mvx[(m2x)<<2];                          \
786
+    COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );                \
787
+    COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );                \
788
+    COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );                \
789
+  }
790
 
791
 /*  1  */
792
 /* 101 */
793
 /*  1  */
794
-#define DIA1_ITER( mx, my )\
795
-{\
796
-    omx = mx; omy = my;\
797
-    COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );\
798
-}
799
-
800
-#define CROSS( start, x_max, y_max )\
801
-{\
802
-    i = start;\
803
-    if( x_max <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )\
804
-        for( ; i < x_max-2; i+=4 )\
805
-            COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );\
806
-    for( ; i < x_max; i+=2 )\
807
-    {\
808
-        if( omx+i <= mv_x_max )\
809
-            COST_MV( omx+i, omy );\
810
-        if( omx-i >= mv_x_min )\
811
-            COST_MV( omx-i, omy );\
812
-    }\
813
-    i = start;\
814
-    if( y_max <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )\
815
-        for( ; i < y_max-2; i+=4 )\
816
-            COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );\
817
-    for( ; i < y_max; i+=2 )\
818
-    {\
819
-        if( omy+i <= mv_y_max )\
820
-            COST_MV( omx, omy+i );\
821
-        if( omy-i >= mv_y_min )\
822
-            COST_MV( omx, omy-i );\
823
-    }\
824
-}
825
+#define DIA1_ITER( mx, my )                    \
826
+  {                                            \
827
+    omx = mx; omy = my;                                \
828
+    COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );                \
829
+  }
830
+
831
+#define CROSS( start, x_max, y_max )                   \
832
+  {                                                    \
833
+    i = start;                                         \
834
+    if( x_max <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )        \
835
+      for( ; i < x_max-2; i+=4 )                       \
836
+       COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );         \
837
+    for( ; i < x_max; i+=2 )                           \
838
+      {                                                        \
839
+        if( omx+i <= mv_x_max )                                \
840
+         COST_MV( omx+i, omy );                        \
841
+        if( omx-i >= mv_x_min )                                \
842
+         COST_MV( omx-i, omy );                        \
843
+      }                                                        \
844
+    i = start;                                         \
845
+    if( y_max <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )        \
846
+      for( ; i < y_max-2; i+=4 )                       \
847
+       COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );         \
848
+    for( ; i < y_max; i+=2 )                           \
849
+      {                                                        \
850
+        if( omy+i <= mv_y_max )                                \
851
+         COST_MV( omx, omy+i );                        \
852
+        if( omy-i >= mv_y_min )                                \
853
+         COST_MV( omx, omy-i );                        \
854
+      }                                                        \
855
+  }
856
 
857
 void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc, int *p_halfpel_thresh )
858
 {
859
-    const int bw = x264_pixel_size[m->i_pixel].w;
860
-    const int bh = x264_pixel_size[m->i_pixel].h;
861
-    const int i_pixel = m->i_pixel;
862
-    const int stride = m->i_stride[0];
863
-    int i_me_range = h->param.analyse.i_me_range;
864
-    int bmx, bmy, bcost;
865
-    int bpred_mx = 0, bpred_my = 0, bpred_cost = COST_MAX;
866
-    int omx, omy, pmx, pmy;
867
-    uint8_t *p_fenc = m->p_fenc[0];
868
-    uint8_t *p_fref = m->p_fref[0];
869
-    ALIGNED_ARRAY_16( uint8_t, pix,[16*16] );
870
-
871
-    int i, j;
872
-    int dir;
873
-    int costs[16];
874
-
875
-    int mv_x_min = h->mb.mv_min_fpel[0];
876
-    int mv_y_min = h->mb.mv_min_fpel[1];
877
-    int mv_x_max = h->mb.mv_max_fpel[0];
878
-    int mv_y_max = h->mb.mv_max_fpel[1];
879
+  const int bw = x264_pixel_size[m->i_pixel].w; // x264_pixel_size[] in common/pixel.h
880
+  const int bh = x264_pixel_size[m->i_pixel].h;
881
+  const int i_pixel = m->i_pixel; // macroblock pixel size
882
+  const int stride = m->i_stride[0]; // array stride (usually used only in Y direction)
883
+  int i_me_range = h->param.analyse.i_me_range; /* integer pixel motion estimation search range (from predicted mv) */
884
+  int bmx, bmy, bcost;
885
+  int bpred_mx = 0, bpred_my = 0, bpred_cost = COST_MAX;
886
+  int omx, omy, pmx, pmy;
887
+  uint8_t *p_fenc = m->p_fenc[0];
888
+  uint8_t *p_fref = m->p_fref[0];
889
+  //V(fprintf(stderr, "s264_me_search_ref *p_fenc = 0x%.8x *p_fref = 0x%.8x\n", (unsigned long) p_fenc, (unsigned long) p_fref));
890
+  ALIGNED_ARRAY_16( uint8_t, pix,[16*16] );
891
+
892
+  int i, j;
893
+  int dir;
894
+  int costs[16];
895
+
896
+  //fprintf(stderr, "x264_me_search_ref: x264_t *h = 0x%.8x\n", (unsigned int) h);
897
+  /* Ranges for MVs - perhaps we're on/near the frame boarder and can't go beyond it, these ranges indicate that. They're set in x264_mb_analyse_init() */
898
+  int mv_x_min = h->mb.mv_min_fpel[0];
899
+  int mv_y_min = h->mb.mv_min_fpel[1];
900
+  int mv_x_max = h->mb.mv_max_fpel[0];
901
+  int mv_y_max = h->mb.mv_max_fpel[1];
902
 
903
 #define CHECK_MVRANGE(mx,my) ( mx >= mv_x_min && mx <= mv_x_max && my >= mv_y_min && my <= mv_y_max )
904
 
905
-    const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
906
-    const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
907
-
908
-    bmx = x264_clip3( m->mvp[0], mv_x_min*4, mv_x_max*4 );
909
-    bmy = x264_clip3( m->mvp[1], mv_y_min*4, mv_y_max*4 );
910
-    pmx = ( bmx + 2 ) >> 2;
911
-    pmy = ( bmy + 2 ) >> 2;
912
-    bcost = COST_MAX;
913
+  const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
914
+  const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
915
+  //V(fprintf(stderr, "p_cost_mvx: 0x%.8x, ( m->p_cost_mv: 0x%.8x m->mvp[0]: 0x%.8x )p_cost_mvy: 0x%.8x (m->p_cost_mv: 0x%.8x  m->mvp[1]: 0x%.8x)\n", (unsigned long) p_cost_mvx, (unsigned long) m->p_cost_mv, (unsigned long) m->mvp[0], (unsigned long) p_cost_mvy, (unsigned long) m->p_cost_mv, (unsigned long) m->mvp[1]));
916
+
917
+  /* m->mvp has the media MV (x in [0] and y in [1]) */
918
+  bmx = x264_clip3( m->mvp[0], mv_x_min*4, mv_x_max*4 );
919
+  bmy = x264_clip3( m->mvp[1], mv_y_min*4, mv_y_max*4 );
920
+  pmx = ( bmx + 2 ) >> 2;
921
+  pmy = ( bmy + 2 ) >> 2;
922
+  bcost = COST_MAX;
923
 
924
-    /* try extra predictors if provided */
925
-    if( h->mb.i_subpel_refine >= 3 )
926
+  /* try extra predictors if provided */
927
+  if( h->mb.i_subpel_refine >= 3 )
928
     {
929
-        uint32_t bmv = pack16to32_mask(bmx,bmy);
930
-        COST_MV_HPEL( bmx, bmy );
931
-        for( i = 0; i < i_mvc; i++ )
932
+      uint32_t bmv = pack16to32_mask(bmx,bmy);
933
+      COST_MV_HPEL( bmx, bmy );
934
+      for( i = 0; i < i_mvc; i++ )
935
         {
936
-            if( *(uint32_t*)mvc[i] && (bmv - *(uint32_t*)mvc[i]) )
937
+         if( *(uint32_t*)mvc[i] && (bmv - *(uint32_t*)mvc[i]) )
938
             {
939
-                int mx = x264_clip3( mvc[i][0], mv_x_min*4, mv_x_max*4 );
940
-                int my = x264_clip3( mvc[i][1], mv_y_min*4, mv_y_max*4 );
941
-                COST_MV_HPEL( mx, my );
942
+             int mx = x264_clip3( mvc[i][0], mv_x_min*4, mv_x_max*4 );
943
+             int my = x264_clip3( mvc[i][1], mv_y_min*4, mv_y_max*4 );
944
+             COST_MV_HPEL( mx, my );
945
             }
946
         }
947
-        bmx = ( bpred_mx + 2 ) >> 2;
948
-        bmy = ( bpred_my + 2 ) >> 2;
949
-        COST_MV( bmx, bmy );
950
+      bmx = ( bpred_mx + 2 ) >> 2;
951
+      bmy = ( bpred_my + 2 ) >> 2;
952
+      COST_MV( bmx, bmy );
953
     }
954
-    else
955
+  else
956
     {
957
-        /* check the MVP */
958
-        COST_MV( pmx, pmy );
959
-        /* Because we are rounding the predicted motion vector to fullpel, there will be
960
-         * an extra MV cost in 15 out of 16 cases.  However, when the predicted MV is
961
-         * chosen as the best predictor, it is often the case that the subpel search will
962
-         * result in a vector at or next to the predicted motion vector.  Therefore, it is
963
-         * sensible to remove the cost of the MV from the rounded MVP to avoid unfairly
964
-         * biasing against use of the predicted motion vector. */
965
-        bcost -= BITS_MVD( pmx, pmy );
966
-        for( i = 0; i < i_mvc; i++ )
967
+      /* check the MVP */
968
+      COST_MV( pmx, pmy );
969
+      /* Because we are rounding the predicted motion vector to fullpel, there will be
970
+       * an extra MV cost in 15 out of 16 cases.  However, when the predicted MV is
971
+       * chosen as the best predictor, it is often the case that the subpel search will
972
+       * result in a vector at or next to the predicted motion vector.  Therefore, it is
973
+       * sensible to remove the cost of the MV from the rounded MVP to avoid unfairly
974
+       * biasing against use of the predicted motion vector. */
975
+      bcost -= BITS_MVD( pmx, pmy );
976
+      for( i = 0; i < i_mvc; i++ )
977
         {
978
-            int mx = (mvc[i][0] + 2) >> 2;
979
-            int my = (mvc[i][1] + 2) >> 2;
980
-            if( (mx | my) && ((mx-bmx) | (my-bmy)) )
981
+         int mx = (mvc[i][0] + 2) >> 2;
982
+         int my = (mvc[i][1] + 2) >> 2;
983
+         if( (mx | my) && ((mx-bmx) | (my-bmy)) )
984
             {
985
-                mx = x264_clip3( mx, mv_x_min, mv_x_max );
986
-                my = x264_clip3( my, mv_y_min, mv_y_max );
987
-                COST_MV( mx, my );
988
+             mx = x264_clip3( mx, mv_x_min, mv_x_max );
989
+             my = x264_clip3( my, mv_y_min, mv_y_max );
990
+             COST_MV( mx, my );
991
             }
992
         }
993
     }
994
-    COST_MV( 0, 0 );
995
+  COST_MV( 0, 0 );
996
 
997
-    switch( h->mb.i_me_method )
998
+  switch( h->mb.i_me_method )
999
     {
1000
     case X264_ME_DIA:
1001
-        /* diamond search, radius 1 */
1002
-        i = 0;
1003
-        bcost <<= 4;
1004
-        do
1005
+      /* diamond search, radius 1 */
1006
+      //V(fprintf(stderr, "x264_me_search_ref; diamond search. *p_fenc = 0x%.8x *p_fref = 0x%.8x\n", (unsigned long) p_fenc, (unsigned long) p_fref));
1007
+      i = 0;
1008
+      bcost <<= 4;
1009
+      do
1010
         {
1011
-            COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );
1012
-            COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
1013
-            COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
1014
-            COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
1015
-            COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
1016
-            if( !(bcost&15) )
1017
-                break;
1018
-            bmx -= (bcost<<28)>>30;
1019
-            bmy -= (bcost<<30)>>30;
1020
-            bcost &= ~15;
1021
-            if( !CHECK_MVRANGE(bmx, bmy) )
1022
-                break;
1023
+         COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );
1024
+         COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
1025
+         COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
1026
+         COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
1027
+         COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
1028
+         if( !(bcost&15) )
1029
+           break;
1030
+         bmx -= (bcost<<28)>>30;
1031
+         bmy -= (bcost<<30)>>30;
1032
+         bcost &= ~15;
1033
+         if( !CHECK_MVRANGE(bmx, bmy) )
1034
+           break;
1035
         } while( ++i < i_me_range );
1036
-        bcost >>= 4;
1037
-        break;
1038
+      bcost >>= 4;
1039
+      break;
1040
 
1041
     case X264_ME_HEX:
1042
-me_hex2:
1043
-        /* hexagon search, radius 2 */
1044
+    me_hex2:
1045
+      /* hexagon search, radius 2 */
1046
 #if 0
1047
-        for( i = 0; i < i_me_range/2; i++ )
1048
+      for( i = 0; i < i_me_range/2; i++ )
1049
         {
1050
-            omx = bmx; omy = bmy;
1051
-            COST_MV( omx-2, omy   );
1052
-            COST_MV( omx-1, omy+2 );
1053
-            COST_MV( omx+1, omy+2 );
1054
-            COST_MV( omx+2, omy   );
1055
-            COST_MV( omx+1, omy-2 );
1056
-            COST_MV( omx-1, omy-2 );
1057
-            if( bmx == omx && bmy == omy )
1058
-                break;
1059
-            if( !CHECK_MVRANGE(bmx, bmy) )
1060
-                break;
1061
+         omx = bmx; omy = bmy;
1062
+         COST_MV( omx-2, omy   );
1063
+         COST_MV( omx-1, omy+2 );
1064
+         COST_MV( omx+1, omy+2 );
1065
+         COST_MV( omx+2, omy   );
1066
+         COST_MV( omx+1, omy-2 );
1067
+         COST_MV( omx-1, omy-2 );
1068
+         if( bmx == omx && bmy == omy )
1069
+           break;
1070
+         if( !CHECK_MVRANGE(bmx, bmy) )
1071
+           break;
1072
         }
1073
 #else
1074
-        /* equivalent to the above, but eliminates duplicate candidates */
1075
+      /* equivalent to the above, but eliminates duplicate candidates */
1076
 
1077
-        /* hexagon */
1078
-        COST_MV_X3_DIR( -2,0, -1, 2,  1, 2, costs   );
1079
-        COST_MV_X3_DIR(  2,0,  1,-2, -1,-2, costs+3 );
1080
-        bcost <<= 3;
1081
-        COPY1_IF_LT( bcost, (costs[0]<<3)+2 );
1082
-        COPY1_IF_LT( bcost, (costs[1]<<3)+3 );
1083
-        COPY1_IF_LT( bcost, (costs[2]<<3)+4 );
1084
-        COPY1_IF_LT( bcost, (costs[3]<<3)+5 );
1085
-        COPY1_IF_LT( bcost, (costs[4]<<3)+6 );
1086
-        COPY1_IF_LT( bcost, (costs[5]<<3)+7 );
1087
+      /* hexagon */
1088
+      COST_MV_X3_DIR( -2,0, -1, 2,  1, 2, costs   );
1089
+      COST_MV_X3_DIR(  2,0,  1,-2, -1,-2, costs+3 );
1090
+      bcost <<= 3;
1091
+      COPY1_IF_LT( bcost, (costs[0]<<3)+2 );
1092
+      COPY1_IF_LT( bcost, (costs[1]<<3)+3 );
1093
+      COPY1_IF_LT( bcost, (costs[2]<<3)+4 );
1094
+      COPY1_IF_LT( bcost, (costs[3]<<3)+5 );
1095
+      COPY1_IF_LT( bcost, (costs[4]<<3)+6 );
1096
+      COPY1_IF_LT( bcost, (costs[5]<<3)+7 );
1097
 
1098
-        if( bcost&7 )
1099
+      if( bcost&7 )
1100
         {
1101
-            dir = (bcost&7)-2;
1102
-            bmx += hex2[dir+1][0];
1103
-            bmy += hex2[dir+1][1];
1104
-            /* half hexagon, not overlapping the previous iteration */
1105
-            for( i = 1; i < i_me_range/2 && CHECK_MVRANGE(bmx, bmy); i++ )
1106
+         dir = (bcost&7)-2;
1107
+         bmx += hex2[dir+1][0];
1108
+         bmy += hex2[dir+1][1];
1109
+         /* half hexagon, not overlapping the previous iteration */
1110
+         for( i = 1; i < i_me_range/2 && CHECK_MVRANGE(bmx, bmy); i++ )
1111
             {
1112
-                COST_MV_X3_DIR( hex2[dir+0][0], hex2[dir+0][1],
1113
-                                hex2[dir+1][0], hex2[dir+1][1],
1114
-                                hex2[dir+2][0], hex2[dir+2][1],
1115
-                                costs );
1116
-                bcost &= ~7;
1117
-                COPY1_IF_LT( bcost, (costs[0]<<3)+1 );
1118
-                COPY1_IF_LT( bcost, (costs[1]<<3)+2 );
1119
-                COPY1_IF_LT( bcost, (costs[2]<<3)+3 );
1120
-                if( !(bcost&7) )
1121
-                    break;
1122
-                dir += (bcost&7)-2;
1123
-                dir = mod6m1[dir+1];
1124
-                bmx += hex2[dir+1][0];
1125
-                bmy += hex2[dir+1][1];
1126
+             COST_MV_X3_DIR( hex2[dir+0][0], hex2[dir+0][1],
1127
+                             hex2[dir+1][0], hex2[dir+1][1],
1128
+                             hex2[dir+2][0], hex2[dir+2][1],
1129
+                             costs );
1130
+             bcost &= ~7;
1131
+             COPY1_IF_LT( bcost, (costs[0]<<3)+1 );
1132
+             COPY1_IF_LT( bcost, (costs[1]<<3)+2 );
1133
+             COPY1_IF_LT( bcost, (costs[2]<<3)+3 );
1134
+             if( !(bcost&7) )
1135
+               break;
1136
+             dir += (bcost&7)-2;
1137
+             dir = mod6m1[dir+1];
1138
+             bmx += hex2[dir+1][0];
1139
+             bmy += hex2[dir+1][1];
1140
             }
1141
         }
1142
-        bcost >>= 3;
1143
+      bcost >>= 3;
1144
 #endif
1145
-        /* square refine */
1146
-        dir = 0;
1147
-        COST_MV_X4_DIR(  0,-1,  0,1, -1,0, 1,0, costs );
1148
-        COPY2_IF_LT( bcost, costs[0], dir, 1 );
1149
-        COPY2_IF_LT( bcost, costs[1], dir, 2 );
1150
-        COPY2_IF_LT( bcost, costs[2], dir, 3 );
1151
-        COPY2_IF_LT( bcost, costs[3], dir, 4 );
1152
-        COST_MV_X4_DIR( -1,-1, -1,1, 1,-1, 1,1, costs );
1153
-        COPY2_IF_LT( bcost, costs[0], dir, 5 );
1154
-        COPY2_IF_LT( bcost, costs[1], dir, 6 );
1155
-        COPY2_IF_LT( bcost, costs[2], dir, 7 );
1156
-        COPY2_IF_LT( bcost, costs[3], dir, 8 );
1157
-        bmx += square1[dir][0];
1158
-        bmy += square1[dir][1];
1159
-        break;
1160
+      /* square refine */
1161
+      dir = 0;
1162
+      COST_MV_X4_DIR(  0,-1,  0,1, -1,0, 1,0, costs );
1163
+      COPY2_IF_LT( bcost, costs[0], dir, 1 );
1164
+      COPY2_IF_LT( bcost, costs[1], dir, 2 );
1165
+      COPY2_IF_LT( bcost, costs[2], dir, 3 );
1166
+      COPY2_IF_LT( bcost, costs[3], dir, 4 );
1167
+      COST_MV_X4_DIR( -1,-1, -1,1, 1,-1, 1,1, costs );
1168
+      COPY2_IF_LT( bcost, costs[0], dir, 5 );
1169
+      COPY2_IF_LT( bcost, costs[1], dir, 6 );
1170
+      COPY2_IF_LT( bcost, costs[2], dir, 7 );
1171
+      COPY2_IF_LT( bcost, costs[3], dir, 8 );
1172
+      bmx += square1[dir][0];
1173
+      bmy += square1[dir][1];
1174
+      break;
1175
 
1176
     case X264_ME_UMH:
1177
-        {
1178
-            /* Uneven-cross Multi-Hexagon-grid Search
1179
-             * as in JM, except with different early termination */
1180
-
1181
-            static const int x264_pixel_size_shift[7] = { 0, 1, 1, 2, 3, 3, 4 };
1182
-
1183
-            int ucost1, ucost2;
1184
-            int cross_start = 1;
1185
+      {
1186
+       /* Uneven-cross Multi-Hexagon-grid Search
1187
+        * as in JM, except with different early termination */
1188
+
1189
+       static const int x264_pixel_size_shift[7] = { 0, 1, 1, 2, 3, 3, 4 };
1190
+
1191
+       int ucost1, ucost2;
1192
+       int cross_start = 1;
1193
+
1194
+       /* refine predictors */
1195
+       ucost1 = bcost;
1196
+       DIA1_ITER( pmx, pmy );
1197
+       if( pmx | pmy )
1198
+         DIA1_ITER( 0, 0 );
1199
+
1200
+       if(i_pixel == PIXEL_4x4)
1201
+         goto me_hex2;
1202
+
1203
+       ucost2 = bcost;
1204
+       if( (bmx | bmy) && ((bmx-pmx) | (bmy-pmy)) )
1205
+         DIA1_ITER( bmx, bmy );
1206
+       if( bcost == ucost2 )
1207
+         cross_start = 3;
1208
+       omx = bmx; omy = bmy;
1209
 
1210
-            /* refine predictors */
1211
-            ucost1 = bcost;
1212
-            DIA1_ITER( pmx, pmy );
1213
-            if( pmx | pmy )
1214
-                DIA1_ITER( 0, 0 );
1215
-
1216
-            if(i_pixel == PIXEL_4x4)
1217
-                goto me_hex2;
1218
-
1219
-            ucost2 = bcost;
1220
-            if( (bmx | bmy) && ((bmx-pmx) | (bmy-pmy)) )
1221
-                DIA1_ITER( bmx, bmy );
1222
-            if( bcost == ucost2 )
1223
-                cross_start = 3;
1224
-            omx = bmx; omy = bmy;
1225
-
1226
-            /* early termination */
1227
+       /* early termination */
1228
 #define SAD_THRESH(v) ( bcost < ( v >> x264_pixel_size_shift[i_pixel] ) )
1229
-            if( bcost == ucost2 && SAD_THRESH(2000) )
1230
-            {
1231
-                COST_MV_X4( 0,-2, -1,-1, 1,-1, -2,0 );
1232
-                COST_MV_X4( 2, 0, -1, 1, 1, 1,  0,2 );
1233
-                if( bcost == ucost1 && SAD_THRESH(500) )
1234
-                    break;
1235
-                if( bcost == ucost2 )
1236
-                {
1237
-                    int range = (i_me_range>>1) | 1;
1238
-                    CROSS( 3, range, range );
1239
-                    COST_MV_X4( -1,-2, 1,-2, -2,-1, 2,-1 );
1240
-                    COST_MV_X4( -2, 1, 2, 1, -1, 2, 1, 2 );
1241
-                    if( bcost == ucost2 )
1242
-                        break;
1243
-                    cross_start = range + 2;
1244
-                }
1245
-            }
1246
-
1247
-            /* adaptive search range */
1248
-            if( i_mvc )
1249
-            {
1250
-                /* range multipliers based on casual inspection of some statistics of
1251
-                 * average distance between current predictor and final mv found by ESA.
1252
-                 * these have not been tuned much by actual encoding. */
1253
-                static const int range_mul[4][4] =
1254
-                {
1255
-                    { 3, 3, 4, 4 },
1256
-                    { 3, 4, 4, 4 },
1257
-                    { 4, 4, 4, 5 },
1258
-                    { 4, 4, 5, 6 },
1259
-                };
1260
-                int mvd;
1261
-                int sad_ctx, mvd_ctx;
1262
-                int denom = 1;
1263
-
1264
-                if( i_mvc == 1 )
1265
-                {
1266
-                    if( i_pixel == PIXEL_16x16 )
1267
-                        /* mvc is probably the same as mvp, so the difference isn't meaningful.
1268
-                         * but prediction usually isn't too bad, so just use medium range */
1269
-                        mvd = 25;
1270
-                    else
1271
-                        mvd = abs( m->mvp[0] - mvc[0][0] )
1272
-                            + abs( m->mvp[1] - mvc[0][1] );
1273
-                }
1274
-                else
1275
-                {
1276
-                    /* calculate the degree of agreement between predictors. */
1277
-                    /* in 16x16, mvc includes all the neighbors used to make mvp,
1278
-                     * so don't count mvp separately. */
1279
-                    denom = i_mvc - 1;
1280
-                    mvd = 0;
1281
-                    if( i_pixel != PIXEL_16x16 )
1282
-                    {
1283
-                        mvd = abs( m->mvp[0] - mvc[0][0] )
1284
-                            + abs( m->mvp[1] - mvc[0][1] );
1285
-                        denom++;
1286
-                    }
1287
-                    mvd += x264_predictor_difference( mvc, i_mvc );
1288
-                }
1289
-
1290
-                sad_ctx = SAD_THRESH(1000) ? 0
1291
-                        : SAD_THRESH(2000) ? 1
1292
-                        : SAD_THRESH(4000) ? 2 : 3;
1293
-                mvd_ctx = mvd < 10*denom ? 0
1294
-                        : mvd < 20*denom ? 1
1295
-                        : mvd < 40*denom ? 2 : 3;
1296
-
1297
-                i_me_range = i_me_range * range_mul[mvd_ctx][sad_ctx] / 4;
1298
-            }
1299
-
1300
-            /* FIXME if the above DIA2/OCT2/CROSS found a new mv, it has not updated omx/omy.
1301
-             * we are still centered on the same place as the DIA2. is this desirable? */
1302
-            CROSS( cross_start, i_me_range, i_me_range/2 );
1303
-
1304
-            COST_MV_X4( -2,-2, -2,2, 2,-2, 2,2 );
1305
-
1306
-            /* hexagon grid */
1307
-            omx = bmx; omy = bmy;
1308
-            const uint16_t *p_cost_omvx = p_cost_mvx + omx*4;
1309
-            const uint16_t *p_cost_omvy = p_cost_mvy + omy*4;
1310
-            i = 1;
1311
-            do
1312
-            {
1313
-                static const int hex4[16][2] = {
1314
-                    { 0,-4}, { 0, 4}, {-2,-3}, { 2,-3},
1315
-                    {-4,-2}, { 4,-2}, {-4,-1}, { 4,-1},
1316
-                    {-4, 0}, { 4, 0}, {-4, 1}, { 4, 1},
1317
-                    {-4, 2}, { 4, 2}, {-2, 3}, { 2, 3},
1318
-                };
1319
-
1320
-                if( 4*i > X264_MIN4( mv_x_max-omx, omx-mv_x_min,
1321
-                                     mv_y_max-omy, omy-mv_y_min ) )
1322
-                {
1323
-                    for( j = 0; j < 16; j++ )
1324
-                    {
1325
-                        int mx = omx + hex4[j][0]*i;
1326
-                        int my = omy + hex4[j][1]*i;
1327
-                        if( CHECK_MVRANGE(mx, my) )
1328
-                            COST_MV( mx, my );
1329
-                    }
1330
-                }
1331
-                else
1332
-                {
1333
-                    int dir = 0;
1334
-                    uint8_t *pix_base = p_fref + omx + (omy-4*i)*stride;
1335
-                    int dy = i*stride;
1336
-#define SADS(k,x0,y0,x1,y1,x2,y2,x3,y3)\
1337
-                    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
1338
-                            pix_base x0*i+(y0-2*k+4)*dy,\
1339
-                            pix_base x1*i+(y1-2*k+4)*dy,\
1340
-                            pix_base x2*i+(y2-2*k+4)*dy,\
1341
-                            pix_base x3*i+(y3-2*k+4)*dy,\
1342
-                            stride, costs+4*k );\
1343
-                    pix_base += 2*dy;
1344
+       if( bcost == ucost2 && SAD_THRESH(2000) )
1345
+         {
1346
+           COST_MV_X4( 0,-2, -1,-1, 1,-1, -2,0 );
1347
+           COST_MV_X4( 2, 0, -1, 1, 1, 1,  0,2 );
1348
+           if( bcost == ucost1 && SAD_THRESH(500) )
1349
+             break;
1350
+           if( bcost == ucost2 )
1351
+             {
1352
+               int range = (i_me_range>>1) | 1;
1353
+               CROSS( 3, range, range );
1354
+               COST_MV_X4( -1,-2, 1,-2, -2,-1, 2,-1 );
1355
+               COST_MV_X4( -2, 1, 2, 1, -1, 2, 1, 2 );
1356
+               if( bcost == ucost2 )
1357
+                 break;
1358
+               cross_start = range + 2;
1359
+             }
1360
+         }
1361
+
1362
+       /* adaptive search range */
1363
+       if( i_mvc )
1364
+         {
1365
+           /* range multipliers based on casual inspection of some statistics of
1366
+            * average distance between current predictor and final mv found by ESA.
1367
+            * these have not been tuned much by actual encoding. */
1368
+           static const int range_mul[4][4] =
1369
+             {
1370
+               { 3, 3, 4, 4 },
1371
+               { 3, 4, 4, 4 },
1372
+               { 4, 4, 4, 5 },
1373
+               { 4, 4, 5, 6 },
1374
+             };
1375
+           int mvd;
1376
+           int sad_ctx, mvd_ctx;
1377
+           int denom = 1;
1378
+
1379
+           if( i_mvc == 1 )
1380
+             {
1381
+               if( i_pixel == PIXEL_16x16 )
1382
+                 /* mvc is probably the same as mvp, so the difference isn't meaningful.
1383
+                  * but prediction usually isn't too bad, so just use medium range */
1384
+                 mvd = 25;
1385
+               else
1386
+                 mvd = abs( m->mvp[0] - mvc[0][0] )
1387
+                   + abs( m->mvp[1] - mvc[0][1] );
1388
+             }
1389
+           else
1390
+             {
1391
+               /* calculate the degree of agreement between predictors. */
1392
+               /* in 16x16, mvc includes all the neighbors used to make mvp,
1393
+                * so don't count mvp separately. */
1394
+               denom = i_mvc - 1;
1395
+               mvd = 0;
1396
+               if( i_pixel != PIXEL_16x16 )
1397
+                 {
1398
+                   mvd = abs( m->mvp[0] - mvc[0][0] )
1399
+                     + abs( m->mvp[1] - mvc[0][1] );
1400
+                   denom++;
1401
+                 }
1402
+               mvd += x264_predictor_difference( mvc, i_mvc );
1403
+             }
1404
+
1405
+           sad_ctx = SAD_THRESH(1000) ? 0
1406
+             : SAD_THRESH(2000) ? 1
1407
+             : SAD_THRESH(4000) ? 2 : 3;
1408
+           mvd_ctx = mvd < 10*denom ? 0
1409
+             : mvd < 20*denom ? 1
1410
+             : mvd < 40*denom ? 2 : 3;
1411
+
1412
+           i_me_range = i_me_range * range_mul[mvd_ctx][sad_ctx] / 4;
1413
+         }
1414
+
1415
+       /* FIXME if the above DIA2/OCT2/CROSS found a new mv, it has not updated omx/omy.
1416
+        * we are still centered on the same place as the DIA2. is this desirable? */
1417
+       CROSS( cross_start, i_me_range, i_me_range/2 );
1418
+
1419
+       COST_MV_X4( -2,-2, -2,2, 2,-2, 2,2 );
1420
+
1421
+       /* hexagon grid */
1422
+       omx = bmx; omy = bmy;
1423
+       const uint16_t *p_cost_omvx = p_cost_mvx + omx*4;
1424
+       const uint16_t *p_cost_omvy = p_cost_mvy + omy*4;
1425
+       i = 1;
1426
+       do
1427
+         {
1428
+           static const int hex4[16][2] = {
1429
+             { 0,-4}, { 0, 4}, {-2,-3}, { 2,-3},
1430
+             {-4,-2}, { 4,-2}, {-4,-1}, { 4,-1},
1431
+             {-4, 0}, { 4, 0}, {-4, 1}, { 4, 1},
1432
+             {-4, 2}, { 4, 2}, {-2, 3}, { 2, 3},
1433
+           };
1434
+
1435
+           if( 4*i > X264_MIN4( mv_x_max-omx, omx-mv_x_min,
1436
+                                mv_y_max-omy, omy-mv_y_min ) )
1437
+             {
1438
+               for( j = 0; j < 16; j++ )
1439
+                 {
1440
+                   int mx = omx + hex4[j][0]*i;
1441
+                   int my = omy + hex4[j][1]*i;
1442
+                   if( CHECK_MVRANGE(mx, my) )
1443
+                     COST_MV( mx, my );
1444
+                 }
1445
+             }
1446
+           else
1447
+             {
1448
+               int dir = 0;
1449
+               uint8_t *pix_base = p_fref + omx + (omy-4*i)*stride;
1450
+               int dy = i*stride;
1451
+#define SADS(k,x0,y0,x1,y1,x2,y2,x3,y3)                                        \
1452
+               h->pixf.fpelcmp_x4[i_pixel]( p_fenc,                    \
1453
+                                            pix_base x0*i+(y0-2*k+4)*dy, \
1454
+                                            pix_base x1*i+(y1-2*k+4)*dy, \
1455
+                                            pix_base x2*i+(y2-2*k+4)*dy, \
1456
+                                            pix_base x3*i+(y3-2*k+4)*dy, \
1457
+                                            stride, costs+4*k );       \
1458
+               pix_base += 2*dy;
1459
 #define ADD_MVCOST(k,x,y) costs[k] += p_cost_omvx[x*4*i] + p_cost_omvy[y*4*i]
1460
 #define MIN_MV(k,x,y)     COPY2_IF_LT( bcost, costs[k], dir, x*16+(y&15) )
1461
-                    SADS( 0, +0,-4, +0,+4, -2,-3, +2,-3 );
1462
-                    SADS( 1, -4,-2, +4,-2, -4,-1, +4,-1 );
1463
-                    SADS( 2, -4,+0, +4,+0, -4,+1, +4,+1 );
1464
-                    SADS( 3, -4,+2, +4,+2, -2,+3, +2,+3 );
1465
-                    ADD_MVCOST(  0, 0,-4 );
1466
-                    ADD_MVCOST(  1, 0, 4 );
1467
-                    ADD_MVCOST(  2,-2,-3 );
1468
-                    ADD_MVCOST(  3, 2,-3 );
1469
-                    ADD_MVCOST(  4,-4,-2 );
1470
-                    ADD_MVCOST(  5, 4,-2 );
1471
-                    ADD_MVCOST(  6,-4,-1 );
1472
-                    ADD_MVCOST(  7, 4,-1 );
1473
-                    ADD_MVCOST(  8,-4, 0 );
1474
-                    ADD_MVCOST(  9, 4, 0 );
1475
-                    ADD_MVCOST( 10,-4, 1 );
1476
-                    ADD_MVCOST( 11, 4, 1 );
1477
-                    ADD_MVCOST( 12,-4, 2 );
1478
-                    ADD_MVCOST( 13, 4, 2 );
1479
-                    ADD_MVCOST( 14,-2, 3 );
1480
-                    ADD_MVCOST( 15, 2, 3 );
1481
-                    MIN_MV(  0, 0,-4 );
1482
-                    MIN_MV(  1, 0, 4 );
1483
-                    MIN_MV(  2,-2,-3 );
1484
-                    MIN_MV(  3, 2,-3 );
1485
-                    MIN_MV(  4,-4,-2 );
1486
-                    MIN_MV(  5, 4,-2 );
1487
-                    MIN_MV(  6,-4,-1 );
1488
-                    MIN_MV(  7, 4,-1 );
1489
-                    MIN_MV(  8,-4, 0 );
1490
-                    MIN_MV(  9, 4, 0 );
1491
-                    MIN_MV( 10,-4, 1 );
1492
-                    MIN_MV( 11, 4, 1 );
1493
-                    MIN_MV( 12,-4, 2 );
1494
-                    MIN_MV( 13, 4, 2 );
1495
-                    MIN_MV( 14,-2, 3 );
1496
-                    MIN_MV( 15, 2, 3 );
1497
+               SADS( 0, +0,-4, +0,+4, -2,-3, +2,-3 );
1498
+               SADS( 1, -4,-2, +4,-2, -4,-1, +4,-1 );
1499
+               SADS( 2, -4,+0, +4,+0, -4,+1, +4,+1 );
1500
+               SADS( 3, -4,+2, +4,+2, -2,+3, +2,+3 );
1501
+               ADD_MVCOST(  0, 0,-4 );
1502
+               ADD_MVCOST(  1, 0, 4 );
1503
+               ADD_MVCOST(  2,-2,-3 );
1504
+               ADD_MVCOST(  3, 2,-3 );
1505
+               ADD_MVCOST(  4,-4,-2 );
1506
+               ADD_MVCOST(  5, 4,-2 );
1507
+               ADD_MVCOST(  6,-4,-1 );
1508
+               ADD_MVCOST(  7, 4,-1 );
1509
+               ADD_MVCOST(  8,-4, 0 );
1510
+               ADD_MVCOST(  9, 4, 0 );
1511
+               ADD_MVCOST( 10,-4, 1 );
1512
+               ADD_MVCOST( 11, 4, 1 );
1513
+               ADD_MVCOST( 12,-4, 2 );
1514
+               ADD_MVCOST( 13, 4, 2 );
1515
+               ADD_MVCOST( 14,-2, 3 );
1516
+               ADD_MVCOST( 15, 2, 3 );
1517
+               MIN_MV(  0, 0,-4 );
1518
+               MIN_MV(  1, 0, 4 );
1519
+               MIN_MV(  2,-2,-3 );
1520
+               MIN_MV(  3, 2,-3 );
1521
+               MIN_MV(  4,-4,-2 );
1522
+               MIN_MV(  5, 4,-2 );
1523
+               MIN_MV(  6,-4,-1 );
1524
+               MIN_MV(  7, 4,-1 );
1525
+               MIN_MV(  8,-4, 0 );
1526
+               MIN_MV(  9, 4, 0 );
1527
+               MIN_MV( 10,-4, 1 );
1528
+               MIN_MV( 11, 4, 1 );
1529
+               MIN_MV( 12,-4, 2 );
1530
+               MIN_MV( 13, 4, 2 );
1531
+               MIN_MV( 14,-2, 3 );
1532
+               MIN_MV( 15, 2, 3 );
1533
 #undef SADS
1534
 #undef ADD_MVCOST
1535
 #undef MIN_MV
1536
-                    if(dir)
1537
-                    {
1538
-                        bmx = omx + i*(dir>>4);
1539
-                        bmy = omy + i*((dir<<28)>>28);
1540
-                    }
1541
-                }
1542
-            } while( ++i <= i_me_range/4 );
1543
-            if( bmy <= mv_y_max && bmy >= mv_y_min )
1544
-                goto me_hex2;
1545
-            break;
1546
-        }
1547
+               if(dir)
1548
+                 {
1549
+                   bmx = omx + i*(dir>>4);
1550
+                   bmy = omy + i*((dir<<28)>>28);
1551
+                 }
1552
+             }
1553
+         } while( ++i <= i_me_range/4 );
1554
+       if( bmy <= mv_y_max && bmy >= mv_y_min )
1555
+         goto me_hex2;
1556
+       break;
1557
+      }
1558
 
1559
     case X264_ME_ESA:
1560
     case X264_ME_TESA:
1561
-        {
1562
-            const int min_x = X264_MAX( bmx - i_me_range, mv_x_min );
1563
-            const int min_y = X264_MAX( bmy - i_me_range, mv_y_min );
1564
-            const int max_x = X264_MIN( bmx + i_me_range, mv_x_max );
1565
-            const int max_y = X264_MIN( bmy + i_me_range, mv_y_max );
1566
-            /* SEA is fastest in multiples of 4 */
1567
-            const int width = (max_x - min_x + 3) & ~3;
1568
-            int my;
1569
+      {
1570
+       const int min_x = X264_MAX( bmx - i_me_range, mv_x_min );
1571
+       const int min_y = X264_MAX( bmy - i_me_range, mv_y_min );
1572
+       const int max_x = X264_MIN( bmx + i_me_range, mv_x_max );
1573
+       const int max_y = X264_MIN( bmy + i_me_range, mv_y_max );
1574
+       /* SEA is fastest in multiples of 4 */
1575
+       const int width = (max_x - min_x + 3) & ~3;
1576
+       int my;
1577
 #if 0
1578
-            /* plain old exhaustive search */
1579
-            int mx;
1580
-            for( my = min_y; my <= max_y; my++ )
1581
-                for( mx = min_x; mx <= max_x; mx++ )
1582
-                    COST_MV( mx, my );
1583
+       /* plain old exhaustive search */
1584
+       int mx;
1585
+       for( my = min_y; my <= max_y; my++ )
1586
+         for( mx = min_x; mx <= max_x; mx++ )
1587
+           COST_MV( mx, my );
1588
 #else
1589
-            /* successive elimination by comparing DC before a full SAD,
1590
-             * because sum(abs(diff)) >= abs(diff(sum)). */
1591
-            uint16_t *sums_base = m->integral;
1592
-            /* due to a GCC bug on some platforms (win32?), zero[] may not actually be aligned.
1593
-             * this is not a problem because it is not used for any SSE instructions. */
1594
-            ALIGNED_16( static uint8_t zero[8*FENC_STRIDE] );
1595
-            ALIGNED_ARRAY_16( int, enc_dc,[4] );
1596
-            int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4;
1597
-            int delta = x264_pixel_size[sad_size].w;
1598
-            int16_t *xs = h->scratch_buffer;
1599
-            int xn;
1600
-            uint16_t *cost_fpel_mvx = h->cost_mv_fpel[x264_lambda_tab[h->mb.i_qp]][-m->mvp[0]&3] + (-m->mvp[0]>>2);
1601
-
1602
-            h->pixf.sad_x4[sad_size]( zero, p_fenc, p_fenc+delta,
1603
-                p_fenc+delta*FENC_STRIDE, p_fenc+delta+delta*FENC_STRIDE,
1604
-                FENC_STRIDE, enc_dc );
1605
-            if( delta == 4 )
1606
-                sums_base += stride * (h->fenc->i_lines[0] + PADV*2);
1607
-            if( i_pixel == PIXEL_16x16 || i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
1608
-                delta *= stride;
1609
-            if( i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
1610
-                enc_dc[1] = enc_dc[2];
1611
-
1612
-            if( h->mb.i_me_method == X264_ME_TESA )
1613
-            {
1614
-                // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD
1615
-                mvsad_t *mvsads = (mvsad_t *)(xs + ((width+15)&~15));
1616
-                int nmvsad = 0, limit;
1617
-                int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;
1618
-                int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref+bmy*stride+bmx, stride )
1619
-                         + BITS_MVD( bmx, bmy );
1620
-                for( my = min_y; my <= max_y; my++ )
1621
-                {
1622
-                    int ycost = p_cost_mvy[my<<2];
1623
-                    if( bsad <= ycost )
1624
-                        continue;
1625
-                    bsad -= ycost;
1626
-                    xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
1627
-                                               cost_fpel_mvx+min_x, xs, width, bsad*17/16 );
1628
-                    for( i=0; i<xn-2; i+=3 )
1629
-                    {
1630
-                        uint8_t *ref = p_fref+min_x+my*stride;
1631
-                        int sads[3];
1632
-                        h->pixf.sad_x3[i_pixel]( p_fenc, ref+xs[i], ref+xs[i+1], ref+xs[i+2], stride, sads );
1633
-                        for( j=0; j<3; j++ )
1634
-                        {
1635
-                            int sad = sads[j] + cost_fpel_mvx[xs[i+j]];
1636
-                            if( sad < bsad*sad_thresh>>3 )
1637
-                            {
1638
-                                COPY1_IF_LT( bsad, sad );
1639
-                                mvsads[nmvsad].sad = sad + ycost;
1640
-                                mvsads[nmvsad].mx = min_x+xs[i+j];
1641
-                                mvsads[nmvsad].my = my;
1642
-                                nmvsad++;
1643
-                            }
1644
-                        }
1645
-                    }
1646
-                    for( ; i<xn; i++ )
1647
-                    {
1648
-                        int mx = min_x+xs[i];
1649
-                        int sad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref+mx+my*stride, stride )
1650
-                                + cost_fpel_mvx[xs[i]];
1651
-                        if( sad < bsad*sad_thresh>>3 )
1652
-                        {
1653
-                            COPY1_IF_LT( bsad, sad );
1654
-                            mvsads[nmvsad].sad = sad + ycost;
1655
-                            mvsads[nmvsad].mx = mx;
1656
-                            mvsads[nmvsad].my = my;
1657
-                            nmvsad++;
1658
-                        }
1659
-                    }
1660
-                    bsad += ycost;
1661
-                }
1662
-
1663
-                limit = i_me_range / 2;
1664
-                sad_thresh = bsad*sad_thresh>>3;
1665
-                while( nmvsad > limit*2 && sad_thresh > bsad )
1666
-                {
1667
-                    // halve the range if the domain is too large... eh, close enough
1668
-                    sad_thresh = (sad_thresh + bsad) >> 1;
1669
-                    for( i=0; i<nmvsad && mvsads[i].sad <= sad_thresh; i++ );
1670
-                    for( j=i; j<nmvsad; j++ )
1671
-                    {
1672
-                        /* mvsad_t is not guaranteed to be 8 bytes on all archs, so check before using explicit write-combining */
1673
-                        if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
1674
-                            *(uint64_t*)&mvsads[i] = *(uint64_t*)&mvsads[j];
1675
-                        else
1676
-                            mvsads[i] = mvsads[j];
1677
-                        i += mvsads[j].sad <= sad_thresh;
1678
-                    }
1679
-                    nmvsad = i;
1680
-                }
1681
-                while( nmvsad > limit )
1682
-                {
1683
-                    int bsad = mvsads[0].sad;
1684
-                    int bi = 0;
1685
-                    for( i=1; i<nmvsad; i++ )
1686
-                        COPY2_IF_GT( bsad, mvsads[i].sad, bi, i );
1687
-                    nmvsad--;
1688
-                    mvsads[bi] = mvsads[nmvsad];
1689
-                    if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
1690
-                        *(uint64_t*)&mvsads[bi] = *(uint64_t*)&mvsads[nmvsad];
1691
-                    else
1692
-                        mvsads[bi] = mvsads[nmvsad];
1693
-                }
1694
-                for( i=0; i<nmvsad; i++ )
1695
-                    COST_MV( mvsads[i].mx, mvsads[i].my );
1696
-            }
1697
-            else
1698
-            {
1699
-                // just ADS and SAD
1700
-                for( my = min_y; my <= max_y; my++ )
1701
-                {
1702
-                    int ycost = p_cost_mvy[my<<2];
1703
-                    if( bcost <= ycost )
1704
-                        continue;
1705
-                    bcost -= ycost;
1706
-                    xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
1707
-                                               cost_fpel_mvx+min_x, xs, width, bcost );
1708
-                    for( i=0; i<xn-2; i+=3 )
1709
-                        COST_MV_X3_ABS( min_x+xs[i],my, min_x+xs[i+1],my, min_x+xs[i+2],my );
1710
-                    bcost += ycost;
1711
-                    for( ; i<xn; i++ )
1712
-                        COST_MV( min_x+xs[i], my );
1713
-                }
1714
-            }
1715
+       /* successive elimination by comparing DC before a full SAD,
1716
+        * because sum(abs(diff)) >= abs(diff(sum)). */
1717
+       uint16_t *sums_base = m->integral;
1718
+       /* due to a GCC bug on some platforms (win32?), zero[] may not actually be aligned.
1719
+        * this is not a problem because it is not used for any SSE instructions. */
1720
+       ALIGNED_16( static uint8_t zero[8*FENC_STRIDE] );
1721
+       ALIGNED_ARRAY_16( int, enc_dc,[4] );
1722
+       int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4;
1723
+       int delta = x264_pixel_size[sad_size].w;
1724
+       int16_t *xs = h->scratch_buffer;
1725
+       int xn;
1726
+       uint16_t *cost_fpel_mvx = h->cost_mv_fpel[x264_lambda_tab[h->mb.i_qp]][-m->mvp[0]&3] + (-m->mvp[0]>>2);
1727
+
1728
+       h->pixf.sad_x4[sad_size]( zero, p_fenc, p_fenc+delta,
1729
+                                 p_fenc+delta*FENC_STRIDE, p_fenc+delta+delta*FENC_STRIDE,
1730
+                                 FENC_STRIDE, enc_dc );
1731
+       if( delta == 4 )
1732
+         sums_base += stride * (h->fenc->i_lines[0] + PADV*2);
1733
+       if( i_pixel == PIXEL_16x16 || i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
1734
+         delta *= stride;
1735
+       if( i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
1736
+         enc_dc[1] = enc_dc[2];
1737
+
1738
+       if( h->mb.i_me_method == X264_ME_TESA )
1739
+         {
1740
+           // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD
1741
+           mvsad_t *mvsads = (mvsad_t *)(xs + ((width+15)&~15));
1742
+           int nmvsad = 0, limit;
1743
+           int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;
1744
+           int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref+bmy*stride+bmx, stride )
1745
+             + BITS_MVD( bmx, bmy );
1746
+           for( my = min_y; my <= max_y; my++ )
1747
+             {
1748
+               int ycost = p_cost_mvy[my<<2];
1749
+               if( bsad <= ycost )
1750
+                 continue;
1751
+               bsad -= ycost;
1752
+               xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
1753
+                                          cost_fpel_mvx+min_x, xs, width, bsad*17/16 );
1754
+               for( i=0; i<xn-2; i+=3 )
1755
+                 {
1756
+                   uint8_t *ref = p_fref+min_x+my*stride;
1757
+                   int sads[3];
1758
+                   h->pixf.sad_x3[i_pixel]( p_fenc, ref+xs[i], ref+xs[i+1], ref+xs[i+2], stride, sads );
1759
+                   for( j=0; j<3; j++ )
1760
+                     {
1761
+                       int sad = sads[j] + cost_fpel_mvx[xs[i+j]];
1762
+                       if( sad < bsad*sad_thresh>>3 )
1763
+                         {
1764
+                           COPY1_IF_LT( bsad, sad );
1765
+                           mvsads[nmvsad].sad = sad + ycost;
1766
+                           mvsads[nmvsad].mx = min_x+xs[i+j];
1767
+                           mvsads[nmvsad].my = my;
1768
+                           nmvsad++;
1769
+                         }
1770
+                     }
1771
+                 }
1772
+               for( ; i<xn; i++ )
1773
+                 {
1774
+                   int mx = min_x+xs[i];
1775
+                   int sad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref+mx+my*stride, stride )
1776
+                     + cost_fpel_mvx[xs[i]];
1777
+                   if( sad < bsad*sad_thresh>>3 )
1778
+                     {
1779
+                       COPY1_IF_LT( bsad, sad );
1780
+                       mvsads[nmvsad].sad = sad + ycost;
1781
+                       mvsads[nmvsad].mx = mx;
1782
+                       mvsads[nmvsad].my = my;
1783
+                       nmvsad++;
1784
+                     }
1785
+                 }
1786
+               bsad += ycost;
1787
+             }
1788
+
1789
+           limit = i_me_range / 2;
1790
+           sad_thresh = bsad*sad_thresh>>3;
1791
+           while( nmvsad > limit*2 && sad_thresh > bsad )
1792
+             {
1793
+               // halve the range if the domain is too large... eh, close enough
1794
+               sad_thresh = (sad_thresh + bsad) >> 1;
1795
+               for( i=0; i<nmvsad && mvsads[i].sad <= sad_thresh; i++ );
1796
+               for( j=i; j<nmvsad; j++ )
1797
+                 {
1798
+                   /* mvsad_t is not guaranteed to be 8 bytes on all archs, so check before using explicit write-combining */
1799
+                   if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
1800
+                     *(uint64_t*)&mvsads[i] = *(uint64_t*)&mvsads[j];
1801
+                   else
1802
+                     mvsads[i] = mvsads[j];
1803
+                   i += mvsads[j].sad <= sad_thresh;
1804
+                 }
1805
+               nmvsad = i;
1806
+             }
1807
+           while( nmvsad > limit )
1808
+             {
1809
+               int bsad = mvsads[0].sad;
1810
+               int bi = 0;
1811
+               for( i=1; i<nmvsad; i++ )
1812
+                 COPY2_IF_GT( bsad, mvsads[i].sad, bi, i );
1813
+               nmvsad--;
1814
+               mvsads[bi] = mvsads[nmvsad];
1815
+               if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
1816
+                 *(uint64_t*)&mvsads[bi] = *(uint64_t*)&mvsads[nmvsad];
1817
+               else
1818
+                 mvsads[bi] = mvsads[nmvsad];
1819
+             }
1820
+           for( i=0; i<nmvsad; i++ )
1821
+             COST_MV( mvsads[i].mx, mvsads[i].my );
1822
+         }
1823
+       else
1824
+         {
1825
+           // just ADS and SAD
1826
+           for( my = min_y; my <= max_y; my++ )
1827
+             {
1828
+               int ycost = p_cost_mvy[my<<2];
1829
+               if( bcost <= ycost )
1830
+                 continue;
1831
+               bcost -= ycost;
1832
+               xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
1833
+                                          cost_fpel_mvx+min_x, xs, width, bcost );
1834
+               for( i=0; i<xn-2; i+=3 )
1835
+                 COST_MV_X3_ABS( min_x+xs[i],my, min_x+xs[i+1],my, min_x+xs[i+2],my );
1836
+               bcost += ycost;
1837
+               for( ; i<xn; i++ )
1838
+                 COST_MV( min_x+xs[i], my );
1839
+             }
1840
+         }
1841
 #endif
1842
-        }
1843
-        break;
1844
+      }
1845
+      break;
1846
     }
1847
 
1848
-    /* -> qpel mv */
1849
-    if( bpred_cost < bcost )
1850
+  /* -> qpel mv */
1851
+  if( bpred_cost < bcost )
1852
     {
1853
-        m->mv[0] = bpred_mx;
1854
-        m->mv[1] = bpred_my;
1855
-        m->cost = bpred_cost;
1856
+      m->mv[0] = bpred_mx;
1857
+      m->mv[1] = bpred_my;
1858
+      m->cost = bpred_cost;
1859
     }
1860
-    else
1861
+  else
1862
     {
1863
-        m->mv[0] = bmx << 2;
1864
-        m->mv[1] = bmy << 2;
1865
-        m->cost = bcost;
1866
+      m->mv[0] = bmx << 2;
1867
+      m->mv[1] = bmy << 2;
1868
+      m->cost = bcost;
1869
     }
1870
 
1871
-    /* compute the real cost */
1872
-    m->cost_mv = p_cost_mvx[ m->mv[0] ] + p_cost_mvy[ m->mv[1] ];
1873
-    if( bmx == pmx && bmy == pmy && h->mb.i_subpel_refine < 3 )
1874
-        m->cost += m->cost_mv;
1875
+  /* compute the real cost */
1876
+  m->cost_mv = p_cost_mvx[ m->mv[0] ] + p_cost_mvy[ m->mv[1] ];
1877
+  if( bmx == pmx && bmy == pmy && h->mb.i_subpel_refine < 3 )
1878
+    m->cost += m->cost_mv;
1879
 
1880
-    /* subpel refine */
1881
-    if( h->mb.i_subpel_refine >= 2 )
1882
+  /* subpel refine */
1883
+  if( h->mb.i_subpel_refine >= 2 )
1884
     {
1885
-        int hpel = subpel_iterations[h->mb.i_subpel_refine][2];
1886
-        int qpel = subpel_iterations[h->mb.i_subpel_refine][3];
1887
-        refine_subpel( h, m, hpel, qpel, p_halfpel_thresh, 0 );
1888
+      int hpel = subpel_iterations[h->mb.i_subpel_refine][2];
1889
+      int qpel = subpel_iterations[h->mb.i_subpel_refine][3];
1890
+      refine_subpel( h, m, hpel, qpel, p_halfpel_thresh, 0 );
1891
     }
1892
 }
1893
 #undef COST_MV
1894
 
1895
 void x264_me_refine_qpel( x264_t *h, x264_me_t *m )
1896
 {
1897
-    int hpel = subpel_iterations[h->mb.i_subpel_refine][0];
1898
-    int qpel = subpel_iterations[h->mb.i_subpel_refine][1];
1899
+  int hpel = subpel_iterations[h->mb.i_subpel_refine][0];
1900
+  int qpel = subpel_iterations[h->mb.i_subpel_refine][1];
1901
 
1902
-    if( m->i_pixel <= PIXEL_8x8 && h->sh.i_type == SLICE_TYPE_P )
1903
-        m->cost -= m->i_ref_cost;
1904
+  if( m->i_pixel <= PIXEL_8x8 && h->sh.i_type == SLICE_TYPE_P )
1905
+    m->cost -= m->i_ref_cost;
1906
 
1907
-    refine_subpel( h, m, hpel, qpel, NULL, 1 );
1908
+  refine_subpel( h, m, hpel, qpel, NULL, 1 );
1909
 }
1910
 
1911
-#define COST_MV_SAD( mx, my ) \
1912
-{ \
1913
-    int stride = 16; \
1914
+#define COST_MV_SAD( mx, my )                                          \
1915
+  {                                                                    \
1916
+    int stride = 16;                                                   \
1917
     uint8_t *src = h->mc.get_ref( pix[0], &stride, m->p_fref, m->i_stride[0], mx, my, bw, bh ); \
1918
     int cost = h->pixf.fpelcmp[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
1919
-             + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
1920
-    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my ); \
1921
-}
1922
-
1923
-#define COST_MV_SATD( mx, my, dir ) \
1924
-if( b_refine_qpel || (dir^1) != odir ) \
1925
-{ \
1926
-    int stride = 16; \
1927
-    uint8_t *src = h->mc.get_ref( pix[0], &stride, m->p_fref, m->i_stride[0], mx, my, bw, bh ); \
1928
-    int cost = h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
1929
-             + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
1930
-    if( b_chroma_me && cost < bcost ) \
1931
-    { \
1932
-        h->mc.mc_chroma( pix[0], 8, m->p_fref[4], m->i_stride[1], mx, my, bw/2, bh/2 ); \
1933
-        cost += h->pixf.mbcmp[i_pixel+3]( m->p_fenc[1], FENC_STRIDE, pix[0], 8 ); \
1934
-        if( cost < bcost ) \
1935
-        { \
1936
-            h->mc.mc_chroma( pix[0], 8, m->p_fref[5], m->i_stride[1], mx, my, bw/2, bh/2 ); \
1937
-            cost += h->pixf.mbcmp[i_pixel+3]( m->p_fenc[2], FENC_STRIDE, pix[0], 8 ); \
1938
-        } \
1939
-    } \
1940
-    if( cost < bcost ) \
1941
-    {                  \
1942
-        bcost = cost;  \
1943
-        bmx = mx;      \
1944
-        bmy = my;      \
1945
-        bdir = dir;    \
1946
-    } \
1947
-}
1948
+      + p_cost_mvx[ mx ] + p_cost_mvy[ my ];                           \
1949
+    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );                      \
1950
+  }
1951
+
1952
+#define COST_MV_SATD( mx, my, dir )                                    \
1953
+  if( b_refine_qpel || (dir^1) != odir )                               \
1954
+    {                                                                  \
1955
+      int stride = 16;                                                 \
1956
+      uint8_t *src = h->mc.get_ref( pix[0], &stride, m->p_fref, m->i_stride[0], mx, my, bw, bh ); \
1957
+      int cost = h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
1958
+       + p_cost_mvx[ mx ] + p_cost_mvy[ my ];                          \
1959
+      if( b_chroma_me && cost < bcost )                                        \
1960
+       {                                                               \
1961
+         h->mc.mc_chroma( pix[0], 8, m->p_fref[4], m->i_stride[1], mx, my, bw/2, bh/2 ); \
1962
+         cost += h->pixf.mbcmp[i_pixel+3]( m->p_fenc[1], FENC_STRIDE, pix[0], 8 ); \
1963
+         if( cost < bcost )                                            \
1964
+           {                                                           \
1965
+             h->mc.mc_chroma( pix[0], 8, m->p_fref[5], m->i_stride[1], mx, my, bw/2, bh/2 ); \
1966
+             cost += h->pixf.mbcmp[i_pixel+3]( m->p_fenc[2], FENC_STRIDE, pix[0], 8 ); \
1967
+           }                                                           \
1968
+       }                                                               \
1969
+      if( cost < bcost )                                               \
1970
+       {                                                               \
1971
+         bcost = cost;                                                 \
1972
+         bmx = mx;                                                     \
1973
+         bmy = my;                                                     \
1974
+         bdir = dir;                                                   \
1975
+       }                                                               \
1976
+    }
1977
 
1978
 static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel )
1979
 {
1980
-    const int bw = x264_pixel_size[m->i_pixel].w;
1981
-    const int bh = x264_pixel_size[m->i_pixel].h;
1982
-    const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
1983
-    const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
1984
-    const int i_pixel = m->i_pixel;
1985
-    const int b_chroma_me = h->mb.b_chroma_me && i_pixel <= PIXEL_8x8;
1986
-
1987
-    ALIGNED_ARRAY_16( uint8_t, pix,[2],[32*18] );   // really 17x17, but round up for alignment
1988
-    int omx, omy;
1989
-    int i;
1990
-
1991
-    int bmx = m->mv[0];
1992
-    int bmy = m->mv[1];
1993
-    int bcost = m->cost;
1994
-    int odir = -1, bdir;
1995
+  const int bw = x264_pixel_size[m->i_pixel].w;
1996
+  const int bh = x264_pixel_size[m->i_pixel].h;
1997
+  const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
1998
+  const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
1999
+  const int i_pixel = m->i_pixel;
2000
+  const int b_chroma_me = h->mb.b_chroma_me && i_pixel <= PIXEL_8x8;
2001
+
2002
+  ALIGNED_ARRAY_16( uint8_t, pix,[2],[32*18] );   // really 17x17, but round up for alignment
2003
+  int omx, omy;
2004
+  int i;
2005
+
2006
+  int bmx = m->mv[0];
2007
+  int bmy = m->mv[1];
2008
+  int bcost = m->cost;
2009
+  int odir = -1, bdir;
2010
 
2011
-    /* try the subpel component of the predicted mv */
2012
-    if( hpel_iters && h->mb.i_subpel_refine < 3 )
2013
+  /* try the subpel component of the predicted mv */
2014
+  if( hpel_iters && h->mb.i_subpel_refine < 3 )
2015
     {
2016
-        int mx = x264_clip3( m->mvp[0], h->mb.mv_min_spel[0]+2, h->mb.mv_max_spel[0]-2 );
2017
-        int my = x264_clip3( m->mvp[1], h->mb.mv_min_spel[1]+2, h->mb.mv_max_spel[1]-2 );
2018
-        if( (mx-bmx)|(my-bmy) )
2019
-            COST_MV_SAD( mx, my );
2020
+      int mx = x264_clip3( m->mvp[0], h->mb.mv_min_spel[0]+2, h->mb.mv_max_spel[0]-2 );
2021
+      int my = x264_clip3( m->mvp[1], h->mb.mv_min_spel[1]+2, h->mb.mv_max_spel[1]-2 );
2022
+      if( (mx-bmx)|(my-bmy) )
2023
+       COST_MV_SAD( mx, my );
2024
     }
2025
 
2026
-    /* halfpel diamond search */
2027
-    for( i = hpel_iters; i > 0; i-- )
2028
+  /* halfpel diamond search */
2029
+  for( i = hpel_iters; i > 0; i-- )
2030
     {
2031
-        int omx = bmx, omy = bmy;
2032
-        int costs[4];
2033
-        int stride = 32; // candidates are either all hpel or all qpel, so one stride is enough
2034
-        uint8_t *src0, *src1, *src2, *src3;
2035
-        src0 = h->mc.get_ref( pix[0], &stride, m->p_fref, m->i_stride[0], omx, omy-2, bw, bh+1 );
2036
-        src2 = h->mc.get_ref( pix[1], &stride, m->p_fref, m->i_stride[0], omx-2, omy, bw+4, bh );
2037
-        src1 = src0 + stride;
2038
-        src3 = src2 + 1;
2039
-        h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], src0, src1, src2, src3, stride, costs );
2040
-        COPY2_IF_LT( bcost, costs[0] + p_cost_mvx[omx  ] + p_cost_mvy[omy-2], bmy, omy-2 );
2041
-        COPY2_IF_LT( bcost, costs[1] + p_cost_mvx[omx  ] + p_cost_mvy[omy+2], bmy, omy+2 );
2042
-        COPY3_IF_LT( bcost, costs[2] + p_cost_mvx[omx-2] + p_cost_mvy[omy  ], bmx, omx-2, bmy, omy );
2043
-        COPY3_IF_LT( bcost, costs[3] + p_cost_mvx[omx+2] + p_cost_mvy[omy  ], bmx, omx+2, bmy, omy );
2044
-        if( (bmx == omx) & (bmy == omy) )
2045
-            break;
2046
+      int omx = bmx, omy = bmy;
2047
+      int costs[4];
2048
+      int stride = 32; // candidates are either all hpel or all qpel, so one stride is enough
2049
+      uint8_t *src0, *src1, *src2, *src3;
2050
+      src0 = h->mc.get_ref( pix[0], &stride, m->p_fref, m->i_stride[0], omx, omy-2, bw, bh+1 );
2051
+      src2 = h->mc.get_ref( pix[1], &stride, m->p_fref, m->i_stride[0], omx-2, omy, bw+4, bh );
2052
+      src1 = src0 + stride;
2053
+      src3 = src2 + 1;
2054
+      h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], src0, src1, src2, src3, stride, costs );
2055
+      COPY2_IF_LT( bcost, costs[0] + p_cost_mvx[omx  ] + p_cost_mvy[omy-2], bmy, omy-2 );
2056
+      COPY2_IF_LT( bcost, costs[1] + p_cost_mvx[omx  ] + p_cost_mvy[omy+2], bmy, omy+2 );
2057
+      COPY3_IF_LT( bcost, costs[2] + p_cost_mvx[omx-2] + p_cost_mvy[omy  ], bmx, omx-2, bmy, omy );
2058
+      COPY3_IF_LT( bcost, costs[3] + p_cost_mvx[omx+2] + p_cost_mvy[omy  ], bmx, omx+2, bmy, omy );
2059
+      if( (bmx == omx) & (bmy == omy) )
2060
+       break;
2061
     }
2062
 
2063
-    if( !b_refine_qpel )
2064
+  if( !b_refine_qpel )
2065
     {
2066
-        bcost = COST_MAX;
2067
-        COST_MV_SATD( bmx, bmy, -1 );
2068
+      bcost = COST_MAX;
2069
+      COST_MV_SATD( bmx, bmy, -1 );
2070
     }
2071
 
2072
-    /* early termination when examining multiple reference frames */
2073
-    if( p_halfpel_thresh )
2074
+  /* early termination when examining multiple reference frames */
2075
+  if( p_halfpel_thresh )
2076
     {
2077
-        if( (bcost*7)>>3 > *p_halfpel_thresh )
2078
+      if( (bcost*7)>>3 > *p_halfpel_thresh )
2079
         {
2080
-            m->cost = bcost;
2081
-            m->mv[0] = bmx;
2082
-            m->mv[1] = bmy;
2083
-            // don't need cost_mv
2084
-            return;
2085
+         m->cost = bcost;
2086
+         m->mv[0] = bmx;
2087
+         m->mv[1] = bmy;
2088
+         // don't need cost_mv
2089
+         return;
2090
         }
2091
-        else if( bcost < *p_halfpel_thresh )
2092
-            *p_halfpel_thresh = bcost;
2093
+      else if( bcost < *p_halfpel_thresh )
2094
+       *p_halfpel_thresh = bcost;
2095
     }
2096
 
2097
-    /* quarterpel diamond search */
2098
-    bdir = -1;
2099
-    for( i = qpel_iters; i > 0; i-- )
2100
+  /* quarterpel diamond search */
2101
+  bdir = -1;
2102
+  for( i = qpel_iters; i > 0; i-- )
2103
     {
2104
-        if( bmy <= h->mb.mv_min_spel[1] || bmy >= h->mb.mv_max_spel[1] )
2105
-            break;
2106
-        odir = bdir;
2107
-        omx = bmx;
2108
-        omy = bmy;
2109
-        COST_MV_SATD( omx, omy - 1, 0 );
2110
-        COST_MV_SATD( omx, omy + 1, 1 );
2111
-        COST_MV_SATD( omx - 1, omy, 2 );
2112
-        COST_MV_SATD( omx + 1, omy, 3 );
2113
-        if( bmx == omx && bmy == omy )
2114
-            break;
2115
+      if( bmy <= h->mb.mv_min_spel[1] || bmy >= h->mb.mv_max_spel[1] )
2116
+       break;
2117
+      odir = bdir;
2118
+      omx = bmx;
2119
+      omy = bmy;
2120
+      COST_MV_SATD( omx, omy - 1, 0 );
2121
+      COST_MV_SATD( omx, omy + 1, 1 );
2122
+      COST_MV_SATD( omx - 1, omy, 2 );
2123
+      COST_MV_SATD( omx + 1, omy, 3 );
2124
+      if( bmx == omx && bmy == omy )
2125
+       break;
2126
     }
2127
 
2128
-    m->cost = bcost;
2129
-    m->mv[0] = bmx;
2130
-    m->mv[1] = bmy;
2131
-    m->cost_mv = p_cost_mvx[ bmx ] + p_cost_mvy[ bmy ];
2132
+  m->cost = bcost;
2133
+  m->mv[0] = bmx;
2134
+  m->mv[1] = bmy;
2135
+  m->cost_mv = p_cost_mvx[ bmx ] + p_cost_mvy[ bmy ];
2136
 }
2137
 
2138
-#define BIME_CACHE( dx, dy, list ) \
2139
-{ \
2140
-    x264_me_t *m = m##list;\
2141
-    int i = 4 + 3*dx + dy; \
2142
-    int mvx = om##list##x+dx;\
2143
-    int mvy = om##list##y+dy;\
2144
-    stride##list[i] = bw;\
2145
+#define BIME_CACHE( dx, dy, list )                                     \
2146
+  {                                                                    \
2147
+    x264_me_t *m = m##list;                                            \
2148
+    int i = 4 + 3*dx + dy;                                             \
2149
+    int mvx = om##list##x+dx;                                          \
2150
+    int mvy = om##list##y+dy;                                          \
2151
+    stride##list[i] = bw;                                              \
2152
     src##list[i] = h->mc.get_ref( pixy_buf[list][i], &stride##list[i], m->p_fref, m->i_stride[0], mvx, mvy, bw, bh ); \
2153
-    if( rd )\
2154
-    {\
2155
-        if( h->mb.b_interlaced & ref##list )\
2156
-            mvy += (h->mb.i_mb_y & 1)*4 - 2;\
2157
-        h->mc.mc_chroma( pixu_buf[list][i], 8, m->p_fref[4], m->i_stride[1], mvx, mvy, bw>>1, bh>>1 );\
2158
-        h->mc.mc_chroma( pixv_buf[list][i], 8, m->p_fref[5], m->i_stride[1], mvx, mvy, bw>>1, bh>>1 );\
2159
-    }\
2160
-}
2161
-
2162
-#define BIME_CACHE2(a,b,list) \
2163
-    BIME_CACHE(a,b,list) \
2164
-    BIME_CACHE(-(a),-(b),list)
2165
-
2166
-#define BIME_CACHE8(list) \
2167
-{\
2168
-    BIME_CACHE2( 1, 0, list );\
2169
-    BIME_CACHE2( 0, 1, list );\
2170
-    BIME_CACHE2( 1, 1, list );\
2171
-    BIME_CACHE2( 1,-1, list );\
2172
-}
2173
+    if( rd )                                                           \
2174
+      {                                                                        \
2175
+        if( h->mb.b_interlaced & ref##list )                           \
2176
+         mvy += (h->mb.i_mb_y & 1)*4 - 2;                              \
2177
+        h->mc.mc_chroma( pixu_buf[list][i], 8, m->p_fref[4], m->i_stride[1], mvx, mvy, bw>>1, bh>>1 ); \
2178
+        h->mc.mc_chroma( pixv_buf[list][i], 8, m->p_fref[5], m->i_stride[1], mvx, mvy, bw>>1, bh>>1 ); \
2179
+      }                                                                        \
2180
+  }
2181
+
2182
+#define BIME_CACHE2(a,b,list)                  \
2183
+  BIME_CACHE(a,b,list)                         \
2184
+  BIME_CACHE(-(a),-(b),list)
2185
+
2186
+#define BIME_CACHE8(list)                      \
2187
+  {                                            \
2188
+    BIME_CACHE2( 1, 0, list );                 \
2189
+    BIME_CACHE2( 0, 1, list );                 \
2190
+    BIME_CACHE2( 1, 1, list );                 \
2191
+    BIME_CACHE2( 1,-1, list );                 \
2192
+  }
2193
 
2194
 #define SATD_THRESH 17/16
2195
 
2196
-#define COST_BIMV_SATD( m0x, m0y, m1x, m1y ) \
2197
-if( pass == 0 || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) ) \
2198
-{ \
2199
-    int cost; \
2200
-    int i0 = 4 + 3*(m0x-om0x) + (m0y-om0y); \
2201
-    int i1 = 4 + 3*(m1x-om1x) + (m1y-om1y); \
2202
-    visited[(m0x)&7][(m0y)&7][(m1x)&7] |= (1<<((m1y)&7));\
2203
-    h->mc.avg[i_pixel]( pix, FDEC_STRIDE, src0[i0], stride0[i0], src1[i1], stride1[i1], i_weight ); \
2204
-    cost = h->pixf.mbcmp[i_pixel]( m0->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE ) \
2205
-         + p_cost_m0x[ m0x ] + p_cost_m0y[ m0y ] \
2206
-         + p_cost_m1x[ m1x ] + p_cost_m1y[ m1y ]; \
2207
-    if( rd ) \
2208
-    { \
2209
-        if( cost < bcost * SATD_THRESH ) \
2210
-        { \
2211
-            uint64_t costrd; \
2212
-            if( cost < bcost ) \
2213
-                bcost = cost; \
2214
-            *(uint32_t*)cache0_mv = *(uint32_t*)cache0_mv2 = pack16to32_mask(m0x,m0y); \
2215
-            *(uint32_t*)cache1_mv = *(uint32_t*)cache1_mv2 = pack16to32_mask(m1x,m1y); \
2216
-            h->mc.avg[i_pixel+3]( pixu, FDEC_STRIDE, pixu_buf[0][i0], 8, pixu_buf[1][i1], 8, i_weight );\
2217
-            h->mc.avg[i_pixel+3]( pixv, FDEC_STRIDE, pixv_buf[0][i0], 8, pixv_buf[1][i1], 8, i_weight );\
2218
-            costrd = x264_rd_cost_part( h, i_lambda2, i8*4, m0->i_pixel ); \
2219
-            if( costrd < bcostrd ) \
2220
-            {\
2221
-                bcostrd = costrd;\
2222
-                bm0x = m0x;      \
2223
-                bm0y = m0y;      \
2224
-                bm1x = m1x;      \
2225
-                bm1y = m1y;      \
2226
-            }\
2227
-        } \
2228
-    } \
2229
-    else if( cost < bcost ) \
2230
-    {                  \
2231
-        bcost = cost;  \
2232
-        bm0x = m0x;    \
2233
-        bm0y = m0y;    \
2234
-        bm1x = m1x;    \
2235
-        bm1y = m1y;    \
2236
-    } \
2237
-}
2238
+#define COST_BIMV_SATD( m0x, m0y, m1x, m1y )                           \
2239
+  if( pass == 0 || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) ) \
2240
+    {                                                                  \
2241
+      int cost;                                                                \
2242
+      int i0 = 4 + 3*(m0x-om0x) + (m0y-om0y);                          \
2243
+      int i1 = 4 + 3*(m1x-om1x) + (m1y-om1y);                          \
2244
+      visited[(m0x)&7][(m0y)&7][(m1x)&7] |= (1<<((m1y)&7));            \
2245
+      h->mc.avg[i_pixel]( pix, FDEC_STRIDE, src0[i0], stride0[i0], src1[i1], stride1[i1], i_weight ); \
2246
+      cost = h->pixf.mbcmp[i_pixel]( m0->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE ) \
2247
+       + p_cost_m0x[ m0x ] + p_cost_m0y[ m0y ]                         \
2248
+       + p_cost_m1x[ m1x ] + p_cost_m1y[ m1y ];                        \
2249
+      if( rd )                                                         \
2250
+       {                                                               \
2251
+         if( cost < bcost * SATD_THRESH )                              \
2252
+           {                                                           \
2253
+             uint64_t costrd;                                          \
2254
+             if( cost < bcost )                                        \
2255
+                bcost = cost;                                          \
2256
+             *(uint32_t*)cache0_mv = *(uint32_t*)cache0_mv2 = pack16to32_mask(m0x,m0y); \
2257
+             *(uint32_t*)cache1_mv = *(uint32_t*)cache1_mv2 = pack16to32_mask(m1x,m1y); \
2258
+             h->mc.avg[i_pixel+3]( pixu, FDEC_STRIDE, pixu_buf[0][i0], 8, pixu_buf[1][i1], 8, i_weight ); \
2259
+             h->mc.avg[i_pixel+3]( pixv, FDEC_STRIDE, pixv_buf[0][i0], 8, pixv_buf[1][i1], 8, i_weight ); \
2260
+             costrd = x264_rd_cost_part( h, i_lambda2, i8*4, m0->i_pixel ); \
2261
+             if( costrd < bcostrd )                                    \
2262
+               {                                                       \
2263
+                 bcostrd = costrd;                                     \
2264
+                 bm0x = m0x;                                           \
2265
+                 bm0y = m0y;                                           \
2266
+                 bm1x = m1x;                                           \
2267
+                 bm1y = m1y;                                           \
2268
+               }                                                       \
2269
+           }                                                           \
2270
+       }                                                               \
2271
+      else if( cost < bcost )                                          \
2272
+       {                                                               \
2273
+         bcost = cost;                                                 \
2274
+         bm0x = m0x;                                                   \
2275
+         bm0y = m0y;                                                   \
2276
+         bm1x = m1x;                                                   \
2277
+         bm1y = m1y;                                                   \
2278
+       }                                                               \
2279
+    }
2280
 
2281
-#define CHECK_BIDIR(a,b,c,d) \
2282
-    COST_BIMV_SATD(om0x+a, om0y+b, om1x+c, om1y+d)
2283
+#define CHECK_BIDIR(a,b,c,d)                           \
2284
+  COST_BIMV_SATD(om0x+a, om0y+b, om1x+c, om1y+d)
2285
 
2286
 static void ALWAYS_INLINE x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2, int rd )
2287
 {
2288
-    static const int pixel_mv_offs[] = { 0, 4, 4*8, 0 };
2289
-    int16_t *cache0_mv = h->mb.cache.mv[0][x264_scan8[i8*4]];
2290
-    int16_t *cache0_mv2 = cache0_mv + pixel_mv_offs[m0->i_pixel];
2291
-    int16_t *cache1_mv = h->mb.cache.mv[1][x264_scan8[i8*4]];
2292
-    int16_t *cache1_mv2 = cache1_mv + pixel_mv_offs[m0->i_pixel];
2293
-    const int i_pixel = m0->i_pixel;
2294
-    const int bw = x264_pixel_size[i_pixel].w;
2295
-    const int bh = x264_pixel_size[i_pixel].h;
2296
-    const uint16_t *p_cost_m0x = m0->p_cost_mv - m0->mvp[0];
2297
-    const uint16_t *p_cost_m0y = m0->p_cost_mv - m0->mvp[1];
2298
-    const uint16_t *p_cost_m1x = m1->p_cost_mv - m1->mvp[0];
2299
-    const uint16_t *p_cost_m1y = m1->p_cost_mv - m1->mvp[1];
2300
-    ALIGNED_ARRAY_16( uint8_t, pixy_buf,[2],[9][16*16] );
2301
-    ALIGNED_8( uint8_t pixu_buf[2][9][8*8] );
2302
-    ALIGNED_8( uint8_t pixv_buf[2][9][8*8] );
2303
-    uint8_t *src0[9];
2304
-    uint8_t *src1[9];
2305
-    uint8_t *pix  = &h->mb.pic.p_fdec[0][(i8>>1)*8*FDEC_STRIDE+(i8&1)*8];
2306
-    uint8_t *pixu = &h->mb.pic.p_fdec[1][(i8>>1)*4*FDEC_STRIDE+(i8&1)*4];
2307
-    uint8_t *pixv = &h->mb.pic.p_fdec[2][(i8>>1)*4*FDEC_STRIDE+(i8&1)*4];
2308
-    int ref0 = h->mb.cache.ref[0][x264_scan8[i8*4]];
2309
-    int ref1 = h->mb.cache.ref[1][x264_scan8[i8*4]];
2310
-    int stride0[9];
2311
-    int stride1[9];
2312
-    int bm0x = m0->mv[0], om0x = bm0x;
2313
-    int bm0y = m0->mv[1], om0y = bm0y;
2314
-    int bm1x = m1->mv[0], om1x = bm1x;
2315
-    int bm1y = m1->mv[1], om1y = bm1y;
2316
-    int bcost = COST_MAX;
2317
-    int pass = 0;
2318
-    int j;
2319
-    int mc_list0 = 1, mc_list1 = 1;
2320
-    uint64_t bcostrd = COST_MAX64;
2321
-    /* each byte of visited represents 8 possible m1y positions, so a 4D array isn't needed */
2322
-    ALIGNED_ARRAY_16( uint8_t, visited,[8],[8][8] );
2323
-    /* all permutations of an offset in up to 2 of the dimensions */
2324
-    static const int8_t dia4d[32][4] = {
2325
-        {0,0,0,1}, {0,0,0,-1}, {0,0,1,0}, {0,0,-1,0},
2326
-        {0,1,0,0}, {0,-1,0,0}, {1,0,0,0}, {-1,0,0,0},
2327
-        {0,0,1,1}, {0,0,-1,-1},{0,1,1,0}, {0,-1,-1,0},
2328
-        {1,1,0,0}, {-1,-1,0,0},{1,0,0,1}, {-1,0,0,-1},
2329
-        {0,1,0,1}, {0,-1,0,-1},{1,0,1,0}, {-1,0,-1,0},
2330
-        {0,0,-1,1},{0,0,1,-1}, {0,-1,1,0},{0,1,-1,0},
2331
-        {-1,1,0,0},{1,-1,0,0}, {1,0,0,-1},{-1,0,0,1},
2332
-        {0,-1,0,1},{0,1,0,-1}, {-1,0,1,0},{1,0,-1,0},
2333
-    };
2334
-
2335
-    if( bm0y < h->mb.mv_min_spel[1] + 8 || bm1y < h->mb.mv_min_spel[1] + 8 ||
2336
-        bm0y > h->mb.mv_max_spel[1] - 8 || bm1y > h->mb.mv_max_spel[1] - 8 )
2337
-        return;
2338
-
2339
-    h->mc.memzero_aligned( visited, sizeof(uint8_t[8][8][8]) );
2340
-
2341
-    BIME_CACHE( 0, 0, 0 );
2342
-    BIME_CACHE( 0, 0, 1 );
2343
-    CHECK_BIDIR( 0, 0, 0, 0 );
2344
+  static const int pixel_mv_offs[] = { 0, 4, 4*8, 0 };
2345
+  int16_t *cache0_mv = h->mb.cache.mv[0][x264_scan8[i8*4]];
2346
+  int16_t *cache0_mv2 = cache0_mv + pixel_mv_offs[m0->i_pixel];
2347
+  int16_t *cache1_mv = h->mb.cache.mv[1][x264_scan8[i8*4]];
2348
+  int16_t *cache1_mv2 = cache1_mv + pixel_mv_offs[m0->i_pixel];
2349
+  const int i_pixel = m0->i_pixel;
2350
+  const int bw = x264_pixel_size[i_pixel].w;
2351
+  const int bh = x264_pixel_size[i_pixel].h;
2352
+  const uint16_t *p_cost_m0x = m0->p_cost_mv - m0->mvp[0];
2353
+  const uint16_t *p_cost_m0y = m0->p_cost_mv - m0->mvp[1];
2354
+  const uint16_t *p_cost_m1x = m1->p_cost_mv - m1->mvp[0];
2355
+  const uint16_t *p_cost_m1y = m1->p_cost_mv - m1->mvp[1];
2356
+  ALIGNED_ARRAY_16( uint8_t, pixy_buf,[2],[9][16*16] );
2357
+  ALIGNED_8( uint8_t pixu_buf[2][9][8*8] );
2358
+  ALIGNED_8( uint8_t pixv_buf[2][9][8*8] );
2359
+  uint8_t *src0[9];
2360
+  uint8_t *src1[9];
2361
+  uint8_t *pix  = &h->mb.pic.p_fdec[0][(i8>>1)*8*FDEC_STRIDE+(i8&1)*8];
2362
+  uint8_t *pixu = &h->mb.pic.p_fdec[1][(i8>>1)*4*FDEC_STRIDE+(i8&1)*4];
2363
+  uint8_t *pixv = &h->mb.pic.p_fdec[2][(i8>>1)*4*FDEC_STRIDE+(i8&1)*4];
2364
+  int ref0 = h->mb.cache.ref[0][x264_scan8[i8*4]];
2365
+  int ref1 = h->mb.cache.ref[1][x264_scan8[i8*4]];
2366
+  int stride0[9];
2367
+  int stride1[9];
2368
+  int bm0x = m0->mv[0], om0x = bm0x;
2369
+  int bm0y = m0->mv[1], om0y = bm0y;
2370
+  int bm1x = m1->mv[0], om1x = bm1x;
2371
+  int bm1y = m1->mv[1], om1y = bm1y;
2372
+  int bcost = COST_MAX;
2373
+  int pass = 0;
2374
+  int j;
2375
+  int mc_list0 = 1, mc_list1 = 1;
2376
+  uint64_t bcostrd = COST_MAX64;
2377
+  /* each byte of visited represents 8 possible m1y positions, so a 4D array isn't needed */
2378
+  ALIGNED_ARRAY_16( uint8_t, visited,[8],[8][8] );
2379
+  /* all permutations of an offset in up to 2 of the dimensions */
2380
+  static const int8_t dia4d[32][4] = {
2381
+    {0,0,0,1}, {0,0,0,-1}, {0,0,1,0}, {0,0,-1,0},
2382
+    {0,1,0,0}, {0,-1,0,0}, {1,0,0,0}, {-1,0,0,0},
2383
+    {0,0,1,1}, {0,0,-1,-1},{0,1,1,0}, {0,-1,-1,0},
2384
+    {1,1,0,0}, {-1,-1,0,0},{1,0,0,1}, {-1,0,0,-1},
2385
+    {0,1,0,1}, {0,-1,0,-1},{1,0,1,0}, {-1,0,-1,0},
2386
+    {0,0,-1,1},{0,0,1,-1}, {0,-1,1,0},{0,1,-1,0},
2387
+    {-1,1,0,0},{1,-1,0,0}, {1,0,0,-1},{-1,0,0,1},
2388
+    {0,-1,0,1},{0,1,0,-1}, {-1,0,1,0},{1,0,-1,0},
2389
+  };
2390
+
2391
+  if( bm0y < h->mb.mv_min_spel[1] + 8 || bm1y < h->mb.mv_min_spel[1] + 8 ||
2392
+      bm0y > h->mb.mv_max_spel[1] - 8 || bm1y > h->mb.mv_max_spel[1] - 8 )
2393
+    return;
2394
+
2395
+  h->mc.memzero_aligned( visited, sizeof(uint8_t[8][8][8]) );
2396
+
2397
+  BIME_CACHE( 0, 0, 0 );
2398
+  BIME_CACHE( 0, 0, 1 );
2399
+  CHECK_BIDIR( 0, 0, 0, 0 );
2400
 
2401
-    for( pass = 0; pass < 8; pass++ )
2402
+  for( pass = 0; pass < 8; pass++ )
2403
     {
2404
-        /* check all mv pairs that differ in at most 2 components from the current mvs. */
2405
-        /* doesn't do chroma ME. this probably doesn't matter, as the gains
2406
-         * from bidir ME are the same with and without chroma ME. */
2407
-
2408
-        if( mc_list0 )
2409
-            BIME_CACHE8( 0 );
2410
-        if( mc_list1 )
2411
-            BIME_CACHE8( 1 );
2412
-
2413
-        for( j=0; j<32; j++ )
2414
-            CHECK_BIDIR( dia4d[j][0], dia4d[j][1], dia4d[j][2], dia4d[j][3] );
2415
-
2416
-        mc_list0 = (om0x-bm0x)|(om0y-bm0y);
2417
-        mc_list1 = (om1x-bm1x)|(om1y-bm1y);
2418
-        if( !mc_list0 && !mc_list1 )
2419
-            break;
2420
-
2421
-        om0x = bm0x;
2422
-        om0y = bm0y;
2423
-        om1x = bm1x;
2424
-        om1y = bm1y;
2425
-
2426
-        if( mc_list0 )
2427
-            BIME_CACHE( 0, 0, 0 );
2428
-        if( mc_list1 )
2429
-            BIME_CACHE( 0, 0, 1 );
2430
+      /* check all mv pairs that differ in at most 2 components from the current mvs. */
2431
+      /* doesn't do chroma ME. this probably doesn't matter, as the gains
2432
+       * from bidir ME are the same with and without chroma ME. */
2433
+
2434
+      if( mc_list0 )
2435
+       BIME_CACHE8( 0 );
2436
+      if( mc_list1 )
2437
+       BIME_CACHE8( 1 );
2438
+
2439
+      for( j=0; j<32; j++ )
2440
+       CHECK_BIDIR( dia4d[j][0], dia4d[j][1], dia4d[j][2], dia4d[j][3] );
2441
+
2442
+      mc_list0 = (om0x-bm0x)|(om0y-bm0y);
2443
+      mc_list1 = (om1x-bm1x)|(om1y-bm1y);
2444
+      if( !mc_list0 && !mc_list1 )
2445
+       break;
2446
+
2447
+      om0x = bm0x;
2448
+      om0y = bm0y;
2449
+      om1x = bm1x;
2450
+      om1y = bm1y;
2451
+
2452
+      if( mc_list0 )
2453
+       BIME_CACHE( 0, 0, 0 );
2454
+      if( mc_list1 )
2455
+       BIME_CACHE( 0, 0, 1 );
2456
     }
2457
 
2458
-    m0->mv[0] = bm0x;
2459
-    m0->mv[1] = bm0y;
2460
-    m1->mv[0] = bm1x;
2461
-    m1->mv[1] = bm1y;
2462
+  m0->mv[0] = bm0x;
2463
+  m0->mv[1] = bm0y;
2464
+  m1->mv[0] = bm1x;
2465
+  m1->mv[1] = bm1y;
2466
 }
2467
 
2468
 void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight )
2469
 {
2470
-    x264_me_refine_bidir( h, m0, m1, i_weight, 0, 0, 0 );
2471
+  x264_me_refine_bidir( h, m0, m1, i_weight, 0, 0, 0 );
2472
 }
2473
 
2474
 void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2 )
2475
 {
2476
-    /* Motion compensation is done as part of bidir_rd; don't repeat
2477
-     * it in encoding. */
2478
-    h->mb.b_skip_mc = 1;
2479
-    x264_me_refine_bidir( h, m0, m1, i_weight, i8, i_lambda2, 1 );
2480
-    h->mb.b_skip_mc = 0;
2481
+  /* Motion compensation is done as part of bidir_rd; don't repeat
2482
+   * it in encoding. */
2483
+  h->mb.b_skip_mc = 1;
2484
+  x264_me_refine_bidir( h, m0, m1, i_weight, i8, i_lambda2, 1 );
2485
+  h->mb.b_skip_mc = 0;
2486
 }
2487
 
2488
 #undef COST_MV_SATD
2489
-#define COST_MV_SATD( mx, my, dst, avoid_mvp ) \
2490
-{ \
2491
-    if( !avoid_mvp || !(mx == pmx && my == pmy) ) \
2492
-    { \
2493
-        int stride = 16; \
2494
+#define COST_MV_SATD( mx, my, dst, avoid_mvp )                         \
2495
+  {                                                                    \
2496
+    if( !avoid_mvp || !(mx == pmx && my == pmy) )                      \
2497
+      {                                                                        \
2498
+        int stride = 16;                                               \
2499
         uint8_t *src = h->mc.get_ref( pix, &stride, m->p_fref, m->i_stride[0], mx, my, bw*4, bh*4 ); \
2500
         dst = h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
2501
-            + p_cost_mvx[mx] + p_cost_mvy[my]; \
2502
-        COPY1_IF_LT( bsatd, dst ); \
2503
-    } \
2504
-    else \
2505
-        dst = COST_MAX; \
2506
-}
2507
-
2508
-#define COST_MV_RD( mx, my, satd, do_dir, mdir ) \
2509
-{ \
2510
-    if( satd <= bsatd * SATD_THRESH ) \
2511
-    { \
2512
-        uint64_t cost; \
2513
+         + p_cost_mvx[mx] + p_cost_mvy[my];                            \
2514
+        COPY1_IF_LT( bsatd, dst );                                     \
2515
+      }                                                                        \
2516
+    else                                                               \
2517
+      dst = COST_MAX;                                                  \
2518
+  }
2519
+
2520
+#define COST_MV_RD( mx, my, satd, do_dir, mdir )                       \
2521
+  {                                                                    \
2522
+    if( satd <= bsatd * SATD_THRESH )                                  \
2523
+      {                                                                        \
2524
+        uint64_t cost;                                                 \
2525
         *(uint32_t*)cache_mv = *(uint32_t*)cache_mv2 = pack16to32_mask(mx,my); \
2526
-        cost = x264_rd_cost_part( h, i_lambda2, i4, m->i_pixel ); \
2527
+        cost = x264_rd_cost_part( h, i_lambda2, i4, m->i_pixel );      \
2528
         COPY4_IF_LT( bcost, cost, bmx, mx, bmy, my, dir, do_dir?mdir:dir ); \
2529
-    } \
2530
-}
2531
+      }                                                                        \
2532
+  }
2533
 
2534
 void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i4, int i_list )
2535
 {
2536
-    // don't have to fill the whole mv cache rectangle
2537
-    static const int pixel_mv_offs[] = { 0, 4, 4*8, 0, 2, 2*8, 0 };
2538
-    int16_t *cache_mv = h->mb.cache.mv[i_list][x264_scan8[i4]];
2539
-    int16_t *cache_mv2 = cache_mv + pixel_mv_offs[m->i_pixel];
2540
-    const uint16_t *p_cost_mvx, *p_cost_mvy;
2541
-    const int bw = x264_pixel_size[m->i_pixel].w>>2;
2542
-    const int bh = x264_pixel_size[m->i_pixel].h>>2;
2543
-    const int i_pixel = m->i_pixel;
2544
-
2545
-    ALIGNED_ARRAY_16( uint8_t, pix,[16*16] );
2546
-    uint64_t bcost = COST_MAX64;
2547
-    int bmx = m->mv[0];
2548
-    int bmy = m->mv[1];
2549
-    int omx, omy, pmx, pmy, i, j;
2550
-    unsigned bsatd;
2551
-    int satd = 0;
2552
-    int dir = -2;
2553
-    int satds[8];
2554
-
2555
-    if( m->i_pixel != PIXEL_16x16 && i4 != 0 )
2556
-        x264_mb_predict_mv( h, i_list, i4, bw, m->mvp );
2557
-    pmx = m->mvp[0];
2558
-    pmy = m->mvp[1];
2559
-    p_cost_mvx = m->p_cost_mv - pmx;
2560
-    p_cost_mvy = m->p_cost_mv - pmy;
2561
-    COST_MV_SATD( bmx, bmy, bsatd, 0 );
2562
-    if( m->i_pixel != PIXEL_16x16 )
2563
-        COST_MV_RD( bmx, bmy, 0, 0, 0 )
2564
+  // don't have to fill the whole mv cache rectangle
2565
+  static const int pixel_mv_offs[] = { 0, 4, 4*8, 0, 2, 2*8, 0 };
2566
+  int16_t *cache_mv = h->mb.cache.mv[i_list][x264_scan8[i4]];
2567
+  int16_t *cache_mv2 = cache_mv + pixel_mv_offs[m->i_pixel];
2568
+  const uint16_t *p_cost_mvx, *p_cost_mvy;
2569
+  const int bw = x264_pixel_size[m->i_pixel].w>>2;
2570
+  const int bh = x264_pixel_size[m->i_pixel].h>>2;
2571
+  const int i_pixel = m->i_pixel;
2572
+
2573
+  ALIGNED_ARRAY_16( uint8_t, pix,[16*16] );
2574
+  uint64_t bcost = COST_MAX64;
2575
+  int bmx = m->mv[0];
2576
+  int bmy = m->mv[1];
2577
+  int omx, omy, pmx, pmy, i, j;
2578
+  unsigned bsatd;
2579
+  int satd = 0;
2580
+  int dir = -2;
2581
+  int satds[8];
2582
+
2583
+  if( m->i_pixel != PIXEL_16x16 && i4 != 0 )
2584
+    x264_mb_predict_mv( h, i_list, i4, bw, m->mvp );
2585
+  pmx = m->mvp[0];
2586
+  pmy = m->mvp[1];
2587
+  p_cost_mvx = m->p_cost_mv - pmx;
2588
+  p_cost_mvy = m->p_cost_mv - pmy;
2589
+  COST_MV_SATD( bmx, bmy, bsatd, 0 );
2590
+  if( m->i_pixel != PIXEL_16x16 )
2591
+    COST_MV_RD( bmx, bmy, 0, 0, 0 )
2592
     else
2593
-        bcost = m->cost;
2594
+      bcost = m->cost;
2595
 
2596
-    /* check the predicted mv */
2597
-    if( (bmx != pmx || bmy != pmy)
2598
-        && pmx >= h->mb.mv_min_spel[0] && pmx <= h->mb.mv_max_spel[0]
2599
-        && pmy >= h->mb.mv_min_spel[1] && pmy <= h->mb.mv_max_spel[1] )
2600
+  /* check the predicted mv */
2601
+  if( (bmx != pmx || bmy != pmy)
2602
+      && pmx >= h->mb.mv_min_spel[0] && pmx <= h->mb.mv_max_spel[0]
2603
+      && pmy >= h->mb.mv_min_spel[1] && pmy <= h->mb.mv_max_spel[1] )
2604
     {
2605
-        COST_MV_SATD( pmx, pmy, satd, 0 );
2606
-        COST_MV_RD( pmx, pmy, satd, 0,0 );
2607
-        /* The hex motion search is guaranteed to not repeat the center candidate,
2608
-         * so if pmv is chosen, set the "MV to avoid checking" to bmv instead. */
2609
-        if( bmx == pmx && bmy == pmy )
2610
+      COST_MV_SATD( pmx, pmy, satd, 0 );
2611
+      COST_MV_RD( pmx, pmy, satd, 0,0 );
2612
+      /* The hex motion search is guaranteed to not repeat the center candidate,
2613
+       * so if pmv is chosen, set the "MV to avoid checking" to bmv instead. */
2614
+      if( bmx == pmx && bmy == pmy )
2615
         {
2616
-            pmx = m->mv[0];
2617
-            pmy = m->mv[1];
2618
+         pmx = m->mv[0];
2619
+         pmy = m->mv[1];
2620
         }
2621
     }
2622
 
2623
-    if( bmy < h->mb.mv_min_spel[1] + 3 ||
2624
-        bmy > h->mb.mv_max_spel[1] - 3 )
2625
-        return;
2626
-
2627
-    /* subpel hex search, same pattern as ME HEX. */
2628
-    dir = -2;
2629
-    omx = bmx;
2630
-    omy = bmy;
2631
-    for( j=0; j<6; j++ ) COST_MV_SATD( omx + hex2[j+1][0], omy + hex2[j+1][1], satds[j], 1 );
2632
-    for( j=0; j<6; j++ ) COST_MV_RD  ( omx + hex2[j+1][0], omy + hex2[j+1][1], satds[j], 1,j );
2633
+  if( bmy < h->mb.mv_min_spel[1] + 3 ||
2634
+      bmy > h->mb.mv_max_spel[1] - 3 )
2635
+    return;
2636
+
2637
+  /* subpel hex search, same pattern as ME HEX. */
2638
+  dir = -2;
2639
+  omx = bmx;
2640
+  omy = bmy;
2641
+  for( j=0; j<6; j++ ) COST_MV_SATD( omx + hex2[j+1][0], omy + hex2[j+1][1], satds[j], 1 );
2642
+  for( j=0; j<6; j++ ) COST_MV_RD  ( omx + hex2[j+1][0], omy + hex2[j+1][1], satds[j], 1,j );
2643
 
2644
-    if( dir != -2 )
2645
+  if( dir != -2 )
2646
     {
2647
-        /* half hexagon, not overlapping the previous iteration */
2648
-        for( i = 1; i < 10; i++ )
2649
+      /* half hexagon, not overlapping the previous iteration */
2650
+      for( i = 1; i < 10; i++ )
2651
         {
2652
-            const int odir = mod6m1[dir+1];
2653
-            if( bmy < h->mb.mv_min_spel[1] + 3 ||
2654
-                bmy > h->mb.mv_max_spel[1] - 3 )
2655
-                break;
2656
-            dir = -2;
2657
-            omx = bmx;
2658
-            omy = bmy;
2659
-            for( j=0; j<3; j++ ) COST_MV_SATD( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satds[j], 1 );
2660
-            for( j=0; j<3; j++ ) COST_MV_RD  ( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satds[j], 1, odir-1+j );
2661
-            if( dir == -2 )
2662
-                break;
2663
+         const int odir = mod6m1[dir+1];
2664
+         if( bmy < h->mb.mv_min_spel[1] + 3 ||
2665
+             bmy > h->mb.mv_max_spel[1] - 3 )
2666
+           break;
2667
+         dir = -2;
2668
+         omx = bmx;
2669
+         omy = bmy;
2670
+         for( j=0; j<3; j++ ) COST_MV_SATD( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satds[j], 1 );
2671
+         for( j=0; j<3; j++ ) COST_MV_RD  ( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satds[j], 1, odir-1+j );
2672
+         if( dir == -2 )
2673
+           break;
2674
         }
2675
     }
2676
 
2677
-    /* square refine, same pattern as ME HEX. */
2678
-    omx = bmx;
2679
-    omy = bmy;
2680
-    for( i=0; i<8; i++ ) COST_MV_SATD( omx + square1[i+1][0], omy + square1[i+1][1], satds[i], 1 );
2681
-    for( i=0; i<8; i++ ) COST_MV_RD  ( omx + square1[i+1][0], omy + square1[i+1][1], satds[i], 0,0 );
2682
-
2683
-    m->cost = bcost;
2684
-    m->mv[0] = bmx;
2685
-    m->mv[1] = bmy;
2686
-    x264_macroblock_cache_mv ( h, block_idx_x[i4], block_idx_y[i4], bw, bh, i_list, pack16to32_mask(bmx, bmy) );
2687
-    x264_macroblock_cache_mvd( h, block_idx_x[i4], block_idx_y[i4], bw, bh, i_list, pack16to32_mask(bmx - m->mvp[0], bmy - m->mvp[1]) );
2688
+  /* square refine, same pattern as ME HEX. */
2689
+  omx = bmx;
2690
+  omy = bmy;
2691
+  for( i=0; i<8; i++ ) COST_MV_SATD( omx + square1[i+1][0], omy + square1[i+1][1], satds[i], 1 );
2692
+  for( i=0; i<8; i++ ) COST_MV_RD  ( omx + square1[i+1][0], omy + square1[i+1][1], satds[i], 0,0 );
2693
+
2694
+  m->cost = bcost;
2695
+  m->mv[0] = bmx;
2696
+  m->mv[1] = bmy;
2697
+  x264_macroblock_cache_mv ( h, block_idx_x[i4], block_idx_y[i4], bw, bh, i_list, pack16to32_mask(bmx, bmy) );
2698
+  x264_macroblock_cache_mvd( h, block_idx_x[i4], block_idx_y[i4], bw, bh, i_list, pack16to32_mask(bmx - m->mvp[0], bmy - m->mvp[1]) );
2699
 }
2700
diff -Naur --exclude=.git --exclude=.gitignore x264/encoder/slicetype.c x264-or/encoder/slicetype.c
2701
--- x264/encoder/slicetype.c    2009-10-25 17:41:22.000000000 +0100
2702
+++ x264-or/encoder/slicetype.c 2009-10-28 15:05:29.000000000 +0100
2703
@@ -909,7 +909,9 @@
2704
 
2705
     /* Restore frametypes for all frames that haven't actually been decided yet. */
2706
     for( j = reset_start; j <= num_frames; j++ )
2707
-        frames[j]->i_type = X264_TYPE_AUTO;
2708
+      frames[j]->i_type = X264_TYPE_AUTO;
2709
+
2710
+    return;
2711
 }
2712
 
2713
 void x264_slicetype_decide( x264_t *h )
2714
diff -Naur --exclude=.git --exclude=.gitignore x264/link.ld x264-or/link.ld
2715
--- x264/link.ld        1970-01-01 01:00:00.000000000 +0100
2716
+++ x264-or/link.ld     2009-10-28 15:05:29.000000000 +0100
2717
@@ -0,0 +1,100 @@
2718
+/*
2719
+MEMORY
2720
+        {
2721
+        vectors : ORIGIN = 0x00000000, LENGTH = 0x00002000
2722
+        flash   : ORIGIN = 0x04000000, LENGTH = 0x00200000
2723
+        ram     : ORIGIN = 0x00002000, LENGTH = 0x001fe000
2724
+        icm     : ORIGIN = 0x00800000, LENGTH = 0x00004000
2725
+        }
2726
+ */
2727
+
2728
+MEMORY
2729
+       {
2730
+/*
2731
+       reset    : ORIGIN = 0x00000000, LENGTH = 0x00000200
2732
+       vectors  : ORIGIN = 0x00000200, LENGTH = 0x00001E00
2733
+        text     : ORIGIN = 0x00002000, LENGTH = 0x000fe000
2734
+       data     : ORIGIN = 0x00100000, LENGTH = 0x00fe0000
2735
+       stack    : ORIGIN = 0x001fe000, LENGTH = 0x00010000
2736
+*/
2737
+       yuv_data : ORIGIN = 25M, LENGTH = 7M
2738
+       }
2739
+
2740
+/*
2741
+MEMORY
2742
+        {
2743
+        reset   : ORIGIN = 0xc0000000, LENGTH = 0x00000200
2744
+        vectors : ORIGIN = 0xc0000200, LENGTH = 0x00001000
2745
+        ram     : ORIGIN = 0xc0001200, LENGTH = 0x00FFED00
2746
+        }
2747
+*/
2748
+SECTIONS
2749
+{
2750
+/*
2751
+       .reset :
2752
+        {
2753
+        *(.reset)
2754
+        } > reset
2755
+
2756
+
2757
+
2758
+       .vectors :
2759
+        {
2760
+        _vec_start = .;
2761
+        *(.vectors)
2762
+        _vec_end = .;
2763
+        } > vectors
2764
+
2765
+        .text :
2766
+        {
2767
+        *(.text)
2768
+        } > text
2769
+
2770
+      .rodata :
2771
+        {
2772
+        *(.rodata)
2773
+        *(.rodata.*)
2774
+        } > text
2775
+
2776
+     .icm :
2777
+        {
2778
+        _icm_start = .;
2779
+        *(.icm)
2780
+        _icm_end = .;
2781
+        } > data
2782
+
2783
+     .data :
2784
+        {
2785
+        _dst_beg = .;
2786
+        *(.data)
2787
+        _dst_end = .;
2788
+        } > data
2789
+
2790
+      .bss :
2791
+        {
2792
+        *(.bss)
2793
+        } > data
2794
+
2795
+       .heap :
2796
+       {
2797
+       _heap_start = .;
2798
+       *(.heap)
2799
+       _heap_end = .;
2800
+       } > data
2801
+
2802
+      .stack (NOLOAD) :
2803
+        {
2804
+        *(.stack)
2805
+        _src_addr = .;
2806
+        } > data
2807
+*/
2808
+       .yuv_data :
2809
+       {
2810
+       _yuv_data_start = .;
2811
+       __yuv_data_start = .;
2812
+       *(.yuv_data)
2813
+       _yuv_data_end = .;
2814
+       __yuv_data_end = .;
2815
+       } > yuv_data
2816
+
2817
+}
2818
diff -Naur --exclude=.git --exclude=.gitignore x264/Makefile x264-or/Makefile
2819
--- x264/Makefile       2009-10-25 17:41:22.000000000 +0100
2820
+++ x264-or/Makefile    2009-11-15 18:48:46.000000000 +0100
2821
@@ -91,7 +91,66 @@
2822
 $(SONAME): .depend $(OBJS) $(OBJASM)
2823
        $(CC) -shared -o $@ $(OBJS) $(OBJASM) $(SOFLAGS) $(LDFLAGS)
2824
 
2825
-x264$(EXE): $(OBJCLI) libx264.a
2826
+
2827
+## OR32-specific build rules
2828
+##
2829
+## Essentially we create some sample YUV video data
2830
+## and embed it in an ELF file, in a section called yuv_data.
2831
+## In the linker script we then link this section to a place
2832
+## in ram and tell x264 to look here when it wants data.
2833
+##
2834
+## You can download sample h264 encoded CIF sequences
2835
+## here: http://www.tkn.tu-berlin.de/research/evalvid/cif.html
2836
+##
2837
+##
2838
+GEN_FILES=encoder/analyse_init_log2.c
2839
+VIDEO_DATA_FILE=yuv_data.elf
2840
+INPUT_VIDEO_FILE ?= ../test-sequences/football_cif.264
2841
+INPUT_VIDEO_SIZE ?= cif
2842
+YUV_VIDEO_FILE ?= video_data.yuv
2843
+
2844
+# We will use a limit of about 5MB of video data to encode
2845
+ifeq ($(INPUT_VIDEO_SIZE), 4cif)
2846
+NUM_FRAMES ?=10
2847
+endif
2848
+ifeq ($(INPUT_VIDEO_SIZE), cif)
2849
+NUM_FRAMES ?=30
2850
+endif
2851
+ifeq ($(INPUT_VIDEO_SIZE), qcif)
2852
+NUM_FRAMES ?=90
2853
+endif
2854
+
2855
+$(INPUT_VIDEO_FILE):
2856
+       @echo; echo;
2857
+       @echo "\tNo sample video file to embed! Please edit the Makefile"
2858
+       @echo "\tand set the variable INPUT_VIDEO_FILE to the location of"
2859
+       @echo "\tsome sample video material."
2860
+       @echo "\tOr, isntead of editing the makefile, specify it on the"
2861
+       @echo "\tcommand line like so:"
2862
+       @echo "\t\tINPUT_VIDEO_FILE=../coastguard_cif.yuv"; echo
2863
+       @echo "\tYou can also specify the size of the video (cif, qcif etc.)"
2864
+       @echo "\tby specifying INPUT_VIDEO_SIZE - the default is cif, however"
2865
+       @echo "\tyou will need to change the program's hardcoded resolution"
2866
+       @echo
2867
+       exit 1
2868
+$(YUV_VIDEO_FILE): $(INPUT_VIDEO_FILE)
2869
+# First convert it to raw YUV, only about 5MB large, so for cif take 30 frames, qcif is 90 frames
2870
+       ffmpeg -s $(INPUT_VIDEO_SIZE) -i $(INPUT_VIDEO_FILE) -vframes $(NUM_FRAMES) $(YUV_VIDEO_FILE)
2871
+$(VIDEO_DATA_FILE): $(YUV_VIDEO_FILE)
2872
+       or32-elf-ld -r -b binary -o yuv_data.o $(YUV_VIDEO_FILE)
2873
+       or32-elf-objcopy --rename-section .data=.yuv_data yuv_data.o
2874
+       mv yuv_data.o $(VIDEO_DATA_FILE)
2875
+encoder/analyse_init_log2.c: encoder/analyse_gen_init_array.sh
2876
+       cd encoder && chmod a+x analyse_gen_init_array.sh && ./analyse_gen_init_array.sh
2877
+#OR32_DEPS= reset.o except.o uart.o syscalls.o $(VIDEO_DATA_FILE)
2878
+
2879
+OR32_DEPS= $(VIDEO_DATA_FILE) $(GEN_FILES)
2880
+
2881
+sim: x264$(EXE)
2882
+       or32-elf-sim -f or1ksim_x264.cfg $<
2883
+
2884
+
2885
+x264$(EXE): $(OR32_DEPS) $(OBJCLI) libx264.a
2886
        $(CC) -o $@ $+ $(LDFLAGS)
2887
 
2888
 checkasm: tools/checkasm.o libx264.a
2889
@@ -147,12 +206,13 @@
2890
 endif
2891
 
2892
 clean:
2893
-       rm -f $(OBJS) $(OBJASM) $(OBJCLI) $(SONAME) *.a x264 x264.exe .depend TAGS
2894
+       rm -f $(OBJS) $(OBJASM) $(OBJCLI) $(SONAME) *.a x264 x264.exe .depend TAGS $(YUV_VIDEO_FILE)
2895
        rm -f checkasm checkasm.exe tools/checkasm.o tools/checkasm-a.o
2896
        rm -f $(SRC2:%.c=%.gcda) $(SRC2:%.c=%.gcno)
2897
        - sed -e 's/ *-fprofile-\(generate\|use\)//g' config.mak > config.mak2 && mv config.mak2 config.mak
2898
 
2899
 distclean: clean
2900
+       rm -f $(OR32_DEPS) uart0* $(GEN_FILES)
2901
        rm -f config.mak config.h x264.pc
2902
        rm -rf test/
2903
 
2904
diff -Naur --exclude=.git --exclude=.gitignore x264/muxers.c x264-or/muxers.c
2905
--- x264/muxers.c       2009-10-25 17:41:22.000000000 +0100
2906
+++ x264-or/muxers.c    2009-11-15 18:45:09.000000000 +0100
2907
@@ -52,15 +52,32 @@
2908
 
2909
 typedef struct
2910
 {
2911
-    FILE *fh;
2912
-    int width, height;
2913
-    int next_frame;
2914
+  //FILE *fh;
2915
+  char *fh;
2916
+
2917
+  int width, height;
2918
+  int next_frame;
2919
+
2920
 } yuv_input_t;
2921
 
2922
+void init_yuv_dataspace(char* yuv_dat_addr, hnd_t *p_handle, x264_param_t *p_param)
2923
+{
2924
+  yuv_input_t *h = malloc( sizeof(yuv_input_t) );
2925
+  V(fprintf( stderr, "init_yuv_dataspace: h addr = 0x%.8x\n", h));
2926
+  h->fh = yuv_dat_addr;
2927
+  V(fprintf( stderr, "init_yuv_dataspace: h->fh 0x%.8x\n",h->fh));
2928
+  h->width = p_param->i_width;
2929
+  V(fprintf( stderr, "init_yuv_dataspace: h->width %d\n",h->width));
2930
+  h->height = p_param->i_height;
2931
+  V(fprintf( stderr, "init_yuv_dataspace: h->height %d\n",h->height));
2932
+  h->next_frame = 0;
2933
+  *p_handle = (hnd_t *)h;
2934
+}
2935
+
2936
 /* raw 420 yuv file operation */
2937
 int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
2938
 {
2939
-    yuv_input_t *h = malloc( sizeof(yuv_input_t) );
2940
+  yuv_input_t *h = malloc( sizeof(yuv_input_t) );
2941
     if( !h )
2942
         return -1;
2943
     h->width = p_param->i_width;
2944
@@ -80,7 +97,10 @@
2945
 
2946
 int get_frame_total_yuv( hnd_t handle )
2947
 {
2948
-    yuv_input_t *h = handle;
2949
+      yuv_input_t *h = handle;
2950
+
2951
+      V(fprintf(stderr, "get_frame_total_yuv args: handle: 0x%.8x, h: 0x%.8x\n", handle, h));
2952
+      /*
2953
     int i_frame_total = 0;
2954
 
2955
     if( !fseek( h->fh, 0, SEEK_END ) )
2956
@@ -88,24 +108,45 @@
2957
         uint64_t i_size = ftell( h->fh );
2958
         fseek( h->fh, 0, SEEK_SET );
2959
         i_frame_total = (int)(i_size / ( h->width * h->height * 3 / 2 ));
2960
-    }
2961
+       }*/
2962
+
2963
+#ifdef      USE_HARDCODED_FRAME_NUM
2964
+      return (int)HARDCODED_FRAME_NUM;
2965
+#else
2966
+      fprintf( stderr, "get_frame_total_yuv: %d %d %d %d\n",
2967
+                h->width, h->height,
2968
+                YUV_DATA_SIZE,
2969
+                (int)(YUV_DATA_SIZE / ( h->width * h->height * 3 / 2 )));
2970
+      return (int)(YUV_DATA_SIZE / ( h->width * h->height * 3 / 2 ));//i_frame_total;
2971
+
2972
+#endif
2973
 
2974
-    return i_frame_total;
2975
 }
2976
 
2977
 int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame )
2978
 {
2979
     yuv_input_t *h = handle;
2980
-
2981
+    int i;
2982
+
2983
     if( i_frame != h->next_frame )
2984
-        if( fseek( h->fh, (uint64_t)i_frame * h->width * h->height * 3 / 2, SEEK_SET ) )
2985
-            return -1;
2986
-
2987
-    if( fread( p_pic->img.plane[0], 1, h->width * h->height, h->fh ) <= 0
2988
-     || fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
2989
-     || fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
2990
-        return -1;
2991
+      //if( fseek( h->fh, (uint64_t)i_frame * h->width * h->height * 3 / 2, SEEK_SET ) )
2992
+      // Progress pointer to beginning of this frame
2993
+      h->fh = (void*) YUV_DATA_ADDR + i_frame * h->width * h->height * 3 / 2;
2994
+      //return -1;
2995
+
2996
+    //if( fread( p_pic->img.plane[0], 1, h->width * h->height, h->fh ) <= 0
2997
+    for (i=0;i<h->width * h->height;i++) p_pic->img.plane[0][i] = h->fh[i];
2998
+    h->fh += h->width * h->height;
2999
+    //|| fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
3000
+    for (i=0;i<(h->width * h->height) / 4;i++) p_pic->img.plane[1][i] = h->fh[i];
3001
+    h->fh += (h->width * h->height) / 4;
3002
+
3003
+    //|| fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
3004
+    for (i=0;i<(h->width * h->height) / 4;i++) p_pic->img.plane[2][i] = h->fh[i];
3005
+    h->fh += (h->width * h->height) / 4;
3006
 
3007
+    //return -1;
3008
+
3009
     h->next_frame = i_frame+1;
3010
 
3011
     return 0;
3012
@@ -113,12 +154,15 @@
3013
 
3014
 int close_file_yuv(hnd_t handle)
3015
 {
3016
+  return 0;
3017
+  /* // -- jb
3018
     yuv_input_t *h = handle;
3019
     if( !h || !h->fh )
3020
         return 0;
3021
     fclose( h->fh );
3022
     free( h );
3023
     return 0;
3024
+  */
3025
 }
3026
 
3027
 /* YUV4MPEG2 raw 420 yuv file operation */
3028
@@ -530,11 +574,21 @@
3029
 }
3030
 #endif
3031
 
3032
+#ifdef ENC_OUT_ADDR
3033
+char * enc_out_pos;
3034
+#endif
3035
 
3036
 int open_file_bsf( char *psz_filename, hnd_t *p_handle )
3037
 {
3038
+#ifdef ENC_OUT_ADDR
3039
+  V(fprintf(stderr, "open_file_bsf\n"));
3040
+  // Use a hardcoded memory location for outputting encoded data to
3041
+  p_handle = (void *) ENC_OUT_ADDR;
3042
+  enc_out_pos = (char *) ENC_OUT_ADDR;
3043
+#else
3044
     if( !(*p_handle = fopen(psz_filename, "w+b")) )
3045
         return -1;
3046
+#endif
3047
 
3048
     return 0;
3049
 }
3050
@@ -546,9 +600,16 @@
3051
 
3052
 int write_nalu_bsf( hnd_t handle, uint8_t *p_nalu, int i_size )
3053
 {
3054
+#ifdef ENC_OUT_ADDR
3055
+  V(fprintf(stderr, "write_nalu_bsf: writing %d bytes from 0x%.8x\n", i_size, (unsigned long) enc_out_pos));
3056
+  // Just write to the spot in memory and increment the pointer
3057
+  int i; for (i=0;i<i_size;i++) *enc_out_pos++ = p_nalu[i];
3058
+  return i_size;
3059
+#else
3060
     if( fwrite( p_nalu, i_size, 1, (FILE*)handle ) > 0 )
3061
         return i_size;
3062
     return -1;
3063
+#endif
3064
 }
3065
 
3066
 int set_eop_bsf( hnd_t handle,  x264_picture_t *p_picture )
3067
@@ -558,6 +619,15 @@
3068
 
3069
 int close_file_bsf( hnd_t handle )
3070
 {
3071
+#ifdef ENC_OUT_ADDR
3072
+  // Let's calculate a checksum for the written data
3073
+  int i; char* d = (char*)ENC_OUT_ADDR; unsigned int cksum;
3074
+  unsigned int size = (unsigned int)enc_out_pos - (unsigned int)ENC_OUT_ADDR;
3075
+  for(i=0;i<size;i++)cksum+=d[i++];
3076
+  fprintf(stderr, "close_file_bsf: wrote %d bytes from 0x%.8x to 0x%.8x\n", size, (unsigned int) ENC_OUT_ADDR, (unsigned int) enc_out_pos);
3077
+  fprintf(stderr, "close_file_bsf: cksum 0x%.8x\n", cksum);
3078
+  return 0;
3079
+#endif
3080
     if( !handle || handle == stdout )
3081
         return 0;
3082
 
3083
diff -Naur --exclude=.git --exclude=.gitignore x264/muxers.h x264-or/muxers.h
3084
--- x264/muxers.h       2009-10-25 17:41:22.000000000 +0100
3085
+++ x264-or/muxers.h    2009-10-28 15:05:29.000000000 +0100
3086
@@ -27,6 +27,7 @@
3087
 typedef void *hnd_t;
3088
 
3089
 int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
3090
+void init_yuv_dataspace(char* yuv_dat_addr, hnd_t *p_handle, x264_param_t *p_param);
3091
 int get_frame_total_yuv( hnd_t handle );
3092
 int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame );
3093
 int close_file_yuv( hnd_t handle );
3094
diff -Naur --exclude=.git --exclude=.gitignore x264/or1ksim_x264.cfg x264-or/or1ksim_x264.cfg
3095
--- x264/or1ksim_x264.cfg       1970-01-01 01:00:00.000000000 +0100
3096
+++ x264-or/or1ksim_x264.cfg    2009-11-15 19:57:09.000000000 +0100
3097
@@ -0,0 +1,886 @@
3098
+/* sim.cfg -- Simulator configuration script file
3099
+   Copyright (C) 2001-2002, Marko Mlinar, markom@opencores.org
3100
+
3101
+This file is part of OpenRISC 1000 Architectural Simulator.
3102
+It contains the default configuration and help about configuring
3103
+the simulator.
3104
+
3105
+This program is free software; you can redistribute it and/or modify
3106
+it under the terms of the GNU General Public License as published by
3107
+the Free Software Foundation; either version 2 of the License, or
3108
+(at your option) any later version.
3109
+
3110
+This program is distributed in the hope that it will be useful,
3111
+but WITHOUT ANY WARRANTY; without even the implied warranty of
3112
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3113
+GNU General Public License for more details.
3114
+
3115
+You should have received a copy of the GNU General Public License
3116
+along with this program; if not, write to the Free Software
3117
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
3118
+
3119
+
3120
+/* INTRODUCTION
3121
+
3122
+   The ork1sim has various parameters, that are set in configuration files
3123
+   like this one. The user can switch between configurations at startup by
3124
+   specifying the required configuration file with the -f <filename.cfg> option.
3125
+   If no configuration file is specified or1ksim searches for the default
3126
+   configuration file sim.cfg. First it searches for './sim.cfg'. If this
3127
+   file is not found, it searches for '~/or1k/sim.cfg'. If this file is
3128
+   not found too, it reverts to the built-in default configuration.
3129
+
3130
+   NOTE: Users should not rely on the built-in configuration, since the
3131
+         default configuration may differ between version.
3132
+         Rather create a configuration file that sets all critical values.
3133
+
3134
+   This file may contain (standard C) comments only - no // support.
3135
+
3136
+   Configure files may be be included, using:
3137
+   include "file_name_to_include"
3138
+
3139
+   Like normal configuration files, the included file is divided into
3140
+   sections. Each section is described in detail also.
3141
+
3142
+   Some section have subsections. One example of such a subsection is:
3143
+
3144
+   device <index>
3145
+     instance specific parameters...
3146
+   enddevice
3147
+
3148
+   which creates a device instance.
3149
+*/
3150
+
3151
+
3152
+/* MEMORY SECTION
3153
+
3154
+   This section specifies how the memory is generated and the blocks
3155
+   it consists of.
3156
+
3157
+   type = random/unknown/pattern
3158
+      Specifies the initial memory values.
3159
+      'random' generates random memory using seed 'random_seed'.
3160
+      'pattern' fills memory with 'pattern'.
3161
+      'unknown' does not specify how memory should be generated,
3162
+      leaving the memory in a undefined state. This is the fastest
3163
+      option.
3164
+
3165
+   random_seed = <value>
3166
+      random seed for randomizer, used if type = 'random'.
3167
+
3168
+   pattern = <value>
3169
+      pattern to fill memory, used if type = 'pattern'.
3170
+
3171
+   nmemories = <value>
3172
+      number of memory instances connected
3173
+
3174
+   baseaddr = <hex_value>
3175
+      memory start address
3176
+
3177
+   size = <hex_value>
3178
+      memory size
3179
+
3180
+   name = "<string>"
3181
+      memory block name
3182
+
3183
+   ce = <value>
3184
+      chip enable index of the memory instance
3185
+
3186
+   mc = <value>
3187
+      memory controller this memory is connected to
3188
+
3189
+   delayr = <value>
3190
+      cycles, required for read access, -1 if instance does not support reading
3191
+
3192
+   delayw = <value>
3193
+      cycles, required for write access, -1 if instance does not support writing
3194
+
3195
+   log = "<filename>"
3196
+      filename, where to log memory accesses to, no log, if log command is not specified
3197
+*/
3198
+
3199
+
3200
+section memory
3201
+  /*random_seed = 12345
3202
+  type = random*/
3203
+  pattern = 0x00
3204
+  type = unknown /* Fastest */
3205
+
3206
+  name = "FLASH"
3207
+  ce = 0
3208
+  mc = 0
3209
+  baseaddr = 0xf0000000
3210
+  size = 0x01000000
3211
+  delayr = 10
3212
+  delayw = -1
3213
+end
3214
+
3215
+section memory
3216
+  /*random_seed = 12345
3217
+  type = random*/
3218
+  pattern = 0x00
3219
+  type = unknown /* Fastest */
3220
+
3221
+  name = "RAM"
3222
+  ce = 1
3223
+  mc = 0
3224
+  baseaddr = 0x00000000
3225
+  size = 0x02000000
3226
+  delayr = 20
3227
+  delayw = 25
3228
+end
3229
+
3230
+section memory
3231
+  /*random_seed = 12345
3232
+  type = random*/
3233
+  pattern = 0x00
3234
+  type = unknown /* Fastest */
3235
+
3236
+  name = "SRAM"
3237
+  mc = 0
3238
+  ce = 2
3239
+  baseaddr = 0xa4000000
3240
+  size = 0x00100000
3241
+  delayr = 1
3242
+  delayw = 2
3243
+end
3244
+
3245
+
3246
+/* IMMU SECTION
3247
+
3248
+    This section configures the Instruction Memory Manangement Unit
3249
+
3250
+    enabled = 0/1
3251
+       '0': disabled
3252
+       '1': enabled
3253
+       (NOTE: UPR bit is set)
3254
+
3255
+    nsets = <value>
3256
+       number of ITLB sets; must be power of two
3257
+
3258
+    nways = <value>
3259
+       number of ITLB ways
3260
+
3261
+    pagesize = <value>
3262
+       instruction page size; must be power of two
3263
+
3264
+    entrysize = <value>
3265
+       instruction entry size in bytes
3266
+
3267
+    ustates = <value>
3268
+       number of ITLB usage states (2, 3, 4 etc., max is 4)
3269
+
3270
+    hitdelay = <value>
3271
+       number of cycles immu hit costs
3272
+
3273
+    missdelay = <value>
3274
+       number of cycles immu miss costs
3275
+*/
3276
+
3277
+section immu
3278
+  enabled = 1
3279
+  nsets = 64
3280
+  nways = 1
3281
+  pagesize = 8192
3282
+  hitdelay = 0
3283
+  missdelay = 0
3284
+end
3285
+
3286
+
3287
+/* DMMU SECTION
3288
+
3289
+    This section configures the Data Memory Manangement Unit
3290
+
3291
+    enabled = 0/1
3292
+       '0': disabled
3293
+       '1': enabled
3294
+       (NOTE: UPR bit is set)
3295
+
3296
+    nsets = <value>
3297
+       number of DTLB sets; must be power of two
3298
+
3299
+    nways = <value>
3300
+       number of DTLB ways
3301
+
3302
+    pagesize = <value>
3303
+       data page size; must be power of two
3304
+
3305
+    entrysize = <value>
3306
+       data entry size in bytes
3307
+
3308
+    ustates = <value>
3309
+       number of DTLB usage states (2, 3, 4 etc., max is 4)
3310
+
3311
+    hitdelay = <value>
3312
+       number of cycles dmmu hit costs
3313
+
3314
+    missdelay = <value>
3315
+       number of cycles dmmu miss costs
3316
+*/
3317
+
3318
+section dmmu
3319
+  enabled = 1
3320
+  nsets = 64
3321
+  nways = 1
3322
+  pagesize = 8192
3323
+  hitdelay = 0
3324
+  missdelay = 0
3325
+end
3326
+
3327
+
3328
+/* IC SECTION
3329
+
3330
+   This section configures the Instruction Cache
3331
+
3332
+   enabled = 0/1
3333
+       '0': disabled
3334
+       '1': enabled
3335
+      (NOTE: UPR bit is set)
3336
+
3337
+   nsets = <value>
3338
+      number of IC sets; must be power of two
3339
+
3340
+   nways = <value>
3341
+      number of IC ways
3342
+
3343
+   blocksize = <value>
3344
+      IC block size in bytes; must be power of two
3345
+
3346
+   ustates = <value>
3347
+      number of IC usage states (2, 3, 4 etc., max is 4)
3348
+
3349
+   hitdelay = <value>
3350
+      number of cycles ic hit costs
3351
+
3352
+    missdelay = <value>
3353
+      number of cycles ic miss costs
3354
+*/
3355
+
3356
+section ic
3357
+  enabled = 0
3358
+  nsets = 512
3359
+  nways = 1
3360
+  blocksize = 16
3361
+  hitdelay = 20
3362
+  missdelay = 20
3363
+end
3364
+
3365
+
3366
+/* DC SECTION
3367
+
3368
+   This section configures the Data Cache
3369
+
3370
+   enabled = 0/1
3371
+       '0': disabled
3372
+       '1': enabled
3373
+      (NOTE: UPR bit is set)
3374
+
3375
+   nsets = <value>
3376
+      number of DC sets; must be power of two
3377
+
3378
+   nways = <value>
3379
+      number of DC ways
3380
+
3381
+   blocksize = <value>
3382
+      DC block size in bytes; must be power of two
3383
+
3384
+   ustates = <value>
3385
+      number of DC usage states (2, 3, 4 etc., max is 4)
3386
+
3387
+   load_hitdelay = <value>
3388
+      number of cycles dc load hit costs
3389
+
3390
+   load_missdelay = <value>
3391
+      number of cycles dc load miss costs
3392
+
3393
+   store_hitdelay = <value>
3394
+      number of cycles dc load hit costs
3395
+
3396
+   store_missdelay = <value>
3397
+      number of cycles dc load miss costs
3398
+*/
3399
+
3400
+section dc
3401
+  enabled = 0
3402
+  nsets = 512
3403
+  nways = 1
3404
+  blocksize = 16
3405
+  load_hitdelay = 20
3406
+  load_missdelay = 20
3407
+  store_hitdelay = 20
3408
+  store_missdelay = 20
3409
+end
3410
+
3411
+
3412
+/* SIM SECTION
3413
+
3414
+  This section specifies how or1ksim should behave.
3415
+
3416
+  verbose = 0/1
3417
+       '0': don't print extra messages
3418
+       '1': print extra messages
3419
+
3420
+  debug = 0-9
3421
+      0  : no debug messages
3422
+      1-9: debug message level.
3423
+           higher numbers produce more messages
3424
+
3425
+  profile = 0/1
3426
+      '0': don't generate profiling file 'sim.profile'
3427
+      '1': don't generate profiling file 'sim.profile'
3428
+
3429
+  prof_fn = "<filename>"
3430
+      optional filename for the profiling file.
3431
+      valid only if 'profile' is set
3432
+
3433
+  mprofile = 0/1
3434
+      '0': don't generate memory profiling file 'sim.mprofile'
3435
+      '1': generate memory profiling file 'sim.mprofile'
3436
+
3437
+  mprof_fn = "<filename>"
3438
+      optional filename for the memory profiling file.
3439
+      valid only if 'mprofile' is set
3440
+
3441
+  history = 0/1
3442
+      '0': don't track execution flow
3443
+      '1': track execution flow
3444
+      Execution flow can be tracked for the simulator's
3445
+      'hist' command. Useful for back-trace debugging.
3446
+
3447
+  iprompt = 0/1
3448
+     '0': start in <not interactive prompt> (so what do we start in ???)
3449
+     '1': start in interactive prompt.
3450
+
3451
+  exe_log = 0/1
3452
+      '0': don't generate execution log.
3453
+      '1': generate execution log.
3454
+
3455
+  exe_log = default/hardware/simple/software
3456
+      type of execution log, default is used when not specified
3457
+
3458
+  exe_log_start = <value>
3459
+      index of first instruction to start logging, default = 0
3460
+
3461
+  exe_log_end = <value>
3462
+      index of last instruction to end logging; not limited, if omitted
3463
+
3464
+  exe_log_marker = <value>
3465
+      <value> specifies number of instructions before horizontal marker is
3466
+      printed; if zero, markers are disabled (default)
3467
+
3468
+  exe_log_fn = "<filename>"
3469
+      filename for the exection log file.
3470
+      valid only if 'exe_log' is set
3471
+
3472
+  clkcycle = <value>[ps|ns|us|ms]
3473
+      specifies time measurement for one cycle
3474
+*/
3475
+
3476
+section sim
3477
+  verbose = 1
3478
+  debug = 0
3479
+  profile = 0
3480
+  history = 0
3481
+
3482
+  clkcycle = 10ns
3483
+end
3484
+
3485
+
3486
+/* SECTION VAPI
3487
+
3488
+    This section configures the Verification API, used for Advanced
3489
+    Core Verification.
3490
+
3491
+    enabled = 0/1
3492
+        '0': disbable VAPI server
3493
+        '1': enable/start VAPI server
3494
+
3495
+    server_port = <value>
3496
+        TCP/IP port to start VAPI server on
3497
+
3498
+    log_enabled = 0/1
3499
+       '0': disable VAPI requests logging
3500
+       '1': enable VAPI requests logging
3501
+
3502
+    hide_device_id = 0/1
3503
+       '0': don't log device id (for compatability with old version)
3504
+       '1': log device id
3505
+
3506
+
3507
+    vapi_fn = <filename>
3508
+       filename for the log file.
3509
+       valid only if log_enabled is set
3510
+*/
3511
+
3512
+section VAPI
3513
+  enabled = 0
3514
+  server_port = 9998
3515
+  log_enabled = 0
3516
+  vapi_log_fn = "vapi.log"
3517
+end
3518
+
3519
+
3520
+/* CPU SECTION
3521
+
3522
+   This section specifies various CPU parameters.
3523
+
3524
+   ver = <value>
3525
+   rev = <value>
3526
+      specifies version and revision of the CPU used
3527
+
3528
+   upr = <value>
3529
+      changes the upr register
3530
+
3531
+   sr = <value>
3532
+      sets the initial Supervision Register value
3533
+
3534
+   superscalar = 0/1
3535
+      '0': CPU is scalar
3536
+      '1': CPU is superscalar
3537
+      (modify cpu/or32/execute.c to tune superscalar model)
3538
+
3539
+   hazards = 0/1
3540
+      '0': don't track data hazards in superscalar CPU
3541
+      '1': track data hazards in superscalar CPU
3542
+      If tracked, data hazards can be displayed using the
3543
+      simulator's 'r' command.
3544
+
3545
+   dependstats = 0/1
3546
+      '0': don't calculate inter-instruction dependencies.
3547
+      '1': calculate inter-instruction dependencies.
3548
+      If calculated, inter-instruction dependencies can be
3549
+      displayed using the simulator's 'stat' command.
3550
+
3551
+   sbuf_len = <value>
3552
+      length of store buffer (<= 256), 0 = disabled
3553
+*/
3554
+
3555
+section cpu
3556
+  ver = 0x12
3557
+  cfg = 0x00
3558
+  rev = 0x01
3559
+  /* upr = */
3560
+  superscalar = 0
3561
+  hazards = 0
3562
+  dependstats = 0
3563
+  sbuf_len = 0
3564
+  hardfloat = 1
3565
+end
3566
+
3567
+
3568
+/* PM SECTION
3569
+
3570
+   This section specifies Power Management parameters
3571
+
3572
+   enabled = 0/1
3573
+      '0': disable power management
3574
+      '1': enable power management
3575
+*/
3576
+
3577
+section pm
3578
+  enabled = 0
3579
+end
3580
+
3581
+
3582
+/* BPB SECTION
3583
+
3584
+   This section specifies how branch prediction should behave.
3585
+
3586
+   enabled = 0/1
3587
+     '0': disable branch prediction
3588
+     '1': enable branch prediction
3589
+
3590
+   btic = 0/1
3591
+     '0': disable branch target instruction cache model
3592
+     '1': enable branch target instruction cache model
3593
+
3594
+   sbp_bf_fwd = 0/1
3595
+     Static branch prediction for 'l.bf'
3596
+     '0': don't use forward prediction
3597
+     '1': use forward prediction
3598
+
3599
+   sbp_bnf_fwd = 0/1
3600
+     Static branch prediction for 'l.bnf'
3601
+     '0': don't use forward prediction
3602
+     '1': use forward prediction
3603
+
3604
+   hitdelay = <value>
3605
+       number of cycles bpb hit costs
3606
+
3607
+   missdelay = <value>
3608
+       number of cycles bpb miss costs
3609
+*/
3610
+
3611
+section bpb
3612
+  enabled = 0
3613
+  btic = 0
3614
+  sbp_bf_fwd = 0
3615
+  sbp_bnf_fwd = 0
3616
+  hitdelay = 0
3617
+  missdelay = 0
3618
+end
3619
+
3620
+
3621
+/* DEBUG SECTION
3622
+
3623
+   This sections specifies how the debug unit should behave.
3624
+
3625
+   enabled = 0/1
3626
+      '0': disable debug unit
3627
+      '1': enable debug unit
3628
+
3629
+   gdb_enabled = 0/1
3630
+      '0': don't start gdb server
3631
+      '1': start gdb server at port 'server_port'
3632
+
3633
+   server_port = <value>
3634
+      TCP/IP port to start gdb server on
3635
+      valid only if gdb_enabled is set
3636
+
3637
+   vapi_id = <hex_value>
3638
+      Used to create "fake" vapi log file containing the JTAG proxy messages.
3639
+*/
3640
+section debug
3641
+  enabled = 0
3642
+  rsp_enabled = 0
3643
+  rsp_port = 5554
3644
+  /*server_port = 9999*/
3645
+end
3646
+
3647
+
3648
+/* MC SECTION
3649
+
3650
+   This section configures the memory controller
3651
+
3652
+   enabled = 0/1
3653
+     '0': disable memory controller
3654
+     '1': enable memory controller
3655
+
3656
+   baseaddr = <hex_value>
3657
+      address of first MC register
3658
+
3659
+   POC = <hex_value>
3660
+      Power On Configuration register
3661
+
3662
+   index = <value>
3663
+      Index of this memory controller amongst all the memory controllers
3664
+*/
3665
+
3666
+section mc
3667
+  enabled = 0
3668
+  baseaddr = 0x93000000
3669
+  POC = 0x00000008                 /* Power on configuration register */
3670
+  index = 0
3671
+end
3672
+
3673
+
3674
+/* UART SECTION
3675
+
3676
+   This section configures the UARTs
3677
+
3678
+     enabled = <0|1>
3679
+        Enable/disable the peripheral.  By default if it is enabled.
3680
+
3681
+     baseaddr = <hex_value>
3682
+        address of first UART register for this device
3683
+
3684
+
3685
+     channel = <channeltype>:<args>
3686
+
3687
+        The channel parameter indicates the source of received UART characters
3688
+        and the sink for transmitted UART characters.
3689
+
3690
+        The <channeltype> can be either "file", "xterm", "tcp", "fd", or "tty"
3691
+        (without quotes).
3692
+
3693
+          A) To send/receive characters from a pair of files, use a file
3694
+             channel:
3695
+
3696
+               channel=file:<rxfile>,<txfile>
3697
+
3698
+         B) To create an interactive terminal window, use an xterm channel:
3699
+
3700
+               channel=xterm:[<xterm_arg>]*
3701
+
3702
+         C) To create a bidirectional tcp socket which one could, for example,
3703
+             access via telnet, use a tcp channel:
3704
+
3705
+               channel=tcp:<port number>
3706
+
3707
+         D) To cause the UART to read/write from existing numeric file
3708
+             descriptors, use an fd channel:
3709
+
3710
+               channel=fd:<rx file descriptor num>,<tx file descriptor num>
3711
+
3712
+          E) To connect the UART to a physical serial port, create a tty
3713
+             channel:
3714
+
3715
+              channel=tty:device=/dev/ttyS0,baud=9600
3716
+
3717
+     irq = <value>
3718
+        irq number for this device
3719
+
3720
+     16550 = 0/1
3721
+        '0': this device is a UART16450
3722
+        '1': this device is a UART16550
3723
+
3724
+     jitter = <value>
3725
+        in msecs... time to block, -1 to disable it
3726
+
3727
+     vapi_id = <hex_value>
3728
+        VAPI id of this instance
3729
+*/
3730
+
3731
+section uart
3732
+  enabled = 1
3733
+  baseaddr = 0x90000000
3734
+  irq = 2
3735
+  channel = "file:uart0.rx,uart0.tx"
3736
+  /* channel = "tcp:10084" */
3737
+  /* channel = "xterm:" */
3738
+  jitter = -1                     /* async behaviour */
3739
+  16550 = 1
3740
+end
3741
+
3742
+
3743
+/* DMA SECTION
3744
+
3745
+   This section configures the DMAs
3746
+
3747
+     enabled = <0|1>
3748
+        Enable/disable the peripheral.  By default if it is enabled.
3749
+
3750
+     baseaddr = <hex_value>
3751
+        address of first DMA register for this device
3752
+
3753
+     irq = <value>
3754
+        irq number for this device
3755
+
3756
+     vapi_id = <hex_value>
3757
+        VAPI id of this instance
3758
+*/
3759
+
3760
+section dma
3761
+  enabled = 1
3762
+  baseaddr = 0x9a000000
3763
+  irq = 11
3764
+end
3765
+
3766
+
3767
+/* ETHERNET SECTION
3768
+
3769
+   This section configures the ETHERNETs
3770
+
3771
+     enabled = <0|1>
3772
+        Enable/disable the peripheral.  By default if it is enabled.
3773
+
3774
+     baseaddr = <hex_value>
3775
+        address of first ethernet register for this device
3776
+
3777
+     dma = <value>
3778
+        which controller is this ethernet "connected" to
3779
+
3780
+     irq = <value>
3781
+        ethernet mac IRQ level
3782
+
3783
+     rtx_type = <value>
3784
+        use 0 - file interface, 1 - socket interface
3785
+
3786
+     rx_channel = <value>
3787
+        DMA channel used for RX
3788
+
3789
+     tx_channel = <value>
3790
+        DMA channel used for TX
3791
+
3792
+     rxfile = "<filename>"
3793
+        filename, where to read data from
3794
+
3795
+     txfile = "<filename>"
3796
+        filename, where to write data to
3797
+
3798
+     sockif = "<ifacename>"
3799
+        interface name of ethernet socket
3800
+
3801
+     vapi_id = <hex_value>
3802
+        VAPI id of this instance
3803
+*/
3804
+
3805
+section ethernet
3806
+  enabled = 0
3807
+  baseaddr = 0x92000000
3808
+  /* dma = 0 */
3809
+  irq = 4
3810
+  rtx_type = 0
3811
+  /* tx_channel = 0 */
3812
+  /* rx_channel = 1 */
3813
+  /*rxfile = "eth0.rx"*/
3814
+  txfile = "eth0.tx"
3815
+  sockif = "eth0"
3816
+end
3817
+
3818
+
3819
+/* GPIO SECTION
3820
+
3821
+   This section configures the GPIOs
3822
+
3823
+     enabled = <0|1>
3824
+        Enable/disable the peripheral.  By default if it is enabled.
3825
+
3826
+     baseaddr = <hex_value>
3827
+        address of first GPIO register for this device
3828
+
3829
+     irq = <value>
3830
+        irq number for this device
3831
+
3832
+     base_vapi_id = <hex_value>
3833
+        first VAPI id of this instance
3834
+       GPIO uses 8 consecutive VAPI IDs
3835
+*/
3836
+
3837
+section gpio
3838
+  enabled = 0
3839
+  baseaddr = 0x91000000
3840
+  irq = 3
3841
+  base_vapi_id = 0x0200
3842
+end
3843
+
3844
+/* VGA SECTION
3845
+
3846
+    This section configures the VGA/LCD controller
3847
+
3848
+      enabled = <0|1>
3849
+        Enable/disable the peripheral.  By default if it is enabled.
3850
+
3851
+      baseaddr = <hex_value>
3852
+        address of first VGA register
3853
+
3854
+      irq = <value>
3855
+        irq number for this device
3856
+
3857
+      refresh_rate = <value>
3858
+        number of cycles between screen dumps
3859
+
3860
+      filename = "<filename>"
3861
+        template name for generated names (e.g. "primary" produces "primary0023.bmp")
3862
+*/
3863
+
3864
+section vga
3865
+  enabled = 1
3866
+  baseaddr = 0x97100000
3867
+  irq = 8
3868
+  refresh_rate = 100000
3869
+  filename = "primary"
3870
+end
3871
+
3872
+
3873
+/* TICK TIMER SECTION
3874
+
3875
+    This section configures tick timer
3876
+
3877
+    enabled = 0/1
3878
+      whether tick timer is enabled
3879
+*/
3880
+
3881
+section pic
3882
+  enabled = 1
3883
+  edge_trigger = 1
3884
+end
3885
+
3886
+/* FB SECTION
3887
+
3888
+    This section configures the frame buffer
3889
+
3890
+    enabled = <0|1>
3891
+      Enable/disable the peripheral.  By default if it is enabled.
3892
+
3893
+    baseaddr = <hex_value>
3894
+      base address of frame buffer
3895
+
3896
+    paladdr = <hex_value>
3897
+      base address of first palette entry
3898
+
3899
+    refresh_rate = <value>
3900
+      number of cycles between screen dumps
3901
+
3902
+    filename = "<filename>"
3903
+      template name for generated names (e.g. "primary" produces "primary0023.bmp")
3904
+*/
3905
+
3906
+section fb
3907
+  enabled = 1
3908
+  baseaddr = 0x97000000
3909
+  refresh_rate = 1000000
3910
+  filename = "primary"
3911
+end
3912
+
3913
+
3914
+/* KBD SECTION
3915
+
3916
+    This section configures the PS/2 compatible keyboard
3917
+
3918
+    baseaddr = <hex_value>
3919
+      base address of the keyboard device
3920
+
3921
+    rxfile = "<filename>"
3922
+      filename, where to read data from
3923
+*/
3924
+
3925
+section kbd
3926
+  enabled = 1
3927
+  irq = 5
3928
+  baseaddr = 0x94000000
3929
+  rxfile = "kbd.rx"
3930
+end
3931
+
3932
+
3933
+/* ATA SECTION
3934
+
3935
+    This section configures the ATA/ATAPI host controller
3936
+
3937
+      baseaddr = <hex_value>
3938
+        address of first ATA register
3939
+
3940
+      enabled = <0|1>
3941
+        Enable/disable the peripheral.  By default if it is enabled.
3942
+
3943
+      irq = <value>
3944
+        irq number for this device
3945
+
3946
+      debug = <value>
3947
+        debug level for ata models.
3948
+       0: no debug messages
3949
+       1: verbose messages
3950
+       3: normal messages (more messages than verbose)
3951
+        5: debug messages (normal debug messages)
3952
+       7: flow control messages (debug statemachine flows)
3953
+       9: low priority message (display everything the code does)
3954
+
3955
+      dev_type0/1 = <value>
3956
+        ata device 0 type
3957
+        0: NO_CONNeCT: none (not connected)
3958
+       1: FILE      : simulated harddisk
3959
+       2: LOCAL     : local system harddisk
3960
+
3961
+      dev_file0/1 = "<filename>"
3962
+        filename for simulated ATA device
3963
+       valid only if dev_type0 == 1
3964
+
3965
+      dev_size0/1 = <value>
3966
+        size of simulated hard-disk (in MBytes)
3967
+       valid only if dev_type0 == 1
3968
+
3969
+      dev_packet0/1 = <value>
3970
+        0: simulated ATA device does NOT implement PACKET command feature set
3971
+       1: simulated ATA device does implement PACKET command feature set
3972
+
3973
+   FIXME: irq number
3974
+*/
3975
+
3976
+section ata
3977
+  enabled = 0
3978
+  baseaddr = 0x9e000000
3979
+  irq = 15
3980
+
3981
+end
3982
+
3983
+
3984
diff -Naur --exclude=.git --exclude=.gitignore x264/rsp_or1ksim_x264.cfg x264-or/rsp_or1ksim_x264.cfg
3985
--- x264/rsp_or1ksim_x264.cfg   1970-01-01 01:00:00.000000000 +0100
3986
+++ x264-or/rsp_or1ksim_x264.cfg        2009-11-15 19:56:58.000000000 +0100
3987
@@ -0,0 +1,886 @@
3988
+/* sim.cfg -- Simulator configuration script file
3989
+   Copyright (C) 2001-2002, Marko Mlinar, markom@opencores.org
3990
+
3991
+This file is part of OpenRISC 1000 Architectural Simulator.
3992
+It contains the default configuration and help about configuring
3993
+the simulator.
3994
+
3995
+This program is free software; you can redistribute it and/or modify
3996
+it under the terms of the GNU General Public License as published by
3997
+the Free Software Foundation; either version 2 of the License, or
3998
+(at your option) any later version.
3999
+
4000
+This program is distributed in the hope that it will be useful,
4001
+but WITHOUT ANY WARRANTY; without even the implied warranty of
4002
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4003
+GNU General Public License for more details.
4004
+
4005
+You should have received a copy of the GNU General Public License
4006
+along with this program; if not, write to the Free Software
4007
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4008
+
4009
+
4010
+/* INTRODUCTION
4011
+
4012
+   The ork1sim has various parameters, that are set in configuration files
4013
+   like this one. The user can switch between configurations at startup by
4014
+   specifying the required configuration file with the -f <filename.cfg> option.
4015
+   If no configuration file is specified or1ksim searches for the default
4016
+   configuration file sim.cfg. First it searches for './sim.cfg'. If this
4017
+   file is not found, it searches for '~/or1k/sim.cfg'. If this file is
4018
+   not found too, it reverts to the built-in default configuration.
4019
+
4020
+   NOTE: Users should not rely on the built-in configuration, since the
4021
+         default configuration may differ between version.
4022
+         Rather create a configuration file that sets all critical values.
4023
+
4024
+   This file may contain (standard C) comments only - no // support.
4025
+
4026
+   Configure files may be be included, using:
4027
+   include "file_name_to_include"
4028
+
4029
+   Like normal configuration files, the included file is divided into
4030
+   sections. Each section is described in detail also.
4031
+
4032
+   Some section have subsections. One example of such a subsection is:
4033
+
4034
+   device <index>
4035
+     instance specific parameters...
4036
+   enddevice
4037
+
4038
+   which creates a device instance.
4039
+*/
4040
+
4041
+
4042
+/* MEMORY SECTION
4043
+
4044
+   This section specifies how the memory is generated and the blocks
4045
+   it consists of.
4046
+
4047
+   type = random/unknown/pattern
4048
+      Specifies the initial memory values.
4049
+      'random' generates random memory using seed 'random_seed'.
4050
+      'pattern' fills memory with 'pattern'.
4051
+      'unknown' does not specify how memory should be generated,
4052
+      leaving the memory in a undefined state. This is the fastest
4053
+      option.
4054
+
4055
+   random_seed = <value>
4056
+      random seed for randomizer, used if type = 'random'.
4057
+
4058
+   pattern = <value>
4059
+      pattern to fill memory, used if type = 'pattern'.
4060
+
4061
+   nmemories = <value>
4062
+      number of memory instances connected
4063
+
4064
+   baseaddr = <hex_value>
4065
+      memory start address
4066
+
4067
+   size = <hex_value>
4068
+      memory size
4069
+
4070
+   name = "<string>"
4071
+      memory block name
4072
+
4073
+   ce = <value>
4074
+      chip enable index of the memory instance
4075
+
4076
+   mc = <value>
4077
+      memory controller this memory is connected to
4078
+
4079
+   delayr = <value>
4080
+      cycles, required for read access, -1 if instance does not support reading
4081
+
4082
+   delayw = <value>
4083
+      cycles, required for write access, -1 if instance does not support writing
4084
+
4085
+   log = "<filename>"
4086
+      filename, where to log memory accesses to, no log, if log command is not specified
4087
+*/
4088
+
4089
+
4090
+section memory
4091
+  /*random_seed = 12345
4092
+  type = random*/
4093
+  pattern = 0x00
4094
+  type = unknown /* Fastest */
4095
+
4096
+  name = "FLASH"
4097
+  ce = 0
4098
+  mc = 0
4099
+  baseaddr = 0xf0000000
4100
+  size = 0x01000000
4101
+  delayr = 10
4102
+  delayw = -1
4103
+end
4104
+
4105
+section memory
4106
+  /*random_seed = 12345
4107
+  type = random*/
4108
+  pattern = 0x00
4109
+  type = unknown /* Fastest */
4110
+
4111
+  name = "RAM"
4112
+  ce = 1
4113
+  mc = 0
4114
+  baseaddr = 0x00000000
4115
+  size = 0x02000000
4116
+  delayr = 20
4117
+  delayw = 25
4118
+end
4119
+
4120
+section memory
4121
+  /*random_seed = 12345
4122
+  type = random*/
4123
+  pattern = 0x00
4124
+  type = unknown /* Fastest */
4125
+
4126
+  name = "SRAM"
4127
+  mc = 0
4128
+  ce = 2
4129
+  baseaddr = 0xa4000000
4130
+  size = 0x00100000
4131
+  delayr = 1
4132
+  delayw = 2
4133
+end
4134
+
4135
+
4136
+/* IMMU SECTION
4137
+
4138
+    This section configures the Instruction Memory Manangement Unit
4139
+
4140
+    enabled = 0/1
4141
+       '0': disabled
4142
+       '1': enabled
4143
+       (NOTE: UPR bit is set)
4144
+
4145
+    nsets = <value>
4146
+       number of ITLB sets; must be power of two
4147
+
4148
+    nways = <value>
4149
+       number of ITLB ways
4150
+
4151
+    pagesize = <value>
4152
+       instruction page size; must be power of two
4153
+
4154
+    entrysize = <value>
4155
+       instruction entry size in bytes
4156
+
4157
+    ustates = <value>
4158
+       number of ITLB usage states (2, 3, 4 etc., max is 4)
4159
+
4160
+    hitdelay = <value>
4161
+       number of cycles immu hit costs
4162
+
4163
+    missdelay = <value>
4164
+       number of cycles immu miss costs
4165
+*/
4166
+
4167
+section immu
4168
+  enabled = 1
4169
+  nsets = 64
4170
+  nways = 1
4171
+  pagesize = 8192
4172
+  hitdelay = 0
4173
+  missdelay = 0
4174
+end
4175
+
4176
+
4177
+/* DMMU SECTION
4178
+
4179
+    This section configures the Data Memory Manangement Unit
4180
+
4181
+    enabled = 0/1
4182
+       '0': disabled
4183
+       '1': enabled
4184
+       (NOTE: UPR bit is set)
4185
+
4186
+    nsets = <value>
4187
+       number of DTLB sets; must be power of two
4188
+
4189
+    nways = <value>
4190
+       number of DTLB ways
4191
+
4192
+    pagesize = <value>
4193
+       data page size; must be power of two
4194
+
4195
+    entrysize = <value>
4196
+       data entry size in bytes
4197
+
4198
+    ustates = <value>
4199
+       number of DTLB usage states (2, 3, 4 etc., max is 4)
4200
+
4201
+    hitdelay = <value>
4202
+       number of cycles dmmu hit costs
4203
+
4204
+    missdelay = <value>
4205
+       number of cycles dmmu miss costs
4206
+*/
4207
+
4208
+section dmmu
4209
+  enabled = 1
4210
+  nsets = 64
4211
+  nways = 1
4212
+  pagesize = 8192
4213
+  hitdelay = 0
4214
+  missdelay = 0
4215
+end
4216
+
4217
+
4218
+/* IC SECTION
4219
+
4220
+   This section configures the Instruction Cache
4221
+
4222
+   enabled = 0/1
4223
+       '0': disabled
4224
+       '1': enabled
4225
+      (NOTE: UPR bit is set)
4226
+
4227
+   nsets = <value>
4228
+      number of IC sets; must be power of two
4229
+
4230
+   nways = <value>
4231
+      number of IC ways
4232
+
4233
+   blocksize = <value>
4234
+      IC block size in bytes; must be power of two
4235
+
4236
+   ustates = <value>
4237
+      number of IC usage states (2, 3, 4 etc., max is 4)
4238
+
4239
+   hitdelay = <value>
4240
+      number of cycles ic hit costs
4241
+
4242
+    missdelay = <value>
4243
+      number of cycles ic miss costs
4244
+*/
4245
+
4246
+section ic
4247
+  enabled = 0
4248
+  nsets = 512
4249
+  nways = 1
4250
+  blocksize = 16
4251
+  hitdelay = 20
4252
+  missdelay = 20
4253
+end
4254
+
4255
+
4256
+/* DC SECTION
4257
+
4258
+   This section configures the Data Cache
4259
+
4260
+   enabled = 0/1
4261
+       '0': disabled
4262
+       '1': enabled
4263
+      (NOTE: UPR bit is set)
4264
+
4265
+   nsets = <value>
4266
+      number of DC sets; must be power of two
4267
+
4268
+   nways = <value>
4269
+      number of DC ways
4270
+
4271
+   blocksize = <value>
4272
+      DC block size in bytes; must be power of two
4273
+
4274
+   ustates = <value>
4275
+      number of DC usage states (2, 3, 4 etc., max is 4)
4276
+
4277
+   load_hitdelay = <value>
4278
+      number of cycles dc load hit costs
4279
+
4280
+   load_missdelay = <value>
4281
+      number of cycles dc load miss costs
4282
+
4283
+   store_hitdelay = <value>
4284
+      number of cycles dc load hit costs
4285
+
4286
+   store_missdelay = <value>
4287
+      number of cycles dc load miss costs
4288
+*/
4289
+
4290
+section dc
4291
+  enabled = 0
4292
+  nsets = 512
4293
+  nways = 1
4294
+  blocksize = 16
4295
+  load_hitdelay = 20
4296
+  load_missdelay = 20
4297
+  store_hitdelay = 20
4298
+  store_missdelay = 20
4299
+end
4300
+
4301
+
4302
+/* SIM SECTION
4303
+
4304
+  This section specifies how or1ksim should behave.
4305
+
4306
+  verbose = 0/1
4307
+       '0': don't print extra messages
4308
+       '1': print extra messages
4309
+
4310
+  debug = 0-9
4311
+      0  : no debug messages
4312
+      1-9: debug message level.
4313
+           higher numbers produce more messages
4314
+
4315
+  profile = 0/1
4316
+      '0': don't generate profiling file 'sim.profile'
4317
+      '1': don't generate profiling file 'sim.profile'
4318
+
4319
+  prof_fn = "<filename>"
4320
+      optional filename for the profiling file.
4321
+      valid only if 'profile' is set
4322
+
4323
+  mprofile = 0/1
4324
+      '0': don't generate memory profiling file 'sim.mprofile'
4325
+      '1': generate memory profiling file 'sim.mprofile'
4326
+
4327
+  mprof_fn = "<filename>"
4328
+      optional filename for the memory profiling file.
4329
+      valid only if 'mprofile' is set
4330
+
4331
+  history = 0/1
4332
+      '0': don't track execution flow
4333
+      '1': track execution flow
4334
+      Execution flow can be tracked for the simulator's
4335
+      'hist' command. Useful for back-trace debugging.
4336
+
4337
+  iprompt = 0/1
4338
+     '0': start in <not interactive prompt> (so what do we start in ???)
4339
+     '1': start in interactive prompt.
4340
+
4341
+  exe_log = 0/1
4342
+      '0': don't generate execution log.
4343
+      '1': generate execution log.
4344
+
4345
+  exe_log = default/hardware/simple/software
4346
+      type of execution log, default is used when not specified
4347
+
4348
+  exe_log_start = <value>
4349
+      index of first instruction to start logging, default = 0
4350
+
4351
+  exe_log_end = <value>
4352
+      index of last instruction to end logging; not limited, if omitted
4353
+
4354
+  exe_log_marker = <value>
4355
+      <value> specifies number of instructions before horizontal marker is
4356
+      printed; if zero, markers are disabled (default)
4357
+
4358
+  exe_log_fn = "<filename>"
4359
+      filename for the exection log file.
4360
+      valid only if 'exe_log' is set
4361
+
4362
+  clkcycle = <value>[ps|ns|us|ms]
4363
+      specifies time measurement for one cycle
4364
+*/
4365
+
4366
+section sim
4367
+  verbose = 1
4368
+  debug = 0
4369
+  profile = 0
4370
+  history = 0
4371
+
4372
+  clkcycle = 10ns
4373
+end
4374
+
4375
+
4376
+/* SECTION VAPI
4377
+
4378
+    This section configures the Verification API, used for Advanced
4379
+    Core Verification.
4380
+
4381
+    enabled = 0/1
4382
+        '0': disbable VAPI server
4383
+        '1': enable/start VAPI server
4384
+
4385
+    server_port = <value>
4386
+        TCP/IP port to start VAPI server on
4387
+
4388
+    log_enabled = 0/1
4389
+       '0': disable VAPI requests logging
4390
+       '1': enable VAPI requests logging
4391
+
4392
+    hide_device_id = 0/1
4393
+       '0': don't log device id (for compatability with old version)
4394
+       '1': log device id
4395
+
4396
+
4397
+    vapi_fn = <filename>
4398
+       filename for the log file.
4399
+       valid only if log_enabled is set
4400
+*/
4401
+
4402
+section VAPI
4403
+  enabled = 0
4404
+  server_port = 9998
4405
+  log_enabled = 0
4406
+  vapi_log_fn = "vapi.log"
4407
+end
4408
+
4409
+
4410
+/* CPU SECTION
4411
+
4412
+   This section specifies various CPU parameters.
4413
+
4414
+   ver = <value>
4415
+   rev = <value>
4416
+      specifies version and revision of the CPU used
4417
+
4418
+   upr = <value>
4419
+      changes the upr register
4420
+
4421
+   sr = <value>
4422
+      sets the initial Supervision Register value
4423
+
4424
+   superscalar = 0/1
4425
+      '0': CPU is scalar
4426
+      '1': CPU is superscalar
4427
+      (modify cpu/or32/execute.c to tune superscalar model)
4428
+
4429
+   hazards = 0/1
4430
+      '0': don't track data hazards in superscalar CPU
4431
+      '1': track data hazards in superscalar CPU
4432
+      If tracked, data hazards can be displayed using the
4433
+      simulator's 'r' command.
4434
+
4435
+   dependstats = 0/1
4436
+      '0': don't calculate inter-instruction dependencies.
4437
+      '1': calculate inter-instruction dependencies.
4438
+      If calculated, inter-instruction dependencies can be
4439
+      displayed using the simulator's 'stat' command.
4440
+
4441
+   sbuf_len = <value>
4442
+      length of store buffer (<= 256), 0 = disabled
4443
+*/
4444
+
4445
+section cpu
4446
+  ver = 0x12
4447
+  cfg = 0x00
4448
+  rev = 0x01
4449
+  /* upr = */
4450
+  superscalar = 0
4451
+  hazards = 0
4452
+  dependstats = 0
4453
+  sbuf_len = 0
4454
+  hardfloat = 1
4455
+end
4456
+
4457
+
4458
+/* PM SECTION
4459
+
4460
+   This section specifies Power Management parameters
4461
+
4462
+   enabled = 0/1
4463
+      '0': disable power management
4464
+      '1': enable power management
4465
+*/
4466
+
4467
+section pm
4468
+  enabled = 0
4469
+end
4470
+
4471
+
4472
+/* BPB SECTION
4473
+
4474
+   This section specifies how branch prediction should behave.
4475
+
4476
+   enabled = 0/1
4477
+     '0': disable branch prediction
4478
+     '1': enable branch prediction
4479
+
4480
+   btic = 0/1
4481
+     '0': disable branch target instruction cache model
4482
+     '1': enable branch target instruction cache model
4483
+
4484
+   sbp_bf_fwd = 0/1
4485
+     Static branch prediction for 'l.bf'
4486
+     '0': don't use forward prediction
4487
+     '1': use forward prediction
4488
+
4489
+   sbp_bnf_fwd = 0/1
4490
+     Static branch prediction for 'l.bnf'
4491
+     '0': don't use forward prediction
4492
+     '1': use forward prediction
4493
+
4494
+   hitdelay = <value>
4495
+       number of cycles bpb hit costs
4496
+
4497
+   missdelay = <value>
4498
+       number of cycles bpb miss costs
4499
+*/
4500
+
4501
+section bpb
4502
+  enabled = 0
4503
+  btic = 0
4504
+  sbp_bf_fwd = 0
4505
+  sbp_bnf_fwd = 0
4506
+  hitdelay = 0
4507
+  missdelay = 0
4508
+end
4509
+
4510
+
4511
+/* DEBUG SECTION
4512
+
4513
+   This sections specifies how the debug unit should behave.
4514
+
4515
+   enabled = 0/1
4516
+      '0': disable debug unit
4517
+      '1': enable debug unit
4518
+
4519
+   gdb_enabled = 0/1
4520
+      '0': don't start gdb server
4521
+      '1': start gdb server at port 'server_port'
4522
+
4523
+   server_port = <value>
4524
+      TCP/IP port to start gdb server on
4525
+      valid only if gdb_enabled is set
4526
+
4527
+   vapi_id = <hex_value>
4528
+      Used to create "fake" vapi log file containing the JTAG proxy messages.
4529
+*/
4530
+section debug
4531
+  enabled = 1
4532
+  rsp_enabled = 1
4533
+  rsp_port = 5554
4534
+  /*server_port = 9999*/
4535
+end
4536
+
4537
+
4538
+/* MC SECTION
4539
+
4540
+   This section configures the memory controller
4541
+
4542
+   enabled = 0/1
4543
+     '0': disable memory controller
4544
+     '1': enable memory controller
4545
+
4546
+   baseaddr = <hex_value>
4547
+      address of first MC register
4548
+
4549
+   POC = <hex_value>
4550
+      Power On Configuration register
4551
+
4552
+   index = <value>
4553
+      Index of this memory controller amongst all the memory controllers
4554
+*/
4555
+
4556
+section mc
4557
+  enabled = 0
4558
+  baseaddr = 0x93000000
4559
+  POC = 0x00000008                 /* Power on configuration register */
4560
+  index = 0
4561
+end
4562
+
4563
+
4564
+/* UART SECTION
4565
+
4566
+   This section configures the UARTs
4567
+
4568
+     enabled = <0|1>
4569
+        Enable/disable the peripheral.  By default if it is enabled.
4570
+
4571
+     baseaddr = <hex_value>
4572
+        address of first UART register for this device
4573
+
4574
+
4575
+     channel = <channeltype>:<args>
4576
+
4577
+        The channel parameter indicates the source of received UART characters
4578
+        and the sink for transmitted UART characters.
4579
+
4580
+        The <channeltype> can be either "file", "xterm", "tcp", "fd", or "tty"
4581
+        (without quotes).
4582
+
4583
+          A) To send/receive characters from a pair of files, use a file
4584
+             channel:
4585
+
4586
+               channel=file:<rxfile>,<txfile>
4587
+
4588
+         B) To create an interactive terminal window, use an xterm channel:
4589
+
4590
+               channel=xterm:[<xterm_arg>]*
4591
+
4592
+         C) To create a bidirectional tcp socket which one could, for example,
4593
+             access via telnet, use a tcp channel:
4594
+
4595
+               channel=tcp:<port number>
4596
+
4597
+         D) To cause the UART to read/write from existing numeric file
4598
+             descriptors, use an fd channel:
4599
+
4600
+               channel=fd:<rx file descriptor num>,<tx file descriptor num>
4601
+
4602
+          E) To connect the UART to a physical serial port, create a tty
4603
+             channel:
4604
+
4605
+              channel=tty:device=/dev/ttyS0,baud=9600
4606
+
4607
+     irq = <value>
4608
+        irq number for this device
4609
+
4610
+     16550 = 0/1
4611
+        '0': this device is a UART16450
4612
+        '1': this device is a UART16550
4613
+
4614
+     jitter = <value>
4615
+        in msecs... time to block, -1 to disable it
4616
+
4617
+     vapi_id = <hex_value>
4618
+        VAPI id of this instance
4619
+*/
4620
+
4621
+section uart
4622
+  enabled = 1
4623
+  baseaddr = 0x90000000
4624
+  irq = 2
4625
+  channel = "file:uart0.rx,uart0.tx"
4626
+  /* channel = "tcp:10084" */
4627
+  /* channel = "xterm:" */
4628
+  jitter = -1                     /* async behaviour */
4629
+  16550 = 1
4630
+end
4631
+
4632
+
4633
+/* DMA SECTION
4634
+
4635
+   This section configures the DMAs
4636
+
4637
+     enabled = <0|1>
4638
+        Enable/disable the peripheral.  By default if it is enabled.
4639
+
4640
+     baseaddr = <hex_value>
4641
+        address of first DMA register for this device
4642
+
4643
+     irq = <value>
4644
+        irq number for this device
4645
+
4646
+     vapi_id = <hex_value>
4647
+        VAPI id of this instance
4648
+*/
4649
+
4650
+section dma
4651
+  enabled = 1
4652
+  baseaddr = 0x9a000000
4653
+  irq = 11
4654
+end
4655
+
4656
+
4657
+/* ETHERNET SECTION
4658
+
4659
+   This section configures the ETHERNETs
4660
+
4661
+     enabled = <0|1>
4662
+        Enable/disable the peripheral.  By default if it is enabled.
4663
+
4664
+     baseaddr = <hex_value>
4665
+        address of first ethernet register for this device
4666
+
4667
+     dma = <value>
4668
+        which controller is this ethernet "connected" to
4669
+
4670
+     irq = <value>
4671
+        ethernet mac IRQ level
4672
+
4673
+     rtx_type = <value>
4674
+        use 0 - file interface, 1 - socket interface
4675
+
4676
+     rx_channel = <value>
4677
+        DMA channel used for RX
4678
+
4679
+     tx_channel = <value>
4680
+        DMA channel used for TX
4681
+
4682
+     rxfile = "<filename>"
4683
+        filename, where to read data from
4684
+
4685
+     txfile = "<filename>"
4686
+        filename, where to write data to
4687
+
4688
+     sockif = "<ifacename>"
4689
+        interface name of ethernet socket
4690
+
4691
+     vapi_id = <hex_value>
4692
+        VAPI id of this instance
4693
+*/
4694
+
4695
+section ethernet
4696
+  enabled = 0
4697
+  baseaddr = 0x92000000
4698
+  /* dma = 0 */
4699
+  irq = 4
4700
+  rtx_type = 0
4701
+  /* tx_channel = 0 */
4702
+  /* rx_channel = 1 */
4703
+  /*rxfile = "eth0.rx"*/
4704
+  txfile = "eth0.tx"
4705
+  sockif = "eth0"
4706
+end
4707
+
4708
+
4709
+/* GPIO SECTION
4710
+
4711
+   This section configures the GPIOs
4712
+
4713
+     enabled = <0|1>
4714
+        Enable/disable the peripheral.  By default if it is enabled.
4715
+
4716
+     baseaddr = <hex_value>
4717
+        address of first GPIO register for this device
4718
+
4719
+     irq = <value>
4720
+        irq number for this device
4721
+
4722
+     base_vapi_id = <hex_value>
4723
+        first VAPI id of this instance
4724
+       GPIO uses 8 consecutive VAPI IDs
4725
+*/
4726
+
4727
+section gpio
4728
+  enabled = 0
4729
+  baseaddr = 0x91000000
4730
+  irq = 3
4731
+  base_vapi_id = 0x0200
4732
+end
4733
+
4734
+/* VGA SECTION
4735
+
4736
+    This section configures the VGA/LCD controller
4737
+
4738
+      enabled = <0|1>
4739
+        Enable/disable the peripheral.  By default if it is enabled.
4740
+
4741
+      baseaddr = <hex_value>
4742
+        address of first VGA register
4743
+
4744
+      irq = <value>
4745
+        irq number for this device
4746
+
4747
+      refresh_rate = <value>
4748
+        number of cycles between screen dumps
4749
+
4750
+      filename = "<filename>"
4751
+        template name for generated names (e.g. "primary" produces "primary0023.bmp")
4752
+*/
4753
+
4754
+section vga
4755
+  enabled = 1
4756
+  baseaddr = 0x97100000
4757
+  irq = 8
4758
+  refresh_rate = 100000
4759
+  filename = "primary"
4760
+end
4761
+
4762
+
4763
+/* TICK TIMER SECTION
4764
+
4765
+    This section configures tick timer
4766
+
4767
+    enabled = 0/1
4768
+      whether tick timer is enabled
4769
+*/
4770
+
4771
+section pic
4772
+  enabled = 1
4773
+  edge_trigger = 1
4774
+end
4775
+
4776
+/* FB SECTION
4777
+
4778
+    This section configures the frame buffer
4779
+
4780
+    enabled = <0|1>
4781
+      Enable/disable the peripheral.  By default if it is enabled.
4782
+
4783
+    baseaddr = <hex_value>
4784
+      base address of frame buffer
4785
+
4786
+    paladdr = <hex_value>
4787
+      base address of first palette entry
4788
+
4789
+    refresh_rate = <value>
4790
+      number of cycles between screen dumps
4791
+
4792
+    filename = "<filename>"
4793
+      template name for generated names (e.g. "primary" produces "primary0023.bmp")
4794
+*/
4795
+
4796
+section fb
4797
+  enabled = 1
4798
+  baseaddr = 0x97000000
4799
+  refresh_rate = 1000000
4800
+  filename = "primary"
4801
+end
4802
+
4803
+
4804
+/* KBD SECTION
4805
+
4806
+    This section configures the PS/2 compatible keyboard
4807
+
4808
+    baseaddr = <hex_value>
4809
+      base address of the keyboard device
4810
+
4811
+    rxfile = "<filename>"
4812
+      filename, where to read data from
4813
+*/
4814
+
4815
+section kbd
4816
+  enabled = 1
4817
+  irq = 5
4818
+  baseaddr = 0x94000000
4819
+  rxfile = "kbd.rx"
4820
+end
4821
+
4822
+
4823
+/* ATA SECTION
4824
+
4825
+    This section configures the ATA/ATAPI host controller
4826
+
4827
+      baseaddr = <hex_value>
4828
+        address of first ATA register
4829
+
4830
+      enabled = <0|1>
4831
+        Enable/disable the peripheral.  By default if it is enabled.
4832
+
4833
+      irq = <value>
4834
+        irq number for this device
4835
+
4836
+      debug = <value>
4837
+        debug level for ata models.
4838
+       0: no debug messages
4839
+       1: verbose messages
4840
+       3: normal messages (more messages than verbose)
4841
+        5: debug messages (normal debug messages)
4842
+       7: flow control messages (debug statemachine flows)
4843
+       9: low priority message (display everything the code does)
4844
+
4845
+      dev_type0/1 = <value>
4846
+        ata device 0 type
4847
+        0: NO_CONNeCT: none (not connected)
4848
+       1: FILE      : simulated harddisk
4849
+       2: LOCAL     : local system harddisk
4850
+
4851
+      dev_file0/1 = "<filename>"
4852
+        filename for simulated ATA device
4853
+       valid only if dev_type0 == 1
4854
+
4855
+      dev_size0/1 = <value>
4856
+        size of simulated hard-disk (in MBytes)
4857
+       valid only if dev_type0 == 1
4858
+
4859
+      dev_packet0/1 = <value>
4860
+        0: simulated ATA device does NOT implement PACKET command feature set
4861
+       1: simulated ATA device does implement PACKET command feature set
4862
+
4863
+   FIXME: irq number
4864
+*/
4865
+
4866
+section ata
4867
+  enabled = 0
4868
+  baseaddr = 0x9e000000
4869
+  irq = 15
4870
+
4871
+end
4872
+
4873
+
4874
diff -Naur --exclude=.git --exclude=.gitignore x264/x264.c x264-or/x264.c
4875
--- x264/x264.c 2009-10-25 17:41:22.000000000 +0100
4876
+++ x264-or/x264.c      2009-11-15 17:35:30.000000000 +0100
4877
@@ -40,6 +40,8 @@
4878
 #define SetConsoleTitle(t)
4879
 #endif
4880
 
4881
+//#include "uart.h"
4882
+
4883
 uint8_t *mux_buffer = NULL;
4884
 int mux_buffer_size = 0;
4885
 
4886
@@ -54,11 +56,11 @@
4887
 }
4888
 
4889
 typedef struct {
4890
-    int b_progress;
4891
-    int i_seek;
4892
-    hnd_t hin;
4893
-    hnd_t hout;
4894
-    FILE *qpfile;
4895
+  int b_progress;
4896
+  int i_seek;
4897
+  hnd_t hin; /* hnd_t is a void* */
4898
+  hnd_t hout;
4899
+  FILE *qpfile;
4900
 } cli_opt_t;
4901
 
4902
 /* input file operation function pointers */
4903
@@ -78,15 +80,28 @@
4904
 static int  Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
4905
 static int  Encode( x264_param_t *param, cli_opt_t *opt );
4906
 
4907
-
4908
+int yuv_data_size;
4909
 /****************************************************************************
4910
  * main:
4911
  ****************************************************************************/
4912
 int main( int argc, char **argv )
4913
 {
4914
-    x264_param_t param;
4915
-    cli_opt_t opt;
4916
-    int ret;
4917
+  char* new_argv ="./x264 --profile=baseline";
4918
+  argc = 2;
4919
+
4920
+  x264_param_t param;
4921
+  cli_opt_t opt;
4922
+  int ret;
4923
+
4924
+  //uart_init()  ;
4925
+
4926
+  extern int _end;   // start of free memory
4927
+  extern int _stack; // end of free memory
4928
+
4929
+  V(fprintf( stderr, "start of heap at 0x%.8x, size of heap: %d bytes\n",
4930
+            (unsigned int) &_end, (unsigned int) ((unsigned int) &_stack) - ((unsigned int) &_end)));
4931
+
4932
+  //fprintf(stderr,"start\n");
4933
 
4934
 #ifdef PTW32_STATIC_LIB
4935
     pthread_win32_process_attach_np();
4936
@@ -100,14 +115,90 @@
4937
 
4938
     x264_param_default( &param );
4939
 
4940
-    /* Parse command line */
4941
-    if( Parse( argc, argv, &param, &opt ) < 0 )
4942
-        return -1;
4943
+    // Baseline parameters
4944
+     param.analyse.b_transform_8x8 = 0;
4945
+     param.b_cabac = 0;
4946
+     param.i_cqm_preset = X264_CQM_FLAT;
4947
+     param.i_bframe = 0;
4948
+     /*
4949
+     // Ultrafast preset
4950
+     param.i_frame_reference = 1;
4951
+     param.i_scenecut_threshold = 0;
4952
+     param.b_deblocking_filter = 0;
4953
+     param.b_cabac = 0;
4954
+     param.i_bframe = 0;
4955
+     param.analyse.intra = 0;
4956
+     param.analyse.inter = 0;
4957
+     param.analyse.b_transform_8x8 = 0;
4958
+     param.analyse.i_me_method = X264_ME_DIA;
4959
+     param.analyse.i_subpel_refine = 0;
4960
+     param.rc.i_aq_mode = 0;
4961
+     param.analyse.b_mixed_references = 0;
4962
+     param.analyse.i_trellis = 0;
4963
+     param.i_bframe_adaptive = X264_B_ADAPT_NONE;
4964
+     param.rc.b_mb_tree = 0;
4965
+     */
4966
+     // Fast preset, standard
4967
+     /*
4968
+     param.i_frame_reference = 2;
4969
+     param.analyse.i_subpel_refine = 6;
4970
+     param.rc.i_lookahead = 30;
4971
+     */
4972
+     // Fast preset, but with 0 lookahead
4973
+     param.i_frame_reference = 2;
4974
+     param.analyse.i_subpel_refine = 6;
4975
+     param.rc.i_lookahead = 0;
4976
+
4977
+     // Verbose (info per frame encoded)
4978
+     param.i_log_level = X264_LOG_DEBUG;
4979
+
4980
+     // Set a bitrate
4981
+     param.rc.i_bitrate = 500; // kbps
4982
+     param.rc.i_rc_method = X264_RC_ABR;
4983
+
4984
+     // A smaller range of QP (qp_max - qp_min) means less startup time for computing motion vectors costs (not sure exactly how this is done) --jb
4985
+     // Defaults here were i_qp_min = 10, i_qp_max = 51
4986
+     param.rc.i_qp_min = 10;
4987
+     param.rc.i_qp_max = 51;
4988
+     param.rc.i_qp_step = 4;
4989
+
4990
+
4991
+     // Picture height and width
4992
+     // CIF = 352x288
4993
+     param.i_width = 352;
4994
+     param.i_height = 288;
4995
+     V(fprintf( stderr, "x264 [info]: %dx%d @ %.2f fps\n",
4996
+               param.i_width, param.i_height,
4997
+               (float)param.i_fps_num / (float)param.i_fps_den));
4998
+
4999
+     // VBV buffer size in kbits
5000
+     param.rc.i_vbv_buffer_size = (352*288*12*4)/1024; // 4 frames of VBV (?!)
5001
+
5002
+
5003
+
5004
+     V(printf("video file in memory from 0x%.8x, size %d bytes\n", YUV_DATA_ADDR, YUV_DATA_SIZE));
5005
+     /* Must define a few things for use of static CIF data
5006
+       YUV_DATA_ADDR - address where the data starts
5007
+       YUV_DATA_SIZE - size of data in bytes
5008
+       ENC_OUT_ADDR  - where we'll store the encoded data
5009
+     */
5010
+
5011
+     /* Parse command line */
5012
+     Parse( argc, &new_argv, &param, &opt );
5013
+     //if( Parse( argc, &new_argv, &param, &opt ) < 0 )
5014
+     //return -1;
5015
+
5016
+     // We specify a place in memory of some YUV CIF data instad of opening a file
5017
+     init_yuv_dataspace((char *)YUV_DATA_ADDR, (hnd_t *)&opt.hin, &param);
5018
+#ifdef ENC_OUT_ADDR
5019
+     p_open_outfile( 0, &opt.hout ); // Setup the out "file" which is really just a spot in memory
5020
+#endif
5021
+     //opt.hout = (void*) ENC_OUT_ADDR; // We specify a place in memory of where we'll dump the encoded data
5022
 
5023
-    /* Control-C handler */
5024
-    signal( SIGINT, SigIntHandler );
5025
+     /* Control-C handler */
5026
+     //signal( SIGINT, SigIntHandler );
5027
 
5028
-    ret = Encode( &param, &opt );
5029
+   ret = Encode( &param, &opt );
5030
 
5031
 #ifdef PTW32_STATIC_LIB
5032
     pthread_win32_thread_detach_np();
5033
@@ -555,10 +646,12 @@
5034
     /* Default output file driver */
5035
     p_open_outfile = open_file_bsf;
5036
     p_set_outfile_param = set_param_bsf;
5037
-    p_write_nalu = write_nalu_bsf;
5038
+    p_write_nalu = write_nalu_bsf; //--jb
5039
     p_set_eop = set_eop_bsf;
5040
     p_close_outfile = close_file_bsf;
5041
 
5042
+    return 0; // -- added jb
5043
+
5044
     /* Presets are applied before all other options. */
5045
     for( optind = 0;; )
5046
     {
5047
@@ -978,9 +1071,11 @@
5048
         }
5049
         else
5050
         {
5051
-            sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
5052
-            if( param->i_log_level >= X264_LOG_INFO )
5053
-                fprintf( stderr, "x264 [info]: %dx%d @ %.2f fps\n", param->i_width, param->i_height, (double)param->i_fps_num / (double)param->i_fps_den);
5054
+         sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
5055
+         if( param->i_log_level >= X264_LOG_INFO )
5056
+           fprintf( stderr, "x264 [info]: %dx%d @ %.2f fps\n",
5057
+                    param->i_width, param->i_height,
5058
+                    (double)param->i_fps_num / (double)param->i_fps_den);
5059
         }
5060
     }
5061
 
5062
@@ -1127,7 +1222,10 @@
5063
     {
5064
         i_nalu_size = p_write_nalu( hout, nal[i].p_payload, nal[i].i_payload );
5065
         if( i_nalu_size < 0 )
5066
+         {
5067
+           fprintf(stderr, "Encode_frame: p_write_nalu() returned i_nalu_size < 0: %d\n", i_nalu_size);
5068
             return -1;
5069
+         }
5070
         i_file += i_nalu_size;
5071
     }
5072
     if (i_nal)
5073
@@ -1172,12 +1270,15 @@
5074
     opt->b_progress &= param->i_log_level < X264_LOG_DEBUG;
5075
     i_frame_total = p_get_frame_total( opt->hin );
5076
     i_frame_total -= opt->i_seek;
5077
+
5078
     if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total )
5079
         && param->i_frame_total > 0 )
5080
         i_frame_total = param->i_frame_total;
5081
+
5082
     param->i_frame_total = i_frame_total;
5083
+    V(fprintf(stderr, "Encode: i_frame_total:%d\n", param->i_frame_total));
5084
     i_update_interval = i_frame_total ? x264_clip3( i_frame_total / 1000, 1, 10 ) : 10;
5085
-
5086
+    V(fprintf(stderr, "Encode: i_update_interval:%d\n", i_update_interval));
5087
     if( ( h = x264_encoder_open( param ) ) == NULL )
5088
     {
5089
         fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
5090
@@ -1202,6 +1303,7 @@
5091
 
5092
     i_start = x264_mdate();
5093
 
5094
+    V(fprintf(stderr, "Starting Encoder loop: i_frame_total %d\n", i_frame_total));
5095
     /* Encode frames */
5096
     for( i_frame = 0, i_file = 0, i_frame_output = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); )
5097
     {
5098
diff -Naur --exclude=.git --exclude=.gitignore x264/x264.h x264-or/x264.h
5099
--- x264/x264.h 2009-10-25 17:41:22.000000000 +0100
5100
+++ x264-or/x264.h      2009-10-28 15:05:29.000000000 +0100
5101
@@ -24,6 +24,29 @@
5102
 #ifndef X264_X264_H
5103
 #define X264_X264_H
5104
 
5105
+extern int yuv_data_start;
5106
+extern int yuv_data_end;
5107
+//#define YUV_DATA_ADDR 0x01000000
5108
+#define YUV_DATA_ADDR &yuv_data_start
5109
+#define YUV_DATA_SIZE ((int)(&yuv_data_end) - (int)(YUV_DATA_ADDR))
5110
+#define ENC_OUT_ADDR &yuv_data_end
5111
+
5112
+//#define USE_HARDCODED_FRAME_NUM
5113
+#define HARDCODED_FRAME_NUM 10
5114
+
5115
+
5116
+
5117
+/* Enable for verbose output during runtime */
5118
+//#define X264_VERBOSE
5119
+#ifdef X264_VERBOSE
5120
+#define V(x) x
5121
+#else
5122
+#define V(x)
5123
+#endif
5124
+
5125
+
5126
+extern int yuv_data_size;
5127
+
5128
 #if !defined(_STDINT_H) && !defined(_STDINT_H_) && \
5129
     !defined(_INTTYPES_H) && !defined(_INTTYPES_H_)
5130
 # ifdef _MSC_VER

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.