FFmpeg
Loading...
Searching...
No Matches
pgxdec.c
Go to the documentation of this file.
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 "bytestream.h"
24#include "codec_internal.h"
25#include "decode.h"
26
27static int pgx_get_number(AVCodecContext *avctx, GetByteContext *g, int *number) {
28 int ret = AVERROR_INVALIDDATA;
29 char digit;
30
31 *number = 0;
32 while (1) {
33 uint64_t temp;
36 digit = bytestream2_get_byteu(g);
37 if (digit == ' ' || digit == 0xA || digit == 0xD)
38 break;
39 else if (digit < '0' || digit > '9')
41
42 temp = (uint64_t)10 * (*number) + (digit - '0');
43 if (temp > INT_MAX)
45 *number = temp;
46 ret = 0;
47 }
48
49 return ret;
50}
51
53 int *depth, int *width, int *height,
54 int *sign)
55{
56 int byte;
57
60
62
63 // Is the component signed?
64 byte = bytestream2_peek_byteu(g);
65 if (byte == '+') {
66 *sign = 0;
68 } else if (byte == '-') {
69 *sign = 1;
71 }
72
73 byte = bytestream2_peek_byteu(g);
74 if (byte == ' ')
76
77 if (pgx_get_number(avctx, g, depth))
78 goto error;
79 if (pgx_get_number(avctx, g, width))
80 goto error;
81 if (pgx_get_number(avctx, g, height))
82 goto error;
83
84 if (bytestream2_peek_byte(g) == 0xA)
86 return 0;
87
88error:
89 av_log(avctx, AV_LOG_ERROR, "Error in decoding header.\n");
91}
92
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) \
96 { \
97 const unsigned offset = sign ? (1 << (D - 1)) : 0; \
98 int i, j; \
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); \
103 val ^= offset; \
104 *(line + j) = val; \
105 } \
106 } \
107 } \
108
109WRITE_FRAME(8, uint8_t, byte)
110WRITE_FRAME(16, uint16_t, be16)
111
113 int *got_frame, AVPacket *avpkt)
114{
115 int ret;
116 int bpp;
117 int width, height, depth;
118 int sign = 0;
120 bytestream2_init(&g, avpkt->data, avpkt->size);
121
122 if ((ret = pgx_decode_header(avctx, &g, &depth, &width, &height, &sign)) < 0)
123 return ret;
124
125 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
126 return ret;
127
128 if (depth > 0 && depth <= 8) {
129 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
130 bpp = 8;
131 } else if (depth > 0 && depth <= 16) {
132 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
133 bpp = 16;
134 } else {
135 av_log(avctx, AV_LOG_ERROR, "depth %d is invalid or unsupported.\n", depth);
137 }
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)
141 return ret;
142 avctx->bits_per_raw_sample = depth;
143 if (bpp == 8)
144 write_frame_8(p, &g, width, height, sign, depth);
145 else if (bpp == 16)
146 write_frame_16(p, &g, width, height, sign, depth);
147 *got_frame = 1;
148 return 0;
149}
150
152 .p.name = "pgx",
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,
158};
const FFCodec ff_pgx_decoder
Definition pgxdec.c:151
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL be16
Definition bytestream.h:98
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition bytestream.h:99
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_PGX
Definition codec_id.h:244
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static int pgx_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition pgxdec.c:112
static int pgx_decode_header(AVCodecContext *avctx, GetByteContext *g, int *depth, int *width, int *height, int *sign)
Definition pgxdec.c:52
#define WRITE_FRAME(D, PIXEL, suffix)
Definition pgxdec.c:93
static int pgx_get_number(AVCodecContext *avctx, GetByteContext *g, int *number)
Definition pgxdec.c:27
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
#define av_log(a,...)
static void error(const char *err)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
const char * g
Definition vf_curves.c:128
else temp
Definition vf_mcdeint.c:275