OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[coroid/ffmpeg_saccubus.git] / 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     "pgm",
200     AVMEDIA_TYPE_VIDEO,
201     CODEC_ID_PGM,
202     sizeof(PNMContext),
203     ff_pnm_init,
204     NULL,
205     ff_pnm_end,
206     pnm_decode_frame,
207     CODEC_CAP_DR1,
208     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
209     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
210 };
211 #endif
212
213 #if CONFIG_PGMYUV_DECODER
214 AVCodec ff_pgmyuv_decoder = {
215     "pgmyuv",
216     AVMEDIA_TYPE_VIDEO,
217     CODEC_ID_PGMYUV,
218     sizeof(PNMContext),
219     ff_pnm_init,
220     NULL,
221     ff_pnm_end,
222     pnm_decode_frame,
223     CODEC_CAP_DR1,
224     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
225     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
226 };
227 #endif
228
229 #if CONFIG_PPM_DECODER
230 AVCodec ff_ppm_decoder = {
231     "ppm",
232     AVMEDIA_TYPE_VIDEO,
233     CODEC_ID_PPM,
234     sizeof(PNMContext),
235     ff_pnm_init,
236     NULL,
237     ff_pnm_end,
238     pnm_decode_frame,
239     CODEC_CAP_DR1,
240     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
241     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
242 };
243 #endif
244
245 #if CONFIG_PBM_DECODER
246 AVCodec ff_pbm_decoder = {
247     "pbm",
248     AVMEDIA_TYPE_VIDEO,
249     CODEC_ID_PBM,
250     sizeof(PNMContext),
251     ff_pnm_init,
252     NULL,
253     ff_pnm_end,
254     pnm_decode_frame,
255     CODEC_CAP_DR1,
256     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
257     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
258 };
259 #endif
260
261 #if CONFIG_PAM_DECODER
262 AVCodec ff_pam_decoder = {
263     "pam",
264     AVMEDIA_TYPE_VIDEO,
265     CODEC_ID_PAM,
266     sizeof(PNMContext),
267     ff_pnm_init,
268     NULL,
269     ff_pnm_end,
270     pnm_decode_frame,
271     CODEC_CAP_DR1,
272     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
273     .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
274 };
275 #endif