2007-04-06 21:29:27 +03:00
|
|
|
/*
|
|
|
|
* SGI image encoder
|
|
|
|
* Todd Kirby <doubleshot@pacbell.net>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-11-30 19:17:31 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
2007-04-06 21:29:27 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2021-05-11 15:17:13 +02:00
|
|
|
#include "encode.h"
|
2007-04-06 21:29:27 +03:00
|
|
|
#include "sgi.h"
|
|
|
|
#include "rle.h"
|
|
|
|
|
|
|
|
#define SGI_SINGLE_CHAN 2
|
|
|
|
#define SGI_MULTI_CHAN 3
|
|
|
|
|
2015-11-30 19:17:31 +02:00
|
|
|
typedef struct SgiContext {
|
|
|
|
AVClass *class;
|
|
|
|
|
|
|
|
int rle;
|
|
|
|
} SgiContext;
|
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
static av_cold int encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
2012-05-31 21:10:04 +03:00
|
|
|
if (avctx->width > 65535 || avctx->height > 65535) {
|
2020-11-26 20:15:16 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported resolution %dx%d. "
|
|
|
|
"SGI does not support resolutions above 65535x65535\n",
|
|
|
|
avctx->width, avctx->height);
|
2012-05-31 21:10:04 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-05-31 21:10:04 +03:00
|
|
|
}
|
2012-05-31 21:10:04 +03:00
|
|
|
|
2007-04-06 21:29:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-20 03:17:33 +02:00
|
|
|
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
|
|
|
|
int w, int bpp)
|
|
|
|
{
|
|
|
|
int val, count, x, start = bytestream2_tell_p(pbc);
|
|
|
|
void (*bytestream2_put)(PutByteContext *, unsigned int);
|
|
|
|
|
2015-11-20 03:17:34 +02:00
|
|
|
if (bpp == 1)
|
|
|
|
bytestream2_put = bytestream2_put_byte;
|
|
|
|
else
|
|
|
|
bytestream2_put = bytestream2_put_be16;
|
2015-11-20 03:17:33 +02:00
|
|
|
|
|
|
|
for (x = 0; x < w; x += count) {
|
|
|
|
/* see if we can encode the next set of pixels with RLE */
|
|
|
|
count = ff_rle_count_pixels(src, w - x, bpp, 1);
|
|
|
|
if (count > 1) {
|
|
|
|
if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2015-11-20 03:17:34 +02:00
|
|
|
val = bpp == 1 ? *src : AV_RB16(src);
|
2015-11-20 03:17:33 +02:00
|
|
|
bytestream2_put(pbc, count);
|
|
|
|
bytestream2_put(pbc, val);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
/* fall back on uncompressed */
|
|
|
|
count = ff_rle_count_pixels(src, w - x, bpp, 0);
|
|
|
|
if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
bytestream2_put(pbc, count + 0x80);
|
|
|
|
for (i = 0; i < count; i++) {
|
2015-11-20 03:17:34 +02:00
|
|
|
val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
|
2015-11-20 03:17:33 +02:00
|
|
|
bytestream2_put(pbc, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src += count * bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytestream2_tell_p(pbc) - start;
|
|
|
|
}
|
|
|
|
|
2012-02-12 11:32:40 +03:00
|
|
|
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
2009-10-01 12:11:07 +03:00
|
|
|
{
|
2015-11-30 19:17:31 +02:00
|
|
|
SgiContext *s = avctx->priv_data;
|
2013-11-09 12:14:46 +03:00
|
|
|
const AVFrame * const p = frame;
|
2015-11-20 03:17:32 +02:00
|
|
|
PutByteContext pbc;
|
2022-07-26 09:09:44 +02:00
|
|
|
uint8_t *encode_buf;
|
2015-12-03 02:10:02 +02:00
|
|
|
int x, y, z, length, tablesize, ret, i;
|
2007-04-06 21:29:27 +03:00
|
|
|
unsigned int width, height, depth, dimension;
|
2014-03-13 03:19:12 +03:00
|
|
|
unsigned int bytes_per_channel, pixmax, put_be;
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
width = avctx->width;
|
2007-04-06 21:29:27 +03:00
|
|
|
height = avctx->height;
|
2012-01-20 01:09:17 +03:00
|
|
|
bytes_per_channel = 1;
|
|
|
|
pixmax = 0xFF;
|
|
|
|
put_be = HAVE_BIGENDIAN;
|
2007-04-06 21:29:27 +03:00
|
|
|
|
|
|
|
switch (avctx->pix_fmt) {
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_GRAY8:
|
2009-10-01 12:11:07 +03:00
|
|
|
dimension = SGI_SINGLE_CHAN;
|
|
|
|
depth = SGI_GRAYSCALE;
|
|
|
|
break;
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_RGB24:
|
2009-10-01 12:11:07 +03:00
|
|
|
dimension = SGI_MULTI_CHAN;
|
|
|
|
depth = SGI_RGB;
|
|
|
|
break;
|
2012-10-06 13:10:34 +03:00
|
|
|
case AV_PIX_FMT_RGBA:
|
2009-10-01 12:11:07 +03:00
|
|
|
dimension = SGI_MULTI_CHAN;
|
|
|
|
depth = SGI_RGBA;
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_GRAY16LE:
|
2012-01-20 01:09:17 +03:00
|
|
|
put_be = !HAVE_BIGENDIAN;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_GRAY16BE:
|
2012-01-20 01:09:17 +03:00
|
|
|
bytes_per_channel = 2;
|
|
|
|
pixmax = 0xFFFF;
|
|
|
|
dimension = SGI_SINGLE_CHAN;
|
|
|
|
depth = SGI_GRAYSCALE;
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_RGB48LE:
|
2012-01-20 01:09:17 +03:00
|
|
|
put_be = !HAVE_BIGENDIAN;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_RGB48BE:
|
2012-01-20 01:09:17 +03:00
|
|
|
bytes_per_channel = 2;
|
|
|
|
pixmax = 0xFFFF;
|
|
|
|
dimension = SGI_MULTI_CHAN;
|
|
|
|
depth = SGI_RGB;
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_RGBA64LE:
|
2012-01-20 01:09:17 +03:00
|
|
|
put_be = !HAVE_BIGENDIAN;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_RGBA64BE:
|
2012-01-20 01:09:17 +03:00
|
|
|
bytes_per_channel = 2;
|
|
|
|
pixmax = 0xFFFF;
|
|
|
|
dimension = SGI_MULTI_CHAN;
|
|
|
|
depth = SGI_RGBA;
|
|
|
|
break;
|
2009-10-01 12:11:07 +03:00
|
|
|
default:
|
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-06 21:29:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tablesize = depth * height * 4;
|
2012-02-12 11:32:40 +03:00
|
|
|
length = SGI_HEADER_SIZE;
|
2015-11-30 19:17:31 +02:00
|
|
|
if (!s->rle)
|
2012-02-12 11:32:40 +03:00
|
|
|
length += depth * height * width;
|
2015-11-20 03:17:33 +02:00
|
|
|
else // assume sgi_rle_encode() produces at most 2x size of input
|
2012-02-12 11:32:40 +03:00
|
|
|
length += tablesize * 2 + depth * height * (2 * width + 1);
|
|
|
|
|
2021-05-11 15:17:13 +02:00
|
|
|
if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0)
|
2012-02-12 11:32:40 +03:00
|
|
|
return ret;
|
2015-11-20 03:17:32 +02:00
|
|
|
|
|
|
|
bytestream2_init_writer(&pbc, pkt->data, pkt->size);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
|
|
|
/* Encode header. */
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_be16(&pbc, SGI_MAGIC);
|
2015-11-30 19:17:31 +02:00
|
|
|
bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_byte(&pbc, bytes_per_channel);
|
|
|
|
bytestream2_put_be16(&pbc, dimension);
|
|
|
|
bytestream2_put_be16(&pbc, width);
|
|
|
|
bytestream2_put_be16(&pbc, height);
|
|
|
|
bytestream2_put_be16(&pbc, depth);
|
|
|
|
|
|
|
|
bytestream2_put_be32(&pbc, 0L); /* pixmin */
|
|
|
|
bytestream2_put_be32(&pbc, pixmax);
|
|
|
|
bytestream2_put_be32(&pbc, 0L); /* dummy */
|
2007-04-06 21:29:27 +03:00
|
|
|
|
|
|
|
/* name */
|
2015-12-03 02:10:02 +02:00
|
|
|
for (i = 0; i < 80; i++)
|
|
|
|
bytestream2_put_byte(&pbc, 0L);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2014-03-13 17:20:00 +03:00
|
|
|
/* colormap */
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_be32(&pbc, 0L);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
|
|
|
/* The rest of the 512 byte header is unused. */
|
2015-12-03 02:10:02 +02:00
|
|
|
for (i = 0; i < 404; i++)
|
|
|
|
bytestream2_put_byte(&pbc, 0L);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2015-11-30 19:17:31 +02:00
|
|
|
if (s->rle) {
|
2015-11-20 03:17:32 +02:00
|
|
|
PutByteContext taboff_pcb, tablen_pcb;
|
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
/* Skip RLE offset table. */
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
|
|
|
|
bytestream2_skip_p(&pbc, tablesize);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
/* Skip RLE length table. */
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
|
|
|
|
bytestream2_skip_p(&pbc, tablesize);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
/* Make an intermediate consecutive buffer. */
|
2015-11-20 03:17:34 +02:00
|
|
|
if (!(encode_buf = av_malloc(width * bytes_per_channel)))
|
2015-02-09 16:05:13 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
for (z = 0; z < depth; z++) {
|
2022-07-26 09:09:44 +02:00
|
|
|
const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
for (y = 0; y < height; y++) {
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2015-11-20 03:17:34 +02:00
|
|
|
for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
|
2020-07-19 21:57:41 +02:00
|
|
|
if (bytes_per_channel == 1) {
|
|
|
|
encode_buf[x] = in_buf[depth * x];
|
|
|
|
} else if (HAVE_BIGENDIAN ^ put_be) {
|
|
|
|
encode_buf[x + 1] = in_buf[depth * x];
|
|
|
|
encode_buf[x] = in_buf[depth * x + 1];
|
|
|
|
} else {
|
|
|
|
encode_buf[x] = in_buf[depth * x];
|
|
|
|
encode_buf[x + 1] = in_buf[depth * x + 1];
|
|
|
|
}
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2015-11-20 03:17:34 +02:00
|
|
|
length = sgi_rle_encode(&pbc, encode_buf, width,
|
|
|
|
bytes_per_channel);
|
2015-11-20 03:17:32 +02:00
|
|
|
if (length < 1) {
|
2009-10-01 12:11:07 +03:00
|
|
|
av_free(encode_buf);
|
2015-11-24 01:56:09 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-10-01 12:11:07 +03:00
|
|
|
}
|
2007-04-06 21:29:27 +03:00
|
|
|
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_be32(&tablen_pcb, length);
|
2009-10-01 12:11:07 +03:00
|
|
|
in_buf -= p->linesize[0];
|
|
|
|
}
|
2007-04-06 21:29:27 +03:00
|
|
|
}
|
|
|
|
|
2009-10-01 12:11:07 +03:00
|
|
|
av_free(encode_buf);
|
2009-10-01 12:30:08 +03:00
|
|
|
} else {
|
|
|
|
for (z = 0; z < depth; z++) {
|
2022-07-26 09:09:44 +02:00
|
|
|
const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
|
2009-10-01 00:23:47 +03:00
|
|
|
|
2009-10-01 12:30:08 +03:00
|
|
|
for (y = 0; y < height; y++) {
|
|
|
|
for (x = 0; x < width * depth; x += depth)
|
2014-03-13 03:19:12 +03:00
|
|
|
if (bytes_per_channel == 1)
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_byte(&pbc, in_buf[x]);
|
2014-03-13 03:19:12 +03:00
|
|
|
else
|
|
|
|
if (put_be)
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
|
2014-03-13 03:19:12 +03:00
|
|
|
else
|
2015-11-20 03:17:32 +02:00
|
|
|
bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
|
2009-10-01 00:23:47 +03:00
|
|
|
|
2009-10-01 12:30:08 +03:00
|
|
|
in_buf -= p->linesize[0];
|
2009-10-01 00:23:47 +03:00
|
|
|
}
|
|
|
|
}
|
2009-10-01 12:30:08 +03:00
|
|
|
}
|
2009-10-01 00:23:47 +03:00
|
|
|
|
2007-04-06 21:29:27 +03:00
|
|
|
/* total length */
|
2015-11-20 03:17:32 +02:00
|
|
|
pkt->size = bytestream2_tell_p(&pbc);
|
2012-02-12 11:32:40 +03:00
|
|
|
*got_packet = 1;
|
|
|
|
|
|
|
|
return 0;
|
2007-04-06 21:29:27 +03:00
|
|
|
}
|
|
|
|
|
2015-11-30 19:17:31 +02:00
|
|
|
#define OFFSET(x) offsetof(SgiContext, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
|
|
|
|
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass sgi_class = {
|
|
|
|
.class_name = "sgi",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_sgi_encoder = {
|
|
|
|
.p.name = "sgi",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("SGI image"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_SGI,
|
2021-05-11 20:52:13 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2015-11-30 19:17:31 +02:00
|
|
|
.priv_data_size = sizeof(SgiContext),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &sgi_class,
|
2014-03-13 17:20:00 +03:00
|
|
|
.init = encode_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_ENCODE_CB(encode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.pix_fmts = (const enum AVPixelFormat[]) {
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
|
|
|
|
AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
|
|
|
|
AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
|
2014-03-13 03:19:12 +03:00
|
|
|
AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
|
|
|
|
AV_PIX_FMT_NONE
|
2012-04-06 19:19:39 +03:00
|
|
|
},
|
2007-04-06 21:29:27 +03:00
|
|
|
};
|