3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
28 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
29 int *got_frame, AVPacket *avpkt)
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 = &s->picture;
36 int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
38 int components, sample_len, ret;
41 s->bytestream = (uint8_t *)buf;
42 s->bytestream_end = (uint8_t *)buf + buf_size;
44 if (ff_pnm_decode_header(avctx, s) < 0)
45 return AVERROR_INVALIDDATA;
48 avctx->release_buffer(avctx, p);
51 if ((ret = ff_get_buffer(avctx, p)) < 0) {
52 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
55 p->pict_type = AV_PICTURE_TYPE_I;
58 switch (avctx->pix_fmt) {
60 return AVERROR_INVALIDDATA;
61 case AV_PIX_FMT_RGBA64BE:
66 case AV_PIX_FMT_RGB48BE:
76 case AV_PIX_FMT_RGB24:
81 case AV_PIX_FMT_GRAY8:
88 case AV_PIX_FMT_GRAY8A:
93 case AV_PIX_FMT_GRAY16BE:
94 case AV_PIX_FMT_GRAY16LE:
98 if (s->maxval < 65535)
101 case AV_PIX_FMT_MONOWHITE:
102 case AV_PIX_FMT_MONOBLACK:
103 n = (avctx->width + 7) >> 3;
109 linesize = p->linesize[0];
110 if (s->bytestream + n * avctx->height > s->bytestream_end)
111 return AVERROR_INVALIDDATA;
112 if(s->type < 4 || (is_mono && s->type==7)){
113 for (i=0; i<avctx->height; i++) {
115 init_put_bits(&pb, ptr, linesize);
116 for(j=0; j<avctx->width * components; j++){
120 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
122 if(s->bytestream >= s->bytestream_end)
123 return AVERROR_INVALIDDATA;
125 /* read a single digit */
126 v = (*s->bytestream++)&1;
128 /* read a sequence of digits */
131 c = (*s->bytestream++) - '0';
134 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
140 for (i = 0; i < avctx->height; i++) {
142 memcpy(ptr, s->bytestream, n);
143 else if (upgrade == 1) {
144 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
145 for (j = 0; j < n; j++)
146 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
147 } else if (upgrade == 2) {
148 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
149 for (j = 0; j < n / 2; j++) {
150 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
151 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
159 case AV_PIX_FMT_YUV420P:
161 unsigned char *ptr1, *ptr2;
165 linesize = p->linesize[0];
166 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
167 return AVERROR_INVALIDDATA;
168 for (i = 0; i < avctx->height; i++) {
169 memcpy(ptr, s->bytestream, n);
176 h = avctx->height >> 1;
177 for (i = 0; i < h; i++) {
178 memcpy(ptr1, s->bytestream, n);
180 memcpy(ptr2, s->bytestream, n);
182 ptr1 += p->linesize[1];
183 ptr2 += p->linesize[2];
188 *picture = s->picture;
191 return s->bytestream - s->bytestream_start;
195 #if CONFIG_PGM_DECODER
196 AVCodec ff_pgm_decoder = {
198 .type = AVMEDIA_TYPE_VIDEO,
199 .id = AV_CODEC_ID_PGM,
200 .priv_data_size = sizeof(PNMContext),
203 .decode = pnm_decode_frame,
204 .capabilities = CODEC_CAP_DR1,
205 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
209 #if CONFIG_PGMYUV_DECODER
210 AVCodec ff_pgmyuv_decoder = {
212 .type = AVMEDIA_TYPE_VIDEO,
213 .id = AV_CODEC_ID_PGMYUV,
214 .priv_data_size = sizeof(PNMContext),
217 .decode = pnm_decode_frame,
218 .capabilities = CODEC_CAP_DR1,
219 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
223 #if CONFIG_PPM_DECODER
224 AVCodec ff_ppm_decoder = {
226 .type = AVMEDIA_TYPE_VIDEO,
227 .id = AV_CODEC_ID_PPM,
228 .priv_data_size = sizeof(PNMContext),
231 .decode = pnm_decode_frame,
232 .capabilities = CODEC_CAP_DR1,
233 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
237 #if CONFIG_PBM_DECODER
238 AVCodec ff_pbm_decoder = {
240 .type = AVMEDIA_TYPE_VIDEO,
241 .id = AV_CODEC_ID_PBM,
242 .priv_data_size = sizeof(PNMContext),
245 .decode = pnm_decode_frame,
246 .capabilities = CODEC_CAP_DR1,
247 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
251 #if CONFIG_PAM_DECODER
252 AVCodec ff_pam_decoder = {
254 .type = AVMEDIA_TYPE_VIDEO,
255 .id = AV_CODEC_ID_PAM,
256 .priv_data_size = sizeof(PNMContext),
259 .decode = pnm_decode_frame,
260 .capabilities = CODEC_CAP_DR1,
261 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),