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