comparison qtrle.c @ 7805:d1ebbf1e4d50 libavcodec

add 1bpp decoding function and extend 4bpp function to also cover the 2bpp case.
author stefang
date Sun, 07 Sep 2008 07:36:03 +0000
parents 94f82ed28dc4
children 4525dcd81357
comparison
equal deleted inserted replaced
7804:470878c73621 7805:d1ebbf1e4d50
62 return; \ 62 return; \
63 } \ 63 } \
64 64
65 static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change) 65 static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
66 { 66 {
67 }
68
69 static void qtrle_decode_2bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
70 {
71 }
72
73 static void qtrle_decode_4bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
74 {
75 int rle_code; 67 int rle_code;
68 int pixel_ptr = 0;
69 int row_inc = s->frame.linesize[0];
70 unsigned char pi0, pi1; /* 2 8-pixel values */
71 unsigned char *rgb = s->frame.data[0];
72 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
73 int skip;
74
75 while (lines_to_change) {
76 CHECK_STREAM_PTR(2);
77 skip = s->buf[stream_ptr++];
78 rle_code = (signed char)s->buf[stream_ptr++];
79 if (rle_code == 0)
80 break;
81 if(skip & 0x80) {
82 lines_to_change--;
83 row_ptr += row_inc;
84 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
85 } else
86 pixel_ptr += 2 * skip;
87 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
88
89 if (rle_code < 0) {
90 /* decode the run length code */
91 rle_code = -rle_code;
92 /* get the next 2 bytes from the stream, treat them as groups
93 * of 8 pixels, and output them rle_code times */
94 CHECK_STREAM_PTR(2);
95 pi0 = s->buf[stream_ptr++];
96 pi1 = s->buf[stream_ptr++];
97 CHECK_PIXEL_PTR(rle_code * 2);
98
99 while (rle_code--) {
100 rgb[pixel_ptr++] = pi0;
101 rgb[pixel_ptr++] = pi1;
102 }
103 } else {
104 /* copy the same pixel directly to output 2 times */
105 rle_code *= 2;
106 CHECK_STREAM_PTR(rle_code);
107 CHECK_PIXEL_PTR(rle_code);
108
109 while (rle_code--)
110 rgb[pixel_ptr++] = s->buf[stream_ptr++];
111 }
112 }
113 }
114
115 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
116 int row_ptr, int lines_to_change, int bpp)
117 {
118 int rle_code, i;
76 int pixel_ptr; 119 int pixel_ptr;
77 int row_inc = s->frame.linesize[0]; 120 int row_inc = s->frame.linesize[0];
78 unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indexes */ 121 unsigned char pi[16]; /* 16 palette indices */
79 unsigned char *rgb = s->frame.data[0]; 122 unsigned char *rgb = s->frame.data[0];
80 int pixel_limit = s->frame.linesize[0] * s->avctx->height; 123 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
124 int num_pixels = (bpp == 4) ? 8 : 16;
81 125
82 while (lines_to_change--) { 126 while (lines_to_change--) {
83 CHECK_STREAM_PTR(2); 127 CHECK_STREAM_PTR(2);
84 pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1)); 128 pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
85 129
86 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) { 130 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
87 if (rle_code == 0) { 131 if (rle_code == 0) {
88 /* there's another skip code in the stream */ 132 /* there's another skip code in the stream */
89 CHECK_STREAM_PTR(1); 133 CHECK_STREAM_PTR(1);
90 pixel_ptr += (8 * (s->buf[stream_ptr++] - 1)); 134 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
91 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */ 135 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
92 } else if (rle_code < 0) { 136 } else if (rle_code < 0) {
93 /* decode the run length code */ 137 /* decode the run length code */
94 rle_code = -rle_code; 138 rle_code = -rle_code;
95 /* get the next 4 bytes from the stream, treat them as palette 139 /* get the next 4 bytes from the stream, treat them as palette
96 * indexes, and output them rle_code times */ 140 * indexes, and output them rle_code times */
97 CHECK_STREAM_PTR(4); 141 CHECK_STREAM_PTR(4);
98 pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f; 142 for (i = num_pixels-1; i >= 0; i--) {
99 pi2 = (s->buf[stream_ptr++]) & 0x0f; 143 pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
100 pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f; 144 stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
101 pi4 = (s->buf[stream_ptr++]) & 0x0f; 145 }
102 pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f; 146 CHECK_PIXEL_PTR(rle_code * num_pixels);
103 pi6 = (s->buf[stream_ptr++]) & 0x0f; 147 while (rle_code--) {
104 pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f; 148 for (i = 0; i < num_pixels; i++)
105 pi8 = (s->buf[stream_ptr++]) & 0x0f; 149 rgb[pixel_ptr++] = pi[i];
106
107 CHECK_PIXEL_PTR(rle_code * 8);
108
109 while (rle_code--) {
110 rgb[pixel_ptr++] = pi1;
111 rgb[pixel_ptr++] = pi2;
112 rgb[pixel_ptr++] = pi3;
113 rgb[pixel_ptr++] = pi4;
114 rgb[pixel_ptr++] = pi5;
115 rgb[pixel_ptr++] = pi6;
116 rgb[pixel_ptr++] = pi7;
117 rgb[pixel_ptr++] = pi8;
118 } 150 }
119 } else { 151 } else {
120 /* copy the same pixel directly to output 4 times */ 152 /* copy the same pixel directly to output 4 times */
121 rle_code *= 4; 153 rle_code *= 4;
122 CHECK_STREAM_PTR(rle_code); 154 CHECK_STREAM_PTR(rle_code);
123 CHECK_PIXEL_PTR(rle_code*2); 155 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
124 156 while (rle_code--) {
125 while (rle_code--) { 157 if(bpp == 4) {
126 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f; 158 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
127 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f; 159 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
160 } else {
161 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
162 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
163 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
164 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
165 }
128 } 166 }
129 } 167 }
130 } 168 }
131 row_ptr += row_inc; 169 row_ptr += row_inc;
132 } 170 }
345 QtrleContext *s = avctx->priv_data; 383 QtrleContext *s = avctx->priv_data;
346 384
347 s->avctx = avctx; 385 s->avctx = avctx;
348 switch (avctx->bits_per_sample) { 386 switch (avctx->bits_per_sample) {
349 case 1: 387 case 1:
388 case 33:
389 avctx->pix_fmt = PIX_FMT_MONOWHITE;
390 break;
391
350 case 2: 392 case 2:
351 case 4: 393 case 4:
352 case 8: 394 case 8:
353 case 33:
354 case 34: 395 case 34:
355 case 36: 396 case 36:
356 case 40: 397 case 40:
357 avctx->pix_fmt = PIX_FMT_PAL8; 398 avctx->pix_fmt = PIX_FMT_PAL8;
358 break; 399 break;
385 const uint8_t *buf, int buf_size) 426 const uint8_t *buf, int buf_size)
386 { 427 {
387 QtrleContext *s = avctx->priv_data; 428 QtrleContext *s = avctx->priv_data;
388 int header, start_line; 429 int header, start_line;
389 int stream_ptr, height, row_ptr; 430 int stream_ptr, height, row_ptr;
431 int has_palette = 0;
390 432
391 s->buf = buf; 433 s->buf = buf;
392 s->size = buf_size; 434 s->size = buf_size;
393 435
394 s->frame.reference = 1; 436 s->frame.reference = 1;
430 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height); 472 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
431 break; 473 break;
432 474
433 case 2: 475 case 2:
434 case 34: 476 case 34:
435 qtrle_decode_2bpp(s, stream_ptr, row_ptr, height); 477 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
478 has_palette = 1;
436 break; 479 break;
437 480
438 case 4: 481 case 4:
439 case 36: 482 case 36:
440 qtrle_decode_4bpp(s, stream_ptr, row_ptr, height); 483 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
484 has_palette = 1;
485 break;
486
487 case 8:
488 case 40:
489 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
490 has_palette = 1;
491 break;
492
493 case 16:
494 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
495 break;
496
497 case 24:
498 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
499 break;
500
501 case 32:
502 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
503 break;
504
505 default:
506 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
507 avctx->bits_per_sample);
508 break;
509 }
510
511 if(has_palette) {
441 /* make the palette available on the way out */ 512 /* make the palette available on the way out */
442 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE); 513 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
443 if (s->avctx->palctrl->palette_changed) { 514 if (s->avctx->palctrl->palette_changed) {
444 s->frame.palette_has_changed = 1; 515 s->frame.palette_has_changed = 1;
445 s->avctx->palctrl->palette_changed = 0; 516 s->avctx->palctrl->palette_changed = 0;
446 } 517 }
447 break; 518 }
448 519
449 case 8:
450 case 40:
451 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
452 /* make the palette available on the way out */
453 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
454 if (s->avctx->palctrl->palette_changed) {
455 s->frame.palette_has_changed = 1;
456 s->avctx->palctrl->palette_changed = 0;
457 }
458 break;
459
460 case 16:
461 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
462 break;
463
464 case 24:
465 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
466 break;
467
468 case 32:
469 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
470 break;
471
472 default:
473 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
474 avctx->bits_per_sample);
475 break;
476 }
477 done: 520 done:
478 *data_size = sizeof(AVFrame); 521 *data_size = sizeof(AVFrame);
479 *(AVFrame*)data = s->frame; 522 *(AVFrame*)data = s->frame;
480 523
481 /* always report that the buffer was completely consumed */ 524 /* always report that the buffer was completely consumed */