-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutilyuv.c
More file actions
465 lines (351 loc) · 8.77 KB
/
Copy pathutilyuv.c
File metadata and controls
465 lines (351 loc) · 8.77 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "utilyuv.h"
#include <stdio.h>
/*
** <p>this is a utility library. It doesn't do anything itself</p>
going to start putting my most common functions here.
gcc -I/usr/local/include/mjpegtools -c utilyuv.c
*/
struct y4m_stream_info_cache {
int h;
int w;
int cw;
int ch;
};
struct y4m_stream_info_cache sic;
struct colour {
uint8_t y;
uint8_t u;
uint8_t v;
};
// Allocate a uint8_t frame
int chromalloc(uint8_t *m[3], y4m_stream_info_t *sinfo)
{
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
// I'm gonna cheat and use this as an initialisation function.
sic.h = y4m_si_get_plane_height(sinfo,0);
sic.w = y4m_si_get_plane_width(sinfo,0);
sic.ch = y4m_si_get_plane_height(sinfo,1);
sic.cw = y4m_si_get_plane_width(sinfo,1);
m[0] = (uint8_t *)malloc( fs );
m[1] = (uint8_t *)malloc( cfs);
m[2] = (uint8_t *)malloc( cfs);
if( !m[0] || !m[1] || !m[2]) {
return -1;
} else {
return 0;
}
}
//Copy a uint8_t frame
void chromacpy(uint8_t *m[3],uint8_t *n[3],y4m_stream_info_t *sinfo)
{
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
memcpy (m[0],n[0],fs);
memcpy (m[1],n[1],cfs);
memcpy (m[2],n[2],cfs);
}
//allocate an array of frames, for temporal filters
int temporalalloc (uint8_t ****yuv_data, y4m_stream_info_t *sinfo, int length)
{
int c,d;
// fprintf(stderr,"temporalalloc: in\n");
*yuv_data= (uint8_t ***) malloc(sizeof (uint8_t *) * length);
if (*yuv_data == NULL) return -1;
for (c=0;c<length;c++) {
(*yuv_data)[c] = (uint8_t **) malloc(sizeof (uint8_t *) * 3);
if ((*yuv_data)[c] == NULL) {
// how am I going to keep track of what I've allocated?
for (d=0;d<c;d++) {
chromafree((*yuv_data)[d]);
free((*yuv_data)[d]);
}
free (*yuv_data);
return -1;
}
if(chromalloc((*yuv_data)[c],sinfo)) {
for (d=0;d<c;d++) {
chromafree((*yuv_data)[d]);
free((*yuv_data)[d]);
}
free((*yuv_data)[c]);
free(*yuv_data);
return -1;
}
}
// fprintf(stderr,"temporalalloc: out\n");
return 0;
}
void temporalfree(uint8_t ***yuv_data, int length)
{
int d;
for (d=0;d<length;d++) {
chromafree(yuv_data[d]);
free(yuv_data[d]);
}
free (yuv_data);
}
//do I want to also shuffle the other direction?
void temporalshuffle(uint8_t ***yuv_data, int length)
{
uint8_t **temp_yuv;
int c;
// fprintf(stderr,"temporalshuffle: in\n");
temp_yuv = yuv_data[0];
for (c=0;c<length-1;c++)
yuv_data[c] = yuv_data[c+1];
yuv_data[length-1] = temp_yuv;
// fprintf(stderr,"temporalshuffle: out\n");
}
//Copy a single field of a frame
void copyfield(uint8_t *m[3],uint8_t *n[3],y4m_stream_info_t *sinfo, int which)
{
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);
if (which==Y4M_ILACE_TOP_FIRST) {
r=0;
} else if (which==Y4M_ILACE_BOTTOM_FIRST) {
r=1;
} else {
mjpeg_warn("copyfield() invalid interlace selected (%d)",which);
}
for (; r < h; r += 2)
{
memcpy(&m[0][r * w], &n[0][r * w], w);
if (r<ch) {
memcpy(&m[1][r*cw], &n[1][r*cw], cw);
memcpy(&m[2][r*cw], &n[2][r*cw], cw);
}
}
}
// set a solid colour for a uint8_t frame
void chromaset(uint8_t *m[3], y4m_stream_info_t *sinfo, int y, int u, int v )
{
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
memset (m[0],y,fs);
memset (m[1],u,cfs);
memset (m[2],v,cfs);
}
void chromafree(uint8_t *m[3])
{
free(m[0]);
free(m[1]);
free(m[2]);
}
// Get a pixel, with bounds checking.
//how easy is it to make this for all planes
uint8_t get_pixel(register int x, register int y, int plane, uint8_t *m[3],y4m_stream_info_t *si)
{
int w,h,off;
uint8_t *p;
/*
h = y4m_si_get_plane_height(si,plane);
w = y4m_si_get_plane_width(si,plane);
*/
// the y4m_si_get_plane functions are eating large amounts of CPU on PPC machines.
if ( plane == 0) {
h = sic.h;
w = sic.w;
} else {
h = sic.ch;
w = sic.cw;
}
// my poor attempt to optimise for speed.
p=m[plane]+x;
off= y * w;
if (x < 0) {x=0; p=m[plane];}
if (x >= w) {x=w-1; p=m[plane]+x;}
if (y < 0) {y=0; off=0;}
if (y >= h) {y=h-1; off= y * w;}
return *(p+off);
}
void set_pixel(uint8_t val,int x, int y, int plane, uint8_t *m[3],y4m_stream_info_t *si)
{
int w,h;
h = y4m_si_get_plane_height(si,plane);
w = y4m_si_get_plane_width(si,plane);
if (x >= 0 && x < w && y >= 0 && y < h) {
*(m[plane]+x+y*w) = val;
}
}
// Mix two colours, percent is 0-255
uint8_t mix (uint8_t c1, uint8_t c2, uint8_t per, int y) {
return ((c1 - y) * (255 - per) + (c2 - y) * per) / 255 + y;
}
uint8_t luma_mix (uint8_t c1, uint8_t c2, uint8_t per) {
return mix(c1,c2,per,16);
}
uint8_t chroma_mix (uint8_t c1, uint8_t c2, uint8_t per) {
return mix(c1,c2,per,128);
}
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 opposite field ordering
int invert_order(int f)
{
switch (f) {
case Y4M_ILACE_TOP_FIRST:
return Y4M_ILACE_BOTTOM_FIRST;
case Y4M_ILACE_BOTTOM_FIRST:
return Y4M_ILACE_TOP_FIRST;
case Y4M_ILACE_NONE:
return Y4M_ILACE_NONE;
default:
return Y4M_UNKNOWN;
}
}
//
// Returns the greatest common divisor (GCD) of the input parameters.
//
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int xchroma (int x, y4m_stream_info_t *si)
{
int cwr;
cwr = y4m_si_get_plane_width(si,0) / y4m_si_get_plane_width(si,1);
if ( cwr == 1 ) {
return x;
} else if ( cwr == 2) {
return x >> 1;
} else if (cwr == 4) {
return x >> 2;
}
return x / cwr;
}
int ychroma(int y, y4m_stream_info_t *si)
{
int chr,ychr;
chr=y4m_si_get_plane_height(si,0) / y4m_si_get_plane_height(si,1);
if (chr == 1) {
return y;
} else if (chr == 2) {
if (y4m_si_get_interlace(si) == Y4M_ILACE_NONE) {
return y >> 1;
} else {
return ((y >> 2) << 1) + (y%2);
}
} else if (chr == 4) {
// I have no idea how a /4 interlace chroma would work.
return y >> 2;
}
return y / chr;
}
// I don't think this code is valid.
int timecode2framecount (y4m_stream_info_t *si, int h, int m, int s, int f, int df)
{
int sec;
y4m_ratio_t fr;
fr = y4m_si_get_framerate(si);
sec = h * 3600 + m * 60 + s;
if (fr.n % fr.d) {
// non integer frame rate
if (df) {
if ( fabs(29.97 - (1.0 * fr.n / fr.d )) < 0.001){
// TODO: this only works for 29.97
// there is only drop code algorithm for 30000/1001
int totalMinutes = 60 * h + m;
fr.n += fr.d - (fr.n % fr.d);
// 2 skipped frames per minute, excluding the 10 minute divisible ones.
return sec * fr.n / fr.d - 2 * (totalMinutes - totalMinutes / 10);
} else {
mjpeg_warn ("Unknown drop frame frame rate: %d/%d",fr.n,fr.d);
}
} else {
// non drop frame
// round up to the next highest frame rate, time code doesn't represent real time.
fr.n += fr.d - (fr.n % fr.d);
return sec * fr.n / fr.d;
}
} else {
// integer frame rates
return sec * fr.n / fr.d;
}
}
void framecount2timecode(y4m_stream_info_t *si, int *h, int *m, int *s, int *f, int fc, int *df )
{
y4m_ratio_t fr;
// fprintf (stderr,"string_tc\n");
fr = y4m_si_get_framerate (si);
if (fr.n % fr.d) {
int n = fr.n;
// round up to integer frame rate
// non drop calculation.
fr.n += fr.d - (fr.n % fr.d);
mjpeg_debug ("rounded up to : %d:%d\n",fr.n,fr.d);
if (*df) {
// drop calculation.
if ( fabs(29.97 - (1.0 * n / fr.d )) < 0.001){
// the correct SMPTE algorithm for 29.97
int smpted = fc / 17982;
int smptem = fc % 17982;
fc += 18*smpted + 2*((smptem - 2) / 1798);
} else {
// this algorithm is not the correct SMPTE algorithm.
// but is more generic
fc = (fc * fr.n) / n;
}
}
} else {
*df = 0;
}
*h = fr.d * fc / fr.n / 3600;
*m = (fr.d * fc / fr.n / 60) % 60;
*s = (fr.d * fc / fr.n) % 60;
*f = fc % (fr.n / fr.d);
}
void y4m_dump_frame(y4m_stream_info_t *si, uint8_t *m[3])
{
int w,h;
int x,y;
h = y4m_si_get_plane_height(si,0);
w = y4m_si_get_plane_width(si,0);
printf(" ");
for (x=0;x<w;x++) {
if (x%10==0)
printf("%2d",x/10);
else
printf(" ");
}
printf("\n");
printf(" ");
for (x=0;x<w;x++) {
printf("%2d",x%10);
}
printf("\n");
for (y=0; y<h; y++) {
printf ("%03d ",y);
int linew = w * y;
for (x=0;x<w;x++) {
printf("%02x",m[0][linew+x]);
}
printf("\n");
}
}