| 1 |
/* |
| 2 |
* Implementation of JPEG Lite decoding algorithm |
| 3 |
* |
| 4 |
* Author & Copyright (c) 2003 : Sylvain Munaut <nw8xx ]at[ 246tNt.com> |
| 5 |
* |
| 6 |
* v4l library adaptation: Jean-François Moine <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2.1 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 21 |
|
| 22 |
* Note this code was originally licensed under the GNU GPL instead of the |
| 23 |
* GNU LGPL, its license has been changed with permission, see the permission |
| 24 |
* mail at the end of this file. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* Original WebSite: nw802.sourceforge.net */ |
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include "libv4lconvert-priv.h" |
| 31 |
|
| 32 |
#define RING_QUEUE_ADVANCE_INDEX(rq,ind,n) (rq)->ind = ((rq)->ind + (n)) |
| 33 |
#define RING_QUEUE_DEQUEUE_BYTES(rq,n) RING_QUEUE_ADVANCE_INDEX(rq,ri,n) |
| 34 |
#define RING_QUEUE_PEEK(rq,ofs) ((rq)->queue[((ofs) + (rq)->ri)]) |
| 35 |
|
| 36 |
struct RingQueue { |
| 37 |
const unsigned char *queue; |
| 38 |
int length; |
| 39 |
int ri; |
| 40 |
}; |
| 41 |
|
| 42 |
/* ============================================================================ |
| 43 |
* RingQueue bit reader |
| 44 |
* ============================================================================ |
| 45 |
* All what is needed to read bit by nit from the RingQueue pump |
| 46 |
* provided by usbvideo |
| 47 |
* Critical part are macro and not functions to speed things up |
| 48 |
* Rem: Data are read from the RingQueue as if they were 16bits Little Endian |
| 49 |
* words. Most Significants Bits are outputed first. |
| 50 |
*/ |
| 51 |
|
| 52 |
/* Structure used to store what we need. */ |
| 53 |
/* (We may need multiple simultaneous instance from several cam) */ |
| 54 |
struct rqBitReader { |
| 55 |
int cur_bit; |
| 56 |
unsigned int cur_data; |
| 57 |
struct RingQueue *rq; |
| 58 |
}; |
| 59 |
|
| 60 |
static inline void rqBR_init( struct rqBitReader *br, struct RingQueue *rq ) |
| 61 |
{ |
| 62 |
br->cur_bit = 16; |
| 63 |
br->cur_data = |
| 64 |
RING_QUEUE_PEEK( rq, 2 ) | |
| 65 |
RING_QUEUE_PEEK( rq, 3 ) << 8 | |
| 66 |
RING_QUEUE_PEEK( rq, 0 ) << 16 | |
| 67 |
RING_QUEUE_PEEK( rq, 1 ) << 24 ; |
| 68 |
RING_QUEUE_DEQUEUE_BYTES( rq, 2 ); |
| 69 |
br->rq = rq; |
| 70 |
} |
| 71 |
|
| 72 |
#define rqBR_peekBits(br,n) ( br->cur_data >> (32-n) ) |
| 73 |
|
| 74 |
#define rqBR_flushBits(br,n) do { \ |
| 75 |
br->cur_data <<= n; \ |
| 76 |
if ( (br->cur_bit -= n) <= 0 ) { \ |
| 77 |
br->cur_data |= \ |
| 78 |
RING_QUEUE_PEEK( br->rq, 2 ) << -br->cur_bit | \ |
| 79 |
RING_QUEUE_PEEK( br->rq, 3 ) << (8 - br->cur_bit); \ |
| 80 |
RING_QUEUE_DEQUEUE_BYTES( br->rq, 2 ); \ |
| 81 |
br->cur_bit += 16; \ |
| 82 |
} \ |
| 83 |
} while (0) |
| 84 |
|
| 85 |
/* ============================================================================ |
| 86 |
* Real JPEG Lite stuff |
| 87 |
* ============================================================================ |
| 88 |
* |
| 89 |
* Precomputed tables |
| 90 |
* Theses are computed at init time to make real-time operations faster. |
| 91 |
* It takes some space ( about 9k ). But believe me it worth it ! |
| 92 |
*/ |
| 93 |
|
| 94 |
/* Variable Lenght Coding related tables, used for AC coefficient decoding |
| 95 |
* TODO Check that 7 bits is enough ! */ |
| 96 |
static signed char vlcTbl_len[1<<10]; /* Meaningful bit count */ |
| 97 |
static signed char vlcTbl_run[1<<10]; /* Run */ |
| 98 |
static signed char vlcTbl_amp[1<<10]; /* Amplitude (without the sign) */ |
| 99 |
|
| 100 |
/* YUV->RGB conversion table */ |
| 101 |
static int yuvTbl_y[256]; |
| 102 |
static int yuvTbl_u1[256]; |
| 103 |
static int yuvTbl_u2[256]; |
| 104 |
static int yuvTbl_v1[256]; |
| 105 |
static int yuvTbl_v2[256]; |
| 106 |
|
| 107 |
/* Clamping table */ |
| 108 |
#define SAFE_CLAMP |
| 109 |
#ifdef SAFE_CLAMP |
| 110 |
static inline unsigned char clamp(int x) { |
| 111 |
if (x > 255) |
| 112 |
return 255; |
| 113 |
if (x < 0) |
| 114 |
return 0; |
| 115 |
return x; |
| 116 |
} |
| 117 |
#define clamp_adjust(x) clamp(x+128) |
| 118 |
#else |
| 119 |
#define clamp(x) clampTbl[(x)+512] |
| 120 |
#define clamp_adjust(x) clampTbl[(x)+640] |
| 121 |
static char clampTbl[1280]; |
| 122 |
#endif |
| 123 |
|
| 124 |
/* Code to initialize those tables */ |
| 125 |
static void vlcTbl_init(void) |
| 126 |
{ |
| 127 |
/* Bases tables used to compute the bigger one |
| 128 |
* To understands theses, look at the VLC doc in the |
| 129 |
* US patent document. */ |
| 130 |
|
| 131 |
static const int vlc_num = 28; |
| 132 |
static const int vlc_len[] = |
| 133 |
{ 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, |
| 134 |
8 ,8 ,8 ,9, 9, 9, 10, 10, 10, 10, 10, 10 }; |
| 135 |
static const int vlc_run[] = |
| 136 |
{ 0, 0, 0, 1, 0, 2, 3, 1, 0, 4, 0, 5, 1, 0, -1, -2, |
| 137 |
2, 6, 0, 3, 1, 0, 1, 0, 7, 2, 0, 8 }; |
| 138 |
static const int vlc_amp[] = |
| 139 |
{ 0, 1, 2, 1, 3, 1, 1, 2, 4, 1 ,5 ,1 ,3 ,6, -1, -2, |
| 140 |
2, 1, 7, 2, 4, 8, 5, 9, 1 ,3, 10, 1 }; |
| 141 |
static const int vlc_cod[] = |
| 142 |
{ 0x000, 0x002, 0x003, 0x006, 0x00E, 0x008, 0x00B, 0x012, |
| 143 |
0x014, 0x03D, 0x03E, 0x078, 0x079, 0x07E, 0x026, 0x027, |
| 144 |
0x054, 0x057, 0x0FF, 0x0AA, 0x0AC, 0x1FC, 0x156, 0x157, |
| 145 |
0x15A, 0x15B, 0x3FA, 0x3FB }; |
| 146 |
|
| 147 |
/* Vars */ |
| 148 |
int i,j; |
| 149 |
|
| 150 |
/* Main filling loop */ |
| 151 |
for ( i=0 ; i<(1<<10) ; i++ ) { |
| 152 |
|
| 153 |
/* Find the matching one */ |
| 154 |
for ( j=0 ; j<vlc_num ; j++ ) { |
| 155 |
if ( (i >> (10-vlc_len[j])) == vlc_cod[j] ) { |
| 156 |
if ( vlc_run[j] >= 0 ) |
| 157 |
if ( vlc_amp[j] != 0 ) |
| 158 |
vlcTbl_len[i] = vlc_len[j] + 1; |
| 159 |
else |
| 160 |
vlcTbl_len[i] = vlc_len[j]; /* EOB */ |
| 161 |
else |
| 162 |
vlcTbl_len[i] = 16; |
| 163 |
vlcTbl_run[i] = vlc_run[j]; |
| 164 |
vlcTbl_amp[i] = vlc_amp[j]; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static void yuvTbl_init(void) |
| 172 |
{ |
| 173 |
/* These tables are just pre-multiplied and pre-offseted |
| 174 |
* YUV by the book |
| 175 |
* R = 1.164 * (Y-16) + 1.596 * (U-128) |
| 176 |
* G = 1.164 * (Y-16) - 0.813 * (U-128) - 0.391 * (V-128) |
| 177 |
* B = 1.164 * (Y-16) + 2.018 * (V-128) */ |
| 178 |
|
| 179 |
int i; |
| 180 |
|
| 181 |
/* We use fixed point << 16 */ |
| 182 |
for ( i=0 ; i < 256 ; i++ ) { |
| 183 |
yuvTbl_y[i] = 76284 * (i- 16); |
| 184 |
yuvTbl_u1[i] = 104595 * (i-128); |
| 185 |
yuvTbl_u2[i] = 53281 * (i-128); |
| 186 |
yuvTbl_v1[i] = 25625 * (i-128); |
| 187 |
yuvTbl_v2[i] = 132252 * (i-128); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
#ifndef SAFE_CLAMP |
| 192 |
static void clampTbl_init(void) |
| 193 |
{ |
| 194 |
/* Instead of doing if(...) to test for overrange, we use |
| 195 |
* a clamping table */ |
| 196 |
|
| 197 |
int i; |
| 198 |
|
| 199 |
for (i=0 ; i < 512 ; i++) |
| 200 |
clampTbl[i] = 0; |
| 201 |
for (i=512 ; i < 768 ; i++ ) |
| 202 |
clampTbl[i] = i - 512; |
| 203 |
for (i=768 ; i < 1280 ; i++ ) |
| 204 |
clampTbl[i] = 255; |
| 205 |
|
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
/* |
| 210 |
* Internal helpers |
| 211 |
*/ |
| 212 |
|
| 213 |
static inline int readAC( struct rqBitReader *br, int *run, int *amp ) |
| 214 |
{ |
| 215 |
/* Vars */ |
| 216 |
unsigned int cod; |
| 217 |
|
| 218 |
/* Get 16 bits */ |
| 219 |
cod = rqBR_peekBits(br,16); |
| 220 |
|
| 221 |
/* Lookup in the table */ |
| 222 |
*run = vlcTbl_run[cod>>6]; |
| 223 |
*amp = vlcTbl_amp[cod>>6]; |
| 224 |
rqBR_flushBits(br,vlcTbl_len[cod>>6]); |
| 225 |
|
| 226 |
if (*amp > 0) { |
| 227 |
|
| 228 |
/* Normal stuff, just correct the sign */ |
| 229 |
if (cod & (0x10000 >> vlcTbl_len[cod>>6])) |
| 230 |
*amp = - *amp; |
| 231 |
} else { |
| 232 |
|
| 233 |
/* Handle special cases */ |
| 234 |
if (!*amp) |
| 235 |
return 0; |
| 236 |
if (*amp == -1) { |
| 237 |
|
| 238 |
/* 0100110srrraaaaa */ |
| 239 |
*run = ( cod >> 5 ) & 0x07; |
| 240 |
*amp = ( cod & 0x100) ? |
| 241 |
-(cod&0x1F) : (cod&0x1F); |
| 242 |
} else { |
| 243 |
|
| 244 |
/* 0100111srrrraaaa */ |
| 245 |
*run = ( cod >> 4 ) & 0x0F; |
| 246 |
*amp = ( cod & 0x100) ? |
| 247 |
-(cod&0x0F) : (cod&0x0F); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return 1; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
#define iDCT_column(b0,b1,b2,b3) do { \ |
| 256 |
int t0,t1,t2,t3; \ |
| 257 |
\ |
| 258 |
t0 = ( b1 + b3 ) << 5; \ |
| 259 |
t2 = t0 - (b3 << 4); \ |
| 260 |
t3 = (b1 * 47) - t0; \ |
| 261 |
t0 = b0 + b2; \ |
| 262 |
t1 = b0 - b2; \ |
| 263 |
\ |
| 264 |
b0 = ( t0 + t2 ); \ |
| 265 |
b1 = ( t1 + t3 ); \ |
| 266 |
b3 = ( t0 - t2 ); \ |
| 267 |
b2 = ( t1 - t3 ); \ |
| 268 |
} while (0) |
| 269 |
|
| 270 |
#define iDCT_line(b0,b1,b2,b3) do { \ |
| 271 |
int t0,t1,t2,t3,bm0,bm2; \ |
| 272 |
\ |
| 273 |
bm0 = b0 << 7; \ |
| 274 |
bm2 = b2 << 7; \ |
| 275 |
\ |
| 276 |
t0 = bm0 + bm2; \ |
| 277 |
t1 = bm0 - bm2; \ |
| 278 |
t2 = b1 * 183 + b3 * 86; \ |
| 279 |
t3 = b1 * 86 - b3 * 183; \ |
| 280 |
\ |
| 281 |
b0 = ( t0 + t2 ) >> 22; \ |
| 282 |
b1 = ( t1 + t3 ) >> 22; \ |
| 283 |
b3 = ( t0 - t2 ) >> 22; \ |
| 284 |
b2 = ( t1 - t3 ) >> 22; \ |
| 285 |
} while (0) |
| 286 |
|
| 287 |
/* Decode a block |
| 288 |
* Basic ops : get the DC - get the ACs - deZigZag - deWeighting - |
| 289 |
* deQuantization - iDCT |
| 290 |
* Here they are a little mixed-up to speed all this up. |
| 291 |
*/ |
| 292 |
static inline int decodeBlock( struct rqBitReader *br, int *block, int *dc ) |
| 293 |
{ |
| 294 |
/* Tables used for block decoding */ |
| 295 |
|
| 296 |
/* deZigZag table |
| 297 |
* |
| 298 |
* ZigZag: each of the coefficient of the DCT transformed 4x4 |
| 299 |
* matrix is taken in a certain order to make a linear |
| 300 |
* array with the high frequency AC at the end |
| 301 |
* |
| 302 |
* / 0 1 5 6 \ . |
| 303 |
* | 2 4 7 12 | This is the order taken. We must deZigZag |
| 304 |
* | 3 8 11 13 | to reconstitute the original matrix |
| 305 |
* \ 9 10 14 15 / |
| 306 |
*/ |
| 307 |
static const int iZigZagTbl[16] = |
| 308 |
{ 0, 1, 4, 8, 5, 2, 3, 6, 9,12, 13, 10, 7, 11, 14, 15 }; |
| 309 |
|
| 310 |
/* deQuantization, deWeighting & iDCT premultiply */ |
| 311 |
|
| 312 |
/* |
| 313 |
* Weighting : Each DCT coefficient is weighted by a certain factor. We |
| 314 |
* must compensate for this to rebuilt the original DCT matrix. |
| 315 |
* |
| 316 |
* Quantization: According to the read Q factor, DCT coefficient are |
| 317 |
* quantized. We need to compensate for this. |
| 318 |
* |
| 319 |
* iDCT premultiply: Since for the first iDCT pass ( column ), we'll need |
| 320 |
* to do some multiplication, the ones that we can |
| 321 |
* integrate here, we do. |
| 322 |
* |
| 323 |
* Rem: - The factors are here presented in the ZigZaged order, |
| 324 |
* because we will need those BEFORE the deZigZag |
| 325 |
* - For more informations, consult jpgl_tbl.c, it's the little |
| 326 |
* prog that computes this table |
| 327 |
*/ |
| 328 |
static const int iQWTbl[4][16] = { |
| 329 |
{ 32768, 17808, 794, 18618, 850, 18618, 43115, 1828, |
| 330 |
40960, 1924, 2089, 45511, 2089, 49648, 2216, 2521 }, |
| 331 |
{ 32768, 35617, 1589, 37236, 1700, 37236, 86231, 3656, |
| 332 |
81920, 3849, 4179, 91022, 4179, 99296, 4432, 5043 }, |
| 333 |
{ 32768, 71234, 3179, 74472, 3401, 74472, 172463, 7313, |
| 334 |
163840, 7698, 8358, 182044, 8358, 198593, 8865, 10087 }, |
| 335 |
{ 32768, 142469, 6359, 148945, 6803, 148945, 344926, 14627, |
| 336 |
327680, 15397, 16716, 364088, 16716, 397187, 17730, 20175 } |
| 337 |
}; |
| 338 |
|
| 339 |
/* Vars */ |
| 340 |
int hdr; |
| 341 |
int *eff_iQWTbl; |
| 342 |
int cc, run, amp; |
| 343 |
|
| 344 |
/* Read & Decode the block header ( Q, T, DC ) */ |
| 345 |
hdr = rqBR_peekBits(br,11); |
| 346 |
|
| 347 |
if (hdr & 0x100) { |
| 348 |
/* Differential mode */ |
| 349 |
if (hdr & 0x80) |
| 350 |
*dc += ( hdr >> 3 ) | ~0xF; |
| 351 |
else |
| 352 |
*dc += ( hdr >> 3 ) & 0xF; |
| 353 |
|
| 354 |
/* Flush the header bits */ |
| 355 |
rqBR_flushBits(br,8); |
| 356 |
} else { |
| 357 |
/* Direct mode */ |
| 358 |
if ( hdr & 0x80 ) |
| 359 |
*dc = hdr | ~0x7F; |
| 360 |
else |
| 361 |
*dc = hdr & 0x7F; |
| 362 |
|
| 363 |
/* Flush the header bits */ |
| 364 |
rqBR_flushBits(br,11); |
| 365 |
} |
| 366 |
|
| 367 |
/* Clear the block & store DC ( with pre-multiply ) */ |
| 368 |
block[0] = *dc << 15; |
| 369 |
block[1] = 0x00; |
| 370 |
block[2] = 0x00; |
| 371 |
block[3] = 0x00; |
| 372 |
block[4] = 0x00; |
| 373 |
block[5] = 0x00; |
| 374 |
block[6] = 0x00; |
| 375 |
block[7] = 0x00; |
| 376 |
block[8] = 0x00; |
| 377 |
block[9] = 0x00; |
| 378 |
block[10] = 0x00; |
| 379 |
block[11] = 0x00; |
| 380 |
block[12] = 0x00; |
| 381 |
block[13] = 0x00; |
| 382 |
block[14] = 0x00; |
| 383 |
block[15] = 0x00; |
| 384 |
|
| 385 |
/* Read the AC coefficients |
| 386 |
* at the same time, deZigZag, deQuantization, deWeighting & iDCT premultiply |
| 387 |
*/ |
| 388 |
eff_iQWTbl = (int*) iQWTbl[hdr>>9]; |
| 389 |
cc = 0; |
| 390 |
|
| 391 |
while ( readAC(br,&run,&) ) { |
| 392 |
cc += run + 1; |
| 393 |
if ( cc > 15 ) |
| 394 |
return -1; |
| 395 |
block[iZigZagTbl[cc]] = amp * eff_iQWTbl[cc]; |
| 396 |
} |
| 397 |
|
| 398 |
/* Do the column iDCT ( what's left to do ) */ |
| 399 |
iDCT_column(block[0], block[4], block[8], block[12]); |
| 400 |
iDCT_column(block[1], block[5], block[9], block[13]); |
| 401 |
iDCT_column(block[2], block[6], block[10], block[14]); |
| 402 |
iDCT_column(block[3], block[7], block[11], block[15]); |
| 403 |
|
| 404 |
/* Do the line iDCT ( complete one here ) */ |
| 405 |
iDCT_line(block[0], block[1], block[2], block[3]); |
| 406 |
iDCT_line(block[4], block[5], block[6], block[7]); |
| 407 |
iDCT_line(block[8], block[9], block[10], block[11]); |
| 408 |
iDCT_line(block[12], block[13], block[14], block[15]); |
| 409 |
|
| 410 |
return !(hdr & 0x700); |
| 411 |
} |
| 412 |
|
| 413 |
int v4lconvert_decode_jpgl(const unsigned char *inp, int src_size, |
| 414 |
unsigned int dest_pix_fmt, unsigned char *fb, |
| 415 |
int img_width, int img_height) |
| 416 |
{ |
| 417 |
/* Vars */ |
| 418 |
struct RingQueue rq; |
| 419 |
struct rqBitReader br; |
| 420 |
|
| 421 |
int row, col; /* Row & Column in the image */ |
| 422 |
|
| 423 |
int x,y; |
| 424 |
int block_idx; |
| 425 |
|
| 426 |
unsigned char *Yline_baseptr, *Uline_baseptr, *Vline_baseptr; |
| 427 |
unsigned char *Yline, *Uline, *Vline; |
| 428 |
int Yline_baseofs, UVline_baseofs; |
| 429 |
|
| 430 |
int dc_y, dc_u, dc_v; /* DC Coefficients */ |
| 431 |
int block_y[16*4]; /* Y blocks */ |
| 432 |
int block_u[16]; /* U block */ |
| 433 |
int block_v[16]; /* V block */ |
| 434 |
|
| 435 |
unsigned char *mainbuffer; |
| 436 |
|
| 437 |
int yc,uc,vc; |
| 438 |
|
| 439 |
/* init the decoder */ |
| 440 |
if (yuvTbl_y[0] == 0) { |
| 441 |
vlcTbl_init(); |
| 442 |
yuvTbl_init(); |
| 443 |
#ifndef SAFE_CLAMP |
| 444 |
clampTbl_init(); |
| 445 |
#endif |
| 446 |
} |
| 447 |
|
| 448 |
img_height /= 4; |
| 449 |
|
| 450 |
/* Prepare a bit-by-bit reader */ |
| 451 |
rq.queue = inp; |
| 452 |
rq.length = src_size; |
| 453 |
rq.ri = 0; |
| 454 |
rqBR_init(&br, &rq); |
| 455 |
|
| 456 |
/* Allocate a big buffer & setup pointers */ |
| 457 |
switch (dest_pix_fmt) { |
| 458 |
default: |
| 459 |
/* case V4L2_PIX_FMT_RGB24: */ |
| 460 |
/* case V4L2_PIX_FMT_BGR24: */ |
| 461 |
mainbuffer = malloc(4 * (img_width + (img_width >> 1) + 2)); |
| 462 |
|
| 463 |
Yline_baseptr = mainbuffer; |
| 464 |
Uline_baseptr = mainbuffer + (4 * img_width); |
| 465 |
Vline_baseptr = Uline_baseptr + (img_width + 4); |
| 466 |
break; |
| 467 |
case V4L2_PIX_FMT_YUV420: |
| 468 |
mainbuffer = NULL; |
| 469 |
Yline_baseptr = fb; |
| 470 |
Uline_baseptr = fb + img_width * img_height * 16; |
| 471 |
Vline_baseptr = Uline_baseptr + img_width * img_height * 4; |
| 472 |
break; |
| 473 |
case V4L2_PIX_FMT_YVU420: |
| 474 |
mainbuffer = NULL; |
| 475 |
Yline_baseptr = fb; |
| 476 |
Vline_baseptr = fb + img_width * img_height * 16; |
| 477 |
Uline_baseptr = Vline_baseptr + img_width * img_height * 4; |
| 478 |
break; |
| 479 |
} |
| 480 |
|
| 481 |
Yline_baseofs = img_width - 4; |
| 482 |
UVline_baseofs = (img_width >> 2) - 3; |
| 483 |
|
| 484 |
/* Process 4 lines at a time ( one block height ) */ |
| 485 |
for ( row=0 ; row<img_height ; row++ ) { |
| 486 |
/* Line start reset DC */ |
| 487 |
dc_y = dc_u = dc_v = 0; |
| 488 |
|
| 489 |
/* Process 16 columns at a time ( 4 block width ) */ |
| 490 |
for ( col=0 ; col<img_width ; col+=16 ) { |
| 491 |
/* Decode blocks |
| 492 |
* Block order : Y Y Y Y V U ( Why V before U ? |
| 493 |
* that just depends what you call U&V ... I took the |
| 494 |
* 'by-the-book' names and that makes V and then U, |
| 495 |
* ... just ask the DivIO folks ;) ) |
| 496 |
*/ |
| 497 |
if ( decodeBlock(&br, block_y, &dc_y) && (!col) ) |
| 498 |
/* return; * Bad block, so bad frame ... */ |
| 499 |
; |
| 500 |
|
| 501 |
decodeBlock(&br, block_y + 16, &dc_y); |
| 502 |
decodeBlock(&br, block_y + 32, &dc_y); |
| 503 |
decodeBlock(&br, block_y + 48, &dc_y); |
| 504 |
decodeBlock(&br, block_v, &dc_v); |
| 505 |
decodeBlock(&br, block_u, &dc_u); |
| 506 |
|
| 507 |
/* Copy data to temporary buffers ( to make a complete line ) */ |
| 508 |
block_idx = 0; |
| 509 |
Yline = Yline_baseptr + col; |
| 510 |
Uline = Uline_baseptr + (col >> 2); |
| 511 |
Vline = Vline_baseptr + (col >> 2); |
| 512 |
|
| 513 |
for ( y=0 ; y<4 ; y++) { |
| 514 |
/* Scan line */ |
| 515 |
for ( x=0 ; x<4 ; x++ ) { |
| 516 |
/* Y block */ |
| 517 |
Yline[ 0] = clamp_adjust(block_y[block_idx ]); |
| 518 |
Yline[ 4] = clamp_adjust(block_y[block_idx+16]); |
| 519 |
Yline[ 8] = clamp_adjust(block_y[block_idx+32]); |
| 520 |
Yline[12] = clamp_adjust(block_y[block_idx+48]); |
| 521 |
|
| 522 |
/* U block */ |
| 523 |
*Uline = clamp_adjust(block_u[block_idx]); |
| 524 |
|
| 525 |
/* V block */ |
| 526 |
*Vline = clamp_adjust(block_v[block_idx]); |
| 527 |
|
| 528 |
/* Ajust pointers & index */ |
| 529 |
block_idx++; |
| 530 |
Yline++; |
| 531 |
Uline++; |
| 532 |
Vline++; |
| 533 |
} |
| 534 |
|
| 535 |
/* Adjust pointers */ |
| 536 |
Yline += Yline_baseofs; |
| 537 |
Uline += UVline_baseofs; |
| 538 |
Vline += UVline_baseofs; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/* Handle interpolation special case ( at the end of the lines ) */ |
| 543 |
Uline = Uline_baseptr + (UVline_baseofs+2); |
| 544 |
Vline = Vline_baseptr + (UVline_baseofs+2); |
| 545 |
for ( y=0 ; y<4 ; y++ ) { |
| 546 |
/* Copy the last pixel */ |
| 547 |
Uline[1] = Uline[0]; |
| 548 |
Vline[1] = Vline[0]; |
| 549 |
|
| 550 |
/* Adjust ptr */ |
| 551 |
Uline += UVline_baseofs+4; |
| 552 |
Vline += UVline_baseofs+4; |
| 553 |
} |
| 554 |
|
| 555 |
/* We have 4 complete lines, so tempbuffer<YUV> -> framebuffer<RGB> |
| 556 |
* Go line by line */ |
| 557 |
|
| 558 |
switch (dest_pix_fmt) { |
| 559 |
case V4L2_PIX_FMT_RGB24: |
| 560 |
Yline = Yline_baseptr; |
| 561 |
Uline = Uline_baseptr; |
| 562 |
Vline = Vline_baseptr; |
| 563 |
for ( y=0 ; y<4 ; y++ ) { |
| 564 |
/* Process 4 pixel at a time to handle interpolation |
| 565 |
* for U & V values */ |
| 566 |
for ( x=0 ; x<img_width ; x+=4 ) { |
| 567 |
/* First pixel */ |
| 568 |
yc = yuvTbl_y[*(Yline++)]; |
| 569 |
uc = Uline[0]; |
| 570 |
vc = Vline[0]; |
| 571 |
|
| 572 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 573 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 574 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 575 |
|
| 576 |
/* Second pixel */ |
| 577 |
yc = yuvTbl_y[*(Yline++)]; |
| 578 |
uc = ( 3*Uline[0] + Uline[1] ) >> 2; |
| 579 |
vc = ( 3*Vline[0] + Vline[1] ) >> 2; |
| 580 |
|
| 581 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 582 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 583 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 584 |
|
| 585 |
/* Third pixel */ |
| 586 |
yc = yuvTbl_y[*(Yline++)]; |
| 587 |
uc = ( Uline[0] + Uline[1] ) >> 1; |
| 588 |
vc = ( Vline[0] + Vline[1] ) >> 1; |
| 589 |
|
| 590 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 591 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 592 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 593 |
|
| 594 |
/* Fourth pixel */ |
| 595 |
yc = yuvTbl_y[*(Yline++)]; |
| 596 |
uc = ( Uline[0] + 3*Uline[1] ) >> 2; |
| 597 |
vc = ( Vline[0] + 3*Vline[1] ) >> 2; |
| 598 |
|
| 599 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 600 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 601 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 602 |
|
| 603 |
/* Adjust pointers */ |
| 604 |
Uline++; |
| 605 |
Vline++; |
| 606 |
} |
| 607 |
|
| 608 |
/* Adjust pointers */ |
| 609 |
Uline++; |
| 610 |
Vline++; |
| 611 |
} |
| 612 |
break; |
| 613 |
case V4L2_PIX_FMT_BGR24: |
| 614 |
Yline = Yline_baseptr; |
| 615 |
Uline = Uline_baseptr; |
| 616 |
Vline = Vline_baseptr; |
| 617 |
for ( y=0 ; y<4 ; y++ ) { |
| 618 |
/* Process 4 pixel at a time to handle interpolation |
| 619 |
* for U & V values */ |
| 620 |
for ( x=0 ; x<img_width ; x+=4 ) { |
| 621 |
/* First pixel */ |
| 622 |
yc = yuvTbl_y[*(Yline++)]; |
| 623 |
uc = Uline[0]; |
| 624 |
vc = Vline[0]; |
| 625 |
|
| 626 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 627 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 628 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 629 |
|
| 630 |
/* Second pixel */ |
| 631 |
yc = yuvTbl_y[*(Yline++)]; |
| 632 |
uc = ( 3*Uline[0] + Uline[1] ) >> 2; |
| 633 |
vc = ( 3*Vline[0] + Vline[1] ) >> 2; |
| 634 |
|
| 635 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 636 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 637 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 638 |
|
| 639 |
/* Third pixel */ |
| 640 |
yc = yuvTbl_y[*(Yline++)]; |
| 641 |
uc = ( Uline[0] + Uline[1] ) >> 1; |
| 642 |
vc = ( Vline[0] + Vline[1] ) >> 1; |
| 643 |
|
| 644 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 645 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 646 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 647 |
|
| 648 |
/* Fourth pixel */ |
| 649 |
yc = yuvTbl_y[*(Yline++)]; |
| 650 |
uc = ( Uline[0] + 3*Uline[1] ) >> 2; |
| 651 |
vc = ( Vline[0] + 3*Vline[1] ) >> 2; |
| 652 |
|
| 653 |
*(fb++) = clamp(( yc + yuvTbl_v2[vc] ) >> 16); |
| 654 |
*(fb++) = clamp(( yc - yuvTbl_u2[uc] - yuvTbl_v1[vc] ) >> 16); |
| 655 |
*(fb++) = clamp(( yc + yuvTbl_u1[uc] ) >> 16); |
| 656 |
|
| 657 |
/* Adjust pointers */ |
| 658 |
Uline++; |
| 659 |
Vline++; |
| 660 |
} |
| 661 |
|
| 662 |
/* Adjust pointers */ |
| 663 |
Uline++; |
| 664 |
Vline++; |
| 665 |
} |
| 666 |
break; |
| 667 |
case V4L2_PIX_FMT_YUV420: |
| 668 |
case V4L2_PIX_FMT_YVU420: |
| 669 |
Yline_baseptr += img_width * 4; |
| 670 |
Uline_baseptr += img_width; |
| 671 |
Vline_baseptr += img_width; |
| 672 |
break; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
/* Free our buffer */ |
| 677 |
if (mainbuffer != NULL) |
| 678 |
free(mainbuffer); |
| 679 |
|
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
/* |
| 684 |
Return-Path: [email protected] |
| 685 |
Received: from zimbra16-e3.priv.proxad.net (LHLO |
| 686 |
zimbra16-e3.priv.proxad.net) (172.20.243.166) by |
| 687 |
zimbra16-e3.priv.proxad.net with LMTP; Mon, 14 Feb 2011 21:10:38 +0100 |
| 688 |
(CET) |
| 689 |
Received: from mailrelay011.isp.belgacom.be (mx26-g26.priv.proxad.net [172.20.243.96]) |
| 690 |
by zimbra16-e3.priv.proxad.net (Postfix) with ESMTP id 1A661157C5B |
| 691 |
for <[email protected]>; Mon, 14 Feb 2011 21:10:38 +0100 (CET) |
| 692 |
Received: from mailrelay011.isp.belgacom.be ([195.238.6.178]) |
| 693 |
by mx1-g20.free.fr (MXproxy) for [email protected] ; |
| 694 |
Mon, 14 Feb 2011 21:10:36 +0100 (CET) |
| 695 |
X-ProXaD-SC: state=HAM score=0 |
| 696 |
X-Belgacom-Dynamic: yes |
| 697 |
X-IronPort-Anti-Spam-Filtered: true |
| 698 |
X-IronPort-Anti-Spam-Result: ApIBAKsaWU1XQ5W2/2dsb2JhbAAMhBHOEpA5gSeBaYFYdgSLfw |
| 699 |
Received: from 182.149-67-87.adsl-dyn.isp.belgacom.be (HELO [10.0.0.129]) ([87.67.149.182]) |
| 700 |
by relay.skynet.be with ESMTP; 14 Feb 2011 21:10:36 +0100 |
| 701 |
Message-ID: <[email protected]> |
| 702 |
Date: Mon, 14 Feb 2011 21:11:40 +0100 |
| 703 |
From: Sylvain Munaut <[email protected]> |
| 704 |
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101219 Lightning/1.0b3pre Thunderbird/3.1.7 |
| 705 |
MIME-Version: 1.0 |
| 706 |
To: Jean-Francois Moine <[email protected]> |
| 707 |
CC: Kjell Claesson <[email protected]> |
| 708 |
Subject: Re: nw80x as a gspca subdriv |
| 709 |
References: <20110209204208.4b19df88@tele> <[email protected]> <20110214205107.18c29303@tele> |
| 710 |
In-Reply-To: <20110214205107.18c29303@tele> |
| 711 |
Content-Type: text/plain; charset=UTF-8 |
| 712 |
Content-Transfer-Encoding: 7bit |
| 713 |
|
| 714 |
[snip] |
| 715 |
> May I have your permission to relicense your JPEG Lite decompression |
| 716 |
> code under the LGPL (version 2 or later)? |
| 717 |
|
| 718 |
Yes, sure. |
| 719 |
|
| 720 |
""" |
| 721 |
I hereby allow the nw80x driver code, including the jpeg lite decoding |
| 722 |
routines, to be used and distributed under the LGPL v2 or later. |
| 723 |
""" |
| 724 |
[snip] |
| 725 |
Cheers, |
| 726 |
|
| 727 |
Sylvain |
| 728 |
*/ |