libavcodec/pnmdec.c
Go to the documentation of this file.
00001 /*
00002  * PNM image format
00003  * Copyright (c) 2002, 2003 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "put_bits.h"
00024 #include "pnm.h"
00025 
00026 
00027 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
00028                             int *data_size, AVPacket *avpkt)
00029 {
00030     const uint8_t *buf   = avpkt->data;
00031     int buf_size         = avpkt->size;
00032     PNMContext * const s = avctx->priv_data;
00033     AVFrame *picture     = data;
00034     AVFrame * const p    = (AVFrame*)&s->picture;
00035     int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
00036     unsigned char *ptr;
00037     int components, sample_len;
00038 
00039     s->bytestream_start =
00040     s->bytestream       = buf;
00041     s->bytestream_end   = buf + buf_size;
00042 
00043     if (ff_pnm_decode_header(avctx, s) < 0)
00044         return -1;
00045 
00046     if (p->data[0])
00047         avctx->release_buffer(avctx, p);
00048 
00049     p->reference = 0;
00050     if (avctx->get_buffer(avctx, p) < 0) {
00051         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00052         return -1;
00053     }
00054     p->pict_type = AV_PICTURE_TYPE_I;
00055     p->key_frame = 1;
00056 
00057     switch (avctx->pix_fmt) {
00058     default:
00059         return -1;
00060     case PIX_FMT_RGBA64BE:
00061         n = avctx->width * 8;
00062         components=4;
00063         sample_len=16;
00064         goto do_read;
00065     case PIX_FMT_RGB48BE:
00066         n = avctx->width * 6;
00067         components=3;
00068         sample_len=16;
00069         goto do_read;
00070     case PIX_FMT_RGBA:
00071         n = avctx->width * 4;
00072         components=4;
00073         sample_len=8;
00074         goto do_read;
00075     case PIX_FMT_RGB24:
00076         n = avctx->width * 3;
00077         components=3;
00078         sample_len=8;
00079         goto do_read;
00080     case PIX_FMT_GRAY8:
00081         n = avctx->width;
00082         components=1;
00083         sample_len=8;
00084         if (s->maxval < 255)
00085             upgrade = 1;
00086         goto do_read;
00087     case PIX_FMT_GRAY8A:
00088         n = avctx->width * 2;
00089         components=2;
00090         sample_len=8;
00091         goto do_read;
00092     case PIX_FMT_GRAY16BE:
00093     case PIX_FMT_GRAY16LE:
00094         n = avctx->width * 2;
00095         components=1;
00096         sample_len=16;
00097         if (s->maxval < 65535)
00098             upgrade = 2;
00099         goto do_read;
00100     case PIX_FMT_MONOWHITE:
00101     case PIX_FMT_MONOBLACK:
00102         n = (avctx->width + 7) >> 3;
00103         components=1;
00104         sample_len=1;
00105         is_mono = 1;
00106     do_read:
00107         ptr      = p->data[0];
00108         linesize = p->linesize[0];
00109         if (s->bytestream + n * avctx->height > s->bytestream_end)
00110             return -1;
00111         if(s->type < 4){
00112             for (i=0; i<avctx->height; i++) {
00113                 PutBitContext pb;
00114                 init_put_bits(&pb, ptr, linesize);
00115                 for(j=0; j<avctx->width * components; j++){
00116                     unsigned int c=0;
00117                     int v=0;
00118                     while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
00119                         s->bytestream++;
00120                     if(s->bytestream >= s->bytestream_end)
00121                         return -1;
00122                     if (is_mono) {
00123                         /* read a single digit */
00124                         v = (*s->bytestream++) - '0';
00125                     } else {
00126                         /* read a sequence of digits */
00127                         do {
00128                             v = 10*v + c;
00129                             c = (*s->bytestream++) - '0';
00130                         } while (c <= 9);
00131                     }
00132                     put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
00133                 }
00134                 flush_put_bits(&pb);
00135                 ptr+= linesize;
00136             }
00137         }else{
00138         for (i = 0; i < avctx->height; i++) {
00139             if (!upgrade)
00140                 memcpy(ptr, s->bytestream, n);
00141             else if (upgrade == 1) {
00142                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
00143                 for (j = 0; j < n; j++)
00144                     ptr[j] = (s->bytestream[j] * f + 64) >> 7;
00145             } else if (upgrade == 2) {
00146                 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
00147                 for (j = 0; j < n / 2; j++) {
00148                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
00149                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
00150                 }
00151             }
00152             s->bytestream += n;
00153             ptr           += linesize;
00154         }
00155         }
00156         break;
00157     case PIX_FMT_YUV420P:
00158         {
00159             unsigned char *ptr1, *ptr2;
00160 
00161             n        = avctx->width;
00162             ptr      = p->data[0];
00163             linesize = p->linesize[0];
00164             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
00165                 return -1;
00166             for (i = 0; i < avctx->height; i++) {
00167                 memcpy(ptr, s->bytestream, n);
00168                 s->bytestream += n;
00169                 ptr           += linesize;
00170             }
00171             ptr1 = p->data[1];
00172             ptr2 = p->data[2];
00173             n >>= 1;
00174             h = avctx->height >> 1;
00175             for (i = 0; i < h; i++) {
00176                 memcpy(ptr1, s->bytestream, n);
00177                 s->bytestream += n;
00178                 memcpy(ptr2, s->bytestream, n);
00179                 s->bytestream += n;
00180                 ptr1 += p->linesize[1];
00181                 ptr2 += p->linesize[2];
00182             }
00183         }
00184         break;
00185     }
00186     *picture   = *(AVFrame*)&s->picture;
00187     *data_size = sizeof(AVPicture);
00188 
00189     return s->bytestream - s->bytestream_start;
00190 }
00191 
00192 
00193 #if CONFIG_PGM_DECODER
00194 AVCodec ff_pgm_decoder = {
00195     .name           = "pgm",
00196     .type           = AVMEDIA_TYPE_VIDEO,
00197     .id             = CODEC_ID_PGM,
00198     .priv_data_size = sizeof(PNMContext),
00199     .init           = ff_pnm_init,
00200     .close          = ff_pnm_end,
00201     .decode         = pnm_decode_frame,
00202     .capabilities   = CODEC_CAP_DR1,
00203     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
00204     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00205 };
00206 #endif
00207 
00208 #if CONFIG_PGMYUV_DECODER
00209 AVCodec ff_pgmyuv_decoder = {
00210     .name           = "pgmyuv",
00211     .type           = AVMEDIA_TYPE_VIDEO,
00212     .id             = CODEC_ID_PGMYUV,
00213     .priv_data_size = sizeof(PNMContext),
00214     .init           = ff_pnm_init,
00215     .close          = ff_pnm_end,
00216     .decode         = pnm_decode_frame,
00217     .capabilities   = CODEC_CAP_DR1,
00218     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00219     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00220 };
00221 #endif
00222 
00223 #if CONFIG_PPM_DECODER
00224 AVCodec ff_ppm_decoder = {
00225     .name           = "ppm",
00226     .type           = AVMEDIA_TYPE_VIDEO,
00227     .id             = CODEC_ID_PPM,
00228     .priv_data_size = sizeof(PNMContext),
00229     .init           = ff_pnm_init,
00230     .close          = ff_pnm_end,
00231     .decode         = pnm_decode_frame,
00232     .capabilities   = CODEC_CAP_DR1,
00233     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
00234     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
00235 };
00236 #endif
00237 
00238 #if CONFIG_PBM_DECODER
00239 AVCodec ff_pbm_decoder = {
00240     .name           = "pbm",
00241     .type           = AVMEDIA_TYPE_VIDEO,
00242     .id             = CODEC_ID_PBM,
00243     .priv_data_size = sizeof(PNMContext),
00244     .init           = ff_pnm_init,
00245     .close          = ff_pnm_end,
00246     .decode         = pnm_decode_frame,
00247     .capabilities   = CODEC_CAP_DR1,
00248     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00249     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00250 };
00251 #endif
00252 
00253 #if CONFIG_PAM_DECODER
00254 AVCodec ff_pam_decoder = {
00255     .name           = "pam",
00256     .type           = AVMEDIA_TYPE_VIDEO,
00257     .id             = CODEC_ID_PAM,
00258     .priv_data_size = sizeof(PNMContext),
00259     .init           = ff_pnm_init,
00260     .close          = ff_pnm_end,
00261     .decode         = pnm_decode_frame,
00262     .capabilities   = CODEC_CAP_DR1,
00263     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00264     .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
00265 };
00266 #endif