· 8 years ago · Jun 03, 2017, 03:22 AM
1From aa1a8435000228c4d9e74da0f9fd3d16e85a3e80 Mon Sep 17 00:00:00 2001
2From: Loren Merritt <pengvado@akuvian.org>
3Date: Sat, 26 Jun 2010 20:55:59 -0700
4Subject: [PATCH 1/7] Simplify pixel_ads
5
6---
7 common/macroblock.c | 2 +-
8 common/x86/pixel-a.asm | 175 +++++++++++++++++------------------------------
9 encoder/me.c | 2 +-
10 3 files changed, 65 insertions(+), 114 deletions(-)
11
12diff --git a/common/macroblock.c b/common/macroblock.c
13index 8e9b06d..4561d8a 100644
14--- a/common/macroblock.c
15+++ b/common/macroblock.c
16@@ -341,7 +341,7 @@ int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
17 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
18 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
19 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
20- ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
21+ ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
22 scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
23 }
24 int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+3)&~3) * sizeof(int);
25diff --git a/common/x86/pixel-a.asm b/common/x86/pixel-a.asm
26index 78ca4c7..1756f86 100644
27--- a/common/x86/pixel-a.asm
28+++ b/common/x86/pixel-a.asm
29@@ -2142,34 +2142,24 @@ cglobal pixel_ssim_end4_sse2, 3,3,7
30 ; Successive Elimination ADS
31 ;=============================================================================
32
33-%macro ADS_START 1 ; unroll_size
34-%ifdef ARCH_X86_64
35- %define t0 r6
36+%macro ADS_START 0
37 %ifdef WIN64
38- mov r4, r4mp
39- movsxd r5, dword r5m
40+ movsxd r5, r5d
41 %endif
42- mov r10, rsp
43-%else
44- %define t0 r4
45- mov rbp, rsp
46-%endif
47- mov r0d, r5m
48- sub rsp, r0
49- sub rsp, %1*4-1
50- and rsp, ~15
51- mov t0, rsp
52+ mov r0d, r5d
53+ lea r6, [r4+r5+15]
54+ and r6, ~15;
55 shl r2d, 1
56 %endmacro
57
58-%macro ADS_END 1
59+%macro ADS_END 1 ; unroll_size
60 add r1, 8*%1
61 add r3, 8*%1
62- add t0, 4*%1
63+ add r6, 4*%1
64 sub r0d, 4*%1
65 jg .loop
66 %ifdef WIN64
67- RESTORE_XMM r10
68+ RESTORE_XMM rsp
69 %endif
70 jmp ads_mvs
71 %endmacro
72@@ -2180,14 +2170,14 @@ cglobal pixel_ssim_end4_sse2, 3,3,7
73 ; int pixel_ads4( int enc_dc[4], uint16_t *sums, int delta,
74 ; uint16_t *cost_mvx, int16_t *mvs, int width, int thresh )
75 ;-----------------------------------------------------------------------------
76-cglobal pixel_ads4_mmxext, 4,7
77+cglobal pixel_ads4_mmxext, 6,7
78 movq mm6, [r0]
79 movq mm4, [r0+8]
80 pshufw mm7, mm6, 0
81 pshufw mm6, mm6, 0xAA
82 pshufw mm5, mm4, 0
83 pshufw mm4, mm4, 0xAA
84- ADS_START 1
85+ ADS_START
86 .loop:
87 movq mm0, [r1]
88 movq mm1, [r1+16]
89@@ -2204,25 +2194,19 @@ cglobal pixel_ads4_mmxext, 4,7
90 ABS1 mm3, mm1
91 paddw mm0, mm2
92 paddw mm0, mm3
93-%ifdef WIN64
94- pshufw mm1, [r10+stack_offset+56], 0
95-%elifdef ARCH_X86_64
96- pshufw mm1, [r10+8], 0
97-%else
98- pshufw mm1, [ebp+stack_offset+28], 0
99-%endif
100+ pshufw mm1, r6m, 0
101 paddusw mm0, [r3]
102 psubusw mm1, mm0
103 packsswb mm1, mm1
104- movd [t0], mm1
105+ movd [r6], mm1
106 ADS_END 1
107
108-cglobal pixel_ads2_mmxext, 4,7
109+cglobal pixel_ads2_mmxext, 6,7
110 movq mm6, [r0]
111 pshufw mm5, r6m, 0
112 pshufw mm7, mm6, 0
113 pshufw mm6, mm6, 0xAA
114- ADS_START 1
115+ ADS_START
116 .loop:
117 movq mm0, [r1]
118 movq mm1, [r1+r2]
119@@ -2235,13 +2219,13 @@ cglobal pixel_ads2_mmxext, 4,7
120 movq mm4, mm5
121 psubusw mm4, mm0
122 packsswb mm4, mm4
123- movd [t0], mm4
124+ movd [r6], mm4
125 ADS_END 1
126
127-cglobal pixel_ads1_mmxext, 4,7
128+cglobal pixel_ads1_mmxext, 6,7
129 pshufw mm7, [r0], 0
130 pshufw mm6, r6m, 0
131- ADS_START 2
132+ ADS_START
133 .loop:
134 movq mm0, [r1]
135 movq mm1, [r1+8]
136@@ -2256,11 +2240,11 @@ cglobal pixel_ads1_mmxext, 4,7
137 psubusw mm4, mm0
138 psubusw mm5, mm1
139 packsswb mm4, mm5
140- movq [t0], mm4
141+ movq [r6], mm4
142 ADS_END 2
143
144 %macro ADS_SSE2 1
145-cglobal pixel_ads4_%1, 4,7,12
146+cglobal pixel_ads4_%1, 6,7,12
147 movdqa xmm4, [r0]
148 pshuflw xmm7, xmm4, 0
149 pshuflw xmm6, xmm4, 0xAA
150@@ -2273,7 +2257,7 @@ cglobal pixel_ads4_%1, 4,7,12
151 %ifdef ARCH_X86_64
152 pshuflw xmm8, r6m, 0
153 punpcklqdq xmm8, xmm8
154- ADS_START 2
155+ ADS_START
156 movdqu xmm10, [r1]
157 movdqu xmm11, [r1+r2]
158 .loop:
159@@ -2299,9 +2283,9 @@ cglobal pixel_ads4_%1, 4,7,12
160 movdqa xmm1, xmm8
161 psubusw xmm1, xmm0
162 packsswb xmm1, xmm1
163- movq [t0], xmm1
164+ movq [r6], xmm1
165 %else
166- ADS_START 2
167+ ADS_START
168 .loop:
169 movdqu xmm0, [r1]
170 movdqu xmm1, [r1+16]
171@@ -2318,18 +2302,18 @@ cglobal pixel_ads4_%1, 4,7,12
172 ABS1 xmm3, xmm1
173 paddw xmm0, xmm2
174 paddw xmm0, xmm3
175- movd xmm1, [ebp+stack_offset+28]
176+ movd xmm1, r6m
177 movdqu xmm2, [r3]
178 pshuflw xmm1, xmm1, 0
179 punpcklqdq xmm1, xmm1
180 paddusw xmm0, xmm2
181 psubusw xmm1, xmm0
182 packsswb xmm1, xmm1
183- movq [t0], xmm1
184+ movq [r6], xmm1
185 %endif ; ARCH
186 ADS_END 2
187
188-cglobal pixel_ads2_%1, 4,7,8
189+cglobal pixel_ads2_%1, 6,7,8
190 movq xmm6, [r0]
191 movd xmm5, r6m
192 pshuflw xmm7, xmm6, 0
193@@ -2338,7 +2322,7 @@ cglobal pixel_ads2_%1, 4,7,8
194 punpcklqdq xmm7, xmm7
195 punpcklqdq xmm6, xmm6
196 punpcklqdq xmm5, xmm5
197- ADS_START 2
198+ ADS_START
199 .loop:
200 movdqu xmm0, [r1]
201 movdqu xmm1, [r1+r2]
202@@ -2352,17 +2336,17 @@ cglobal pixel_ads2_%1, 4,7,8
203 movdqa xmm1, xmm5
204 psubusw xmm1, xmm0
205 packsswb xmm1, xmm1
206- movq [t0], xmm1
207+ movq [r6], xmm1
208 ADS_END 2
209
210-cglobal pixel_ads1_%1, 4,7,8
211+cglobal pixel_ads1_%1, 6,7,8
212 movd xmm7, [r0]
213 movd xmm6, r6m
214 pshuflw xmm7, xmm7, 0
215 pshuflw xmm6, xmm6, 0
216 punpcklqdq xmm7, xmm7
217 punpcklqdq xmm6, xmm6
218- ADS_START 4
219+ ADS_START
220 .loop:
221 movdqu xmm0, [r1]
222 movdqu xmm1, [r1+16]
223@@ -2379,7 +2363,7 @@ cglobal pixel_ads1_%1, 4,7,8
224 psubusw xmm4, xmm0
225 psubusw xmm5, xmm1
226 packsswb xmm4, xmm5
227- movdqa [t0], xmm4
228+ movdqa [r6], xmm4
229 ADS_END 4
230 %endmacro
231
232@@ -2401,90 +2385,57 @@ ADS_SSE2 ssse3
233 ; }
234 ; return nmv;
235 ; }
236+
237+%macro TEST 1
238+ mov [r4+r0*2], r1w
239+ test r2d, 0xff<<(%1*8)
240+ setne r3b
241+ add r0d, r3d
242+ inc r1d
243+%endmacro
244+
245 cglobal pixel_ads_mvs, 0,7,0
246 ads_mvs:
247-%ifdef ARCH_X86_64
248+ lea r6, [r4+r5+15]
249+ and r6, ~15;
250 ; mvs = r4
251- ; masks = rsp
252+ ; masks = r6
253 ; width = r5
254 ; clear last block in case width isn't divisible by 8. (assume divisible by 4, so clearing 4 bytes is enough.)
255-%ifdef WIN64
256- mov r8, r4
257- mov r9, r5
258-%endif
259- xor eax, eax
260- xor esi, esi
261- mov dword [rsp+r9], 0
262+ xor r0d, r0d
263+ xor r1d, r1d
264+ mov [r6+r5], r0d
265 jmp .loopi
266+ALIGN 16
267 .loopi0:
268- add esi, 8
269- cmp esi, r9d
270+ add r1d, 8
271+ cmp r1d, r5d
272 jge .end
273 .loopi:
274- mov rdi, [rsp+rsi]
275- test rdi, rdi
276+ mov r2, [r6+r1]
277+%ifdef ARCH_X86_64
278+ test r2, r2
279+%else
280+ mov r3, r2
281+ or r3d, [r6+r1+4]
282+%endif
283 jz .loopi0
284- xor ecx, ecx
285-%macro TEST 1
286- mov [r8+rax*2], si
287- test edi, 0xff<<(%1*8)
288- setne cl
289- add eax, ecx
290- inc esi
291-%endmacro
292+ xor r3d, r3d
293 TEST 0
294 TEST 1
295 TEST 2
296 TEST 3
297- shr rdi, 32
298+%ifdef ARCH_X86_64
299+ shr r2, 32
300+%else
301+ mov r2d, [r6+r1]
302+%endif
303 TEST 0
304 TEST 1
305 TEST 2
306 TEST 3
307- cmp esi, r9d
308- jl .loopi
309-.end:
310- mov rsp, r10
311- RET
312-
313-%else
314- xor eax, eax
315- xor esi, esi
316- mov ebx, [ebp+stack_offset+20] ; mvs
317- mov edi, [ebp+stack_offset+24] ; width
318- mov dword [esp+edi], 0
319- push ebp
320- jmp .loopi
321-.loopi0:
322- add esi, 8
323- cmp esi, edi
324- jge .end
325-.loopi:
326- mov ebp, [esp+esi+4]
327- mov edx, [esp+esi+8]
328- mov ecx, ebp
329- or ecx, edx
330- jz .loopi0
331- xor ecx, ecx
332-%macro TEST 2
333- mov [ebx+eax*2], si
334- test %2, 0xff<<(%1*8)
335- setne cl
336- add eax, ecx
337- inc esi
338-%endmacro
339- TEST 0, ebp
340- TEST 1, ebp
341- TEST 2, ebp
342- TEST 3, ebp
343- TEST 0, edx
344- TEST 1, edx
345- TEST 2, edx
346- TEST 3, edx
347- cmp esi, edi
348+ cmp r1d, r5d
349 jl .loopi
350 .end:
351- pop esp
352+ movifnidn eax, r0d
353 RET
354-%endif ; ARCH
355-
356diff --git a/encoder/me.c b/encoder/me.c
357index 291104a..19c5b2b 100644
358--- a/encoder/me.c
359+++ b/encoder/me.c
360@@ -609,7 +609,7 @@ void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc,
361 if( h->mb.i_me_method == X264_ME_TESA )
362 {
363 // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD
364- mvsad_t *mvsads = (mvsad_t *)(xs + ((width+15)&~15));
365+ mvsad_t *mvsads = (mvsad_t *)(xs + ((width+15)&~15) + 4);
366 int nmvsad = 0, limit;
367 int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;
368 int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+bmy*stride+bmx, stride )
369--
3701.7.1
371
372
373From 8b5d1903ccd13ed8f5141de0048601b4c020de98 Mon Sep 17 00:00:00 2001
374From: Jason Garrett-Glaser <darkshikari@gmail.com>
375Date: Mon, 28 Jun 2010 15:02:33 -0700
376Subject: [PATCH 2/7] Callback feature for low-latency per-slice output
377 Add a callback to allow the calling application to send slices immediately after being encoded.
378 Also add some extra information to the x264_nal_t structure to help inform such a calling application how the NAL units should be ordered.
379
380Full documentation is in x264.h.
381---
382 common/bitstream.c | 7 ++-
383 common/bitstream.h | 1 -
384 encoder/encoder.c | 26 ++++++++---
385 x264.h | 128 +++++++++++++++++++++++++++++++++-------------------
386 4 files changed, 105 insertions(+), 57 deletions(-)
387
388diff --git a/common/bitstream.c b/common/bitstream.c
389index 0aaac21..ad8c16e 100644
390--- a/common/bitstream.c
391+++ b/common/bitstream.c
392@@ -44,7 +44,7 @@ uint8_t *x264_nal_escape_sse2( uint8_t *dst, uint8_t *src, uint8_t *end );
393 /****************************************************************************
394 * x264_nal_encode:
395 ****************************************************************************/
396-int x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal, int b_long_startcode )
397+void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal )
398 {
399 uint8_t *src = nal->p_payload;
400 uint8_t *end = nal->p_payload + nal->i_payload;
401@@ -52,7 +52,7 @@ int x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal, int b_long_startc
402
403 if( h->param.b_annexb )
404 {
405- if( b_long_startcode )
406+ if( nal->b_long_startcode )
407 *dst++ = 0x00;
408 *dst++ = 0x00;
409 *dst++ = 0x00;
410@@ -77,7 +77,8 @@ int x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal, int b_long_startc
411 orig_dst[3] = size>> 0;
412 }
413
414- return size+4;
415+ nal->i_payload = size+4;
416+ nal->p_payload = orig_dst;
417 }
418
419 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf )
420diff --git a/common/bitstream.h b/common/bitstream.h
421index 9ce5bd7..dd8118d 100644
422--- a/common/bitstream.h
423+++ b/common/bitstream.h
424@@ -68,7 +68,6 @@ typedef struct
425 uint8_t *(*nal_escape) ( uint8_t *dst, uint8_t *src, uint8_t *end );
426 } x264_bitstream_function_t;
427
428-int x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal, int b_long_startcode );
429 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf );
430
431 /* A larger level table size theoretically could help a bit at extremely
432diff --git a/encoder/encoder.c b/encoder/encoder.c
433index f54fe85..fe97aef 100644
434--- a/encoder/encoder.c
435+++ b/encoder/encoder.c
436@@ -427,6 +427,8 @@ static int x264_validate_parameters( x264_t *h )
437 else
438 h->param.b_sliced_threads = 0;
439 h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads;
440+ if( h->i_thread_frames > 1 )
441+ h->param.nalu_process = NULL;
442
443 if( h->param.b_interlaced )
444 {
445@@ -1253,8 +1255,9 @@ static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
446 {
447 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
448
449- nal->i_ref_idc = i_ref_idc;
450- nal->i_type = i_type;
451+ nal->i_ref_idc = i_ref_idc;
452+ nal->i_type = i_type;
453+ nal->b_long_startcode = 1;
454
455 nal->i_payload= 0;
456 nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8];
457@@ -1280,6 +1283,8 @@ static int x264_nal_end( x264_t *h )
458 {
459 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
460 nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8] - nal->p_payload;
461+ if( h->param.nalu_process )
462+ h->param.nalu_process( h, nal );
463 h->out.i_nal++;
464
465 return x264_nal_check_buffer( h );
466@@ -1289,6 +1294,13 @@ static int x264_encoder_encapsulate_nals( x264_t *h, int start )
467 {
468 int nal_size = 0, previous_nal_size = 0;
469
470+ if( h->param.nalu_process )
471+ {
472+ for( int i = start; i < h->out.i_nal; i++ )
473+ nal_size += h->out.nal[i].i_payload;
474+ return nal_size;
475+ }
476+
477 for( int i = 0; i < start; i++ )
478 previous_nal_size += h->out.nal[i].i_payload;
479
480@@ -1311,11 +1323,9 @@ static int x264_encoder_encapsulate_nals( x264_t *h, int start )
481
482 for( int i = start; i < h->out.i_nal; i++ )
483 {
484- int long_startcode = !i || h->out.nal[i].i_type == NAL_SPS || h->out.nal[i].i_type == NAL_PPS;
485- int size = x264_nal_encode( h, nal_buffer, &h->out.nal[i], long_startcode );
486- h->out.nal[i].i_payload = size;
487- h->out.nal[i].p_payload = nal_buffer;
488- nal_buffer += size;
489+ h->out.nal[i].b_long_startcode = !i || h->out.nal[i].i_type == NAL_SPS || h->out.nal[i].i_type == NAL_PPS;
490+ x264_nal_encode( h, nal_buffer, &h->out.nal[i] );
491+ nal_buffer += h->out.nal[i].i_payload;
492 }
493
494 x264_emms();
495@@ -1805,6 +1815,7 @@ static int x264_slice_write( x264_t *h )
496
497 /* Slice */
498 x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );
499+ h->out.nal[h->out.i_nal].i_first_mb = h->sh.i_first_mb;
500
501 /* Slice header */
502 x264_macroblock_thread_init( h );
503@@ -2020,6 +2031,7 @@ static int x264_slice_write( x264_t *h )
504 i_mb_x = 0;
505 }
506 }
507+ h->out.nal[h->out.i_nal].i_last_mb = h->sh.i_last_mb;
508
509 if( h->param.b_cabac )
510 {
511diff --git a/x264.h b/x264.h
512index 1138a8b..6a663ed 100644
513--- a/x264.h
514+++ b/x264.h
515@@ -35,13 +35,61 @@
516
517 #include <stdarg.h>
518
519-#define X264_BUILD 100
520+#define X264_BUILD 101
521
522 /* x264_t:
523 * opaque handler for encoder */
524 typedef struct x264_t x264_t;
525
526 /****************************************************************************
527+ * NAL structure and functions
528+ ****************************************************************************/
529+
530+enum nal_unit_type_e
531+{
532+ NAL_UNKNOWN = 0,
533+ NAL_SLICE = 1,
534+ NAL_SLICE_DPA = 2,
535+ NAL_SLICE_DPB = 3,
536+ NAL_SLICE_DPC = 4,
537+ NAL_SLICE_IDR = 5, /* ref_idc != 0 */
538+ NAL_SEI = 6, /* ref_idc == 0 */
539+ NAL_SPS = 7,
540+ NAL_PPS = 8,
541+ NAL_AUD = 9,
542+ NAL_FILLER = 12,
543+ /* ref_idc == 0 for 6,9,10,11,12 */
544+};
545+enum nal_priority_e
546+{
547+ NAL_PRIORITY_DISPOSABLE = 0,
548+ NAL_PRIORITY_LOW = 1,
549+ NAL_PRIORITY_HIGH = 2,
550+ NAL_PRIORITY_HIGHEST = 3,
551+};
552+
553+/* The data within the payload is already NAL-encapsulated; the ref_idc and type
554+ * are merely in the struct for easy access by the calling application.
555+ * All data returned in an x264_nal_t, including the data in p_payload, is no longer
556+ * valid after the next call to x264_encoder_encode. Thus it must be used or copied
557+ * before calling x264_encoder_encode or x264_encoder_headers again. */
558+typedef struct
559+{
560+ int i_ref_idc; /* nal_priority_e */
561+ int i_type; /* nal_unit_type_e */
562+ int b_long_startcode;
563+ int i_first_mb; /* If this NAL is a slice, the index of the first MB in the slice. */
564+ int i_last_mb; /* If this NAL is a slice, the index of the last MB in the slice. */
565+
566+ /* Size of payload in bytes. */
567+ int i_payload;
568+ /* If param->b_annexb is set, Annex-B bytestream with 4-byte startcode.
569+ * Otherwise, startcode is replaced with a 4-byte size.
570+ * This size is the size used in mp4/similar muxing; it is equal to i_payload-4 */
571+ uint8_t *p_payload;
572+} x264_nal_t;
573+
574+/****************************************************************************
575 * Encoder parameters
576 ****************************************************************************/
577 /* CPU flags
578@@ -377,8 +425,41 @@ typedef struct x264_param_t
579 * i.e. when an x264_param_t is passed to x264_t in an x264_picture_t or in zones.
580 * Not used when x264_encoder_reconfig is called directly. */
581 void (*param_free)( void* );
582+
583+ /* Optional low-level callback for low-latency encoding. Called for each output NAL unit
584+ * immediately after the NAL unit is finished encoding. This allows the calling application
585+ * to begin processing video data (e.g. by sending packets over a network) before the frame
586+ * is done encoding.
587+ *
588+ * This callback MUST do the following in order to work correctly:
589+ * 1) Have available an output buffer of at least size nal->i_payload*3/2 + 5 + 16.
590+ * 2) Call x264_nal_encode( h, dst, nal ), where dst is the output buffer.
591+ * After these steps, the content of nal is valid and can be used in the same way as if
592+ * the NAL unit were output by x264_encoder_encode.
593+ *
594+ * This does not need to be synchronous with the encoding process: the data pointed to
595+ * by nal (both before and after x264_nal_encode) will remain valid until the next
596+ * x264_encoder_encode call.
597+ *
598+ * This callback does not work with frame-based threads; threads must be disabled
599+ * or sliced-threads enabled. This callback also does not work as one would expect
600+ * with HRD -- since the buffering period SEI cannot be calculated until the frame
601+ * is finished encoding, it will not be sent via this callback.
602+ *
603+ * Note also that the NALs are not necessarily returned in order when sliced threads is
604+ * enabled. Accordingly, the variable i_first_mb and i_last_mb are available in
605+ * x264_nal_t to help the calling application reorder the slices if necessary.
606+ *
607+ * When this callback is enabled, x264_encoder_encode does not return valid NALs;
608+ * the calling application is expected to acquire all output NALs through the callback.
609+ *
610+ * It is generally sensible to combine this callback with a use of slice-max-mbs or
611+ * slice-max-size. */
612+ void (*nalu_process) ( x264_t *h, x264_nal_t *nal );
613 } x264_param_t;
614
615+void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal );
616+
617 /****************************************************************************
618 * H.264 level restriction information
619 ****************************************************************************/
620@@ -586,51 +667,6 @@ int x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_heigh
621 void x264_picture_clean( x264_picture_t *pic );
622
623 /****************************************************************************
624- * NAL structure and functions
625- ****************************************************************************/
626-
627-enum nal_unit_type_e
628-{
629- NAL_UNKNOWN = 0,
630- NAL_SLICE = 1,
631- NAL_SLICE_DPA = 2,
632- NAL_SLICE_DPB = 3,
633- NAL_SLICE_DPC = 4,
634- NAL_SLICE_IDR = 5, /* ref_idc != 0 */
635- NAL_SEI = 6, /* ref_idc == 0 */
636- NAL_SPS = 7,
637- NAL_PPS = 8,
638- NAL_AUD = 9,
639- NAL_FILLER = 12,
640- /* ref_idc == 0 for 6,9,10,11,12 */
641-};
642-enum nal_priority_e
643-{
644- NAL_PRIORITY_DISPOSABLE = 0,
645- NAL_PRIORITY_LOW = 1,
646- NAL_PRIORITY_HIGH = 2,
647- NAL_PRIORITY_HIGHEST = 3,
648-};
649-
650-/* The data within the payload is already NAL-encapsulated; the ref_idc and type
651- * are merely in the struct for easy access by the calling application.
652- * All data returned in an x264_nal_t, including the data in p_payload, is no longer
653- * valid after the next call to x264_encoder_encode. Thus it must be used or copied
654- * before calling x264_encoder_encode or x264_encoder_headers again. */
655-typedef struct
656-{
657- int i_ref_idc; /* nal_priority_e */
658- int i_type; /* nal_unit_type_e */
659-
660- /* Size of payload in bytes. */
661- int i_payload;
662- /* If param->b_annexb is set, Annex-B bytestream with 4-byte startcode.
663- * Otherwise, startcode is replaced with a 4-byte size.
664- * This size is the size used in mp4/similar muxing; it is equal to i_payload-4 */
665- uint8_t *p_payload;
666-} x264_nal_t;
667-
668-/****************************************************************************
669 * Encoder functions
670 ****************************************************************************/
671
672--
6731.7.1
674
675
676From a90edcc40772e54d3cb2c3da65ce52d45c178c29 Mon Sep 17 00:00:00 2001
677From: Lamont Alston <wewk584@gmail.com>
678Date: Tue, 29 Jun 2010 10:11:42 -0700
679Subject: [PATCH 3/7] Make open-GOP Blu-ray compatible
680 Blu-ray is even more braindamaged than we thought.
681 Accordingly, open-gop options are now "normal" and "bluray", as opposed to display and coded.
682 Normal should be used in all cases besides Blu-ray authoring.
683
684---
685 encoder/encoder.c | 2 +-
686 encoder/slicetype.c | 28 +++++++---------------------
687 x264.c | 8 ++++----
688 x264.h | 8 ++++----
689 4 files changed, 16 insertions(+), 30 deletions(-)
690
691diff --git a/encoder/encoder.c b/encoder/encoder.c
692index fe97aef..5cd3307 100644
693--- a/encoder/encoder.c
694+++ b/encoder/encoder.c
695@@ -577,7 +577,7 @@ static int x264_validate_parameters( x264_t *h )
696 h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
697 }
698 h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_MIN( X264_BFRAME_MAX, h->param.i_keyint_max-1 ) );
699- h->param.i_open_gop = x264_clip3( h->param.i_open_gop, X264_OPEN_GOP_NONE, X264_OPEN_GOP_CODED_ORDER );
700+ h->param.i_open_gop = x264_clip3( h->param.i_open_gop, X264_OPEN_GOP_NONE, X264_OPEN_GOP_BLURAY );
701 if( h->param.i_keyint_max == 1 )
702 h->param.b_intra_refresh = 0;
703 h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
704diff --git a/encoder/slicetype.c b/encoder/slicetype.c
705index 2703f02..4ede8cf 100644
706--- a/encoder/slicetype.c
707+++ b/encoder/slicetype.c
708@@ -1233,17 +1233,11 @@ void x264_slicetype_analyse( x264_t *h, int keyframe )
709 if( !h->param.b_intra_refresh )
710 for( int i = keyint_limit+1; i <= num_frames; i += h->param.i_keyint_max )
711 {
712- int j = i;
713- if( h->param.i_open_gop == X264_OPEN_GOP_CODED_ORDER )
714- {
715- while( IS_X264_TYPE_B( frames[i]->i_type ) )
716- i++;
717- while( IS_X264_TYPE_B( frames[j-1]->i_type ) )
718- j--;
719- }
720 frames[i]->i_type = X264_TYPE_I;
721 reset_start = X264_MIN( reset_start, i+1 );
722- i = j;
723+ if( h->param.i_open_gop == X264_OPEN_GOP_BLURAY )
724+ while( IS_X264_TYPE_B( frames[i-1]->i_type ) )
725+ i--;
726 }
727
728 if( vbv_lookahead )
729@@ -1337,16 +1331,8 @@ void x264_slicetype_decide( x264_t *h )
730 if( frm->i_type == X264_TYPE_AUTO || frm->i_type == X264_TYPE_I )
731 frm->i_type = h->param.i_open_gop && h->lookahead->i_last_keyframe >= 0 ? X264_TYPE_I : X264_TYPE_IDR;
732 int warn = frm->i_type != X264_TYPE_IDR;
733- if( warn && h->param.i_open_gop == X264_OPEN_GOP_DISPLAY_ORDER )
734- warn &= frm->i_type != X264_TYPE_I && frm->i_type != X264_TYPE_KEYFRAME;
735- if( warn && h->param.i_open_gop == X264_OPEN_GOP_CODED_ORDER )
736- {
737- /* if this minigop ends with i, it's not a violation */
738- int j = bframes;
739- while( IS_X264_TYPE_B( h->lookahead->next.list[j]->i_type ) )
740- j++;
741- warn = h->lookahead->next.list[j]->i_type != X264_TYPE_I && h->lookahead->next.list[j]->i_type != X264_TYPE_KEYFRAME;
742- }
743+ if( warn && h->param.i_open_gop )
744+ warn &= frm->i_type != X264_TYPE_I;
745 if( warn )
746 x264_log( h, X264_LOG_WARNING, "specified frame type (%d) at %d is not compatible with keyframe interval\n", frm->i_type, frm->i_frame );
747 }
748@@ -1355,8 +1341,8 @@ void x264_slicetype_decide( x264_t *h )
749 if( h->param.i_open_gop )
750 {
751 h->lookahead->i_last_keyframe = frm->i_frame; // Use display order
752- if( h->param.i_open_gop == X264_OPEN_GOP_CODED_ORDER )
753- h->lookahead->i_last_keyframe -= bframes; // Use coded order
754+ if( h->param.i_open_gop == X264_OPEN_GOP_BLURAY )
755+ h->lookahead->i_last_keyframe -= bframes; // Use bluray order
756 frm->b_keyframe = 1;
757 }
758 else
759diff --git a/x264.c b/x264.c
760index df04385..f08ab41 100644
761--- a/x264.c
762+++ b/x264.c
763@@ -382,10 +382,10 @@ static void Help( x264_param_t *defaults, int longhelp )
764 " - normal: Non-strict (not Blu-ray compatible)\n",
765 strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) );
766 H1( " --open-gop <string> Use recovery points to close GOPs [none]\n"
767- " - none: Use standard closed GOPs\n"
768- " - display: Base GOP length on display order\n"
769- " (not Blu-ray compatible)\n"
770- " - coded: Base GOP length on coded order\n"
771+ " - none: closed GOPs only\n"
772+ " - normal: standard open GOPs\n"
773+ " (not Blu-ray compatible)\n"
774+ " - bluray: Blu-ray-compatible open GOPs\n"
775 " Only available with b-frames\n" );
776 H1( " --no-cabac Disable CABAC\n" );
777 H1( " -r, --ref <integer> Number of reference frames [%d]\n", defaults->i_frame_reference );
778diff --git a/x264.h b/x264.h
779index 6a663ed..604ca45 100644
780--- a/x264.h
781+++ b/x264.h
782@@ -153,8 +153,8 @@ typedef struct
783 #define X264_B_PYRAMID_NORMAL 2
784 #define X264_KEYINT_MIN_AUTO 0
785 #define X264_OPEN_GOP_NONE 0
786-#define X264_OPEN_GOP_DISPLAY_ORDER 1
787-#define X264_OPEN_GOP_CODED_ORDER 2
788+#define X264_OPEN_GOP_NORMAL 1
789+#define X264_OPEN_GOP_BLURAY 2
790
791 static const char * const x264_direct_pred_names[] = { "none", "spatial", "temporal", "auto", 0 };
792 static const char * const x264_motion_est_names[] = { "dia", "hex", "umh", "esa", "tesa", 0 };
793@@ -166,7 +166,7 @@ static const char * const x264_colorprim_names[] = { "", "bt709", "undef", "", "
794 static const char * const x264_transfer_names[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", 0 };
795 static const char * const x264_colmatrix_names[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", 0 };
796 static const char * const x264_nal_hrd_names[] = { "none", "vbr", "cbr", 0 };
797-static const char * const x264_open_gop_names[] = { "none", "display", "coded", 0 };
798+static const char * const x264_open_gop_names[] = { "none", "normal", "bluray", 0 };
799
800 /* Colorspace type
801 * legacy only; nothing other than I420 is really supported. */
802@@ -276,7 +276,7 @@ typedef struct x264_param_t
803 int i_bframe_adaptive;
804 int i_bframe_bias;
805 int i_bframe_pyramid; /* Keep some B-frames as references: 0=off, 1=strict hierarchical, 2=normal */
806- int i_open_gop; /* Open gop: 1=display order, 2=coded order to determine gop size */
807+ int i_open_gop; /* Open gop: 1=display order, 2=bluray compatibility braindamage mode */
808
809 int b_deblocking_filter;
810 int i_deblocking_filter_alphac0; /* [-6, 6] -6 light filter, 6 strong */
811--
8121.7.1
813
814
815From 495f11a3dd9c4bdd866142b351c170832f65b678 Mon Sep 17 00:00:00 2001
816From: Steven Walters <kemuri9@gmail.com>
817Date: Sat, 26 Jun 2010 16:28:49 -0400
818Subject: [PATCH 4/7] Centralize logging within x264cli
819 x264cli messages will now respect the log level they pertain to.
820 Slightly reduces binary size.
821
822---
823 input/avs.c | 88 +++++-------------
824 input/ffms.c | 58 +++--------
825 input/input.h | 2 +
826 input/lavf.c | 55 +++--------
827 input/thread.c | 9 +-
828 input/timecode.c | 111 ++++++----------------
829 input/y4m.c | 23 +----
830 input/yuv.c | 8 +-
831 muxers.h | 61 ------------
832 output/flv.c | 10 +-
833 output/flv_bytestream.c | 2 +-
834 output/matroska.c | 2 +-
835 output/matroska_ebml.c | 2 +-
836 output/mp4.c | 12 +--
837 output/output.h | 2 +
838 output/raw.c | 2 +-
839 x264.c | 246 +++++++++++++++++++++--------------------------
840 x264cli.h | 67 +++++++++++++
841 18 files changed, 289 insertions(+), 471 deletions(-)
842 delete mode 100644 muxers.h
843 create mode 100644 x264cli.h
844
845diff --git a/input/avs.c b/input/avs.c
846index 07add40..b83f715 100644
847--- a/input/avs.c
848+++ b/input/avs.c
849@@ -20,8 +20,9 @@
850 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
851 *****************************************************************************/
852
853-#include "muxers.h"
854+#include "input.h"
855 #include <windows.h>
856+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "avs", __VA_ARGS__ )
857
858 /* the AVS interface currently uses __declspec to link function declarations to their definitions in the dll.
859 this has a side effect of preventing program execution if the avisynth dll is not found,
860@@ -131,27 +132,15 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
861 FILE *fh = fopen( psz_filename, "r" );
862 if( !fh )
863 return -1;
864- else if( !x264_is_regular_file( fh ) )
865- {
866- fprintf( stderr, "avs [error]: AVS input is incompatible with non-regular file `%s'\n", psz_filename );
867- return -1;
868- }
869+ FAIL_IF_ERROR( !x264_is_regular_file( fh ), "AVS input is incompatible with non-regular file `%s'\n", psz_filename );
870 fclose( fh );
871
872 avs_hnd_t *h = malloc( sizeof(avs_hnd_t) );
873 if( !h )
874 return -1;
875- if( avs_load_library( h ) )
876- {
877- fprintf( stderr, "avs [error]: failed to load avisynth\n" );
878- return -1;
879- }
880+ FAIL_IF_ERROR( avs_load_library( h ), "failed to load avisynth\n" )
881 h->env = h->func.avs_create_script_environment( AVS_INTERFACE_YV12 );
882- if( !h->env )
883- {
884- fprintf( stderr, "avs [error]: failed to initiate avisynth\n" );
885- return -1;
886- }
887+ FAIL_IF_ERROR( !h->env, "failed to initiate avisynth\n" )
888 AVS_Value arg = avs_new_value_string( psz_filename );
889 AVS_Value res;
890 char *filename_ext = get_filename_extension( psz_filename );
891@@ -159,11 +148,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
892 if( !strcasecmp( filename_ext, "avs" ) )
893 {
894 res = h->func.avs_invoke( h->env, "Import", arg, NULL );
895- if( avs_is_error( res ) )
896- {
897- fprintf( stderr, "avs [error]: %s\n", avs_as_string( res ) );
898- return -1;
899- }
900+ FAIL_IF_ERROR( avs_is_error( res ), "%s\n", avs_as_string( res ) )
901 /* check if the user is using a multi-threaded script and apply distributor if necessary.
902 adapted from avisynth's vfw interface */
903 AVS_Value mt_test = h->func.avs_invoke( h->env, "GetMTMode", avs_new_value_bool( 0 ), NULL );
904@@ -184,78 +169,55 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
905 int i;
906 for( i = 0; filter[i]; i++ )
907 {
908- fprintf( stderr, "avs [info]: trying %s... ", filter[i] );
909+ x264_cli_log( "avs", X264_LOG_INFO, "trying %s... ", filter[i] );
910 if( !h->func.avs_function_exists( h->env, filter[i] ) )
911 {
912- fprintf( stderr, "not found\n" );
913+ x264_cli_printf( X264_LOG_INFO, "not found\n" );
914 continue;
915 }
916 if( !strncasecmp( filter[i], "FFmpegSource", 12 ) )
917 {
918- fprintf( stderr, "indexing... " );
919+ x264_cli_printf( X264_LOG_INFO, "indexing... " );
920 fflush( stderr );
921 }
922 res = h->func.avs_invoke( h->env, filter[i], arg, NULL );
923 if( !avs_is_error( res ) )
924 {
925- fprintf( stderr, "succeeded\n" );
926+ x264_cli_printf( X264_LOG_INFO, "succeeded\n" );
927 break;
928 }
929- fprintf( stderr, "failed\n" );
930- }
931- if( !filter[i] )
932- {
933- fprintf( stderr, "avs [error]: unable to find source filter to open `%s'\n", psz_filename );
934- return -1;
935+ x264_cli_printf( X264_LOG_INFO, "failed\n" );
936 }
937+ FAIL_IF_ERROR( !filter[i], "unable to find source filter to open `%s'\n", psz_filename )
938 }
939- if( !avs_is_clip( res ) )
940- {
941- fprintf( stderr, "avs [error]: `%s' didn't return a video clip\n", psz_filename );
942- return -1;
943- }
944+ FAIL_IF_ERROR( !avs_is_clip( res ), "`%s' didn't return a video clip\n", psz_filename )
945 h->clip = h->func.avs_take_clip( res, h->env );
946 int avs_version = h->func.avs_get_version( h->clip );
947 const AVS_VideoInfo *vi = h->func.avs_get_video_info( h->clip );
948- if( !avs_has_video( vi ) )
949- {
950- fprintf( stderr, "avs [error]: `%s' has no video data\n", psz_filename );
951- return -1;
952- }
953+ FAIL_IF_ERROR( !avs_has_video( vi ), "`%s' has no video data\n", psz_filename )
954 /* if the clip is made of fields instead of frames, call weave to make them frames */
955 if( avs_is_field_based( vi ) )
956 {
957- fprintf( stderr, "avs [warning]: detected fieldbased (separated) input, weaving to frames\n" );
958+ x264_cli_log( "avs", X264_LOG_WARNING, "detected fieldbased (separated) input, weaving to frames\n" );
959 AVS_Value tmp = h->func.avs_invoke( h->env, "Weave", res, NULL );
960- if( avs_is_error( tmp ) )
961- {
962- fprintf( stderr, "avs [error]: couldn't weave fields into frames\n" );
963- return -1;
964- }
965+ FAIL_IF_ERROR( avs_is_error( tmp ), "couldn't weave fields into frames\n" )
966 res = update_clip( h, &vi, tmp, res );
967 info->interlaced = 1;
968 info->tff = avs_is_tff( vi );
969 }
970- if( vi->width&1 || vi->height&1 )
971- {
972- fprintf( stderr, "avs [error]: input clip width or height not divisible by 2 (%dx%d)\n",
973- vi->width, vi->height );
974- return -1;
975- }
976+ FAIL_IF_ERROR( vi->width&1 || vi->height&1, "input clip width or height not divisible by 2 (%dx%d)\n", vi->width, vi->height )
977 /* always call ConvertToYV12 to convert non YV12 planar colorspaces to YV12 when user's AVS supports them,
978 as all planar colorspaces are flagged as YV12. If it is already YV12 in this case, the call does nothing */
979 if( !avs_is_yv12( vi ) || avs_version >= AVS_INTERFACE_OTHER_PLANAR )
980 {
981- fprintf( stderr, "avs %s\n", !avs_is_yv12( vi ) ? "[warning]: converting input clip to YV12"
982- : "[info]: avisynth 2.6+ detected, forcing conversion to YV12" );
983+ if( !avs_is_yv12( vi ) )
984+ x264_cli_log( "avs", X264_LOG_WARNING, "converting input clip to YV12" );
985+ else
986+ x264_cli_log( "avs", X264_LOG_INFO, "avisynth 2.6+ detected, forcing conversion to YV12" );
987 const char *arg_name[2] = { NULL, "interlaced" };
988 AVS_Value arg_arr[2] = { res, avs_new_value_bool( info->interlaced ) };
989 AVS_Value res2 = h->func.avs_invoke( h->env, "ConvertToYV12", avs_new_value_array( arg_arr, 2 ), arg_name );
990- if( avs_is_error( res2 ) )
991- {
992- fprintf( stderr, "avs [error]: couldn't convert input clip to YV12\n" );
993- return -1;
994- }
995+ FAIL_IF_ERROR( avs_is_error( res2 ), "couldn't convert input clip to YV12\n" )
996 res = update_clip( h, &vi, res2, res );
997 }
998 h->func.avs_release_value( res );
999@@ -294,11 +256,7 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
1000 return -1;
1001 AVS_VideoFrame *frm = p_pic->opaque = h->func.avs_get_frame( h->clip, i_frame );
1002 const char *err = h->func.avs_clip_get_error( h->clip );
1003- if( err )
1004- {
1005- fprintf( stderr, "avs [error]: %s occurred while reading frame %d\n", err, i_frame );
1006- return -1;
1007- }
1008+ FAIL_IF_ERROR( err, "%s occurred while reading frame %d\n", err, i_frame )
1009 for( int i = 0; i < 3; i++ )
1010 {
1011 /* explicitly cast away the const attribute to avoid a warning */
1012diff --git a/input/ffms.c b/input/ffms.c
1013index b2a253e..fe8bf7e 100644
1014--- a/input/ffms.c
1015+++ b/input/ffms.c
1016@@ -21,8 +21,10 @@
1017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1018 *****************************************************************************/
1019
1020-#include "muxers.h"
1021+#include "input.h"
1022 #include <ffms.h>
1023+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
1024+
1025 #undef DECLARE_ALIGNED
1026 #include <libavcodec/avcodec.h>
1027 #include <libswscale/swscale.h>
1028@@ -86,28 +88,16 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1029 {
1030 idx = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, NULL, &e );
1031 fprintf( stderr, " \r" );
1032- if( !idx )
1033- {
1034- fprintf( stderr, "ffms [error]: could not create index\n" );
1035- return -1;
1036- }
1037+ FAIL_IF_ERROR( !idx, "could not create index\n" )
1038 if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
1039- fprintf( stderr, "ffms [warning]: could not write index file\n" );
1040+ x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
1041 }
1042
1043 int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
1044- if( trackno < 0 )
1045- {
1046- fprintf( stderr, "ffms [error]: could not find video track\n" );
1047- return -1;
1048- }
1049+ FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
1050
1051 h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
1052- if( !h->video_source )
1053- {
1054- fprintf( stderr, "ffms [error]: could not create video source\n" );
1055- return -1;
1056- }
1057+ FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
1058
1059 h->track = FFMS_GetTrackFromVideo( h->video_source );
1060
1061@@ -121,11 +111,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1062 h->vfr_input = info->vfr;
1063
1064 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
1065- if( !frame )
1066- {
1067- fprintf( stderr, "ffms [error]: could not read frame 0\n" );
1068- return -1;
1069- }
1070+ FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
1071
1072 h->init_width = h->cur_width = info->width = frame->EncodedWidth;
1073 h->init_height = h->cur_height = info->height = frame->EncodedHeight;
1074@@ -134,8 +120,8 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1075 info->tff = frame->TopFieldFirst;
1076
1077 if( h->cur_pix_fmt != PIX_FMT_YUV420P )
1078- fprintf( stderr, "ffms [warning]: converting from %s to YV12\n",
1079- avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
1080+ x264_cli_log( "ffms", X264_LOG_WARNING, "converting from %s to YV12\n",
1081+ avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
1082
1083 /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
1084 * so we need to reduce large timebases to prevent overflow */
1085@@ -173,19 +159,15 @@ static int check_swscale( ffms_hnd_t *h, const FFMS_Frame *frame, int i_frame )
1086 if( h->scaler )
1087 {
1088 sws_freeContext( h->scaler );
1089- fprintf( stderr, "ffms [warning]: stream properties changed to %dx%d, %s at frame %d \n", frame->EncodedWidth,
1090- frame->EncodedHeight, avcodec_get_pix_fmt_name( frame->EncodedPixelFormat ), i_frame );
1091+ x264_cli_log( "ffms", X264_LOG_WARNING, "stream properties changed to %dx%d, %s at frame %d \n", frame->EncodedWidth,
1092+ frame->EncodedHeight, avcodec_get_pix_fmt_name( frame->EncodedPixelFormat ), i_frame );
1093 h->cur_width = frame->EncodedWidth;
1094 h->cur_height = frame->EncodedHeight;
1095 h->cur_pix_fmt = frame->EncodedPixelFormat;
1096 }
1097 h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
1098 PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
1099- if( !h->scaler )
1100- {
1101- fprintf( stderr, "ffms [error]: could not open swscale context\n" );
1102- return -1;
1103- }
1104+ FAIL_IF_ERROR( !h->scaler, "could not open swscale context\n" )
1105 return 0;
1106 }
1107
1108@@ -195,11 +177,7 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
1109 FFMS_ErrorInfo e;
1110 e.BufferSize = 0;
1111 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
1112- if( !frame )
1113- {
1114- fprintf( stderr, "ffms [error]: could not read frame %d\n", i_frame );
1115- return -1;
1116- }
1117+ FAIL_IF_ERROR( !frame, "could not read frame %d\n", i_frame )
1118
1119 if( check_swscale( h, frame, i_frame ) )
1120 return -1;
1121@@ -214,12 +192,8 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
1122
1123 if( h->vfr_input )
1124 {
1125- if( info->PTS == AV_NOPTS_VALUE )
1126- {
1127- fprintf( stderr, "ffms [error]: invalid timestamp. "
1128- "Use --force-cfr and specify a framerate with --fps\n" );
1129- return -1;
1130- }
1131+ FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
1132+ "Use --force-cfr and specify a framerate with --fps\n" )
1133
1134 if( !h->pts_offset_flag )
1135 {
1136diff --git a/input/input.h b/input/input.h
1137index f89b13b..f588f3c 100644
1138--- a/input/input.h
1139+++ b/input/input.h
1140@@ -25,6 +25,8 @@
1141 #ifndef X264_INPUT_H
1142 #define X264_INPUT_H
1143
1144+#include "x264cli.h"
1145+
1146 /* options that are used by only some demuxers */
1147 typedef struct
1148 {
1149diff --git a/input/lavf.c b/input/lavf.c
1150index 4b0375f..54a275f 100644
1151--- a/input/lavf.c
1152+++ b/input/lavf.c
1153@@ -21,7 +21,8 @@
1154 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1155 *****************************************************************************/
1156
1157-#include "muxers.h"
1158+#include "input.h"
1159+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ )
1160 #undef DECLARE_ALIGNED
1161 #include <libavformat/avformat.h>
1162 #include <libswscale/swscale.h>
1163@@ -59,19 +60,15 @@ static int check_swscale( lavf_hnd_t *h, AVCodecContext *c, int i_frame )
1164 if( h->scaler )
1165 {
1166 sws_freeContext( h->scaler );
1167- fprintf( stderr, "lavf [warning]: stream properties changed to %dx%d, %s at frame %d \n",
1168- c->width, c->height, avcodec_get_pix_fmt_name( c->pix_fmt ), i_frame );
1169+ x264_cli_log( "lavf", X264_LOG_WARNING, "stream properties changed to %dx%d, %s at frame %d \n",
1170+ c->width, c->height, avcodec_get_pix_fmt_name( c->pix_fmt ), i_frame );
1171 h->cur_width = c->width;
1172 h->cur_height = c->height;
1173 h->cur_pix_fmt = c->pix_fmt;
1174 }
1175 h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
1176 PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
1177- if( !h->scaler )
1178- {
1179- fprintf( stderr, "lavf [error]: could not open swscale context\n" );
1180- return -1;
1181- }
1182+ FAIL_IF_ERROR( !h->scaler, "could not open swscale context\n" )
1183 return 0;
1184 }
1185
1186@@ -106,12 +103,12 @@ static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_fram
1187 {
1188 c->reordered_opaque = pkt->pts;
1189 if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
1190- fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
1191+ x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
1192 }
1193 if( !finished )
1194 {
1195 if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
1196- fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
1197+ x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
1198 if( !finished )
1199 return -1;
1200 }
1201@@ -166,26 +163,13 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1202 if( !strcmp( psz_filename, "-" ) )
1203 psz_filename = "pipe:";
1204
1205- if( av_open_input_file( &h->lavf, psz_filename, NULL, 0, NULL ) )
1206- {
1207- fprintf( stderr, "lavf [error]: could not open input file\n" );
1208- return -1;
1209- }
1210-
1211- if( av_find_stream_info( h->lavf ) < 0 )
1212- {
1213- fprintf( stderr, "lavf [error]: could not find input stream info\n" );
1214- return -1;
1215- }
1216+ FAIL_IF_ERROR( av_open_input_file( &h->lavf, psz_filename, NULL, 0, NULL ), "could not open input file\n" )
1217+ FAIL_IF_ERROR( av_find_stream_info( h->lavf ) < 0, "could not find input stream info\n" )
1218
1219 int i = 0;
1220 while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != CODEC_TYPE_VIDEO )
1221 i++;
1222- if( i == h->lavf->nb_streams )
1223- {
1224- fprintf( stderr, "lavf [error]: could not find video stream\n" );
1225- return -1;
1226- }
1227+ FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
1228 h->stream_id = i;
1229 h->next_frame = 0;
1230 h->pts_offset_flag = 0;
1231@@ -207,22 +191,15 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1232 info->csp |= X264_CSP_VFLIP;
1233
1234 if( h->cur_pix_fmt != PIX_FMT_YUV420P )
1235- fprintf( stderr, "lavf [warning]: converting from %s to YV12\n",
1236- avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
1237-
1238- if( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ) )
1239- {
1240- fprintf( stderr, "lavf [error]: could not find decoder for video stream\n" );
1241- return -1;
1242- }
1243+ x264_cli_log( "lavf", X264_LOG_WARNING, "converting from %s to YV12\n",
1244+ avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
1245+ FAIL_IF_ERROR( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ),
1246+ "could not find decoder for video stream\n" )
1247
1248 /* prefetch the first frame and set/confirm flags */
1249 h->first_pic = malloc( sizeof(x264_picture_t) );
1250- if( !h->first_pic || lavf_input.picture_alloc( h->first_pic, info->csp, info->width, info->height ) )
1251- {
1252- fprintf( stderr, "lavf [error]: malloc failed\n" );
1253- return -1;
1254- }
1255+ FAIL_IF_ERROR( !h->first_pic || lavf_input.picture_alloc( h->first_pic, info->csp, info->width, info->height ),
1256+ "malloc failed\n" )
1257 else if( read_frame_internal( h->first_pic, h, 0, info ) )
1258 return -1;
1259
1260diff --git a/input/thread.c b/input/thread.c
1261index c4b07fa..98af22b 100644
1262--- a/input/thread.c
1263+++ b/input/thread.c
1264@@ -21,7 +21,7 @@
1265 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1266 *****************************************************************************/
1267
1268-#include "muxers.h"
1269+#include "input.h"
1270
1271 extern cli_input_t input;
1272
1273@@ -47,11 +47,8 @@ typedef struct thread_input_arg_t
1274 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
1275 {
1276 thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
1277- if( !h || input.picture_alloc( &h->pic, info->csp, info->width, info->height ) )
1278- {
1279- fprintf( stderr, "x264 [error]: malloc failed\n" );
1280- return -1;
1281- }
1282+ FAIL_IF_ERR( !h || input.picture_alloc( &h->pic, info->csp, info->width, info->height ),
1283+ "x264", "malloc failed\n" )
1284 h->input = input;
1285 h->p_handle = *p_handle;
1286 h->next_frame = -1;
1287diff --git a/input/timecode.c b/input/timecode.c
1288index a307327..7821e76 100644
1289--- a/input/timecode.c
1290+++ b/input/timecode.c
1291@@ -20,7 +20,8 @@
1292 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1293 *****************************************************************************/
1294
1295-#include "muxers.h"
1296+#include "input.h"
1297+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "timecode", __VA_ARGS__ )
1298 #include <math.h>
1299
1300 extern cli_input_t input;
1301@@ -61,12 +62,8 @@ static double correct_fps( double fps, timecode_hnd_t *h )
1302 {
1303 fps_den = i * h->timebase_num;
1304 fps_num = round( fps_den * fps_sig ) * exponent;
1305- if( fps_num > UINT32_MAX )
1306- {
1307- fprintf( stderr, "timecode [error]: tcfile fps correction failed.\n"
1308- " Specify an appropriate timebase manually or remake tcfile.\n" );
1309- return -1;
1310- }
1311+ FAIL_IF_ERROR( fps_num > UINT32_MAX, "tcfile fps correction failed.\n"
1312+ " Specify an appropriate timebase manually or remake tcfile.\n" )
1313 if( fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
1314 break;
1315 ++i;
1316@@ -91,12 +88,8 @@ static int try_mkv_timebase_den( double *fpss, timecode_hnd_t *h, int loop_num )
1317 double fps_sig = sigexp10( fpss[num], &exponent );
1318 fps_den = round( MKV_TIMEBASE_DEN / fps_sig ) / exponent;
1319 h->timebase_num = fps_den && h->timebase_num ? gcd( h->timebase_num, fps_den ) : fps_den;
1320- if( h->timebase_num > UINT32_MAX || !h->timebase_num )
1321- {
1322- fprintf( stderr, "timecode [error]: automatic timebase generation failed.\n"
1323- " Specify timebase manually.\n" );
1324- return -1;
1325- }
1326+ FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || !h->timebase_num, "automatic timebase generation failed.\n"
1327+ " Specify timebase manually.\n" )
1328 }
1329 return 0;
1330 }
1331@@ -110,11 +103,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1332 double *fpss = NULL;
1333
1334 ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
1335- if( ret != 1 || (tcfv != 1 && tcfv != 2) )
1336- {
1337- fprintf( stderr, "timecode [error]: unsupported timecode format\n" );
1338- return -1;
1339- }
1340+ FAIL_IF_ERROR( ret != 1 || (tcfv != 1 && tcfv != 2), "unsupported timecode format\n" )
1341
1342 if( tcfv == 1 )
1343 {
1344@@ -128,18 +117,11 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1345 {
1346 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
1347 continue;
1348- if( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1 )
1349- {
1350- fprintf( stderr, "timecode [error]: tcfile parsing error: assumed fps not found\n" );
1351- return -1;
1352- }
1353+ FAIL_IF_ERROR( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1,
1354+ "tcfile parsing error: assumed fps not found\n" )
1355 break;
1356 }
1357- if( h->assume_fps <= 0 )
1358- {
1359- fprintf( stderr, "timecode [error]: invalid assumed fps %.6f\n", h->assume_fps );
1360- return -1;
1361- }
1362+ FAIL_IF_ERROR( h->assume_fps <= 0, "invalid assumed fps %.6f\n", h->assume_fps )
1363
1364 file_pos = ftell( tcfile_in );
1365 h->stored_pts_num = 0;
1366@@ -152,16 +134,9 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1367 continue;
1368 }
1369 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
1370- if( ret != 3 && ret != EOF )
1371- {
1372- fprintf( stderr, "timecode [error]: invalid input tcfile\n" );
1373- return -1;
1374- }
1375- if( start > end || start <= prev_start || end <= prev_end || seq_fps <= 0 )
1376- {
1377- fprintf( stderr, "timecode [error]: invalid input tcfile at line %d: %s\n", num, buff );
1378- return -1;
1379- }
1380+ FAIL_IF_ERROR( ret != 3 && ret != EOF, "invalid input tcfile\n" )
1381+ FAIL_IF_ERROR( start > end || start <= prev_start || end <= prev_end || seq_fps <= 0,
1382+ "invalid input tcfile at line %d: %s\n", num, buff )
1383 prev_start = start;
1384 prev_end = end;
1385 if( h->auto_timebase_den || h->auto_timebase_num )
1386@@ -259,11 +234,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1387 ++num;
1388 }
1389 timecodes_num = h->stored_pts_num + h->seek;
1390- if( !timecodes_num )
1391- {
1392- fprintf( stderr, "timecode [error]: input tcfile doesn't have any timecodes!\n" );
1393- return -1;
1394- }
1395+ FAIL_IF_ERROR( !timecodes_num, "input tcfile doesn't have any timecodes!\n" )
1396 fseek( tcfile_in, file_pos, SEEK_SET );
1397
1398 timecodes = malloc( timecodes_num * sizeof(double) );
1399@@ -272,11 +243,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1400
1401 fgets( buff, sizeof(buff), tcfile_in );
1402 ret = sscanf( buff, "%lf", &timecodes[0] );
1403- if( ret != 1 )
1404- {
1405- fprintf( stderr, "timecode [error]: invalid input tcfile for frame 0\n" );
1406- goto fail;
1407- }
1408+ FAIL_IF_ERROR( ret != 1, "invalid input tcfile for frame 0\n" )
1409 for( num = 1; num < timecodes_num; )
1410 {
1411 fgets( buff, sizeof(buff), tcfile_in );
1412@@ -284,11 +251,8 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1413 continue;
1414 ret = sscanf( buff, "%lf", &timecodes[num] );
1415 timecodes[num] *= 1e-3; /* Timecode format v2 is expressed in milliseconds. */
1416- if( ret != 1 || timecodes[num] <= timecodes[num - 1] )
1417- {
1418- fprintf( stderr, "timecode [error]: invalid input tcfile for frame %d\n", num );
1419- goto fail;
1420- }
1421+ FAIL_IF_ERROR( ret != 1 || timecodes[num] <= timecodes[num - 1],
1422+ "invalid input tcfile for frame %d\n", num )
1423 ++num;
1424 }
1425
1426@@ -342,14 +306,10 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1427 uint64_t i = gcd( h->timebase_num, h->timebase_den );
1428 h->timebase_num /= i;
1429 h->timebase_den /= i;
1430- fprintf( stderr, "timecode [info]: automatic timebase generation %"PRIu64"/%"PRIu64"\n", h->timebase_num, h->timebase_den );
1431- }
1432- else if( h->timebase_den > UINT32_MAX || !h->timebase_den )
1433- {
1434- fprintf( stderr, "timecode [error]: automatic timebase generation failed.\n"
1435- " Specify an appropriate timebase manually.\n" );
1436- goto fail;
1437+ x264_cli_log( "timecode", X264_LOG_INFO, "automatic timebase generation %"PRIu64"/%"PRIu64"\n", h->timebase_num, h->timebase_den );
1438 }
1439+ else FAIL_IF_ERROR( h->timebase_den > UINT32_MAX || !h->timebase_den, "automatic timebase generation failed.\n"
1440+ " Specify an appropriate timebase manually.\n" )
1441
1442 h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
1443 if( !h->pts )
1444@@ -360,11 +320,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
1445 {
1446 h->pts[num] = (int64_t)( timecodes[h->seek + num] * ((double)h->timebase_den / h->timebase_num) + 0.5 );
1447 h->pts[num] -= pts_seek_offset;
1448- if( h->pts[num] <= h->pts[num - 1] )
1449- {
1450- fprintf( stderr, "timecode [error]: invalid timebase or timecode for frame %d\n", num );
1451- goto fail;
1452- }
1453+ FAIL_IF_ERROR( h->pts[num] <= h->pts[num - 1], "invalid timebase or timecode for frame %d\n", num )
1454 }
1455
1456 free( timecodes );
1457@@ -386,11 +342,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1458 int ret = 0;
1459 FILE *tcfile_in;
1460 timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
1461- if( !h )
1462- {
1463- fprintf( stderr, "timecode [error]: malloc failed\n" );
1464- return -1;
1465- }
1466+ FAIL_IF_ERROR( !h, "malloc failed\n" )
1467 h->input = input;
1468 h->p_handle = *p_handle;
1469 h->frame_total = input.get_frame_total( h->p_handle );
1470@@ -400,11 +352,8 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1471 ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
1472 if( ret == 1 )
1473 h->timebase_num = strtoul( opt->timebase, NULL, 10 );
1474- if( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX )
1475- {
1476- fprintf( stderr, "timecode [error]: timebase you specified exceeds H.264 maximum\n" );
1477- return -1;
1478- }
1479+ FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX,
1480+ "timebase you specified exceeds H.264 maximum\n" )
1481 }
1482 h->auto_timebase_num = !ret;
1483 h->auto_timebase_den = ret < 2;
1484@@ -418,14 +367,10 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1485 *p_handle = h;
1486
1487 tcfile_in = fopen( psz_filename, "rb" );
1488- if( !tcfile_in )
1489- {
1490- fprintf( stderr, "timecode [error]: can't open `%s'\n", psz_filename );
1491- return -1;
1492- }
1493+ FAIL_IF_ERROR( !tcfile_in, "can't open `%s'\n", psz_filename )
1494 else if( !x264_is_regular_file( tcfile_in ) )
1495 {
1496- fprintf( stderr, "timecode [error]: tcfile input incompatible with non-regular file `%s'\n", psz_filename );
1497+ x264_cli_log( "timecode", X264_LOG_ERROR, "tcfile input incompatible with non-regular file `%s'\n", psz_filename );
1498 fclose( tcfile_in );
1499 return -1;
1500 }
1501@@ -466,8 +411,8 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
1502 {
1503 if( h->pts )
1504 {
1505- fprintf( stderr, "timecode [info]: input timecode file missing data for frame %d and later\n"
1506- " assuming constant fps %.6f\n", i_frame, h->assume_fps );
1507+ x264_cli_log( "timecode", X264_LOG_INFO, "input timecode file missing data for frame %d and later\n"
1508+ " assuming constant fps %.6f\n", i_frame, h->assume_fps );
1509 free( h->pts );
1510 h->pts = NULL;
1511 }
1512diff --git a/input/y4m.c b/input/y4m.c
1513index fd42140..9b39d2f 100644
1514--- a/input/y4m.c
1515+++ b/input/y4m.c
1516@@ -21,7 +21,8 @@
1517 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1518 *****************************************************************************/
1519
1520-#include "muxers.h"
1521+#include "input.h"
1522+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
1523
1524 typedef struct
1525 {
1526@@ -162,11 +163,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1527 if( colorspace == X264_CSP_NONE )
1528 colorspace = X264_CSP_I420;
1529
1530- if( colorspace != X264_CSP_I420 )
1531- {
1532- fprintf( stderr, "y4m [error]: colorspace unhandled\n" );
1533- return -1;
1534- }
1535+ FAIL_IF_ERROR( colorspace != X264_CSP_I420, "colorspace unhandled\n" )
1536
1537 *p_handle = h;
1538 return 0;
1539@@ -202,21 +199,13 @@ static int read_frame_internal( x264_picture_t *p_pic, y4m_hnd_t *h )
1540 return -1;
1541
1542 header[slen] = 0;
1543- if( strncmp( header, Y4M_FRAME_MAGIC, slen ) )
1544- {
1545- fprintf( stderr, "y4m [error]: bad header magic (%"PRIx32" <=> %s)\n",
1546- M32(header), header );
1547- return -1;
1548- }
1549+ FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
1550+ M32(header), header )
1551
1552 /* Skip most of it */
1553 while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
1554 i++;
1555- if( i == MAX_FRAME_HEADER )
1556- {
1557- fprintf( stderr, "y4m [error]: bad frame header!\n" );
1558- return -1;
1559- }
1560+ FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
1561 h->frame_header_len = i+slen+1;
1562
1563 if( fread( p_pic->img.plane[0], h->width * h->height, 1, h->fh ) <= 0
1564diff --git a/input/yuv.c b/input/yuv.c
1565index cbed7fc..613662c 100644
1566--- a/input/yuv.c
1567+++ b/input/yuv.c
1568@@ -21,7 +21,7 @@
1569 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1570 *****************************************************************************/
1571
1572-#include "muxers.h"
1573+#include "input.h"
1574
1575 typedef struct
1576 {
1577@@ -45,11 +45,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
1578 }
1579 else
1580 sscanf( opt->resolution, "%ux%u", &info->width, &info->height );
1581- if( !info->width || !info->height )
1582- {
1583- fprintf( stderr, "yuv [error]: rawyuv input requires a resolution.\n" );
1584- return -1;
1585- }
1586+ FAIL_IF_ERR( !info->width || !info->height, "yuv", "rawyuv input requires a resolution.\n" )
1587
1588 h->next_frame = 0;
1589 info->vfr = 0;
1590diff --git a/muxers.h b/muxers.h
1591deleted file mode 100644
1592index b309320..0000000
1593--- a/muxers.h
1594+++ /dev/null
1595@@ -1,61 +0,0 @@
1596-/*****************************************************************************
1597- * muxers.h: h264 file i/o modules
1598- *****************************************************************************
1599- * Copyright (C) 2003-2009 x264 project
1600- *
1601- * Authors: Laurent Aimar <fenrir@via.ecp.fr>
1602- * Loren Merritt <lorenm@u.washington.edu>
1603- *
1604- * This program is free software; you can redistribute it and/or modify
1605- * it under the terms of the GNU General Public License as published by
1606- * the Free Software Foundation; either version 2 of the License, or
1607- * (at your option) any later version.
1608- *
1609- * This program is distributed in the hope that it will be useful,
1610- * but WITHOUT ANY WARRANTY; without even the implied warranty of
1611- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1612- * GNU General Public License for more details.
1613- *
1614- * You should have received a copy of the GNU General Public License
1615- * along with this program; if not, write to the Free Software
1616- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1617- *****************************************************************************/
1618-
1619-#ifndef X264_MUXERS_H
1620-#define X264_MUXERS_H
1621-
1622-#include "common/common.h"
1623-#include "x264.h"
1624-
1625-typedef void *hnd_t;
1626-
1627-static inline int64_t gcd( int64_t a, int64_t b )
1628-{
1629- while( 1 )
1630- {
1631- int64_t c = a % b;
1632- if( !c )
1633- return b;
1634- a = b;
1635- b = c;
1636- }
1637-}
1638-
1639-static inline int64_t lcm( int64_t a, int64_t b )
1640-{
1641- return ( a / gcd( a, b ) ) * b;
1642-}
1643-
1644-static inline char *get_filename_extension( char *filename )
1645-{
1646- char *ext = filename + strlen( filename );
1647- while( *ext != '.' && ext > filename )
1648- ext--;
1649- ext += *ext == '.';
1650- return ext;
1651-}
1652-
1653-#include "input/input.h"
1654-#include "output/output.h"
1655-
1656-#endif
1657diff --git a/output/flv.c b/output/flv.c
1658index e441b6d..9831a5b 100644
1659--- a/output/flv.c
1660+++ b/output/flv.c
1661@@ -18,7 +18,7 @@
1662 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1663 *****************************************************************************/
1664
1665-#include "muxers.h"
1666+#include "output.h"
1667 #include "flv_bytestream.h"
1668
1669 #define CHECK(x)\
1670@@ -223,14 +223,14 @@ static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_
1671 if( prev_dts == dts )
1672 {
1673 double fps = ((double)p_flv->i_timebase_den / p_flv->i_timebase_num) / (p_picture->i_dts - p_flv->i_prev_dts);
1674- fprintf( stderr, "flv [warning]: duplicate DTS %"PRId64" generated by rounding\n"
1675- " current internal decoding framerate: %.6f fps\n", dts, fps );
1676+ x264_cli_log( "flv", X264_LOG_WARNING, "duplicate DTS %"PRId64" generated by rounding\n"
1677+ " current internal decoding framerate: %.6f fps\n", dts, fps );
1678 }
1679 if( prev_cts == cts )
1680 {
1681 double fps = ((double)p_flv->i_timebase_den / p_flv->i_timebase_num) / (p_picture->i_pts - p_flv->i_prev_pts);
1682- fprintf( stderr, "flv [warning]: duplicate CTS %"PRId64" generated by rounding\n"
1683- " current internal composition framerate: %.6f fps\n", cts, fps );
1684+ x264_cli_log( "flv", X264_LOG_WARNING, "duplicate CTS %"PRId64" generated by rounding\n"
1685+ " current internal composition framerate: %.6f fps\n", cts, fps );
1686 }
1687 }
1688 p_flv->i_prev_dts = p_picture->i_dts;
1689diff --git a/output/flv_bytestream.c b/output/flv_bytestream.c
1690index 316114c..e02476c 100644
1691--- a/output/flv_bytestream.c
1692+++ b/output/flv_bytestream.c
1693@@ -18,7 +18,7 @@
1694 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1695 *****************************************************************************/
1696
1697-#include "muxers.h"
1698+#include "output.h"
1699 #include "flv_bytestream.h"
1700
1701 uint64_t dbl2int( double value )
1702diff --git a/output/matroska.c b/output/matroska.c
1703index 0304c84..a1219d0 100644
1704--- a/output/matroska.c
1705+++ b/output/matroska.c
1706@@ -18,7 +18,7 @@
1707 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1708 *****************************************************************************/
1709
1710-#include "muxers.h"
1711+#include "output.h"
1712 #include "matroska_ebml.h"
1713
1714 typedef struct
1715diff --git a/output/matroska_ebml.c b/output/matroska_ebml.c
1716index 31b62f8..adfcaa8 100644
1717--- a/output/matroska_ebml.c
1718+++ b/output/matroska_ebml.c
1719@@ -18,7 +18,7 @@
1720 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1721 *****************************************************************************/
1722
1723-#include "muxers.h"
1724+#include "output.h"
1725 #include "matroska_ebml.h"
1726
1727 #define CLSIZE 1048576
1728diff --git a/output/mp4.c b/output/mp4.c
1729index 0e3c2fc..f2fc5f5 100644
1730--- a/output/mp4.c
1731+++ b/output/mp4.c
1732@@ -21,7 +21,7 @@
1733 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1734 *****************************************************************************/
1735
1736-#include "muxers.h"
1737+#include "output.h"
1738 #include <gpac/isomedia.h>
1739
1740 #if HAVE_GF_MALLOC
1741@@ -61,12 +61,12 @@ static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
1742
1743 timescale = gf_isom_get_media_timescale( p_file, i_track );
1744 count = gf_isom_get_sample_count( p_file, i_track );
1745- for( int i = 0; i < count; i++ )
1746+ for( u32 i = 0; i < count; i++ )
1747 {
1748 GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
1749 if( !samp )
1750 {
1751- fprintf( stderr, "mp4 [error]: failure reading back frame %u\n", i );
1752+ x264_cli_log( "mp4", X264_LOG_ERROR, "failure reading back frame %u\n", i );
1753 break;
1754 }
1755
1756@@ -163,11 +163,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle )
1757 FILE *fh = fopen( psz_filename, "w" );
1758 if( !fh )
1759 return -1;
1760- else if( !x264_is_regular_file( fh ) )
1761- {
1762- fprintf( stderr, "mp4 [error]: MP4 output is incompatible with non-regular file `%s'\n", psz_filename );
1763- return -1;
1764- }
1765+ FAIL_IF_ERR( !x264_is_regular_file( fh ), "MP4 output is incompatible with non-regular file `%s'\n", psz_filename )
1766 fclose( fh );
1767
1768 if( !(p_mp4 = malloc( sizeof(mp4_hnd_t) )) )
1769diff --git a/output/output.h b/output/output.h
1770index c79b48e..094fefc 100644
1771--- a/output/output.h
1772+++ b/output/output.h
1773@@ -24,6 +24,8 @@
1774 #ifndef X264_OUTPUT_H
1775 #define X264_OUTPUT_H
1776
1777+#include "x264cli.h"
1778+
1779 typedef struct
1780 {
1781 int (*open_file)( char *psz_filename, hnd_t *p_handle );
1782diff --git a/output/raw.c b/output/raw.c
1783index 02e4c56..fc418fb 100644
1784--- a/output/raw.c
1785+++ b/output/raw.c
1786@@ -21,7 +21,7 @@
1787 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
1788 *****************************************************************************/
1789
1790-#include "muxers.h"
1791+#include "output.h"
1792
1793 static int open_file( char *psz_filename, hnd_t *p_handle )
1794 {
1795diff --git a/x264.c b/x264.c
1796index f08ab41..741570c 100644
1797--- a/x264.c
1798+++ b/x264.c
1799@@ -31,9 +31,11 @@
1800 #include <getopt.h>
1801
1802 #include "common/common.h"
1803-#include "common/cpu.h"
1804-#include "x264.h"
1805-#include "muxers.h"
1806+#include "x264cli.h"
1807+#include "input/input.h"
1808+#include "output/output.h"
1809+
1810+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "x264", __VA_ARGS__ )
1811
1812 #ifdef _WIN32
1813 #include <windows.h>
1814@@ -96,6 +98,7 @@ static const char * const muxer_names[] =
1815 };
1816
1817 static const char * const pulldown_names[] = { "none", "22", "32", "64", "double", "triple", "euro", 0 };
1818+static const char * const log_level_names[] = { "none", "error", "warning", "info", "debug", 0 };
1819
1820 typedef struct{
1821 int mod;
1822@@ -141,6 +144,48 @@ static void Help( x264_param_t *defaults, int longhelp );
1823 static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
1824 static int Encode( x264_param_t *param, cli_opt_t *opt );
1825
1826+/* logging and printing for within the cli system */
1827+static int cli_log_level;
1828+void x264_cli_log( const char *name, int i_level, const char *fmt, ... )
1829+{
1830+ if( i_level > cli_log_level )
1831+ return;
1832+ char *s_level;
1833+ switch( i_level )
1834+ {
1835+ case X264_LOG_ERROR:
1836+ s_level = "error";
1837+ break;
1838+ case X264_LOG_WARNING:
1839+ s_level = "warning";
1840+ break;
1841+ case X264_LOG_INFO:
1842+ s_level = "info";
1843+ break;
1844+ case X264_LOG_DEBUG:
1845+ s_level = "debug";
1846+ break;
1847+ default:
1848+ s_level = "unknown";
1849+ break;
1850+ }
1851+ fprintf( stderr, "%s [%s]: ", name, s_level );
1852+ va_list arg;
1853+ va_start( arg, fmt );
1854+ vfprintf( stderr, fmt, arg );
1855+ va_end( arg );
1856+}
1857+
1858+void x264_cli_printf( int i_level, const char *fmt, ... )
1859+{
1860+ if( i_level > cli_log_level )
1861+ return;
1862+ va_list arg;
1863+ va_start( arg, fmt );
1864+ vfprintf( stderr, fmt, arg );
1865+ va_end( arg );
1866+}
1867+
1868 /****************************************************************************
1869 * main:
1870 ****************************************************************************/
1871@@ -571,6 +616,9 @@ static void Help( x264_param_t *defaults, int longhelp )
1872 H1( " -v, --verbose Print stats for each frame\n" );
1873 H1( " --no-progress Don't show the progress indicator while encoding\n" );
1874 H0( " --quiet Quiet Mode\n" );
1875+ H1( " --log-level <string> Specify the maximum level of logging [\"%s\"]\n"
1876+ " - %s\n", strtable_lookup( log_level_names, cli_log_level - X264_LOG_NONE ),
1877+ stringify_names( buf, log_level_names ) );
1878 H1( " --psnr Enable PSNR computation\n" );
1879 H1( " --ssim Enable SSIM computation\n" );
1880 H1( " --threads <integer> Force a specific number of threads\n" );
1881@@ -616,6 +664,7 @@ enum {
1882 OPT_TCFILE_OUT,
1883 OPT_TIMEBASE,
1884 OPT_PULLDOWN,
1885+ OPT_LOG_LEVEL
1886 } OptionsOPT;
1887
1888 static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
1889@@ -729,6 +778,7 @@ static struct option long_options[] =
1890 { "ssim", no_argument, NULL, 0 },
1891 { "quiet", no_argument, NULL, OPT_QUIET },
1892 { "verbose", no_argument, NULL, 'v' },
1893+ { "log-level", required_argument, NULL, OPT_LOG_LEVEL },
1894 { "no-progress", no_argument, NULL, OPT_NOPROGRESS },
1895 { "visualize", no_argument, NULL, OPT_VISUALIZE },
1896 { "dump-yuv", required_argument, NULL, 0 },
1897@@ -780,11 +830,11 @@ static int select_output( const char *muxer, char *filename, x264_param_t *param
1898 param->b_repeat_headers = 0;
1899 if( param->i_nal_hrd == X264_NAL_HRD_CBR )
1900 {
1901- fprintf( stderr, "x264 [warning]: cbr nal-hrd is not compatible with mp4\n" );
1902+ x264_cli_log( "x264", X264_LOG_WARNING, "cbr nal-hrd is not compatible with mp4\n" );
1903 param->i_nal_hrd = X264_NAL_HRD_VBR;
1904 }
1905 #else
1906- fprintf( stderr, "x264 [error]: not compiled with MP4 output support\n" );
1907+ x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with MP4 output support\n" );
1908 return -1;
1909 #endif
1910 }
1911@@ -833,7 +883,7 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
1912 input = avs_input;
1913 module = "avs";
1914 #else
1915- fprintf( stderr, "x264 [error]: not compiled with AVS input support\n" );
1916+ x264_cli_log( "x264", X264_LOG_ERROR, "not compiled with AVS input support\n" );
1917 return -1;
1918 #endif
1919 }
1920@@ -877,11 +927,7 @@ static int select_input( const char *demuxer, char *used_demuxer, char *filename
1921 input = yuv_input;
1922 }
1923
1924- if( !(*p_handle) )
1925- {
1926- fprintf( stderr, "x264 [error]: could not open input file `%s' via any method!\n", filename );
1927- return -1;
1928- }
1929+ FAIL_IF_ERROR( !(*p_handle), "could not open input file `%s' via any method!\n", filename )
1930 }
1931 strcpy( used_demuxer, module );
1932
1933@@ -932,6 +978,7 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
1934 char *tune = NULL;
1935
1936 x264_param_default( &defaults );
1937+ cli_log_level = defaults.i_log_level;
1938
1939 memset( opt, 0, sizeof(cli_opt_t) );
1940 memset( &input_opt, 0, sizeof(cli_input_opt_t) );
1941@@ -1004,32 +1051,20 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
1942 output_filename = optarg;
1943 break;
1944 case OPT_MUXER:
1945- if( parse_enum_name( optarg, muxer_names, &muxer ) < 0 )
1946- {
1947- fprintf( stderr, "x264 [error]: Unknown muxer `%s'\n", optarg );
1948- return -1;
1949- }
1950+ FAIL_IF_ERROR( parse_enum_name( optarg, muxer_names, &muxer ), "Unknown muxer `%s'\n", optarg )
1951 break;
1952 case OPT_DEMUXER:
1953- if( parse_enum_name( optarg, demuxer_names, &demuxer ) < 0 )
1954- {
1955- fprintf( stderr, "x264 [error]: Unknown demuxer `%s'\n", optarg );
1956- return -1;
1957- }
1958+ FAIL_IF_ERROR( parse_enum_name( optarg, demuxer_names, &demuxer ), "Unknown demuxer `%s'\n", optarg )
1959 break;
1960 case OPT_INDEX:
1961 input_opt.index_file = optarg;
1962 break;
1963 case OPT_QPFILE:
1964 opt->qpfile = fopen( optarg, "rb" );
1965- if( !opt->qpfile )
1966- {
1967- fprintf( stderr, "x264 [error]: can't open qpfile `%s'\n", optarg );
1968- return -1;
1969- }
1970- else if( !x264_is_regular_file( opt->qpfile ) )
1971+ FAIL_IF_ERROR( !opt->qpfile, "can't open qpfile `%s'\n", optarg )
1972+ if( !x264_is_regular_file( opt->qpfile ) )
1973 {
1974- fprintf( stderr, "x264 [error]: qpfile incompatible with non-regular file `%s'\n", optarg );
1975+ x264_cli_log( "x264", X264_LOG_ERROR, "qpfile incompatible with non-regular file `%s'\n", optarg );
1976 fclose( opt->qpfile );
1977 return -1;
1978 }
1979@@ -1038,11 +1073,17 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
1980 b_thread_input = 1;
1981 break;
1982 case OPT_QUIET:
1983- param->i_log_level = X264_LOG_NONE;
1984+ cli_log_level = param->i_log_level = X264_LOG_NONE;
1985 break;
1986 case 'v':
1987- param->i_log_level = X264_LOG_DEBUG;
1988+ cli_log_level = param->i_log_level = X264_LOG_DEBUG;
1989 break;
1990+ case OPT_LOG_LEVEL:
1991+ if( !parse_enum_value( optarg, log_level_names, &cli_log_level ) )
1992+ cli_log_level += X264_LOG_NONE;
1993+ else
1994+ cli_log_level = atoi( optarg );
1995+ param->i_log_level = cli_log_level;
1996 case OPT_NOPROGRESS:
1997 opt->b_progress = 0;
1998 break;
1999@@ -1051,7 +1092,7 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
2000 param->b_visualize = 1;
2001 b_exit_on_ctrl_c = 1;
2002 #else
2003- fprintf( stderr, "x264 [warning]: not compiled with visualization support\n" );
2004+ x264_cli_log( "x264", X264_LOG_WARNING, "not compiled with visualization support\n" );
2005 #endif
2006 break;
2007 case OPT_TUNE:
2008@@ -1078,18 +1119,13 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
2009 break;
2010 case OPT_TCFILE_OUT:
2011 opt->tcfile_out = fopen( optarg, "wb" );
2012- if( !opt->tcfile_out )
2013- {
2014- fprintf( stderr, "x264 [error]: can't open `%s'\n", optarg );
2015- return -1;
2016- }
2017+ FAIL_IF_ERROR( !opt->tcfile_out, "can't open `%s'\n", optarg )
2018 break;
2019 case OPT_TIMEBASE:
2020 input_opt.timebase = optarg;
2021 break;
2022 case OPT_PULLDOWN:
2023- if( parse_enum_value( optarg, pulldown_names, &opt->i_pulldown ) < 0 )
2024- return -1;
2025+ FAIL_IF_ERROR( parse_enum_value( optarg, pulldown_names, &opt->i_pulldown ), "Unknown pulldown `%s'\n", optarg )
2026 break;
2027 default:
2028 generic_option:
2029@@ -1116,7 +1152,7 @@ generic_option:
2030 if( b_error )
2031 {
2032 const char *name = long_options_index > 0 ? long_options[long_options_index].name : argv[optind-2];
2033- fprintf( stderr, "x264 [error]: invalid argument: %s = %s\n", name, optarg );
2034+ x264_cli_log( "x264", X264_LOG_ERROR, "invalid argument: %s = %s\n", name, optarg );
2035 return -1;
2036 }
2037 }
2038@@ -1130,20 +1166,12 @@ generic_option:
2039 return -1;
2040
2041 /* Get the file name */
2042- if( optind > argc - 1 || !output_filename )
2043- {
2044- fprintf( stderr, "x264 [error]: No %s file. Run x264 --help for a list of options.\n",
2045- optind > argc - 1 ? "input" : "output" );
2046- return -1;
2047- }
2048+ FAIL_IF_ERROR( optind > argc - 1 || !output_filename, "No %s file. Run x264 --help for a list of options.\n",
2049+ optind > argc - 1 ? "input" : "output" )
2050
2051 if( select_output( muxer, output_filename, param ) )
2052 return -1;
2053- if( output.open_file( output_filename, &opt->hout ) )
2054- {
2055- fprintf( stderr, "x264 [error]: could not open output file `%s'\n", output_filename );
2056- return -1;
2057- }
2058+ FAIL_IF_ERROR( output.open_file( output_filename, &opt->hout ), "could not open output file `%s'\n", output_filename )
2059
2060 input_filename = argv[optind++];
2061 input_opt.resolution = optind < argc ? argv[optind++] : NULL;
2062@@ -1163,39 +1191,22 @@ generic_option:
2063 if( select_input( demuxer, demuxername, input_filename, &opt->hin, &info, &input_opt ) )
2064 return -1;
2065
2066- if( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ) )
2067- {
2068- fprintf( stderr, "x264 [error]: could not open input file `%s'\n", input_filename );
2069- return -1;
2070- }
2071+ FAIL_IF_ERROR( !opt->hin && input.open_file( input_filename, &opt->hin, &info, &input_opt ),
2072+ "could not open input file `%s'\n", input_filename )
2073
2074 x264_reduce_fraction( &info.sar_width, &info.sar_height );
2075 x264_reduce_fraction( &info.fps_num, &info.fps_den );
2076- if( param->i_log_level >= X264_LOG_INFO )
2077- fprintf( stderr, "%s [info]: %dx%d%c %d:%d @ %d/%d fps (%cfr)\n", demuxername, info.width,
2078- info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
2079- info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
2080+ x264_cli_log( demuxername, X264_LOG_INFO, "%dx%d%c %d:%d @ %d/%d fps (%cfr)\n", info.width,
2081+ info.height, info.interlaced ? 'i' : 'p', info.sar_width, info.sar_height,
2082+ info.fps_num, info.fps_den, info.vfr ? 'v' : 'c' );
2083
2084 if( tcfile_name )
2085 {
2086- if( b_user_fps )
2087- {
2088- fprintf( stderr, "x264 [error]: --fps + --tcfile-in is incompatible.\n" );
2089- return -1;
2090- }
2091- if( timecode_input.open_file( tcfile_name, &opt->hin, &info, &input_opt ) )
2092- {
2093- fprintf( stderr, "x264 [error]: timecode input failed\n" );
2094- return -1;
2095- }
2096- else
2097- input = timecode_input;
2098- }
2099- else if( !info.vfr && input_opt.timebase )
2100- {
2101- fprintf( stderr, "x264 [error]: --timebase is incompatible with cfr input\n" );
2102- return -1;
2103+ FAIL_IF_ERROR( b_user_fps, "--fps + --tcfile-in is incompatible.\n" )
2104+ FAIL_IF_ERROR( timecode_input.open_file( tcfile_name, &opt->hin, &info, &input_opt ), "timecode input failed\n" )
2105+ input = timecode_input;
2106 }
2107+ else FAIL_IF_ERROR( !info.vfr && input_opt.timebase, "--timebase is incompatible with cfr input\n" )
2108
2109 /* set param flags from the info flags as necessary */
2110 param->i_csp = info.csp;
2111@@ -1204,9 +1215,9 @@ generic_option:
2112 param->i_width = info.width;
2113 if( !b_user_interlaced && info.interlaced )
2114 {
2115- fprintf( stderr, "x264 [warning]: input appears to be interlaced, enabling %cff interlaced mode.\n"
2116- " If you want otherwise, use --no-interlaced or --%cff\n",
2117- info.tff ? 't' : 'b', info.tff ? 'b' : 't' );
2118+ x264_cli_log( "x264", X264_LOG_WARNING, "input appears to be interlaced, enabling %cff interlaced mode.\n"
2119+ " If you want otherwise, use --no-interlaced or --%cff\n",
2120+ info.tff ? 't' : 'b', info.tff ? 'b' : 't' );
2121 param->b_interlaced = 1;
2122 param->b_tff = !!info.tff;
2123 }
2124@@ -1230,21 +1241,14 @@ generic_option:
2125 uint64_t i_user_timebase_num;
2126 uint64_t i_user_timebase_den;
2127 int ret = sscanf( input_opt.timebase, "%"SCNu64"/%"SCNu64, &i_user_timebase_num, &i_user_timebase_den );
2128- if( !ret )
2129- {
2130- fprintf( stderr, "x264 [error]: invalid argument: timebase = %s\n", input_opt.timebase );
2131- return -1;
2132- }
2133+ FAIL_IF_ERROR( !ret, "invalid argument: timebase = %s\n", input_opt.timebase )
2134 else if( ret == 1 )
2135 {
2136 i_user_timebase_num = param->i_timebase_num;
2137 i_user_timebase_den = strtoul( input_opt.timebase, NULL, 10 );
2138 }
2139- if( i_user_timebase_num > UINT32_MAX || i_user_timebase_den > UINT32_MAX )
2140- {
2141- fprintf( stderr, "x264 [error]: timebase you specified exceeds H.264 maximum\n" );
2142- return -1;
2143- }
2144+ FAIL_IF_ERROR( i_user_timebase_num > UINT32_MAX || i_user_timebase_den > UINT32_MAX,
2145+ "timebase you specified exceeds H.264 maximum\n" )
2146 opt->timebase_convert_multiplier = ((double)i_user_timebase_den / param->i_timebase_den)
2147 * ((double)param->i_timebase_num / i_user_timebase_num);
2148 param->i_timebase_num = i_user_timebase_num;
2149@@ -1261,13 +1265,8 @@ generic_option:
2150 if( b_thread_input || param->i_threads > 1
2151 || (param->i_threads == X264_THREADS_AUTO && x264_cpu_num_processors() > 1) )
2152 {
2153- if( thread_input.open_file( NULL, &opt->hin, &info, NULL ) )
2154- {
2155- fprintf( stderr, "x264 [error]: threaded input failed\n" );
2156- return -1;
2157- }
2158- else
2159- input = thread_input;
2160+ FAIL_IF_ERROR( thread_input.open_file( NULL, &opt->hin, &info, NULL ), "threaded input failed\n" )
2161+ input = thread_input;
2162 }
2163 #endif
2164
2165@@ -1321,7 +1320,7 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
2166 else ret = 0;
2167 if( ret != 3 || qp < -1 || qp > 51 )
2168 {
2169- fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
2170+ x264_cli_log( "x264", X264_LOG_ERROR, "can't parse qpfile for frame %d\n", i_frame );
2171 fclose( opt->qpfile );
2172 opt->qpfile = NULL;
2173 pic->i_type = X264_TYPE_AUTO;
2174@@ -1344,11 +1343,7 @@ static int Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic, int64_t *l
2175
2176 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );
2177
2178- if( i_frame_size < 0 )
2179- {
2180- fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
2181- return -1;
2182- }
2183+ FAIL_IF_ERROR( i_frame_size < 0, "x264_encoder_encode failed\n" );
2184
2185 if( i_frame_size )
2186 {
2187@@ -1424,17 +1419,14 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
2188 param->b_pic_struct = 1;
2189 pulldown = &pulldown_values[opt->i_pulldown];
2190 param->i_timebase_num = param->i_fps_den;
2191- if( fmod( param->i_fps_num * pulldown->fps_factor, 1 ) )
2192- {
2193- fprintf( stderr, "x264 [error]: unsupported framerate for chosen pulldown\n" );
2194- return -1;
2195- }
2196+ FAIL_IF_ERROR( fmod( param->i_fps_num * pulldown->fps_factor, 1 ),
2197+ "unsupported framerate for chosen pulldown\n" )
2198 param->i_timebase_den = param->i_fps_num * pulldown->fps_factor;
2199 }
2200
2201 if( ( h = x264_encoder_open( param ) ) == NULL )
2202 {
2203- fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
2204+ x264_cli_log( "x264", X264_LOG_ERROR, "x264_encoder_open failed\n" );
2205 input.close_file( opt->hin );
2206 return -1;
2207 }
2208@@ -1445,27 +1437,19 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
2209
2210 if( output.set_param( opt->hout, param ) )
2211 {
2212- fprintf( stderr, "x264 [error]: can't set outfile param\n" );
2213+ x264_cli_log( "x264", X264_LOG_ERROR, "can't set outfile param\n" );
2214 input.close_file( opt->hin );
2215 output.close_file( opt->hout, largest_pts, second_largest_pts );
2216 return -1;
2217 }
2218
2219 /* Create a new pic */
2220- if( input.picture_alloc( &pic, param->i_csp, param->i_width, param->i_height ) )
2221- {
2222- fprintf( stderr, "x264 [error]: malloc failed\n" );
2223- return -1;
2224- }
2225+ FAIL_IF_ERROR( input.picture_alloc( &pic, param->i_csp, param->i_width, param->i_height ), "malloc failed\n" )
2226
2227 i_start = x264_mdate();
2228 /* ticks/frame = ticks/second / frames/second */
2229 ticks_per_frame = (int64_t)param->i_timebase_den * param->i_fps_den / param->i_timebase_num / param->i_fps_num;
2230- if( ticks_per_frame < 1 )
2231- {
2232- fprintf( stderr, "x264 [error]: ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame );
2233- return -1;
2234- }
2235+ FAIL_IF_ERROR( ticks_per_frame < 1, "ticks_per_frame invalid: %"PRId64"\n", ticks_per_frame )
2236
2237 if( !param->b_repeat_headers )
2238 {
2239@@ -1473,12 +1457,7 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
2240 x264_nal_t *headers;
2241 int i_nal;
2242
2243- if( x264_encoder_headers( h, &headers, &i_nal ) < 0 )
2244- {
2245- fprintf( stderr, "x264 [error]: x264_encoder_headers failed\n" );
2246- return -1;
2247- }
2248-
2249+ FAIL_IF_ERROR( x264_encoder_headers( h, &headers, &i_nal ) < 0, "x264_encoder_headers failed\n" )
2250 if( (i_file = output.write_headers( opt->hout, headers )) < 0 )
2251 return -1;
2252 }
2253@@ -1508,15 +1487,12 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
2254
2255 if( pic.i_pts <= largest_pts )
2256 {
2257- if( param->i_log_level >= X264_LOG_WARNING )
2258- {
2259- if( param->i_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
2260- fprintf( stderr, "x264 [warning]: non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
2261+ if( cli_log_level >= X264_LOG_DEBUG || pts_warning_cnt < MAX_PTS_WARNING )
2262+ x264_cli_log( "x264", X264_LOG_WARNING, "non-strictly-monotonic pts at frame %d (%"PRId64" <= %"PRId64")\n",
2263 i_frame, output_pts, largest_pts * dts_compress_multiplier );
2264- else if( pts_warning_cnt == MAX_PTS_WARNING )
2265- fprintf( stderr, "x264 [warning]: too many nonmonotonic pts warnings, suppressing further ones\n" );
2266- pts_warning_cnt++;
2267- }
2268+ else if( pts_warning_cnt == MAX_PTS_WARNING )
2269+ x264_cli_log( "x264", X264_LOG_WARNING, "too many nonmonotonic pts warnings, suppressing further ones\n" );
2270+ pts_warning_cnt++;
2271 pic.i_pts = largest_pts + ticks_per_frame;
2272 output_pts = pic.i_pts * dts_compress_multiplier;
2273 }
2274@@ -1573,8 +1549,8 @@ static int Encode( x264_param_t *param, cli_opt_t *opt )
2275 if( opt->b_progress && i_frame_output % i_update_interval == 0 && i_frame_output )
2276 Print_status( i_start, i_frame_output, i_frame_total, i_file, param, 2 * last_dts - prev_dts - first_dts );
2277 }
2278- if( pts_warning_cnt >= MAX_PTS_WARNING && param->i_log_level < X264_LOG_DEBUG )
2279- fprintf( stderr, "x264 [warning]: %d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
2280+ if( pts_warning_cnt >= MAX_PTS_WARNING && cli_log_level < X264_LOG_DEBUG )
2281+ x264_cli_log( "x264", X264_LOG_WARNING, "%d suppressed nonmonotonic pts warnings\n", pts_warning_cnt-MAX_PTS_WARNING );
2282
2283 /* duration algorithm fails when only 1 frame is output */
2284 if( i_frame_output == 1 )
2285diff --git a/x264cli.h b/x264cli.h
2286new file mode 100644
2287index 0000000..1acca56
2288--- /dev/null
2289+++ b/x264cli.h
2290@@ -0,0 +1,67 @@
2291+/*****************************************************************************
2292+ * x264cli.h: x264cli common
2293+ *****************************************************************************
2294+ * Copyright (C) 2003-2010 x264 project
2295+ *
2296+ * Authors: Laurent Aimar <fenrir@via.ecp.fr>
2297+ * Loren Merritt <lorenm@u.washington.edu>
2298+ *
2299+ * This program is free software; you can redistribute it and/or modify
2300+ * it under the terms of the GNU General Public License as published by
2301+ * the Free Software Foundation; either version 2 of the License, or
2302+ * (at your option) any later version.
2303+ *
2304+ * This program is distributed in the hope that it will be useful,
2305+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2306+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2307+ * GNU General Public License for more details.
2308+ *
2309+ * You should have received a copy of the GNU General Public License
2310+ * along with this program; if not, write to the Free Software
2311+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
2312+ *****************************************************************************/
2313+
2314+#ifndef X264_CLI_H
2315+#define X264_CLI_H
2316+
2317+#include "common/common.h"
2318+
2319+typedef void *hnd_t;
2320+
2321+static inline int64_t gcd( int64_t a, int64_t b )
2322+{
2323+ while( 1 )
2324+ {
2325+ int64_t c = a % b;
2326+ if( !c )
2327+ return b;
2328+ a = b;
2329+ b = c;
2330+ }
2331+}
2332+
2333+static inline int64_t lcm( int64_t a, int64_t b )
2334+{
2335+ return ( a / gcd( a, b ) ) * b;
2336+}
2337+
2338+static inline char *get_filename_extension( char *filename )
2339+{
2340+ char *ext = filename + strlen( filename );
2341+ while( *ext != '.' && ext > filename )
2342+ ext--;
2343+ ext += *ext == '.';
2344+ return ext;
2345+}
2346+
2347+void x264_cli_log( const char *name, int i_level, const char *fmt, ... );
2348+void x264_cli_printf( int i_level, const char *fmt, ... );
2349+
2350+#define FAIL_IF_ERR( cond, name, ... )\
2351+if( cond )\
2352+{\
2353+ x264_cli_log( name, X264_LOG_ERROR, __VA_ARGS__ );\
2354+ return -1;\
2355+}
2356+
2357+#endif
2358--
23591.7.1
2360
2361
2362From 871bc451d34daf6fb85dad3a9ababa1513ec2333 Mon Sep 17 00:00:00 2001
2363From: Jason Garrett-Glaser <darkshikari@gmail.com>
2364Date: Wed, 30 Jun 2010 13:06:22 -0700
2365Subject: [PATCH 5/7] Don't check i16x16 planar mode unless previous modes were useful
2366 Saves ~160 clocks per MB at subme=1, ~270 per MB at subme>1 (measured on Core i7).
2367 Negligle effect on compression.
2368
2369---
2370 encoder/analyse.c | 27 ++++++++++++++++++---------
2371 1 files changed, 18 insertions(+), 9 deletions(-)
2372
2373diff --git a/encoder/analyse.c b/encoder/analyse.c
2374index 696c78f..eea464a 100644
2375--- a/encoder/analyse.c
2376+++ b/encoder/analyse.c
2377@@ -646,16 +646,27 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
2378 /* 16x16 prediction selection */
2379 const int8_t *predict_mode = predict_16x16_mode_available( h->mb.i_neighbour_intra );
2380
2381+ /* Not heavily tuned */
2382+ const uint8_t i16x16_thresh_lut[11] = { 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4 };
2383+ int i16x16_thresh = a->b_fast_intra ? (i16x16_thresh_lut[h->mb.i_subpel_refine]*i_satd_inter)>>1 : COST_MAX;
2384+
2385 if( !h->mb.b_lossless && predict_mode[3] >= 0 )
2386 {
2387 h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
2388- h->predict_16x16[I_PRED_16x16_P]( p_dst );
2389- a->i_satd_i16x16_dir[I_PRED_16x16_P] =
2390- h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
2391- for( int i = 0; i < 4; i++ )
2392+ a->i_satd_i16x16_dir[0] += lambda * bs_size_ue(0);
2393+ a->i_satd_i16x16_dir[1] += lambda * bs_size_ue(1);
2394+ a->i_satd_i16x16_dir[2] += lambda * bs_size_ue(2);
2395+ COPY2_IF_LT( a->i_satd_i16x16, a->i_satd_i16x16_dir[0], a->i_predict16x16, 0 );
2396+ COPY2_IF_LT( a->i_satd_i16x16, a->i_satd_i16x16_dir[1], a->i_predict16x16, 1 );
2397+ COPY2_IF_LT( a->i_satd_i16x16, a->i_satd_i16x16_dir[2], a->i_predict16x16, 2 );
2398+
2399+ /* Plane is expensive, so don't check it unless one of the previous modes was useful. */
2400+ if( a->i_satd_i16x16 <= i16x16_thresh )
2401 {
2402- int cost = a->i_satd_i16x16_dir[i] += lambda * bs_size_ue(i);
2403- COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
2404+ h->predict_16x16[I_PRED_16x16_P]( p_dst );
2405+ a->i_satd_i16x16_dir[I_PRED_16x16_P] = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
2406+ a->i_satd_i16x16_dir[I_PRED_16x16_P] += lambda * bs_size_ue(3);
2407+ COPY2_IF_LT( a->i_satd_i16x16, a->i_satd_i16x16_dir[I_PRED_16x16_P], a->i_predict16x16, 3 );
2408 }
2409 }
2410 else
2411@@ -681,9 +692,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
2412 /* cavlc mb type prefix */
2413 a->i_satd_i16x16 += lambda * i_mb_b_cost_table[I_16x16];
2414
2415- /* Not heavily tuned */
2416- const uint8_t i16x16_thresh[11] = { 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4 };
2417- if( a->b_fast_intra && a->i_satd_i16x16 > (i16x16_thresh[h->mb.i_subpel_refine]*i_satd_inter)>>1 )
2418+ if( a->i_satd_i16x16 > i16x16_thresh )
2419 return;
2420
2421 /* 8x8 prediction selection */
2422--
24231.7.1
2424
2425
2426From ce182d5e29380cb15cdea68b72bc4a16c05b8cc2 Mon Sep 17 00:00:00 2001
2427From: Jason Garrett-Glaser <darkshikari@gmail.com>
2428Date: Wed, 30 Jun 2010 13:55:46 -0700
2429Subject: [PATCH 6/7] Support infinite keyint (--keyint infinite).
2430 This just means x264 won't insert non-scenecut keyframes.
2431 Useful for streaming when using interactive error recovery or some other mechanism that makes keyframes unnecessary.
2432
2433Also change POC logic to limit POC/framenum LSB size (to save bits per slice).
2434Also fix a bug in the CPB underflow detection code (didn't affect the bitstream, just resulted in the failure to print certain warning messages).
2435---
2436 common/common.c | 7 ++++---
2437 encoder/encoder.c | 8 ++++----
2438 encoder/ratecontrol.c | 10 +++++-----
2439 encoder/set.c | 29 +++++++++++++++++------------
2440 encoder/slicetype.c | 2 +-
2441 x264.c | 2 +-
2442 x264.h | 3 ++-
2443 7 files changed, 34 insertions(+), 27 deletions(-)
2444
2445diff --git a/common/common.c b/common/common.c
2446index 8c7cf3c..14dd716 100644
2447--- a/common/common.c
2448+++ b/common/common.c
2449@@ -638,9 +638,10 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
2450 p->i_dpb_size = atoi(value);
2451 OPT("keyint")
2452 {
2453- p->i_keyint_max = atoi(value);
2454- if( p->i_keyint_min > p->i_keyint_max )
2455- p->i_keyint_min = p->i_keyint_max;
2456+ if( strstr( value, "infinite" ) )
2457+ p->i_keyint_max = X264_KEYINT_MAX_INFINITE;
2458+ else
2459+ p->i_keyint_max = atoi(value);
2460 }
2461 OPT2("min-keyint", "keyint-min")
2462 {
2463diff --git a/encoder/encoder.c b/encoder/encoder.c
2464index 5cd3307..31cb84a 100644
2465--- a/encoder/encoder.c
2466+++ b/encoder/encoder.c
2467@@ -567,8 +567,7 @@ static int x264_validate_parameters( x264_t *h )
2468
2469 h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 16 );
2470 h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, 16 );
2471- if( h->param.i_keyint_max <= 0 )
2472- h->param.i_keyint_max = 1;
2473+ h->param.i_keyint_max = x264_clip3( h->param.i_keyint_max, 1, X264_KEYINT_MAX_INFINITE );
2474 if( h->param.i_scenecut_threshold < 0 )
2475 h->param.i_scenecut_threshold = 0;
2476 if( !h->param.analyse.i_subpel_refine && h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL )
2477@@ -627,9 +626,10 @@ static int x264_validate_parameters( x264_t *h )
2478 h->param.rc.f_qcompress = x264_clip3f( h->param.rc.f_qcompress, 0.0, 1.0 );
2479 if( h->param.i_keyint_max == 1 || h->param.rc.f_qcompress == 1 )
2480 h->param.rc.b_mb_tree = 0;
2481- if( !h->param.rc.i_lookahead && !h->param.b_intra_refresh && h->param.rc.b_mb_tree )
2482+ if( (!h->param.b_intra_refresh && h->param.i_keyint_max != X264_KEYINT_MAX_INFINITE) &&
2483+ !h->param.rc.i_lookahead && h->param.rc.b_mb_tree )
2484 {
2485- x264_log( h, X264_LOG_WARNING, "lookaheadless mb-tree requires intra refresh\n" );
2486+ x264_log( h, X264_LOG_WARNING, "lookaheadless mb-tree requires intra refresh or infinite keyint\n" );
2487 h->param.rc.b_mb_tree = 0;
2488 }
2489 if( h->param.rc.b_stat_read )
2490diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
2491index 1030ef2..6fdaa98 100644
2492--- a/encoder/ratecontrol.c
2493+++ b/encoder/ratecontrol.c
2494@@ -492,13 +492,13 @@ void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
2495 // arbitrary
2496 #define MAX_DURATION 0.5
2497
2498- int max_cpb_output_delay = h->param.i_keyint_max * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick;
2499+ int max_cpb_output_delay = X264_MIN( h->param.i_keyint_max * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick, INT_MAX );
2500 int max_dpb_output_delay = h->sps->vui.i_max_dec_frame_buffering * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick;
2501 int max_delay = (int)(90000.0 * (double)h->sps->vui.hrd.i_cpb_size_unscaled / h->sps->vui.hrd.i_bit_rate_unscaled + 0.5);
2502
2503 h->sps->vui.hrd.i_initial_cpb_removal_delay_length = 2 + x264_clip3( 32 - x264_clz( max_delay ), 4, 22 );
2504- h->sps->vui.hrd.i_cpb_removal_delay_length = x264_clip3( 32 - x264_clz( max_cpb_output_delay ), 4, 32 );
2505- h->sps->vui.hrd.i_dpb_output_delay_length = x264_clip3( 32 - x264_clz( max_dpb_output_delay ), 4, 32 );
2506+ h->sps->vui.hrd.i_cpb_removal_delay_length = x264_clip3( 32 - x264_clz( max_cpb_output_delay ), 4, 31 );
2507+ h->sps->vui.hrd.i_dpb_output_delay_length = x264_clip3( 32 - x264_clz( max_dpb_output_delay ), 4, 31 );
2508
2509 #undef MAX_DURATION
2510
2511@@ -1781,10 +1781,10 @@ void x264_hrd_fullness( x264_t *h )
2512 uint64_t cpb_size = (uint64_t)h->sps->vui.hrd.i_cpb_size_unscaled * h->sps->vui.i_time_scale;
2513 uint64_t multiply_factor = 180000 / rct->hrd_multiply_denom;
2514
2515- if( cpb_state < 0 || cpb_state > cpb_size )
2516+ if( rct->buffer_fill_final < 0 || rct->buffer_fill_final > cpb_size )
2517 {
2518 x264_log( h, X264_LOG_WARNING, "CPB %s: %.0lf bits in a %.0lf-bit buffer\n",
2519- cpb_state < 0 ? "underflow" : "overflow", (float)cpb_state/denom, (float)cpb_size/denom );
2520+ rct->buffer_fill_final < 0 ? "underflow" : "overflow", (float)rct->buffer_fill_final/denom, (float)cpb_size/denom );
2521 }
2522
2523 h->initial_cpb_removal_delay = (multiply_factor * cpb_state + denom) / (2*denom);
2524diff --git a/encoder/set.c b/encoder/set.c
2525index 8d007aa..9a3c7dc 100644
2526--- a/encoder/set.c
2527+++ b/encoder/set.c
2528@@ -99,6 +99,7 @@ static void x264_sei_write( bs_t *s, uint8_t *p_start )
2529 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
2530 {
2531 sps->i_id = i_id;
2532+ int max_frame_num;
2533
2534 sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
2535 if( sps->b_qpprime_y_zero_transform_bypass )
2536@@ -118,15 +119,27 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
2537 /* Never set constraint_set2, it is not necessary and not used in real world. */
2538 sps->b_constraint_set2 = 0;
2539
2540- sps->i_log2_max_frame_num = 4; /* at least 4 */
2541- while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max && sps->i_log2_max_frame_num < 10 )
2542+ sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
2543+ /* extra slot with pyramid so that we don't have to override the
2544+ * order of forgetting old pictures */
2545+ sps->vui.i_max_dec_frame_buffering =
2546+ sps->i_num_ref_frames = X264_MIN(16, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
2547+ param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
2548+ sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
2549+
2550+ /* number of refs + current frame */
2551+ max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
2552+ sps->i_log2_max_frame_num = 4;
2553+ while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
2554 sps->i_log2_max_frame_num++;
2555- sps->i_log2_max_frame_num++;
2556
2557 sps->i_poc_type = 0;
2558 if( sps->i_poc_type == 0 )
2559 {
2560- sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
2561+ int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
2562+ sps->i_log2_max_poc_lsb = 4;
2563+ while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
2564+ sps->i_log2_max_poc_lsb++;
2565 }
2566 else if( sps->i_poc_type == 1 )
2567 {
2568@@ -219,14 +232,6 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
2569
2570 // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
2571
2572- sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
2573- /* extra slot with pyramid so that we don't have to override the
2574- * order of forgetting old pictures */
2575- sps->vui.i_max_dec_frame_buffering =
2576- sps->i_num_ref_frames = X264_MIN(16, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
2577- param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
2578- sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
2579-
2580 sps->vui.b_bitstream_restriction = 1;
2581 if( sps->vui.b_bitstream_restriction )
2582 {
2583diff --git a/encoder/slicetype.c b/encoder/slicetype.c
2584index 4ede8cf..7d69b71 100644
2585--- a/encoder/slicetype.c
2586+++ b/encoder/slicetype.c
2587@@ -1009,7 +1009,7 @@ static int scenecut_internal( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **f
2588 float f_thresh_max = h->param.i_scenecut_threshold / 100.0;
2589 /* magic numbers pulled out of thin air */
2590 float f_thresh_min = f_thresh_max * h->param.i_keyint_min
2591- / ( h->param.i_keyint_max * 4 );
2592+ / ( h->param.i_keyint_max * 4. );
2593 int res;
2594
2595 if( h->param.i_keyint_min == h->param.i_keyint_max )
2596diff --git a/x264.c b/x264.c
2597index 741570c..0bede93 100644
2598--- a/x264.c
2599+++ b/x264.c
2600@@ -409,7 +409,7 @@ static void Help( x264_param_t *defaults, int longhelp )
2601 H0( "\n" );
2602 H0( "Frame-type options:\n" );
2603 H0( "\n" );
2604- H0( " -I, --keyint <integer> Maximum GOP size [%d]\n", defaults->i_keyint_max );
2605+ H0( " -I, --keyint <integer or \"infinite\"> Maximum GOP size [%d]\n", defaults->i_keyint_max );
2606 H2( " -i, --min-keyint <integer> Minimum GOP size [auto]\n" );
2607 H2( " --no-scenecut Disable adaptive I-frame decision\n" );
2608 H2( " --scenecut <integer> How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
2609diff --git a/x264.h b/x264.h
2610index 604ca45..330cf74 100644
2611--- a/x264.h
2612+++ b/x264.h
2613@@ -35,7 +35,7 @@
2614
2615 #include <stdarg.h>
2616
2617-#define X264_BUILD 101
2618+#define X264_BUILD 102
2619
2620 /* x264_t:
2621 * opaque handler for encoder */
2622@@ -152,6 +152,7 @@ typedef struct
2623 #define X264_B_PYRAMID_STRICT 1
2624 #define X264_B_PYRAMID_NORMAL 2
2625 #define X264_KEYINT_MIN_AUTO 0
2626+#define X264_KEYINT_MAX_INFINITE (1<<30)
2627 #define X264_OPEN_GOP_NONE 0
2628 #define X264_OPEN_GOP_NORMAL 1
2629 #define X264_OPEN_GOP_BLURAY 2
2630--
26311.7.1
2632
2633
2634From bf21031b9840588e899ef72f45c009c38c1c8c64 Mon Sep 17 00:00:00 2001
2635From: Oskar Arvidsson <oskar@irock.se>
2636Date: Fri, 2 Jul 2010 04:06:08 +0200
2637Subject: [PATCH 7/7] Support for 9 and 10-bit encoding
2638 Output bit depth is specified on compilation time via --bit-depth.
2639 There is currently almost no assembly code available for high-bit-depth modes, so encoding will be very slow.
2640 Input is still 8-bit only; this will change in the future.
2641
2642Note that very few H.264 decoders support >8 bit depth currently.
2643---
2644 common/arm/mc-c.c | 42 +++++++-----
2645 common/arm/predict-c.c | 8 ++
2646 common/bitstream.h | 2 +-
2647 common/common.c | 17 ++++-
2648 common/common.h | 39 +++++++----
2649 common/dct.c | 15 +++-
2650 common/deblock.c | 27 +++++---
2651 common/macroblock.c | 2 +-
2652 common/macroblock.h | 66 ++++++++++++-------
2653 common/mc.c | 33 ++++-----
2654 common/mc.h | 2 +-
2655 common/pixel.c | 14 +++-
2656 common/ppc/dct.c | 2 +
2657 common/ppc/deblock.c | 2 +
2658 common/ppc/mc.c | 4 +
2659 common/ppc/pixel.c | 4 +
2660 common/ppc/predict.c | 6 ++
2661 common/ppc/quant.c | 2 +
2662 common/predict.c | 63 ++++++++++--------
2663 common/quant.c | 14 ++---
2664 common/set.c | 25 ++++++-
2665 common/x86/mc-c.c | 12 +++-
2666 common/x86/predict-c.c | 10 +++
2667 configure | 16 +++++
2668 encoder/analyse.c | 80 ++++++++++++-----------
2669 encoder/cabac.c | 25 ++++---
2670 encoder/cavlc.c | 24 +++----
2671 encoder/encoder.c | 45 +++++++++----
2672 encoder/macroblock.h | 4 +-
2673 encoder/me.h | 2 +-
2674 encoder/ratecontrol.c | 20 +++---
2675 encoder/rdo.c | 10 +--
2676 encoder/set.c | 8 ++-
2677 encoder/slicetype.c | 10 ++--
2678 tools/checkasm.c | 169 +++++++++++++++++++++++++----------------------
2679 x264.c | 24 ++++---
2680 x264.h | 4 +-
2681 37 files changed, 516 insertions(+), 336 deletions(-)
2682
2683diff --git a/common/arm/mc-c.c b/common/arm/mc-c.c
2684index d294eff..b1106dd 100644
2685--- a/common/arm/mc-c.c
2686+++ b/common/arm/mc-c.c
2687@@ -64,6 +64,19 @@ MC_WEIGHT(_nodenom)
2688 MC_WEIGHT(_offsetadd)
2689 MC_WEIGHT(_offsetsub)
2690
2691+void x264_mc_copy_w4_neon( uint8_t *, int, uint8_t *, int, int );
2692+void x264_mc_copy_w8_neon( uint8_t *, int, uint8_t *, int, int );
2693+void x264_mc_copy_w16_neon( uint8_t *, int, uint8_t *, int, int );
2694+void x264_mc_copy_w16_aligned_neon( uint8_t *, int, uint8_t *, int, int );
2695+
2696+void x264_mc_chroma_neon( uint8_t *, int, uint8_t *, int, int, int, int, int );
2697+void x264_frame_init_lowres_core_neon( uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *, int, int, int, int);
2698+
2699+void x264_hpel_filter_v_neon( uint8_t *, uint8_t *, int16_t *, int, int );
2700+void x264_hpel_filter_c_neon( uint8_t *, int16_t *, int );
2701+void x264_hpel_filter_h_neon( uint8_t *, uint8_t *, int );
2702+
2703+#if !X264_HIGH_BIT_DEPTH
2704 static void x264_weight_cache_neon( x264_t *h, x264_weight_t *w )
2705 {
2706 if( w->i_scale == 1<<w->i_denom )
2707@@ -85,14 +98,6 @@ static void x264_weight_cache_neon( x264_t *h, x264_weight_t *w )
2708 w->weightfn = x264_mc_wtab_neon;
2709 }
2710
2711-void x264_mc_copy_w4_neon( uint8_t *, int, uint8_t *, int, int );
2712-void x264_mc_copy_w8_neon( uint8_t *, int, uint8_t *, int, int );
2713-void x264_mc_copy_w16_neon( uint8_t *, int, uint8_t *, int, int );
2714-void x264_mc_copy_w16_aligned_neon( uint8_t *, int, uint8_t *, int, int );
2715-
2716-void x264_mc_chroma_neon( uint8_t *, int, uint8_t *, int, int, int, int, int );
2717-void x264_frame_init_lowres_core_neon( uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *, int, int, int, int);
2718-
2719 static void (* const x264_pixel_avg_wtab_neon[6])( uint8_t *, int, uint8_t *, int, uint8_t *, int ) =
2720 {
2721 NULL,
2722@@ -174,10 +179,6 @@ static uint8_t *get_ref_neon( uint8_t *dst, int *i_dst_stride,
2723 }
2724 }
2725
2726-void x264_hpel_filter_v_neon( uint8_t *, uint8_t *, int16_t *, int, int );
2727-void x264_hpel_filter_c_neon( uint8_t *, int16_t *, int );
2728-void x264_hpel_filter_h_neon( uint8_t *, uint8_t *, int );
2729-
2730 static void hpel_filter_neon( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,
2731 int stride, int width, int height, int16_t *buf )
2732 {
2733@@ -198,18 +199,22 @@ static void hpel_filter_neon( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8
2734 src += stride;
2735 }
2736 }
2737+#endif // !X264_HIGH_BIT_DEPTH
2738
2739 void x264_mc_init_arm( int cpu, x264_mc_functions_t *pf )
2740 {
2741 if( !(cpu&X264_CPU_ARMV6) )
2742 return;
2743
2744+#if !X264_HIGH_BIT_DEPTH
2745 pf->prefetch_fenc = x264_prefetch_fenc_arm;
2746 pf->prefetch_ref = x264_prefetch_ref_arm;
2747+#endif // !X264_HIGH_BIT_DEPTH
2748
2749 if( !(cpu&X264_CPU_NEON) )
2750 return;
2751
2752+#if !X264_HIGH_BIT_DEPTH
2753 pf->copy_16x16_unaligned = x264_mc_copy_w16_neon;
2754 pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_neon;
2755 pf->copy[PIXEL_8x8] = x264_mc_copy_w8_neon;
2756@@ -229,15 +234,16 @@ void x264_mc_init_arm( int cpu, x264_mc_functions_t *pf )
2757 pf->offsetsub = x264_mc_offsetsub_wtab_neon;
2758 pf->weight_cache = x264_weight_cache_neon;
2759
2760-// Apple's gcc stupidly cannot align stack variables, and ALIGNED_ARRAY can't work on structs
2761-#ifndef SYS_MACOSX
2762- pf->memcpy_aligned = x264_memcpy_aligned_neon;
2763-#endif
2764- pf->memzero_aligned = x264_memzero_aligned_neon;
2765-
2766 pf->mc_chroma = x264_mc_chroma_neon;
2767 pf->mc_luma = mc_luma_neon;
2768 pf->get_ref = get_ref_neon;
2769 pf->hpel_filter = hpel_filter_neon;
2770 pf->frame_init_lowres_core = x264_frame_init_lowres_core_neon;
2771+#endif // !X264_HIGH_BIT_DEPTH
2772+
2773+// Apple's gcc stupidly cannot align stack variables, and ALIGNED_ARRAY can't work on structs
2774+#ifndef SYS_MACOSX
2775+ pf->memcpy_aligned = x264_memcpy_aligned_neon;
2776+#endif
2777+ pf->memzero_aligned = x264_memzero_aligned_neon;
2778 }
2779diff --git a/common/arm/predict-c.c b/common/arm/predict-c.c
2780index fa7b9f7..b40dc9a 100644
2781--- a/common/arm/predict-c.c
2782+++ b/common/arm/predict-c.c
2783@@ -51,6 +51,7 @@ void x264_predict_4x4_init_arm( int cpu, x264_predict_t pf[12] )
2784 if (!(cpu&X264_CPU_ARMV6))
2785 return;
2786
2787+#if !X264_HIGH_BIT_DEPTH
2788 pf[I_PRED_4x4_H] = x264_predict_4x4_h_armv6;
2789 pf[I_PRED_4x4_DC] = x264_predict_4x4_dc_armv6;
2790 pf[I_PRED_4x4_DDR] = x264_predict_4x4_ddr_armv6;
2791@@ -59,6 +60,7 @@ void x264_predict_4x4_init_arm( int cpu, x264_predict_t pf[12] )
2792 return;
2793
2794 pf[I_PRED_4x4_DDL] = x264_predict_4x4_ddl_neon;
2795+#endif // !X264_HIGH_BIT_DEPTH
2796 }
2797
2798 void x264_predict_8x8c_init_arm( int cpu, x264_predict_t pf[7] )
2799@@ -66,12 +68,14 @@ void x264_predict_8x8c_init_arm( int cpu, x264_predict_t pf[7] )
2800 if (!(cpu&X264_CPU_NEON))
2801 return;
2802
2803+#if !X264_HIGH_BIT_DEPTH
2804 pf[I_PRED_CHROMA_DC] = x264_predict_8x8c_dc_neon;
2805 pf[I_PRED_CHROMA_DC_TOP] = x264_predict_8x8c_dc_top_neon;
2806 pf[I_PRED_CHROMA_DC_LEFT] = x264_predict_8x8c_dc_left_neon;
2807 pf[I_PRED_CHROMA_H] = x264_predict_8x8c_h_neon;
2808 pf[I_PRED_CHROMA_V] = x264_predict_8x8c_v_neon;
2809 pf[I_PRED_CHROMA_P] = x264_predict_8x8c_p_neon;
2810+#endif // !X264_HIGH_BIT_DEPTH
2811 }
2812
2813 void x264_predict_8x8_init_arm( int cpu, x264_predict8x8_t pf[12], x264_predict_8x8_filter_t *predict_filter )
2814@@ -79,8 +83,10 @@ void x264_predict_8x8_init_arm( int cpu, x264_predict8x8_t pf[12], x264_predict_
2815 if (!(cpu&X264_CPU_NEON))
2816 return;
2817
2818+#if !X264_HIGH_BIT_DEPTH
2819 pf[I_PRED_8x8_DC] = x264_predict_8x8_dc_neon;
2820 pf[I_PRED_8x8_H] = x264_predict_8x8_h_neon;
2821+#endif // !X264_HIGH_BIT_DEPTH
2822 }
2823
2824 void x264_predict_16x16_init_arm( int cpu, x264_predict_t pf[7] )
2825@@ -88,10 +94,12 @@ void x264_predict_16x16_init_arm( int cpu, x264_predict_t pf[7] )
2826 if (!(cpu&X264_CPU_NEON))
2827 return;
2828
2829+#if !X264_HIGH_BIT_DEPTH
2830 pf[I_PRED_16x16_DC ] = x264_predict_16x16_dc_neon;
2831 pf[I_PRED_16x16_DC_TOP] = x264_predict_16x16_dc_top_neon;
2832 pf[I_PRED_16x16_DC_LEFT]= x264_predict_16x16_dc_left_neon;
2833 pf[I_PRED_16x16_H ] = x264_predict_16x16_h_neon;
2834 pf[I_PRED_16x16_V ] = x264_predict_16x16_v_neon;
2835 pf[I_PRED_16x16_P ] = x264_predict_16x16_p_neon;
2836+#endif // !X264_HIGH_BIT_DEPTH
2837 }
2838diff --git a/common/bitstream.h b/common/bitstream.h
2839index dd8118d..318c790 100644
2840--- a/common/bitstream.h
2841+++ b/common/bitstream.h
2842@@ -53,7 +53,7 @@ typedef struct bs_s
2843 typedef struct
2844 {
2845 int last;
2846- int16_t level[16];
2847+ dctcoef level[16];
2848 uint8_t run[16];
2849 } x264_run_level_t;
2850
2851diff --git a/common/common.c b/common/common.c
2852index 14dd716..728dfab 100644
2853--- a/common/common.c
2854+++ b/common/common.c
2855@@ -91,10 +91,10 @@ void x264_param_default( x264_param_t *param )
2856 param->rc.i_vbv_max_bitrate = 0;
2857 param->rc.i_vbv_buffer_size = 0;
2858 param->rc.f_vbv_buffer_init = 0.9;
2859- param->rc.i_qp_constant = 23;
2860- param->rc.f_rf_constant = 23;
2861+ param->rc.i_qp_constant = 23 + QP_BD_OFFSET;
2862+ param->rc.f_rf_constant = 23 + QP_BD_OFFSET;
2863 param->rc.i_qp_min = 10;
2864- param->rc.i_qp_max = 51;
2865+ param->rc.i_qp_max = QP_MAX;
2866 param->rc.i_qp_step = 4;
2867 param->rc.f_ip_factor = 1.4;
2868 param->rc.f_pb_factor = 1.3;
2869@@ -418,6 +418,15 @@ int x264_param_apply_profile( x264_param_t *param, const char *profile )
2870 if( !profile )
2871 return 0;
2872
2873+#if BIT_DEPTH > 8
2874+ if( !strcasecmp( profile, "baseline" ) || !strcasecmp( profile, "main" ) ||
2875+ !strcasecmp( profile, "high" ) )
2876+ {
2877+ x264_log( NULL, X264_LOG_ERROR, "%s profile doesn't support a bit depth of %d.\n", profile, BIT_DEPTH );
2878+ return -1;
2879+ }
2880+#endif
2881+
2882 if( !strcasecmp( profile, "baseline" ) )
2883 {
2884 param->analyse.b_transform_8x8 = 0;
2885@@ -441,7 +450,7 @@ int x264_param_apply_profile( x264_param_t *param, const char *profile )
2886 param->analyse.b_transform_8x8 = 0;
2887 param->i_cqm_preset = X264_CQM_FLAT;
2888 }
2889- else if( !strcasecmp( profile, "high" ) )
2890+ else if( !strcasecmp( profile, "high" ) || !strcasecmp( profile, "high10" ) )
2891 {
2892 /* Default */
2893 }
2894diff --git a/common/common.h b/common/common.h
2895index 7b60811..a218d35 100644
2896--- a/common/common.h
2897+++ b/common/common.h
2898@@ -54,8 +54,13 @@ do {\
2899 #define X264_THREAD_MAX 128
2900 #define X264_PCM_COST (386*8)
2901 #define X264_LOOKAHEAD_MAX 250
2902+#define QP_BD_OFFSET (6*(BIT_DEPTH-8))
2903+#define QP_MAX (51+QP_BD_OFFSET)
2904+#define QP_MAX_MAX (51+2*6)
2905+#define LAMBDA_MAX (91 << (BIT_DEPTH-8))
2906+#define PIXEL_MAX ((1 << BIT_DEPTH)-1)
2907 // arbitrary, but low because SATD scores are 1/4 normal
2908-#define X264_LOOKAHEAD_QP 12
2909+#define X264_LOOKAHEAD_QP (12+QP_BD_OFFSET)
2910
2911 // number of pixels (per thread) in progress at any given time.
2912 // 16 for the macroblock in progress + 3 for deblocking + 3 for motion compensation filter + 2 for extra safety
2913@@ -101,17 +106,23 @@ typedef union { x264_uint128_t i; uint64_t a[2]; uint32_t b[4]; uint16_t c[8]; u
2914 #define CP64(dst,src) M64(dst) = M64(src)
2915 #define CP128(dst,src) M128(dst) = M128(src)
2916
2917-typedef uint8_t pixel;
2918-typedef uint32_t pixel4;
2919-typedef int16_t dctcoef;
2920+#if X264_HIGH_BIT_DEPTH
2921+ typedef uint16_t pixel;
2922+ typedef uint64_t pixel4;
2923+ typedef int32_t dctcoef;
2924
2925-#define PIXEL_SPLAT_X4(x) ((x)*0x01010101U)
2926-#define MPIXEL_X4(src) M32(src)
2927-#define CPPIXEL_X4(dst,src) CP32(dst,src)
2928-#define CPPIXEL_X8(dst,src) CP64(dst,src)
2929-#define MDCT_X2(dct) M32(dct)
2930-#define CPDCT_X2(dst,src) CP32(dst,src)
2931-#define CPDCT_X4(dst,src) CP64(dst,src)
2932+# define PIXEL_SPLAT_X4(x) ((x)*0x0001000100010001ULL)
2933+# define MPIXEL_X4(src) M64(src)
2934+#else
2935+ typedef uint8_t pixel;
2936+ typedef uint32_t pixel4;
2937+ typedef int16_t dctcoef;
2938+
2939+# define PIXEL_SPLAT_X4(x) ((x)*0x01010101U)
2940+# define MPIXEL_X4(src) M32(src)
2941+#endif
2942+
2943+#define CPPIXEL_X4(dst,src) MPIXEL_X4(dst) = MPIXEL_X4(src)
2944
2945 #define X264_SCAN8_SIZE (6*8)
2946 #define X264_SCAN8_LUMA_SIZE (5*8)
2947@@ -189,7 +200,7 @@ void x264_init_vlc_tables();
2948
2949 static ALWAYS_INLINE pixel x264_clip_pixel( int x )
2950 {
2951- return x&(~255) ? (-x)>>31 : x;
2952+ return ( (x & ~PIXEL_MAX) ? (-x)>>31 & PIXEL_MAX : x );
2953 }
2954
2955 static ALWAYS_INLINE int x264_clip3( int v, int i_min, int i_max )
2956@@ -449,8 +460,8 @@ struct x264_t
2957 /* mv/ref cost arrays. Indexed by lambda instead of
2958 * qp because, due to rounding, some quantizers share
2959 * lambdas. This saves memory. */
2960- uint16_t *cost_mv[92];
2961- uint16_t *cost_mv_fpel[92][4];
2962+ uint16_t *cost_mv[LAMBDA_MAX+1];
2963+ uint16_t *cost_mv_fpel[LAMBDA_MAX+1][4];
2964
2965 const uint8_t *chroma_qp_table; /* includes both the nonlinear luma->chroma mapping and chroma_qp_offset */
2966
2967diff --git a/common/dct.c b/common/dct.c
2968index 60dbd55..cd27363 100644
2969--- a/common/dct.c
2970+++ b/common/dct.c
2971@@ -418,6 +418,7 @@ void x264_dct_init( int cpu, x264_dct_function_t *dctf )
2972 dctf->dct4x4dc = dct4x4dc;
2973 dctf->idct4x4dc = idct4x4dc;
2974
2975+#if !X264_HIGH_BIT_DEPTH
2976 #if HAVE_MMX
2977 if( cpu&X264_CPU_MMX )
2978 {
2979@@ -515,6 +516,7 @@ void x264_dct_init( int cpu, x264_dct_function_t *dctf )
2980 dctf->add16x16_idct8= x264_add16x16_idct8_neon;
2981 }
2982 #endif
2983+#endif // !X264_HIGH_BIT_DEPTH
2984 }
2985
2986 void x264_dct_init_weights( void )
2987@@ -599,11 +601,9 @@ static void zigzag_scan_4x4_frame( dctcoef level[16], dctcoef dct[16] )
2988
2989 static void zigzag_scan_4x4_field( dctcoef level[16], dctcoef dct[16] )
2990 {
2991- CPDCT_X2( level, dct );
2992+ memcpy( level, dct, 2 * sizeof(dctcoef) );
2993 ZIG(2,0,1) ZIG(3,2,0) ZIG(4,3,0) ZIG(5,1,1)
2994- CPDCT_X2( level+6, dct+6 );
2995- CPDCT_X4( level+8, dct+8 );
2996- CPDCT_X4( level+12, dct+12 );
2997+ memcpy( level+6, dct+6, 10 * sizeof(dctcoef) );
2998 }
2999
3000 #undef ZIG
3001@@ -618,6 +618,7 @@ static void zigzag_scan_4x4_field( dctcoef level[16], dctcoef dct[16] )
3002 CPPIXEL_X4( p_dst+1*FDEC_STRIDE, p_src+1*FENC_STRIDE );\
3003 CPPIXEL_X4( p_dst+2*FDEC_STRIDE, p_src+2*FENC_STRIDE );\
3004 CPPIXEL_X4( p_dst+3*FDEC_STRIDE, p_src+3*FENC_STRIDE );
3005+#define CPPIXEL_X8(dst,src) ( CPPIXEL_X4(dst,src), CPPIXEL_X4(dst+4,src+4) )
3006 #define COPY8x8\
3007 CPPIXEL_X8( p_dst+0*FDEC_STRIDE, p_src+0*FENC_STRIDE );\
3008 CPPIXEL_X8( p_dst+1*FDEC_STRIDE, p_src+1*FENC_STRIDE );\
3009@@ -709,6 +710,7 @@ void x264_zigzag_init( int cpu, x264_zigzag_function_t *pf, int b_interlaced )
3010 pf->sub_8x8 = zigzag_sub_8x8_field;
3011 pf->sub_4x4 = zigzag_sub_4x4_field;
3012 pf->sub_4x4ac = zigzag_sub_4x4ac_field;
3013+#if !X264_HIGH_BIT_DEPTH
3014 #if HAVE_MMX
3015 if( cpu&X264_CPU_MMXEXT )
3016 {
3017@@ -726,6 +728,7 @@ void x264_zigzag_init( int cpu, x264_zigzag_function_t *pf, int b_interlaced )
3018 if( cpu&X264_CPU_ALTIVEC )
3019 pf->scan_4x4 = x264_zigzag_scan_4x4_field_altivec;
3020 #endif
3021+#endif // !X264_HIGH_BIT_DEPTH
3022 }
3023 else
3024 {
3025@@ -734,6 +737,7 @@ void x264_zigzag_init( int cpu, x264_zigzag_function_t *pf, int b_interlaced )
3026 pf->sub_8x8 = zigzag_sub_8x8_frame;
3027 pf->sub_4x4 = zigzag_sub_4x4_frame;
3028 pf->sub_4x4ac = zigzag_sub_4x4ac_frame;
3029+#if !X264_HIGH_BIT_DEPTH
3030 #if HAVE_MMX
3031 if( cpu&X264_CPU_MMX )
3032 pf->scan_4x4 = x264_zigzag_scan_4x4_frame_mmx;
3033@@ -759,13 +763,16 @@ void x264_zigzag_init( int cpu, x264_zigzag_function_t *pf, int b_interlaced )
3034 if( cpu&X264_CPU_NEON )
3035 pf->scan_4x4 = x264_zigzag_scan_4x4_frame_neon;
3036 #endif
3037+#endif // !X264_HIGH_BIT_DEPTH
3038 }
3039
3040 pf->interleave_8x8_cavlc = zigzag_interleave_8x8_cavlc;
3041+#if !X264_HIGH_BIT_DEPTH
3042 #if HAVE_MMX
3043 if( cpu&X264_CPU_MMX )
3044 pf->interleave_8x8_cavlc = x264_zigzag_interleave_8x8_cavlc_mmx;
3045 if( cpu&X264_CPU_SHUFFLE_IS_FAST )
3046 pf->interleave_8x8_cavlc = x264_zigzag_interleave_8x8_cavlc_sse2;
3047 #endif
3048+#endif // !X264_HIGH_BIT_DEPTH
3049 }
3050diff --git a/common/deblock.c b/common/deblock.c
3051index db9c95d..0b3b6df 100644
3052--- a/common/deblock.c
3053+++ b/common/deblock.c
3054@@ -265,18 +265,19 @@ static void deblock_strength_c( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264
3055
3056 static inline void deblock_edge( x264_t *h, pixel *pix1, pixel *pix2, int i_stride, uint8_t bS[4], int i_qp, int b_chroma, x264_deblock_inter_t pf_inter )
3057 {
3058- int index_a = i_qp + h->sh.i_alpha_c0_offset;
3059- int alpha = alpha_table(index_a);
3060- int beta = beta_table(i_qp + h->sh.i_beta_offset);
3061+ int index_a = x264_clip3((i_qp-QP_BD_OFFSET + h->sh.i_alpha_c0_offset), 0, 51);
3062+ int index_b = x264_clip3((i_qp-QP_BD_OFFSET + h->sh.i_beta_offset), 0, 51);
3063+ int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
3064+ int beta = beta_table(index_b) << (BIT_DEPTH-8);
3065 int8_t tc[4];
3066
3067 if( !M32(bS) || !alpha || !beta )
3068 return;
3069
3070- tc[0] = tc0_table(index_a)[bS[0]] + b_chroma;
3071- tc[1] = tc0_table(index_a)[bS[1]] + b_chroma;
3072- tc[2] = tc0_table(index_a)[bS[2]] + b_chroma;
3073- tc[3] = tc0_table(index_a)[bS[3]] + b_chroma;
3074+ tc[0] = (tc0_table(index_a)[bS[0]] << (BIT_DEPTH-8)) + b_chroma;
3075+ tc[1] = (tc0_table(index_a)[bS[1]] << (BIT_DEPTH-8)) + b_chroma;
3076+ tc[2] = (tc0_table(index_a)[bS[2]] << (BIT_DEPTH-8)) + b_chroma;
3077+ tc[3] = (tc0_table(index_a)[bS[3]] << (BIT_DEPTH-8)) + b_chroma;
3078
3079 pf_inter( pix1, i_stride, alpha, beta, tc );
3080 if( b_chroma )
3081@@ -285,8 +286,10 @@ static inline void deblock_edge( x264_t *h, pixel *pix1, pixel *pix2, int i_stri
3082
3083 static inline void deblock_edge_intra( x264_t *h, pixel *pix1, pixel *pix2, int i_stride, uint8_t bS[4], int i_qp, int b_chroma, x264_deblock_intra_t pf_intra )
3084 {
3085- int alpha = alpha_table(i_qp + h->sh.i_alpha_c0_offset);
3086- int beta = beta_table(i_qp + h->sh.i_beta_offset);
3087+ int index_a = x264_clip3((i_qp-QP_BD_OFFSET + h->sh.i_alpha_c0_offset), 0, 51);
3088+ int index_b = x264_clip3((i_qp-QP_BD_OFFSET + h->sh.i_beta_offset), 0, 51);
3089+ int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
3090+ int beta = beta_table(index_b) << (BIT_DEPTH-8);
3091
3092 if( !alpha || !beta )
3093 return;
3094@@ -450,6 +453,7 @@ void x264_deblock_init( int cpu, x264_deblock_function_t *pf )
3095 #if HAVE_MMX
3096 if( cpu&X264_CPU_MMXEXT )
3097 {
3098+#if !X264_HIGH_BIT_DEPTH
3099 pf->deblock_chroma[1] = x264_deblock_v_chroma_mmxext;
3100 pf->deblock_chroma[0] = x264_deblock_h_chroma_mmxext;
3101 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_mmxext;
3102@@ -460,10 +464,12 @@ void x264_deblock_init( int cpu, x264_deblock_function_t *pf )
3103 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_mmxext;
3104 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_mmxext;
3105 #endif
3106+#endif // !X264_HIGH_BIT_DEPTH
3107 pf->deblock_strength = x264_deblock_strength_mmxext;
3108 if( cpu&X264_CPU_SSE2 )
3109 {
3110 pf->deblock_strength = x264_deblock_strength_sse2;
3111+#if !X264_HIGH_BIT_DEPTH
3112 if( !(cpu&X264_CPU_STACK_MOD4) )
3113 {
3114 pf->deblock_luma[1] = x264_deblock_v_luma_sse2;
3115@@ -471,12 +477,14 @@ void x264_deblock_init( int cpu, x264_deblock_function_t *pf )
3116 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_sse2;
3117 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_sse2;
3118 }
3119+#endif // !X264_HIGH_BIT_DEPTH
3120 }
3121 if( cpu&X264_CPU_SSSE3 )
3122 pf->deblock_strength = x264_deblock_strength_ssse3;
3123 }
3124 #endif
3125
3126+#if !X264_HIGH_BIT_DEPTH
3127 #if HAVE_ALTIVEC
3128 if( cpu&X264_CPU_ALTIVEC )
3129 {
3130@@ -494,4 +502,5 @@ void x264_deblock_init( int cpu, x264_deblock_function_t *pf )
3131 pf->deblock_chroma[0] = x264_deblock_h_chroma_neon;
3132 }
3133 #endif
3134+#endif // !X264_HIGH_BIT_DEPTH
3135 }
3136diff --git a/common/macroblock.c b/common/macroblock.c
3137index 4561d8a..f0a624f 100644
3138--- a/common/macroblock.c
3139+++ b/common/macroblock.c
3140@@ -337,7 +337,7 @@ int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
3141 int scratch_size = 0;
3142 if( !b_lookahead )
3143 {
3144- int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
3145+ int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(dctcoef);
3146 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
3147 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
3148 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
3149diff --git a/common/macroblock.h b/common/macroblock.h
3150index 1a4992f..e09cd55 100644
3151--- a/common/macroblock.h
3152+++ b/common/macroblock.h
3153@@ -238,17 +238,30 @@ static const uint16_t block_idx_xy_fdec[16] =
3154 2*4 + 3*4*FDEC_STRIDE, 3*4 + 3*4*FDEC_STRIDE
3155 };
3156
3157-static const uint8_t i_chroma_qp_table[52+12*2] =
3158+#define QP(qP) ( (qP)+QP_BD_OFFSET )
3159+static const uint8_t i_chroma_qp_table[QP_MAX+1+12*2] =
3160 {
3161- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3162- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
3163- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
3164- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
3165- 29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
3166- 36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
3167- 39, 39,
3168- 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
3169+ 0, 0, 0, 0, 0, 0,
3170+ 0, 0, 0, 0, 0, 0,
3171+#if BIT_DEPTH > 9
3172+ QP(-12),QP(-11),QP(-10), QP(-9), QP(-8), QP(-7),
3173+#endif
3174+#if BIT_DEPTH > 8
3175+ QP(-6), QP(-5), QP(-4), QP(-3), QP(-2), QP(-1),
3176+#endif
3177+ QP(0), QP(1), QP(2), QP(3), QP(4), QP(5),
3178+ QP(6), QP(7), QP(8), QP(9), QP(10), QP(11),
3179+ QP(12), QP(13), QP(14), QP(15), QP(16), QP(17),
3180+ QP(18), QP(19), QP(20), QP(21), QP(22), QP(23),
3181+ QP(24), QP(25), QP(26), QP(27), QP(28), QP(29),
3182+ QP(29), QP(30), QP(31), QP(32), QP(32), QP(33),
3183+ QP(34), QP(34), QP(35), QP(35), QP(36), QP(36),
3184+ QP(37), QP(37), QP(37), QP(38), QP(38), QP(38),
3185+ QP(39), QP(39), QP(39), QP(39),
3186+ QP(39), QP(39), QP(39), QP(39), QP(39), QP(39),
3187+ QP(39), QP(39), QP(39), QP(39), QP(39), QP(39),
3188 };
3189+#undef QP
3190
3191 enum cabac_ctx_block_cat_e
3192 {
3193@@ -340,26 +353,31 @@ static ALWAYS_INLINE uint32_t pack16to32_mask( int a, int b )
3194 return (a&0xFFFF) + (b<<16);
3195 #endif
3196 }
3197+static ALWAYS_INLINE uint64_t pack32to64( uint32_t a, uint32_t b )
3198+{
3199+#ifdef WORDS_BIGENDIAN
3200+ return b + ((uint64_t)a<<32);
3201+#else
3202+ return a + ((uint64_t)b<<32);
3203+#endif
3204+}
3205
3206-#define pack_pixel_1to2 pack8to16
3207-#define pack_pixel_2to4 pack16to32
3208+#if X264_HIGH_BIT_DEPTH
3209+# define pack_pixel_1to2 pack16to32
3210+# define pack_pixel_2to4 pack32to64
3211+#else
3212+# define pack_pixel_1to2 pack8to16
3213+# define pack_pixel_2to4 pack16to32
3214+#endif
3215
3216-#define array_non_zero(a) array_non_zero_int(a, sizeof(a))
3217+#define array_non_zero(a) array_non_zero_int(a, sizeof(a)/sizeof(dctcoef))
3218 #define array_non_zero_int array_non_zero_int
3219 static ALWAYS_INLINE int array_non_zero_int( dctcoef *v, int i_count )
3220 {
3221- if(i_count == 8)
3222- return !!M64( &v[0] );
3223- else if(i_count == 16)
3224- return !!(M64( &v[0] ) | M64( &v[4] ));
3225- else if(i_count == 32)
3226- return !!(M64( &v[0] ) | M64( &v[4] ) | M64( &v[8] ) | M64( &v[12] ));
3227- else
3228- {
3229- for( int i = 0; i < i_count; i+=4 )
3230- if( M64( &v[i] ) ) return 1;
3231- return 0;
3232- }
3233+ for( int i = 0; i < i_count; i++ )
3234+ if( v[i] )
3235+ return 1;
3236+ return 0;
3237 }
3238 static ALWAYS_INLINE int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
3239 {
3240diff --git a/common/mc.c b/common/mc.c
3241index 9776bec..5ef0682 100644
3242--- a/common/mc.c
3243+++ b/common/mc.c
3244@@ -117,11 +117,14 @@ static void x264_weight_cache( x264_t *h, x264_weight_t *w )
3245 {
3246 w->weightfn = h->mc.weight;
3247 }
3248-#define opscale(x) dst[x] = x264_clip_pixel( ((src[x] * weight->i_scale + (1<<(weight->i_denom - 1))) >> weight->i_denom) + weight->i_offset )
3249-#define opscale_noden(x) dst[x] = x264_clip_pixel( src[x] * weight->i_scale + weight->i_offset )
3250-static inline void mc_weight( pixel *dst, int i_dst_stride, pixel *src, int i_src_stride, const x264_weight_t *weight, int i_width, int i_height )
3251+#define opscale(x) dst[x] = x264_clip_pixel( ((src[x] * scale + (1<<(denom - 1))) >> denom) + offset )
3252+#define opscale_noden(x) dst[x] = x264_clip_pixel( src[x] * scale + offset )
3253+static void mc_weight( pixel *dst, int i_dst_stride, pixel *src, int i_src_stride, const x264_weight_t *weight, int i_width, int i_height )
3254 {
3255- if( weight->i_denom >= 1 )
3256+ int offset = weight->i_offset << (BIT_DEPTH-8);
3257+ int scale = weight->i_scale;
3258+ int denom = weight->i_denom;
3259+ if( denom >= 1 )
3260 {
3261 for( int y = 0; y < i_height; y++, dst += i_dst_stride, src += i_src_stride )
3262 for( int x = 0; x < i_width; x++ )
3263@@ -135,21 +138,10 @@ static inline void mc_weight( pixel *dst, int i_dst_stride, pixel *src, int i_sr
3264 }
3265 }
3266
3267-#define MC_WEIGHT_C( name, lx ) \
3268+#define MC_WEIGHT_C( name, width ) \
3269 static void name( pixel *dst, int i_dst_stride, pixel *src, int i_src_stride, const x264_weight_t *weight, int height ) \
3270 { \
3271- if( weight->i_denom >= 1 ) \
3272- { \
3273- for( int y = 0; y < height; y++, dst += i_dst_stride, src += i_src_stride ) \
3274- for( int x = 0; x < lx; x++ ) \
3275- opscale( x ); \
3276- } \
3277- else \
3278- { \
3279- for( int y = 0; y < height; y++, dst += i_dst_stride, src += i_src_stride ) \
3280- for( int x = 0; x < lx; x++ ) \
3281- opscale_noden( x ); \
3282- } \
3283+ mc_weight( dst, i_dst_stride, src, i_src_stride, weight, width, height );\
3284 }
3285
3286 MC_WEIGHT_C( mc_weight_w20, 20 )
3287@@ -182,7 +174,7 @@ static void mc_copy( pixel *src, int i_src_stride, pixel *dst, int i_dst_stride,
3288
3289 #define TAPFILTER(pix, d) ((pix)[x-2*d] + (pix)[x+3*d] - 5*((pix)[x-d] + (pix)[x+2*d]) + 20*((pix)[x] + (pix)[x+d]))
3290 static void hpel_filter( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,
3291- int stride, int width, int height, int16_t *buf )
3292+ int stride, int width, int height, dctcoef *buf )
3293 {
3294 for( int y = 0; y < height; y++ )
3295 {
3296@@ -301,7 +293,12 @@ void x264_plane_copy_c( pixel *dst, int i_dst,
3297 {
3298 while( h-- )
3299 {
3300+#if X264_HIGH_BIT_DEPTH
3301+ for( int i = 0; i < w; i++ )
3302+ dst[i] = src[i] << (BIT_DEPTH-8);
3303+#else
3304 memcpy( dst, src, w );
3305+#endif
3306 dst += i_dst;
3307 src += i_src;
3308 }
3309diff --git a/common/mc.h b/common/mc.h
3310index bb16d13..cbdf1a6 100644
3311--- a/common/mc.h
3312+++ b/common/mc.h
3313@@ -82,7 +82,7 @@ typedef struct
3314 uint8_t *src, int i_src, int w, int h);
3315
3316 void (*hpel_filter)( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,
3317- int i_stride, int i_width, int i_height, int16_t *buf );
3318+ int i_stride, int i_width, int i_height, dctcoef *buf );
3319
3320 /* prefetch the next few macroblocks of fenc or fdec */
3321 void (*prefetch_fenc)( pixel *pix_y, int stride_y,
3322diff --git a/common/pixel.c b/common/pixel.c
3323index 8441c7a..069589f 100644
3324--- a/common/pixel.c
3325+++ b/common/pixel.c
3326@@ -177,7 +177,7 @@ static int pixel_var2_8x8( pixel *pix1, int i_stride1, pixel *pix2, int i_stride
3327 pix2 += i_stride2;
3328 }
3329 sum = abs(sum);
3330- var = sqr - (sum * sum >> 6);
3331+ var = sqr - ((uint64_t)sum * sum >> 6);
3332 *ssd = sqr;
3333 return var;
3334 }
3335@@ -406,12 +406,14 @@ SAD_X( 8x4 )
3336 SAD_X( 4x8 )
3337 SAD_X( 4x4 )
3338
3339+#if !X264_HIGH_BIT_DEPTH
3340 #if ARCH_UltraSparc
3341 SAD_X( 16x16_vis )
3342 SAD_X( 16x8_vis )
3343 SAD_X( 8x16_vis )
3344 SAD_X( 8x8_vis )
3345 #endif
3346+#endif // !X264_HIGH_BIT_DEPTH
3347
3348 /****************************************************************************
3349 * pixel_satd_x4
3350@@ -444,6 +446,7 @@ SATD_X_DECL6( cpu )\
3351 SATD_X( 4x4, cpu )
3352
3353 SATD_X_DECL7()
3354+#if !X264_HIGH_BIT_DEPTH
3355 #if HAVE_MMX
3356 SATD_X_DECL7( _mmxext )
3357 SATD_X_DECL6( _sse2 )
3358@@ -454,6 +457,7 @@ SATD_X_DECL7( _sse4 )
3359 #if HAVE_ARMV6
3360 SATD_X_DECL7( _neon )
3361 #endif
3362+#endif // !X264_HIGH_BIT_DEPTH
3363
3364 #define INTRA_MBCMP_8x8( mbcmp )\
3365 void x264_intra_##mbcmp##_x3_8x8( pixel *fenc, pixel edge[33], int res[3] )\
3366@@ -520,8 +524,8 @@ static void ssim_4x4x2_core( const pixel *pix1, int stride1,
3367
3368 static float ssim_end1( int s1, int s2, int ss, int s12 )
3369 {
3370- static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
3371- static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
3372+ static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
3373+ static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
3374 int vars = ss*64 - s1*s1 - s2*s2;
3375 int covar = s12*64 - s1*s2;
3376 return (float)(2*s1*s2 + ssim_c1) * (float)(2*covar + ssim_c2)
3377@@ -678,6 +682,7 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
3378 pixf->intra_sad_x3_16x16 = x264_intra_sad_x3_16x16;
3379 pixf->intra_satd_x3_16x16 = x264_intra_satd_x3_16x16;
3380
3381+#if !X264_HIGH_BIT_DEPTH
3382 #if HAVE_MMX
3383 if( cpu&X264_CPU_MMX )
3384 {
3385@@ -903,17 +908,20 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf )
3386 }
3387 }
3388 #endif
3389+#endif // !X264_HIGH_BIT_DEPTH
3390 #if HAVE_ALTIVEC
3391 if( cpu&X264_CPU_ALTIVEC )
3392 {
3393 x264_pixel_altivec_init( pixf );
3394 }
3395 #endif
3396+#if !X264_HIGH_BIT_DEPTH
3397 #if ARCH_UltraSparc
3398 INIT4( sad, _vis );
3399 INIT4( sad_x3, _vis );
3400 INIT4( sad_x4, _vis );
3401 #endif
3402+#endif // !X264_HIGH_BIT_DEPTH
3403
3404 pixf->ads[PIXEL_8x16] =
3405 pixf->ads[PIXEL_8x4] =
3406diff --git a/common/ppc/dct.c b/common/ppc/dct.c
3407index eb223ae..85d5ce7 100644
3408--- a/common/ppc/dct.c
3409+++ b/common/ppc/dct.c
3410@@ -24,6 +24,7 @@
3411 #include "common/common.h"
3412 #include "ppccommon.h"
3413
3414+#if !X264_HIGH_BIT_DEPTH
3415 #define VEC_DCT(a0,a1,a2,a3,b0,b1,b2,b3) \
3416 b1 = vec_add( a0, a3 ); \
3417 b3 = vec_add( a1, a2 ); \
3418@@ -482,4 +483,5 @@ void x264_zigzag_scan_4x4_field_altivec( int16_t level[16], int16_t dct[4][4] )
3419 vec_st( tmp0v, 0x00, level );
3420 vec_st( tmp1v, 0x10, level );
3421 }
3422+#endif // !X264_HIGH_BIT_DEPTH
3423
3424diff --git a/common/ppc/deblock.c b/common/ppc/deblock.c
3425index 0c8d2d4..986710d 100644
3426--- a/common/ppc/deblock.c
3427+++ b/common/ppc/deblock.c
3428@@ -21,6 +21,7 @@
3429 #include "common/common.h"
3430 #include "ppccommon.h"
3431
3432+#if !X264_HIGH_BIT_DEPTH
3433 #define transpose4x16(r0, r1, r2, r3) \
3434 { \
3435 register vec_u8_t r4; \
3436@@ -292,3 +293,4 @@ void x264_deblock_h_luma_altivec( uint8_t *pix, int stride, int alpha, int beta,
3437 transpose4x16(line1, line2, line3, line4);
3438 write16x4(pix-2, stride, line1, line2, line3, line4);
3439 }
3440+#endif // !X264_HIGH_BIT_DEPTH
3441diff --git a/common/ppc/mc.c b/common/ppc/mc.c
3442index 7ad8050..744a804 100644
3443--- a/common/ppc/mc.c
3444+++ b/common/ppc/mc.c
3445@@ -33,6 +33,7 @@
3446 #include "mc.h"
3447 #include "ppccommon.h"
3448
3449+#if !X264_HIGH_BIT_DEPTH
3450 typedef void (*pf_mc_t)( uint8_t *src, int i_src,
3451 uint8_t *dst, int i_dst, int i_height );
3452
3453@@ -792,9 +793,11 @@ static void frame_init_lowres_core_altivec( uint8_t *src0, uint8_t *dst0, uint8_
3454 dstc += dst_stride;
3455 }
3456 }
3457+#endif // !X264_HIGH_BIT_DEPTH
3458
3459 void x264_mc_altivec_init( x264_mc_functions_t *pf )
3460 {
3461+#if !X264_HIGH_BIT_DEPTH
3462 pf->mc_luma = mc_luma_altivec;
3463 pf->get_ref = get_ref_altivec;
3464 pf->mc_chroma = mc_chroma_altivec;
3465@@ -804,4 +807,5 @@ void x264_mc_altivec_init( x264_mc_functions_t *pf )
3466
3467 pf->hpel_filter = x264_hpel_filter_altivec;
3468 pf->frame_init_lowres_core = frame_init_lowres_core_altivec;
3469+#endif // !X264_HIGH_BIT_DEPTH
3470 }
3471diff --git a/common/ppc/pixel.c b/common/ppc/pixel.c
3472index 3f99606..bd5f547 100644
3473--- a/common/ppc/pixel.c
3474+++ b/common/ppc/pixel.c
3475@@ -24,6 +24,7 @@
3476 #include "common/common.h"
3477 #include "ppccommon.h"
3478
3479+#if !X264_HIGH_BIT_DEPTH
3480 /***********************************************************************
3481 * SAD routines
3482 **********************************************************************/
3483@@ -1979,12 +1980,14 @@ static void ssim_4x4x2_core_altivec( const uint8_t *pix1, int stride1,
3484 sums[0][3] = temp[0];
3485 sums[1][3] = temp[1];
3486 }
3487+#endif // !X264_HIGH_BIT_DEPTH
3488
3489 /****************************************************************************
3490 * x264_pixel_init:
3491 ****************************************************************************/
3492 void x264_pixel_altivec_init( x264_pixel_function_t *pixf )
3493 {
3494+#if !X264_HIGH_BIT_DEPTH
3495 pixf->sad[PIXEL_16x16] = pixel_sad_16x16_altivec;
3496 pixf->sad[PIXEL_8x16] = pixel_sad_8x16_altivec;
3497 pixf->sad[PIXEL_16x8] = pixel_sad_16x8_altivec;
3498@@ -2023,4 +2026,5 @@ void x264_pixel_altivec_init( x264_pixel_function_t *pixf )
3499 pixf->hadamard_ac[PIXEL_8x8] = x264_pixel_hadamard_ac_8x8_altivec;
3500
3501 pixf->ssim_4x4x2_core = ssim_4x4x2_core_altivec;
3502+#endif // !X264_HIGH_BIT_DEPTH
3503 }
3504diff --git a/common/ppc/predict.c b/common/ppc/predict.c
3505index 3fb1a2b..c71dbb5 100644
3506--- a/common/ppc/predict.c
3507+++ b/common/ppc/predict.c
3508@@ -23,6 +23,7 @@
3509 #include "pixel.h"
3510 #include "ppccommon.h"
3511
3512+#if !X264_HIGH_BIT_DEPTH
3513 static void predict_8x8c_p_altivec( uint8_t *src )
3514 {
3515 int H = 0, V = 0;
3516@@ -194,6 +195,7 @@ static void predict_16x16_v_altivec( uint8_t *src )
3517 src += FDEC_STRIDE;
3518 }
3519 }
3520+#endif // !X264_HIGH_BIT_DEPTH
3521
3522
3523 /****************************************************************************
3524@@ -201,6 +203,7 @@ static void predict_16x16_v_altivec( uint8_t *src )
3525 ****************************************************************************/
3526 void x264_predict_16x16_init_altivec( x264_predict_t pf[7] )
3527 {
3528+#if !X264_HIGH_BIT_DEPTH
3529 pf[I_PRED_16x16_V ] = predict_16x16_v_altivec;
3530 pf[I_PRED_16x16_H ] = predict_16x16_h_altivec;
3531 pf[I_PRED_16x16_DC] = predict_16x16_dc_altivec;
3532@@ -208,9 +211,12 @@ void x264_predict_16x16_init_altivec( x264_predict_t pf[7] )
3533 pf[I_PRED_16x16_DC_LEFT] = predict_16x16_dc_left_altivec;
3534 pf[I_PRED_16x16_DC_TOP ] = predict_16x16_dc_top_altivec;
3535 pf[I_PRED_16x16_DC_128 ] = predict_16x16_dc_128_altivec;
3536+#endif // !X264_HIGH_BIT_DEPTH
3537 }
3538
3539 void x264_predict_8x8c_init_altivec( x264_predict_t pf[7] )
3540 {
3541+#if !X264_HIGH_BIT_DEPTH
3542 pf[I_PRED_CHROMA_P] = predict_8x8c_p_altivec;
3543+#endif // !X264_HIGH_BIT_DEPTH
3544 }
3545diff --git a/common/ppc/quant.c b/common/ppc/quant.c
3546index 6f41a06..ffd6a1b 100644
3547--- a/common/ppc/quant.c
3548+++ b/common/ppc/quant.c
3549@@ -22,6 +22,7 @@
3550 #include "ppccommon.h"
3551 #include "quant.h"
3552
3553+#if !X264_HIGH_BIT_DEPTH
3554 // quant of a whole 4x4 block, unrolled 2x and "pre-scheduled"
3555 #define QUANT_16_U( idx0, idx1 ) \
3556 { \
3557@@ -360,4 +361,5 @@ void x264_dequant_8x8_altivec( int16_t dct[8][8], int dequant_mf[6][8][8], int i
3558 DEQUANT_SHR();
3559 }
3560 }
3561+#endif // !X264_HIGH_BIT_DEPTH
3562
3563diff --git a/common/predict.c b/common/predict.c
3564index 79ec1fc..dc92083 100644
3565--- a/common/predict.c
3566+++ b/common/predict.c
3567@@ -53,40 +53,40 @@
3568
3569 void x264_predict_16x16_dc_c( pixel *src )
3570 {
3571- pixel4 dc = 0;
3572+ int dc = 0;
3573
3574 for( int i = 0; i < 16; i++ )
3575 {
3576 dc += src[-1 + i * FDEC_STRIDE];
3577 dc += src[i - FDEC_STRIDE];
3578 }
3579- dc = PIXEL_SPLAT_X4( ( dc + 16 ) >> 5 );
3580+ pixel4 dcsplat = PIXEL_SPLAT_X4( ( dc + 16 ) >> 5 );
3581
3582- PREDICT_16x16_DC( dc );
3583+ PREDICT_16x16_DC( dcsplat );
3584 }
3585 static void x264_predict_16x16_dc_left_c( pixel *src )
3586 {
3587- pixel4 dc = 0;
3588+ int dc = 0;
3589
3590 for( int i = 0; i < 16; i++ )
3591 dc += src[-1 + i * FDEC_STRIDE];
3592- dc = PIXEL_SPLAT_X4( ( dc + 8 ) >> 4 );
3593+ pixel4 dcsplat = PIXEL_SPLAT_X4( ( dc + 8 ) >> 4 );
3594
3595- PREDICT_16x16_DC( dc );
3596+ PREDICT_16x16_DC( dcsplat );
3597 }
3598 static void x264_predict_16x16_dc_top_c( pixel *src )
3599 {
3600- pixel4 dc = 0;
3601+ int dc = 0;
3602
3603 for( int i = 0; i < 16; i++ )
3604 dc += src[i - FDEC_STRIDE];
3605- dc = PIXEL_SPLAT_X4( ( dc + 8 ) >> 4 );
3606+ pixel4 dcsplat = PIXEL_SPLAT_X4( ( dc + 8 ) >> 4 );
3607
3608- PREDICT_16x16_DC( dc );
3609+ PREDICT_16x16_DC( dcsplat );
3610 }
3611 static void x264_predict_16x16_dc_128_c( pixel *src )
3612 {
3613- PREDICT_16x16_DC( PIXEL_SPLAT_X4( 0x80 ) );
3614+ PREDICT_16x16_DC( PIXEL_SPLAT_X4( 1 << (BIT_DEPTH-1) ) );
3615 }
3616 void x264_predict_16x16_h_c( pixel *src )
3617 {
3618@@ -155,53 +155,53 @@ static void x264_predict_8x8c_dc_128_c( pixel *src )
3619 {
3620 for( int y = 0; y < 8; y++ )
3621 {
3622- MPIXEL_X4( src+0 ) = PIXEL_SPLAT_X4( 0x80 );
3623- MPIXEL_X4( src+4 ) = PIXEL_SPLAT_X4( 0x80 );
3624+ MPIXEL_X4( src+0 ) = PIXEL_SPLAT_X4( 1 << (BIT_DEPTH-1) );
3625+ MPIXEL_X4( src+4 ) = PIXEL_SPLAT_X4( 1 << (BIT_DEPTH-1) );
3626 src += FDEC_STRIDE;
3627 }
3628 }
3629 static void x264_predict_8x8c_dc_left_c( pixel *src )
3630 {
3631- pixel4 dc0 = 0, dc1 = 0;
3632+ int dc0 = 0, dc1 = 0;
3633
3634 for( int y = 0; y < 4; y++ )
3635 {
3636 dc0 += src[y * FDEC_STRIDE - 1];
3637 dc1 += src[(y+4) * FDEC_STRIDE - 1];
3638 }
3639- dc0 = PIXEL_SPLAT_X4( ( dc0 + 2 ) >> 2 );
3640- dc1 = PIXEL_SPLAT_X4( ( dc1 + 2 ) >> 2 );
3641+ pixel4 dc0splat = PIXEL_SPLAT_X4( ( dc0 + 2 ) >> 2 );
3642+ pixel4 dc1splat = PIXEL_SPLAT_X4( ( dc1 + 2 ) >> 2 );
3643
3644 for( int y = 0; y < 4; y++ )
3645 {
3646- MPIXEL_X4( src+0 ) = dc0;
3647- MPIXEL_X4( src+4 ) = dc0;
3648+ MPIXEL_X4( src+0 ) = dc0splat;
3649+ MPIXEL_X4( src+4 ) = dc0splat;
3650 src += FDEC_STRIDE;
3651 }
3652 for( int y = 0; y < 4; y++ )
3653 {
3654- MPIXEL_X4( src+0 ) = dc1;
3655- MPIXEL_X4( src+4 ) = dc1;
3656+ MPIXEL_X4( src+0 ) = dc1splat;
3657+ MPIXEL_X4( src+4 ) = dc1splat;
3658 src += FDEC_STRIDE;
3659 }
3660
3661 }
3662 static void x264_predict_8x8c_dc_top_c( pixel *src )
3663 {
3664- pixel4 dc0 = 0, dc1 = 0;
3665+ int dc0 = 0, dc1 = 0;
3666
3667 for( int x = 0; x < 4; x++ )
3668 {
3669 dc0 += src[x - FDEC_STRIDE];
3670 dc1 += src[x + 4 - FDEC_STRIDE];
3671 }
3672- dc0 = PIXEL_SPLAT_X4( ( dc0 + 2 ) >> 2 );
3673- dc1 = PIXEL_SPLAT_X4( ( dc1 + 2 ) >> 2 );
3674+ pixel4 dc0splat = PIXEL_SPLAT_X4( ( dc0 + 2 ) >> 2 );
3675+ pixel4 dc1splat = PIXEL_SPLAT_X4( ( dc1 + 2 ) >> 2 );
3676
3677 for( int y = 0; y < 8; y++ )
3678 {
3679- MPIXEL_X4( src+0 ) = dc0;
3680- MPIXEL_X4( src+4 ) = dc1;
3681+ MPIXEL_X4( src+0 ) = dc0splat;
3682+ MPIXEL_X4( src+4 ) = dc1splat;
3683 src += FDEC_STRIDE;
3684 }
3685 }
3686@@ -306,7 +306,7 @@ static void x264_predict_8x8c_p_c( pixel *src )
3687
3688 static void x264_predict_4x4_dc_128_c( pixel *src )
3689 {
3690- PREDICT_4x4_DC( PIXEL_SPLAT_X4( 0x80 ) );
3691+ PREDICT_4x4_DC( PIXEL_SPLAT_X4( 1 << (BIT_DEPTH-1) ) );
3692 }
3693 static void x264_predict_4x4_dc_left_c( pixel *src )
3694 {
3695@@ -491,7 +491,8 @@ static void x264_predict_8x8_filter_c( pixel *src, pixel edge[33], int i_neighbo
3696 }
3697 else
3698 {
3699- M64( edge+24 ) = SRC(7,-1) * 0x0101010101010101ULL;
3700+ MPIXEL_X4( edge+24 ) = PIXEL_SPLAT_X4( SRC(7,-1) );
3701+ MPIXEL_X4( edge+28 ) = PIXEL_SPLAT_X4( SRC(7,-1) );
3702 edge[32] = SRC(7,-1);
3703 }
3704 }
3705@@ -523,7 +524,7 @@ static void x264_predict_8x8_filter_c( pixel *src, pixel edge[33], int i_neighbo
3706
3707 static void x264_predict_8x8_dc_128_c( pixel *src, pixel edge[33] )
3708 {
3709- PREDICT_8x8_DC( PIXEL_SPLAT_X4( 0x80 ) );
3710+ PREDICT_8x8_DC( PIXEL_SPLAT_X4( 1 << (BIT_DEPTH-1) ) );
3711 }
3712 static void x264_predict_8x8_dc_left_c( pixel *src, pixel edge[33] )
3713 {
3714@@ -554,9 +555,13 @@ void x264_predict_8x8_h_c( pixel *src, pixel edge[33] )
3715 }
3716 void x264_predict_8x8_v_c( pixel *src, pixel edge[33] )
3717 {
3718- uint64_t top = M64( edge+16 );
3719+ pixel4 top[2] = { MPIXEL_X4( edge+16 ),
3720+ MPIXEL_X4( edge+20 ) };
3721 for( int y = 0; y < 8; y++ )
3722- M64( src+y*FDEC_STRIDE ) = top;
3723+ {
3724+ MPIXEL_X4( src+y*FDEC_STRIDE+0 ) = top[0];
3725+ MPIXEL_X4( src+y*FDEC_STRIDE+4 ) = top[1];
3726+ }
3727 }
3728 static void x264_predict_8x8_ddl_c( pixel *src, pixel edge[33] )
3729 {
3730diff --git a/common/quant.c b/common/quant.c
3731index ece52f9..a7b72cf 100644
3732--- a/common/quant.c
3733+++ b/common/quant.c
3734@@ -142,7 +142,7 @@ static void x264_denoise_dct( dctcoef *dct, uint32_t *sum, uint16_t *offset, int
3735 for( int i = 1; i < size; i++ )
3736 {
3737 int level = dct[i];
3738- int sign = level>>15;
3739+ int sign = level>>31;
3740 level = (level+sign)^sign;
3741 sum[i] += level;
3742 level -= offset[i];
3743@@ -177,10 +177,7 @@ static int ALWAYS_INLINE x264_decimate_score_internal( dctcoef *dct, int i_max )
3744 int i_score = 0;
3745 int idx = i_max - 1;
3746
3747- /* Yes, dct[idx-1] is guaranteed to be 32-bit aligned. idx>=0 instead of 1 works correctly for the same reason */
3748- while( idx >= 0 && MDCT_X2( &dct[idx-1] ) == 0 )
3749- idx -= 2;
3750- if( idx >= 0 && dct[idx] == 0 )
3751+ while( idx >= 0 && dct[idx] == 0 )
3752 idx--;
3753 while( idx >= 0 )
3754 {
3755@@ -216,10 +213,7 @@ static int x264_decimate_score64( dctcoef *dct )
3756
3757 static int ALWAYS_INLINE x264_coeff_last_internal( dctcoef *l, int i_count )
3758 {
3759- int i_last;
3760- for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
3761- if( M64( l+i_last-3 ) )
3762- break;
3763+ int i_last = i_count-1;
3764 while( i_last >= 0 && l[i_last] == 0 )
3765 i_last--;
3766 return i_last;
3767@@ -287,6 +281,7 @@ void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
3768 pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15;
3769 pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16;
3770
3771+#if !X264_HIGH_BIT_DEPTH
3772 #if HAVE_MMX
3773 if( cpu&X264_CPU_MMX )
3774 {
3775@@ -425,6 +420,7 @@ void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf )
3776 pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon;
3777 }
3778 #endif
3779+#endif // !X264_HIGH_BIT_DEPTH
3780 pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4];
3781 pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC];
3782 pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4];
3783diff --git a/common/set.c b/common/set.c
3784index 16cff8e..86f3854 100644
3785--- a/common/set.c
3786+++ b/common/set.c
3787@@ -78,6 +78,7 @@ int x264_cqm_init( x264_t *h )
3788 32 - 11, 32 - 21 };
3789 int max_qp_err = -1;
3790 int max_chroma_qp_err = -1;
3791+ int min_qp_err = QP_MAX+1;
3792
3793 for( int i = 0; i < 6; i++ )
3794 {
3795@@ -94,9 +95,9 @@ int x264_cqm_init( x264_t *h )
3796 }
3797 else
3798 {
3799- CHECKED_MALLOC( h-> quant4_mf[i], 52*size*sizeof(uint16_t) );
3800+ CHECKED_MALLOC( h-> quant4_mf[i], (QP_MAX+1)*size*sizeof(uint16_t) );
3801 CHECKED_MALLOC( h->dequant4_mf[i], 6*size*sizeof(int) );
3802- CHECKED_MALLOC( h->unquant4_mf[i], 52*size*sizeof(int) );
3803+ CHECKED_MALLOC( h->unquant4_mf[i], (QP_MAX+1)*size*sizeof(int) );
3804 }
3805
3806 for( j = (i<4 ? 0 : 4); j < i; j++ )
3807@@ -106,7 +107,7 @@ int x264_cqm_init( x264_t *h )
3808 if( j < i )
3809 h->quant4_bias[i] = h->quant4_bias[j];
3810 else
3811- CHECKED_MALLOC( h->quant4_bias[i], 52*size*sizeof(uint16_t) );
3812+ CHECKED_MALLOC( h->quant4_bias[i], (QP_MAX+1)*size*sizeof(uint16_t) );
3813 }
3814
3815 for( int q = 0; q < 6; q++ )
3816@@ -140,7 +141,7 @@ int x264_cqm_init( x264_t *h )
3817 quant8_mf[i_list][q][i] = DIV(def_quant8[q][i] * 16, h->pps->scaling_list[4+i_list][i]);
3818 }
3819 }
3820- for( int q = 0; q < 52; q++ )
3821+ for( int q = 0; q < QP_MAX+1; q++ )
3822 {
3823 int j;
3824 for( int i_list = 0; i_list < 4; i_list++ )
3825@@ -148,6 +149,11 @@ int x264_cqm_init( x264_t *h )
3826 {
3827 h->unquant4_mf[i_list][q][i] = (1ULL << (q/6 + 15 + 8)) / quant4_mf[i_list][q%6][i];
3828 h->quant4_mf[i_list][q][i] = j = SHIFT(quant4_mf[i_list][q%6][i], q/6 - 1);
3829+ if( !j )
3830+ {
3831+ min_qp_err = X264_MIN( min_qp_err, q );
3832+ continue;
3833+ }
3834 // round to nearest, unless that would cause the deadzone to be negative
3835 h->quant4_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
3836 if( j > 0xffff && q > max_qp_err && (i_list == CQM_4IY || i_list == CQM_4PY) )
3837@@ -161,6 +167,11 @@ int x264_cqm_init( x264_t *h )
3838 {
3839 h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][i];
3840 h->quant8_mf[i_list][q][i] = j = SHIFT(quant8_mf[i_list][q%6][i], q/6);
3841+ if( !j )
3842+ {
3843+ min_qp_err = X264_MIN( min_qp_err, q );
3844+ continue;
3845+ }
3846 h->quant8_bias[i_list][q][i] = X264_MIN( DIV(deadzone[i_list]<<10, j), (1<<15)/j );
3847 if( j > 0xffff && q > max_qp_err )
3848 max_qp_err = q;
3849@@ -179,6 +190,12 @@ int x264_cqm_init( x264_t *h )
3850 x264_log( h, X264_LOG_ERROR, "but min chroma QP is implied to be %d.\n", h->chroma_qp_table[h->param.rc.i_qp_min] );
3851 return -1;
3852 }
3853+ if( !h->mb.b_lossless && min_qp_err <= h->param.rc.i_qp_max )
3854+ {
3855+ x264_log( h, X264_LOG_ERROR, "Quantization underflow. Your CQM is incompatible with QP > %d,\n", min_qp_err-1 );
3856+ x264_log( h, X264_LOG_ERROR, "but max QP is implied to be %d.\n", h->param.rc.i_qp_max );
3857+ return -1;
3858+ }
3859 return 0;
3860 fail:
3861 x264_cqm_delete( h );
3862diff --git a/common/x86/mc-c.c b/common/x86/mc-c.c
3863index 2dcd671..4bb5f33 100644
3864--- a/common/x86/mc-c.c
3865+++ b/common/x86/mc-c.c
3866@@ -125,6 +125,7 @@ PIXEL_AVG_WALL(sse2)
3867 PIXEL_AVG_WALL(sse2_misalign)
3868 PIXEL_AVG_WALL(cache64_ssse3)
3869
3870+#if !X264_HIGH_BIT_DEPTH
3871 #define PIXEL_AVG_WTAB(instr, name1, name2, name3, name4, name5)\
3872 static void (* const x264_pixel_avg_wtab_##instr[6])( uint8_t *, int, uint8_t *, int, uint8_t *, int ) =\
3873 {\
3874@@ -355,24 +356,28 @@ static void x264_plane_copy_mmxext( uint8_t *dst, int i_dst, uint8_t *src, int i
3875 x264_plane_copy_core_mmxext( dst+i_dst, i_dst, src+i_src, i_src, (w+15)&~15, h-1 );
3876 }
3877 }
3878+#endif // !X264_HIGH_BIT_DEPTH
3879
3880 void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
3881 {
3882 if( !(cpu&X264_CPU_MMX) )
3883 return;
3884
3885+ pf->memcpy_aligned = x264_memcpy_aligned_mmx;
3886+ pf->memzero_aligned = x264_memzero_aligned_mmx;
3887+#if !X264_HIGH_BIT_DEPTH
3888 pf->copy_16x16_unaligned = x264_mc_copy_w16_mmx;
3889 pf->copy[PIXEL_16x16] = x264_mc_copy_w16_mmx;
3890 pf->copy[PIXEL_8x8] = x264_mc_copy_w8_mmx;
3891 pf->copy[PIXEL_4x4] = x264_mc_copy_w4_mmx;
3892- pf->memcpy_aligned = x264_memcpy_aligned_mmx;
3893- pf->memzero_aligned = x264_memzero_aligned_mmx;
3894 pf->integral_init4v = x264_integral_init4v_mmx;
3895 pf->integral_init8v = x264_integral_init8v_mmx;
3896+#endif // !X264_HIGH_BIT_DEPTH
3897
3898 if( !(cpu&X264_CPU_MMXEXT) )
3899 return;
3900
3901+#if !X264_HIGH_BIT_DEPTH
3902 pf->mc_luma = mc_luma_mmxext;
3903 pf->get_ref = get_ref_mmxext;
3904 pf->mc_chroma = x264_mc_chroma_mmxext;
3905@@ -412,12 +417,14 @@ void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
3906 pf->frame_init_lowres_core = x264_frame_init_lowres_core_cache32_mmxext;
3907 }
3908 #endif
3909+#endif // !X264_HIGH_BIT_DEPTH
3910
3911 if( !(cpu&X264_CPU_SSE2) )
3912 return;
3913
3914 pf->memcpy_aligned = x264_memcpy_aligned_sse2;
3915 pf->memzero_aligned = x264_memzero_aligned_sse2;
3916+#if !X264_HIGH_BIT_DEPTH
3917 pf->integral_init4v = x264_integral_init4v_sse2;
3918 pf->integral_init8v = x264_integral_init8v_sse2;
3919 pf->hpel_filter = x264_hpel_filter_sse2_amd;
3920@@ -492,4 +499,5 @@ void x264_mc_init_mmx( int cpu, x264_mc_functions_t *pf )
3921
3922 pf->integral_init4h = x264_integral_init4h_sse4;
3923 pf->integral_init8h = x264_integral_init8h_sse4;
3924+#endif // !X264_HIGH_BIT_DEPTH
3925 }
3926diff --git a/common/x86/predict-c.c b/common/x86/predict-c.c
3927index e771431..4004265 100644
3928--- a/common/x86/predict-c.c
3929+++ b/common/x86/predict-c.c
3930@@ -75,6 +75,7 @@
3931 void x264_predict_16x16_v_sse2( uint8_t *src );
3932 void x264_predict_16x16_p_core_sse2( uint8_t *src, int i00, int b, int c );
3933
3934+#if !X264_HIGH_BIT_DEPTH
3935 ALIGNED_8( static const int8_t pb_12345678[8] ) = {1,2,3,4,5,6,7,8};
3936 ALIGNED_8( static const int8_t pb_m87654321[8] ) = {-8,-7,-6,-5,-4,-3,-2,-1};
3937 ALIGNED_8( static const int8_t pb_m32101234[8] ) = {-3,-2,-1,0,1,2,3,4};
3938@@ -364,6 +365,7 @@ INTRA_SA8D_X3(ssse3)
3939 #else
3940 INTRA_SA8D_X3(mmxext)
3941 #endif
3942+#endif // !X264_HIGH_BIT_DEPTH
3943
3944 /****************************************************************************
3945 * Exported functions:
3946@@ -372,6 +374,7 @@ void x264_predict_16x16_init_mmx( int cpu, x264_predict_t pf[7] )
3947 {
3948 if( !(cpu&X264_CPU_MMX) )
3949 return;
3950+#if !X264_HIGH_BIT_DEPTH
3951 pf[I_PRED_16x16_V] = x264_predict_16x16_v_mmx;
3952 if( !(cpu&X264_CPU_MMXEXT) )
3953 return;
3954@@ -397,12 +400,14 @@ void x264_predict_16x16_init_mmx( int cpu, x264_predict_t pf[7] )
3955 #ifdef __GNUC__
3956 pf[I_PRED_16x16_P] = x264_predict_16x16_p_ssse3;
3957 #endif
3958+#endif // !X264_HIGH_BIT_DEPTH
3959 }
3960
3961 void x264_predict_8x8c_init_mmx( int cpu, x264_predict_t pf[7] )
3962 {
3963 if( !(cpu&X264_CPU_MMX) )
3964 return;
3965+#if !X264_HIGH_BIT_DEPTH
3966 #if ARCH_X86_64
3967 pf[I_PRED_CHROMA_DC_LEFT] = x264_predict_8x8c_dc_left;
3968 #endif
3969@@ -424,12 +429,14 @@ void x264_predict_8x8c_init_mmx( int cpu, x264_predict_t pf[7] )
3970 #ifdef __GNUC__
3971 pf[I_PRED_CHROMA_P] = x264_predict_8x8c_p_ssse3;
3972 #endif
3973+#endif // !X264_HIGH_BIT_DEPTH
3974 }
3975
3976 void x264_predict_8x8_init_mmx( int cpu, x264_predict8x8_t pf[12], x264_predict_8x8_filter_t *predict_8x8_filter )
3977 {
3978 if( !(cpu&X264_CPU_MMXEXT) )
3979 return;
3980+#if !X264_HIGH_BIT_DEPTH
3981 pf[I_PRED_8x8_V] = x264_predict_8x8_v_mmxext;
3982 pf[I_PRED_8x8_H] = x264_predict_8x8_h_mmxext;
3983 pf[I_PRED_8x8_DC] = x264_predict_8x8_dc_mmxext;
3984@@ -456,12 +463,14 @@ void x264_predict_8x8_init_mmx( int cpu, x264_predict8x8_t pf[12], x264_predict_
3985 pf[I_PRED_8x8_HD] = x264_predict_8x8_hd_ssse3;
3986 pf[I_PRED_8x8_HU] = x264_predict_8x8_hu_ssse3;
3987 *predict_8x8_filter = x264_predict_8x8_filter_ssse3;
3988+#endif // !X264_HIGH_BIT_DEPTH
3989 }
3990
3991 void x264_predict_4x4_init_mmx( int cpu, x264_predict_t pf[12] )
3992 {
3993 if( !(cpu&X264_CPU_MMXEXT) )
3994 return;
3995+#if !X264_HIGH_BIT_DEPTH
3996 pf[I_PRED_4x4_VR] = x264_predict_4x4_vr_mmxext;
3997 pf[I_PRED_4x4_DDL] = x264_predict_4x4_ddl_mmxext;
3998 pf[I_PRED_4x4_VL] = x264_predict_4x4_vl_mmxext;
3999@@ -474,4 +483,5 @@ void x264_predict_4x4_init_mmx( int cpu, x264_predict_t pf[12] )
4000 pf[I_PRED_4x4_DDR] = x264_predict_4x4_ddr_ssse3;
4001 pf[I_PRED_4x4_VR] = x264_predict_4x4_vr_ssse3;
4002 pf[I_PRED_4x4_HD] = x264_predict_4x4_hd_ssse3;
4003+#endif // !X264_HIGH_BIT_DEPTH
4004 }
4005diff --git a/configure b/configure
4006index 24d15ad..5cdd82d 100755
4007--- a/configure
4008+++ b/configure
4009@@ -18,6 +18,7 @@ echo " --enable-gprof adds -pg, doesn't strip"
4010 echo " --enable-visualize enables visualization (X11 only)"
4011 echo " --enable-pic build position-independent code"
4012 echo " --enable-shared build libx264.so"
4013+echo " --bit-depth=BIT_DEPTH sets output bit depth (8-10), default 8"
4014 echo " --extra-asflags=EASFLAGS add EASFLAGS to ASFLAGS"
4015 echo " --extra-cflags=ECFLAGS add ECFLAGS to CFLAGS"
4016 echo " --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS"
4017@@ -124,6 +125,7 @@ gprof="no"
4018 pic="no"
4019 vis="no"
4020 shared="no"
4021+bit_depth="8"
4022
4023 CFLAGS="$CFLAGS -Wall -I."
4024 LDFLAGS="$LDFLAGS"
4025@@ -208,6 +210,13 @@ for opt do
4026 CFLAGS="$CFLAGS --sysroot=${opt#--sysroot=}"
4027 LDFLAGS="$LDFLAGS --sysroot=${opt#--sysroot=}"
4028 ;;
4029+ --bit-depth=*)
4030+ bit_depth="${opt#--bit-depth=}"
4031+ if [ "$bit_depth" -lt "8" ] || [ "$bit_depth" -gt "10" ]; then
4032+ echo "Supplied bit depth must be in range [8,10]."
4033+ exit 1
4034+ fi
4035+ ;;
4036 *)
4037 echo "Unknown option $opt, ignored"
4038 ;;
4039@@ -644,6 +653,12 @@ if cc_check '' -Wshadow ; then
4040 CFLAGS="-Wshadow $CFLAGS"
4041 fi
4042
4043+if [ "$bit_depth" -gt "8" ]; then
4044+ define X264_HIGH_BIT_DEPTH
4045+fi
4046+
4047+define BIT_DEPTH $bit_depth
4048+
4049 rm -f conftest*
4050
4051 # generate config files
4052@@ -724,6 +739,7 @@ gprof: $gprof
4053 PIC: $pic
4054 shared: $shared
4055 visualize: $vis
4056+bit depth: $bit_depth
4057 EOF
4058
4059 echo >> config.log
4060diff --git a/encoder/analyse.c b/encoder/analyse.c
4061index eea464a..c54d95d 100644
4062--- a/encoder/analyse.c
4063+++ b/encoder/analyse.c
4064@@ -134,25 +134,27 @@ typedef struct
4065 } x264_mb_analysis_t;
4066
4067 /* lambda = pow(2,qp/6-2) */
4068-const uint8_t x264_lambda_tab[52] = {
4069- 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
4070- 1, 1, 1, 1, /* 8-11 */
4071- 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
4072- 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
4073- 6, 7, 8, 9,10,11,13,14, /* 28-35 */
4074- 16,18,20,23,25,29,32,36, /* 36-43 */
4075- 40,45,51,57,64,72,81,91 /* 44-51 */
4076+const uint16_t x264_lambda_tab[QP_MAX_MAX+1] = {
4077+ 1, 1, 1, 1, 1, 1, 1, 1, /* 0- 7 */
4078+ 1, 1, 1, 1, 1, 1, 1, 1, /* 8-15 */
4079+ 2, 2, 2, 2, 3, 3, 3, 4, /* 16-23 */
4080+ 4, 4, 5, 6, 6, 7, 8, 9, /* 24-31 */
4081+ 10, 11, 13, 14, 16, 18, 20, 23, /* 32-39 */
4082+ 25, 29, 32, 36, 40, 45, 51, 57, /* 40-47 */
4083+ 64, 72, 81, 91, 102, 114, 128, 144, /* 48-55 */
4084+ 161, 181, 203, 228, 256, 287, 323, 362, /* 56-63 */
4085 };
4086
4087 /* lambda2 = pow(lambda,2) * .9 * 256 */
4088-const int x264_lambda2_tab[52] = {
4089- 14, 18, 22, 28, 36, 45, 57, 72, /* 0 - 7 */
4090- 91, 115, 145, 182, 230, 290, 365, 460, /* 8 - 15 */
4091- 580, 731, 921, 1161, 1462, 1843, 2322, 2925, /* 16 - 23 */
4092- 3686, 4644, 5851, 7372, 9289, 11703, 14745, 18578, /* 24 - 31 */
4093- 23407, 29491, 37156, 46814, 58982, 74313, 93628, 117964, /* 32 - 39 */
4094-148626, 187257, 235929, 297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
4095-943718, 1189010, 1498059, 1887436 /* 48 - 51 */
4096+const int x264_lambda2_tab[QP_MAX_MAX+1] = {
4097+ 14, 18, 22, 28, 36, 45, 57, 72, /* 0- 7 */
4098+ 91, 115, 145, 182, 230, 290, 365, 460, /* 8-15 */
4099+ 580, 731, 921, 1161, 1462, 1843, 2322, 2925, /* 16-23 */
4100+ 3686, 4644, 5851, 7372, 9289, 11703, 14745, 18578, /* 24-31 */
4101+ 23407, 29491, 37156, 46814, 58982, 74313, 93628, 117964, /* 32-39 */
4102+ 148626, 187257, 235929, 297252, 374514, 471859, 594505, 749029, /* 40-47 */
4103+ 943718,1189010,1498059, 1887436, 2378021, 2996119, 3774873, 4756042, /* 48-55 */
4104+5992238,7549747,9512085,11984476,15099494,19024170,23968953,30198988, /* 56-63 */
4105 };
4106
4107 const uint8_t x264_exp2_lut[64] = {
4108@@ -188,27 +190,31 @@ const float x264_log2_lz_lut[32] = {
4109
4110 // should the intra and inter lambdas be different?
4111 // I'm just matching the behaviour of deadzone quant.
4112-static const int x264_trellis_lambda2_tab[2][52] = {
4113+static const int x264_trellis_lambda2_tab[2][QP_MAX_MAX+1] = {
4114 // inter lambda = .85 * .85 * 2**(qp/3. + 10 - LAMBDA_BITS)
4115- { 46, 58, 73, 92, 117, 147,
4116- 185, 233, 294, 370, 466, 587,
4117- 740, 932, 1174, 1480, 1864, 2349,
4118- 2959, 3728, 4697, 5918, 7457, 9395,
4119- 11837, 14914, 18790, 23674, 29828, 37581,
4120- 47349, 59656, 75163, 94699, 119313, 150326,
4121- 189399, 238627, 300652, 378798, 477255, 601304,
4122- 757596, 954511, 1202608, 1515192, 1909022, 2405217,
4123- 3030384, 3818045, 4810435, 6060769 },
4124+ { 46, 58, 73, 92, 117, 147,
4125+ 185, 233, 294, 370, 466, 587,
4126+ 740, 932, 1174, 1480, 1864, 2349,
4127+ 2959, 3728, 4697, 5918, 7457, 9395,
4128+ 11837, 14914, 18790, 23674, 29828, 37581,
4129+ 47349, 59656, 75163, 94699, 119313, 150326,
4130+ 189399, 238627, 300652, 378798, 477255, 601304,
4131+ 757596, 954511, 1202608, 1515192, 1909022, 2405217,
4132+ 3030384, 3818045, 4810435, 6060769, 7636091, 9620872,
4133+ 12121539,15272182,19241743,24243077,30544363,38483486,
4134+ 48486154,61088726,76966972,96972308 },
4135 // intra lambda = .65 * .65 * 2**(qp/3. + 10 - LAMBDA_BITS)
4136- { 27, 34, 43, 54, 68, 86,
4137- 108, 136, 172, 216, 273, 343,
4138- 433, 545, 687, 865, 1090, 1374,
4139- 1731, 2180, 2747, 3461, 4361, 5494,
4140- 6922, 8721, 10988, 13844, 17442, 21976,
4141- 27688, 34885, 43953, 55377, 69771, 87906,
4142- 110755, 139543, 175813, 221511, 279087, 351627,
4143- 443023, 558174, 703255, 886046, 1116348, 1406511,
4144- 1772093, 2232697, 2813022, 3544186 }
4145+ { 27, 34, 43, 54, 68, 86,
4146+ 108, 136, 172, 216, 273, 343,
4147+ 433, 545, 687, 865, 1090, 1374,
4148+ 1731, 2180, 2747, 3461, 4361, 5494,
4149+ 6922, 8721, 10988, 13844, 17442, 21976,
4150+ 27688, 34885, 43953, 55377, 69771, 87906,
4151+ 110755, 139543, 175813, 221511, 279087, 351627,
4152+ 443023, 558174, 703255, 886046, 1116348, 1406511,
4153+ 1772093, 2232697, 2813022, 3544186, 4465396, 5626046,
4154+ 7088374, 8930791,11252092,14176748,17861583,22504184,
4155+ 28353495,35723165,45008368,56706990 }
4156 };
4157
4158 static const uint16_t x264_chroma_lambda2_offset_tab[] = {
4159@@ -237,7 +243,7 @@ static const uint8_t i_sub_mb_p_cost_table[4] = {
4160
4161 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
4162
4163-static uint16_t x264_cost_ref[92][3][33];
4164+static uint16_t x264_cost_ref[LAMBDA_MAX+1][3][33];
4165 static UNUSED x264_pthread_mutex_t cost_ref_mutex = X264_PTHREAD_MUTEX_INITIALIZER;
4166
4167 int x264_analyse_init_costs( x264_t *h, int qp )
4168@@ -275,7 +281,7 @@ fail:
4169
4170 void x264_analyse_free_costs( x264_t *h )
4171 {
4172- for( int i = 0; i < 92; i++ )
4173+ for( int i = 0; i < LAMBDA_MAX+1; i++ )
4174 {
4175 if( h->cost_mv[i] )
4176 x264_free( h->cost_mv[i] - 2*4*2048 );
4177diff --git a/encoder/cabac.c b/encoder/cabac.c
4178index 8bd40f1..e82d7e9 100644
4179--- a/encoder/cabac.c
4180+++ b/encoder/cabac.c
4181@@ -262,9 +262,9 @@ static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
4182 if( i_dqp != 0 )
4183 {
4184 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
4185- /* dqp is interpreted modulo 52 */
4186- if( val >= 51 && val != 52 )
4187- val = 103 - val;
4188+ /* dqp is interpreted modulo (QP_MAX+1) */
4189+ if( val >= QP_MAX && val != QP_MAX+1 )
4190+ val = 2*QP_MAX+1 - val;
4191 do
4192 {
4193 x264_cabac_encode_decision( cb, 60 + ctx, 1 );
4194@@ -767,15 +767,18 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
4195 i_mb_pos_tex = x264_cabac_pos( cb );
4196 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
4197
4198- memcpy( cb->p, h->mb.pic.p_fenc[0], 256 );
4199- cb->p += 256;
4200- for( int i = 0; i < 8; i++ )
4201- memcpy( cb->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
4202- cb->p += 64;
4203- for( int i = 0; i < 8; i++ )
4204- memcpy( cb->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
4205- cb->p += 64;
4206+ bs_t s;
4207+ bs_init( &s, cb->p, cb->p_end - cb->p );
4208
4209+ for( int i = 0; i < 256; i++ )
4210+ bs_write( &s, BIT_DEPTH, h->mb.pic.p_fenc[0][i] );
4211+ for( int ch = 0; ch < 2; ch++ )
4212+ for( int i = 0; i < 8; i++ )
4213+ for( int j = 0; j < 8; j++ )
4214+ bs_write( &s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
4215+
4216+ bs_flush( &s );
4217+ cb->p = s.p;
4218 x264_cabac_encode_init_core( cb );
4219
4220 h->stat.frame.i_tex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
4221diff --git a/encoder/cavlc.c b/encoder/cavlc.c
4222index e2f60b1..632ed41 100644
4223--- a/encoder/cavlc.c
4224+++ b/encoder/cavlc.c
4225@@ -66,7 +66,7 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, int i_suffix_len
4226 bs_t *s = &h->out.bs;
4227 static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
4228 int i_level_prefix = 15;
4229- int mask = level >> 15;
4230+ int mask = level >> 31;
4231 int abs_level = (level^mask)-mask;
4232 int i_level_code = abs_level*2-mask-2;
4233 if( ( i_level_code >> i_suffix_length ) < 15 )
4234@@ -219,10 +219,10 @@ static void cavlc_qp_delta( x264_t *h )
4235
4236 if( i_dqp )
4237 {
4238- if( i_dqp < -26 )
4239- i_dqp += 52;
4240- else if( i_dqp > 25 )
4241- i_dqp -= 52;
4242+ if( i_dqp < -(QP_MAX+1)/2 )
4243+ i_dqp += QP_MAX+1;
4244+ else if( i_dqp > QP_MAX/2 )
4245+ i_dqp -= QP_MAX+1;
4246 }
4247 bs_write_se( s, i_dqp );
4248 }
4249@@ -309,14 +309,12 @@ void x264_macroblock_write_cavlc( x264_t *h )
4250
4251 bs_align_0( s );
4252
4253- memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
4254- s->p += 256;
4255- for( int i = 0; i < 8; i++ )
4256- memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
4257- s->p += 64;
4258- for( int i = 0; i < 8; i++ )
4259- memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
4260- s->p += 64;
4261+ for( int i = 0; i < 256; i++ )
4262+ bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[0][i] );
4263+ for( int ch = 0; ch < 2; ch++ )
4264+ for( int i = 0; i < 8; i++ )
4265+ for( int j = 0; j < 8; j++ )
4266+ bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
4267
4268 bs_init( s, s->p, s->p_end - s->p );
4269 s->p_start = p_start;
4270diff --git a/encoder/encoder.c b/encoder/encoder.c
4271index 31cb84a..f7e0e38 100644
4272--- a/encoder/encoder.c
4273+++ b/encoder/encoder.c
4274@@ -51,7 +51,7 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
4275 ****************************************************************************/
4276 static float x264_psnr( int64_t i_sqe, int64_t i_size )
4277 {
4278- double f_mse = (double)i_sqe / ((double)65025.0 * (double)i_size);
4279+ double f_mse = (double)i_sqe / (PIXEL_MAX*PIXEL_MAX * (double)i_size);
4280 if( f_mse <= 0.0000000001 ) /* Max 100dB */
4281 return 100;
4282
4283@@ -68,11 +68,13 @@ static void x264_frame_dump( x264_t *h )
4284 FILE *f = fopen( h->param.psz_dump_yuv, "r+b" );
4285 if( !f )
4286 return;
4287+ int bytes_per_pixel = (BIT_DEPTH+7)/8;
4288 /* Write the frame in display order */
4289- fseek( f, (uint64_t)h->fdec->i_frame * h->param.i_height * h->param.i_width * 3/2, SEEK_SET );
4290+ fseek( f, (uint64_t)h->fdec->i_frame * h->param.i_height * h->param.i_width * 3/2 * bytes_per_pixel, SEEK_SET );
4291 for( int i = 0; i < h->fdec->i_plane; i++ )
4292 for( int y = 0; y < h->param.i_height >> !!i; y++ )
4293- fwrite( &h->fdec->plane[i][y*h->fdec->i_stride[i]], 1, h->param.i_width >> !!i, f );
4294+ for( int j = 0; j < h->param.i_width >> !!i; j++ )
4295+ fwrite( &h->fdec->plane[i][y*h->fdec->i_stride[i]]+j, bytes_per_pixel, 1, f );
4296 fclose( f );
4297 }
4298
4299@@ -469,8 +471,8 @@ static int x264_validate_parameters( x264_t *h )
4300 x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" );
4301 return -1;
4302 }
4303- h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, 0, 51 );
4304- h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
4305+ h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, 0, QP_MAX );
4306+ h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, QP_MAX );
4307 if( h->param.rc.i_rc_method == X264_RC_CRF )
4308 {
4309 h->param.rc.i_qp_constant = h->param.rc.f_rf_constant;
4310@@ -502,12 +504,12 @@ static int x264_validate_parameters( x264_t *h )
4311 float qp_p = h->param.rc.i_qp_constant;
4312 float qp_i = qp_p - 6*log2f( h->param.rc.f_ip_factor );
4313 float qp_b = qp_p + 6*log2f( h->param.rc.f_pb_factor );
4314- h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, 51 );
4315- h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, 51 );
4316+ h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, QP_MAX );
4317+ h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, QP_MAX );
4318 h->param.rc.i_aq_mode = 0;
4319 h->param.rc.b_mb_tree = 0;
4320 }
4321- h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, 51 );
4322+ h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, QP_MAX );
4323 h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max );
4324 if( h->param.rc.i_vbv_buffer_size )
4325 {
4326@@ -1054,8 +1056,9 @@ x264_t *x264_encoder_open( x264_param_t *param )
4327 if( x264_analyse_init_costs( h, X264_LOOKAHEAD_QP ) )
4328 goto fail;
4329
4330+ static const uint16_t cost_mv_correct[7] = { 24, 47, 95, 189, 379, 757, 1515 };
4331 /* Checks for known miscompilation issues. */
4332- if( h->cost_mv[1][2013] != 24 )
4333+ if( h->cost_mv[x264_lambda_tab[X264_LOOKAHEAD_QP]][2013] != cost_mv_correct[BIT_DEPTH-8] )
4334 {
4335 x264_log( h, X264_LOG_ERROR, "MV cost test failed: x264 has been miscompiled!\n" );
4336 goto fail;
4337@@ -1147,11 +1150,22 @@ x264_t *x264_encoder_open( x264_param_t *param )
4338 fclose( f );
4339 }
4340
4341- x264_log( h, X264_LOG_INFO, "profile %s, level %d.%d\n",
4342- h->sps->i_profile_idc == PROFILE_BASELINE ? "Baseline" :
4343- h->sps->i_profile_idc == PROFILE_MAIN ? "Main" :
4344- h->sps->i_profile_idc == PROFILE_HIGH ? "High" :
4345- "High 4:4:4 Predictive", h->sps->i_level_idc/10, h->sps->i_level_idc%10 );
4346+ const char *profile = h->sps->i_profile_idc == PROFILE_BASELINE ? "Baseline" :
4347+ h->sps->i_profile_idc == PROFILE_MAIN ? "Main" :
4348+ h->sps->i_profile_idc == PROFILE_HIGH ? "High" :
4349+ h->sps->i_profile_idc == PROFILE_HIGH10 ? "High 10" :
4350+ "High 4:4:4 Predictive";
4351+
4352+ if( h->sps->i_profile_idc < PROFILE_HIGH10 )
4353+ {
4354+ x264_log( h, X264_LOG_INFO, "profile %s, level %d.%d\n",
4355+ profile, h->sps->i_level_idc/10, h->sps->i_level_idc%10 );
4356+ }
4357+ else
4358+ {
4359+ x264_log( h, X264_LOG_INFO, "profile %s, level %d.%d, bit depth %d\n",
4360+ profile, h->sps->i_level_idc/10, h->sps->i_level_idc%10, BIT_DEPTH );
4361+ }
4362
4363 return h;
4364 fail:
4365@@ -1836,7 +1850,7 @@ static int x264_slice_write( x264_t *h )
4366 bs_align_1( &h->out.bs );
4367
4368 /* init cabac */
4369- x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );
4370+ x264_cabac_context_init( &h->cabac, h->sh.i_type, x264_clip3( h->sh.i_qp-QP_BD_OFFSET, 0, 51 ), h->sh.i_cabac_init_idc );
4371 x264_cabac_encode_init ( &h->cabac, h->out.bs.p, h->out.bs.p_end );
4372 }
4373 h->mb.i_last_qp = h->sh.i_qp;
4374@@ -2705,6 +2719,7 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
4375 for( int i = 0; i < 3; i++ )
4376 {
4377 pic_out->img.i_stride[i] = h->fdec->i_stride[i];
4378+ // FIXME This breaks the API when pixel != uint8_t.
4379 pic_out->img.plane[i] = h->fdec->plane[i];
4380 }
4381
4382diff --git a/encoder/macroblock.h b/encoder/macroblock.h
4383index b1b02fa..7c83344 100644
4384--- a/encoder/macroblock.h
4385+++ b/encoder/macroblock.h
4386@@ -26,8 +26,8 @@
4387
4388 #include "common/macroblock.h"
4389
4390-extern const int x264_lambda2_tab[52];
4391-extern const uint8_t x264_lambda_tab[52];
4392+extern const int x264_lambda2_tab[QP_MAX_MAX+1];
4393+extern const uint16_t x264_lambda_tab[QP_MAX_MAX+1];
4394
4395 void x264_rdo_init( void );
4396
4397diff --git a/encoder/me.h b/encoder/me.h
4398index 912b05d..b125f3d 100644
4399--- a/encoder/me.h
4400+++ b/encoder/me.h
4401@@ -68,7 +68,7 @@ void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_wei
4402 void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight );
4403 uint64_t x264_rd_cost_part( x264_t *h, int i_lambda2, int i8, int i_pixel );
4404
4405-extern uint16_t *x264_cost_mv_fpel[92][4];
4406+extern uint16_t *x264_cost_mv_fpel[LAMBDA_MAX+1][4];
4407
4408 #define COPY1_IF_LT(x,y)\
4409 if((y)<(x))\
4410diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
4411index 6fdaa98..bdf44dc 100644
4412--- a/encoder/ratecontrol.c
4413+++ b/encoder/ratecontrol.c
4414@@ -219,7 +219,7 @@ static ALWAYS_INLINE uint32_t ac_energy_plane( x264_t *h, int mb_x, int mb_y, x2
4415 uint32_t ssd = res >> 32;
4416 frame->i_pixel_sum[i] += sum;
4417 frame->i_pixel_ssd[i] += ssd;
4418- return ssd - (sum * sum >> shift);
4419+ return ssd - ((uint64_t)sum * sum >> shift);
4420 }
4421
4422 // Find the total AC energy of the block in all planes.
4423@@ -300,7 +300,7 @@ void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_off
4424 avg_adj /= h->mb.i_mb_count;
4425 avg_adj_pow2 /= h->mb.i_mb_count;
4426 strength = h->param.rc.f_aq_strength * avg_adj;
4427- avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj;
4428+ avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - (14.f + 2*(BIT_DEPTH-8))) / avg_adj;
4429 }
4430 else
4431 strength = h->param.rc.f_aq_strength * 1.0397f;
4432@@ -318,7 +318,7 @@ void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_off
4433 else
4434 {
4435 uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
4436- qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - 14.427f);
4437+ qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - (14.427f + 2*(BIT_DEPTH-8)));
4438 }
4439 if( quant_offsets )
4440 qp_adj += quant_offsets[mb_xy];
4441@@ -620,8 +620,8 @@ int x264_ratecontrol_new( x264_t *h )
4442 rc->ip_offset = 6.0 * log2f( h->param.rc.f_ip_factor );
4443 rc->pb_offset = 6.0 * log2f( h->param.rc.f_pb_factor );
4444 rc->qp_constant[SLICE_TYPE_P] = h->param.rc.i_qp_constant;
4445- rc->qp_constant[SLICE_TYPE_I] = x264_clip3( h->param.rc.i_qp_constant - rc->ip_offset + 0.5, 0, 51 );
4446- rc->qp_constant[SLICE_TYPE_B] = x264_clip3( h->param.rc.i_qp_constant + rc->pb_offset + 0.5, 0, 51 );
4447+ rc->qp_constant[SLICE_TYPE_I] = x264_clip3( h->param.rc.i_qp_constant - rc->ip_offset + 0.5, 0, QP_MAX );
4448+ rc->qp_constant[SLICE_TYPE_B] = x264_clip3( h->param.rc.i_qp_constant + rc->pb_offset + 0.5, 0, QP_MAX );
4449 h->mb.ip_offset = rc->ip_offset + 0.5;
4450
4451 rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 );
4452@@ -1231,7 +1231,7 @@ void x264_ratecontrol_start( x264_t *h, int i_force_qp, int overhead )
4453
4454 rc->qpa_rc =
4455 rc->qpa_aq = 0;
4456- rc->qp = x264_clip3( (int)(q + 0.5), 0, 51 );
4457+ rc->qp = x264_clip3( (int)(q + 0.5), 0, QP_MAX );
4458 h->fdec->f_qp_avg_rc =
4459 h->fdec->f_qp_avg_aq =
4460 rc->qpm = q;
4461@@ -1416,9 +1416,9 @@ int x264_ratecontrol_slice_type( x264_t *h, int frame_num )
4462 * So just calculate the average QP used so far. */
4463 h->param.rc.i_qp_constant = (h->stat.i_frame_count[SLICE_TYPE_P] == 0) ? 24
4464 : 1 + h->stat.f_frame_qp[SLICE_TYPE_P] / h->stat.i_frame_count[SLICE_TYPE_P];
4465- rc->qp_constant[SLICE_TYPE_P] = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
4466- rc->qp_constant[SLICE_TYPE_I] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) / fabs( h->param.rc.f_ip_factor )) + 0.5 ), 0, 51 );
4467- rc->qp_constant[SLICE_TYPE_B] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) * fabs( h->param.rc.f_pb_factor )) + 0.5 ), 0, 51 );
4468+ rc->qp_constant[SLICE_TYPE_P] = x264_clip3( h->param.rc.i_qp_constant, 0, QP_MAX );
4469+ rc->qp_constant[SLICE_TYPE_I] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) / fabs( h->param.rc.f_ip_factor )) + 0.5 ), 0, QP_MAX );
4470+ rc->qp_constant[SLICE_TYPE_B] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) * fabs( h->param.rc.f_pb_factor )) + 0.5 ), 0, QP_MAX );
4471
4472 x264_log(h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", rc->num_entries);
4473 x264_log(h, X264_LOG_ERROR, "continuing anyway, at constant QP=%d\n", h->param.rc.i_qp_constant);
4474@@ -2652,7 +2652,7 @@ static int init_pass2( x264_t *h )
4475 }
4476 else if( expected_bits > all_available_bits && avgq > h->param.rc.i_qp_max - 2 )
4477 {
4478- if( h->param.rc.i_qp_max < 51 )
4479+ if( h->param.rc.i_qp_max < QP_MAX )
4480 x264_log( h, X264_LOG_WARNING, "try increasing target bitrate or increasing qp_max (currently %d)\n", h->param.rc.i_qp_max );
4481 else
4482 x264_log( h, X264_LOG_WARNING, "try increasing target bitrate\n");
4483diff --git a/encoder/rdo.c b/encoder/rdo.c
4484index afaa894..4fae811 100644
4485--- a/encoder/rdo.c
4486+++ b/encoder/rdo.c
4487@@ -443,10 +443,7 @@ static ALWAYS_INLINE int quant_trellis_cabac( x264_t *h, dctcoef *dct,
4488 /* We only need to zero an empty 4x4 block. 8x8 can be
4489 implicitly emptied via zero nnz, as can dc. */
4490 if( i_coefs == 16 && !dc )
4491- {
4492- M128( &dct[0] ) = M128_ZERO;
4493- M128( &dct[8] ) = M128_ZERO;
4494- }
4495+ memset( dct, 0, 16 * sizeof(dctcoef) );
4496 return 0;
4497 }
4498
4499@@ -613,10 +610,7 @@ static ALWAYS_INLINE int quant_trellis_cabac( x264_t *h, dctcoef *dct,
4500 if( bnode == &nodes_cur[0] )
4501 {
4502 if( i_coefs == 16 && !dc )
4503- {
4504- M128( &dct[0] ) = M128_ZERO;
4505- M128( &dct[8] ) = M128_ZERO;
4506- }
4507+ memset( dct, 0, 16 * sizeof(dctcoef) );
4508 return 0;
4509 }
4510
4511diff --git a/encoder/set.c b/encoder/set.c
4512index 9a3c7dc..d610e03 100644
4513--- a/encoder/set.c
4514+++ b/encoder/set.c
4515@@ -104,6 +104,8 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
4516 sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
4517 if( sps->b_qpprime_y_zero_transform_bypass )
4518 sps->i_profile_idc = PROFILE_HIGH444_PREDICTIVE;
4519+ else if( BIT_DEPTH > 8 )
4520+ sps->i_profile_idc = PROFILE_HIGH10;
4521 else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
4522 sps->i_profile_idc = PROFILE_HIGH;
4523 else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
4524@@ -260,8 +262,8 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps )
4525 if( sps->i_profile_idc >= PROFILE_HIGH )
4526 {
4527 bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
4528- bs_write_ue( s, 0 ); // bit_depth_luma_minus8
4529- bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
4530+ bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
4531+ bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
4532 bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
4533 bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
4534 }
4535@@ -488,7 +490,7 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps )
4536 bs_write( s, 1, pps->b_weighted_pred );
4537 bs_write( s, 2, pps->b_weighted_bipred );
4538
4539- bs_write_se( s, pps->i_pic_init_qp - 26 );
4540+ bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
4541 bs_write_se( s, pps->i_pic_init_qs - 26 );
4542 bs_write_se( s, pps->i_chroma_qp_index_offset );
4543
4544diff --git a/encoder/slicetype.c b/encoder/slicetype.c
4545index 7d69b71..7c0d021 100644
4546--- a/encoder/slicetype.c
4547+++ b/encoder/slicetype.c
4548@@ -303,7 +303,7 @@ static void x264_slicetype_mb_cost( x264_t *h, x264_mb_analysis_t *a,
4549 (mv1)[0], (mv1)[1], 8, 8, w ); \
4550 h->mc.avg[PIXEL_8x8]( pix1, 16, src1, stride1, src2, stride2, i_bipred_weight ); \
4551 } \
4552- i_cost = penalty + h->pixf.mbcmp[PIXEL_8x8]( \
4553+ i_cost = penalty * a->i_lambda + h->pixf.mbcmp[PIXEL_8x8]( \
4554 m[0].p_fenc[0], FENC_STRIDE, pix1, 16 ); \
4555 COPY2_IF_LT( i_bcost, i_cost, list_used, 3 ); \
4556 }
4557@@ -393,9 +393,9 @@ static void x264_slicetype_mb_cost( x264_t *h, x264_mb_analysis_t *a,
4558 }
4559
4560 x264_me_search( h, &m[l], mvc, i_mvc );
4561- m[l].cost -= 2; // remove mvcost from skip mbs
4562+ m[l].cost -= 2 * a->i_lambda; // remove mvcost from skip mbs
4563 if( M32( m[l].mv ) )
4564- m[l].cost += 5;
4565+ m[l].cost += 5 * a->i_lambda;
4566
4567 skip_motionest:
4568 CP32( fenc_mvs[l], m[l].mv );
4569@@ -418,7 +418,7 @@ lowres_intra_mb:
4570 ALIGNED_ARRAY_16( pixel, edge,[33] );
4571 pixel *pix = &pix1[8+FDEC_STRIDE - 1];
4572 pixel *src = &fenc->lowres[0][i_pel_offset - 1];
4573- const int intra_penalty = 5;
4574+ const int intra_penalty = 5 * a->i_lambda;
4575 int satds[3];
4576
4577 memcpy( pix-FDEC_STRIDE, src-i_stride, 17 * sizeof(pixel) );
4578@@ -496,7 +496,7 @@ lowres_intra_mb:
4579 }
4580 }
4581
4582- fenc->lowres_costs[b-p0][p1-b][i_mb_xy] = i_bcost + (list_used << LOWRES_COST_SHIFT);
4583+ fenc->lowres_costs[b-p0][p1-b][i_mb_xy] = X264_MIN( i_bcost + (list_used << LOWRES_COST_SHIFT), LOWRES_COST_MASK );
4584 }
4585 #undef TRY_BIDIR
4586
4587diff --git a/tools/checkasm.c b/tools/checkasm.c
4588index 7fa2c0c..a5ffa17 100644
4589--- a/tools/checkasm.c
4590+++ b/tools/checkasm.c
4591@@ -40,8 +40,10 @@
4592 uint8_t *buf1, *buf2;
4593 /* buf3, buf4: used to store output */
4594 uint8_t *buf3, *buf4;
4595-/* pbuf*: point to the same memory as above, just for type convenience */
4596-pixel *pbuf1, *pbuf2, *pbuf3, *pbuf4;
4597+/* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
4598+pixel *pbuf1, *pbuf2;
4599+/* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
4600+pixel *pbuf3, *pbuf4;
4601
4602 int quiet = 0;
4603
4604@@ -256,11 +258,15 @@ static int check_pixel( int cpu_ref, int cpu_new )
4605 int z = i|(i>>4);
4606 z ^= z>>2;
4607 z ^= z>>1;
4608- buf3[i] = ~(buf4[i] = -(z&1));
4609+ pbuf4[i] = -(z&1) & PIXEL_MAX;
4610+ pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
4611 }
4612 // random pattern made of maxed pixel differences, in case an intermediate value overflows
4613 for( int i = 256; i < 0x1000; i++ )
4614- buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
4615+ {
4616+ pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
4617+ pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
4618+ }
4619
4620 #define TEST_PIXEL( name, align ) \
4621 ok = 1, used_asm = 0; \
4622@@ -535,22 +541,22 @@ static int check_dct( int cpu_ref, int cpu_new )
4623 used_asm = 1; \
4624 call_c( dct_c.name, t1, pbuf1, pbuf2 ); \
4625 call_a( dct_asm.name, t2, pbuf1, pbuf2 ); \
4626- if( memcmp( t1, t2, size ) ) \
4627+ if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
4628 { \
4629 ok = 0; \
4630 fprintf( stderr, #name " [FAILED]\n" ); \
4631 } \
4632 }
4633 ok = 1; used_asm = 0;
4634- TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
4635- TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
4636- TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4*2 );
4637- TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
4638+ TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
4639+ TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
4640+ TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
4641+ TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
4642 report( "sub_dct4 :" );
4643
4644 ok = 1; used_asm = 0;
4645- TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
4646- TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
4647+ TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
4648+ TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
4649 report( "sub_dct8 :" );
4650 #undef TEST_DCT
4651
4652@@ -574,13 +580,13 @@ static int check_dct( int cpu_ref, int cpu_new )
4653 { \
4654 set_func_name( #name ); \
4655 used_asm = 1; \
4656- memcpy( buf3, buf1, 32*32 * sizeof(pixel) ); \
4657- memcpy( buf4, buf1, 32*32 * sizeof(pixel) ); \
4658- memcpy( dct1, src, 512 * sizeof(pixel) ); \
4659- memcpy( dct2, src, 512 * sizeof(pixel) ); \
4660+ memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
4661+ memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
4662+ memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
4663+ memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
4664 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
4665 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
4666- if( memcmp( buf3, buf4, 32*32 * sizeof(pixel) ) ) \
4667+ if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
4668 { \
4669 ok = 0; \
4670 fprintf( stderr, #name " [FAILED]\n" ); \
4671@@ -615,10 +621,10 @@ static int check_dct( int cpu_ref, int cpu_new )
4672 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
4673 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
4674 : ((*p++)&0x1fff)-0x1000; /* general case */\
4675- memcpy( dct2, dct1, 32 );\
4676+ memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
4677 call_c1( dct_c.name, dct1[0] );\
4678 call_a1( dct_asm.name, dct2[0] );\
4679- if( memcmp( dct1, dct2, 32 ) )\
4680+ if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
4681 ok = 0;\
4682 }\
4683 call_c2( dct_c.name, dct1[0] );\
4684@@ -658,11 +664,11 @@ static int check_dct( int cpu_ref, int cpu_new )
4685 int nz_a, nz_c; \
4686 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
4687 used_asm = 1; \
4688- memcpy( buf3, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
4689- memcpy( buf4, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
4690+ memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
4691+ memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
4692 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3 ); \
4693 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
4694- if( memcmp( t1, t2, size*sizeof(dctcoef) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a ) \
4695+ if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
4696 { \
4697 ok = 0; \
4698 fprintf( stderr, #name " [FAILED]\n" ); \
4699@@ -680,8 +686,8 @@ static int check_dct( int cpu_ref, int cpu_new )
4700 used_asm = 1; \
4701 for( int i = 0; i < 2; i++ ) \
4702 { \
4703- memcpy( buf3, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
4704- memcpy( buf4, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
4705+ memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
4706+ memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
4707 for( int j = 0; j < 4; j++ ) \
4708 { \
4709 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
4710@@ -689,7 +695,7 @@ static int check_dct( int cpu_ref, int cpu_new )
4711 } \
4712 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
4713 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
4714- if( memcmp( t1+1, t2+1, 15*sizeof(dctcoef) ) || memcmp( buf3, buf4, 16*FDEC_STRIDE * sizeof(pixel) ) || nz_c != nz_a || dc_c != dc_a ) \
4715+ if( memcmp( t1+1, t2+1, 15*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE * sizeof(pixel) ) || nz_c != nz_a || dc_c != dc_a ) \
4716 { \
4717 ok = 0; \
4718 fprintf( stderr, #name " [FAILED]\n" ); \
4719@@ -779,11 +785,11 @@ static int check_mc( int cpu_ref, int cpu_new )
4720 const x264_weight_t *weight = weight_none; \
4721 set_func_name( "mc_luma_%dx%d", w, h ); \
4722 used_asm = 1; \
4723- memset( buf3, 0xCD, 1024 ); \
4724- memset( buf4, 0xCD, 1024 ); \
4725+ for( int i = 0; i < 1024; i++ ) \
4726+ pbuf3[i] = pbuf4[i] = 0xCD; \
4727 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
4728 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
4729- if( memcmp( buf3, buf4, 1024 ) ) \
4730+ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
4731 { \
4732 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
4733 ok = 0; \
4734@@ -796,8 +802,8 @@ static int check_mc( int cpu_ref, int cpu_new )
4735 const x264_weight_t *weight = weight_none; \
4736 set_func_name( "get_ref_%dx%d", w, h ); \
4737 used_asm = 1; \
4738- memset( buf3, 0xCD, 1024 ); \
4739- memset( buf4, 0xCD, 1024 ); \
4740+ for( int i = 0; i < 1024; i++ ) \
4741+ pbuf3[i] = pbuf4[i] = 0xCD; \
4742 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
4743 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
4744 for( int i = 0; i < h; i++ ) \
4745@@ -814,15 +820,15 @@ static int check_mc( int cpu_ref, int cpu_new )
4746 { \
4747 set_func_name( "mc_chroma_%dx%d", w, h ); \
4748 used_asm = 1; \
4749- memset( buf3, 0xCD, 1024 ); \
4750- memset( buf4, 0xCD, 1024 ); \
4751+ for( int i = 0; i < 1024; i++ ) \
4752+ pbuf3[i] = pbuf4[i] = 0xCD; \
4753 call_c( mc_c.mc_chroma, dst1, 16, src, 64, dx, dy, w, h ); \
4754 call_a( mc_a.mc_chroma, dst2, 16, src, 64, dx, dy, w, h ); \
4755 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
4756 for( int j = 0; j < h; j++ ) \
4757 for( int i = w; i < 4; i++ ) \
4758 dst2[i+j*16] = dst1[i+j*16]; \
4759- if( memcmp( buf3, buf4, 1024 ) ) \
4760+ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
4761 { \
4762 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
4763 ok = 0; \
4764@@ -867,15 +873,15 @@ static int check_mc( int cpu_ref, int cpu_new )
4765 ok = 1, used_asm = 0; \
4766 for( int i = 0; i < 10; i++ ) \
4767 { \
4768- memcpy( buf3, pbuf1+320, 320 * sizeof(pixel) ); \
4769- memcpy( buf4, pbuf1+320, 320 * sizeof(pixel) ); \
4770+ memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
4771+ memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
4772 if( mc_a.name[i] != mc_ref.name[i] ) \
4773 { \
4774 set_func_name( "%s_%s", #name, pixel_names[i] ); \
4775 used_asm = 1; \
4776 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
4777 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
4778- if( memcmp( buf3, buf4, 320 * sizeof(pixel) ) ) \
4779+ if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
4780 { \
4781 ok = 0; \
4782 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
4783@@ -971,8 +977,8 @@ static int check_mc( int cpu_ref, int cpu_new )
4784 void *tmp = pbuf3+49*64;
4785 set_func_name( "hpel_filter" );
4786 ok = 1; used_asm = 1;
4787- memset( buf3, 0, 4096 * sizeof(pixel) );
4788- memset( buf4, 0, 4096 * sizeof(pixel) );
4789+ memset( pbuf3, 0, 4096 * sizeof(pixel) );
4790+ memset( pbuf4, 0, 4096 * sizeof(pixel) );
4791 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
4792 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
4793 for( int i = 0; i < 3; i++ )
4794@@ -1030,13 +1036,13 @@ static int check_mc( int cpu_ref, int cpu_new )
4795 int stride = 80;\
4796 set_func_name( #name );\
4797 used_asm = 1;\
4798- memcpy( buf3, buf1, size*2*stride * sizeof(pixel) );\
4799- memcpy( buf4, buf1, size*2*stride * sizeof(pixel) );\
4800- uint16_t *sum = (uint16_t*)buf3;\
4801+ memcpy( pbuf3, pbuf1, size*2*stride * sizeof(pixel) );\
4802+ memcpy( pbuf4, pbuf1, size*2*stride * sizeof(pixel) );\
4803+ uint16_t *sum = (uint16_t*)pbuf3;\
4804 call_c1( mc_c.name, __VA_ARGS__ );\
4805- sum = (uint16_t*)buf4;\
4806+ sum = (uint16_t*)pbuf4;\
4807 call_a1( mc_a.name, __VA_ARGS__ );\
4808- if( memcmp( buf3, buf4, (stride-8)*2 * sizeof(pixel) )\
4809+ if( memcmp( pbuf3, pbuf4, (stride-8)*2 * sizeof(pixel) )\
4810 || (size>9 && memcmp( pbuf3+18*stride, pbuf4+18*stride, (stride-8)*2 * sizeof(pixel) )))\
4811 ok = 0;\
4812 call_c2( mc_c.name, __VA_ARGS__ );\
4813@@ -1096,11 +1102,11 @@ static int check_deblock( int cpu_ref, int cpu_new )
4814 /* not exactly the real values of a,b,tc but close enough */
4815 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
4816 {
4817- alphas[i] = a;
4818- betas[i] = (i+1)/2;
4819- tcs[i][0] = tcs[i][3] = (c+6)/10;
4820- tcs[i][1] = (c+7)/15;
4821- tcs[i][2] = (c+9)/20;
4822+ alphas[i] = a << (BIT_DEPTH-8);
4823+ betas[i] = (i+1)/2 << (BIT_DEPTH-8);
4824+ tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
4825+ tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
4826+ tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
4827 a = a*9/10;
4828 c = c*9/10;
4829 }
4830@@ -1111,15 +1117,15 @@ static int check_deblock( int cpu_ref, int cpu_new )
4831 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
4832 for( int j = 0; j < 1024; j++ ) \
4833 /* two distributions of random to excersize different failure modes */ \
4834- buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
4835- memcpy( buf4, buf3, 1024 * sizeof(pixel) ); \
4836+ pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
4837+ memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
4838 if( db_a.name != db_ref.name ) \
4839 { \
4840 set_func_name( #name ); \
4841 used_asm = 1; \
4842 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
4843 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
4844- if( memcmp( buf3, buf4, 1024 * sizeof(pixel) ) ) \
4845+ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
4846 { \
4847 ok = 0; \
4848 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
4849@@ -1200,7 +1206,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4850 h->pps = h->pps_array;
4851 x264_param_default( &h->param );
4852 h->chroma_qp_table = i_chroma_qp_table + 12;
4853- h->param.rc.i_qp_min = 26;
4854+ h->param.rc.i_qp_min = 26 + QP_BD_OFFSET;
4855 h->param.analyse.b_transform_8x8 = 1;
4856
4857 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
4858@@ -1219,9 +1225,10 @@ static int check_quant( int cpu_ref, int cpu_new )
4859 }
4860 else
4861 {
4862+ int max_scale = BIT_DEPTH < 10 ? 255 : 228;
4863 if( i_cqm == 2 )
4864 for( int i = 0; i < 64; i++ )
4865- cqm_buf[i] = 10 + rand() % 246;
4866+ cqm_buf[i] = 10 + rand() % (max_scale - 9);
4867 else
4868 for( int i = 0; i < 64; i++ )
4869 cqm_buf[i] = 1;
4870@@ -1260,7 +1267,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4871 { \
4872 set_func_name( #name ); \
4873 used_asms[0] = 1; \
4874- for( int qp = 51; qp > 0; qp-- ) \
4875+ for( int qp = QP_MAX; qp > 0; qp-- ) \
4876 { \
4877 for( int j = 0; j < 2; j++ ) \
4878 { \
4879@@ -1269,7 +1276,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4880 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
4881 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
4882 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
4883- if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a ) \
4884+ if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
4885 { \
4886 oks[0] = 0; \
4887 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
4888@@ -1286,14 +1293,14 @@ static int check_quant( int cpu_ref, int cpu_new )
4889 { \
4890 set_func_name( #qname ); \
4891 used_asms[0] = 1; \
4892- for( int qp = 51; qp > 0; qp-- ) \
4893+ for( int qp = QP_MAX; qp > 0; qp-- ) \
4894 { \
4895 for( int j = 0; j < 2; j++ ) \
4896 { \
4897 INIT_QUANT##w(j) \
4898 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
4899 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
4900- if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
4901+ if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
4902 { \
4903 oks[0] = 0; \
4904 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
4905@@ -1317,14 +1324,14 @@ static int check_quant( int cpu_ref, int cpu_new )
4906 { \
4907 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
4908 used_asms[1] = 1; \
4909- for( int qp = 51; qp > 0; qp-- ) \
4910+ for( int qp = QP_MAX; qp > 0; qp-- ) \
4911 { \
4912 INIT_QUANT##w(1) \
4913 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
4914- memcpy( dct2, dct1, w*w*2 ); \
4915+ memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
4916 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
4917 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
4918- if( memcmp( dct1, dct2, w*w*2 ) ) \
4919+ if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
4920 { \
4921 oks[1] = 0; \
4922 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
4923@@ -1345,15 +1352,15 @@ static int check_quant( int cpu_ref, int cpu_new )
4924 { \
4925 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
4926 used_asms[1] = 1; \
4927- for( int qp = 51; qp > 0; qp-- ) \
4928+ for( int qp = QP_MAX; qp > 0; qp-- ) \
4929 { \
4930 for( int i = 0; i < 16; i++ ) \
4931 dct1[i] = rand(); \
4932 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
4933- memcpy( dct2, dct1, w*w*2 ); \
4934+ memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
4935 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
4936 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
4937- if( memcmp( dct1, dct2, w*w*2 ) ) \
4938+ if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
4939 { \
4940 oks[1] = 0; \
4941 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
4942@@ -1381,12 +1388,12 @@ static int check_quant( int cpu_ref, int cpu_new )
4943 for( int size = 16; size <= 64; size += 48 )
4944 {
4945 set_func_name( "denoise_dct" );
4946- memcpy( dct1, buf1, size*2 );
4947- memcpy( dct2, buf1, size*2 );
4948+ memcpy( dct1, buf1, size*sizeof(dctcoef) );
4949+ memcpy( dct2, buf1, size*sizeof(dctcoef) );
4950 memcpy( buf3+256, buf3, 256 );
4951 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
4952 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
4953- if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
4954+ if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
4955 ok = 0;
4956 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
4957 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
4958@@ -1431,7 +1438,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4959 { \
4960 int nnz = 0; \
4961 int max = rand() & (w*w-1); \
4962- memset( dct1, 0, w*w*2 ); \
4963+ memset( dct1, 0, w*w*sizeof(dctcoef) ); \
4964 for( int idx = ac; idx < max; idx++ ) \
4965 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
4966 if( !nnz ) \
4967@@ -1464,7 +1471,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4968 x264_run_level_t runlevel_c, runlevel_a; \
4969 int nnz = 0; \
4970 int max = rand() & (w*w-1); \
4971- memset( dct1, 0, w*w*2 ); \
4972+ memset( dct1, 0, w*w*sizeof(dctcoef) ); \
4973 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
4974 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
4975 for( int idx = ac; idx < max; idx++ ) \
4976@@ -1474,7 +1481,7 @@ static int check_quant( int cpu_ref, int cpu_new )
4977 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
4978 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
4979 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
4980- memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
4981+ memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
4982 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
4983 { \
4984 ok = 0; \
4985@@ -1529,11 +1536,11 @@ static int check_intra( int cpu_ref, int cpu_new )
4986 {\
4987 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
4988 used_asm = 1;\
4989- memcpy( buf3, buf1, 32*20 * sizeof(pixel) );\
4990- memcpy( buf4, buf1, 32*20 * sizeof(pixel) );\
4991+ memcpy( pbuf3, pbuf1, 32*20 * sizeof(pixel) );\
4992+ memcpy( pbuf4, pbuf1, 32*20 * sizeof(pixel) );\
4993 call_c( ip_c.name[dir], pbuf3+48, ##__VA_ARGS__ );\
4994 call_a( ip_a.name[dir], pbuf4+48, ##__VA_ARGS__ );\
4995- if( memcmp( buf3, buf4, 32*20 * sizeof(pixel) ) )\
4996+ if( memcmp( pbuf3, pbuf4, 32*20 * sizeof(pixel) ) )\
4997 {\
4998 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
4999 ok = 0;\
5000@@ -1544,7 +1551,7 @@ static int check_intra( int cpu_ref, int cpu_new )
5001 {\
5002 printf( "%2x ", edge[14-j] );\
5003 for( int k = 0; k < w; k++ )\
5004- printf( "%2x ", buf4[48+k+j*32] );\
5005+ printf( "%2x ", pbuf4[48+k+j*32] );\
5006 printf( "\n" );\
5007 }\
5008 printf( "\n" );\
5009@@ -1552,7 +1559,7 @@ static int check_intra( int cpu_ref, int cpu_new )
5010 {\
5011 printf( " " );\
5012 for( int k = 0; k < w; k++ )\
5013- printf( "%2x ", buf3[48+k+j*32] );\
5014+ printf( "%2x ", pbuf3[48+k+j*32] );\
5015 printf( "\n" );\
5016 }\
5017 }\
5018@@ -1831,8 +1838,9 @@ int main(int argc, char *argv[])
5019 fprintf( stderr, "x264: using random seed %u\n", seed );
5020 srand( seed );
5021
5022- buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
5023- if( !buf1 )
5024+ buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
5025+ pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
5026+ if( !buf1 || !pbuf1 )
5027 {
5028 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
5029 return -1;
5030@@ -1840,15 +1848,17 @@ int main(int argc, char *argv[])
5031 #define INIT_POINTER_OFFSETS\
5032 buf2 = buf1 + 0xf00;\
5033 buf3 = buf2 + 0xf00;\
5034- buf4 = buf3 + 0x1000;\
5035- pbuf1 = (pixel*)buf1;\
5036- pbuf2 = (pixel*)buf2;\
5037+ buf4 = buf3 + 0x1000*sizeof(pixel);\
5038+ pbuf2 = pbuf1 + 0xf00;\
5039 pbuf3 = (pixel*)buf3;\
5040 pbuf4 = (pixel*)buf4;
5041 INIT_POINTER_OFFSETS;
5042 for( int i = 0; i < 0x1e00; i++ )
5043+ {
5044 buf1[i] = rand() & 0xFF;
5045- memset( buf1+0x1e00, 0, 0x2000 );
5046+ pbuf1[i] = rand() & PIXEL_MAX;
5047+ }
5048+ memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
5049
5050 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
5051 if( do_bench )
5052@@ -1857,6 +1867,7 @@ int main(int argc, char *argv[])
5053 INIT_POINTER_OFFSETS;
5054 ret |= x264_stack_pagealign( check_all_flags, i*16 );
5055 buf1 += 16;
5056+ pbuf1 += 16;
5057 quiet = 1;
5058 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
5059 }
5060diff --git a/x264.c b/x264.c
5061index 0bede93..c1141ab 100644
5062--- a/x264.c
5063+++ b/x264.c
5064@@ -262,6 +262,7 @@ static void Help( x264_param_t *defaults, int longhelp )
5065 " .mkv -> Matroska\n"
5066 " .flv -> Flash Video\n"
5067 " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
5068+ "Output bit depth: %d (configured at compile time)\n"
5069 "\n"
5070 "Options:\n"
5071 "\n"
5072@@ -286,10 +287,11 @@ static void Help( x264_param_t *defaults, int longhelp )
5073 "no",
5074 #endif
5075 #if HAVE_GPAC
5076- "yes"
5077+ "yes",
5078 #else
5079- "no"
5080+ "no",
5081 #endif
5082+ BIT_DEPTH
5083 );
5084 H0( "Example usage:\n" );
5085 H0( "\n" );
5086@@ -311,8 +313,8 @@ static void Help( x264_param_t *defaults, int longhelp )
5087 H0( "\n" );
5088 H0( "Presets:\n" );
5089 H0( "\n" );
5090- H0( " --profile Force the limits of an H.264 profile [high]\n"
5091- " Overrides all settings.\n" );
5092+ H0( " --profile Force the limits of an H.264 profile [%s]\n"
5093+ " Overrides all settings.\n", BIT_DEPTH > 8 ? "high10" : "high" );
5094 H2( " - baseline:\n"
5095 " --no-8x8dct --bframes 0 --no-cabac\n"
5096 " --cqm flat --weightp 0\n"
5097@@ -322,8 +324,11 @@ static void Help( x264_param_t *defaults, int longhelp )
5098 " --no-8x8dct --cqm flat\n"
5099 " No lossless.\n"
5100 " - high:\n"
5101- " No lossless.\n" );
5102- else H0( " - baseline,main,high\n" );
5103+ " No lossless.\n"
5104+ " - high10:\n"
5105+ " No lossless.\n"
5106+ " Support for bit depth 8-10.\n" );
5107+ else H0( " - baseline,main,high,high10\n" );
5108 H0( " --preset Use a preset to select encoding settings [medium]\n"
5109 " Overridden by user settings.\n" );
5110 H2( " - ultrafast:\n"
5111@@ -453,9 +458,9 @@ static void Help( x264_param_t *defaults, int longhelp )
5112 H0( "\n" );
5113 H0( "Ratecontrol:\n" );
5114 H0( "\n" );
5115- H1( " -q, --qp <integer> Force constant QP (0-51, 0=lossless)\n" );
5116+ H1( " -q, --qp <integer> Force constant QP (0-%d, 0=lossless)\n", QP_MAX );
5117 H0( " -B, --bitrate <integer> Set bitrate (kbit/s)\n" );
5118- H0( " --crf <float> Quality-based VBR (0-51, 0=lossless) [%.1f]\n", defaults->rc.f_rf_constant );
5119+ H0( " --crf <float> Quality-based VBR (0-%d, 0=lossless) [%.1f]\n", QP_MAX, defaults->rc.f_rf_constant );
5120 H1( " --rc-lookahead <integer> Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead );
5121 H0( " --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
5122 H0( " --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
5123@@ -1040,6 +1045,7 @@ static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt )
5124 #else
5125 printf( "using a non-gcc compiler\n" );
5126 #endif
5127+ printf( "configuration: --bit-depth=%d\n", BIT_DEPTH );
5128 exit(0);
5129 case OPT_FRAMES:
5130 param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
5131@@ -1318,7 +1324,7 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
5132 else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
5133 else if( type == 'b' ) pic->i_type = X264_TYPE_B;
5134 else ret = 0;
5135- if( ret != 3 || qp < -1 || qp > 51 )
5136+ if( ret != 3 || qp < -1 || qp > QP_MAX )
5137 {
5138 x264_cli_log( "x264", X264_LOG_ERROR, "can't parse qpfile for frame %d\n", i_frame );
5139 fclose( opt->qpfile );
5140diff --git a/x264.h b/x264.h
5141index 330cf74..a25f27e 100644
5142--- a/x264.h
5143+++ b/x264.h
5144@@ -344,7 +344,7 @@ typedef struct x264_param_t
5145 {
5146 int i_rc_method; /* X264_RC_* */
5147
5148- int i_qp_constant; /* 0-51 */
5149+ int i_qp_constant; /* 0 to (51 + 6*(BIT_DEPTH-8)) */
5150 int i_qp_min; /* min allowed QP value */
5151 int i_qp_max; /* max allowed QP value */
5152 int i_qp_step; /* max QP step between frames */
5153@@ -550,7 +550,7 @@ void x264_param_apply_fastfirstpass( x264_param_t * );
5154 /* x264_param_apply_profile:
5155 * Applies the restrictions of the given profile.
5156 * Currently available profiles are, from most to least restrictive: */
5157-static const char * const x264_profile_names[] = { "baseline", "main", "high", 0 };
5158+static const char * const x264_profile_names[] = { "baseline", "main", "high", "high10", 0 };
5159
5160 /* (can be NULL, in which case the function will do nothing)
5161 *
5162--
51631.7.1