-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathyv.c
1221 lines (1063 loc) · 32.6 KB
/
yv.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "SDL.h"
/* Supported YUV-formats */
#define YV12 0
#define IYUV 1
#define YUY2 2
#define UYVY 3
#define YVYU 4
#define YV1210 5 /* 10 bpp YV12 */
#define Y42210 6
/* IPC */
#define NONE 0
#define MASTER 1
#define SLAVE 2
/* IPC Commands */
#define NEXT 'a'
#define PREV 'b'
#define REW 'c'
#define ZOOM_IN 'd'
#define ZOOM_OUT 'e'
#define QUIT 'f'
#define Y_ONLY 'g'
#define CB_ONLY 'h'
#define CR_ONLY 'i'
#define ALL_PLANES 'j'
/* PROTOTYPES */
Uint32 rd(Uint8* data, Uint32 size);
Uint32 read_yv12(void);
Uint32 read_iyuv(void);
Uint32 read_422(void);
Uint32 read_y42210(void);
Uint32 read_yv1210(void);
Uint32 allocate_memory(void);
void draw_grid422(void);
void draw_grid420(void);
void luma_only(void);
void cb_only(void);
void cr_only(void);
void draw_420(void);
void draw_422(void);
Uint32 diff_mode(void);
void calc_psnr(Uint8* frame0, Uint8* frame1);
void usage(char* name);
void mb_loop(char* str, Uint32 rows, Uint8* data, Uint32 pitch);
void show_mb(Uint32 mouse_x, Uint32 mouse_y);
void draw_frame(void);
Uint32 read_frame(void);
void setup_param(void);
void check_input(void);
Uint32 open_input(void);
Uint32 create_message_queue(void);
void destroy_message_queue(void);
Uint32 connect_message_queue(void);
Uint32 send_message(char cmd);
Uint32 read_message(void);
Uint32 event_dispatcher(void);
Uint32 event_loop(void);
Uint32 parse_input(int argc, char **argv);
Uint32 sdl_init(void);
void set_caption(char *array, Uint32 frame, Uint32 bytes);
void set_zoom_rect(void);
void histogram(void);
Uint32 ten2eight(Uint8* src, Uint8* dst, Uint32 length);
SDL_Surface *screen;
SDL_Event event;
SDL_Rect video_rect;
SDL_Overlay *my_overlay;
const SDL_VideoInfo* info = NULL;
Uint32 FORMAT = YV12;
FILE* fd;
struct my_msgbuf {
long mtype;
char mtext[2];
};
struct param {
Uint32 width; /* frame width - in pixels */
Uint32 height; /* frame height - in pixels */
Uint32 wh; /* width x height */
Uint32 frame_size; /* size of 1 frame - in bytes */
Sint32 zoom; /* zoom-factor */
Uint32 zoom_width;
Uint32 zoom_height;
Uint32 grid; /* grid-mode - on or off */
Uint32 hist; /* histogram-mode - on or off */
Uint32 grid_start_pos;
Uint32 diff; /* diff-mode */
Uint32 y_start_pos; /* start pos for first Y pel */
Uint32 cb_start_pos; /* start pos for first Cb pel */
Uint32 cr_start_pos; /* start pos for first Cr pel */
Uint32 y_only; /* Grayscale, i.e Luma only */
Uint32 cb_only; /* Only Cb plane */
Uint32 cr_only; /* Only Cr plane */
Uint32 mb; /* macroblock-mode - on or off */
Uint32 y_size; /* sizeof luma-data for 1 frame - in bytes */
Uint32 cb_size; /* sizeof croma-data for 1 frame - in bytes */
Uint32 cr_size; /* sizeof croma-data for 1 frame - in bytes */
Uint8* raw; /* pointer towards complete frame - frame_size bytes */
Uint8* y_data; /* pointer towards luma-data */
Uint8* cb_data; /* pointer towards croma-data */
Uint8* cr_data; /* pointer towards croma-data */
char* filename; /* obvious */
char* fname_diff; /* see above */
Uint32 overlay_format; /* YV12, IYUV, YUY2, UYVY or YVYU - SDL */
Uint32 vflags; /* HW support or SW support */
Uint8 bpp; /* bits per pixel */
Uint32 mode; /* MASTER, SLAVE or NONE - defaults to NONE */
struct my_msgbuf buf;
int msqid;
key_t key;
FILE* fd2; /* diff file */
};
/* Global parameter struct */
struct param P;
Uint32 rd(Uint8* data, Uint32 size)
{
Uint32 cnt;
cnt = fread(data, sizeof(Uint8), size, fd);
if (cnt < size) {
fprintf(stderr, "No more data to read!\n");
return 0;
}
return 1;
}
Uint32 read_yv12(void)
{
if (!rd(P.y_data, P.y_size)) return 0;
if (!rd(P.cb_data, P.cb_size)) return 0;
if (!rd(P.cr_data, P.cr_size)) return 0;
return 1;
}
Uint32 read_iyuv(void)
{
if (!rd(P.y_data, P.y_size)) return 0;
if (!rd(P.cb_data, P.cb_size)) return 0;
if (!rd(P.cr_data, P.cr_size)) return 0;
return 1;
}
Uint32 read_422(void)
{
Uint8* y = P.y_data;
Uint8* cb = P.cb_data;
Uint8* cr = P.cr_data;
if (!rd(P.raw, P.frame_size)) return 0;
for (Uint32 i = P.y_start_pos; i < P.frame_size; i += 2) *y++ = P.raw[i];
for (Uint32 i = P.cb_start_pos; i < P.frame_size; i += 4) *cb++ = P.raw[i];
for (Uint32 i = P.cr_start_pos; i < P.frame_size; i += 4) *cr++ = P.raw[i];
return 1;
}
Uint32 read_y42210(void)
{
Uint32 ret = 1;
Uint8* data;
Uint8* tmp;
data = malloc(sizeof(Uint8) * P.frame_size * 2);
if (!data) {
fprintf(stderr, "Error allocating memory...\n");
return 0;
}
tmp = malloc(sizeof(Uint8) * P.frame_size);
if (!tmp) {
fprintf(stderr, "Error allocating memory...\n");
ret = 0;
goto cleany42210;
}
if (!rd(data, P.frame_size * 2)) {
ret = 0;
goto cleany42210;
}
ten2eight(data, tmp, P.frame_size * 2);
/* Y */
for (Uint32 i = 0, j = 0; i < P.frame_size; i += 2) {
P.raw[i] = tmp[j];
j++;
}
/* Cb */
for (Uint32 i = P.cb_start_pos, j = 0 ; i < P.frame_size; i += 4) {
P.raw[i] = tmp[P.wh + j];
j++;
}
/* Cr */
for (Uint32 i = P.cr_start_pos, j = 0; i < P.frame_size; i += 4) {
P.raw[i] = tmp[P.wh/2*3 + j];
j++;
}
cleany42210:
free(tmp);
free(data);
return ret;
}
Uint32 read_yv1210(void)
{
Uint32 ret = 1;
Uint8* data;
data = malloc(sizeof(Uint8) * P.y_size * 2);
if (!data) {
fprintf(stderr, "Error allocating memory...\n");
return 0;
}
if (!rd(data, P.y_size * 2)) {
ret = 0;
goto cleanyv1210;
}
ten2eight(data, P.y_data, P.y_size * 2);
if (!rd(data, P.cb_size * 2)) {
ret = 0;
goto cleanyv1210;
}
ten2eight(data, P.cb_data, P.cb_size * 2);
if (!rd(data, P.cr_size * 2)) {
ret = 0;
goto cleanyv1210;
}
ten2eight(data, P.cr_data, P.cr_size * 2);
cleanyv1210:
free(data);
return ret;
}
Uint32 ten2eight(Uint8* src, Uint8* dst, Uint32 length)
{
Uint16 x = 0;
for (Uint32 i = 0; i < length; i += 2) {
x = (src[i+1] << 8) | src[i];
x = (x + 2) >> 2;
if (x > 255) {
x = 255;
}
*dst++ = x;
}
return 1;
}
Uint32 allocate_memory(void)
{
P.raw = malloc(sizeof(Uint8) * P.frame_size);
P.y_data = malloc(sizeof(Uint8) * P.y_size);
P.cb_data = malloc(sizeof(Uint8) * P.cb_size);
P.cr_data = malloc(sizeof(Uint8) * P.cr_size);
if (!P.raw || !P.y_data || !P.cb_data || !P.cr_data) {
fprintf(stderr, "Error allocating memory...\n");
return 0;
}
return 1;
}
void draw_grid422(void)
{
if (!P.grid) {
return;
}
/* horizontal grid lines */
for (Uint32 y = 0; y < P.height; y += 16) {
for (Uint32 x = P.grid_start_pos; x < P.width * 2; x += 16) {
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x) = 0xF0;
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x + 8) = 0x20;
}
}
/* vertical grid lines */
for (Uint32 x = P.grid_start_pos; x < P.width * 2; x += 32) {
for (Uint32 y = 0; y < P.height; y += 8) {
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x) = 0xF0;
*(my_overlay->pixels[0] + (y + 4) * my_overlay->pitches[0] + x) = 0x20;
}
}
}
void draw_grid420(void)
{
if (!P.grid) {
return;
}
/* horizontal grid lines */
for (Uint32 y = 0; y < P.height; y += 16) {
for (Uint32 x = 0; x < P.width; x += 8) {
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x) = 0xF0;
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x + 4) = 0x20;
}
}
/* vertical grid lines */
for (Uint32 x = 0; x < P.width; x += 16) {
for (Uint32 y = 0; y < P.height; y += 8) {
*(my_overlay->pixels[0] + y * my_overlay->pitches[0] + x) = 0xF0;
*(my_overlay->pixels[0] + (y + 4) * my_overlay->pitches[0] + x) = 0x20;
}
}
}
void luma_only(void)
{
if (!P.y_only) {
return;
}
if (FORMAT == YV12 || FORMAT == IYUV || FORMAT == YV1210) {
/* Set croma part to 0x80 */
for (Uint32 i = 0; i < P.cr_size; i++) my_overlay->pixels[1][i] = 0x80;
for (Uint32 i = 0; i < P.cb_size; i++) my_overlay->pixels[2][i] = 0x80;
return;
}
/* YUY2, UYVY, YVYU */
for (Uint32 i = P.cb_start_pos; i < P.frame_size; i += 4) {
*(my_overlay->pixels[0] + i) = 0x80;
}
for (Uint32 i = P.cr_start_pos; i < P.frame_size; i += 4) {
*(my_overlay->pixels[0] + i) = 0x80;
}
}
void cb_only(void)
{
if (!P.cb_only) {
return;
}
if (FORMAT == YV12 || FORMAT == IYUV || FORMAT == YV1210) {
/* Set Luma part and Cr to 0x80 */
for (Uint32 i = 0; i < P.y_size; i++) my_overlay->pixels[0][i] = 0x80;
for (Uint32 i = 0; i < P.cr_size; i++) my_overlay->pixels[1][i] = 0x80;
return;
}
/* YUY2, UYVY, YVYU */
for (Uint32 i = P.y_start_pos; i < P.frame_size; i += 2) {
*(my_overlay->pixels[0] + i) = 0x80;
}
for (Uint32 i = P.cr_start_pos; i < P.frame_size; i += 4) {
*(my_overlay->pixels[0] + i) = 0x80;
}
}
void cr_only(void)
{
if (!P.cr_only) {
return;
}
if (FORMAT == YV12 || FORMAT == IYUV || FORMAT == YV1210) {
/* Set Luma part and Cb to 0x80 */
for (Uint32 i = 0; i < P.y_size; i++) my_overlay->pixels[0][i] = 0x80;
for (Uint32 i = 0; i < P.cb_size; i++) my_overlay->pixels[2][i] = 0x80;
return;
}
/* YUY2, UYVY, YVYU */
for (Uint32 i = P.y_start_pos; i < P.frame_size; i += 2) {
*(my_overlay->pixels[0] + i) = 0x80;
}
for (Uint32 i = P.cb_start_pos; i < P.frame_size; i += 4) {
*(my_overlay->pixels[0] + i) = 0x80;
}
}
void draw_420(void)
{
memcpy(my_overlay->pixels[0], P.y_data, P.y_size);
memcpy(my_overlay->pixels[1], P.cr_data, P.cr_size);
memcpy(my_overlay->pixels[2], P.cb_data, P.cb_size);
draw_grid420();
luma_only();
cb_only();
cr_only();
histogram();
}
void draw_422(void)
{
memcpy(my_overlay->pixels[0], P.raw, P.frame_size);
draw_grid422();
luma_only();
cb_only();
cr_only();
histogram();
}
void usage(char* name)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s filename width height format [diff_filename]\n", name);
}
void mb_loop(char* str, Uint32 rows, Uint8* data, Uint32 pitch)
{
printf("%s\n", str);
for (Uint32 i = 0; i < rows;i++) {
for (Uint32 j = 0; j < 16; j++) {
printf("%02X ", data[pitch+i]);
}
printf("\n");
}
}
void show_mb(Uint32 mouse_x, Uint32 mouse_y)
{
/* TODO: bad code */
Uint32 MB;
Uint32 pitch[5] = {64, 64, 128, 128, 128};
Uint32 length[5] = {4, 4, 8, 8, 8};
Uint16 y_pitch, cb_pitch, cr_pitch;
if (!P.mb) {
return;
}
/* which MB are we in? */
MB = mouse_x / (16 * P.zoom) +
(P.width / 16) *
(mouse_y / (16 * P.zoom));
y_pitch = 16 * MB;
cb_pitch = pitch[FORMAT] * MB;
cr_pitch = pitch[FORMAT] * MB;
printf("\nMB #%d\n", MB);
mb_loop("= Y =", 16, P.y_data, y_pitch);
mb_loop("= Cb =", length[FORMAT], P.cb_data, cb_pitch);
mb_loop("= Cr =", length[FORMAT], P.cr_data, cr_pitch);
printf("\n");
fflush(stdout);
}
Uint32 (*reader[])(void) = {read_yv12, read_iyuv, read_422, read_422, read_422, read_yv1210, read_y42210};
void (*drawer[])(void) = {draw_420, draw_420, draw_422, draw_422, draw_422, draw_420, draw_422};
void draw_frame(void)
{
SDL_LockYUVOverlay(my_overlay);
(*drawer[FORMAT])();
set_zoom_rect();
video_rect.x = 0;
video_rect.y = 0;
video_rect.w = P.zoom_width;
video_rect.h = P.zoom_height;
SDL_UnlockYUVOverlay(my_overlay);
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
}
Uint32 read_frame(void)
{
if (!P.diff) {
return (*reader[FORMAT])();
} else {
return diff_mode();
}
}
Uint32 diff_mode(void)
{
FILE* fd_tmp;
Uint8* y_tmp;
/* Perhaps a bit ugly but it seams to work...
* 1. read frame from fd
* 2. store data away
* 3. read frame from P.fd2
* 4. calculate diff
* 5. place result in P.raw or P.y_data depending on FORMAT
* 6. diff works on luma data so clear P.cb_data and P.cr_data
* Fiddle with the file descriptors so that we read
* from correct file.
*/
if (!(*reader[FORMAT])()) {
return 0;
}
y_tmp = malloc(sizeof(Uint8) * P.y_size);
if (!y_tmp) {
fprintf(stderr, "Error allocating memory...\n");
return 0;
}
for (Uint32 i = 0; i < P.y_size; i++) {
y_tmp[i] = P.y_data[i];
}
fd_tmp = fd;
fd = P.fd2;
if (!(*reader[FORMAT])()) {
free(y_tmp);
fd = fd_tmp;
return 0;
}
/* restore file descriptor */
fd = fd_tmp;
/* now, P.y_data contains luminance data for fd2 and
* y_tmp contains luma data for fd.
* Calculate diff and place result where it belongs
* Clear croma data */
calc_psnr(y_tmp, P.y_data);
if (FORMAT == YV12 || FORMAT == IYUV) {
for (Uint32 i = 0; i < P.y_size; i++) {
P.y_data[i] = 0x80 - (y_tmp[i] - P.y_data[i]);
}
for (Uint32 i = 0; i < P.cb_size; i++) P.cb_data[i] = 0x80;
for (Uint32 i = 0; i < P.cr_size; i++) P.cr_data[i] = 0x80;
} else {
Uint32 j = 0;
for (Uint32 i = P.y_start_pos; i < P.frame_size; i += 2) {
P.raw[i] = 0x80 - (y_tmp[j] - P.y_data[j]);
j++;
}
for (Uint32 i = P.cb_start_pos; i < P.frame_size; i += 4) P.raw[i] = 0x80;
for (Uint32 i = P.cr_start_pos; i < P.frame_size; i += 4) P.raw[i] = 0x80;
}
free(y_tmp);
return 1;
}
void calc_psnr(Uint8* frame0, Uint8* frame1)
{
double mse = 0.0;
double mse_tmp = 0.0;
double psnr = 0.0;
for (Uint32 i = 0; i < P.y_size; i++) {
mse_tmp = abs(frame0[i] - frame1[i]);
mse += mse_tmp * mse_tmp;
}
/* division by zero */
if (mse == 0) {
fprintf(stdout, "PSNR: NaN\n");
return;
}
mse /= P.y_size;
psnr = 10.0*log10((256 * 256) / mse);
fprintf(stdout, "PSNR: %f\n", psnr);
}
void histogram(void)
{
if (!P.hist) {
return;
}
Uint8 y[256] = {0};
Uint8 b[256] = {0};
Uint8 r[256] = {0};
for (Uint32 i = 0; i < P.y_size; i++) y[P.y_data[i]]++;
for (Uint32 i = 0; i < P.cb_size; i++) b[P.cb_data[i]]++;
for (Uint32 i = 0; i < P.cr_size; i++) r[P.cr_data[i]]++;
fprintf(stdout, "\nY,");
for (Uint32 i = 0; i < 256; i++) fprintf(stdout, "%u,", y[i]);
fprintf(stdout, "\nCb,");
for (Uint32 i = 0; i < 256; i++) fprintf(stdout, "%u,", b[i]);
fprintf(stdout, "\nCr,");
for (Uint32 i = 0; i < 256; i++) fprintf(stdout, "%u,", r[i]);
fprintf(stdout, "\n");
fflush(stdout);
}
void setup_param(void)
{
P.zoom = 1;
P.wh = P.width * P.height;
if (FORMAT == YV12 || FORMAT == IYUV) {
P.frame_size = P.wh * 3 / 2;
P.y_size = P.wh;
P.cb_size = P.wh / 4;
P.cr_size = P.wh / 4;
} else if (FORMAT == YUY2 || FORMAT == UYVY || FORMAT == YVYU || FORMAT == Y42210) {
P.grid_start_pos = 0;
P.frame_size = P.wh * 2;
P.y_size = P.wh;
P.cb_size = P.wh / 2;
P.cr_size = P.wh / 2;
} else if (FORMAT == YV1210) {
/* SDL cannot natively display this format; let's fake it
* and pretend it's YV12 */
P.frame_size = P.wh * 3 / 2;
P.y_size = P.wh;
P.cb_size = P.wh / 4;
P.cr_size = P.wh / 4;
}
if (FORMAT == YUY2) {
/* Y U Y V
* 0 1 2 3 */
P.y_start_pos = 0;
P.cb_start_pos = 1;
P.cr_start_pos = 3;
} else if (FORMAT == UYVY) {
/* U Y V Y
* 0 1 2 3 */
P.y_start_pos = 1;
P.grid_start_pos = 1;
P.cb_start_pos = 0;
P.cr_start_pos = 2;
} else if (FORMAT == YVYU || FORMAT == Y42210) {
/* Y V Y U
* 0 1 2 3 */
P.y_start_pos = 0;
P.cb_start_pos = 3;
P.cr_start_pos = 1;
}
}
void check_input(void)
{
Uint32 file_size;
/* Frame Size is an even multipe of 16x16? */
if (P.width % 16 != 0) {
fprintf(stderr, "WIDTH not multiple of 16, check input...\n");
}
if (P.height % 16 != 0) {
fprintf(stderr, "HEIGHT not multiple of 16, check input...\n");
}
/* Even number of frames? */
fseek(fd, 0L, SEEK_END);
file_size = ftell(fd);
fseek(fd, 0L, SEEK_SET);
if (file_size % P.frame_size != 0) {
fprintf(stderr, "#FRAMES not an integer, check input...\n");
}
}
Uint32 create_message_queue(void)
{
/* Should probably use argv[0] or similar as pathname
* when creating the key;
* but let's keep it simple for now. Y for YCbCr
*/
if ((P.key = ftok("/tmp", 'Y')) == -1) {
perror("ftok");
return 0;
}
if ((P.msqid = msgget(P.key, 0644 | IPC_CREAT)) == -1) {
perror("msgget");
return 0;
}
/* we don't really care in this case */
P.buf.mtype = 1;
return 1;
}
Uint32 connect_message_queue(void)
{
if ((P.key = ftok("/tmp", 'Y')) == -1) {
perror("ftok");
return 0;
}
/* connect to the queue */
if ((P.msqid = msgget(P.key, 0644)) == -1) {
perror("msgget");
return 0;
}
printf("Ready to receive messages, captain.\n");
return 1;
}
Uint32 send_message(char cmd)
{
if (P.mode != MASTER) {
return 1;
}
P.buf.mtext[0] = cmd;
P.buf.mtext[1] = '\0';
if (msgsnd(P.msqid, &P.buf, 2, 0) == -1) {
perror("msgsnd");
return 0;
}
return 1;
}
Uint32 read_message(void)
{
if (msgrcv(P.msqid, &P.buf, sizeof(P.buf.mtext), 0, 0) == -1) {
perror("msgrcv");
P.mode = NONE;
return 0;
}
return 1;
}
void destroy_message_queue(void)
{
if (P.mode == MASTER) {
if (msgctl(P.msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
}
printf("queue destroyed\n");
}
}
Uint32 event_dispatcher(void)
{
if (read_message()) {
switch (P.buf.mtext[0])
{
case NEXT:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_RIGHT;
SDL_PushEvent(&event);
break;
case PREV:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_LEFT;
SDL_PushEvent(&event);
break;
case REW:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_r;
SDL_PushEvent(&event);
break;
case ZOOM_IN:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_UP;
SDL_PushEvent(&event);
break;
case ZOOM_OUT:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_DOWN;
SDL_PushEvent(&event);
break;
case QUIT:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_q;
SDL_PushEvent(&event);
break;
case Y_ONLY:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_F5;
SDL_PushEvent(&event);
break;
case CB_ONLY:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_F6;
SDL_PushEvent(&event);
break;
case CR_ONLY:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_F7;
SDL_PushEvent(&event);
break;
case ALL_PLANES:
event.type = SDL_KEYDOWN;
event.key.keysym.sym = SDLK_F8;
SDL_PushEvent(&event);
break;
default:
fprintf(stderr, "~TILT\n");
return 0;
}
}
return 1;
}
void set_caption(char *array, Uint32 frame, Uint32 bytes)
{
snprintf(array, bytes, "%s - %s%s%s%s%s%s%s%s frame %d, size %dx%d",
P.filename,
(P.mode == MASTER) ? "[MASTER]" :
(P.mode == SLAVE) ? "[SLAVE]": "",
P.grid ? "G" : "",
P.mb ? "M" : "",
P.diff ? "D" : "",
P.hist ? "H" : "",
P.y_only ? "Y" : "",
P.cb_only ? "Cb" : "",
P.cr_only ? "Cr" : "",
frame,
P.zoom_width,
P.zoom_height);
}
void set_zoom_rect(void)
{
if (P.zoom > 0) {
P.zoom_width = P.width * P.zoom;
P.zoom_height = P.height * P.zoom;
} else if (P.zoom <= 0) {
P.zoom_width = P.width / (abs(P.zoom) + 2);
P.zoom_height = P.height / (abs(P.zoom) + 2);
} else {
fprintf(stderr, "ERROR in zoom:\n");
}
}
/* loop inspired by yay
* http://freecode.com/projects/yay
*/
Uint32 event_loop(void)
{
char caption[256];
Uint16 quit = 0;
Uint32 frame = 0;
int play_yuv = 0;
unsigned int start_ticks = 0;
while (!quit) {
set_caption(caption, frame, 256);
SDL_WM_SetCaption(caption, NULL);
/* wait for SDL event */
if (P.mode == NONE || P.mode == MASTER) {
SDL_WaitEvent(&event);
} else if (P.mode == SLAVE) {
if (!event_dispatcher()) {
SDL_WaitEvent(&event);
}
}
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_SPACE:
play_yuv = 1; /* play it, sam! */
while (play_yuv) {
start_ticks = SDL_GetTicks();
set_caption(caption, frame, 256);
SDL_WM_SetCaption( caption, NULL );
/* check for next frame existing */
if (read_frame()) {
draw_frame();
/* insert delay for real time viewing */
if (SDL_GetTicks() - start_ticks < 40)
SDL_Delay(40 - (SDL_GetTicks() - start_ticks));
frame++;
send_message(NEXT);
} else {
play_yuv = 0;
}
/* check for any key event */
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
/* stop playing */
play_yuv = 0;
}
}
}
break;
case SDLK_RIGHT: /* next frame */
/* check for next frame existing */
if (read_frame()) {
draw_frame();
frame++;
send_message(NEXT);
}
break;
case SDLK_LEFT: /* previous frame */
if (frame > 1) {
frame--;
fseek(fd, ((frame-1) * P.frame_size), SEEK_SET);
if (P.diff) {
fseek(P.fd2, ((frame-1) * P.frame_size), SEEK_SET);
}
read_frame();
draw_frame();
send_message(PREV);
}
break;
case SDLK_UP: /* zoom in */
P.zoom++;
set_zoom_rect();
screen = SDL_SetVideoMode(P.zoom_width,
P.zoom_height,
P.bpp, P.vflags);
video_rect.w = P.zoom_width;
video_rect.h = P.zoom_height;
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
send_message(ZOOM_IN);
break;
case SDLK_DOWN: /* zoom out */
P.zoom--;
set_zoom_rect();
screen = SDL_SetVideoMode(P.zoom_width,
P.zoom_height,
P.bpp, P.vflags);
video_rect.w = P.zoom_width;
video_rect.h = P.zoom_height;
SDL_DisplayYUVOverlay(my_overlay, &video_rect);
send_message(ZOOM_OUT);
break;
case SDLK_r: /* rewind */
if (frame > 1) {
frame = 1;
fseek(fd, 0, SEEK_SET);
if (P.diff) {
fseek(P.fd2, 0, SEEK_SET);
}
read_frame();
draw_frame();
send_message(REW);
}
break;
case SDLK_g: /* display grid */
P.grid = ~P.grid;
if (P.zoom < 1)
P.grid = 0;
draw_frame();
break;
case SDLK_m: /* show mb-data on stdout */
P.mb = ~P.mb;
if (P.zoom < 1)
P.mb = 0;
draw_frame();
break;
case SDLK_F5: /* Luma data only */
P.y_only = ~P.y_only;
P.cb_only = 0;
P.cr_only = 0;
draw_frame();
send_message(Y_ONLY);
break;
case SDLK_F6: /* Cb data only */
P.cb_only = ~P.cb_only;
P.y_only = 0;
P.cr_only = 0;
draw_frame();
send_message(CB_ONLY);
break;
case SDLK_F7: /* Cr data only */
P.cr_only = ~P.cr_only;
P.y_only = 0;