/
filter_vflip.c
153 lines (130 loc) · 3.42 KB
/
filter_vflip.c
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
/*
* filter_vflip
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "filter.h"
#include "frame.h"
typedef struct ThisFilter
{
VideoFilter vf;
TF_STRUCT;
} ThisFilter;
static void reverse_memcpy(unsigned char *dst, const unsigned char *src, int n)
{
int i;
for (i = 0; i < n; ++i)
dst[n - 1 - i] = src[i];
}
static int swap(VideoFrame *frame, int datasize, int offset, int shift)
{
int i, j;
int oldoffset;
int newoffset;
unsigned char *temp = malloc(datasize);
if (temp == NULL)
{
fprintf(stderr, "Couldn't allocate memory for temp\n");
return 0;
}
for (i = 0, j = (frame->height - 1); i < frame->height / 2; i++, j--)
{
newoffset = i * datasize + offset;
if (shift)
newoffset += datasize;
oldoffset = j * datasize + offset;
if (!shift || i != frame->height - 1)
{
memcpy(temp, frame->buf + newoffset,
datasize); // new -> temp
reverse_memcpy(frame->buf + newoffset, frame->buf + oldoffset,
datasize); // old -> new
reverse_memcpy(frame->buf + oldoffset, temp,
datasize); // temp -> old
}
}
if (temp)
free(temp);
return 1;
}
static int comp(const void *va, const void *vb)
{
int *a = (int *) va;
int *b = (int *) vb;
if (*a == *b)
return 0;
else if (*a < *b)
return -1;
else
return 1;
}
static int vflip(VideoFilter *vf, VideoFrame *frame, int field)
{
(void)field;
if (frame->codec != FMT_YV12)
{
fprintf(stderr, "codec %d unsupported for vflip, skipping\n",
frame->codec);
return 0;
}
TF_VARS;
(void)vf;
TF_START;
int ouroffsets[3];
memcpy(ouroffsets, frame->offsets, 3 * sizeof(int));
qsort(ouroffsets, 3, sizeof(int), comp);
int datasize;
datasize = (ouroffsets[1] - ouroffsets[0]) / frame->height;
if (!swap(frame, datasize, ouroffsets[0], 0))
return 0;
datasize = (ouroffsets[2] - ouroffsets[1]) / frame->height;
if (!swap(frame, datasize, ouroffsets[1], 0))
return 0;
datasize = (frame->size - ouroffsets[2]) / frame->height;
if (!swap(frame, datasize, ouroffsets[2], 0))
return 0;
TF_END((ThisFilter *)vf, "VFlip");
return 1;
}
static VideoFilter *new_filter(VideoFrameType inpixfmt,
VideoFrameType outpixfmt,
int *width, int *height, char *options,
int threads)
{
ThisFilter *filter;
(void)width;
(void)height;
(void)options;
(void)threads;
if ((inpixfmt != outpixfmt) ||
(inpixfmt != FMT_YV12 && inpixfmt != FMT_RGB24 &&
inpixfmt != FMT_YUV422P) )
return NULL;
filter = malloc(sizeof(ThisFilter));
if (filter == NULL)
{
fprintf(stderr, "Couldn't allocate memory for filter\n");
return NULL;
}
filter->vf.filter = &vflip;
filter->vf.cleanup = NULL;
TF_INIT(filter)
return (VideoFilter *) filter;
}
static FmtConv FmtList[] =
{
{ FMT_YV12, FMT_YV12 },
FMT_NULL
};
const FilterInfo filter_table[] =
{
{
filter_init: &new_filter,
name: "vflip",
descript: "flips the video image vertically",
formats: FmtList,
libname: NULL
},
FILT_NULL
};