/
yuvrfps.c
443 lines (350 loc) · 12.1 KB
/
yuvrfps.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* yuvrfps.c
**<h3>Frame Dropping Frame Rate Converter </h3>
**<p>
**Performs frame rate reduction by simple fractional amounts, by
**dropping 1 out of every X frames or fields.
**</p><p>
**This is to work with progressive 3:2 pulldown files performing conversions such as
**29.97 to 23.976 similar to yuvkineco or even 30 to 25 fps. It
**analyses X frames and finds the least different fram e and drops
**it, in theory removing any duplicated frames.
**</p>
**<p> It is possible that the 3:2 pulldown cadence starts on an interlace frame
**in which the detection will make all frames interlace (field reversed progressive)
**this can be corrected by the -s option to start the sequence on a progressive frame.
**It should be less than the Decimation number.
**</p>
**<h4>Usage</h4>
**<pre>
**-F <Decimation> reduces X frames to X-1
** Common Decimation ratios
** 5: 29.970 -> 23.976
** 6: 30 -> 25
**-f <X[:Y]> force drop frame X (or X:Y fields)
**-I t|b|p Force interlace mode
**-s <X> skip X frames. Output X frames unchanged before starting detection.
**</pre>
*
* modified from yuvfps.c by
* Copyright (C) 2002 Alfonso Garcia-Patiño Barbolani
*
*
* 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 <yuv4mpeg.h>
#include <mpegconsts.h>
#define YUVRFPS_VERSION "0.1"
static void print_usage()
{
fprintf (stderr,
"usage: yuvrfps [-F <decimation>] [-I t|b|p] [-v 0|1|2]\n"
"yuvrfps reduces frame rate by decimation\n"
"\n"
"\t -v Verbosity degree : 0=quiet, 1=normal, 2=verbose/debug\n"
"\t -I<pbt> Force interlace mode\n"
"\t -F <X> Drop 1 frame every X frames\n"
"\t -f <X>[:<Y>] force drop frame X (or fields X:Y) rather than detect\n"
"\t -s <X> skip X frames. Output X frames before performing drop. To synchronize 3:2 pulldown cadence\n"
"\t -h print this help\n"
);
}
void copyfield(int which, y4m_stream_info_t *sinfo, unsigned char *input[], unsigned char *output[])
{
// "which" is 0 for top field, 1 for bottom field
int r = 0;
int h,w,cw,ch;
h = y4m_si_get_plane_height(sinfo,0);
w = y4m_si_get_plane_width(sinfo,0);
cw = y4m_si_get_plane_width(sinfo,1);
ch = y4m_si_get_plane_height(sinfo,1);
int chromapos = which * cw;
int lumapos = which * w;
for (r = which; r < h; r += 2)
{
memcpy(&output[0][lumapos], &input[0][lumapos], w);
if (r < ch) {
memcpy(&output[1][chromapos], &input[1][chromapos], cw);
memcpy(&output[2][chromapos], &input[2][chromapos], cw);
chromapos += cw * 2;
}
lumapos += w * 2;
}
}
// read X frames
// diff each frame (X-1)
// for 1 to X
// if frame != diff write frame
static void detect( int fdIn , y4m_stream_info_t *inStrInfo,
int fdOut, y4m_stream_info_t *outStrInfo,
int interlacing, int drop_frames,
int iforce, int oforce,int skip)
{
y4m_frame_info_t in_frame ;
uint8_t *yuv_data[drop_frames+1][3] ;
int frame_data_size ;
int read_error_code ;
int write_error_code ;
int *bri, *bro,l=0,f=0,mini=0,mino=0,dropmo=0,dropmi=0,x,w,h;
// Allocate memory for the YUV channels
w = y4m_si_get_width(inStrInfo);
h = y4m_si_get_height(inStrInfo);
frame_data_size = h *w;
bri = (int *)malloc(sizeof(int) * (drop_frames+1));
bro = (int *)malloc(sizeof(int) * (drop_frames+1));
// should check for allocation errors here.
for (f=0; f<= drop_frames; f++) {
yuv_data[f][0] = (uint8_t *)malloc( frame_data_size );
yuv_data[f][1] = (uint8_t *)malloc( frame_data_size >> 2);
yuv_data[f][2] = (uint8_t *)malloc( frame_data_size >> 2);
if( !yuv_data[f][0] || !yuv_data[f][1] || !yuv_data[f][2])
mjpeg_error_exit1 ("Could'nt allocate memory for the YUV4MPEG data!");
}
write_error_code = Y4M_OK ;
// initialise and read the first number of frames
for (f=0; f <= skip; f++) {
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn,inStrInfo,&in_frame,yuv_data[0] );
// we will never drop the first frame of a file
write_error_code = y4m_write_frame( fdOut, outStrInfo, &in_frame, yuv_data[0] );
}
for (f=1; f<=drop_frames && Y4M_ERR_EOF != read_error_code; f++) {
y4m_fini_frame_info( &in_frame );
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn,inStrInfo,&in_frame,yuv_data[f] );
}
// f should be drop_frames + 1
// do some test if fewer frames read and change drop_frames to equal frames read
while( Y4M_ERR_EOF != read_error_code && write_error_code == Y4M_OK ) {
// compare all frames
if (iforce>=0) {
dropmi = iforce;
dropmo = oforce;
} else {
for (f=1; f<=drop_frames;f++) {
// perform frame difference.
// only comparing Luma, less noise, more resolution... blah blah
bri[f] = 0; bro[f]=0;
for (l=0; l< frame_data_size; l+=w<<1) {
for (x=0; x<w; x++) {
bri[f] += abs(yuv_data[f][0][l+x]-yuv_data[f-1][0][l+x]);
bro[f] += abs(yuv_data[f][0][l+x+w]-yuv_data[f-1][0][l+x+w]);
}
}
}
// find the minimum difference field.
if (interlacing == Y4M_ILACE_NONE) {
mini = bri[1] + bro[1]; dropmi = 1;
for (f=2; f<=drop_frames; f++) {
if (mini > (bri[f]+bro[f])) {
mini = bri[f]+bro[f];
dropmi=f;
}
}
} else {
// fprintf(stderr,"field 1: odd: %d even: %d\n",bro[1],bri[1]);
mini = bri[1]; dropmi=1;
mino = bro[1]; dropmo=1;
for (f=2; f<=drop_frames; f++) {
// fprintf(stderr,"field %d: odd: %d even: %d\n",f,bro[f],bri[f]);
if (mini > bri[f]) {
mini = bri[f];
dropmi=f;
}
if (mino > bro[f]) {
mino = bro[f];
dropmo=f;
}
}
}
}
// for progressive dropmi and dropmo *should* be the same.
if (interlacing == Y4M_ILACE_NONE || dropmi == dropmo) {
mjpeg_info("Dropping frame: %d",dropmi);
for (f=1; f<=drop_frames;f++) {
if (f != dropmi) {
// fprintf(stderr,"writing %d (drop %d)\n",f,dropmi);
write_error_code = y4m_write_frame( fdOut, outStrInfo, &in_frame, yuv_data[f] );
}
}
} else {
mjpeg_info("Dropping fields even: %d odd: %d",dropmi,dropmo);
for (f=1; f<drop_frames;f++) {
if (f == dropmi)
for (l=f; l<drop_frames; l++)
copyfield (0,inStrInfo, yuv_data[l+1],yuv_data[l]);
if (f == dropmo)
for (l=f; l<drop_frames; l++)
copyfield (1,inStrInfo, yuv_data[l+1],yuv_data[l]);
write_error_code = y4m_write_frame( fdOut, outStrInfo, &in_frame, yuv_data[f] );
}
}
// output all but the minimum difference frame.
// how do I drop a single field from two different frames
// the additional frame is for comparing to the next block of frames
// we do not want to write it twice
// copy the last frame to the first
memcpy(yuv_data[0][0],yuv_data[drop_frames][0],frame_data_size);
// assumes 420 video
// TODO: read chroma subsampling from stream
memcpy(yuv_data[0][1],yuv_data[drop_frames][1],frame_data_size>>2);
memcpy(yuv_data[0][2],yuv_data[drop_frames][2],frame_data_size>>2);
/*
for (l=0; l< frame_data_size; l++) {
yuv_data[0][0][l] = yuv_data[drop_frames][0][l];
if (!l%2) {
yuv_data[0][1][l>>2] = yuv_data[drop_frames][1][l>>2];
yuv_data[0][2][l>>2] = yuv_data[drop_frames][2][l>>2];
}
}
*/
// TODO if read fewer than drop_frames main loop will exit.
for (f=1; f<=drop_frames && Y4M_ERR_EOF != read_error_code; f++) {
y4m_fini_frame_info( &in_frame );
y4m_init_frame_info( &in_frame );
read_error_code = y4m_read_frame(fdIn,inStrInfo,&in_frame,yuv_data[f] );
}
}
// Clean-up regardless an error happened or not
free(bro); free(bri);
y4m_fini_frame_info( &in_frame );
for (f=0; f<=drop_frames; f++) {
free( yuv_data[f][0] );
free( yuv_data[f][1] );
free( yuv_data[f][2] );
}
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!");
}
static int parse_interlacing(char *str)
{
if (str[0] != '\0' && str[1] == '\0')
{
switch (str[0])
{
case 'p':
return Y4M_ILACE_NONE;
case 't':
return Y4M_ILACE_TOP_FIRST;
case 'b':
return Y4M_ILACE_BOTTOM_FIRST;
}
}
mjpeg_error_exit1("Valid interlacing modes are: p - progressive, t - top-field first, b - bottom-field first");
return Y4M_UNKNOWN; /* to avoid compiler warnings */
}
//
// Returns the greatest common divisor (GCD) of the input parameters.
//
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// *************************************************************************************
// MAIN
// *************************************************************************************
int main (int argc, char *argv[])
{
int verbose = 4; // LOG_ERROR ;
int drop_frames = 0;
int oforce = -1;
int iforce = -1;
int skip = 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 = "s:F:f:I:v:h";
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 'I':
src_interlacing = parse_interlacing(optarg);
break;
case 'F':
drop_frames = atoi(optarg);
break;
case 'f':
//force = atof(optarg);
sscanf (optarg,"%d:%d",&iforce,&oforce);
// fprintf (stderr,"Full: %d Even: %d Odd: %d\n",force,iforce,oforce);
break;
case 's':
skip = atoi(optarg);
break;
case 'h':
case '?':
print_usage (argv);
return 0 ;
break;
}
}
if (drop_frames < 2)
mjpeg_error_exit1("Minimum 2 frames to compare");
// 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
y4m_accept_extensions(1); // because we handle different chroma subsampling
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 );
mjpeg_info ("Input framerate %d:%d",src_frame_rate.n,src_frame_rate.d);
src_frame_rate.d = src_frame_rate.d * drop_frames;
src_frame_rate.n = src_frame_rate.n * (drop_frames-1);
mjpeg_info( "Output framerate %d:%d\n",src_frame_rate.n,src_frame_rate.d);
y4m_si_set_framerate( &out_streaminfo, src_frame_rate );
// Information output
mjpeg_info ("yuvrfps (version " YUVRFPS_VERSION ") is a frame rate converter which drops doubled frames for yuv streams");
mjpeg_info ("yuvrfps -h for help");
if (src_interlacing == Y4M_UNKNOWN)
src_interlacing = y4m_si_get_interlace(&in_streaminfo);
/* in that function we do all the important work */
y4m_write_stream_header(fdOut,&out_streaminfo);
detect( fdIn,&in_streaminfo,fdOut,&out_streaminfo,src_interlacing,drop_frames,iforce,oforce,skip);
y4m_fini_stream_info (&in_streaminfo);
y4m_fini_stream_info (&out_streaminfo);
return 0;
}
/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/