Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with FFmpeg; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19.  */
  20.  
  21. #ifndef AVCODEC_X86_CABAC_H
  22. #define AVCODEC_X86_CABAC_H
  23.  
  24. #include "libavcodec/cabac.h"
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/x86/asm.h"
  27. #include "libavutil/internal.h"
  28. #include "config.h"
  29.  
  30. #if   (defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
  31.    || (                  !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
  32. #       define BROKEN_COMPILER 1
  33. #else
  34. #       define BROKEN_COMPILER 0
  35. #endif
  36.  
  37. #if HAVE_INLINE_ASM
  38.  
  39. #ifndef UNCHECKED_BITSTREAM_READER
  40. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  41. #endif
  42.  
  43. #if UNCHECKED_BITSTREAM_READER
  44. #define END_CHECK(end) ""
  45. #else
  46. #define END_CHECK(end) \
  47.         "cmp    "end"       , %%"REG_c"                                 \n\t"\
  48.         "jge    1f                                                      \n\t"
  49. #endif
  50.  
  51. #ifdef BROKEN_RELOCATIONS
  52. #define TABLES_ARG , "r"(tables)
  53.  
  54. #if HAVE_FAST_CMOV
  55. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  56.         "cmp    "low"       , "tmp"                        \n\t"\
  57.         "cmova  %%ecx       , "range"                      \n\t"\
  58.         "sbb    %%rcx       , %%rcx                        \n\t"\
  59.         "and    %%ecx       , "tmp"                        \n\t"\
  60.         "xor    %%rcx       , "retq"                       \n\t"\
  61.         "sub    "tmp"       , "low"                        \n\t"
  62. #else /* HAVE_FAST_CMOV */
  63. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  64. /* P4 Prescott has crappy cmov,sbb,64bit shift so avoid them */ \
  65.         "sub    "low"       , "tmp"                        \n\t"\
  66.         "sar    $31         , "tmp"                        \n\t"\
  67.         "sub    %%ecx       , "range"                      \n\t"\
  68.         "and    "tmp"       , "range"                      \n\t"\
  69.         "add    %%ecx       , "range"                      \n\t"\
  70.         "shl    $17         , %%ecx                        \n\t"\
  71.         "and    "tmp"       , %%ecx                        \n\t"\
  72.         "sub    %%ecx       , "low"                        \n\t"\
  73.         "xor    "tmp"       , "ret"                        \n\t"\
  74.         "movslq "ret"       , "retq"                       \n\t"
  75. #endif /* HAVE_FAST_CMOV */
  76.  
  77. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  78.         "movzbl "statep"    , "ret"                                     \n\t"\
  79.         "mov    "range"     , "tmp"                                     \n\t"\
  80.         "and    $0xC0       , "range"                                   \n\t"\
  81.         "lea    ("ret", "range", 2), %%ecx                              \n\t"\
  82.         "movzbl "lps_off"("tables", %%rcx), "range"                     \n\t"\
  83.         "sub    "range"     , "tmp"                                     \n\t"\
  84.         "mov    "tmp"       , %%ecx                                     \n\t"\
  85.         "shl    $17         , "tmp"                                     \n\t"\
  86.         BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp)              \
  87.         "movzbl "norm_off"("tables", "rangeq"), %%ecx                   \n\t"\
  88.         "shl    %%cl        , "range"                                   \n\t"\
  89.         "movzbl "mlps_off"+128("tables", "retq"), "tmp"                 \n\t"\
  90.         "shl    %%cl        , "low"                                     \n\t"\
  91.         "mov    "tmpbyte"   , "statep"                                  \n\t"\
  92.         "test   "lowword"   , "lowword"                                 \n\t"\
  93.         "jnz    2f                                                      \n\t"\
  94.         "mov    "byte"      , %%"REG_c"                                 \n\t"\
  95.         END_CHECK(end)\
  96.         "add"OPSIZE" $2     , "byte"                                    \n\t"\
  97.         "1:                                                             \n\t"\
  98.         "movzwl (%%"REG_c") , "tmp"                                     \n\t"\
  99.         "lea    -1("low")   , %%ecx                                     \n\t"\
  100.         "xor    "low"       , %%ecx                                     \n\t"\
  101.         "shr    $15         , %%ecx                                     \n\t"\
  102.         "bswap  "tmp"                                                   \n\t"\
  103.         "shr    $15         , "tmp"                                     \n\t"\
  104.         "movzbl "norm_off"("tables", %%rcx), %%ecx                      \n\t"\
  105.         "sub    $0xFFFF     , "tmp"                                     \n\t"\
  106.         "neg    %%ecx                                                   \n\t"\
  107.         "add    $7          , %%ecx                                     \n\t"\
  108.         "shl    %%cl        , "tmp"                                     \n\t"\
  109.         "add    "tmp"       , "low"                                     \n\t"\
  110.         "2:                                                             \n\t"
  111.  
  112. #else /* BROKEN_RELOCATIONS */
  113. #define TABLES_ARG
  114. #define RIP_ARG
  115.  
  116. #if HAVE_FAST_CMOV
  117. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  118.         "mov    "tmp"       , %%ecx     \n\t"\
  119.         "shl    $17         , "tmp"     \n\t"\
  120.         "cmp    "low"       , "tmp"     \n\t"\
  121.         "cmova  %%ecx       , "range"   \n\t"\
  122.         "sbb    %%ecx       , %%ecx     \n\t"\
  123.         "and    %%ecx       , "tmp"     \n\t"\
  124.         "xor    %%ecx       , "ret"     \n\t"\
  125.         "sub    "tmp"       , "low"     \n\t"
  126. #else /* HAVE_FAST_CMOV */
  127. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  128.         "mov    "tmp"       , %%ecx     \n\t"\
  129.         "shl    $17         , "tmp"     \n\t"\
  130.         "sub    "low"       , "tmp"     \n\t"\
  131.         "sar    $31         , "tmp"     \n\t" /*lps_mask*/\
  132.         "sub    %%ecx       , "range"   \n\t" /*RangeLPS - range*/\
  133.         "and    "tmp"       , "range"   \n\t" /*(RangeLPS - range)&lps_mask*/\
  134.         "add    %%ecx       , "range"   \n\t" /*new range*/\
  135.         "shl    $17         , %%ecx     \n\t"\
  136.         "and    "tmp"       , %%ecx     \n\t"\
  137.         "sub    %%ecx       , "low"     \n\t"\
  138.         "xor    "tmp"       , "ret"     \n\t"
  139. #endif /* HAVE_FAST_CMOV */
  140.  
  141. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  142.         "movzbl "statep"    , "ret"                                     \n\t"\
  143.         "mov    "range"     , "tmp"                                     \n\t"\
  144.         "and    $0xC0       , "range"                                   \n\t"\
  145.         "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
  146.         "sub    "range"     , "tmp"                                     \n\t"\
  147.         BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)                    \
  148.         "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx    \n\t"\
  149.         "shl    %%cl        , "range"                                   \n\t"\
  150.         "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp"  \n\t"\
  151.         "shl    %%cl        , "low"                                     \n\t"\
  152.         "mov    "tmpbyte"   , "statep"                                  \n\t"\
  153.         "test   "lowword"   , "lowword"                                 \n\t"\
  154.         " jnz   2f                                                      \n\t"\
  155.         "mov    "byte"      , %%"REG_c"                                 \n\t"\
  156.         END_CHECK(end)\
  157.         "add"OPSIZE" $2     , "byte"                                    \n\t"\
  158.         "1:                                                             \n\t"\
  159.         "movzwl (%%"REG_c")     , "tmp"                                 \n\t"\
  160.         "lea    -1("low")   , %%ecx                                     \n\t"\
  161.         "xor    "low"       , %%ecx                                     \n\t"\
  162.         "shr    $15         , %%ecx                                     \n\t"\
  163.         "bswap  "tmp"                                                   \n\t"\
  164.         "shr    $15         , "tmp"                                     \n\t"\
  165.         "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
  166.         "sub    $0xFFFF     , "tmp"                                     \n\t"\
  167.         "neg    %%ecx                                                   \n\t"\
  168.         "add    $7          , %%ecx                                     \n\t"\
  169.         "shl    %%cl        , "tmp"                                     \n\t"\
  170.         "add    "tmp"       , "low"                                     \n\t"\
  171.         "2:                                                             \n\t"
  172.  
  173. #endif /* BROKEN_RELOCATIONS */
  174.  
  175. #if HAVE_7REGS && !BROKEN_COMPILER
  176. #define get_cabac_inline get_cabac_inline_x86
  177. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  178.                                                  uint8_t *const state)
  179. {
  180.     int bit, tmp;
  181. #ifdef BROKEN_RELOCATIONS
  182.     void *tables;
  183.  
  184.     __asm__ volatile(
  185.         "lea    "MANGLE(ff_h264_cabac_tables)", %0      \n\t"
  186.         : "=&r"(tables)
  187.     );
  188. #endif
  189.  
  190.     __asm__ volatile(
  191.         BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
  192.                              "%2", "%q2", "%3", "%b3",
  193.                              "%c6(%5)", "%c7(%5)",
  194.                              AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  195.                              AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  196.                              AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  197.                              "%8")
  198.         : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
  199.         : "r"(state), "r"(c),
  200.           "i"(offsetof(CABACContext, bytestream)),
  201.           "i"(offsetof(CABACContext, bytestream_end))
  202.           TABLES_ARG
  203.           ,"1"(c->low), "2"(c->range)
  204.         : "%"REG_c, "memory"
  205.     );
  206.     return bit & 1;
  207. }
  208. #endif /* HAVE_7REGS */
  209.  
  210. #if !BROKEN_COMPILER
  211. #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
  212. static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
  213. {
  214.     x86_reg tmp;
  215.     __asm__ volatile(
  216.         "movl        %c6(%2), %k1       \n\t"
  217.         "movl        %c3(%2), %%eax     \n\t"
  218.         "shl             $17, %k1       \n\t"
  219.         "add           %%eax, %%eax     \n\t"
  220.         "sub             %k1, %%eax     \n\t"
  221.         "cltd                           \n\t"
  222.         "and           %%edx, %k1       \n\t"
  223.         "add             %k1, %%eax     \n\t"
  224.         "xor           %%edx, %%ecx     \n\t"
  225.         "sub           %%edx, %%ecx     \n\t"
  226.         "test           %%ax, %%ax      \n\t"
  227.         "jnz              1f            \n\t"
  228.         "mov         %c4(%2), %1        \n\t"
  229.         "subl        $0xFFFF, %%eax     \n\t"
  230.         "movzwl         (%1), %%edx     \n\t"
  231.         "bswap         %%edx            \n\t"
  232.         "shrl            $15, %%edx     \n\t"
  233. #if UNCHECKED_BITSTREAM_READER
  234.         "add              $2, %1        \n\t"
  235.         "addl          %%edx, %%eax     \n\t"
  236.         "mov              %1, %c4(%2)   \n\t"
  237. #else
  238.         "addl          %%edx, %%eax     \n\t"
  239.         "cmp         %c5(%2), %1        \n\t"
  240.         "jge              1f            \n\t"
  241.         "add"OPSIZE"      $2, %c4(%2)   \n\t"
  242. #endif
  243.         "1:                             \n\t"
  244.         "movl          %%eax, %c3(%2)   \n\t"
  245.  
  246.         : "+c"(val), "=&r"(tmp)
  247.         : "r"(c),
  248.           "i"(offsetof(CABACContext, low)),
  249.           "i"(offsetof(CABACContext, bytestream)),
  250.           "i"(offsetof(CABACContext, bytestream_end)),
  251.           "i"(offsetof(CABACContext, range))
  252.         : "%eax", "%edx", "memory"
  253.     );
  254.     return val;
  255. }
  256.  
  257. #define get_cabac_bypass get_cabac_bypass_x86
  258. static av_always_inline int get_cabac_bypass_x86(CABACContext *c)
  259. {
  260.     x86_reg tmp;
  261.     int res;
  262.     __asm__ volatile(
  263.         "movl        %c6(%2), %k1       \n\t"
  264.         "movl        %c3(%2), %%eax     \n\t"
  265.         "shl             $17, %k1       \n\t"
  266.         "add           %%eax, %%eax     \n\t"
  267.         "sub             %k1, %%eax     \n\t"
  268.         "cltd                           \n\t"
  269.         "and           %%edx, %k1       \n\t"
  270.         "add             %k1, %%eax     \n\t"
  271.         "inc           %%edx            \n\t"
  272.         "test           %%ax, %%ax      \n\t"
  273.         "jnz              1f            \n\t"
  274.         "mov         %c4(%2), %1        \n\t"
  275.         "subl        $0xFFFF, %%eax     \n\t"
  276.         "movzwl         (%1), %%ecx     \n\t"
  277.         "bswap         %%ecx            \n\t"
  278.         "shrl            $15, %%ecx     \n\t"
  279.         "addl          %%ecx, %%eax     \n\t"
  280.         "cmp         %c5(%2), %1        \n\t"
  281.         "jge              1f            \n\t"
  282.         "add"OPSIZE"      $2, %c4(%2)   \n\t"
  283.         "1:                             \n\t"
  284.         "movl          %%eax, %c3(%2)   \n\t"
  285.  
  286.         : "=&d"(res), "=&r"(tmp)
  287.         : "r"(c),
  288.           "i"(offsetof(CABACContext, low)),
  289.           "i"(offsetof(CABACContext, bytestream)),
  290.           "i"(offsetof(CABACContext, bytestream_end)),
  291.           "i"(offsetof(CABACContext, range))
  292.         : "%eax", "%ecx", "memory"
  293.     );
  294.     return res;
  295. }
  296. #endif /* !BROKEN_COMPILER */
  297.  
  298. #endif /* HAVE_INLINE_ASM */
  299. #endif /* AVCODEC_X86_CABAC_H */
  300.