/
usbvision-core.c
2641 lines (2268 loc) · 72.1 KB
/
usbvision-core.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
/*
* usbvision-core.c - driver for NT100x USB video capture devices
*
*
* Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
* Dwaine Garden <dwainegarden@rogers.com>
*
* This module is part of usbvision driver project.
* Updates to driver completed by Dwaine P. Garden
*
* 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 <linux/kernel.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/utsname.h>
#include <linux/highmem.h>
#include <linux/videodev.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <asm/io.h>
#include <linux/videodev2.h>
#include <linux/video_decoder.h>
#include <linux/i2c.h>
#include <media/saa7115.h>
#include <media/v4l2-common.h>
#include <media/tuner.h>
#include <linux/workqueue.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif
#include "usbvision.h"
static unsigned int core_debug;
module_param(core_debug,int,0644);
MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
static unsigned int force_testpattern;
module_param(force_testpattern,int,0644);
MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]");
static int adjustCompression = 1; /* Set the compression to be adaptive */
module_param(adjustCompression, int, 0444);
MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)");
/* To help people with Black and White output with using s-video input.
* Some cables and input device are wired differently. */
static int SwitchSVideoInput;
module_param(SwitchSVideoInput, int, 0444);
MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
static unsigned int adjust_X_Offset = -1;
module_param(adjust_X_Offset, int, 0644);
MODULE_PARM_DESC(adjust_X_Offset, "adjust X offset display [core]");
static unsigned int adjust_Y_Offset = -1;
module_param(adjust_Y_Offset, int, 0644);
MODULE_PARM_DESC(adjust_Y_Offset, "adjust Y offset display [core]");
#define ENABLE_HEXDUMP 0 /* Enable if you need it */
#ifdef USBVISION_DEBUG
#define PDEBUG(level, fmt, args...) { \
if (core_debug & (level)) \
info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
}
#else
#define PDEBUG(level, fmt, args...) do {} while(0)
#endif
#define DBG_HEADER 1<<0
#define DBG_IRQ 1<<1
#define DBG_ISOC 1<<2
#define DBG_PARSE 1<<3
#define DBG_SCRATCH 1<<4
#define DBG_FUNC 1<<5
static const int max_imgwidth = MAX_FRAME_WIDTH;
static const int max_imgheight = MAX_FRAME_HEIGHT;
static const int min_imgwidth = MIN_FRAME_WIDTH;
static const int min_imgheight = MIN_FRAME_HEIGHT;
/* The value of 'scratch_buf_size' affects quality of the picture
* in many ways. Shorter buffers may cause loss of data when client
* is too slow. Larger buffers are memory-consuming and take longer
* to work with. This setting can be adjusted, but the default value
* should be OK for most desktop users.
*/
#define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
// Function prototypes
static int usbvision_request_intra (struct usb_usbvision *usbvision);
static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
/*******************************/
/* Memory management functions */
/*******************************/
/*
* Here we want the physical address of the memory.
* This is used when initializing the contents of the area.
*/
static void *usbvision_rvmalloc(unsigned long size)
{
void *mem;
unsigned long adr;
size = PAGE_ALIGN(size);
mem = vmalloc_32(size);
if (!mem)
return NULL;
memset(mem, 0, size); /* Clear the ram out, no junk to the user */
adr = (unsigned long) mem;
while (size > 0) {
SetPageReserved(vmalloc_to_page((void *)adr));
adr += PAGE_SIZE;
size -= PAGE_SIZE;
}
return mem;
}
static void usbvision_rvfree(void *mem, unsigned long size)
{
unsigned long adr;
if (!mem)
return;
size = PAGE_ALIGN(size);
adr = (unsigned long) mem;
while ((long) size > 0) {
ClearPageReserved(vmalloc_to_page((void *)adr));
adr += PAGE_SIZE;
size -= PAGE_SIZE;
}
vfree(mem);
}
#if ENABLE_HEXDUMP
static void usbvision_hexdump(const unsigned char *data, int len)
{
char tmp[80];
int i, k;
for (i = k = 0; len > 0; i++, len--) {
if (i > 0 && (i % 16 == 0)) {
printk("%s\n", tmp);
k = 0;
}
k += sprintf(&tmp[k], "%02x ", data[i]);
}
if (k > 0)
printk("%s\n", tmp);
}
#endif
/********************************
* scratch ring buffer handling
********************************/
static int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */
{
int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
if (len < 0) {
len += scratch_buf_size;
}
PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
return len;
}
/* This returns the free space left in the buffer */
static int scratch_free(struct usb_usbvision *usbvision)
{
int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
if (free <= 0) {
free += scratch_buf_size;
}
if (free) {
free -= 1; /* at least one byte in the buffer must */
/* left blank, otherwise there is no chance to differ between full and empty */
}
PDEBUG(DBG_SCRATCH, "return %d\n", free);
return free;
}
/* This puts data into the buffer */
static int scratch_put(struct usb_usbvision *usbvision, unsigned char *data,
int len)
{
int len_part;
if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
usbvision->scratch_write_ptr += len;
}
else {
len_part = scratch_buf_size - usbvision->scratch_write_ptr;
memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
if (len == len_part) {
usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
}
else {
memcpy(usbvision->scratch, data + len_part, len - len_part);
usbvision->scratch_write_ptr = len - len_part;
}
}
PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
return len;
}
/* This marks the write_ptr as position of new frame header */
static void scratch_mark_header(struct usb_usbvision *usbvision)
{
PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
usbvision->scratch_write_ptr;
usbvision->scratch_headermarker_write_ptr += 1;
usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
}
/* This gets data from the buffer at the given "ptr" position */
static int scratch_get_extra(struct usb_usbvision *usbvision,
unsigned char *data, int *ptr, int len)
{
int len_part;
if (*ptr + len < scratch_buf_size) {
memcpy(data, usbvision->scratch + *ptr, len);
*ptr += len;
}
else {
len_part = scratch_buf_size - *ptr;
memcpy(data, usbvision->scratch + *ptr, len_part);
if (len == len_part) {
*ptr = 0; /* just set the y_ptr to zero */
}
else {
memcpy(data + len_part, usbvision->scratch, len - len_part);
*ptr = len - len_part;
}
}
PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
return len;
}
/* This sets the scratch extra read pointer */
static void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr,
int len)
{
*ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size;
PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
}
/*This increments the scratch extra read pointer */
static void scratch_inc_extra_ptr(int *ptr, int len)
{
*ptr = (*ptr + len) % scratch_buf_size;
PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
}
/* This gets data from the buffer */
static int scratch_get(struct usb_usbvision *usbvision, unsigned char *data,
int len)
{
int len_part;
if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
usbvision->scratch_read_ptr += len;
}
else {
len_part = scratch_buf_size - usbvision->scratch_read_ptr;
memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
if (len == len_part) {
usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
}
else {
memcpy(data + len_part, usbvision->scratch, len - len_part);
usbvision->scratch_read_ptr = len - len_part;
}
}
PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
return len;
}
/* This sets read pointer to next header and returns it */
static int scratch_get_header(struct usb_usbvision *usbvision,
struct usbvision_frame_header *header)
{
int errCode = 0;
PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
while (usbvision->scratch_headermarker_write_ptr -
usbvision->scratch_headermarker_read_ptr != 0) {
usbvision->scratch_read_ptr =
usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
usbvision->scratch_headermarker_read_ptr += 1;
usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
if ((header->magic_1 == USBVISION_MAGIC_1)
&& (header->magic_2 == USBVISION_MAGIC_2)
&& (header->headerLength == USBVISION_HEADER_LENGTH)) {
errCode = USBVISION_HEADER_LENGTH;
header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8);
header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8);
break;
}
}
return errCode;
}
/*This removes len bytes of old data from the buffer */
static void scratch_rm_old(struct usb_usbvision *usbvision, int len)
{
usbvision->scratch_read_ptr += len;
usbvision->scratch_read_ptr %= scratch_buf_size;
PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
}
/*This resets the buffer - kills all data in it too */
static void scratch_reset(struct usb_usbvision *usbvision)
{
PDEBUG(DBG_SCRATCH, "\n");
usbvision->scratch_read_ptr = 0;
usbvision->scratch_write_ptr = 0;
usbvision->scratch_headermarker_read_ptr = 0;
usbvision->scratch_headermarker_write_ptr = 0;
usbvision->isocstate = IsocState_NoFrame;
}
int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
{
usbvision->scratch = vmalloc_32(scratch_buf_size);
scratch_reset(usbvision);
if(usbvision->scratch == NULL) {
err("%s: unable to allocate %d bytes for scratch",
__func__, scratch_buf_size);
return -ENOMEM;
}
return 0;
}
void usbvision_scratch_free(struct usb_usbvision *usbvision)
{
if (usbvision->scratch != NULL) {
vfree(usbvision->scratch);
usbvision->scratch = NULL;
}
}
/*
* usbvision_testpattern()
*
* Procedure forms a test pattern (yellow grid on blue background).
*
* Parameters:
* fullframe: if TRUE then entire frame is filled, otherwise the procedure
* continues from the current scanline.
* pmode 0: fill the frame with solid blue color (like on VCR or TV)
* 1: Draw a colored grid
*
*/
static void usbvision_testpattern(struct usb_usbvision *usbvision,
int fullframe, int pmode)
{
static const char proc[] = "usbvision_testpattern";
struct usbvision_frame *frame;
unsigned char *f;
int num_cell = 0;
int scan_length = 0;
static int num_pass;
if (usbvision == NULL) {
printk(KERN_ERR "%s: usbvision == NULL\n", proc);
return;
}
if (usbvision->curFrame == NULL) {
printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc);
return;
}
/* Grab the current frame */
frame = usbvision->curFrame;
/* Optionally start at the beginning */
if (fullframe) {
frame->curline = 0;
frame->scanlength = 0;
}
/* Form every scan line */
for (; frame->curline < frame->frmheight; frame->curline++) {
int i;
f = frame->data + (usbvision->curwidth * 3 * frame->curline);
for (i = 0; i < usbvision->curwidth; i++) {
unsigned char cb = 0x80;
unsigned char cg = 0;
unsigned char cr = 0;
if (pmode == 1) {
if (frame->curline % 32 == 0)
cb = 0, cg = cr = 0xFF;
else if (i % 32 == 0) {
if (frame->curline % 32 == 1)
num_cell++;
cb = 0, cg = cr = 0xFF;
} else {
cb =
((num_cell * 7) +
num_pass) & 0xFF;
cg =
((num_cell * 5) +
num_pass * 2) & 0xFF;
cr =
((num_cell * 3) +
num_pass * 3) & 0xFF;
}
} else {
/* Just the blue screen */
}
*f++ = cb;
*f++ = cg;
*f++ = cr;
scan_length += 3;
}
}
frame->grabstate = FrameState_Done;
frame->scanlength += scan_length;
++num_pass;
}
/*
* usbvision_decompress_alloc()
*
* allocates intermediate buffer for decompression
*/
int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
{
int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
usbvision->IntraFrameBuffer = vmalloc_32(IFB_size);
if (usbvision->IntraFrameBuffer == NULL) {
err("%s: unable to allocate %d for compr. frame buffer",
__func__, IFB_size);
return -ENOMEM;
}
return 0;
}
/*
* usbvision_decompress_free()
*
* frees intermediate buffer for decompression
*/
void usbvision_decompress_free(struct usb_usbvision *usbvision)
{
if (usbvision->IntraFrameBuffer != NULL) {
vfree(usbvision->IntraFrameBuffer);
usbvision->IntraFrameBuffer = NULL;
}
}
/************************************************************
* Here comes the data parsing stuff that is run as interrupt
************************************************************/
/*
* usbvision_find_header()
*
* Locate one of supported header markers in the scratch buffer.
*/
static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
{
struct usbvision_frame *frame;
int foundHeader = 0;
frame = usbvision->curFrame;
while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
// found header in scratch
PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
frame->isocHeader.magic_2,
frame->isocHeader.magic_1,
frame->isocHeader.headerLength,
frame->isocHeader.frameNum,
frame->isocHeader.framePhase,
frame->isocHeader.frameLatency,
frame->isocHeader.dataFormat,
frame->isocHeader.formatParam,
frame->isocHeader.frameWidth,
frame->isocHeader.frameHeight);
if (usbvision->requestIntra) {
if (frame->isocHeader.formatParam & 0x80) {
foundHeader = 1;
usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time
usbvision_unrequest_intra(usbvision);
break;
}
}
else {
foundHeader = 1;
break;
}
}
if (foundHeader) {
frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
}
else { // no header found
PDEBUG(DBG_HEADER, "skipping scratch data, no header");
scratch_reset(usbvision);
return ParseState_EndParse;
}
// found header
if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) {
//check isocHeader.frameNum for lost frames
if (usbvision->lastIsocFrameNum >= 0) {
if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) {
// unexpected frame drop: need to request new intra frame
PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum);
usbvision_request_intra(usbvision);
return ParseState_NextFrame;
}
}
usbvision->lastIsocFrameNum = frame->isocHeader.frameNum;
}
usbvision->header_count++;
frame->scanstate = ScanState_Lines;
frame->curline = 0;
if (force_testpattern) {
usbvision_testpattern(usbvision, 1, 1);
return ParseState_NextFrame;
}
return ParseState_Continue;
}
static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision,
long *pcopylen)
{
volatile struct usbvision_frame *frame;
unsigned char *f;
int len;
int i;
unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
unsigned char rv, gv, bv; // RGB components
int clipmask_index, bytes_per_pixel;
int stretch_bytes, clipmask_add;
frame = usbvision->curFrame;
f = frame->data + (frame->v4l2_linesize * frame->curline);
/* Make sure there's enough data for the entire line */
len = (frame->isocHeader.frameWidth * 2)+5;
if (scratch_len(usbvision) < len) {
PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
return ParseState_Out;
}
if ((frame->curline + 1) >= frame->frmheight) {
return ParseState_NextFrame;
}
bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
clipmask_index = frame->curline * MAX_FRAME_WIDTH;
clipmask_add = usbvision->stretch_width;
for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) {
scratch_get(usbvision, &yuyv[0], 4);
if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
*f++ = yuyv[0]; // Y
*f++ = yuyv[3]; // U
}
else {
YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
switch (frame->v4l2_format.format) {
case V4L2_PIX_FMT_RGB565:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x07 & (gv >> 3)) |
(0xF8 & bv);
break;
case V4L2_PIX_FMT_RGB24:
*f++ = rv;
*f++ = gv;
*f++ = bv;
break;
case V4L2_PIX_FMT_RGB32:
*f++ = rv;
*f++ = gv;
*f++ = bv;
f++;
break;
case V4L2_PIX_FMT_RGB555:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x03 & (gv >> 3)) |
(0x7C & (bv << 2));
break;
}
}
clipmask_index += clipmask_add;
f += stretch_bytes;
if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
*f++ = yuyv[2]; // Y
*f++ = yuyv[1]; // V
}
else {
YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
switch (frame->v4l2_format.format) {
case V4L2_PIX_FMT_RGB565:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x07 & (gv >> 3)) |
(0xF8 & bv);
break;
case V4L2_PIX_FMT_RGB24:
*f++ = rv;
*f++ = gv;
*f++ = bv;
break;
case V4L2_PIX_FMT_RGB32:
*f++ = rv;
*f++ = gv;
*f++ = bv;
f++;
break;
case V4L2_PIX_FMT_RGB555:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x03 & (gv >> 3)) |
(0x7C & (bv << 2));
break;
}
}
clipmask_index += clipmask_add;
f += stretch_bytes;
}
frame->curline += usbvision->stretch_height;
*pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
if (frame->curline >= frame->frmheight) {
return ParseState_NextFrame;
}
else {
return ParseState_Continue;
}
}
/* The decompression routine */
static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
unsigned char *Decompressed, int *StartPos,
int *BlockTypeStartPos, int Len)
{
int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen;
unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator;
Integrator = 0;
Pos = *StartPos;
BlockTypePos = *BlockTypeStartPos;
MaxPos = 396; //Pos + Len;
ExtraPos = Pos;
BlockLen = 0;
BlockByte = 0;
BlockCode = 0;
BlockType = 0;
BlockTypeByte = 0;
BlockTypeLen = 0;
RestPixel = Len;
for (Idx = 0; Idx < Len; Idx++) {
if (BlockLen == 0) {
if (BlockTypeLen==0) {
BlockTypeByte = Compressed[BlockTypePos];
BlockTypePos++;
BlockTypeLen = 4;
}
BlockType = (BlockTypeByte & 0xC0) >> 6;
//statistic:
usbvision->ComprBlockTypes[BlockType]++;
Pos = ExtraPos;
if (BlockType == 0) {
if(RestPixel >= 24) {
Idx += 23;
RestPixel -= 24;
Integrator = Decompressed[Idx];
} else {
Idx += RestPixel - 1;
RestPixel = 0;
}
} else {
BlockCode = Compressed[Pos];
Pos++;
if (RestPixel >= 24) {
BlockLen = 24;
} else {
BlockLen = RestPixel;
}
RestPixel -= BlockLen;
ExtraPos = Pos + (BlockLen / 4);
}
BlockTypeByte <<= 2;
BlockTypeLen -= 1;
}
if (BlockLen > 0) {
if ((BlockLen%4) == 0) {
BlockByte = Compressed[Pos];
Pos++;
}
if (BlockType == 1) { //inter Block
Integrator = Decompressed[Idx];
}
switch (BlockByte & 0xC0) {
case 0x03<<6:
Integrator += Compressed[ExtraPos];
ExtraPos++;
break;
case 0x02<<6:
Integrator += BlockCode;
break;
case 0x00:
Integrator -= BlockCode;
break;
}
Decompressed[Idx] = Integrator;
BlockByte <<= 2;
BlockLen -= 1;
}
}
*StartPos = ExtraPos;
*BlockTypeStartPos = BlockTypePos;
return Idx;
}
/*
* usbvision_parse_compress()
*
* Parse compressed frame from the scratch buffer, put
* decoded RGB value into the current frame buffer and add the written
* number of bytes (RGB) to the *pcopylen.
*
*/
static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
long *pcopylen)
{
#define USBVISION_STRIP_MAGIC 0x5A
#define USBVISION_STRIP_LEN_MAX 400
#define USBVISION_STRIP_HEADER_LEN 3
struct usbvision_frame *frame;
unsigned char *f,*u = NULL ,*v = NULL;
unsigned char StripData[USBVISION_STRIP_LEN_MAX];
unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
int clipmask_index, bytes_per_pixel, rc;
int imageSize;
unsigned char rv, gv, bv;
static unsigned char *Y, *U, *V;
frame = usbvision->curFrame;
imageSize = frame->frmwidth * frame->frmheight;
if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
(frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) { // this is a planar format
//... v4l2_linesize not used here.
f = frame->data + (frame->width * frame->curline);
} else
f = frame->data + (frame->v4l2_linesize * frame->curline);
if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
// get base of u and b planes add halfoffset
u = frame->data
+ imageSize
+ (frame->frmwidth >>1) * frame->curline ;
v = u + (imageSize >>1 );
} else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
u = v + (imageSize >>2) ;
}
if (frame->curline == 0) {
usbvision_adjust_compression(usbvision);
}
if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) {
return ParseState_Out;
}
//get strip header without changing the scratch_read_ptr
scratch_set_extra_ptr(usbvision, &StripPtr, 0);
scratch_get_extra(usbvision, &StripHeader[0], &StripPtr,
USBVISION_STRIP_HEADER_LEN);
if (StripHeader[0] != USBVISION_STRIP_MAGIC) {
// wrong strip magic
usbvision->stripMagicErrors++;
return ParseState_NextFrame;
}
if (frame->curline != (int)StripHeader[2]) {
//line number missmatch error
usbvision->stripLineNumberErrors++;
}
StripLen = 2 * (unsigned int)StripHeader[1];
if (StripLen > USBVISION_STRIP_LEN_MAX) {
// strip overrun
// I think this never happens
usbvision_request_intra(usbvision);
}
if (scratch_len(usbvision) < StripLen) {
//there is not enough data for the strip
return ParseState_Out;
}
if (usbvision->IntraFrameBuffer) {
Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline;
U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2);
V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
}
else {
return ParseState_NextFrame;
}
bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
clipmask_index = frame->curline * MAX_FRAME_WIDTH;
scratch_get(usbvision, StripData, StripLen);
IdxEnd = frame->frmwidth;
BlockTypePos = USBVISION_STRIP_HEADER_LEN;
StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2;
BlockPos = StartBlockPos;
usbvision->BlockPos = BlockPos;
if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) {
//return ParseState_Continue;
}
if (StripLen > usbvision->maxStripLen) {
usbvision->maxStripLen = StripLen;
}
if (frame->curline%2) {
if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
//return ParseState_Continue;
}
}
else {
if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
//return ParseState_Continue;
}
}
if (BlockPos > usbvision->comprBlockPos) {
usbvision->comprBlockPos = BlockPos;
}
if (BlockPos > StripLen) {
usbvision->stripLenErrors++;
}
for (Idx = 0; Idx < IdxEnd; Idx++) {
if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
*f++ = Y[Idx];
*f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
}
else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
*f++ = Y[Idx];
if ( Idx & 0x01)
*u++ = U[Idx>>1] ;
else
*v++ = V[Idx>>1];
}
else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
*f++ = Y [Idx];
if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){
/* only need do this for 1 in 4 pixels */
/* intraframe buffer is YUV420 format */
*u++ = U[Idx >>1];
*v++ = V[Idx >>1];
}
}
else {
YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
switch (frame->v4l2_format.format) {
case V4L2_PIX_FMT_GREY:
*f++ = Y[Idx];
break;
case V4L2_PIX_FMT_RGB555:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x03 & (gv >> 3)) |
(0x7C & (bv << 2));
break;
case V4L2_PIX_FMT_RGB565:
*f++ = (0x1F & rv) |
(0xE0 & (gv << 5));
*f++ = (0x07 & (gv >> 3)) |
(0xF8 & bv);
break;
case V4L2_PIX_FMT_RGB24:
*f++ = rv;
*f++ = gv;
*f++ = bv;
break;
case V4L2_PIX_FMT_RGB32:
*f++ = rv;
*f++ = gv;
*f++ = bv;
f++;
break;
}
}
clipmask_index++;
}
/* Deal with non-integer no. of bytes for YUV420P */
if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
*pcopylen += frame->v4l2_linesize;
else
*pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
frame->curline += 1;
if (frame->curline >= frame->frmheight) {
return ParseState_NextFrame;