yading@10
|
1 /*
|
yading@10
|
2 * BRender PIX (.pix) image decoder
|
yading@10
|
3 * Copyright (c) 2012 Aleksi Nurmi
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /*
|
yading@10
|
23 * Tested against samples from I-War / Independence War and Defiance.
|
yading@10
|
24 * If the PIX file does not contain a palette, the
|
yading@10
|
25 * palette_has_changed property of the AVFrame is set to 0.
|
yading@10
|
26 */
|
yading@10
|
27
|
yading@10
|
28 #include "libavutil/imgutils.h"
|
yading@10
|
29 #include "avcodec.h"
|
yading@10
|
30 #include "bytestream.h"
|
yading@10
|
31 #include "internal.h"
|
yading@10
|
32
|
yading@10
|
33 typedef struct BRPixHeader {
|
yading@10
|
34 int format;
|
yading@10
|
35 unsigned int width, height;
|
yading@10
|
36 } BRPixHeader;
|
yading@10
|
37
|
yading@10
|
38 static int brpix_decode_header(BRPixHeader *out, GetByteContext *pgb)
|
yading@10
|
39 {
|
yading@10
|
40 unsigned int header_len = bytestream2_get_be32(pgb);
|
yading@10
|
41
|
yading@10
|
42 out->format = bytestream2_get_byte(pgb);
|
yading@10
|
43 bytestream2_skip(pgb, 2);
|
yading@10
|
44 out->width = bytestream2_get_be16(pgb);
|
yading@10
|
45 out->height = bytestream2_get_be16(pgb);
|
yading@10
|
46
|
yading@10
|
47 // the header is at least 11 bytes long; we read the first 7
|
yading@10
|
48 if (header_len < 11) {
|
yading@10
|
49 return 0;
|
yading@10
|
50 }
|
yading@10
|
51
|
yading@10
|
52 // skip the rest of the header
|
yading@10
|
53 bytestream2_skip(pgb, header_len-7);
|
yading@10
|
54
|
yading@10
|
55 return 1;
|
yading@10
|
56 }
|
yading@10
|
57
|
yading@10
|
58 static int brpix_decode_frame(AVCodecContext *avctx,
|
yading@10
|
59 void *data, int *got_frame,
|
yading@10
|
60 AVPacket *avpkt)
|
yading@10
|
61 {
|
yading@10
|
62 AVFrame *frame = data;
|
yading@10
|
63
|
yading@10
|
64 int ret;
|
yading@10
|
65 GetByteContext gb;
|
yading@10
|
66
|
yading@10
|
67 unsigned int bytes_pp;
|
yading@10
|
68
|
yading@10
|
69 unsigned int magic[4];
|
yading@10
|
70 unsigned int chunk_type;
|
yading@10
|
71 unsigned int data_len;
|
yading@10
|
72 BRPixHeader hdr;
|
yading@10
|
73
|
yading@10
|
74 bytestream2_init(&gb, avpkt->data, avpkt->size);
|
yading@10
|
75
|
yading@10
|
76 magic[0] = bytestream2_get_be32(&gb);
|
yading@10
|
77 magic[1] = bytestream2_get_be32(&gb);
|
yading@10
|
78 magic[2] = bytestream2_get_be32(&gb);
|
yading@10
|
79 magic[3] = bytestream2_get_be32(&gb);
|
yading@10
|
80
|
yading@10
|
81 if (magic[0] != 0x12 ||
|
yading@10
|
82 magic[1] != 0x8 ||
|
yading@10
|
83 magic[2] != 0x2 ||
|
yading@10
|
84 magic[3] != 0x2) {
|
yading@10
|
85 av_log(avctx, AV_LOG_ERROR, "Not a BRender PIX file\n");
|
yading@10
|
86 return AVERROR_INVALIDDATA;
|
yading@10
|
87 }
|
yading@10
|
88
|
yading@10
|
89 chunk_type = bytestream2_get_be32(&gb);
|
yading@10
|
90 if (chunk_type != 0x3 && chunk_type != 0x3d) {
|
yading@10
|
91 av_log(avctx, AV_LOG_ERROR, "Invalid chunk type %d\n", chunk_type);
|
yading@10
|
92 return AVERROR_INVALIDDATA;
|
yading@10
|
93 }
|
yading@10
|
94
|
yading@10
|
95 ret = brpix_decode_header(&hdr, &gb);
|
yading@10
|
96 if (!ret) {
|
yading@10
|
97 av_log(avctx, AV_LOG_ERROR, "Invalid header length\n");
|
yading@10
|
98 return AVERROR_INVALIDDATA;
|
yading@10
|
99 }
|
yading@10
|
100 switch (hdr.format) {
|
yading@10
|
101 case 3:
|
yading@10
|
102 avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
yading@10
|
103 bytes_pp = 1;
|
yading@10
|
104 break;
|
yading@10
|
105 case 4:
|
yading@10
|
106 avctx->pix_fmt = AV_PIX_FMT_RGB555BE;
|
yading@10
|
107 bytes_pp = 2;
|
yading@10
|
108 break;
|
yading@10
|
109 case 5:
|
yading@10
|
110 avctx->pix_fmt = AV_PIX_FMT_RGB565BE;
|
yading@10
|
111 bytes_pp = 2;
|
yading@10
|
112 break;
|
yading@10
|
113 case 6:
|
yading@10
|
114 avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
yading@10
|
115 bytes_pp = 3;
|
yading@10
|
116 break;
|
yading@10
|
117 case 7:
|
yading@10
|
118 avctx->pix_fmt = AV_PIX_FMT_0RGB;
|
yading@10
|
119 bytes_pp = 4;
|
yading@10
|
120 break;
|
yading@10
|
121 case 18:
|
yading@10
|
122 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
|
yading@10
|
123 bytes_pp = 2;
|
yading@10
|
124 break;
|
yading@10
|
125 default:
|
yading@10
|
126 av_log(avctx, AV_LOG_ERROR, "Format %d is not supported\n",
|
yading@10
|
127 hdr.format);
|
yading@10
|
128 return AVERROR_PATCHWELCOME;
|
yading@10
|
129 }
|
yading@10
|
130
|
yading@10
|
131 if (av_image_check_size(hdr.width, hdr.height, 0, avctx) < 0)
|
yading@10
|
132 return AVERROR_INVALIDDATA;
|
yading@10
|
133
|
yading@10
|
134 if (hdr.width != avctx->width || hdr.height != avctx->height)
|
yading@10
|
135 avcodec_set_dimensions(avctx, hdr.width, hdr.height);
|
yading@10
|
136
|
yading@10
|
137 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
yading@10
|
138 return ret;
|
yading@10
|
139
|
yading@10
|
140 chunk_type = bytestream2_get_be32(&gb);
|
yading@10
|
141
|
yading@10
|
142 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
|
yading@10
|
143 (chunk_type == 0x3 || chunk_type == 0x3d)) {
|
yading@10
|
144 BRPixHeader palhdr;
|
yading@10
|
145 uint32_t *pal_out = (uint32_t *)frame->data[1];
|
yading@10
|
146 int i;
|
yading@10
|
147
|
yading@10
|
148 ret = brpix_decode_header(&palhdr, &gb);
|
yading@10
|
149 if (!ret) {
|
yading@10
|
150 av_log(avctx, AV_LOG_ERROR, "Invalid palette header length\n");
|
yading@10
|
151 return AVERROR_INVALIDDATA;
|
yading@10
|
152 }
|
yading@10
|
153 if (palhdr.format != 7) {
|
yading@10
|
154 av_log(avctx, AV_LOG_ERROR, "Palette is not in 0RGB format\n");
|
yading@10
|
155 return AVERROR_INVALIDDATA;
|
yading@10
|
156 }
|
yading@10
|
157
|
yading@10
|
158 chunk_type = bytestream2_get_be32(&gb);
|
yading@10
|
159 data_len = bytestream2_get_be32(&gb);
|
yading@10
|
160 bytestream2_skip(&gb, 8);
|
yading@10
|
161 if (chunk_type != 0x21 || data_len != 1032 ||
|
yading@10
|
162 bytestream2_get_bytes_left(&gb) < 1032) {
|
yading@10
|
163 av_log(avctx, AV_LOG_ERROR, "Invalid palette data\n");
|
yading@10
|
164 return AVERROR_INVALIDDATA;
|
yading@10
|
165 }
|
yading@10
|
166 // convert 0RGB to machine endian format (ARGB32)
|
yading@10
|
167 for (i = 0; i < 256; ++i) {
|
yading@10
|
168 bytestream2_skipu(&gb, 1);
|
yading@10
|
169 *pal_out++ = (0xFFU << 24) | bytestream2_get_be24u(&gb);
|
yading@10
|
170 }
|
yading@10
|
171 bytestream2_skip(&gb, 8);
|
yading@10
|
172
|
yading@10
|
173 frame->palette_has_changed = 1;
|
yading@10
|
174
|
yading@10
|
175 chunk_type = bytestream2_get_be32(&gb);
|
yading@10
|
176 } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
yading@10
|
177 uint32_t *pal_out = (uint32_t *)frame->data[1];
|
yading@10
|
178 int i;
|
yading@10
|
179
|
yading@10
|
180 for (i = 0; i < 256; ++i) {
|
yading@10
|
181 *pal_out++ = (0xFFU << 24) | (i * 0x010101);
|
yading@10
|
182 }
|
yading@10
|
183 frame->palette_has_changed = 1;
|
yading@10
|
184 }
|
yading@10
|
185
|
yading@10
|
186 data_len = bytestream2_get_be32(&gb);
|
yading@10
|
187 bytestream2_skip(&gb, 8);
|
yading@10
|
188
|
yading@10
|
189 // read the image data to the buffer
|
yading@10
|
190 {
|
yading@10
|
191 unsigned int bytes_per_scanline = bytes_pp * hdr.width;
|
yading@10
|
192 unsigned int bytes_left = bytestream2_get_bytes_left(&gb);
|
yading@10
|
193
|
yading@10
|
194 if (chunk_type != 0x21 || data_len != bytes_left ||
|
yading@10
|
195 bytes_left / bytes_per_scanline < hdr.height)
|
yading@10
|
196 {
|
yading@10
|
197 av_log(avctx, AV_LOG_ERROR, "Invalid image data\n");
|
yading@10
|
198 return AVERROR_INVALIDDATA;
|
yading@10
|
199 }
|
yading@10
|
200
|
yading@10
|
201 av_image_copy_plane(frame->data[0], frame->linesize[0],
|
yading@10
|
202 avpkt->data + bytestream2_tell(&gb),
|
yading@10
|
203 bytes_per_scanline,
|
yading@10
|
204 bytes_per_scanline, hdr.height);
|
yading@10
|
205 }
|
yading@10
|
206
|
yading@10
|
207 *got_frame = 1;
|
yading@10
|
208
|
yading@10
|
209 return avpkt->size;
|
yading@10
|
210 }
|
yading@10
|
211
|
yading@10
|
212 AVCodec ff_brender_pix_decoder = {
|
yading@10
|
213 .name = "brender_pix",
|
yading@10
|
214 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
215 .id = AV_CODEC_ID_BRENDER_PIX,
|
yading@10
|
216 .decode = brpix_decode_frame,
|
yading@10
|
217 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
218 .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"),
|
yading@10
|
219 };
|