2004-02-05 06:22:44 +01:00
|
|
|
/*
|
|
|
|
* Quicktime Animation (RLE) Video Decoder
|
|
|
|
* Copyright (C) 2004 the ffmpeg project
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2006-10-07 17:30:46 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2004-02-05 06:22:44 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-02-05 06:22:44 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2004-02-05 06:22:44 +01: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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-02-05 06:22:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2004-02-05 06:22:44 +01: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 05:04:08 +01:00
|
|
|
* the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
|
2007-02-07 02:48:09 +01:00
|
|
|
* data. 24-bit data is RGB24 and 32-bit data is RGB32.
|
2004-02-05 06:22:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2012-03-15 20:13:23 +01:00
|
|
|
#include "bytestream.h"
|
2004-02-05 06:22:44 +01:00
|
|
|
|
|
|
|
typedef struct QtrleContext {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVFrame frame;
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
GetByteContext g;
|
2011-04-09 15:49:51 +02:00
|
|
|
uint32_t pal[256];
|
2004-02-05 06:22:44 +01:00
|
|
|
} QtrleContext;
|
|
|
|
|
|
|
|
#define CHECK_PIXEL_PTR(n) \
|
2005-08-13 19:46:09 +02:00
|
|
|
if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
|
|
|
|
av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
|
2004-02-05 06:22:44 +01:00
|
|
|
pixel_ptr + n, pixel_limit); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2008-09-07 09:36:03 +02:00
|
|
|
int rle_code;
|
|
|
|
int pixel_ptr = 0;
|
|
|
|
int row_inc = s->frame.linesize[0];
|
|
|
|
unsigned char pi0, pi1; /* 2 8-pixel values */
|
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
|
|
|
int skip;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2008-09-07 09:36:03 +02:00
|
|
|
while (lines_to_change) {
|
2012-03-15 20:13:23 +01:00
|
|
|
skip = bytestream2_get_byte(&s->g);
|
|
|
|
rle_code = (signed char)bytestream2_get_byte(&s->g);
|
2008-09-07 09:36:03 +02:00
|
|
|
if (rle_code == 0)
|
|
|
|
break;
|
|
|
|
if(skip & 0x80) {
|
|
|
|
lines_to_change--;
|
|
|
|
row_ptr += row_inc;
|
|
|
|
pixel_ptr = row_ptr + 2 * (skip & 0x7f);
|
|
|
|
} else
|
|
|
|
pixel_ptr += 2 * skip;
|
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
|
|
|
|
|
|
|
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 20:13:23 +01:00
|
|
|
|
|
|
|
pi0 = bytestream2_get_byte(&s->g);
|
|
|
|
pi1 = bytestream2_get_byte(&s->g);
|
2008-09-07 09:36:03 +02: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);
|
|
|
|
|
|
|
|
while (rle_code--)
|
2012-03-15 20:13:23 +01:00
|
|
|
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
|
2008-09-07 09:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
|
|
|
|
int lines_to_change, int bpp)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2008-09-07 09:36:03 +02:00
|
|
|
int rle_code, i;
|
2008-08-31 10:09:54 +02:00
|
|
|
int pixel_ptr;
|
2004-05-31 22:18:08 +02:00
|
|
|
int row_inc = s->frame.linesize[0];
|
2008-09-07 09:36:03 +02:00
|
|
|
unsigned char pi[16]; /* 16 palette indices */
|
2004-05-31 22:18:08 +02:00
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
2008-09-07 09:36:03 +02:00
|
|
|
int num_pixels = (bpp == 4) ? 8 : 16;
|
2004-05-31 22:18:08 +02:00
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
|
2004-05-31 22:18:08 +02:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
|
2004-05-31 22:18:08 +02:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
|
2005-08-13 19:46:09 +02:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-05-31 22:18:08 +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 01:14:25 +02:00
|
|
|
* indexes, and output them rle_code times */
|
2008-09-07 09:36:03 +02:00
|
|
|
for (i = num_pixels-1; i >= 0; i--) {
|
2012-03-15 20:13:23 +01: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 09:36:03 +02:00
|
|
|
}
|
|
|
|
CHECK_PIXEL_PTR(rle_code * num_pixels);
|
2004-05-31 22:18:08 +02:00
|
|
|
while (rle_code--) {
|
2008-09-07 09:36:03 +02:00
|
|
|
for (i = 0; i < num_pixels; i++)
|
|
|
|
rgb[pixel_ptr++] = pi[i];
|
2004-05-31 22:18:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* copy the same pixel directly to output 4 times */
|
|
|
|
rle_code *= 4;
|
2008-09-07 09:36:03 +02:00
|
|
|
CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
|
2004-05-31 22:18:08 +02:00
|
|
|
while (rle_code--) {
|
2008-09-07 09:36:03 +02:00
|
|
|
if(bpp == 4) {
|
2012-03-15 20:13:23 +01:00
|
|
|
int x = bytestream2_get_byte(&s->g);
|
|
|
|
rgb[pixel_ptr++] = (x >> 4) & 0x0f;
|
|
|
|
rgb[pixel_ptr++] = x & 0x0f;
|
2008-09-07 09:36:03 +02:00
|
|
|
} else {
|
2012-03-15 20:13:23 +01: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 09:36:03 +02:00
|
|
|
}
|
2004-05-31 22:18:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2004-02-14 19:12:36 +01:00
|
|
|
int rle_code;
|
2008-08-31 10:09:54 +02:00
|
|
|
int pixel_ptr;
|
2004-02-05 06:22:44 +01:00
|
|
|
int row_inc = s->frame.linesize[0];
|
2008-05-27 01:14:25 +02:00
|
|
|
unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
|
2004-02-05 06:22:44 +01:00
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 06:22:44 +01:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
|
2005-08-13 19:46:09 +02:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 06:22:44 +01: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 01:14:25 +02:00
|
|
|
* indexes, and output them rle_code times */
|
2012-03-15 20:13:23 +01: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 06:22:44 +01: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);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2005-10-28 18:01:12 +02:00
|
|
|
int rle_code;
|
2008-08-31 10:09:54 +02:00
|
|
|
int pixel_ptr;
|
2004-02-05 06:22:44 +01:00
|
|
|
int row_inc = s->frame.linesize[0];
|
|
|
|
unsigned short rgb16;
|
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 06:22:44 +01:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
|
2005-08-13 19:46:09 +02:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 06:22:44 +01:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 20:13:23 +01:00
|
|
|
rgb16 = bytestream2_get_be16(&s->g);
|
2004-02-05 06:22:44 +01:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 2);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
|
|
|
*(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
|
|
|
|
pixel_ptr += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 2);
|
|
|
|
|
|
|
|
/* copy pixels directly to output */
|
|
|
|
while (rle_code--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
rgb16 = bytestream2_get_be16(&s->g);
|
2004-02-05 06:22:44 +01:00
|
|
|
*(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
|
|
|
|
pixel_ptr += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2005-10-28 18:01:12 +02:00
|
|
|
int rle_code;
|
2008-08-31 10:09:54 +02:00
|
|
|
int pixel_ptr;
|
2004-02-05 06:22:44 +01:00
|
|
|
int row_inc = s->frame.linesize[0];
|
|
|
|
unsigned char r, g, b;
|
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 06:22:44 +01:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
|
2005-08-13 19:46:09 +02:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 06:22:44 +01:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 20:13:23 +01:00
|
|
|
r = bytestream2_get_byte(&s->g);
|
|
|
|
g = bytestream2_get_byte(&s->g);
|
|
|
|
b = bytestream2_get_byte(&s->g);
|
2004-02-05 06:22:44 +01: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 20:13:23 +01: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 06:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2005-10-28 18:01:12 +02:00
|
|
|
int rle_code;
|
2008-08-31 10:09:54 +02:00
|
|
|
int pixel_ptr;
|
2004-02-05 06:22:44 +01:00
|
|
|
int row_inc = s->frame.linesize[0];
|
|
|
|
unsigned int argb;
|
|
|
|
unsigned char *rgb = s->frame.data[0];
|
|
|
|
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
|
|
|
|
|
|
|
|
while (lines_to_change--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
|
2004-02-05 06:22:44 +01:00
|
|
|
if (rle_code == 0) {
|
|
|
|
/* there's another skip code in the stream */
|
2012-03-15 20:13:23 +01:00
|
|
|
pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
|
2005-08-13 19:46:09 +02:00
|
|
|
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
|
2004-02-05 06:22:44 +01:00
|
|
|
} else if (rle_code < 0) {
|
|
|
|
/* decode the run length code */
|
|
|
|
rle_code = -rle_code;
|
2012-03-15 20:13:23 +01:00
|
|
|
argb = bytestream2_get_be32(&s->g);
|
2004-02-05 06:22:44 +01:00
|
|
|
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 4);
|
|
|
|
|
|
|
|
while (rle_code--) {
|
2011-11-25 19:16:44 +01:00
|
|
|
AV_WN32A(rgb + pixel_ptr, argb);
|
2004-02-05 06:22:44 +01:00
|
|
|
pixel_ptr += 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_PIXEL_PTR(rle_code * 4);
|
|
|
|
|
|
|
|
/* copy pixels directly to output */
|
|
|
|
while (rle_code--) {
|
2012-03-15 20:13:23 +01:00
|
|
|
argb = bytestream2_get_be32(&s->g);
|
2011-11-25 19:16:44 +01:00
|
|
|
AV_WN32A(rgb + pixel_ptr, argb);
|
|
|
|
pixel_ptr += 4;
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_ptr += row_inc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int qtrle_decode_init(AVCodecContext *avctx)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
|
|
|
s->avctx = avctx;
|
2008-09-08 16:24:59 +02:00
|
|
|
switch (avctx->bits_per_coded_sample) {
|
2004-02-05 06:22:44 +01:00
|
|
|
case 1:
|
2008-09-07 09:36:03 +02:00
|
|
|
case 33:
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
|
2008-09-07 09:36:03 +02:00
|
|
|
break;
|
|
|
|
|
2004-02-05 06:22:44 +01:00
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 8:
|
|
|
|
case 34:
|
|
|
|
case 36:
|
|
|
|
case 40:
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB555;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB32;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-02-14 18:11:46 +01:00
|
|
|
av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
|
2008-09-08 16:24:59 +02:00
|
|
|
avctx->bits_per_coded_sample);
|
2012-02-29 04:00:39 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s->frame.data[0] = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qtrle_decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data, int *data_size,
|
2009-04-07 17:59:50 +02:00
|
|
|
AVPacket *avpkt)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2008-08-31 10:09:54 +02:00
|
|
|
int header, start_line;
|
2012-03-15 20:13:23 +01:00
|
|
|
int height, row_ptr;
|
2008-09-07 09:36:03 +02:00
|
|
|
int has_palette = 0;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
2012-03-15 20:13:23 +01:00
|
|
|
bytestream2_init(&s->g, avpkt->data, avpkt->size);
|
2004-02-05 06:22:44 +01:00
|
|
|
s->frame.reference = 1;
|
|
|
|
s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
|
|
|
|
FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
|
|
|
|
if (avctx->reget_buffer(avctx, &s->frame)) {
|
2004-02-14 18:11:46 +01:00
|
|
|
av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
2004-02-05 06:22:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-31 10:09:54 +02:00
|
|
|
/* check if this frame is even supposed to change */
|
2012-03-15 20:13:23 +01:00
|
|
|
if (avpkt->size < 8)
|
2008-08-31 10:09:54 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* start after the chunk size */
|
2012-03-15 20:13:23 +01:00
|
|
|
bytestream2_seek(&s->g, 4, SEEK_SET);
|
2008-08-31 10:09:54 +02:00
|
|
|
|
|
|
|
/* fetch the header */
|
2012-03-15 20:13:23 +01:00
|
|
|
header = bytestream2_get_be16(&s->g);
|
2008-08-31 10:09:54 +02:00
|
|
|
|
|
|
|
/* if a header is present, fetch additional decoding parameters */
|
|
|
|
if (header & 0x0008) {
|
2012-03-15 20:13:23 +01:00
|
|
|
if (avpkt->size < 14)
|
2008-08-31 10:09:54 +02:00
|
|
|
goto done;
|
2012-03-15 20:13:23 +01:00
|
|
|
start_line = bytestream2_get_be16(&s->g);
|
|
|
|
bytestream2_skip(&s->g, 2);
|
|
|
|
height = bytestream2_get_be16(&s->g);
|
|
|
|
bytestream2_skip(&s->g, 2);
|
2008-08-31 10:09:54 +02:00
|
|
|
} else {
|
|
|
|
start_line = 0;
|
2012-03-15 20:13:23 +01:00
|
|
|
height = s->avctx->height;
|
2008-08-31 10:09:54 +02:00
|
|
|
}
|
|
|
|
row_ptr = s->frame.linesize[0] * start_line;
|
|
|
|
|
2008-09-08 16:24:59 +02:00
|
|
|
switch (avctx->bits_per_coded_sample) {
|
2004-02-05 06:22:44 +01:00
|
|
|
case 1:
|
|
|
|
case 33:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_1bpp(s, row_ptr, height);
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 34:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_2n4bpp(s, row_ptr, height, 2);
|
2008-09-07 09:36:03 +02:00
|
|
|
has_palette = 1;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
case 36:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_2n4bpp(s, row_ptr, height, 4);
|
2008-09-07 09:36:03 +02:00
|
|
|
has_palette = 1;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
case 40:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_8bpp(s, row_ptr, height);
|
2008-09-07 09:36:03 +02:00
|
|
|
has_palette = 1;
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_16bpp(s, row_ptr, height);
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_24bpp(s, row_ptr, height);
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
2012-03-15 20:13:23 +01:00
|
|
|
qtrle_decode_32bpp(s, row_ptr, height);
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-02-14 18:11:46 +01:00
|
|
|
av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
|
2008-09-08 16:24:59 +02:00
|
|
|
avctx->bits_per_coded_sample);
|
2004-02-05 06:22:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-09-07 09:36:03 +02:00
|
|
|
|
|
|
|
if(has_palette) {
|
2011-04-09 15:49:51 +02:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
|
|
|
|
|
|
|
if (pal) {
|
2008-09-07 09:36:03 +02:00
|
|
|
s->frame.palette_has_changed = 1;
|
2011-04-09 15:49:51 +02:00
|
|
|
memcpy(s->pal, pal, AVPALETTE_SIZE);
|
2008-09-07 09:36:03 +02:00
|
|
|
}
|
2011-04-09 15:49:51 +02:00
|
|
|
|
|
|
|
/* make the palette available on the way out */
|
|
|
|
memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
|
2008-09-07 09:36:03 +02:00
|
|
|
}
|
|
|
|
|
2008-08-31 10:09:54 +02:00
|
|
|
done:
|
2004-02-05 06:22:44 +01:00
|
|
|
*data_size = sizeof(AVFrame);
|
|
|
|
*(AVFrame*)data = s->frame;
|
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
2012-03-15 20:13:23 +01:00
|
|
|
return avpkt->size;
|
2004-02-05 06:22:44 +01:00
|
|
|
}
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int qtrle_decode_end(AVCodecContext *avctx)
|
2004-02-05 06:22:44 +01:00
|
|
|
{
|
2007-04-08 22:24:16 +02:00
|
|
|
QtrleContext *s = avctx->priv_data;
|
2004-02-05 06:22:44 +01:00
|
|
|
|
|
|
|
if (s->frame.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_qtrle_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "qtrle",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_QTRLE,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(QtrleContext),
|
|
|
|
.init = qtrle_decode_init,
|
|
|
|
.close = qtrle_decode_end,
|
|
|
|
.decode = qtrle_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2012-04-06 18:19:39 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
|
2004-02-05 06:22:44 +01:00
|
|
|
};
|