bitstream.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*****************************************************************************
  2. * bitstream.c: bitstream writing
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 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. #include "common.h"
  27. static uint8_t *nal_escape_c( uint8_t *dst, uint8_t *src, uint8_t *end )
  28. {
  29. if( src < end ) *dst++ = *src++;
  30. if( src < end ) *dst++ = *src++;
  31. while( src < end )
  32. {
  33. if( src[0] <= 0x03 && !dst[-2] && !dst[-1] )
  34. *dst++ = 0x03;
  35. *dst++ = *src++;
  36. }
  37. return dst;
  38. }
  39. #if HAVE_MMX
  40. #include "x86/bitstream.h"
  41. #endif
  42. #if HAVE_ARMV6
  43. #include "arm/bitstream.h"
  44. #endif
  45. #if ARCH_AARCH64
  46. #include "aarch64/bitstream.h"
  47. #endif
  48. /****************************************************************************
  49. * x264_nal_encode:
  50. ****************************************************************************/
  51. void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal )
  52. {
  53. uint8_t *src = nal->p_payload;
  54. uint8_t *end = nal->p_payload + nal->i_payload;
  55. uint8_t *orig_dst = dst;
  56. if( h->param.b_annexb )
  57. {
  58. if( nal->b_long_startcode )
  59. *dst++ = 0x00;
  60. *dst++ = 0x00;
  61. *dst++ = 0x00;
  62. *dst++ = 0x01;
  63. }
  64. else /* save room for size later */
  65. dst += 4;
  66. /* nal header */
  67. *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
  68. dst = h->bsf.nal_escape( dst, src, end );
  69. int size = dst - orig_dst;
  70. /* Apply AVC-Intra padding */
  71. if( h->param.i_avcintra_class )
  72. {
  73. int padding = nal->i_payload + nal->i_padding + NALU_OVERHEAD - size;
  74. if( padding > 0 )
  75. {
  76. memset( dst, 0, padding );
  77. size += padding;
  78. }
  79. nal->i_padding = X264_MAX( padding, 0 );
  80. }
  81. /* Write the size header for mp4/etc */
  82. if( !h->param.b_annexb )
  83. {
  84. /* Size doesn't include the size of the header we're writing now. */
  85. int chunk_size = size - 4;
  86. orig_dst[0] = chunk_size >> 24;
  87. orig_dst[1] = chunk_size >> 16;
  88. orig_dst[2] = chunk_size >> 8;
  89. orig_dst[3] = chunk_size >> 0;
  90. }
  91. nal->i_payload = size;
  92. nal->p_payload = orig_dst;
  93. x264_emms();
  94. }
  95. void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf )
  96. {
  97. memset( pf, 0, sizeof(*pf) );
  98. pf->nal_escape = nal_escape_c;
  99. #if HAVE_MMX
  100. #if ARCH_X86_64 && !defined( __MACH__ )
  101. pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_sse2;
  102. pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_sse2;
  103. pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_sse2;
  104. #endif
  105. if( cpu&X264_CPU_MMX2 )
  106. pf->nal_escape = x264_nal_escape_mmx2;
  107. if( cpu&X264_CPU_SSE2 )
  108. {
  109. if( cpu&X264_CPU_SSE2_IS_FAST )
  110. pf->nal_escape = x264_nal_escape_sse2;
  111. }
  112. #if ARCH_X86_64 && !defined( __MACH__ )
  113. if( cpu&X264_CPU_LZCNT )
  114. {
  115. pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_lzcnt;
  116. pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_lzcnt;
  117. pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_lzcnt;
  118. }
  119. if( cpu&X264_CPU_SSSE3 )
  120. {
  121. pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3;
  122. pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3;
  123. if( cpu&X264_CPU_LZCNT )
  124. {
  125. pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_ssse3_lzcnt;
  126. pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_ssse3_lzcnt;
  127. }
  128. }
  129. if( cpu&X264_CPU_AVX2 )
  130. {
  131. pf->nal_escape = x264_nal_escape_avx2;
  132. pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_avx2;
  133. }
  134. if( cpu&X264_CPU_AVX512 )
  135. {
  136. pf->cabac_block_residual_internal = x264_cabac_block_residual_internal_avx512;
  137. pf->cabac_block_residual_rd_internal = x264_cabac_block_residual_rd_internal_avx512;
  138. pf->cabac_block_residual_8x8_rd_internal = x264_cabac_block_residual_8x8_rd_internal_avx512;
  139. }
  140. #endif
  141. #endif
  142. #if HAVE_ARMV6
  143. if( cpu&X264_CPU_NEON )
  144. pf->nal_escape = x264_nal_escape_neon;
  145. #endif
  146. #if ARCH_AARCH64
  147. if( cpu&X264_CPU_NEON )
  148. pf->nal_escape = x264_nal_escape_neon;
  149. #endif
  150. }