00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037
00038 #include "avcodec.h"
00039 #include "bytestream.h"
00040
00041 typedef struct QtrleContext {
00042 AVCodecContext *avctx;
00043 AVFrame frame;
00044
00045 GetByteContext g;
00046 uint32_t pal[256];
00047 } QtrleContext;
00048
00049 #define CHECK_PIXEL_PTR(n) \
00050 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
00051 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
00052 pixel_ptr + n, pixel_limit); \
00053 return; \
00054 } \
00055
00056 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
00057 {
00058 int rle_code;
00059 int pixel_ptr = 0;
00060 int row_inc = s->frame.linesize[0];
00061 unsigned char pi0, pi1;
00062 unsigned char *rgb = s->frame.data[0];
00063 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00064 int skip;
00065
00066
00067
00068
00069
00070
00071
00072
00073 lines_to_change++;
00074
00075 while (lines_to_change) {
00076 skip = bytestream2_get_byte(&s->g);
00077 rle_code = (signed char)bytestream2_get_byte(&s->g);
00078 if (rle_code == 0)
00079 break;
00080 if(skip & 0x80) {
00081 lines_to_change--;
00082 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
00083 row_ptr += row_inc;
00084 } else
00085 pixel_ptr += 2 * skip;
00086 CHECK_PIXEL_PTR(0);
00087
00088 if(rle_code == -1)
00089 continue;
00090
00091 if (rle_code < 0) {
00092
00093 rle_code = -rle_code;
00094
00095
00096
00097 pi0 = bytestream2_get_byte(&s->g);
00098 pi1 = bytestream2_get_byte(&s->g);
00099 CHECK_PIXEL_PTR(rle_code * 2);
00100
00101 while (rle_code--) {
00102 rgb[pixel_ptr++] = pi0;
00103 rgb[pixel_ptr++] = pi1;
00104 }
00105 } else {
00106
00107 rle_code *= 2;
00108 CHECK_PIXEL_PTR(rle_code);
00109
00110 while (rle_code--)
00111 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
00112 }
00113 }
00114 }
00115
00116 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
00117 int lines_to_change, int bpp)
00118 {
00119 int rle_code, i;
00120 int pixel_ptr;
00121 int row_inc = s->frame.linesize[0];
00122 unsigned char pi[16];
00123 unsigned char *rgb = s->frame.data[0];
00124 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00125 int num_pixels = (bpp == 4) ? 8 : 16;
00126
00127 while (lines_to_change--) {
00128 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
00129 CHECK_PIXEL_PTR(0);
00130
00131 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
00132 if (rle_code == 0) {
00133
00134 pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
00135 CHECK_PIXEL_PTR(0);
00136 } else if (rle_code < 0) {
00137
00138 rle_code = -rle_code;
00139
00140
00141 for (i = num_pixels-1; i >= 0; i--) {
00142 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
00143 bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
00144 }
00145 CHECK_PIXEL_PTR(rle_code * num_pixels);
00146 while (rle_code--) {
00147 for (i = 0; i < num_pixels; i++)
00148 rgb[pixel_ptr++] = pi[i];
00149 }
00150 } else {
00151
00152 rle_code *= 4;
00153 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
00154 while (rle_code--) {
00155 if(bpp == 4) {
00156 int x = bytestream2_get_byte(&s->g);
00157 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
00158 rgb[pixel_ptr++] = x & 0x0f;
00159 } else {
00160 int x = bytestream2_get_byte(&s->g);
00161 rgb[pixel_ptr++] = (x >> 6) & 0x03;
00162 rgb[pixel_ptr++] = (x >> 4) & 0x03;
00163 rgb[pixel_ptr++] = (x >> 2) & 0x03;
00164 rgb[pixel_ptr++] = x & 0x03;
00165 }
00166 }
00167 }
00168 }
00169 row_ptr += row_inc;
00170 }
00171 }
00172
00173 static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
00174 {
00175 int rle_code;
00176 int pixel_ptr;
00177 int row_inc = s->frame.linesize[0];
00178 unsigned char pi1, pi2, pi3, pi4;
00179 unsigned char *rgb = s->frame.data[0];
00180 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00181
00182 while (lines_to_change--) {
00183 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
00184 CHECK_PIXEL_PTR(0);
00185
00186 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
00187 if (rle_code == 0) {
00188
00189 pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
00190 CHECK_PIXEL_PTR(0);
00191 } else if (rle_code < 0) {
00192
00193 rle_code = -rle_code;
00194
00195
00196 pi1 = bytestream2_get_byte(&s->g);
00197 pi2 = bytestream2_get_byte(&s->g);
00198 pi3 = bytestream2_get_byte(&s->g);
00199 pi4 = bytestream2_get_byte(&s->g);
00200
00201 CHECK_PIXEL_PTR(rle_code * 4);
00202
00203 while (rle_code--) {
00204 rgb[pixel_ptr++] = pi1;
00205 rgb[pixel_ptr++] = pi2;
00206 rgb[pixel_ptr++] = pi3;
00207 rgb[pixel_ptr++] = pi4;
00208 }
00209 } else {
00210
00211 rle_code *= 4;
00212 CHECK_PIXEL_PTR(rle_code);
00213
00214 while (rle_code--) {
00215 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
00216 }
00217 }
00218 }
00219 row_ptr += row_inc;
00220 }
00221 }
00222
00223 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
00224 {
00225 int rle_code;
00226 int pixel_ptr;
00227 int row_inc = s->frame.linesize[0];
00228 unsigned short rgb16;
00229 unsigned char *rgb = s->frame.data[0];
00230 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00231
00232 while (lines_to_change--) {
00233 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
00234 CHECK_PIXEL_PTR(0);
00235
00236 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
00237 if (rle_code == 0) {
00238
00239 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
00240 CHECK_PIXEL_PTR(0);
00241 } else if (rle_code < 0) {
00242
00243 rle_code = -rle_code;
00244 rgb16 = bytestream2_get_be16(&s->g);
00245
00246 CHECK_PIXEL_PTR(rle_code * 2);
00247
00248 while (rle_code--) {
00249 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
00250 pixel_ptr += 2;
00251 }
00252 } else {
00253 CHECK_PIXEL_PTR(rle_code * 2);
00254
00255
00256 while (rle_code--) {
00257 rgb16 = bytestream2_get_be16(&s->g);
00258 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
00259 pixel_ptr += 2;
00260 }
00261 }
00262 }
00263 row_ptr += row_inc;
00264 }
00265 }
00266
00267 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
00268 {
00269 int rle_code;
00270 int pixel_ptr;
00271 int row_inc = s->frame.linesize[0];
00272 unsigned char r, g, b;
00273 unsigned char *rgb = s->frame.data[0];
00274 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00275
00276 while (lines_to_change--) {
00277 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
00278 CHECK_PIXEL_PTR(0);
00279
00280 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
00281 if (rle_code == 0) {
00282
00283 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
00284 CHECK_PIXEL_PTR(0);
00285 } else if (rle_code < 0) {
00286
00287 rle_code = -rle_code;
00288 r = bytestream2_get_byte(&s->g);
00289 g = bytestream2_get_byte(&s->g);
00290 b = bytestream2_get_byte(&s->g);
00291
00292 CHECK_PIXEL_PTR(rle_code * 3);
00293
00294 while (rle_code--) {
00295 rgb[pixel_ptr++] = r;
00296 rgb[pixel_ptr++] = g;
00297 rgb[pixel_ptr++] = b;
00298 }
00299 } else {
00300 CHECK_PIXEL_PTR(rle_code * 3);
00301
00302
00303 while (rle_code--) {
00304 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
00305 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
00306 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
00307 }
00308 }
00309 }
00310 row_ptr += row_inc;
00311 }
00312 }
00313
00314 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
00315 {
00316 int rle_code;
00317 int pixel_ptr;
00318 int row_inc = s->frame.linesize[0];
00319 unsigned int argb;
00320 unsigned char *rgb = s->frame.data[0];
00321 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
00322
00323 while (lines_to_change--) {
00324 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
00325 CHECK_PIXEL_PTR(0);
00326
00327 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
00328 if (rle_code == 0) {
00329
00330 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
00331 CHECK_PIXEL_PTR(0);
00332 } else if (rle_code < 0) {
00333
00334 rle_code = -rle_code;
00335 argb = bytestream2_get_be32(&s->g);
00336
00337 CHECK_PIXEL_PTR(rle_code * 4);
00338
00339 while (rle_code--) {
00340 AV_WN32A(rgb + pixel_ptr, argb);
00341 pixel_ptr += 4;
00342 }
00343 } else {
00344 CHECK_PIXEL_PTR(rle_code * 4);
00345
00346
00347 while (rle_code--) {
00348 argb = bytestream2_get_be32(&s->g);
00349 AV_WN32A(rgb + pixel_ptr, argb);
00350 pixel_ptr += 4;
00351 }
00352 }
00353 }
00354 row_ptr += row_inc;
00355 }
00356 }
00357
00358 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
00359 {
00360 QtrleContext *s = avctx->priv_data;
00361
00362 s->avctx = avctx;
00363 switch (avctx->bits_per_coded_sample) {
00364 case 1:
00365 case 33:
00366 avctx->pix_fmt = PIX_FMT_MONOWHITE;
00367 break;
00368
00369 case 2:
00370 case 4:
00371 case 8:
00372 case 34:
00373 case 36:
00374 case 40:
00375 avctx->pix_fmt = PIX_FMT_PAL8;
00376 break;
00377
00378 case 16:
00379 avctx->pix_fmt = PIX_FMT_RGB555;
00380 break;
00381
00382 case 24:
00383 avctx->pix_fmt = PIX_FMT_RGB24;
00384 break;
00385
00386 case 32:
00387 avctx->pix_fmt = PIX_FMT_RGB32;
00388 break;
00389
00390 default:
00391 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
00392 avctx->bits_per_coded_sample);
00393 return AVERROR_INVALIDDATA;
00394 }
00395
00396 avcodec_get_frame_defaults(&s->frame);
00397 s->frame.data[0] = NULL;
00398
00399 return 0;
00400 }
00401
00402 static int qtrle_decode_frame(AVCodecContext *avctx,
00403 void *data, int *data_size,
00404 AVPacket *avpkt)
00405 {
00406 QtrleContext *s = avctx->priv_data;
00407 int header, start_line;
00408 int height, row_ptr;
00409 int has_palette = 0;
00410
00411 bytestream2_init(&s->g, avpkt->data, avpkt->size);
00412 s->frame.reference = 3;
00413 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00414 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
00415 if (avctx->reget_buffer(avctx, &s->frame)) {
00416 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00417 return -1;
00418 }
00419
00420
00421 if (avpkt->size < 8)
00422 goto done;
00423
00424
00425 bytestream2_seek(&s->g, 4, SEEK_SET);
00426
00427
00428 header = bytestream2_get_be16(&s->g);
00429
00430
00431 if (header & 0x0008) {
00432 if (avpkt->size < 14)
00433 goto done;
00434 start_line = bytestream2_get_be16(&s->g);
00435 bytestream2_skip(&s->g, 2);
00436 height = bytestream2_get_be16(&s->g);
00437 bytestream2_skip(&s->g, 2);
00438 if (height > s->avctx->height - start_line)
00439 goto done;
00440 } else {
00441 start_line = 0;
00442 height = s->avctx->height;
00443 }
00444 row_ptr = s->frame.linesize[0] * start_line;
00445
00446 switch (avctx->bits_per_coded_sample) {
00447 case 1:
00448 case 33:
00449 qtrle_decode_1bpp(s, row_ptr, height);
00450 break;
00451
00452 case 2:
00453 case 34:
00454 qtrle_decode_2n4bpp(s, row_ptr, height, 2);
00455 has_palette = 1;
00456 break;
00457
00458 case 4:
00459 case 36:
00460 qtrle_decode_2n4bpp(s, row_ptr, height, 4);
00461 has_palette = 1;
00462 break;
00463
00464 case 8:
00465 case 40:
00466 qtrle_decode_8bpp(s, row_ptr, height);
00467 has_palette = 1;
00468 break;
00469
00470 case 16:
00471 qtrle_decode_16bpp(s, row_ptr, height);
00472 break;
00473
00474 case 24:
00475 qtrle_decode_24bpp(s, row_ptr, height);
00476 break;
00477
00478 case 32:
00479 qtrle_decode_32bpp(s, row_ptr, height);
00480 break;
00481
00482 default:
00483 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
00484 avctx->bits_per_coded_sample);
00485 break;
00486 }
00487
00488 if(has_palette) {
00489 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00490
00491 if (pal) {
00492 s->frame.palette_has_changed = 1;
00493 memcpy(s->pal, pal, AVPALETTE_SIZE);
00494 }
00495
00496
00497 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00498 }
00499
00500 done:
00501 *data_size = sizeof(AVFrame);
00502 *(AVFrame*)data = s->frame;
00503
00504
00505 return avpkt->size;
00506 }
00507
00508 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
00509 {
00510 QtrleContext *s = avctx->priv_data;
00511
00512 if (s->frame.data[0])
00513 avctx->release_buffer(avctx, &s->frame);
00514
00515 return 0;
00516 }
00517
00518 AVCodec ff_qtrle_decoder = {
00519 .name = "qtrle",
00520 .type = AVMEDIA_TYPE_VIDEO,
00521 .id = CODEC_ID_QTRLE,
00522 .priv_data_size = sizeof(QtrleContext),
00523 .init = qtrle_decode_init,
00524 .close = qtrle_decode_end,
00525 .decode = qtrle_decode_frame,
00526 .capabilities = CODEC_CAP_DR1,
00527 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
00528 };