3 * Copyright (c) 2020 Gautam Ramakrishnan
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 #include "bytestream.h"
24 #include "codec_internal.h"
27 static int pgx_get_number(AVCodecContext
*avctx
, GetByteContext
*g
, int *number
) {
28 int ret
= AVERROR_INVALIDDATA
;
34 if (bytestream2_get_bytes_left(g
) <= 0)
35 return AVERROR_INVALIDDATA
;
36 digit
= bytestream2_get_byteu(g
);
37 if (digit
== ' ' || digit
== 0xA || digit
== 0xD)
39 else if (digit
< '0' || digit
> '9')
40 return AVERROR_INVALIDDATA
;
42 temp
= (uint64_t)10 * (*number
) + (digit
- '0');
44 return AVERROR_INVALIDDATA
;
52 static int pgx_decode_header(AVCodecContext
*avctx
, GetByteContext
*g
,
53 int *depth
, int *width
, int *height
,
58 if (bytestream2_get_bytes_left(g
) < 12)
59 return AVERROR_INVALIDDATA
;
61 bytestream2_skipu(g
, 6);
63 // Is the component signed?
64 byte
= bytestream2_peek_byteu(g
);
67 bytestream2_skipu(g
, 1);
68 } else if (byte
== '-') {
70 bytestream2_skipu(g
, 1);
73 byte
= bytestream2_peek_byteu(g
);
75 bytestream2_skipu(g
, 1);
77 if (pgx_get_number(avctx
, g
, depth
))
79 if (pgx_get_number(avctx
, g
, width
))
81 if (pgx_get_number(avctx
, g
, height
))
84 if (bytestream2_peek_byte(g
) == 0xA)
85 bytestream2_skip(g
, 1);
89 av_log(avctx
, AV_LOG_ERROR
, "Error in decoding header.\n");
90 return AVERROR_INVALIDDATA
;
93 #define WRITE_FRAME(D, PIXEL, suffix) \
94 static inline void write_frame_ ##D(AVFrame *frame, GetByteContext *g, \
95 int width, int height, int sign, int depth) \
97 const unsigned offset = sign ? (1 << (D - 1)) : 0; \
99 for (i = 0; i < height; i++) { \
100 PIXEL *line = (PIXEL*)(frame->data[0] + i * frame->linesize[0]); \
101 for (j = 0; j < width; j++) { \
102 unsigned val = bytestream2_get_ ##suffix##u(g) << (D - depth); \
109 WRITE_FRAME(8, uint8_t, byte)
110 WRITE_FRAME(16, uint16_t, be16
)
112 static int pgx_decode_frame(AVCodecContext
*avctx
, AVFrame
*p
,
113 int *got_frame
, AVPacket
*avpkt
)
117 int width
, height
, depth
;
120 bytestream2_init(&g
, avpkt
->data
, avpkt
->size
);
122 if ((ret
= pgx_decode_header(avctx
, &g
, &depth
, &width
, &height
, &sign
)) < 0)
125 if ((ret
= ff_set_dimensions(avctx
, width
, height
)) < 0)
128 if (depth
> 0 && depth
<= 8) {
129 avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
131 } else if (depth
> 0 && depth
<= 16) {
132 avctx
->pix_fmt
= AV_PIX_FMT_GRAY16
;
135 av_log(avctx
, AV_LOG_ERROR
, "depth %d is invalid or unsupported.\n", depth
);
136 return AVERROR_PATCHWELCOME
;
138 if (bytestream2_get_bytes_left(&g
) < width
* height
* (bpp
>> 3))
139 return AVERROR_INVALIDDATA
;
140 if ((ret
= ff_get_buffer(avctx
, p
, 0)) < 0)
142 avctx
->bits_per_raw_sample
= depth
;
144 write_frame_8(p
, &g
, width
, height
, sign
, depth
);
146 write_frame_16(p
, &g
, width
, height
, sign
, depth
);
151 const FFCodec ff_pgx_decoder
= {
153 CODEC_LONG_NAME("PGX (JPEG2000 Test Format)"),
154 .p
.type
= AVMEDIA_TYPE_VIDEO
,
155 .p
.id
= AV_CODEC_ID_PGX
,
156 .p
.capabilities
= AV_CODEC_CAP_DR1
,
157 FF_CODEC_DECODE_CB(pgx_decode_frame
),