]> git.sesse.net Git - x264/blob - common/bitstream.c
x86-64: cabac_block_residual assembly
[x264] / common / bitstream.c
1 /*****************************************************************************
2  * bitstream.c: bitstream writing
3  *****************************************************************************
4  * Copyright (C) 2003-2013 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Fiona Glaser <fiona@x264.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "common.h"
28
29 static uint8_t *x264_nal_escape_c( uint8_t *dst, uint8_t *src, uint8_t *end )
30 {
31     if( src < end ) *dst++ = *src++;
32     if( src < end ) *dst++ = *src++;
33     while( src < end )
34     {
35         if( src[0] <= 0x03 && !dst[-2] && !dst[-1] )
36             *dst++ = 0x03;
37         *dst++ = *src++;
38     }
39     return dst;
40 }
41
42 uint8_t *x264_nal_escape_mmx2( uint8_t *dst, uint8_t *src, uint8_t *end );
43 uint8_t *x264_nal_escape_sse2( uint8_t *dst, uint8_t *src, uint8_t *end );
44 uint8_t *x264_nal_escape_avx( uint8_t *dst, uint8_t *src, uint8_t *end );
45 void x264_cabac_block_residual_rd_internal_sse2       ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
46 void x264_cabac_block_residual_rd_internal_sse2_lzcnt ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
47 void x264_cabac_block_residual_rd_internal_ssse3      ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
48 void x264_cabac_block_residual_rd_internal_ssse3_lzcnt( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
49 void x264_cabac_block_residual_8x8_rd_internal_sse2       ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
50 void x264_cabac_block_residual_8x8_rd_internal_sse2_lzcnt ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
51 void x264_cabac_block_residual_8x8_rd_internal_ssse3      ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
52 void x264_cabac_block_residual_8x8_rd_internal_ssse3_lzcnt( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
53 void x264_cabac_block_residual_internal_sse2       ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
54 void x264_cabac_block_residual_internal_sse2_lzcnt ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
55
56 /****************************************************************************
57  * x264_nal_encode:
58  ****************************************************************************/
59 void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal )
60 {
61     uint8_t *src = nal->p_payload;
62     uint8_t *end = nal->p_payload + nal->i_payload;
63     uint8_t *orig_dst = dst;
64
65     if( h->param.b_annexb )
66     {
67         if( nal->b_long_startcode )
68             *dst++ = 0x00;
69         *dst++ = 0x00;
70         *dst++ = 0x00;
71         *dst++ = 0x01;
72     }
73     else /* save room for size later */
74         dst += 4;
75
76     /* nal header */
77     *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
78
79     dst = h->bsf.nal_escape( dst, src, end );
80     int size = (dst - orig_dst) - 4;
81
82     /* Write the size header for mp4/etc */
83     if( !h->param.b_annexb )
84     {
85         /* Size doesn't include the size of the header we're writing now. */
86         orig_dst[0] = size>>24;
87         orig_dst[1] = size>>16;
88         orig_dst[2] = size>> 8;
89         orig_dst[3] = size>> 0;
90     }
91
92     nal->i_payload = size+4;
93     nal->p_payload = orig_dst;
94     x264_emms();
95 }
96
97 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf )
98 {
99     memset( pf, 0, sizeof(*pf) );
100
101     pf->nal_escape = x264_nal_escape_c;
102 #if HAVE_MMX
103 #if ARCH_X86_64
104     pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_sse2;
105     pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_sse2;
106     pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_sse2;
107 #endif
108
109     if( cpu&X264_CPU_MMX2 )
110         pf->nal_escape = x264_nal_escape_mmx2;
111     if( cpu&X264_CPU_SSE2 )
112     {
113 #if ARCH_X86_64
114         if( cpu&X264_CPU_LZCNT )
115         {
116             pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_sse2_lzcnt;
117             pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_sse2_lzcnt;
118             pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_sse2_lzcnt;
119         }
120 #endif
121         if( cpu&X264_CPU_SSE2_IS_FAST )
122             pf->nal_escape = x264_nal_escape_sse2;
123     }
124 #if ARCH_X86_64
125     if( cpu&X264_CPU_SSSE3 )
126     {
127         pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3;
128         pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3;
129         if( cpu&X264_CPU_LZCNT )
130         {
131             pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3_lzcnt;
132             pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3_lzcnt;
133         }
134     }
135 #endif
136     if( cpu&X264_CPU_AVX )
137         pf->nal_escape = x264_nal_escape_avx;
138 #endif
139 }