xref: /third_party/ffmpeg/libavcodec/pgxdec.c (revision cabdff1a)
1/*
2 * PGX image format
3 * Copyright (c) 2020 Gautam Ramakrishnan
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "avcodec.h"
23#include "internal.h"
24#include "bytestream.h"
25#include "codec_internal.h"
26#include "libavutil/imgutils.h"
27
28static int pgx_get_number(AVCodecContext *avctx, GetByteContext *g, int *number) {
29    int ret = AVERROR_INVALIDDATA;
30    char digit;
31
32    *number = 0;
33    while (1) {
34        uint64_t temp;
35        if (bytestream2_get_bytes_left(g) <= 0)
36            return AVERROR_INVALIDDATA;
37        digit = bytestream2_get_byteu(g);
38        if (digit == ' ' || digit == 0xA || digit == 0xD)
39            break;
40        else if (digit < '0' || digit > '9')
41            return AVERROR_INVALIDDATA;
42
43        temp = (uint64_t)10 * (*number) + (digit - '0');
44        if (temp > INT_MAX)
45            return AVERROR_INVALIDDATA;
46        *number = temp;
47        ret = 0;
48    }
49
50    return ret;
51}
52
53static int pgx_decode_header(AVCodecContext *avctx, GetByteContext *g,
54                             int *depth, int *width, int *height,
55                             int *sign)
56{
57    int byte;
58
59    if (bytestream2_get_bytes_left(g) < 12)
60        return AVERROR_INVALIDDATA;
61
62    bytestream2_skipu(g, 6);
63
64    // Is the component signed?
65    byte = bytestream2_peek_byteu(g);
66    if (byte == '+') {
67        *sign = 0;
68        bytestream2_skipu(g, 1);
69    } else if (byte == '-') {
70        *sign = 1;
71        bytestream2_skipu(g, 1);
72    }
73
74    byte = bytestream2_peek_byteu(g);
75    if (byte == ' ')
76        bytestream2_skipu(g, 1);
77
78    if (pgx_get_number(avctx, g, depth))
79        goto error;
80    if (pgx_get_number(avctx, g, width))
81        goto error;
82    if (pgx_get_number(avctx, g, height))
83        goto error;
84
85    if (bytestream2_peek_byte(g) == 0xA)
86        bytestream2_skip(g, 1);
87    return 0;
88
89error:
90    av_log(avctx, AV_LOG_ERROR, "Error in decoding header.\n");
91    return AVERROR_INVALIDDATA;
92}
93
94#define WRITE_FRAME(D, PIXEL, suffix)                                                       \
95    static inline void write_frame_ ##D(AVFrame *frame, GetByteContext *g, \
96                                        int width, int height, int sign, int depth)         \
97    {                                                                                       \
98        const unsigned offset = sign ? (1 << (D - 1)) : 0;                                  \
99        int i, j;                                                                           \
100        for (i = 0; i < height; i++) {                                                      \
101            PIXEL *line = (PIXEL*)(frame->data[0] + i * frame->linesize[0]);                \
102            for (j = 0; j < width; j++) {                                                   \
103                unsigned val = bytestream2_get_ ##suffix##u(g) << (D - depth);              \
104                val ^= offset;                                                              \
105                *(line + j) = val;                                                          \
106            }                                                                               \
107        }                                                                                   \
108    }                                                                                       \
109
110WRITE_FRAME(8, uint8_t, byte)
111WRITE_FRAME(16, uint16_t, be16)
112
113static int pgx_decode_frame(AVCodecContext *avctx, AVFrame *p,
114                            int *got_frame, AVPacket *avpkt)
115{
116    int ret;
117    int bpp;
118    int width, height, depth;
119    int sign = 0;
120    GetByteContext g;
121    bytestream2_init(&g, avpkt->data, avpkt->size);
122
123    if ((ret = pgx_decode_header(avctx, &g, &depth, &width, &height, &sign)) < 0)
124        return ret;
125
126    if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
127        return ret;
128
129    if (depth > 0 && depth <= 8) {
130        avctx->pix_fmt = AV_PIX_FMT_GRAY8;
131        bpp = 8;
132    } else if (depth > 0 && depth <= 16) {
133        avctx->pix_fmt = AV_PIX_FMT_GRAY16;
134        bpp = 16;
135    } else {
136        av_log(avctx, AV_LOG_ERROR, "depth %d is invalid or unsupported.\n", depth);
137        return AVERROR_PATCHWELCOME;
138    }
139    if (bytestream2_get_bytes_left(&g) < width * height * (bpp >> 3))
140        return AVERROR_INVALIDDATA;
141    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
142        return ret;
143    p->pict_type = AV_PICTURE_TYPE_I;
144    p->key_frame = 1;
145    avctx->bits_per_raw_sample = depth;
146    if (bpp == 8)
147        write_frame_8(p, &g, width, height, sign, depth);
148    else if (bpp == 16)
149        write_frame_16(p, &g, width, height, sign, depth);
150    *got_frame = 1;
151    return 0;
152}
153
154const FFCodec ff_pgx_decoder = {
155    .p.name         = "pgx",
156    .p.long_name    = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
157    .p.type         = AVMEDIA_TYPE_VIDEO,
158    .p.id           = AV_CODEC_ID_PGX,
159    .p.capabilities = AV_CODEC_CAP_DR1,
160    FF_CODEC_DECODE_CB(pgx_decode_frame),
161};
162