Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libmpcodecs / vf_halfpack.c
blob4a3bdb578f11a6fbecb6f48ace9592fac1b59b96
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "cpudetect.h"
10 #include "img_format.h"
11 #include "mp_image.h"
12 #include "vf.h"
13 #include "vf_scale.h"
15 #include "libswscale/swscale.h"
16 #include "fmt-conversion.h"
18 struct vf_priv_s {
19 int field;
20 struct SwsContext *ctx;
23 #if HAVE_MMX
24 static void halfpack_MMX(unsigned char *dst, unsigned char *src[3],
25 int dststride, int srcstride[3],
26 int w, int h)
28 int j;
29 unsigned char *y1, *y2, *u, *v;
30 int dstinc, yinc, uinc, vinc;
32 y1 = src[0];
33 y2 = src[0] + srcstride[0];
34 u = src[1];
35 v = src[2];
37 dstinc = dststride - 2*w;
38 yinc = 2*srcstride[0] - w;
39 uinc = srcstride[1] - w/2;
40 vinc = srcstride[2] - w/2;
42 for (h/=2; h; h--) {
43 __asm__ (
44 "pxor %%mm0, %%mm0 \n\t"
45 ASMALIGN(4)
46 "1: \n\t"
47 "movq (%0), %%mm1 \n\t"
48 "movq (%0), %%mm2 \n\t"
49 "movq (%1), %%mm3 \n\t"
50 "movq (%1), %%mm4 \n\t"
51 "punpcklbw %%mm0, %%mm1 \n\t"
52 "punpckhbw %%mm0, %%mm2 \n\t"
53 "punpcklbw %%mm0, %%mm3 \n\t"
54 "punpckhbw %%mm0, %%mm4 \n\t"
55 "paddw %%mm3, %%mm1 \n\t"
56 "paddw %%mm4, %%mm2 \n\t"
57 "psrlw $1, %%mm1 \n\t"
58 "psrlw $1, %%mm2 \n\t"
60 "movq (%2), %%mm3 \n\t"
61 "movq (%3), %%mm5 \n\t"
62 "punpcklbw %%mm0, %%mm3 \n\t"
63 "punpcklbw %%mm0, %%mm5 \n\t"
64 "movq %%mm3, %%mm4 \n\t"
65 "movq %%mm5, %%mm6 \n\t"
66 "punpcklwd %%mm0, %%mm3 \n\t"
67 "punpckhwd %%mm0, %%mm4 \n\t"
68 "punpcklwd %%mm0, %%mm5 \n\t"
69 "punpckhwd %%mm0, %%mm6 \n\t"
70 "pslld $8, %%mm3 \n\t"
71 "pslld $8, %%mm4 \n\t"
72 "pslld $24, %%mm5 \n\t"
73 "pslld $24, %%mm6 \n\t"
75 "por %%mm3, %%mm1 \n\t"
76 "por %%mm4, %%mm2 \n\t"
77 "por %%mm5, %%mm1 \n\t"
78 "por %%mm6, %%mm2 \n\t"
80 "add $8, %0 \n\t"
81 "add $8, %1 \n\t"
82 "add $4, %2 \n\t"
83 "add $4, %3 \n\t"
84 "movq %%mm1, (%8) \n\t"
85 "movq %%mm2, 8(%8) \n\t"
86 "add $16, %8 \n\t"
87 "decl %9 \n\t"
88 "jnz 1b \n\t"
89 : "=r" (y1), "=r" (y2), "=r" (u), "=r" (v)
90 : "0" (y1), "1" (y2), "2" (u), "3" (v), "r" (dst), "r" (w/8)
91 : "memory"
93 for (j = (w&7)/2; j; j--) {
94 *dst++ = (*y1++ + *y2++)/2;
95 *dst++ = *u++;
96 *dst++ = (*y1++ + *y2++)/2;
97 *dst++ = *v++;
99 y1 += yinc;
100 y2 += yinc;
101 u += uinc;
102 v += vinc;
103 dst += dstinc;
105 __asm__ volatile ( "emms \n\t" ::: "memory" );
107 #endif
111 static void halfpack_C(unsigned char *dst, unsigned char *src[3],
112 int dststride, int srcstride[3],
113 int w, int h)
115 int i, j;
116 unsigned char *y1, *y2, *u, *v;
117 int dstinc, yinc, uinc, vinc;
119 y1 = src[0];
120 y2 = src[0] + srcstride[0];
121 u = src[1];
122 v = src[2];
124 dstinc = dststride - 2*w;
125 yinc = 2*srcstride[0] - w;
126 uinc = srcstride[1] - w/2;
127 vinc = srcstride[2] - w/2;
129 for (i = h/2; i; i--) {
130 for (j = w/2; j; j--) {
131 *dst++ = (*y1++ + *y2++)>>1;
132 *dst++ = *u++;
133 *dst++ = (*y1++ + *y2++)>>1;
134 *dst++ = *v++;
136 y1 += yinc;
137 y2 += yinc;
138 u += uinc;
139 v += vinc;
140 dst += dstinc;
144 static void (*halfpack)(unsigned char *dst, unsigned char *src[3],
145 int dststride, int srcstride[3], int w, int h);
148 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts)
150 const uint8_t *src[MP_MAX_PLANES] = {
151 mpi->planes[0] + mpi->stride[0]*vf->priv->field,
152 mpi->planes[1], mpi->planes[2], NULL};
153 int src_stride[MP_MAX_PLANES] = {mpi->stride[0]*2, mpi->stride[1], mpi->stride[2], 0};
154 mp_image_t *dmpi;
156 // hope we'll get DR buffer:
157 dmpi=vf_get_image(vf->next, IMGFMT_YUY2,
158 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
159 mpi->w, mpi->h/2);
161 switch(vf->priv->field) {
162 case 0:
163 case 1:
164 sws_scale(vf->priv->ctx, src, src_stride,
165 0, mpi->h/2, dmpi->planes, dmpi->stride);
166 break;
167 default:
168 halfpack(dmpi->planes[0], mpi->planes, dmpi->stride[0],
169 mpi->stride, mpi->w, mpi->h);
172 return vf_next_put_image(vf,dmpi, pts);
175 static int config(struct vf_instance* vf,
176 int width, int height, int d_width, int d_height,
177 unsigned int flags, unsigned int outfmt)
179 if (vf->priv->field < 2) {
180 sws_freeContext(vf->priv->ctx);
181 // get unscaled 422p -> yuy2 conversion
182 vf->priv->ctx =
183 sws_getContext(width, height / 2, PIX_FMT_YUV422P,
184 width, height / 2, PIX_FMT_YUYV422,
185 SWS_POINT | SWS_PRINT_INFO | get_sws_cpuflags(),
186 NULL, NULL, NULL);
188 /* FIXME - also support UYVY output? */
189 return vf_next_config(vf, width, height/2, d_width, d_height, flags, IMGFMT_YUY2);
193 static int query_format(struct vf_instance* vf, unsigned int fmt)
195 /* FIXME - really any YUV 4:2:0 input format should work */
196 switch (fmt) {
197 case IMGFMT_YV12:
198 case IMGFMT_IYUV:
199 case IMGFMT_I420:
200 return vf_next_query_format(vf,IMGFMT_YUY2);
202 return 0;
205 static void uninit(struct vf_instance* vf)
207 sws_freeContext(vf->priv->ctx);
208 free(vf->priv);
211 static int open(vf_instance_t *vf, char* args)
213 vf->config=config;
214 vf->query_format=query_format;
215 vf->put_image=put_image;
216 vf->uninit=uninit;
218 vf->priv = calloc(1, sizeof (struct vf_priv_s));
219 vf->priv->field = 2;
220 if (args) sscanf(args, "%d", &vf->priv->field);
222 halfpack = halfpack_C;
223 #if HAVE_MMX
224 if(gCpuCaps.hasMMX) halfpack = halfpack_MMX;
225 #endif
226 return 1;
229 const vf_info_t vf_info_halfpack = {
230 "yuv planar 4:2:0 -> packed 4:2:2, half height",
231 "halfpack",
232 "Richard Felker",
234 open,
235 NULL