2
0

h264_cabac.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG-4 part10 codec.
  24. * non-SIMD x86-specific optimizations for H.264
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include <stddef.h>
  28. #include "libavcodec/cabac.h"
  29. #include "cabac.h"
  30. #if HAVE_INLINE_ASM
  31. #if ARCH_X86_64
  32. #define REG64 "r"
  33. #else
  34. #define REG64 "m"
  35. #endif
  36. //FIXME use some macros to avoid duplicating get_cabac (cannot be done yet
  37. //as that would make optimization work hard)
  38. #if HAVE_7REGS && !BROKEN_COMPILER
  39. #define decode_significance decode_significance_x86
  40. static int decode_significance_x86(CABACContext *c, int max_coeff,
  41. uint8_t *significant_coeff_ctx_base,
  42. int *index, x86_reg last_off){
  43. void *end= significant_coeff_ctx_base + max_coeff - 1;
  44. int minusstart= -(intptr_t)significant_coeff_ctx_base;
  45. int minusindex= 4-(intptr_t)index;
  46. int bit;
  47. x86_reg coeff_count;
  48. #ifdef BROKEN_RELOCATIONS
  49. void *tables;
  50. __asm__ volatile(
  51. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  52. : "=&r"(tables)
  53. : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
  54. );
  55. #endif
  56. __asm__ volatile(
  57. "3: \n\t"
  58. BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
  59. "%5", "%q5", "%k0", "%b0",
  60. "%c11(%6)", "%c12(%6)",
  61. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  62. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  63. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  64. "%13")
  65. "test $1, %4 \n\t"
  66. " jz 4f \n\t"
  67. "add %10, %1 \n\t"
  68. BRANCHLESS_GET_CABAC("%4", "%q4", "(%1)", "%3", "%w3",
  69. "%5", "%q5", "%k0", "%b0",
  70. "%c11(%6)", "%c12(%6)",
  71. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  72. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  73. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  74. "%13")
  75. "sub %10, %1 \n\t"
  76. "mov %2, %0 \n\t"
  77. "movl %7, %%ecx \n\t"
  78. "add %1, %%"FF_REG_c" \n\t"
  79. "movl %%ecx, (%0) \n\t"
  80. "test $1, %4 \n\t"
  81. " jnz 5f \n\t"
  82. "add"FF_OPSIZE" $4, %2 \n\t"
  83. "4: \n\t"
  84. "add $1, %1 \n\t"
  85. "cmp %8, %1 \n\t"
  86. " jb 3b \n\t"
  87. "mov %2, %0 \n\t"
  88. "movl %7, %%ecx \n\t"
  89. "add %1, %%"FF_REG_c" \n\t"
  90. "movl %%ecx, (%0) \n\t"
  91. "5: \n\t"
  92. "add %9, %k0 \n\t"
  93. "shr $2, %k0 \n\t"
  94. : "=&q"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index),
  95. "+&r"(c->low), "=&r"(bit), "+&r"(c->range)
  96. : "r"(c), "m"(minusstart), "m"(end), "m"(minusindex), "m"(last_off),
  97. "i"(offsetof(CABACContext, bytestream)),
  98. "i"(offsetof(CABACContext, bytestream_end))
  99. TABLES_ARG
  100. : "%"FF_REG_c, "memory"
  101. );
  102. return coeff_count;
  103. }
  104. #define decode_significance_8x8 decode_significance_8x8_x86
  105. static int decode_significance_8x8_x86(CABACContext *c,
  106. uint8_t *significant_coeff_ctx_base,
  107. int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off){
  108. int minusindex= 4-(intptr_t)index;
  109. int bit;
  110. x86_reg coeff_count;
  111. x86_reg last=0;
  112. x86_reg state;
  113. #ifdef BROKEN_RELOCATIONS
  114. void *tables;
  115. __asm__ volatile(
  116. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  117. : "=&r"(tables)
  118. : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
  119. );
  120. #endif
  121. __asm__ volatile(
  122. "mov %1, %6 \n\t"
  123. "3: \n\t"
  124. "mov %10, %0 \n\t"
  125. "movzb (%0, %6), %6 \n\t"
  126. "add %9, %6 \n\t"
  127. BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
  128. "%5", "%q5", "%k0", "%b0",
  129. "%c12(%7)", "%c13(%7)",
  130. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  131. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  132. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  133. "%15")
  134. "mov %1, %6 \n\t"
  135. "test $1, %4 \n\t"
  136. " jz 4f \n\t"
  137. #ifdef BROKEN_RELOCATIONS
  138. "movzb %c14(%15, %q6), %6\n\t"
  139. #else
  140. "movzb "MANGLE(ff_h264_cabac_tables)"+%c14(%6), %6\n\t"
  141. #endif
  142. "add %11, %6 \n\t"
  143. BRANCHLESS_GET_CABAC("%4", "%q4", "(%6)", "%3", "%w3",
  144. "%5", "%q5", "%k0", "%b0",
  145. "%c12(%7)", "%c13(%7)",
  146. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  147. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  148. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  149. "%15")
  150. "mov %2, %0 \n\t"
  151. "mov %1, %6 \n\t"
  152. "mov %k6, (%0) \n\t"
  153. "test $1, %4 \n\t"
  154. " jnz 5f \n\t"
  155. "add"FF_OPSIZE" $4, %2 \n\t"
  156. "4: \n\t"
  157. "add $1, %6 \n\t"
  158. "mov %6, %1 \n\t"
  159. "cmp $63, %6 \n\t"
  160. " jb 3b \n\t"
  161. "mov %2, %0 \n\t"
  162. "mov %k6, (%0) \n\t"
  163. "5: \n\t"
  164. "addl %8, %k0 \n\t"
  165. "shr $2, %k0 \n\t"
  166. : "=&q"(coeff_count), "+"REG64(last), "+"REG64(index), "+&r"(c->low),
  167. "=&r"(bit), "+&r"(c->range), "=&r"(state)
  168. : "r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base),
  169. REG64(sig_off), REG64(last_coeff_ctx_base),
  170. "i"(offsetof(CABACContext, bytestream)),
  171. "i"(offsetof(CABACContext, bytestream_end)),
  172. "i"(H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET) TABLES_ARG
  173. : "%"FF_REG_c, "memory"
  174. );
  175. return coeff_count;
  176. }
  177. #endif /* HAVE_7REGS && BROKEN_COMPILER */
  178. #endif /* HAVE_INLINE_ASM */