2004-02-05 07:22:44 +02:00
|
|
|
/*
|
|
|
|
* Quicktime Animation (RLE) Video Decoder
|
2014-08-23 22:39:22 +03:00
|
|
|
* Copyright (c) 2004 The FFmpeg Project
|
2004-02-05 07:22:44 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-02-05 07:22:44 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-02-05 07:22:44 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-02-05 07:22:44 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-02-05 07:22:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2004-02-05 07:22:44 +02:00
|
|
|
* QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
|
|
|
|
* For more information about the QT RLE format, visit:
|
|
|
|
* http://www.pcisys.net/~melanson/codecs/
|
|
|
|
*
|
|
|
|
* The QT RLE decoder has seven modes of operation:
|
|
|
|
* 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
|
2004-03-14 06:04:08 +02:00
|
|
|
* the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
|
2007-02-07 03:48:09 +02:00
|
|
|
* data. 24-bit data is RGB24 and 32-bit data is RGB32.
|
2004-02-05 07:22:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2012-03-15 22:13:23 +03:00
|
|
|
#include "bytestream.h"
|
2012-11-21 23:34:46 +03:00
|
|
|
#include "internal.h"
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
typedef struct QtrleContext {
|
|
|
|
AVCodecContext *avctx;
|
2013-08-21 22:27:31 +03:00
|
|
|
AVFrame *frame;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
GetByteContext g;
|
2011-04-09 16:49:51 +03:00
|
|
|
uint32_t pal[256];
|
2004-02-05 07:22:44 +02:00
|
|
|
} QtrleContext;
|
|
|
|
|
2013-02-14 20:00:11 +03:00
|
|
|
#define CHECK_PIXEL_PTR(n) \
|
|
|
|
if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
|
2013-02-14 20:00:39 +03:00
|
|
|
av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
|
2013-02-14 20:00:11 +03:00
|
|
|
pixel_ptr + n, pixel_limit); \
|
|
|
|
return; \
|
|
|
|
} \
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2008-09-07 10:36:03 +03:00
|
|
|
int rle_code;
|
2013-01-30 08:42:17 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2013-08-21 22:03:40 +03:00
|
|
|
uint8_t pi0, pi1; /* 2 8-pixel values */
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2008-09-07 10:36:03 +03:00
|
|
|
int skip;
|
2011-12-07 15:27:20 +03:00
|
|
|
/* skip & 0x80 appears to mean 'start a new line', which can be interpreted
|
|
|
|
* as 'go to next line' during the decoding of a frame but is 'go to first
|
|
|
|
* line' at the beginning. Since we always interpret it as 'go to next line'
|
|
|
|
* in the decoding loop (which makes code simpler/faster), the first line
|
|
|
|
* would not be counted, so we count one more.
|
2013-06-29 20:27:49 +03:00
|
|
|
* See: https://trac.ffmpeg.org/ticket/226
|
2011-12-07 15:27:20 +03:00
|
|
|
* In the following decoding loop, row_ptr will be the position of the
|
2013-02-06 18:46:23 +03:00
|
|
|
* current row. */
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-01-30 08:42:17 +03:00
|
|
|
row_ptr -= row_inc;
|
|
|
|
pixel_ptr = row_ptr;
|
|
|
|
lines_to_change++;
|
2008-09-07 10:36:03 +03:00
|
|
|
while (lines_to_change) {
|
2012-03-15 22:13:23 +03:00
|
|
|
skip = bytestream2_get_byte(&s->g);
|
2013-08-21 22:03:40 +03:00
|
|
|
rle_code = (int8_t)bytestream2_get_byte(&s->g);
|
2008-09-07 10:36:03 +03:00
|
|
|
if (rle_code == 0)
|
|
|
|
break;
|
|
|
|
if(skip & 0x80) {
|
|
|
|
lines_to_change--;
|
2011-12-07 15:27:20 +03:00
|
|
|
row_ptr += row_inc;
|
2008-09-07 10:36:03 +03:00
|
|
|
pixel_ptr = row_ptr + 2 * (skip & 0x7f);
|
|
|
|
} else
|
|
|
|
pixel_ptr += 2 * skip;
|
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
|
|
|
|
2011-12-07 22:29:38 +03:00
|
|
|
if(rle_code == -1)
|
|
|
|
continue;
|
|
|
|
|
2008-09-07 10:36:03 +03:00
|
|
|
if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
|
|
|
/* get the next 2 bytes from the stream, treat them as groups
|
|
|
|
* of 8 pixels, and output them rle_code times */
|
2012-03-15 22:13:23 +03:00
|
|
|
|
|
|
|
pi0 = bytestream2_get_byte(&s->g);
|
|
|
|
pi1 = bytestream2_get_byte(&s->g);
|
2008-09-07 10:36:03 +03:00
|
|
|
CHECK_PIXEL_PTR(rle_code * 2);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
|
|
|
rgb[pixel_ptr++] = pi0;
|
|
|
|
rgb[pixel_ptr++] = pi1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* copy the same pixel directly to output 2 times */
|
|
|
|
rle_code *= 2;
|
|
|
|
CHECK_PIXEL_PTR(rle_code);
|
|
|
|
|
2013-08-21 21:57:30 +03:00
|
|
|
bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
|
|
|
|
pixel_ptr += rle_code;
|
2008-09-07 10:36:03 +03:00
|
|
|
}
|
|
|
|
}
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
|
|
|
|
int lines_to_change, int bpp)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2008-09-07 10:36:03 +03:00
|
|
|
int rle_code, i;
|
2008-08-31 11:09:54 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2013-08-21 22:03:40 +03:00
|
|
|
uint8_t pi[16]; /* 16 palette indices */
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2008-09-07 10:36:03 +03:00
|
|
|
int num_pixels = (bpp == 4) ? 8 : 16;
|
2004-05-31 23:18:08 +03:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
|
2013-02-14 19:58:12 +03:00
|
|
|
CHECK_PIXEL_PTR(0);
|
2004-05-31 23:18:08 +03:00
|
|
|
|
2013-08-21 22:03:40 +03:00
|
|
|
while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
|
2004-05-31 23:18:08 +03:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
|
2005-08-13 20:46:09 +03:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-05-31 23:18:08 +03:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
|
|
|
/* get the next 4 bytes from the stream, treat them as palette
|
2008-05-27 02:14:25 +03:00
|
|
|
* indexes, and output them rle_code times */
|
2008-09-07 10:36:03 +03:00
|
|
|
for (i = num_pixels-1; i >= 0; i--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
|
|
|
|
bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
|
2008-09-07 10:36:03 +03:00
|
|
|
}
|
|
|
|
CHECK_PIXEL_PTR(rle_code * num_pixels);
|
2004-05-31 23:18:08 +03:00
|
|
|
while (rle_code--) {
|
2013-08-21 22:13:03 +03:00
|
|
|
memcpy(&rgb[pixel_ptr], &pi, num_pixels);
|
|
|
|
pixel_ptr += num_pixels;
|
2004-05-31 23:18:08 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* copy the same pixel directly to output 4 times */
|
|
|
|
rle_code *= 4;
|
2008-09-07 10:36:03 +03:00
|
|
|
CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
|
2004-05-31 23:18:08 +03:00
|
|
|
while (rle_code--) {
|
2008-09-07 10:36:03 +03:00
|
|
|
if(bpp == 4) {
|
2012-03-15 22:13:23 +03:00
|
|
|
int x = bytestream2_get_byte(&s->g);
|
|
|
|
rgb[pixel_ptr++] = (x >> 4) & 0x0f;
|
|
|
|
rgb[pixel_ptr++] = x & 0x0f;
|
2008-09-07 10:36:03 +03:00
|
|
|
} else {
|
2012-03-15 22:13:23 +03:00
|
|
|
int x = bytestream2_get_byte(&s->g);
|
|
|
|
rgb[pixel_ptr++] = (x >> 6) & 0x03;
|
|
|
|
rgb[pixel_ptr++] = (x >> 4) & 0x03;
|
|
|
|
rgb[pixel_ptr++] = (x >> 2) & 0x03;
|
|
|
|
rgb[pixel_ptr++] = x & 0x03;
|
2008-09-07 10:36:03 +03:00
|
|
|
}
|
2004-05-31 23:18:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2004-02-14 20:12:36 +02:00
|
|
|
int rle_code;
|
2008-08-31 11:09:54 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2013-08-21 22:03:40 +03:00
|
|
|
uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
|
2013-02-14 19:58:12 +03:00
|
|
|
CHECK_PIXEL_PTR(0);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-08-21 22:03:40 +03:00
|
|
|
while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 07:22:44 +02:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
|
2005-08-13 20:46:09 +03:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 07:22:44 +02:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
|
|
|
/* get the next 4 bytes from the stream, treat them as palette
|
2008-05-27 02:14:25 +03:00
|
|
|
* indexes, and output them rle_code times */
|
2012-03-15 22:13:23 +03:00
|
|
|
pi1 = bytestream2_get_byte(&s->g);
|
|
|
|
pi2 = bytestream2_get_byte(&s->g);
|
|
|
|
pi3 = bytestream2_get_byte(&s->g);
|
|
|
|
pi4 = bytestream2_get_byte(&s->g);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 4);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
|
|
|
rgb[pixel_ptr++] = pi1;
|
|
|
|
rgb[pixel_ptr++] = pi2;
|
|
|
|
rgb[pixel_ptr++] = pi3;
|
|
|
|
rgb[pixel_ptr++] = pi4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* copy the same pixel directly to output 4 times */
|
|
|
|
rle_code *= 4;
|
|
|
|
CHECK_PIXEL_PTR(rle_code);
|
|
|
|
|
2013-08-21 21:57:30 +03:00
|
|
|
bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
|
|
|
|
pixel_ptr += rle_code;
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2005-10-28 19:01:12 +03:00
|
|
|
int rle_code;
|
2008-08-31 11:09:54 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2013-08-21 22:03:40 +03:00
|
|
|
uint16_t rgb16;
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
|
2013-02-14 19:58:12 +03:00
|
|
|
CHECK_PIXEL_PTR(0);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-08-21 22:03:40 +03:00
|
|
|
while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 07:22:44 +02:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
|
2005-08-13 20:46:09 +03:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 07:22:44 +02:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 22:13:23 +03:00
|
|
|
rgb16 = bytestream2_get_be16(&s->g);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 2);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
2013-08-21 22:03:40 +03:00
|
|
|
*(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
|
2004-02-05 07:22:44 +02:00
|
|
|
pixel_ptr += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 2);
|
|
|
|
|
|
|
|
/* copy pixels directly to output */
|
|
|
|
while (rle_code--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
rgb16 = bytestream2_get_be16(&s->g);
|
2013-08-21 22:03:40 +03:00
|
|
|
*(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
|
2004-02-05 07:22:44 +02:00
|
|
|
pixel_ptr += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2005-10-28 19:01:12 +03:00
|
|
|
int rle_code;
|
2008-08-31 11:09:54 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2013-08-21 22:03:40 +03:00
|
|
|
uint8_t r, g, b;
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
|
2013-02-14 19:58:12 +03:00
|
|
|
CHECK_PIXEL_PTR(0);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-08-21 22:03:40 +03:00
|
|
|
while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 07:22:44 +02:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
|
2005-08-13 20:46:09 +03:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 07:22:44 +02:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 22:13:23 +03:00
|
|
|
r = bytestream2_get_byte(&s->g);
|
|
|
|
g = bytestream2_get_byte(&s->g);
|
|
|
|
b = bytestream2_get_byte(&s->g);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 3);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
|
|
|
rgb[pixel_ptr++] = r;
|
|
|
|
rgb[pixel_ptr++] = g;
|
|
|
|
rgb[pixel_ptr++] = b;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 3);
|
|
|
|
|
|
|
|
/* copy pixels directly to output */
|
|
|
|
while (rle_code--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
|
|
|
|
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
|
|
|
|
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2005-10-28 19:01:12 +03:00
|
|
|
int rle_code;
|
2008-08-31 11:09:54 +03:00
|
|
|
int pixel_ptr;
|
2013-08-21 22:27:31 +03:00
|
|
|
int row_inc = s->frame->linesize[0];
|
2004-02-05 07:22:44 +02:00
|
|
|
unsigned int argb;
|
2013-08-21 22:27:31 +03:00
|
|
|
uint8_t *rgb = s->frame->data[0];
|
|
|
|
int pixel_limit = s->frame->linesize[0] * s->avctx->height;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
|
2013-02-14 19:58:12 +03:00
|
|
|
CHECK_PIXEL_PTR(0);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-08-21 22:03:40 +03:00
|
|
|
while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 07:22:44 +02:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 22:13:23 +03:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
|
2005-08-13 20:46:09 +03:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 07:22:44 +02:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 22:13:23 +03:00
|
|
|
argb = bytestream2_get_be32(&s->g);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 4);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
2011-11-25 21:16:44 +03:00
|
|
|
AV_WN32A(rgb + pixel_ptr, argb);
|
2004-02-05 07:22:44 +02:00
|
|
|
pixel_ptr += 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 4);
|
|
|
|
|
|
|
|
/* copy pixels directly to output */
|
|
|
|
while (rle_code--) {
|
2012-03-15 22:13:23 +03:00
|
|
|
argb = bytestream2_get_be32(&s->g);
|
2011-11-25 21:16:44 +03:00
|
|
|
AV_WN32A(rgb + pixel_ptr, argb);
|
|
|
|
pixel_ptr += 4;
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int qtrle_decode_init(AVCodecContext *avctx)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
s->avctx = avctx;
|
2008-09-08 17:24:59 +03:00
|
|
|
switch (avctx->bits_per_coded_sample) {
|
2004-02-05 07:22:44 +02:00
|
|
|
case 1:
|
2008-09-07 10:36:03 +03:00
|
|
|
case 33:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
|
2008-09-07 10:36:03 +03:00
|
|
|
break;
|
|
|
|
|
2004-02-05 07:22:44 +02:00
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 8:
|
|
|
|
case 34:
|
|
|
|
case 36:
|
|
|
|
case 40:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB555;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB32;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-02-14 19:11:46 +02:00
|
|
|
av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
|
2008-09-08 17:24:59 +03:00
|
|
|
avctx->bits_per_coded_sample);
|
2012-02-29 06:00:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 22:27:31 +03:00
|
|
|
s->frame = av_frame_alloc();
|
|
|
|
if (!s->frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qtrle_decode_frame(AVCodecContext *avctx,
|
2012-11-13 21:35:22 +03:00
|
|
|
void *data, int *got_frame,
|
2009-04-07 18:59:50 +03:00
|
|
|
AVPacket *avpkt)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2008-08-31 11:09:54 +03:00
|
|
|
int header, start_line;
|
2012-03-15 22:13:23 +03:00
|
|
|
int height, row_ptr;
|
2008-09-07 10:36:03 +03:00
|
|
|
int has_palette = 0;
|
2012-11-19 12:33:54 +03:00
|
|
|
int ret;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2012-03-15 22:13:23 +03:00
|
|
|
bytestream2_init(&s->g, avpkt->data, avpkt->size);
|
2013-08-21 22:27:31 +03:00
|
|
|
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
|
2012-11-19 12:33:54 +03:00
|
|
|
return ret;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2008-08-31 11:09:54 +03:00
|
|
|
/* check if this frame is even supposed to change */
|
2012-03-15 22:13:23 +03:00
|
|
|
if (avpkt->size < 8)
|
2008-08-31 11:09:54 +03:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* start after the chunk size */
|
2012-03-15 22:13:23 +03:00
|
|
|
bytestream2_seek(&s->g, 4, SEEK_SET);
|
2008-08-31 11:09:54 +03:00
|
|
|
|
|
|
|
/* fetch the header */
|
2012-03-15 22:13:23 +03:00
|
|
|
header = bytestream2_get_be16(&s->g);
|
2008-08-31 11:09:54 +03:00
|
|
|
|
|
|
|
/* if a header is present, fetch additional decoding parameters */
|
|
|
|
if (header & 0x0008) {
|
2012-03-15 22:13:23 +03:00
|
|
|
if (avpkt->size < 14)
|
2008-08-31 11:09:54 +03:00
|
|
|
goto done;
|
2012-03-15 22:13:23 +03:00
|
|
|
start_line = bytestream2_get_be16(&s->g);
|
|
|
|
bytestream2_skip(&s->g, 2);
|
|
|
|
height = bytestream2_get_be16(&s->g);
|
|
|
|
bytestream2_skip(&s->g, 2);
|
2011-10-09 00:01:33 +03:00
|
|
|
if (height > s->avctx->height - start_line)
|
|
|
|
goto done;
|
2008-08-31 11:09:54 +03:00
|
|
|
} else {
|
|
|
|
start_line = 0;
|
2012-03-15 22:13:23 +03:00
|
|
|
height = s->avctx->height;
|
2008-08-31 11:09:54 +03:00
|
|
|
}
|
2013-08-21 22:27:31 +03:00
|
|
|
row_ptr = s->frame->linesize[0] * start_line;
|
2008-08-31 11:09:54 +03:00
|
|
|
|
2008-09-08 17:24:59 +03:00
|
|
|
switch (avctx->bits_per_coded_sample) {
|
2004-02-05 07:22:44 +02:00
|
|
|
case 1:
|
|
|
|
case 33:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_1bpp(s, row_ptr, height);
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 34:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_2n4bpp(s, row_ptr, height, 2);
|
2008-09-07 10:36:03 +03:00
|
|
|
has_palette = 1;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
case 36:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_2n4bpp(s, row_ptr, height, 4);
|
2008-09-07 10:36:03 +03:00
|
|
|
has_palette = 1;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
case 40:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_8bpp(s, row_ptr, height);
|
2008-09-07 10:36:03 +03:00
|
|
|
has_palette = 1;
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_16bpp(s, row_ptr, height);
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_24bpp(s, row_ptr, height);
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
2012-03-15 22:13:23 +03:00
|
|
|
qtrle_decode_32bpp(s, row_ptr, height);
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-02-14 19:11:46 +02:00
|
|
|
av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
|
2008-09-08 17:24:59 +03:00
|
|
|
avctx->bits_per_coded_sample);
|
2004-02-05 07:22:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-09-07 10:36:03 +03:00
|
|
|
|
|
|
|
if(has_palette) {
|
2011-04-09 16:49:51 +03:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
|
|
|
|
|
|
|
if (pal) {
|
2013-08-21 22:27:31 +03:00
|
|
|
s->frame->palette_has_changed = 1;
|
2011-04-09 16:49:51 +03:00
|
|
|
memcpy(s->pal, pal, AVPALETTE_SIZE);
|
2008-09-07 10:36:03 +03:00
|
|
|
}
|
2011-04-09 16:49:51 +03:00
|
|
|
|
|
|
|
/* make the palette available on the way out */
|
2013-08-21 22:27:31 +03:00
|
|
|
memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
|
2008-09-07 10:36:03 +03:00
|
|
|
}
|
|
|
|
|
2008-08-31 11:09:54 +03:00
|
|
|
done:
|
2013-08-21 22:27:31 +03:00
|
|
|
if ((ret = av_frame_ref(data, s->frame)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
2012-03-15 22:13:23 +03:00
|
|
|
return avpkt->size;
|
2004-02-05 07:22:44 +02:00
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int qtrle_decode_end(AVCodecContext *avctx)
|
2004-02-05 07:22:44 +02:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2004-02-05 07:22:44 +02:00
|
|
|
|
2013-08-21 22:27:31 +03:00
|
|
|
av_frame_free(&s->frame);
|
2004-02-05 07:22:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_qtrle_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "qtrle",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_QTRLE,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(QtrleContext),
|
|
|
|
.init = qtrle_decode_init,
|
|
|
|
.close = qtrle_decode_end,
|
|
|
|
.decode = qtrle_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2004-02-05 07:22:44 +02:00
|
|
|
};
|