2 * Kodak PhotoCD (a.k.a. ImagePac) image decoder
4 * Copyright (c) 1996-2002 Gerd Knorr
5 * Copyright (c) 2010 Kenneth Vermeirsch
6 * Copyright (c) 2020 Paul B Mahol
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Kodak PhotoCD (a.k.a. ImagePac) image decoder
29 * Supports resolutions up to 3072x2048.
32 #define CACHED_BITSTREAM_READER !ARCH_X86_32
34 #include "libavutil/avassert.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/opt.h"
38 #include "bytestream.h"
43 typedef struct PhotoCDContext {
48 int thumbnails; //* number of thumbnails; 0 for normal image */
61 typedef struct ImageInfo {
63 uint16_t width, height;
66 static const ImageInfo img_info[6] = {
75 static av_noinline void interp_lowres(PhotoCDContext *s, AVFrame *picture,
76 int width, int height)
78 GetByteContext *gb = &s->gb;
79 int start = s->streampos + img_info[2].start;
80 uint8_t *ptr, *ptr1, *ptr2;
84 ptr = picture->data[0];
85 ptr1 = picture->data[1];
86 ptr2 = picture->data[2];
88 bytestream2_seek(gb, start, SEEK_SET);
90 for (int y = 0; y < height; y += 2) {
92 for (int x = 0; x < width - 1; x++) {
93 fill = bytestream2_get_byte(gb);
95 *(dst++) = (fill + bytestream2_peek_byte(gb) + 1) >> 1;
97 fill = bytestream2_get_byte(gb);
101 ptr += picture->linesize[0] << 1;
104 for (int x = 0; x < width - 1; x++) {
105 fill = bytestream2_get_byte(gb);
107 *(dst++) = (fill + bytestream2_peek_byte(gb) + 1) >> 1;
109 fill = bytestream2_get_byte(gb);
113 ptr += picture->linesize[0] << 1;
116 for (int x = 0; x < (width >> 1) - 1; x++) {
117 fill = bytestream2_get_byte(gb);
119 *(dst++) = (fill + bytestream2_peek_byte(gb) + 1) >> 1;
121 fill = bytestream2_get_byte(gb);
125 ptr1 += picture->linesize[1] << 1;
128 for (int x = 0; x < (width >> 1) - 1; x++) {
129 fill = bytestream2_get_byte(gb);
131 *(dst++) = (fill + bytestream2_peek_byte(gb) + 1) >> 1;
133 fill = bytestream2_get_byte(gb);
137 ptr2 += picture->linesize[2] << 1;
140 s->streampos += bytestream2_tell(gb) - start;
143 static av_noinline void interp_lines(uint8_t *ptr, int linesize,
144 int width, int height)
150 for (int y = 0; y < height - 2; y += 2) {
151 const uint8_t *src1 = ptr;
152 uint8_t *dst = ptr + linesize;
153 const uint8_t *src2 = dst + linesize;
154 for (x = 0; x < width - 2; x += 2) {
155 dst[x] = (src1[x] + src2[x] + 1) >> 1;
156 dst[x + 1] = (src1[x] + src2[x] + src1[x + 2] + src2[x + 2] + 2) >> 2;
158 dst[x] = dst[x + 1] = (src1[x] + src2[x] + 1) >> 1;
160 ptr += linesize << 1;
164 dst = ptr + linesize;
165 for (x = 0; x < width - 2; x += 2) {
167 dst[x + 1] = (src1[x] + src1[x + 2] + 1) >> 1;
169 dst[x] = dst[x + 1] = src1[x];
172 static av_noinline void interp_pixels(uint8_t *ptr, int linesize,
173 int width, int height)
175 for (int y = height - 2; y >= 0; y -= 2) {
176 const uint8_t *src = ptr + (y >> 1) * linesize;
177 uint8_t *dst = ptr + y * linesize;
179 dst[width - 2] = dst[width - 1] = src[(width >> 1) - 1];
180 for (int x = width - 4; x >= 0; x -= 2) {
181 dst[x] = src[x >> 1];
182 dst[x + 1] = (src[x >> 1] + src[(x >> 1) + 1] + 1) >> 1;
187 static av_noinline int read_hufftable(AVCodecContext *avctx, VLC *vlc)
189 PhotoCDContext *s = avctx->priv_data;
190 GetByteContext *gb = &s->gb;
191 int start = s->streampos;
194 bytestream2_seek(gb, start, SEEK_SET);
196 count = bytestream2_get_byte(gb) + 1;
197 if (bytestream2_get_bytes_left(gb) < count * 4)
198 return AVERROR_INVALIDDATA;
200 for (int j = 0; j < count; j++) {
201 const int bit = bytestream2_get_byteu(gb) + 1;
202 const int code = bytestream2_get_be16u(gb);
203 const int sym = bytestream2_get_byteu(gb);
206 return AVERROR_INVALIDDATA;
209 s->codes[j] = code >> (16 - bit);
214 ret = ff_init_vlc_sparse(vlc, 12, count,
215 s->bits, sizeof(*s->bits), sizeof(*s->bits),
216 s->codes, sizeof(*s->codes), sizeof(*s->codes),
217 s->syms, sizeof(*s->syms), sizeof(*s->syms), 0);
219 s->streampos = bytestream2_tell(gb);
224 static av_noinline int decode_huff(AVCodecContext *avctx, AVFrame *frame,
225 int target_res, int curr_res)
227 PhotoCDContext *s = avctx->priv_data;
229 GetByteContext *gb = &s->gb;
230 int ret, y = 0, type, height;
231 int start = s->streampos;
233 const int scaling = target_res - curr_res;
234 const uint8_t type2idx[] = { 0, 0xff, 1, 2 };
236 bytestream2_seek(gb, start, SEEK_SET);
237 ret = init_get_bits8(&g, gb->buffer, bytestream2_get_bytes_left(gb));
241 height = img_info[curr_res].height;
247 for (; get_bits_left(&g) > 0;) {
248 if (show_bits(&g, 12) == 0xfff)
253 shiftreg = show_bits(&g, 24);
254 while (shiftreg != 0xfffffe) {
255 if (get_bits_left(&g) <= 0)
256 return AVERROR_INVALIDDATA;
258 shiftreg = show_bits(&g, 24);
261 y = show_bits(&g, 15) & 0x1fff;
264 type = get_bits(&g, 2);
268 return AVERROR_INVALIDDATA;
269 idx = type2idx[type];
271 data = frame->data[idx] + (y >> !!idx) * frame->linesize[idx];
273 x2 = avctx->width >> (scaling + !!idx);
274 for (int x = 0; x < x2; x++) {
277 if (get_bits_left(&g) <= 0)
278 return AVERROR_INVALIDDATA;
279 m = get_vlc2(&g, s->vlc[idx].table, s->vlc[idx].bits, 2);
281 return AVERROR_INVALIDDATA;
282 m = sign_extend(m, 8);
283 data[x] = av_clip_uint8(data[x] + m);
287 s->streampos += (get_bits_count(&g) + 7) >> 3;
288 s->streampos = (s->streampos + 0x6000 + 2047) & ~0x7ff;
293 static int photocd_decode_frame(AVCodecContext *avctx, void *data,
294 int *got_frame, AVPacket *avpkt)
296 PhotoCDContext *s = avctx->priv_data;
297 ThreadFrame frame = { .f = data };
298 const uint8_t *buf = avpkt->data;
299 GetByteContext *gb = &s->gb;
301 uint8_t *ptr, *ptr1, *ptr2;
304 if (avpkt->size < img_info[0].start)
305 return AVERROR_INVALIDDATA;
307 if (!memcmp("PCD_OPA", buf, 7)) {
308 s->thumbnails = AV_RL16(buf + 10);
309 av_log(avctx, AV_LOG_WARNING, "this is a thumbnails file, "
310 "reading first thumbnail only\n");
311 } else if (avpkt->size < 786432) {
312 return AVERROR_INVALIDDATA;
313 } else if (memcmp("PCD_IPI", buf + 0x800, 7)) {
314 return AVERROR_INVALIDDATA;
317 s->orientation = s->thumbnails ? buf[12] & 3 : buf[0x48] & 3;
321 else if (avpkt->size <= 788480)
324 s->resolution = av_clip(4 - s->lowres, 0, 4);
326 ret = ff_set_dimensions(avctx, img_info[s->resolution].width, img_info[s->resolution].height);
330 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
333 p->pict_type = AV_PICTURE_TYPE_I;
336 bytestream2_init(gb, avpkt->data, avpkt->size);
338 if (s->resolution < 3) {
344 bytestream2_seek(gb, 10240, SEEK_SET);
346 bytestream2_seek(gb, img_info[s->resolution].start, SEEK_SET);
348 for (int y = 0; y < avctx->height; y += 2) {
349 bytestream2_get_buffer(gb, ptr, avctx->width);
350 ptr += p->linesize[0];
352 bytestream2_get_buffer(gb, ptr, avctx->width);
353 ptr += p->linesize[0];
355 bytestream2_get_buffer(gb, ptr1, avctx->width >> 1);
356 ptr1 += p->linesize[1];
358 bytestream2_get_buffer(gb, ptr2, avctx->width >> 1);
359 ptr2 += p->linesize[2];
367 interp_lowres(s, p, img_info[2].width, img_info[2].height);
369 interp_lines(ptr1, p->linesize[1], img_info[2].width, img_info[2].height);
370 interp_lines(ptr2, p->linesize[2], img_info[2].width, img_info[2].height);
372 if (s->resolution == 4) {
373 interp_pixels(ptr1, p->linesize[1], img_info[3].width, img_info[3].height);
374 interp_lines (ptr1, p->linesize[1], img_info[3].width, img_info[3].height);
375 interp_pixels(ptr2, p->linesize[2], img_info[3].width, img_info[3].height);
376 interp_lines (ptr2, p->linesize[2], img_info[3].width, img_info[3].height);
379 interp_lines(ptr, p->linesize[0], img_info[3].width, img_info[3].height);
381 s->streampos = 0xc2000;
382 for (int n = 0; n < 3; n++) {
383 if ((ret = read_hufftable(avctx, &s->vlc[n])) < 0)
386 s->streampos = (s->streampos + 2047) & ~0x3ff;
387 if (decode_huff(avctx, p, s->resolution, 3) < 0)
388 return AVERROR_INVALIDDATA;
390 if (s->resolution == 4) {
391 interp_pixels(ptr, p->linesize[0], img_info[4].width, img_info[4].height);
392 interp_lines (ptr, p->linesize[0], img_info[4].width, img_info[4].height);
394 for (int n = 0; n < 3; n++) {
395 if ((ret = read_hufftable(avctx, &s->vlc[n])) < 0)
398 s->streampos = (s->streampos + 2047) & ~0x3ff;
399 if (decode_huff(avctx, p, 4, 4) < 0)
400 return AVERROR_INVALIDDATA;
408 for (int y = 0; y < avctx->height >> 1; y++) {
409 for (int x = 0; x < avctx->width >> 1; x++) {
410 ptr1[x] = av_clip_uint8(ptr1[x] - 28);
411 ptr2[x] = av_clip_uint8(ptr2[x] - 9);
414 ptr1 += p->linesize[1];
415 ptr2 += p->linesize[2];
424 static av_cold int photocd_decode_init(AVCodecContext *avctx)
426 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
427 avctx->colorspace = AVCOL_SPC_BT709;
428 avctx->color_primaries = AVCOL_PRI_BT709;
429 avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
430 avctx->color_range = AVCOL_RANGE_JPEG;
435 static av_cold int photocd_decode_close(AVCodecContext *avctx)
437 PhotoCDContext *s = avctx->priv_data;
439 for (int i = 0; i < 3; i++)
440 ff_free_vlc(&s->vlc[i]);
445 #define OFFSET(x) offsetof(PhotoCDContext, x)
446 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
448 static const AVOption options[] = {
449 { "lowres", "Lower the decoding resolution by a power of two",
450 OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 4, VD },
454 static const AVClass photocd_class = {
455 .class_name = "photocd",
456 .item_name = av_default_item_name,
458 .version = LIBAVUTIL_VERSION_INT,
461 AVCodec ff_photocd_decoder = {
463 .type = AVMEDIA_TYPE_VIDEO,
464 .id = AV_CODEC_ID_PHOTOCD,
465 .priv_data_size = sizeof(PhotoCDContext),
466 .priv_class = &photocd_class,
467 .init = photocd_decode_init,
468 .close = photocd_decode_close,
469 .decode = photocd_decode_frame,
470 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
471 .long_name = NULL_IF_CONFIG_SMALL("Kodak Photo CD"),
472 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,