summaryrefslogtreecommitdiffstats
path: root/libmpcodecs/vf_ilpack.c
blob: 365336cd7a5ca9ed3b4621f268f66e1555110425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "../config.h"
#include "../mp_msg.h"
#include "../cpudetect.h"

#include "img_format.h"
#include "mp_image.h"
#include "vf.h"

#include "../libvo/fastmemcpy.h"
#include "../postproc/rgb2rgb.h"

#ifdef HAVE_MMX
static void pack_MMX(unsigned char *dst, unsigned char *y,
	unsigned char *u, unsigned char *v, int w)
{
	int j;
	asm (""
		"pxor %%mm0, %%mm0 \n\t"
		".balign 16 \n\t"
		"1: \n\t"
		"movq (%0), %%mm1 \n\t"
		"movq (%0), %%mm2 \n\t"
		"punpcklbw %%mm0, %%mm1 \n\t"
		"punpckhbw %%mm0, %%mm2 \n\t"
		
		"movq (%1), %%mm3 \n\t"
		"movq (%2), %%mm5 \n\t"
		"punpcklbw %%mm0, %%mm3 \n\t"
		"punpcklbw %%mm0, %%mm5 \n\t"
		"movq %%mm3, %%mm4 \n\t"
		"movq %%mm5, %%mm6 \n\t"
		"punpcklwd %%mm0, %%mm3 \n\t"
		"punpckhwd %%mm0, %%mm4 \n\t"
		"punpcklwd %%mm0, %%mm5 \n\t"
		"punpckhwd %%mm0, %%mm6 \n\t"
		"pslld $8, %%mm3 \n\t"
		"pslld $8, %%mm4 \n\t"
		"pslld $24, %%mm5 \n\t"
		"pslld $24, %%mm6 \n\t"
		
		"por %%mm3, %%mm1 \n\t"
		"por %%mm4, %%mm2 \n\t"
		"por %%mm5, %%mm1 \n\t"
		"por %%mm6, %%mm2 \n\t"
		
		"addl $8, %0 \n\t"
		"addl $4, %1 \n\t"
		"addl $4, %2 \n\t"
		"movq %%mm1, (%3) \n\t"
		"movq %%mm2, 8(%3) \n\t"
		"addl $16, %3 \n\t"
		"decl %4 \n\t"
		"jnz 1b \n\t"
		: 
		: "r" (y), "r" (u), "r" (v), "r" (dst), "r" (w/8)
		: "memory"
		);
	for (j = (w&7)/2; j; j--) {
		*dst++ = *y++;
		*dst++ = *u++;
		*dst++ = *y++;
		*dst++ = *v++;
	}
	asm volatile ( "emms \n\t" ::: "memory" );
}
#endif

static void pack_C(unsigned char *dst, unsigned char *y,
	unsigned char *u, unsigned char *v, int w)
{
	int j;
	for (j = w/2; j; j--) {
		*dst++ = *y++;
		*dst++ = *u++;
		*dst++ = *y++;
		*dst++ = *v++;
	}
}

static void (*pack)(unsigned char *dst, unsigned char *y,
	unsigned char *u, unsigned char *v, int w);

static void ilpack(unsigned char *dst, unsigned char *src[3],
	unsigned int dststride, unsigned int srcstride[3], int w, int h)
{
	int i;
	unsigned char *y, *u, *v;

	y = src[0];
	u = src[1];
	v = src[2];

	for (i=0; i<h; i++) {
		pack(dst, y, u, v, w);
		y += srcstride[0];
		if ((i&3) == 1) {
			u -= srcstride[1];
			v -= srcstride[2];
		} else {
			u += srcstride[1];
			v += srcstride[2];
		}
		dst += dststride;
	}
}


static int put_image(struct vf_instance_s* vf, mp_image_t *mpi)
{
	mp_image_t *dmpi;

	// hope we'll get DR buffer:
	dmpi=vf_get_image(vf->next, IMGFMT_YUY2,
			  MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
			  mpi->w, mpi->h);

	ilpack(dmpi->planes[0], mpi->planes, dmpi->stride[0], mpi->stride, mpi->w, mpi->h);

	return vf_next_put_image(vf,dmpi);
}

static int config(struct vf_instance_s* vf,
		  int width, int height, int d_width, int d_height,
		  unsigned int flags, unsigned int outfmt)
{
	/* FIXME - also support UYVY output? */
	return vf_next_config(vf, width, height, d_width, d_height, flags, IMGFMT_YUY2);
}


static int query_format(struct vf_instance_s* vf, unsigned int fmt)
{
	/* FIXME - really any YUV 4:2:0 input format should work */
	switch (fmt) {
	case IMGFMT_YV12:
	case IMGFMT_IYUV:
	case IMGFMT_I420:
		return vf_next_query_format(vf,IMGFMT_YUY2);
	}
	return 0;
}

static int open(vf_instance_t *vf, char* args)
{
	vf->config=config;
	vf->query_format=query_format;
	vf->put_image=put_image;
	
	pack = pack_C;
#ifdef HAVE_MMX
	if(gCpuCaps.hasMMX) pack = pack_MMX;
#endif
	return 1;
}

vf_info_t vf_info_ilpack = {
	"4:2:0 planar -> 4:2:2 packed reinterlacer",
	"ilpack",
	"Richard Felker",
	"",
	open,
	NULL
};