forked from silicontrip/lavtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yuvhsync.c
356 lines (285 loc) · 9.24 KB
/
yuvhsync.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* yuvhsync.c
** <p>attempts to align horizontal sync drift.
** work in progress </p>
*
* modified from yuvadjust.c by
* Copyright (C) 2010 Mark Heath
*
*
* 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.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <math.h>
#include <yuv4mpeg.h>
#include <mpegconsts.h>
#include "utilyuv.h"
static void print_usage()
{
fprintf (stderr,
"usage: yuvhsync -m <max shift> -s <search length>\n"
"\n"
"-v\tVerbosity degree : 0=quiet, 1=normal, 2=verbose/debug\n"
"-m\tMaximum number of pixels to shift\n"
"-s\tCompare this number of pixels when determining shift\n"
"-n\tDo not shift video\n"
);
}
void shift_video (int s, int line, uint8_t *yuv_data[3],y4m_stream_info_t *sinfo)
{
int x,w,cw,cs;
int vss;
int linew,linecw;
// mjpeg_debug("shift_video %d %d",s,line);
w = y4m_si_get_plane_width(sinfo,0);
cw = y4m_si_get_plane_width(sinfo,1);
vss = y4m_si_get_plane_height(sinfo,0) / y4m_si_get_plane_height(sinfo,1);
cs = s * cw / w;
linew = line * w;
linecw = line * cw;
// could memcpy() do this?
if (s>0) {
for (x=w-1; x>=0; x--)
if (x>=s)
*(yuv_data[0]+x+(linew))= *(yuv_data[0]+(x-s)+(linew));
else
*(yuv_data[0]+x+(linew))= 16;
// one day I should start catering for more or less than 3 planes.
// have to take proper interlace chroma into account.
// would be easier to work with 422 or 444
if (line % vss) {
for (x=cw; x>=0; x--) {
if (x>=cs) {
*(yuv_data[1]+x+linecw)= *(yuv_data[1]+(x-cs)+linecw);
*(yuv_data[2]+x+linecw)= *(yuv_data[2]+(x-cs)+linecw);
} else {
*(yuv_data[1]+x+linecw)= 128;
*(yuv_data[2]+x+linecw)= 128;
}
}
}
}
if (s<0) {
for (x=0; x<=w; x++)
if (x-s < w)
*(yuv_data[0]+x+(linew))= *(yuv_data[0]+(x-s)+(linew));
else
*(yuv_data[0]+x+linew)=16;
// one day I should start catering for more or less than 3 planes.
if (line % vss) {
for (x=0; x<=cw; x++) {
if (x-cs<=cw) {
*(yuv_data[1]+x+(linecw))= *(yuv_data[1]+(x-cs)+(linecw));
*(yuv_data[2]+x+(linecw))= *(yuv_data[2]+(x-cs)+(linecw));
} else {
*(yuv_data[1]+x+(linecw))= 128;
*(yuv_data[2]+x+(linecw))= 128;
}
}
}
}
// mjpeg_debug("exit shift_video %d %d",s,line);
}
int search_video (int m, int s, int line, uint8_t *yuv_data[3],y4m_stream_info_t *sinfo)
{
int w,x1,diff;
int max,c=0,linew;
w = y4m_si_get_plane_width(sinfo,0);
linew= w * line;
for (x1=0;x1<m;x1++)
{
if (yuv_data[0][linew+x1] >=16) return x1;
// if (yuv_data[0][linew+x1]>= 32) return x1;
}
/*
for (x1=1;x1<m;x1++)
{
// it appears that sync is outside of the mpeg video range.
if (abs(yuv_data[0][linew+x1] - yuv_data[0][linew+x1+1] ) > 6) {
return x1-1;
}
}
*/
// fprintf(stderr," %d",diff);
// fprintf (stderr,"\n");
return 0;
}
// this method isn't too effective
int search_video_1 (int m, int s, int line, uint8_t *yuv_data[3],y4m_stream_info_t *sinfo)
{
int w,h;
int x1,x2;
int min,shift,tot;
int linew, line1w;
int ilace = y4m_si_get_interlace(sinfo);
w = y4m_si_get_plane_width(sinfo,0);
h = y4m_si_get_plane_height(sinfo,0);
linew = line * w;
if (ilace == Y4M_ILACE_NONE)
line1w = (line+1) * w ;
else
line1w = (line+2) * w;
line1w = (line+2) * w;
mjpeg_debug("search_video %d",line);
// 2 or 1 dependent on interlace or not.
if (line+2 > h) {
mjpeg_warn("line > height");
return 0;
}
shift = 0;
for (x1=-m;x1<m;x1++)
{
tot = 0;
for(x2=0; x2<s;x2++)
{
// don't know if I should apply a standard addition to pixels outside the box.
if (x1+x2 >=0 && x1+x2 < w)
tot += abs ( *(yuv_data[0]+x1+x2+linew) - *(yuv_data[0]+x2+line1w));
else
tot += 128;
}
// ok it wasn't max afterall, it was min.
if (x1==0) min = tot;
if (tot < min) { min = tot; shift = x1;}
}
mjpeg_debug("exit search_video %d",line);
return shift;
}
static void process( int fdIn , y4m_stream_info_t *inStrInfo,
int fdOut, y4m_stream_info_t *outStrInfo,
int max,int search, int noshift)
{
y4m_frame_info_t in_frame ;
uint8_t *yuv_data[3],*yuv_odata[3];
// int result[720]; // will change to malloc based on max shift
int *lineresult;
int y_frame_data_size, uv_frame_data_size ;
int read_error_code = Y4M_OK;
int write_error_code = Y4M_OK ;
int src_frame_counter ;
int x,y,w,h,cw,ch;
h = y4m_si_get_plane_height(inStrInfo,0);
ch = y4m_si_get_plane_height(inStrInfo,1);
lineresult = (int *) malloc(sizeof(int) * h);
chromalloc(yuv_data,inStrInfo);
// initialise and read the first number of frames
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 ) {
for (y=0; y<h-1; y++)
lineresult[y] = search_video(max,search,y,yuv_data,inStrInfo);
if (noshift) {
/* graphing this would be nice */
for (x=0; x < h; x++) {
if (x!=0) printf(", ");
printf ("%d",lineresult[x]);
}
printf("\n");
} else {
int shifter = 0;
for (y=0; y<h-1; y++) {
// shifter += lineresult[y];
shifter = -lineresult[y];
shift_video(shifter,y,yuv_data,inStrInfo);
}
write_error_code = y4m_write_frame( fdOut, outStrInfo, &in_frame, yuv_data );
}
y4m_fini_frame_info( &in_frame );
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn,inStrInfo,&in_frame,yuv_data );
++src_frame_counter ;
}
// Clean-up regardless an error happened or not
y4m_fini_frame_info( &in_frame );
free (lineresult);
chromafree(yuv_data);
if( read_error_code != Y4M_ERR_EOF )
mjpeg_error_exit1 ("Error reading from input stream!");
if( write_error_code != Y4M_OK )
mjpeg_error_exit1 ("Error writing output stream!");
}
// *************************************************************************************
// MAIN
// *************************************************************************************
int main (int argc, char *argv[])
{
int verbose = 1 ; // LOG_ERROR ?
int drop_frames = 0;
int fdIn = 0 ;
int fdOut = 1 ;
y4m_stream_info_t in_streaminfo,out_streaminfo;
int src_interlacing = Y4M_UNKNOWN;
y4m_ratio_t src_frame_rate;
const static char *legal_flags = "v:m:s:n";
int max_shift = 0, search = 0;
int noshift=0;
int c;
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 'm':
max_shift = atof(optarg);
break;
case 's':
search = atof(optarg);
break;
case 'n':
noshift=1;
break;
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);
y4m_init_stream_info (&out_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
if (y4m_read_stream_header (fdIn, &in_streaminfo) != Y4M_OK)
mjpeg_error_exit1 ("Could'nt read YUV4MPEG header!");
src_frame_rate = y4m_si_get_framerate( &in_streaminfo );
y4m_copy_stream_info( &out_streaminfo, &in_streaminfo );
// Information output
/* in that function we do all the important work */
if (!noshift)
y4m_write_stream_header(fdOut,&out_streaminfo);
process( fdIn,&in_streaminfo,fdOut,&out_streaminfo,max_shift,search,noshift);
y4m_fini_stream_info (&in_streaminfo);
y4m_fini_stream_info (&out_streaminfo);
return 0;
}
/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/