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
27 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
28 int *data_size, AVPacket *avpkt)
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 = &s->picture;
35 int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
37 int components, sample_len;
40 s->bytestream = (uint8_t *)buf;
41 s->bytestream_end = (uint8_t *)buf + buf_size;
43 if (ff_pnm_decode_header(avctx, s) < 0)
47 avctx->release_buffer(avctx, p);
50 if (avctx->get_buffer(avctx, p) < 0) {
51 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
54 p->pict_type = AV_PICTURE_TYPE_I;
57 switch (avctx->pix_fmt) {
60 case PIX_FMT_RGBA64BE:
92 case PIX_FMT_GRAY16BE:
93 case PIX_FMT_GRAY16LE:
97 if (s->maxval < 65535)
100 case PIX_FMT_MONOWHITE:
101 case PIX_FMT_MONOBLACK:
102 n = (avctx->width + 7) >> 3;
108 linesize = p->linesize[0];
109 if (s->bytestream + n * avctx->height > s->bytestream_end)
111 if(s->type < 4 || (is_mono && s->type==7)){
112 for (i=0; i<avctx->height; i++) {
114 init_put_bits(&pb, ptr, linesize);
115 for(j=0; j<avctx->width * components; j++){
119 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
121 if(s->bytestream >= s->bytestream_end)
124 /* read a single digit */
125 v = (*s->bytestream++)&1;
127 /* read a sequence of digits */
130 c = (*s->bytestream++) - '0';
133 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
139 for (i = 0; i < avctx->height; i++) {
141 memcpy(ptr, s->bytestream, n);
142 else if (upgrade == 1) {
143 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
144 for (j = 0; j < n; j++)
145 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
146 } else if (upgrade == 2) {
147 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
148 for (j = 0; j < n / 2; j++) {
149 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
150 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
158 case PIX_FMT_YUV420P:
160 unsigned char *ptr1, *ptr2;
164 linesize = p->linesize[0];
165 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
167 for (i = 0; i < avctx->height; i++) {
168 memcpy(ptr, s->bytestream, n);
175 h = avctx->height >> 1;
176 for (i = 0; i < h; i++) {
177 memcpy(ptr1, s->bytestream, n);
179 memcpy(ptr2, s->bytestream, n);
181 ptr1 += p->linesize[1];
182 ptr2 += p->linesize[2];
187 *picture = s->picture;
188 *data_size = sizeof(AVPicture);
190 return s->bytestream - s->bytestream_start;
194 #if CONFIG_PGM_DECODER
195 AVCodec ff_pgm_decoder = {
197 .type = AVMEDIA_TYPE_VIDEO,
198 .id = AV_CODEC_ID_PGM,
199 .priv_data_size = sizeof(PNMContext),
202 .decode = pnm_decode_frame,
203 .capabilities = CODEC_CAP_DR1,
204 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
208 #if CONFIG_PGMYUV_DECODER
209 AVCodec ff_pgmyuv_decoder = {
211 .type = AVMEDIA_TYPE_VIDEO,
212 .id = AV_CODEC_ID_PGMYUV,
213 .priv_data_size = sizeof(PNMContext),
216 .decode = pnm_decode_frame,
217 .capabilities = CODEC_CAP_DR1,
218 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
222 #if CONFIG_PPM_DECODER
223 AVCodec ff_ppm_decoder = {
225 .type = AVMEDIA_TYPE_VIDEO,
226 .id = AV_CODEC_ID_PPM,
227 .priv_data_size = sizeof(PNMContext),
230 .decode = pnm_decode_frame,
231 .capabilities = CODEC_CAP_DR1,
232 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
236 #if CONFIG_PBM_DECODER
237 AVCodec ff_pbm_decoder = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .id = AV_CODEC_ID_PBM,
241 .priv_data_size = sizeof(PNMContext),
244 .decode = pnm_decode_frame,
245 .capabilities = CODEC_CAP_DR1,
246 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
250 #if CONFIG_PAM_DECODER
251 AVCodec ff_pam_decoder = {
253 .type = AVMEDIA_TYPE_VIDEO,
254 .id = AV_CODEC_ID_PAM,
255 .priv_data_size = sizeof(PNMContext),
258 .decode = pnm_decode_frame,
259 .capabilities = CODEC_CAP_DR1,
260 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),