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, n, linesize, h, upgrade = 0, is_mono = 0;
48 int components, sample_len, ret;
51 s->bytestream = (uint8_t *)buf;
52 s->bytestream_end = (uint8_t *)buf + buf_size;
54 if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
57 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
59 p->pict_type = AV_PICTURE_TYPE_I;
61 avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
63 switch (avctx->pix_fmt) {
65 return AVERROR(EINVAL);
66 case AV_PIX_FMT_RGBA64:
70 if (s->maxval < 65535)
73 case AV_PIX_FMT_RGB48:
77 if (s->maxval < 65535)
85 case AV_PIX_FMT_RGB24:
92 case AV_PIX_FMT_GRAY8:
99 case AV_PIX_FMT_GRAY8A:
100 n = avctx->width * 2;
104 case AV_PIX_FMT_GRAY16:
105 n = avctx->width * 2;
108 if (s->maxval < 65535)
111 case AV_PIX_FMT_MONOWHITE:
112 case AV_PIX_FMT_MONOBLACK:
113 n = (avctx->width + 7) >> 3;
119 linesize = p->linesize[0];
120 if (s->bytestream + n * avctx->height > s->bytestream_end)
121 return AVERROR_INVALIDDATA;
122 if(s->type < 4 || (is_mono && s->type==7)){
123 for (i=0; i<avctx->height; i++) {
125 init_put_bits(&pb, ptr, linesize);
126 for(j=0; j<avctx->width * components; j++){
130 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
132 if(s->bytestream >= s->bytestream_end)
133 return AVERROR_INVALIDDATA;
135 /* read a single digit */
136 v = (*s->bytestream++)&1;
138 /* read a sequence of digits */
141 c = (*s->bytestream++) - '0';
144 if (sample_len == 16) {
145 ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
147 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
149 if (sample_len != 16)
154 for (i = 0; i < avctx->height; i++) {
156 samplecpy(ptr, s->bytestream, n, s->maxval);
157 else if (upgrade == 1) {
158 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
159 for (j = 0; j < n; j++)
160 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
161 } else if (upgrade == 2) {
162 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
163 for (j = 0; j < n / 2; j++) {
164 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
165 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
173 case AV_PIX_FMT_YUV420P:
174 case AV_PIX_FMT_YUV420P9:
175 case AV_PIX_FMT_YUV420P10:
177 unsigned char *ptr1, *ptr2;
181 linesize = p->linesize[0];
182 if (s->maxval >= 256)
184 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
185 return AVERROR_INVALIDDATA;
186 for (i = 0; i < avctx->height; i++) {
187 samplecpy(ptr, s->bytestream, n, s->maxval);
194 h = avctx->height >> 1;
195 for (i = 0; i < h; i++) {
196 samplecpy(ptr1, s->bytestream, n, s->maxval);
198 samplecpy(ptr2, s->bytestream, n, s->maxval);
200 ptr1 += p->linesize[1];
201 ptr2 += p->linesize[2];
205 case AV_PIX_FMT_YUV420P16:
207 uint16_t *ptr1, *ptr2;
208 const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
211 n = avctx->width * 2;
213 linesize = p->linesize[0];
214 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
215 return AVERROR_INVALIDDATA;
216 for (i = 0; i < avctx->height; i++) {
217 for (j = 0; j < n / 2; j++) {
218 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
219 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
224 ptr1 = (uint16_t*)p->data[1];
225 ptr2 = (uint16_t*)p->data[2];
227 h = avctx->height >> 1;
228 for (i = 0; i < h; i++) {
229 for (j = 0; j < n / 2; j++) {
230 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
231 ptr1[j] = (v * f + 16384) >> 15;
235 for (j = 0; j < n / 2; j++) {
236 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
237 ptr2[j] = (v * f + 16384) >> 15;
241 ptr1 += p->linesize[1] / 2;
242 ptr2 += p->linesize[2] / 2;
249 return s->bytestream - s->bytestream_start;
253 #if CONFIG_PGM_DECODER
254 AVCodec ff_pgm_decoder = {
256 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
257 .type = AVMEDIA_TYPE_VIDEO,
258 .id = AV_CODEC_ID_PGM,
259 .priv_data_size = sizeof(PNMContext),
260 .decode = pnm_decode_frame,
261 .capabilities = CODEC_CAP_DR1,
265 #if CONFIG_PGMYUV_DECODER
266 AVCodec ff_pgmyuv_decoder = {
268 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
269 .type = AVMEDIA_TYPE_VIDEO,
270 .id = AV_CODEC_ID_PGMYUV,
271 .priv_data_size = sizeof(PNMContext),
272 .decode = pnm_decode_frame,
273 .capabilities = CODEC_CAP_DR1,
277 #if CONFIG_PPM_DECODER
278 AVCodec ff_ppm_decoder = {
280 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
281 .type = AVMEDIA_TYPE_VIDEO,
282 .id = AV_CODEC_ID_PPM,
283 .priv_data_size = sizeof(PNMContext),
284 .decode = pnm_decode_frame,
285 .capabilities = CODEC_CAP_DR1,
289 #if CONFIG_PBM_DECODER
290 AVCodec ff_pbm_decoder = {
292 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
293 .type = AVMEDIA_TYPE_VIDEO,
294 .id = AV_CODEC_ID_PBM,
295 .priv_data_size = sizeof(PNMContext),
296 .decode = pnm_decode_frame,
297 .capabilities = CODEC_CAP_DR1,
301 #if CONFIG_PAM_DECODER
302 AVCodec ff_pam_decoder = {
304 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
305 .type = AVMEDIA_TYPE_VIDEO,
306 .id = AV_CODEC_ID_PAM,
307 .priv_data_size = sizeof(PNMContext),
308 .decode = pnm_decode_frame,
309 .capabilities = CODEC_CAP_DR1,