Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 1 | /* |
| 2 | * Quicktime Planar RGB (8BPS) Video Decoder |
| 3 | * Copyright (C) 2003 Roberto Togni |
| 4 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 10 | * version 2.1 of the License, or (at your option) any later version. |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 11 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 12 | * FFmpeg is distributed in the hope that it will be useful, |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 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. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 18 | * License along with FFmpeg; if not, write to the Free Software |
Diego Biurrun | 5509bff | 2006-01-12 22:43:26 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 23 | * @file |
Roberto Togni | 0f6feb5 | 2008-06-07 13:14:49 | [diff] [blame] | 24 | * QT 8BPS Video Decoder by Roberto Togni |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 25 | * For more information about the 8BPS format, visit: |
| 26 | * http://www.pcisys.net/~melanson/codecs/ |
| 27 | * |
| 28 | * Supports: PAL8 (RGB 8bpp, paletted) |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 29 | * : GBRP (RGB 24bpp) |
| 30 | * : GBRAP (RGB 32bpp, 4th plane is alpha) |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 31 | */ |
| 32 | |
Martin Storsjö | 1d9c2dc | 2012-08-06 13:49:32 | [diff] [blame] | 33 | #include <string.h> |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 34 | |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 35 | #include "libavutil/intreadwrite.h" |
Martin Storsjö | 1d9c2dc | 2012-08-06 13:49:32 | [diff] [blame] | 36 | #include "libavutil/internal.h" |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 37 | #include "avcodec.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 38 | #include "codec_internal.h" |
Andreas Rheinhardt | 7b10083 | 2021-03-17 21:43:32 | [diff] [blame] | 39 | #include "decode.h" |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 40 | |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 41 | typedef struct EightBpsContext { |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 42 | AVCodecContext *avctx; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 43 | |
Paul B Mahol | 1ce1970 | 2023-09-10 20:19:19 | [diff] [blame] | 44 | uint8_t planes; |
| 45 | uint8_t planemap[4]; |
Anton Khirnov | 6dbde68 | 2023-10-24 13:03:51 | [diff] [blame^] | 46 | |
| 47 | uint32_t pal[256]; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 48 | } EightBpsContext; |
| 49 | |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 50 | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
Anton Khirnov | df9b956 | 2012-11-13 18:35:22 | [diff] [blame] | 51 | int *got_frame, AVPacket *avpkt) |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 52 | { |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 53 | const uint8_t *buf = avpkt->data; |
| 54 | int buf_size = avpkt->size; |
| 55 | EightBpsContext * const c = avctx->priv_data; |
Paul B Mahol | 1ce1970 | 2023-09-10 20:19:19 | [diff] [blame] | 56 | const uint8_t *encoded = buf; |
| 57 | uint8_t *pixptr, *pixptr_end; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 58 | unsigned int height = avctx->height; // Real image height |
| 59 | unsigned int dlen, p, row; |
Paul B Mahol | 1ce1970 | 2023-09-10 20:19:19 | [diff] [blame] | 60 | const uint8_t *lp, *dp, *ep; |
| 61 | uint8_t count; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 62 | const uint8_t *planemap = c->planemap; |
| 63 | unsigned int planes = c->planes; |
Anton Khirnov | 313da47 | 2012-11-14 07:59:03 | [diff] [blame] | 64 | int ret; |
Diego Biurrun | 115329f | 2005-12-17 18:14:38 | [diff] [blame] | 65 | |
Paul B Mahol | 05066cb | 2022-09-17 08:32:16 | [diff] [blame] | 66 | if (buf_size < planes * height * 2) |
Michael Niedermayer | 2316d5e | 2022-08-22 20:10:09 | [diff] [blame] | 67 | return AVERROR_INVALIDDATA; |
| 68 | |
Clément Bœsch | 1ec94b0 | 2013-03-12 07:41:53 | [diff] [blame] | 69 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
Anton Khirnov | 313da47 | 2012-11-14 07:59:03 | [diff] [blame] | 70 | return ret; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 71 | |
Luca Barbato | bd7b4da | 2013-07-22 21:26:05 | [diff] [blame] | 72 | ep = encoded + buf_size; |
| 73 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 74 | /* Set data pointer after line lengths */ |
| 75 | dp = encoded + planes * (height << 1); |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 76 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 77 | for (p = 0; p < planes; p++) { |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 78 | const int pi = planemap[p]; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 79 | /* Lines length pointer for this plane */ |
| 80 | lp = encoded + p * (height << 1); |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 81 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 82 | /* Decode a plane */ |
| 83 | for (row = 0; row < height; row++) { |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 84 | pixptr = frame->data[pi] + row * frame->linesize[pi]; |
| 85 | pixptr_end = pixptr + frame->linesize[pi]; |
Luca Barbato | bd7b4da | 2013-07-22 21:26:05 | [diff] [blame] | 86 | if (ep - lp < row * 2 + 2) |
| 87 | return AVERROR_INVALIDDATA; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 88 | dlen = AV_RB16(lp + row * 2); |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 89 | /* Decode a row of this plane */ |
| 90 | while (dlen > 0) { |
Luca Barbato | bd7b4da | 2013-07-22 21:26:05 | [diff] [blame] | 91 | if (ep - dp <= 1) |
Anton Khirnov | 313da47 | 2012-11-14 07:59:03 | [diff] [blame] | 92 | return AVERROR_INVALIDDATA; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 93 | if ((count = *dp++) <= 127) { |
| 94 | count++; |
| 95 | dlen -= count + 1; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 96 | if (pixptr_end - pixptr < count) |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 97 | break; |
Luca Barbato | bd7b4da | 2013-07-22 21:26:05 | [diff] [blame] | 98 | if (ep - dp < count) |
Anton Khirnov | 313da47 | 2012-11-14 07:59:03 | [diff] [blame] | 99 | return AVERROR_INVALIDDATA; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 100 | memcpy(pixptr, dp, count); |
| 101 | pixptr += count; |
| 102 | dp += count; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 103 | } else { |
| 104 | count = 257 - count; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 105 | if (pixptr_end - pixptr < count) |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 106 | break; |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 107 | memset(pixptr, dp[0], count); |
| 108 | pixptr += count; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 109 | dp++; |
| 110 | dlen -= 2; |
Diego Biurrun | bb270c0 | 2005-12-22 01:10:11 | [diff] [blame] | 111 | } |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Carl Eugen Hoyos | 54bbe3e | 2014-03-11 19:09:07 | [diff] [blame] | 116 | if (avctx->bits_per_coded_sample <= 8) { |
James Almer | 63767b7 | 2023-05-18 01:39:57 | [diff] [blame] | 117 | #if FF_API_PALETTE_HAS_CHANGED |
| 118 | FF_DISABLE_DEPRECATION_WARNINGS |
| 119 | frame->palette_has_changed = |
| 120 | #endif |
Anton Khirnov | 6dbde68 | 2023-10-24 13:03:51 | [diff] [blame^] | 121 | ff_copy_palette(c->pal, avpkt, avctx); |
James Almer | 63767b7 | 2023-05-18 01:39:57 | [diff] [blame] | 122 | #if FF_API_PALETTE_HAS_CHANGED |
| 123 | FF_ENABLE_DEPRECATION_WARNINGS |
| 124 | #endif |
Anton Khirnov | 6dbde68 | 2023-10-24 13:03:51 | [diff] [blame^] | 125 | |
| 126 | memcpy(frame->data[1], c->pal, AVPALETTE_SIZE); |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 127 | } |
Kostya Shishkov | 2d8591c | 2011-04-09 13:49:51 | [diff] [blame] | 128 | |
Anton Khirnov | df9b956 | 2012-11-13 18:35:22 | [diff] [blame] | 129 | *got_frame = 1; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 130 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 131 | /* always report that the buffer was completely consumed */ |
| 132 | return buf_size; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 133 | } |
| 134 | |
Zuxy Meng | 98a6fff | 2008-03-21 03:11:20 | [diff] [blame] | 135 | static av_cold int decode_init(AVCodecContext *avctx) |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 136 | { |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 137 | EightBpsContext * const c = avctx->priv_data; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 138 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 139 | c->avctx = avctx; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 140 | |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 141 | switch (avctx->bits_per_coded_sample) { |
| 142 | case 8: |
Anton Khirnov | 716d413 | 2012-10-06 10:10:34 | [diff] [blame] | 143 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 144 | c->planes = 1; |
| 145 | c->planemap[0] = 0; // 1st plane is palette indexes |
| 146 | break; |
| 147 | case 24: |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 148 | avctx->pix_fmt = AV_PIX_FMT_GBRP; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 149 | c->planes = 3; |
| 150 | c->planemap[0] = 2; // 1st plane is red |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 151 | c->planemap[1] = 0; // 2nd plane is green |
| 152 | c->planemap[2] = 1; // 3rd plane is blue |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 153 | break; |
| 154 | case 32: |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 155 | avctx->pix_fmt = AV_PIX_FMT_GBRAP; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 156 | c->planes = 4; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", |
| 160 | avctx->bits_per_coded_sample); |
Anton Khirnov | 313da47 | 2012-11-14 07:59:03 | [diff] [blame] | 161 | return AVERROR_INVALIDDATA; |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 162 | } |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 163 | |
Paul B Mahol | 1735ca3 | 2023-09-10 20:42:11 | [diff] [blame] | 164 | if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) { |
| 165 | c->planemap[0] = 2; // 1st plane is red |
| 166 | c->planemap[1] = 0; // 2nd plane is green |
| 167 | c->planemap[2] = 1; // 3rd plane is blue |
| 168 | c->planemap[3] = 3; // 4th plane is alpha |
Janne Grunau | e8c0def | 2013-07-19 11:51:51 | [diff] [blame] | 169 | } |
Paul B Mahol | 324e818 | 2012-01-26 19:21:15 | [diff] [blame] | 170 | return 0; |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 171 | } |
| 172 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 173 | const FFCodec ff_eightbps_decoder = { |
| 174 | .p.name = "8bps", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 175 | CODEC_LONG_NAME("QuickTime 8BPS video"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 176 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 177 | .p.id = AV_CODEC_ID_8BPS, |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 178 | .priv_data_size = sizeof(EightBpsContext), |
| 179 | .init = decode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 180 | FF_CODEC_DECODE_CB(decode_frame), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 181 | .p.capabilities = AV_CODEC_CAP_DR1, |
Roberto Togni | 1dc1ed9 | 2003-11-07 22:39:18 | [diff] [blame] | 182 | }; |