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
28 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
29 int *got_frame, AVPacket *avpkt)
31 const uint8_t *buf = avpkt->data;
32 int buf_size = avpkt->size;
33 PNMContext * const s = avctx->priv_data;
34 AVFrame * const p = data;
35 int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
37 int components, sample_len, ret;
40 s->bytestream = (uint8_t *)buf;
41 s->bytestream_end = (uint8_t *)buf + buf_size;
43 if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
46 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
48 p->pict_type = AV_PICTURE_TYPE_I;
51 switch (avctx->pix_fmt) {
53 return AVERROR(EINVAL);
54 case AV_PIX_FMT_RGBA64BE:
59 case AV_PIX_FMT_RGB48BE:
69 case AV_PIX_FMT_RGB24:
74 case AV_PIX_FMT_GRAY8:
81 case AV_PIX_FMT_GRAY8A:
86 case AV_PIX_FMT_GRAY16BE:
87 case AV_PIX_FMT_GRAY16LE:
91 if (s->maxval < 65535)
94 case AV_PIX_FMT_MONOWHITE:
95 case AV_PIX_FMT_MONOBLACK:
96 n = (avctx->width + 7) >> 3;
102 linesize = p->linesize[0];
103 if (s->bytestream + n * avctx->height > s->bytestream_end)
104 return AVERROR_INVALIDDATA;
105 if(s->type < 4 || (is_mono && s->type==7)){
106 for (i=0; i<avctx->height; i++) {
108 init_put_bits(&pb, ptr, linesize);
109 for(j=0; j<avctx->width * components; j++){
113 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
115 if(s->bytestream >= s->bytestream_end)
116 return AVERROR_INVALIDDATA;
118 /* read a single digit */
119 v = (*s->bytestream++)&1;
121 /* read a sequence of digits */
124 c = (*s->bytestream++) - '0';
127 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
133 for (i = 0; i < avctx->height; i++) {
135 memcpy(ptr, s->bytestream, n);
136 else if (upgrade == 1) {
137 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
138 for (j = 0; j < n; j++)
139 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
140 } else if (upgrade == 2) {
141 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
142 for (j = 0; j < n / 2; j++) {
143 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
144 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
152 case AV_PIX_FMT_YUV420P:
153 case AV_PIX_FMT_YUV420P9BE:
154 case AV_PIX_FMT_YUV420P10BE:
156 unsigned char *ptr1, *ptr2;
160 linesize = p->linesize[0];
161 if (s->maxval >= 256)
163 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
164 return AVERROR_INVALIDDATA;
165 for (i = 0; i < avctx->height; i++) {
166 memcpy(ptr, s->bytestream, n);
173 h = avctx->height >> 1;
174 for (i = 0; i < h; i++) {
175 memcpy(ptr1, s->bytestream, n);
177 memcpy(ptr2, s->bytestream, n);
179 ptr1 += p->linesize[1];
180 ptr2 += p->linesize[2];
184 case AV_PIX_FMT_YUV420P16:
186 uint16_t *ptr1, *ptr2;
187 const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
190 n = avctx->width * 2;
192 linesize = p->linesize[0];
193 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
194 return AVERROR_INVALIDDATA;
195 for (i = 0; i < avctx->height; i++) {
196 for (j = 0; j < n / 2; j++) {
197 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
198 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
203 ptr1 = (uint16_t*)p->data[1];
204 ptr2 = (uint16_t*)p->data[2];
206 h = avctx->height >> 1;
207 for (i = 0; i < h; i++) {
208 for (j = 0; j < n / 2; j++) {
209 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
210 ptr1[j] = (v * f + 16384) >> 15;
214 for (j = 0; j < n / 2; j++) {
215 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
216 ptr2[j] = (v * f + 16384) >> 15;
220 ptr1 += p->linesize[1] / 2;
221 ptr2 += p->linesize[2] / 2;
228 return s->bytestream - s->bytestream_start;
232 #if CONFIG_PGM_DECODER
233 AVCodec ff_pgm_decoder = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .id = AV_CODEC_ID_PGM,
237 .priv_data_size = sizeof(PNMContext),
238 .decode = pnm_decode_frame,
239 .capabilities = CODEC_CAP_DR1,
240 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
244 #if CONFIG_PGMYUV_DECODER
245 AVCodec ff_pgmyuv_decoder = {
247 .type = AVMEDIA_TYPE_VIDEO,
248 .id = AV_CODEC_ID_PGMYUV,
249 .priv_data_size = sizeof(PNMContext),
250 .decode = pnm_decode_frame,
251 .capabilities = CODEC_CAP_DR1,
252 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
256 #if CONFIG_PPM_DECODER
257 AVCodec ff_ppm_decoder = {
259 .type = AVMEDIA_TYPE_VIDEO,
260 .id = AV_CODEC_ID_PPM,
261 .priv_data_size = sizeof(PNMContext),
262 .decode = pnm_decode_frame,
263 .capabilities = CODEC_CAP_DR1,
264 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
268 #if CONFIG_PBM_DECODER
269 AVCodec ff_pbm_decoder = {
271 .type = AVMEDIA_TYPE_VIDEO,
272 .id = AV_CODEC_ID_PBM,
273 .priv_data_size = sizeof(PNMContext),
274 .decode = pnm_decode_frame,
275 .capabilities = CODEC_CAP_DR1,
276 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
280 #if CONFIG_PAM_DECODER
281 AVCodec ff_pam_decoder = {
283 .type = AVMEDIA_TYPE_VIDEO,
284 .id = AV_CODEC_ID_PAM,
285 .priv_data_size = sizeof(PNMContext),
286 .decode = pnm_decode_frame,
287 .capabilities = CODEC_CAP_DR1,
288 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),