]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/x86/dsputil_mmx.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / x86 / dsputil_mmx.c
index 3daa09b31403170860872e8b006fca3bc226995c..c36a02722920abf5c302164e740d43312d2213df 100644 (file)
@@ -3,34 +3,34 @@
  * Copyright (c) 2000, 2001 Fabrice Bellard
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  *
- * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
- *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
  */
 
 #include "libavutil/cpu.h"
-#include "libavutil/x86_cpu.h"
+#include "libavutil/x86/asm.h"
 #include "libavcodec/dsputil.h"
 #include "libavcodec/h264dsp.h"
 #include "libavcodec/mpegvideo.h"
 #include "libavcodec/simple_idct.h"
-#include "libavcodec/ac3dec.h"
 #include "dsputil_mmx.h"
 #include "idct_xvid.h"
+#include "diracdsp_mmx.h"
 
 //#undef NDEBUG
 //#include <assert.h>
@@ -170,7 +170,7 @@ DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
 #define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
 
-#include "dsputil_mmx_rnd_template.c"
+#include "dsputil_rnd_template.c"
 
 #undef DEF
 #undef SET_RND
@@ -184,7 +184,7 @@ DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
 
-#include "dsputil_mmx_rnd_template.c"
+#include "dsputil_rnd_template.c"
 
 #undef DEF
 #undef SET_RND
@@ -199,7 +199,7 @@ DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
 #define PAVGB "pavgusb"
 #define OP_AVG PAVGB
 
-#include "dsputil_mmx_avg_template.c"
+#include "dsputil_avg_template.c"
 
 #undef DEF
 #undef PAVGB
@@ -214,7 +214,7 @@ DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
 #define PAVGB "pavgb"
 #define OP_AVG PAVGB
 
-#include "dsputil_mmx_avg_template.c"
+#include "dsputil_avg_template.c"
 
 #undef DEF
 #undef PAVGB
@@ -836,7 +836,7 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
             : "+r"(ptr)
             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
             );
-    } else {
+    } else if(w==16){
         __asm__ volatile (
             "1:                                 \n\t"
             "movd            (%0), %%mm0        \n\t"
@@ -857,6 +857,25 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
             : "+r"(ptr)
             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
             );
+    } else {
+        av_assert1(w == 4);
+        __asm__ volatile (
+            "1:                             \n\t"
+            "movd            (%0), %%mm0    \n\t"
+            "punpcklbw      %%mm0, %%mm0    \n\t"
+            "punpcklwd      %%mm0, %%mm0    \n\t"
+            "movd           %%mm0, -4(%0)   \n\t"
+            "movd      -4(%0, %2), %%mm1    \n\t"
+            "punpcklbw      %%mm1, %%mm1    \n\t"
+            "punpckhwd      %%mm1, %%mm1    \n\t"
+            "punpckhdq      %%mm1, %%mm1    \n\t"
+            "movd           %%mm1, (%0, %2) \n\t"
+            "add               %1, %0       \n\t"
+            "cmp               %3, %0       \n\t"
+            "jb                1b           \n\t"
+            : "+r"(ptr)
+            : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
+            );
     }
 
     /* top and bottom (and hopefully also the corners) */
@@ -2081,7 +2100,7 @@ PREFETCH(prefetch_3dnow, prefetch)
 
 #endif /* HAVE_INLINE_ASM */
 
-#include "h264_qpel_mmx.c"
+#include "h264_qpel.c"
 
 void ff_put_h264_chroma_mc8_mmx_rnd  (uint8_t *dst, uint8_t *src,
                                       int stride, int h, int x, int y);
@@ -2162,32 +2181,115 @@ void ff_avg_vc1_mspel_mc00_mmx2(uint8_t *dst, const uint8_t *src,
     avg_pixels8_mmx2(dst, src, stride, 8);
 }
 
+/* only used in VP3/5/6 */
+static void put_vp_no_rnd_pixels8_l2_mmx(uint8_t *dst, const uint8_t *a, const uint8_t *b, int stride, int h)
+{
+//    START_TIMER
+    MOVQ_BFE(mm6);
+    __asm__ volatile(
+        "1:                             \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        "movq   (%2), %%mm1             \n\t"
+        "movq   (%1,%4), %%mm2          \n\t"
+        "movq   (%2,%4), %%mm3          \n\t"
+        PAVGBP_MMX_NO_RND(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%3)             \n\t"
+        "movq   %%mm5, (%3,%4)          \n\t"
+
+        "movq   (%1,%4,2), %%mm0        \n\t"
+        "movq   (%2,%4,2), %%mm1        \n\t"
+        "movq   (%1,%5), %%mm2          \n\t"
+        "movq   (%2,%5), %%mm3          \n\t"
+        "lea    (%1,%4,4), %1           \n\t"
+        "lea    (%2,%4,4), %2           \n\t"
+        PAVGBP_MMX_NO_RND(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%3,%4,2)        \n\t"
+        "movq   %%mm5, (%3,%5)          \n\t"
+        "lea    (%3,%4,4), %3           \n\t"
+        "subl   $4, %0                  \n\t"
+        "jnz    1b                      \n\t"
+        :"+r"(h), "+r"(a), "+r"(b), "+r"(dst)
+        :"r"((x86_reg)stride), "r"((x86_reg)3L*stride)
+        :"memory");
+//    STOP_TIMER("put_vp_no_rnd_pixels8_l2_mmx")
+}
+static void put_vp_no_rnd_pixels16_l2_mmx(uint8_t *dst, const uint8_t *a, const uint8_t *b, int stride, int h)
+{
+    put_vp_no_rnd_pixels8_l2_mmx(dst, a, b, stride, h);
+    put_vp_no_rnd_pixels8_l2_mmx(dst+8, a+8, b+8, stride, h);
+}
+
+#if CONFIG_DIRAC_DECODER
+#define DIRAC_PIXOP(OPNAME, EXT)\
+void ff_ ## OPNAME ## _dirac_pixels8_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
+{\
+    OPNAME ## _pixels8_ ## EXT(dst, src[0], stride, h);\
+}\
+void ff_ ## OPNAME ## _dirac_pixels16_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
+{\
+    OPNAME ## _pixels16_ ## EXT(dst, src[0], stride, h);\
+}\
+void ff_ ## OPNAME ## _dirac_pixels32_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
+{\
+    OPNAME ## _pixels16_ ## EXT(dst   , src[0]   , stride, h);\
+    OPNAME ## _pixels16_ ## EXT(dst+16, src[0]+16, stride, h);\
+}
+
+DIRAC_PIXOP(put, mmx)
+DIRAC_PIXOP(avg, mmx)
+DIRAC_PIXOP(avg, mmx2)
+
+void ff_put_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
+{
+    put_pixels16_sse2(dst, src[0], stride, h);
+}
+void ff_avg_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
+{
+    avg_pixels16_sse2(dst, src[0], stride, h);
+}
+void ff_put_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
+{
+    put_pixels16_sse2(dst   , src[0]   , stride, h);
+    put_pixels16_sse2(dst+16, src[0]+16, stride, h);
+}
+void ff_avg_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
+{
+    avg_pixels16_sse2(dst   , src[0]   , stride, h);
+    avg_pixels16_sse2(dst+16, src[0]+16, stride, h);
+}
+#endif
+
 /* XXX: Those functions should be suppressed ASAP when all IDCTs are
  * converted. */
-
-static void ff_idct_xvid_mmx_put(uint8_t *dest, int line_size, DCTELEM *block)
+#if CONFIG_GPL
+static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size,
+                                    DCTELEM *block)
 {
-    ff_idct_xvid_mmx(block);
+    ff_mmx_idct(block);
     ff_put_pixels_clamped_mmx(block, dest, line_size);
 }
 
-static void ff_idct_xvid_mmx_add(uint8_t *dest, int line_size, DCTELEM *block)
+static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size,
+                                    DCTELEM *block)
 {
-    ff_idct_xvid_mmx(block);
+    ff_mmx_idct(block);
     ff_add_pixels_clamped_mmx(block, dest, line_size);
 }
 
-static void ff_idct_xvid_mmx2_put(uint8_t *dest, int line_size, DCTELEM *block)
+static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size,
+                                     DCTELEM *block)
 {
-    ff_idct_xvid_mmx2(block);
+    ff_mmxext_idct(block);
     ff_put_pixels_clamped_mmx(block, dest, line_size);
 }
 
-static void ff_idct_xvid_mmx2_add(uint8_t *dest, int line_size, DCTELEM *block)
+static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size,
+                                     DCTELEM *block)
 {
-    ff_idct_xvid_mmx2(block);
+    ff_mmxext_idct(block);
     ff_add_pixels_clamped_mmx(block, dest, line_size);
 }
+#endif
 
 static void vorbis_inverse_coupling_3dnow(float *mag, float *ang, int blocksize)
 {
@@ -2248,115 +2350,6 @@ static void vorbis_inverse_coupling_sse(float *mag, float *ang, int blocksize)
     }
 }
 
-#define IF1(x) x
-#define IF0(x)
-
-#define MIX5(mono, stereo)                                      \
-    __asm__ volatile (                                          \
-        "movss           0(%2), %%xmm5          \n"             \
-        "movss           8(%2), %%xmm6          \n"             \
-        "movss          24(%2), %%xmm7          \n"             \
-        "shufps     $0, %%xmm5, %%xmm5          \n"             \
-        "shufps     $0, %%xmm6, %%xmm6          \n"             \
-        "shufps     $0, %%xmm7, %%xmm7          \n"             \
-        "1:                                     \n"             \
-        "movaps       (%0, %1), %%xmm0          \n"             \
-        "movaps  0x400(%0, %1), %%xmm1          \n"             \
-        "movaps  0x800(%0, %1), %%xmm2          \n"             \
-        "movaps  0xc00(%0, %1), %%xmm3          \n"             \
-        "movaps 0x1000(%0, %1), %%xmm4          \n"             \
-        "mulps          %%xmm5, %%xmm0          \n"             \
-        "mulps          %%xmm6, %%xmm1          \n"             \
-        "mulps          %%xmm5, %%xmm2          \n"             \
-        "mulps          %%xmm7, %%xmm3          \n"             \
-        "mulps          %%xmm7, %%xmm4          \n"             \
- stereo("addps          %%xmm1, %%xmm0          \n")            \
-        "addps          %%xmm1, %%xmm2          \n"             \
-        "addps          %%xmm3, %%xmm0          \n"             \
-        "addps          %%xmm4, %%xmm2          \n"             \
-   mono("addps          %%xmm2, %%xmm0          \n")            \
-        "movaps         %%xmm0, (%0, %1)        \n"             \
- stereo("movaps         %%xmm2, 0x400(%0, %1)   \n")            \
-        "add               $16, %0              \n"             \
-        "jl                 1b                  \n"             \
-        : "+&r"(i)                                              \
-        : "r"(samples[0] + len), "r"(matrix)                    \
-        : XMM_CLOBBERS("%xmm0", "%xmm1", "%xmm2", "%xmm3",      \
-                      "%xmm4", "%xmm5", "%xmm6", "%xmm7",)      \
-         "memory"                                               \
-    );
-
-#define MIX_MISC(stereo)                                        \
-    __asm__ volatile (                                          \
-        "1:                                 \n"                 \
-        "movaps     (%3, %0), %%xmm0        \n"                 \
- stereo("movaps       %%xmm0, %%xmm1        \n")                \
-        "mulps        %%xmm4, %%xmm0        \n"                 \
- stereo("mulps        %%xmm5, %%xmm1        \n")                \
-        "lea    1024(%3, %0), %1            \n"                 \
-        "mov              %5, %2            \n"                 \
-        "2:                                 \n"                 \
-        "movaps         (%1), %%xmm2        \n"                 \
- stereo("movaps       %%xmm2, %%xmm3        \n")                \
-        "mulps      (%4, %2), %%xmm2        \n"                 \
- stereo("mulps    16(%4, %2), %%xmm3        \n")                \
-        "addps        %%xmm2, %%xmm0        \n"                 \
- stereo("addps        %%xmm3, %%xmm1        \n")                \
-        "add           $1024, %1            \n"                 \
-        "add             $32, %2            \n"                 \
-        "jl               2b                \n"                 \
-        "movaps       %%xmm0,     (%3, %0)  \n"                 \
- stereo("movaps       %%xmm1, 1024(%3, %0)  \n")                \
-        "add             $16, %0            \n"                 \
-        "jl               1b                \n"                 \
-        : "+&r"(i), "=&r"(j), "=&r"(k)                          \
-        : "r"(samples[0] + len), "r"(matrix_simd + in_ch),      \
-          "g"((intptr_t) - 32 * (in_ch - 1))                    \
-        : "memory"                                              \
-    );
-
-static void ac3_downmix_sse(float (*samples)[256], float (*matrix)[2],
-                            int out_ch, int in_ch, int len)
-{
-    int (*matrix_cmp)[2] = (int(*)[2])matrix;
-    intptr_t i, j, k;
-
-    i = -len * sizeof(float);
-    if (in_ch == 5 && out_ch == 2 &&
-        !(matrix_cmp[0][1] | matrix_cmp[2][0]   |
-          matrix_cmp[3][1] | matrix_cmp[4][0]   |
-          (matrix_cmp[1][0] ^ matrix_cmp[1][1]) |
-          (matrix_cmp[0][0] ^ matrix_cmp[2][1]))) {
-        MIX5(IF0, IF1);
-    } else if (in_ch == 5 && out_ch == 1 &&
-               matrix_cmp[0][0] == matrix_cmp[2][0] &&
-               matrix_cmp[3][0] == matrix_cmp[4][0]) {
-        MIX5(IF1, IF0);
-    } else {
-        DECLARE_ALIGNED(16, float, matrix_simd)[AC3_MAX_CHANNELS][2][4];
-        j = 2 * in_ch * sizeof(float);
-        __asm__ volatile (
-            "1:                                 \n"
-            "sub             $8, %0             \n"
-            "movss     (%2, %0), %%xmm4         \n"
-            "movss    4(%2, %0), %%xmm5         \n"
-            "shufps          $0, %%xmm4, %%xmm4 \n"
-            "shufps          $0, %%xmm5, %%xmm5 \n"
-            "movaps      %%xmm4,   (%1, %0, 4)  \n"
-            "movaps      %%xmm5, 16(%1, %0, 4)  \n"
-            "jg              1b                 \n"
-            : "+&r"(j)
-            : "r"(matrix_simd), "r"(matrix)
-            : "memory"
-        );
-        if (out_ch == 2) {
-            MIX_MISC(IF1);
-        } else {
-            MIX_MISC(IF0);
-        }
-    }
-}
-
 #if HAVE_6REGS
 static void vector_fmul_window_3dnowext(float *dst, const float *src0,
                                         const float *src1, const float *win,
@@ -2597,6 +2590,9 @@ static void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx, int mm_flags)
 
     c->add_bytes = add_bytes_mmx;
 
+    c->put_no_rnd_pixels_l2[0]= put_vp_no_rnd_pixels16_l2_mmx;
+    c->put_no_rnd_pixels_l2[1]= put_vp_no_rnd_pixels8_l2_mmx;
+
     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
         c->h263_v_loop_filter = h263_v_loop_filter_mmx;
         c->h263_h_loop_filter = h263_h_loop_filter_mmx;
@@ -2656,8 +2652,8 @@ static void dsputil_init_mmx2(DSPContext *c, AVCodecContext *avctx,
         }
     }
 
-    if (CONFIG_VP3_DECODER && (avctx->codec_id == CODEC_ID_VP3 ||
-                               avctx->codec_id == CODEC_ID_THEORA)) {
+    if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
+                               avctx->codec_id == AV_CODEC_ID_THEORA)) {
         c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_mmx2;
         c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_mmx2;
     }
@@ -2692,7 +2688,7 @@ static void dsputil_init_mmx2(DSPContext *c, AVCodecContext *avctx,
 #endif
             SET_QPEL_FUNCS(put_h264_qpel, 2, 4,  10_mmxext, ff_);
             SET_QPEL_FUNCS(avg_h264_qpel, 2, 4,  10_mmxext, ff_);
-#endif
+#endif /* HAVE_YASM */
         }
 
 #if HAVE_INLINE_ASM
@@ -2717,7 +2713,9 @@ static void dsputil_init_mmx2(DSPContext *c, AVCodecContext *avctx,
         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_10_mmx2;
     }
 
-    c->add_hfyu_median_prediction   = ff_add_hfyu_median_prediction_mmx2;
+    /* slower than cmov version on AMD */
+    if (!(mm_flags & AV_CPU_FLAG_3DNOW))
+        c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmx2;
 
     c->scalarproduct_int16          = ff_scalarproduct_int16_mmx2;
     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmx2;
@@ -2727,7 +2725,7 @@ static void dsputil_init_mmx2(DSPContext *c, AVCodecContext *avctx,
     } else {
         c->apply_window_int16 = ff_apply_window_int16_mmxext;
     }
-#endif
+#endif /* HAVE_YASM */
 }
 
 static void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
@@ -2764,8 +2762,8 @@ static void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
         }
     }
 
-    if (CONFIG_VP3_DECODER && (avctx->codec_id == CODEC_ID_VP3 ||
-                               avctx->codec_id == CODEC_ID_THEORA)) {
+    if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
+                               avctx->codec_id == AV_CODEC_ID_THEORA)) {
         c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_3dnow;
         c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_3dnow;
     }
@@ -2794,11 +2792,6 @@ static void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
     }
 
     c->vorbis_inverse_coupling = vorbis_inverse_coupling_3dnow;
-
-#if HAVE_7REGS
-    if (mm_flags & AV_CPU_FLAG_CMOV)
-        c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
-#endif
 #endif /* HAVE_INLINE_ASM */
 
 #if HAVE_YASM
@@ -2806,13 +2799,13 @@ static void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
         c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_3dnow_rnd;
         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_3dnow;
     }
-#endif
+#endif /* HAVE_YASM */
 }
 
 static void dsputil_init_3dnowext(DSPContext *c, AVCodecContext *avctx,
                                   int mm_flags)
 {
-#if HAVE_6REGS && HAVE_INLINE_ASM
+#if HAVE_AMD3DNOWEXT_INLINE && HAVE_6REGS
     c->vector_fmul_window  = vector_fmul_window_3dnowext;
 #endif
 }
@@ -2831,7 +2824,6 @@ static void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx, int mm_flags)
     }
 
     c->vorbis_inverse_coupling = vorbis_inverse_coupling_sse;
-    c->ac3_downmix             = ac3_downmix_sse;
 
 #if HAVE_6REGS
     c->vector_fmul_window = vector_fmul_window_sse;
@@ -2852,7 +2844,7 @@ static void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx, int mm_flags)
 #if HAVE_INLINE_ASM
     c->gmc = gmc_sse;
 #endif
-#endif
+#endif /* HAVE_YASM */
 }
 
 static void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
@@ -2920,17 +2912,16 @@ static void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
         c->apply_window_int16 = ff_apply_window_int16_sse2;
     }
     c->bswap_buf = ff_bswap32_buf_sse2;
-#endif
+#endif /* HAVE_YASM */
 }
 
 static void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
                                int mm_flags)
 {
-#if HAVE_SSSE3
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
     const int bit_depth      = avctx->bits_per_raw_sample;
 
-#if HAVE_INLINE_ASM
+#if HAVE_SSSE3_INLINE
     if (!high_bit_depth && CONFIG_H264QPEL) {
         H264_QPEL_FUNCS(1, 0, ssse3);
         H264_QPEL_FUNCS(1, 1, ssse3);
@@ -2945,8 +2936,9 @@ static void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
         H264_QPEL_FUNCS(3, 2, ssse3);
         H264_QPEL_FUNCS(3, 3, ssse3);
     }
-#endif /* HAVE_INLINE_ASM */
-#if HAVE_YASM
+#endif /* HAVE_SSSE3_INLINE */
+
+#if HAVE_SSSE3_EXTERNAL
     if (bit_depth == 10 && CONFIG_H264QPEL) {
         H264_QPEL_FUNCS_10(1, 0, ssse3_cache64);
         H264_QPEL_FUNCS_10(2, 0, ssse3_cache64);
@@ -2969,21 +2961,20 @@ static void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
     if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) // cachesplit
         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
     c->bswap_buf = ff_bswap32_buf_ssse3;
-#endif
-#endif
+#endif /* HAVE_SSSE3_EXTERNAL */
 }
 
 static void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
                               int mm_flags)
 {
-#if HAVE_YASM
+#if HAVE_SSE4_EXTERNAL
     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
-#endif
+#endif /* HAVE_SSE4_EXTERNAL */
 }
 
 static void dsputil_init_avx(DSPContext *c, AVCodecContext *avctx, int mm_flags)
 {
-#if HAVE_AVX && HAVE_YASM
+#if HAVE_AVX_EXTERNAL
     const int bit_depth = avctx->bits_per_raw_sample;
 
     if (bit_depth == 10) {
@@ -3003,25 +2994,41 @@ static void dsputil_init_avx(DSPContext *c, AVCodecContext *avctx, int mm_flags)
     c->butterflies_float_interleave = ff_butterflies_float_interleave_avx;
     c->vector_fmul_reverse = ff_vector_fmul_reverse_avx;
     c->vector_fmul_add = ff_vector_fmul_add_avx;
-#endif
+#endif /* HAVE_AVX_EXTERNAL */
 }
 
 void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
 {
     int mm_flags = av_get_cpu_flags();
 
+#if HAVE_7REGS && HAVE_INLINE_ASM
+    if (mm_flags & AV_CPU_FLAG_CMOV)
+        c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
+#endif
+
     if (mm_flags & AV_CPU_FLAG_MMX) {
 #if HAVE_INLINE_ASM
         const int idct_algo = avctx->idct_algo;
 
-        if (avctx->bits_per_raw_sample <= 8) {
+        if (avctx->lowres == 0 && avctx->bits_per_raw_sample <= 8) {
             if (idct_algo == FF_IDCT_AUTO || idct_algo == FF_IDCT_SIMPLEMMX) {
                 c->idct_put              = ff_simple_idct_put_mmx;
                 c->idct_add              = ff_simple_idct_add_mmx;
                 c->idct                  = ff_simple_idct_mmx;
                 c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
-            } else if (idct_algo == FF_IDCT_CAVS) {
-                    c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
+#if CONFIG_GPL
+            } else if (idct_algo == FF_IDCT_LIBMPEG2MMX) {
+                if (mm_flags & AV_CPU_FLAG_MMX2) {
+                    c->idct_put = ff_libmpeg2mmx2_idct_put;
+                    c->idct_add = ff_libmpeg2mmx2_idct_add;
+                    c->idct     = ff_mmxext_idct;
+                } else {
+                    c->idct_put = ff_libmpeg2mmx_idct_put;
+                    c->idct_add = ff_libmpeg2mmx_idct_add;
+                    c->idct     = ff_mmx_idct;
+                }
+                c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
+#endif
             } else if (idct_algo == FF_IDCT_XVIDMMX) {
                 if (mm_flags & AV_CPU_FLAG_SSE2) {
                     c->idct_put              = ff_idct_xvid_sse2_put;
@@ -3047,13 +3054,13 @@ void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
     if (mm_flags & AV_CPU_FLAG_MMXEXT)
         dsputil_init_mmx2(c, avctx, mm_flags);
 
-    if (mm_flags & AV_CPU_FLAG_3DNOW && HAVE_AMD3DNOW)
+    if (mm_flags & AV_CPU_FLAG_3DNOW)
         dsputil_init_3dnow(c, avctx, mm_flags);
 
-    if (mm_flags & AV_CPU_FLAG_3DNOWEXT && HAVE_AMD3DNOWEXT)
+    if (mm_flags & AV_CPU_FLAG_3DNOWEXT)
         dsputil_init_3dnowext(c, avctx, mm_flags);
 
-    if (mm_flags & AV_CPU_FLAG_SSE && HAVE_SSE)
+    if (mm_flags & AV_CPU_FLAG_SSE)
         dsputil_init_sse(c, avctx, mm_flags);
 
     if (mm_flags & AV_CPU_FLAG_SSE2)
@@ -3062,7 +3069,7 @@ void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
     if (mm_flags & AV_CPU_FLAG_SSSE3)
         dsputil_init_ssse3(c, avctx, mm_flags);
 
-    if (mm_flags & AV_CPU_FLAG_SSE4 && HAVE_SSE)
+    if (mm_flags & AV_CPU_FLAG_SSE4)
         dsputil_init_sse4(c, avctx, mm_flags);
 
     if (mm_flags & AV_CPU_FLAG_AVX)