]> git.sesse.net Git - ffmpeg/blob - libavcodec/pnmdec.c
h264: check for luma and chroma bit dept being equal
[ffmpeg] / libavcodec / pnmdec.c
1 /*
2  * PNM image format
3  * Copyright (c) 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "internal.h"
25 #include "put_bits.h"
26 #include "pnm.h"
27
28
29 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
30                             int *got_frame, AVPacket *avpkt)
31 {
32     const uint8_t *buf   = avpkt->data;
33     int buf_size         = avpkt->size;
34     PNMContext * const s = avctx->priv_data;
35     AVFrame *picture     = data;
36     AVFrame * const p    = &s->picture;
37     int i, j, n, linesize, h, upgrade = 0;
38     unsigned char *ptr;
39     int components, sample_len, ret;
40
41     s->bytestream_start =
42     s->bytestream       = buf;
43     s->bytestream_end   = buf + buf_size;
44
45     if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
46         return ret;
47
48     if (p->data[0])
49         avctx->release_buffer(avctx, p);
50
51     p->reference = 0;
52     if ((ret = ff_get_buffer(avctx, p)) < 0) {
53         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
54         return ret;
55     }
56     p->pict_type = AV_PICTURE_TYPE_I;
57     p->key_frame = 1;
58
59     switch (avctx->pix_fmt) {
60     default:
61         return AVERROR(EINVAL);
62     case AV_PIX_FMT_RGB48BE:
63         n = avctx->width * 6;
64         components=3;
65         sample_len=16;
66         goto do_read;
67     case AV_PIX_FMT_RGB24:
68         n = avctx->width * 3;
69         components=3;
70         sample_len=8;
71         goto do_read;
72     case AV_PIX_FMT_GRAY8:
73         n = avctx->width;
74         components=1;
75         sample_len=8;
76         if (s->maxval < 255)
77             upgrade = 1;
78         goto do_read;
79     case AV_PIX_FMT_GRAY16BE:
80     case AV_PIX_FMT_GRAY16LE:
81         n = avctx->width * 2;
82         components=1;
83         sample_len=16;
84         if (s->maxval < 65535)
85             upgrade = 2;
86         goto do_read;
87     case AV_PIX_FMT_MONOWHITE:
88     case AV_PIX_FMT_MONOBLACK:
89         n = (avctx->width + 7) >> 3;
90         components=1;
91         sample_len=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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
108                     do{
109                         v= 10*v + c;
110                         c= (*s->bytestream++) - '0';
111                     }while(c <= 9);
112                     put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
113                 }
114                 flush_put_bits(&pb);
115                 ptr+= linesize;
116             }
117         }else{
118         for (i = 0; i < avctx->height; i++) {
119             if (!upgrade)
120                 memcpy(ptr, s->bytestream, n);
121             else if (upgrade == 1) {
122                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
123                 for (j = 0; j < n; j++)
124                     ptr[j] = (s->bytestream[j] * f + 64) >> 7;
125             } else if (upgrade == 2) {
126                 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
127                 for (j = 0; j < n / 2; j++) {
128                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
129                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
130                 }
131             }
132             s->bytestream += n;
133             ptr           += linesize;
134         }
135         }
136         break;
137     case AV_PIX_FMT_YUV420P:
138     case AV_PIX_FMT_YUV420P9BE:
139     case AV_PIX_FMT_YUV420P10BE:
140         {
141             unsigned char *ptr1, *ptr2;
142
143             n        = avctx->width;
144             ptr      = p->data[0];
145             linesize = p->linesize[0];
146             if (s->maxval >= 256)
147                 n *= 2;
148             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
149                 return AVERROR_INVALIDDATA;
150             for (i = 0; i < avctx->height; i++) {
151                 memcpy(ptr, s->bytestream, n);
152                 s->bytestream += n;
153                 ptr           += linesize;
154             }
155             ptr1 = p->data[1];
156             ptr2 = p->data[2];
157             n >>= 1;
158             h = avctx->height >> 1;
159             for (i = 0; i < h; i++) {
160                 memcpy(ptr1, s->bytestream, n);
161                 s->bytestream += n;
162                 memcpy(ptr2, s->bytestream, n);
163                 s->bytestream += n;
164                 ptr1 += p->linesize[1];
165                 ptr2 += p->linesize[2];
166             }
167         }
168         break;
169     case AV_PIX_FMT_YUV420P16:
170         {
171             uint16_t *ptr1, *ptr2;
172             const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
173             unsigned int j, v;
174
175             n        = avctx->width * 2;
176             ptr      = p->data[0];
177             linesize = p->linesize[0];
178             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
179                 return AVERROR_INVALIDDATA;
180             for (i = 0; i < avctx->height; i++) {
181                 for (j = 0; j < n / 2; j++) {
182                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
183                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
184                 }
185                 s->bytestream += n;
186                 ptr           += linesize;
187             }
188             ptr1 = (uint16_t*)p->data[1];
189             ptr2 = (uint16_t*)p->data[2];
190             n >>= 1;
191             h = avctx->height >> 1;
192             for (i = 0; i < h; i++) {
193                 for (j = 0; j < n / 2; j++) {
194                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
195                     ptr1[j] = (v * f + 16384) >> 15;
196                 }
197                 s->bytestream += n;
198
199                 for (j = 0; j < n / 2; j++) {
200                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
201                     ptr2[j] = (v * f + 16384) >> 15;
202                 }
203                 s->bytestream += n;
204
205                 ptr1 += p->linesize[1] / 2;
206                 ptr2 += p->linesize[2] / 2;
207             }
208         }
209         break;
210     case AV_PIX_FMT_RGB32:
211         ptr      = p->data[0];
212         linesize = p->linesize[0];
213         if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
214             return AVERROR_INVALIDDATA;
215         for (i = 0; i < avctx->height; i++) {
216             int j, r, g, b, a;
217
218             for (j = 0; j < avctx->width; j++) {
219                 r = *s->bytestream++;
220                 g = *s->bytestream++;
221                 b = *s->bytestream++;
222                 a = *s->bytestream++;
223                 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
224             }
225             ptr += linesize;
226         }
227         break;
228     }
229     *picture   = s->picture;
230     *got_frame = 1;
231
232     return s->bytestream - s->bytestream_start;
233 }
234
235
236 #if CONFIG_PGM_DECODER
237 AVCodec ff_pgm_decoder = {
238     .name           = "pgm",
239     .type           = AVMEDIA_TYPE_VIDEO,
240     .id             = AV_CODEC_ID_PGM,
241     .priv_data_size = sizeof(PNMContext),
242     .init           = ff_pnm_init,
243     .close          = ff_pnm_end,
244     .decode         = pnm_decode_frame,
245     .capabilities   = CODEC_CAP_DR1,
246     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
247 };
248 #endif
249
250 #if CONFIG_PGMYUV_DECODER
251 AVCodec ff_pgmyuv_decoder = {
252     .name           = "pgmyuv",
253     .type           = AVMEDIA_TYPE_VIDEO,
254     .id             = AV_CODEC_ID_PGMYUV,
255     .priv_data_size = sizeof(PNMContext),
256     .init           = ff_pnm_init,
257     .close          = ff_pnm_end,
258     .decode         = pnm_decode_frame,
259     .capabilities   = CODEC_CAP_DR1,
260     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
261 };
262 #endif
263
264 #if CONFIG_PPM_DECODER
265 AVCodec ff_ppm_decoder = {
266     .name           = "ppm",
267     .type           = AVMEDIA_TYPE_VIDEO,
268     .id             = AV_CODEC_ID_PPM,
269     .priv_data_size = sizeof(PNMContext),
270     .init           = ff_pnm_init,
271     .close          = ff_pnm_end,
272     .decode         = pnm_decode_frame,
273     .capabilities   = CODEC_CAP_DR1,
274     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
275 };
276 #endif
277
278 #if CONFIG_PBM_DECODER
279 AVCodec ff_pbm_decoder = {
280     .name           = "pbm",
281     .type           = AVMEDIA_TYPE_VIDEO,
282     .id             = AV_CODEC_ID_PBM,
283     .priv_data_size = sizeof(PNMContext),
284     .init           = ff_pnm_init,
285     .close          = ff_pnm_end,
286     .decode         = pnm_decode_frame,
287     .capabilities   = CODEC_CAP_DR1,
288     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
289 };
290 #endif
291
292 #if CONFIG_PAM_DECODER
293 AVCodec ff_pam_decoder = {
294     .name           = "pam",
295     .type           = AVMEDIA_TYPE_VIDEO,
296     .id             = AV_CODEC_ID_PAM,
297     .priv_data_size = sizeof(PNMContext),
298     .init           = ff_pnm_init,
299     .close          = ff_pnm_end,
300     .decode         = pnm_decode_frame,
301     .capabilities   = CODEC_CAP_DR1,
302     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
303 };
304 #endif