1/*
2 * PNM image format
3 * Copyright (c) 2002, 2003 Fabrice Bellard
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 "internal.h"
24#include "put_bits.h"
25#include "pnm.h"
26
27static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
28{
29    if (maxval <= 255) {
30        memcpy(dst, src, n);
31    } else {
32        int i;
33        for (i=0; i<n/2; i++) {
34            ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
35        }
36    }
37}
38
39static int pnm_decode_frame(AVCodecContext *avctx, void *data,
40                            int *got_frame, AVPacket *avpkt)
41{
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;
47    unsigned char *ptr;
48    int components, sample_len, ret;
49
50    s->bytestream_start =
51    s->bytestream       = (uint8_t *)buf;
52    s->bytestream_end   = (uint8_t *)buf + buf_size;
53
54    if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
55        return ret;
56
57    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
58        return ret;
59    p->pict_type = AV_PICTURE_TYPE_I;
60    p->key_frame = 1;
61    avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
62
63    switch (avctx->pix_fmt) {
64    default:
65        return AVERROR(EINVAL);
66    case AV_PIX_FMT_RGBA64:
67        n = avctx->width * 8;
68        components=4;
69        sample_len=16;
70        if (s->maxval < 65535)
71            upgrade = 2;
72        goto do_read;
73    case AV_PIX_FMT_RGB48:
74        n = avctx->width * 6;
75        components=3;
76        sample_len=16;
77        if (s->maxval < 65535)
78            upgrade = 2;
79        goto do_read;
80    case AV_PIX_FMT_RGBA:
81        n = avctx->width * 4;
82        components=4;
83        sample_len=8;
84        goto do_read;
85    case AV_PIX_FMT_RGB24:
86        n = avctx->width * 3;
87        components=3;
88        sample_len=8;
89        if (s->maxval < 255)
90            upgrade = 1;
91        goto do_read;
92    case AV_PIX_FMT_GRAY8:
93        n = avctx->width;
94        components=1;
95        sample_len=8;
96        if (s->maxval < 255)
97            upgrade = 1;
98        goto do_read;
99    case AV_PIX_FMT_GRAY8A:
100        n = avctx->width * 2;
101        components=2;
102        sample_len=8;
103        goto do_read;
104    case AV_PIX_FMT_GRAY16:
105        n = avctx->width * 2;
106        components=1;
107        sample_len=16;
108        if (s->maxval < 65535)
109            upgrade = 2;
110        goto do_read;
111    case AV_PIX_FMT_MONOWHITE:
112    case AV_PIX_FMT_MONOBLACK:
113        n = (avctx->width + 7) >> 3;
114        components=1;
115        sample_len=1;
116        is_mono = 1;
117    do_read:
118        ptr      = p->data[0];
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++) {
124                PutBitContext pb;
125                init_put_bits(&pb, ptr, linesize);
126                for(j=0; j<avctx->width * components; j++){
127                    unsigned int c=0;
128                    int v=0;
129                    if(s->type < 4)
130                    while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
131                        s->bytestream++;
132                    if(s->bytestream >= s->bytestream_end)
133                        return AVERROR_INVALIDDATA;
134                    if (is_mono) {
135                        /* read a single digit */
136                        v = (*s->bytestream++)&1;
137                    } else {
138                        /* read a sequence of digits */
139                        do {
140                            v = 10*v + c;
141                            c = (*s->bytestream++) - '0';
142                        } while (c <= 9);
143                    }
144                    if (sample_len == 16) {
145                        ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
146                    } else
147                        put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
148                }
149                if (sample_len != 16)
150                    flush_put_bits(&pb);
151                ptr+= linesize;
152            }
153        }else{
154        for (i = 0; i < avctx->height; i++) {
155            if (!upgrade)
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;
166                }
167            }
168            s->bytestream += n;
169            ptr           += linesize;
170        }
171        }
172        break;
173    case AV_PIX_FMT_YUV420P:
174    case AV_PIX_FMT_YUV420P9:
175    case AV_PIX_FMT_YUV420P10:
176        {
177            unsigned char *ptr1, *ptr2;
178
179            n        = avctx->width;
180            ptr      = p->data[0];
181            linesize = p->linesize[0];
182            if (s->maxval >= 256)
183                n *= 2;
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);
188                s->bytestream += n;
189                ptr           += linesize;
190            }
191            ptr1 = p->data[1];
192            ptr2 = p->data[2];
193            n >>= 1;
194            h = avctx->height >> 1;
195            for (i = 0; i < h; i++) {
196                samplecpy(ptr1, s->bytestream, n, s->maxval);
197                s->bytestream += n;
198                samplecpy(ptr2, s->bytestream, n, s->maxval);
199                s->bytestream += n;
200                ptr1 += p->linesize[1];
201                ptr2 += p->linesize[2];
202            }
203        }
204        break;
205    case AV_PIX_FMT_YUV420P16:
206        {
207            uint16_t *ptr1, *ptr2;
208            const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
209            unsigned int j, v;
210
211            n        = avctx->width * 2;
212            ptr      = p->data[0];
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;
220                }
221                s->bytestream += n;
222                ptr           += linesize;
223            }
224            ptr1 = (uint16_t*)p->data[1];
225            ptr2 = (uint16_t*)p->data[2];
226            n >>= 1;
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;
232                }
233                s->bytestream += n;
234
235                for (j = 0; j < n / 2; j++) {
236                    v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
237                    ptr2[j] = (v * f + 16384) >> 15;
238                }
239                s->bytestream += n;
240
241                ptr1 += p->linesize[1] / 2;
242                ptr2 += p->linesize[2] / 2;
243            }
244        }
245        break;
246    }
247    *got_frame = 1;
248
249    return s->bytestream - s->bytestream_start;
250}
251
252
253#if CONFIG_PGM_DECODER
254AVCodec ff_pgm_decoder = {
255    .name           = "pgm",
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,
262};
263#endif
264
265#if CONFIG_PGMYUV_DECODER
266AVCodec ff_pgmyuv_decoder = {
267    .name           = "pgmyuv",
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,
274};
275#endif
276
277#if CONFIG_PPM_DECODER
278AVCodec ff_ppm_decoder = {
279    .name           = "ppm",
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,
286};
287#endif
288
289#if CONFIG_PBM_DECODER
290AVCodec ff_pbm_decoder = {
291    .name           = "pbm",
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,
298};
299#endif
300
301#if CONFIG_PAM_DECODER
302AVCodec ff_pam_decoder = {
303    .name           = "pam",
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,
310};
311#endif
312