3 * Copyright (c) 2002, 2003 Fabrice Bellard
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
27 static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
33 for (i=0; i<n/2; i++) {
34 ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
39 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
40 int *got_frame, AVPacket *avpkt)
42 const uint8_t *buf = avpkt->data;
43 int buf_size = avpkt->size;
44 PNMContext * const s = avctx->priv_data;
45 AVFrame * const p = data;
46 int i, j, k, n, linesize, h, upgrade = 0, is_mono = 0;
48 int components, sample_len, ret;
52 s->bytestream = (uint8_t *)buf;
53 s->bytestream_end = (uint8_t *)buf + buf_size;
55 if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
58 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
60 p->pict_type = AV_PICTURE_TYPE_I;
62 avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
64 switch (avctx->pix_fmt) {
66 return AVERROR(EINVAL);
67 case AV_PIX_FMT_RGBA64:
71 if (s->maxval < 65535)
74 case AV_PIX_FMT_RGB48:
78 if (s->maxval < 65535)
86 case AV_PIX_FMT_RGB24:
93 case AV_PIX_FMT_GRAY8:
100 case AV_PIX_FMT_GRAY8A:
101 n = avctx->width * 2;
105 case AV_PIX_FMT_GRAY16:
106 n = avctx->width * 2;
109 if (s->maxval < 65535)
112 case AV_PIX_FMT_YA16:
113 n = avctx->width * 4;
116 if (s->maxval < 65535)
119 case AV_PIX_FMT_MONOWHITE:
120 case AV_PIX_FMT_MONOBLACK:
121 n = (avctx->width + 7) >> 3;
127 linesize = p->linesize[0];
128 if (n * avctx->height > s->bytestream_end - s->bytestream)
129 return AVERROR_INVALIDDATA;
130 if(s->type < 4 || (is_mono && s->type==7)){
131 for (i=0; i<avctx->height; i++) {
133 init_put_bits(&pb, ptr, linesize);
134 for(j=0; j<avctx->width * components; j++){
138 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
140 if(s->bytestream >= s->bytestream_end)
141 return AVERROR_INVALIDDATA;
143 /* read a single digit */
144 v = (*s->bytestream++)&1;
146 /* read a sequence of digits */
147 for (k = 0; k < 6 && c <= 9; k += 1) {
149 c = (*s->bytestream++) - '0';
152 av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
153 return AVERROR_INVALIDDATA;
156 if (sample_len == 16) {
157 ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
159 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
161 if (sample_len != 16)
166 for (i = 0; i < avctx->height; i++) {
168 samplecpy(ptr, s->bytestream, n, s->maxval);
169 else if (upgrade == 1) {
170 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
171 for (j = 0; j < n; j++)
172 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
173 } else if (upgrade == 2) {
174 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
175 for (j = 0; j < n / 2; j++) {
176 v = AV_RB16(s->bytestream + 2*j);
177 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
185 case AV_PIX_FMT_YUV420P:
186 case AV_PIX_FMT_YUV420P9:
187 case AV_PIX_FMT_YUV420P10:
189 unsigned char *ptr1, *ptr2;
193 linesize = p->linesize[0];
194 if (s->maxval >= 256)
196 if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
197 return AVERROR_INVALIDDATA;
198 for (i = 0; i < avctx->height; i++) {
199 samplecpy(ptr, s->bytestream, n, s->maxval);
206 h = avctx->height >> 1;
207 for (i = 0; i < h; i++) {
208 samplecpy(ptr1, s->bytestream, n, s->maxval);
210 samplecpy(ptr2, s->bytestream, n, s->maxval);
212 ptr1 += p->linesize[1];
213 ptr2 += p->linesize[2];
217 case AV_PIX_FMT_YUV420P16:
219 uint16_t *ptr1, *ptr2;
220 const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
223 n = avctx->width * 2;
225 linesize = p->linesize[0];
226 if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
227 return AVERROR_INVALIDDATA;
228 for (i = 0; i < avctx->height; i++) {
229 for (j = 0; j < n / 2; j++) {
230 v = AV_RB16(s->bytestream + 2*j);
231 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
236 ptr1 = (uint16_t*)p->data[1];
237 ptr2 = (uint16_t*)p->data[2];
239 h = avctx->height >> 1;
240 for (i = 0; i < h; i++) {
241 for (j = 0; j < n / 2; j++) {
242 v = AV_RB16(s->bytestream + 2*j);
243 ptr1[j] = (v * f + 16384) >> 15;
247 for (j = 0; j < n / 2; j++) {
248 v = AV_RB16(s->bytestream + 2*j);
249 ptr2[j] = (v * f + 16384) >> 15;
253 ptr1 += p->linesize[1] / 2;
254 ptr2 += p->linesize[2] / 2;
258 case AV_PIX_FMT_GBRPF32:
259 if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
260 return AVERROR_INVALIDDATA;
261 scale = 1.f / s->scale;
265 r = (float *)p->data[2];
266 g = (float *)p->data[0];
267 b = (float *)p->data[1];
268 for (int i = 0; i < avctx->height; i++) {
269 for (int j = 0; j < avctx->width; j++) {
270 r[j] = av_int2float(AV_RL32(s->bytestream+0)) * scale;
271 g[j] = av_int2float(AV_RL32(s->bytestream+4)) * scale;
272 b[j] = av_int2float(AV_RL32(s->bytestream+8)) * scale;
276 r += p->linesize[2] / 4;
277 g += p->linesize[0] / 4;
278 b += p->linesize[1] / 4;
283 r = (float *)p->data[2];
284 g = (float *)p->data[0];
285 b = (float *)p->data[1];
286 for (int i = 0; i < avctx->height; i++) {
287 for (int j = 0; j < avctx->width; j++) {
288 r[j] = av_int2float(AV_RB32(s->bytestream+0)) * scale;
289 g[j] = av_int2float(AV_RB32(s->bytestream+4)) * scale;
290 b[j] = av_int2float(AV_RB32(s->bytestream+8)) * scale;
294 r += p->linesize[2] / 4;
295 g += p->linesize[0] / 4;
296 b += p->linesize[1] / 4;
303 return s->bytestream - s->bytestream_start;
307 #if CONFIG_PGM_DECODER
308 AVCodec ff_pgm_decoder = {
310 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
311 .type = AVMEDIA_TYPE_VIDEO,
312 .id = AV_CODEC_ID_PGM,
313 .priv_data_size = sizeof(PNMContext),
314 .decode = pnm_decode_frame,
315 .capabilities = AV_CODEC_CAP_DR1,
319 #if CONFIG_PGMYUV_DECODER
320 AVCodec ff_pgmyuv_decoder = {
322 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
323 .type = AVMEDIA_TYPE_VIDEO,
324 .id = AV_CODEC_ID_PGMYUV,
325 .priv_data_size = sizeof(PNMContext),
326 .decode = pnm_decode_frame,
327 .capabilities = AV_CODEC_CAP_DR1,
331 #if CONFIG_PPM_DECODER
332 AVCodec ff_ppm_decoder = {
334 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
335 .type = AVMEDIA_TYPE_VIDEO,
336 .id = AV_CODEC_ID_PPM,
337 .priv_data_size = sizeof(PNMContext),
338 .decode = pnm_decode_frame,
339 .capabilities = AV_CODEC_CAP_DR1,
343 #if CONFIG_PBM_DECODER
344 AVCodec ff_pbm_decoder = {
346 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
347 .type = AVMEDIA_TYPE_VIDEO,
348 .id = AV_CODEC_ID_PBM,
349 .priv_data_size = sizeof(PNMContext),
350 .decode = pnm_decode_frame,
351 .capabilities = AV_CODEC_CAP_DR1,
355 #if CONFIG_PAM_DECODER
356 AVCodec ff_pam_decoder = {
358 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
359 .type = AVMEDIA_TYPE_VIDEO,
360 .id = AV_CODEC_ID_PAM,
361 .priv_data_size = sizeof(PNMContext),
362 .decode = pnm_decode_frame,
363 .capabilities = AV_CODEC_CAP_DR1,
367 #if CONFIG_PFM_DECODER
368 AVCodec ff_pfm_decoder = {
370 .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
371 .type = AVMEDIA_TYPE_VIDEO,
372 .id = AV_CODEC_ID_PFM,
373 .priv_data_size = sizeof(PNMContext),
374 .decode = pnm_decode_frame,
375 .capabilities = AV_CODEC_CAP_DR1,