]> git.sesse.net Git - ffmpeg/blob - libavcodec/pnmdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / pnmdec.c
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 "put_bits.h"
24 #include "pnm.h"
25
26
27 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
28                             int *data_size, AVPacket *avpkt)
29 {
30     const uint8_t *buf   = avpkt->data;
31     int buf_size         = avpkt->size;
32     PNMContext * const s = avctx->priv_data;
33     AVFrame *picture     = data;
34     AVFrame * const p    = (AVFrame*)&s->picture;
35     int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
36     unsigned char *ptr;
37     int components, sample_len;
38
39     s->bytestream_start =
40     s->bytestream       = buf;
41     s->bytestream_end   = buf + buf_size;
42
43     if (ff_pnm_decode_header(avctx, s) < 0)
44         return -1;
45
46     if (p->data[0])
47         avctx->release_buffer(avctx, p);
48
49     p->reference = 0;
50     if (avctx->get_buffer(avctx, p) < 0) {
51         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
52         return -1;
53     }
54     p->pict_type = AV_PICTURE_TYPE_I;
55     p->key_frame = 1;
56
57     switch (avctx->pix_fmt) {
58     default:
59         return -1;
60     case PIX_FMT_RGBA64BE:
61         n = avctx->width * 8;
62         components=4;
63         sample_len=16;
64         goto do_read;
65     case PIX_FMT_RGB48BE:
66         n = avctx->width * 6;
67         components=3;
68         sample_len=16;
69         goto do_read;
70     case PIX_FMT_RGBA:
71         n = avctx->width * 4;
72         components=4;
73         sample_len=8;
74         goto do_read;
75     case PIX_FMT_RGB24:
76         n = avctx->width * 3;
77         components=3;
78         sample_len=8;
79         goto do_read;
80     case PIX_FMT_GRAY8:
81         n = avctx->width;
82         components=1;
83         sample_len=8;
84         if (s->maxval < 255)
85             upgrade = 1;
86         goto do_read;
87     case PIX_FMT_GRAY8A:
88         n = avctx->width * 2;
89         components=2;
90         sample_len=8;
91         goto do_read;
92     case PIX_FMT_GRAY16BE:
93     case PIX_FMT_GRAY16LE:
94         n = avctx->width * 2;
95         components=1;
96         sample_len=16;
97         if (s->maxval < 65535)
98             upgrade = 2;
99         goto do_read;
100     case PIX_FMT_MONOWHITE:
101     case PIX_FMT_MONOBLACK:
102         n = (avctx->width + 7) >> 3;
103         components=1;
104         sample_len=1;
105         is_mono = 1;
106     do_read:
107         ptr      = p->data[0];
108         linesize = p->linesize[0];
109         if (s->bytestream + n * avctx->height > s->bytestream_end)
110             return -1;
111         if(s->type < 4){
112             for (i=0; i<avctx->height; i++) {
113                 PutBitContext pb;
114                 init_put_bits(&pb, ptr, linesize);
115                 for(j=0; j<avctx->width * components; j++){
116                     unsigned int c=0;
117                     int v=0;
118                     while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
119                         s->bytestream++;
120                     if(s->bytestream >= s->bytestream_end)
121                         return -1;
122                     if (is_mono) {
123                         /* read a single digit */
124                         v = (*s->bytestream++) - '0';
125                     } else {
126                         /* read a sequence of digits */
127                         do {
128                             v = 10*v + c;
129                             c = (*s->bytestream++) - '0';
130                         } while (c <= 9);
131                     }
132                     put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
133                 }
134                 flush_put_bits(&pb);
135                 ptr+= linesize;
136             }
137         }else{
138         for (i = 0; i < avctx->height; i++) {
139             if (!upgrade)
140                 memcpy(ptr, s->bytestream, n);
141             else if (upgrade == 1) {
142                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
143                 for (j = 0; j < n; j++)
144                     ptr[j] = (s->bytestream[j] * f + 64) >> 7;
145             } else if (upgrade == 2) {
146                 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
147                 for (j = 0; j < n / 2; j++) {
148                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
149                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
150                 }
151             }
152             s->bytestream += n;
153             ptr           += linesize;
154         }
155         }
156         break;
157     case PIX_FMT_YUV420P:
158         {
159             unsigned char *ptr1, *ptr2;
160
161             n        = avctx->width;
162             ptr      = p->data[0];
163             linesize = p->linesize[0];
164             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
165                 return -1;
166             for (i = 0; i < avctx->height; i++) {
167                 memcpy(ptr, s->bytestream, n);
168                 s->bytestream += n;
169                 ptr           += linesize;
170             }
171             ptr1 = p->data[1];
172             ptr2 = p->data[2];
173             n >>= 1;
174             h = avctx->height >> 1;
175             for (i = 0; i < h; i++) {
176                 memcpy(ptr1, s->bytestream, n);
177                 s->bytestream += n;
178                 memcpy(ptr2, s->bytestream, n);
179                 s->bytestream += n;
180                 ptr1 += p->linesize[1];
181                 ptr2 += p->linesize[2];
182             }
183         }
184         break;
185     }
186     *picture   = *(AVFrame*)&s->picture;
187     *data_size = sizeof(AVPicture);
188
189     return s->bytestream - s->bytestream_start;
190 }
191
192
193 #if CONFIG_PGM_DECODER
194 AVCodec ff_pgm_decoder = {
195     .name           = "pgm",
196     .type           = AVMEDIA_TYPE_VIDEO,
197     .id             = CODEC_ID_PGM,
198     .priv_data_size = sizeof(PNMContext),
199     .init           = ff_pnm_init,
200     .close          = ff_pnm_end,
201     .decode         = pnm_decode_frame,
202     .capabilities   = CODEC_CAP_DR1,
203     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
204     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
205 };
206 #endif
207
208 #if CONFIG_PGMYUV_DECODER
209 AVCodec ff_pgmyuv_decoder = {
210     .name           = "pgmyuv",
211     .type           = AVMEDIA_TYPE_VIDEO,
212     .id             = CODEC_ID_PGMYUV,
213     .priv_data_size = sizeof(PNMContext),
214     .init           = ff_pnm_init,
215     .close          = ff_pnm_end,
216     .decode         = pnm_decode_frame,
217     .capabilities   = CODEC_CAP_DR1,
218     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
219     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
220 };
221 #endif
222
223 #if CONFIG_PPM_DECODER
224 AVCodec ff_ppm_decoder = {
225     .name           = "ppm",
226     .type           = AVMEDIA_TYPE_VIDEO,
227     .id             = CODEC_ID_PPM,
228     .priv_data_size = sizeof(PNMContext),
229     .init           = ff_pnm_init,
230     .close          = ff_pnm_end,
231     .decode         = pnm_decode_frame,
232     .capabilities   = CODEC_CAP_DR1,
233     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
234     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
235 };
236 #endif
237
238 #if CONFIG_PBM_DECODER
239 AVCodec ff_pbm_decoder = {
240     .name           = "pbm",
241     .type           = AVMEDIA_TYPE_VIDEO,
242     .id             = CODEC_ID_PBM,
243     .priv_data_size = sizeof(PNMContext),
244     .init           = ff_pnm_init,
245     .close          = ff_pnm_end,
246     .decode         = pnm_decode_frame,
247     .capabilities   = CODEC_CAP_DR1,
248     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
249     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
250 };
251 #endif
252
253 #if CONFIG_PAM_DECODER
254 AVCodec ff_pam_decoder = {
255     .name           = "pam",
256     .type           = AVMEDIA_TYPE_VIDEO,
257     .id             = CODEC_ID_PAM,
258     .priv_data_size = sizeof(PNMContext),
259     .init           = ff_pnm_init,
260     .close          = ff_pnm_end,
261     .decode         = pnm_decode_frame,
262     .capabilities   = CODEC_CAP_DR1,
263     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
264     .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
265 };
266 #endif