2 * BRender PIX (.pix) image decoder
3 * Copyright (c) 2012 Aleksi Nurmi
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Tested against samples from I-War / Independence War and Defiance.
24 * If the PIX file does not contain a palette, the
25 * palette_has_changed property of the AVFrame is set to 0.
28 #include "libavutil/imgutils.h"
30 #include "bytestream.h"
33 typedef struct BRPixHeader {
35 unsigned int width, height;
38 static int brpix_decode_header(BRPixHeader *out, GetByteContext *pgb)
40 unsigned int header_len = bytestream2_get_be32(pgb);
42 out->format = bytestream2_get_byte(pgb);
43 bytestream2_skip(pgb, 2);
44 out->width = bytestream2_get_be16(pgb);
45 out->height = bytestream2_get_be16(pgb);
47 // the header is at least 11 bytes long; we read the first 7
48 if (header_len < 11) {
52 // skip the rest of the header
53 bytestream2_skip(pgb, header_len-7);
58 static int brpix_decode_frame(AVCodecContext *avctx,
59 void *data, int *got_frame,
62 AVFrame *frame = data;
67 unsigned int bytes_pp;
69 unsigned int magic[4];
70 unsigned int chunk_type;
71 unsigned int data_len;
74 bytestream2_init(&gb, avpkt->data, avpkt->size);
76 magic[0] = bytestream2_get_be32(&gb);
77 magic[1] = bytestream2_get_be32(&gb);
78 magic[2] = bytestream2_get_be32(&gb);
79 magic[3] = bytestream2_get_be32(&gb);
81 if (magic[0] != 0x12 ||
85 av_log(avctx, AV_LOG_ERROR, "Not a BRender PIX file\n");
86 return AVERROR_INVALIDDATA;
89 chunk_type = bytestream2_get_be32(&gb);
90 if (chunk_type != 0x3 && chunk_type != 0x3d) {
91 av_log(avctx, AV_LOG_ERROR, "Invalid chunk type %d\n", chunk_type);
92 return AVERROR_INVALIDDATA;
95 ret = brpix_decode_header(&hdr, &gb);
97 av_log(avctx, AV_LOG_ERROR, "Invalid header length\n");
98 return AVERROR_INVALIDDATA;
100 switch (hdr.format) {
102 avctx->pix_fmt = AV_PIX_FMT_PAL8;
106 avctx->pix_fmt = AV_PIX_FMT_RGB555BE;
110 avctx->pix_fmt = AV_PIX_FMT_RGB565BE;
114 avctx->pix_fmt = AV_PIX_FMT_RGB24;
118 avctx->pix_fmt = AV_PIX_FMT_0RGB;
122 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
126 av_log(avctx, AV_LOG_ERROR, "Format %d is not supported\n",
128 return AVERROR_PATCHWELCOME;
131 if (av_image_check_size(hdr.width, hdr.height, 0, avctx) < 0)
132 return AVERROR_INVALIDDATA;
134 if (hdr.width != avctx->width || hdr.height != avctx->height)
135 avcodec_set_dimensions(avctx, hdr.width, hdr.height);
137 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
140 chunk_type = bytestream2_get_be32(&gb);
142 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
143 (chunk_type == 0x3 || chunk_type == 0x3d)) {
145 uint32_t *pal_out = (uint32_t *)frame->data[1];
148 ret = brpix_decode_header(&palhdr, &gb);
150 av_log(avctx, AV_LOG_ERROR, "Invalid palette header length\n");
151 return AVERROR_INVALIDDATA;
153 if (palhdr.format != 7) {
154 av_log(avctx, AV_LOG_ERROR, "Palette is not in 0RGB format\n");
155 return AVERROR_INVALIDDATA;
158 chunk_type = bytestream2_get_be32(&gb);
159 data_len = bytestream2_get_be32(&gb);
160 bytestream2_skip(&gb, 8);
161 if (chunk_type != 0x21 || data_len != 1032 ||
162 bytestream2_get_bytes_left(&gb) < 1032) {
163 av_log(avctx, AV_LOG_ERROR, "Invalid palette data\n");
164 return AVERROR_INVALIDDATA;
166 // convert 0RGB to machine endian format (ARGB32)
167 for (i = 0; i < 256; ++i) {
168 bytestream2_skipu(&gb, 1);
169 *pal_out++ = (0xFFU << 24) | bytestream2_get_be24u(&gb);
171 bytestream2_skip(&gb, 8);
173 frame->palette_has_changed = 1;
175 chunk_type = bytestream2_get_be32(&gb);
176 } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
177 uint32_t *pal_out = (uint32_t *)frame->data[1];
180 for (i = 0; i < 256; ++i) {
181 *pal_out++ = (0xFFU << 24) | (i * 0x010101);
183 frame->palette_has_changed = 1;
186 data_len = bytestream2_get_be32(&gb);
187 bytestream2_skip(&gb, 8);
189 // read the image data to the buffer
191 unsigned int bytes_per_scanline = bytes_pp * hdr.width;
192 unsigned int bytes_left = bytestream2_get_bytes_left(&gb);
194 if (chunk_type != 0x21 || data_len != bytes_left ||
195 bytes_left / bytes_per_scanline < hdr.height)
197 av_log(avctx, AV_LOG_ERROR, "Invalid image data\n");
198 return AVERROR_INVALIDDATA;
201 av_image_copy_plane(frame->data[0], frame->linesize[0],
202 avpkt->data + bytestream2_tell(&gb),
204 bytes_per_scanline, hdr.height);
212 AVCodec ff_brender_pix_decoder = {
213 .name = "brender_pix",
214 .type = AVMEDIA_TYPE_VIDEO,
215 .id = AV_CODEC_ID_BRENDER_PIX,
216 .decode = brpix_decode_frame,
217 .capabilities = CODEC_CAP_DR1,
218 .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"),