pnmdec.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * PNM image format
 * Copyright (c) 2002, 2003 Fabrice Bellard
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avcodec.h"
23
#include "internal.h"
24
#include "put_bits.h"
25 26
#include "pnm.h"

27
static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
28 29 30 31 32 33
{
    if (maxval <= 255) {
        memcpy(dst, src, n);
    } else {
        int i;
        for (i=0; i<n/2; i++) {
34
            ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
35 36 37
        }
    }
}
38 39

static int pnm_decode_frame(AVCodecContext *avctx, void *data,
40
                            int *got_frame, AVPacket *avpkt)
41 42 43 44
{
    const uint8_t *buf   = avpkt->data;
    int buf_size         = avpkt->size;
    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 52
    s->bytestream       = (uint8_t *)buf;
    s->bytestream_end   = (uint8_t *)buf + buf_size;
53

54 55
    if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
        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 64

    switch (avctx->pix_fmt) {
    default:
65
        return AVERROR(EINVAL);
66
    case AV_PIX_FMT_RGBA64:
67 68 69
        n = avctx->width * 8;
        components=4;
        sample_len=16;
70 71
        if (s->maxval < 65535)
            upgrade = 2;
72
        goto do_read;
73
    case AV_PIX_FMT_RGB48:
74
        n = avctx->width * 6;
75 76
        components=3;
        sample_len=16;
77 78
        if (s->maxval < 65535)
            upgrade = 2;
79
        goto do_read;
80
    case AV_PIX_FMT_RGBA:
81 82 83 84
        n = avctx->width * 4;
        components=4;
        sample_len=8;
        goto do_read;
85
    case AV_PIX_FMT_RGB24:
86
        n = avctx->width * 3;
87 88
        components=3;
        sample_len=8;
89 90
        if (s->maxval < 255)
            upgrade = 1;
91
        goto do_read;
92
    case AV_PIX_FMT_GRAY8:
93
        n = avctx->width;
94 95
        components=1;
        sample_len=8;
96
        if (s->maxval < 255)
97 98
            upgrade = 1;
        goto do_read;
99
    case AV_PIX_FMT_GRAY8A:
100 101 102 103
        n = avctx->width * 2;
        components=2;
        sample_len=8;
        goto do_read;
104
    case AV_PIX_FMT_GRAY16:
105
        n = avctx->width * 2;
106 107
        components=1;
        sample_len=16;
108
        if (s->maxval < 65535)
109 110
            upgrade = 2;
        goto do_read;
111 112
    case AV_PIX_FMT_MONOWHITE:
    case AV_PIX_FMT_MONOBLACK:
113
        n = (avctx->width + 7) >> 3;
114 115
        components=1;
        sample_len=1;
116
        is_mono = 1;
117 118 119 120
    do_read:
        ptr      = p->data[0];
        linesize = p->linesize[0];
        if (s->bytestream + n * avctx->height > s->bytestream_end)
121
            return AVERROR_INVALIDDATA;
122
        if(s->type < 4 || (is_mono && s->type==7)){
123 124 125 126 127 128
            for (i=0; i<avctx->height; i++) {
                PutBitContext pb;
                init_put_bits(&pb, ptr, linesize);
                for(j=0; j<avctx->width * components; j++){
                    unsigned int c=0;
                    int v=0;
129
                    if(s->type < 4)
130 131 132
                    while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
                        s->bytestream++;
                    if(s->bytestream >= s->bytestream_end)
133
                        return AVERROR_INVALIDDATA;
134 135
                    if (is_mono) {
                        /* read a single digit */
136
                        v = (*s->bytestream++)&1;
137
                    } else {
138 139 140 141 142
                        /* read a sequence of digits */
                        do {
                            v = 10*v + c;
                            c = (*s->bytestream++) - '0';
                        } while (c <= 9);
143
                    }
144 145 146 147
                    if (sample_len == 16) {
                        ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
                    } else
                        put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
148
                }
149 150
                if (sample_len != 16)
                    flush_put_bits(&pb);
151 152 153
                ptr+= linesize;
            }
        }else{
154 155
        for (i = 0; i < avctx->height; i++) {
            if (!upgrade)
156
                samplecpy(ptr, s->bytestream, n, s->maxval);
157 158 159
            else if (upgrade == 1) {
                unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
                for (j = 0; j < n; j++)
160
                    ptr[j] = (s->bytestream[j] * f + 64) >> 7;
161 162 163
            } else if (upgrade == 2) {
                unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
                for (j = 0; j < n / 2; j++) {
164
                    v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
165 166 167 168 169 170
                    ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
                }
            }
            s->bytestream += n;
            ptr           += linesize;
        }
171
        }
172
        break;
173
    case AV_PIX_FMT_YUV420P:
174 175
    case AV_PIX_FMT_YUV420P9:
    case AV_PIX_FMT_YUV420P10:
176 177 178 179 180 181
        {
            unsigned char *ptr1, *ptr2;

            n        = avctx->width;
            ptr      = p->data[0];
            linesize = p->linesize[0];
182 183
            if (s->maxval >= 256)
                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 189 190 191 192 193 194 195
                s->bytestream += n;
                ptr           += linesize;
            }
            ptr1 = p->data[1];
            ptr2 = p->data[2];
            n >>= 1;
            h = avctx->height >> 1;
            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 200 201 202 203 204
                s->bytestream += n;
                ptr1 += p->linesize[1];
                ptr2 += p->linesize[2];
            }
        }
        break;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    case AV_PIX_FMT_YUV420P16:
        {
            uint16_t *ptr1, *ptr2;
            const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
            unsigned int j, v;

            n        = avctx->width * 2;
            ptr      = p->data[0];
            linesize = p->linesize[0];
            if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
                return AVERROR_INVALIDDATA;
            for (i = 0; i < avctx->height; i++) {
                for (j = 0; j < n / 2; j++) {
                    v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                    ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
                }
                s->bytestream += n;
                ptr           += linesize;
            }
            ptr1 = (uint16_t*)p->data[1];
            ptr2 = (uint16_t*)p->data[2];
            n >>= 1;
            h = avctx->height >> 1;
            for (i = 0; i < h; i++) {
                for (j = 0; j < n / 2; j++) {
                    v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                    ptr1[j] = (v * f + 16384) >> 15;
                }
                s->bytestream += n;

                for (j = 0; j < n / 2; j++) {
                    v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                    ptr2[j] = (v * f + 16384) >> 15;
                }
                s->bytestream += n;

                ptr1 += p->linesize[1] / 2;
                ptr2 += p->linesize[2] / 2;
            }
        }
        break;
246
    }
247
    *got_frame = 1;
248 249 250 251 252 253

    return s->bytestream - s->bytestream_start;
}


#if CONFIG_PGM_DECODER
254
AVCodec 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 260 261
    .priv_data_size = sizeof(PNMContext),
    .decode         = pnm_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
262 263 264 265
};
#endif

#if CONFIG_PGMYUV_DECODER
266
AVCodec 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 272 273
    .priv_data_size = sizeof(PNMContext),
    .decode         = pnm_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
274 275 276 277
};
#endif

#if CONFIG_PPM_DECODER
278
AVCodec 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 284 285
    .priv_data_size = sizeof(PNMContext),
    .decode         = pnm_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
286 287 288 289
};
#endif

#if CONFIG_PBM_DECODER
290
AVCodec 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 296 297
    .priv_data_size = sizeof(PNMContext),
    .decode         = pnm_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
298 299 300 301
};
#endif

#if CONFIG_PAM_DECODER
302
AVCodec 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 308 309
    .priv_data_size = sizeof(PNMContext),
    .decode         = pnm_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
310 311
};
#endif