/
yuvGENERIC.c
198 lines (152 loc) · 5.18 KB
/
yuvGENERIC.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* generic.c
* Mark Heath <mjpeg0 at silicontrip.org>
* http://silicontrip.net/~mark/lavtools/
** <p> Skeleton code for generic spatial filters </p>
*
* based on code:
* Copyright (C) 2002 Alfonso Garcia-Patiño Barbolani <barbolani at jazzfree.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
gcc yuvdeinterlace.c -I/sw/include/mjpegtools -lmjpegutils
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <yuv4mpeg.h>
#include <mpegconsts.h>
#include "utilyuv.h"
#define VERSION "0.1"
static void print_usage()
{
fprintf (stderr,
"usage: yuv\n"
);
}
static void filterpixel(uint8_t *o, uint8_t *p, int i, int j, int w, int h) {
o[i+j*w] = p[i+j*w];
}
static void filterframe (uint8_t *m[3], uint8_t *n[3], y4m_stream_info_t *si)
{
int x,y;
int height,width,height2,width2;
height=y4m_si_get_plane_height(si,0);
width=y4m_si_get_plane_width(si,0);
// I'll assume that the chroma subsampling is the same for both u and v channels
height2=y4m_si_get_plane_height(si,1);
width2=y4m_si_get_plane_width(si,1);
for (y=0; y < height; y++) {
for (x=0; x < width; x++) {
filterpixel(m[0],n[0],x,y,width,height);
if (x<width2 && y<height2) {
filterpixel(m[1],n[1],x,y,width2,height2);
filterpixel(m[2],n[2],x,y,width2,height2);
}
}
}
}
static void filter( int fdIn , y4m_stream_info_t *inStrInfo )
{
y4m_frame_info_t in_frame ;
uint8_t *yuv_data[3] ;
int read_error_code ;
int write_error_code ;
// Allocate memory for the YUV channels
if (chromalloc(yuv_data,inStrInfo))
mjpeg_error_exit1 ("Could'nt allocate memory for the YUV4MPEG data!");
/* Initialize counters */
write_error_code = Y4M_OK ;
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn, inStrInfo,&in_frame,yuv_data );
while( Y4M_ERR_EOF != read_error_code && write_error_code == Y4M_OK ) {
// do work
if (read_error_code == Y4M_OK) {
filterframe(yuv_odata,yuv_data,inStrInfo);
write_error_code = y4m_write_frame( fdOut, inStrInfo, &in_frame, yuv_odata );
}
y4m_fini_frame_info( &in_frame );
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn, inStrInfo,&in_frame,yuv_data );
}
// Clean-up regardless an error happened or not
y4m_fini_frame_info( &in_frame );
free( yuv_data[0] );
free( yuv_data[1] );
free( yuv_data[2] );
if( read_error_code != Y4M_ERR_EOF )
mjpeg_error_exit1 ("Error reading from input stream!");
}
// *************************************************************************************
// MAIN
// *************************************************************************************
int main (int argc, char *argv[])
{
int verbose = 4; // LOG_ERROR ;
int fdIn = 0 ;
int fdOut = 1 ;
y4m_stream_info_t in_streaminfo, out_streaminfo ;
y4m_ratio_t frame_rate;
int interlaced,ilace=0,pro_chroma=0,yuv_interlacing= Y4M_UNKNOWN;
int c ;
const static char *legal_flags = "hv:";
while ((c = getopt (argc, argv, legal_flags)) != -1) {
switch (c) {
case 'v':
verbose = atoi (optarg);
if (verbose < 0 || verbose > 2)
mjpeg_error_exit1 ("Verbose level must be [0..2]");
break;
case 'h':
case '?':
print_usage (argv);
return 0 ;
break;
}
}
// mjpeg tools global initialisations
mjpeg_default_handler_verbosity (verbose);
// Initialize input streams
y4m_init_stream_info (&in_streaminfo);
// ***************************************************************
// Get video stream informations (size, framerate, interlacing, aspect ratio).
// The streaminfo structure is filled in
// ***************************************************************
// INPUT comes from stdin, we check for a correct file header
y4m_accept_extensions(1); // most of my code is chroma subsampling aware
if (y4m_read_stream_header (fdIn, &in_streaminfo) != Y4M_OK)
mjpeg_error_exit1 ("Could'nt read YUV4MPEG header!");
// Information output
mjpeg_info ("yuv (version " VERSION ") is a general utility for yuv streams");
mjpeg_info ("(C) Mark Heath <mjpeg0 at silicontrip.org>");
// mjpeg_info ("yuvcropdetect -h for help");
y4m_copy_stream_info( &out_streaminfo, &in_streaminfo );
// make changes to output stream
y4m_write_stream_header(fdOut,&out_streaminfo);
/* in that function we do all the important work */
filter(fdIn, &in_streaminfo);
y4m_fini_stream_info (&in_streaminfo);
y4m_fini_stream_info (&out_streaminfo);
return 0;
}
/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/