K-means weightp
[x264-7mod.git] / common / bitstream.c
blobcc7630009abf9e601a0923ea1087bb762b1ceb89
1 /*****************************************************************************
2 * bitstream.c: bitstream writing
3 *****************************************************************************
4 * Copyright (C) 2003-2017 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Fiona Glaser <fiona@x264.com>
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.
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.
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.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
27 #include "common.h"
29 static uint8_t *x264_nal_escape_c( uint8_t *dst, uint8_t *src, uint8_t *end )
31 if( src < end ) *dst++ = *src++;
32 if( src < end ) *dst++ = *src++;
33 while( src < end )
35 if( src[0] <= 0x03 && !dst[-2] && !dst[-1] )
36 *dst++ = 0x03;
37 *dst++ = *src++;
39 return dst;
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_avx2( 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_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_rd_internal_avx512 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
50 void x264_cabac_block_residual_8x8_rd_internal_sse2 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
51 void x264_cabac_block_residual_8x8_rd_internal_lzcnt ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
52 void x264_cabac_block_residual_8x8_rd_internal_ssse3 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
53 void x264_cabac_block_residual_8x8_rd_internal_ssse3_lzcnt( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
54 void x264_cabac_block_residual_8x8_rd_internal_avx512 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
55 void x264_cabac_block_residual_internal_sse2 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
56 void x264_cabac_block_residual_internal_lzcnt ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
57 void x264_cabac_block_residual_internal_avx2 ( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
58 void x264_cabac_block_residual_internal_avx512( dctcoef *l, int b_interlaced, intptr_t ctx_block_cat, x264_cabac_t *cb );
60 uint8_t *x264_nal_escape_neon( uint8_t *dst, uint8_t *src, uint8_t *end );
62 /****************************************************************************
63 * x264_nal_encode:
64 ****************************************************************************/
65 void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal )
67 uint8_t *src = nal->p_payload;
68 uint8_t *end = nal->p_payload + nal->i_payload;
69 uint8_t *orig_dst = dst;
71 if( h->param.b_annexb )
73 if( nal->b_long_startcode )
74 *dst++ = 0x00;
75 *dst++ = 0x00;
76 *dst++ = 0x00;
77 *dst++ = 0x01;
79 else /* save room for size later */
80 dst += 4;
82 /* nal header */
83 *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
85 dst = h->bsf.nal_escape( dst, src, end );
86 int size = dst - orig_dst;
88 /* Apply AVC-Intra padding */
89 if( h->param.i_avcintra_class )
91 int padding = nal->i_payload + nal->i_padding + NALU_OVERHEAD - size;
92 if( padding > 0 )
94 memset( dst, 0, padding );
95 size += padding;
97 nal->i_padding = X264_MAX( padding, 0 );
100 /* Write the size header for mp4/etc */
101 if( !h->param.b_annexb )
103 /* Size doesn't include the size of the header we're writing now. */
104 int chunk_size = size - 4;
105 orig_dst[0] = chunk_size >> 24;
106 orig_dst[1] = chunk_size >> 16;
107 orig_dst[2] = chunk_size >> 8;
108 orig_dst[3] = chunk_size >> 0;
111 nal->i_payload = size;
112 nal->p_payload = orig_dst;
113 x264_emms();
116 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf )
118 memset( pf, 0, sizeof(*pf) );
120 pf->nal_escape = x264_nal_escape_c;
121 #if HAVE_MMX
122 #if ARCH_X86_64 && !defined( __MACH__ )
123 pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_sse2;
124 pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_sse2;
125 pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_sse2;
126 #endif
128 if( cpu&X264_CPU_MMX2 )
129 pf->nal_escape = x264_nal_escape_mmx2;
130 if( cpu&X264_CPU_SSE2 )
132 if( cpu&X264_CPU_SSE2_IS_FAST )
133 pf->nal_escape = x264_nal_escape_sse2;
135 #if ARCH_X86_64 && !defined( __MACH__ )
136 if( cpu&X264_CPU_LZCNT )
138 pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_lzcnt;
139 pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_lzcnt;
140 pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_lzcnt;
143 if( cpu&X264_CPU_SSSE3 )
145 pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3;
146 pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3;
147 if( cpu&X264_CPU_LZCNT )
149 pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3_lzcnt;
150 pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3_lzcnt;
154 if( cpu&X264_CPU_AVX2 )
156 pf->nal_escape = x264_nal_escape_avx2;
157 pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_avx2;
160 if( cpu&X264_CPU_AVX512 )
162 pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_avx512;
163 pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_avx512;
164 pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_avx512;
166 #endif
167 #endif
168 #if HAVE_ARMV6
169 if( cpu&X264_CPU_NEON )
170 pf->nal_escape = x264_nal_escape_neon;
171 #endif
172 #if ARCH_AARCH64
173 if( cpu&X264_CPU_NEON )
174 pf->nal_escape = x264_nal_escape_neon;
175 #endif