• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 "bytestream.h"
00024 #include "put_bits.h"
00025 #include "pnm.h"
00026 
00027 
00028 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
00029                             int *data_size, AVPacket *avpkt)
00030 {
00031     const uint8_t *buf   = avpkt->data;
00032     int buf_size         = avpkt->size;
00033     PNMContext * const s = avctx->priv_data;
00034     AVFrame *picture     = data;
00035     AVFrame * const p    = (AVFrame*)&s->picture;
00036     int i, j, n, linesize, h, upgrade = 0;
00037     unsigned char *ptr;
00038     int components, sample_len;
00039 
00040     s->bytestream_start =
00041     s->bytestream       = buf;
00042     s->bytestream_end   = buf + buf_size;
00043 
00044     if (ff_pnm_decode_header(avctx, s) < 0)
00045         return -1;
00046 
00047     if (p->data[0])
00048         avctx->release_buffer(avctx, p);
00049 
00050     p->reference = 0;
00051     if (avctx->get_buffer(avctx, p) < 0) {
00052         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00053         return -1;
00054     }
00055     p->pict_type = FF_I_TYPE;
00056     p->key_frame = 1;
00057 
00058     switch (avctx->pix_fmt) {
00059     default:
00060         return -1;
00061     case PIX_FMT_RGB48BE:
00062         n = avctx->width * 6;
00063         components=3;
00064         sample_len=16;
00065         goto do_read;
00066     case PIX_FMT_RGB24:
00067         n = avctx->width * 3;
00068         components=3;
00069         sample_len=8;
00070         goto do_read;
00071     case PIX_FMT_GRAY8:
00072         n = avctx->width;
00073         components=1;
00074         sample_len=8;
00075         if (s->maxval < 255)
00076             upgrade = 1;
00077         goto do_read;
00078     case PIX_FMT_GRAY16BE:
00079     case PIX_FMT_GRAY16LE:
00080         n = avctx->width * 2;
00081         components=1;
00082         sample_len=16;
00083         if (s->maxval < 65535)
00084             upgrade = 2;
00085         goto do_read;
00086     case PIX_FMT_MONOWHITE:
00087     case PIX_FMT_MONOBLACK:
00088         n = (avctx->width + 7) >> 3;
00089         components=1;
00090         sample_len=1;
00091     do_read:
00092         ptr      = p->data[0];
00093         linesize = p->linesize[0];
00094         if (s->bytestream + n * avctx->height > s->bytestream_end)
00095             return -1;
00096         if(s->type < 4){
00097             for (i=0; i<avctx->height; i++) {
00098                 PutBitContext pb;
00099                 init_put_bits(&pb, ptr, linesize);
00100                 for(j=0; j<avctx->width * components; j++){
00101                     unsigned int c=0;
00102                     int v=0;
00103                     while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
00104                         s->bytestream++;
00105                     if(s->bytestream >= s->bytestream_end)
00106                         return -1;
00107                     do{
00108                         v= 10*v + c;
00109                         c= (*s->bytestream++) - '0';
00110                     }while(c <= 9);
00111                     put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
00112                 }
00113                 flush_put_bits(&pb);
00114                 ptr+= linesize;
00115             }
00116         }else{
00117         for (i = 0; i < avctx->height; i++) {
00118             if (!upgrade)
00119                 memcpy(ptr, s->bytestream, n);
00120             else if (upgrade == 1) {
00121                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
00122                 for (j = 0; j < n; j++)
00123                     ptr[j] = (s->bytestream[j] * f + 64) >> 7;
00124             } else if (upgrade == 2) {
00125                 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
00126                 for (j = 0; j < n / 2; j++) {
00127                     v = be2me_16(((uint16_t *)s->bytestream)[j]);
00128                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
00129                 }
00130             }
00131             s->bytestream += n;
00132             ptr           += linesize;
00133         }
00134         }
00135         break;
00136     case PIX_FMT_YUV420P:
00137         {
00138             unsigned char *ptr1, *ptr2;
00139 
00140             n        = avctx->width;
00141             ptr      = p->data[0];
00142             linesize = p->linesize[0];
00143             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
00144                 return -1;
00145             for (i = 0; i < avctx->height; i++) {
00146                 memcpy(ptr, s->bytestream, n);
00147                 s->bytestream += n;
00148                 ptr           += linesize;
00149             }
00150             ptr1 = p->data[1];
00151             ptr2 = p->data[2];
00152             n >>= 1;
00153             h = avctx->height >> 1;
00154             for (i = 0; i < h; i++) {
00155                 memcpy(ptr1, s->bytestream, n);
00156                 s->bytestream += n;
00157                 memcpy(ptr2, s->bytestream, n);
00158                 s->bytestream += n;
00159                 ptr1 += p->linesize[1];
00160                 ptr2 += p->linesize[2];
00161             }
00162         }
00163         break;
00164     case PIX_FMT_RGB32:
00165         ptr      = p->data[0];
00166         linesize = p->linesize[0];
00167         if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
00168             return -1;
00169         for (i = 0; i < avctx->height; i++) {
00170             int j, r, g, b, a;
00171 
00172             for (j = 0; j < avctx->width; j++) {
00173                 r = *s->bytestream++;
00174                 g = *s->bytestream++;
00175                 b = *s->bytestream++;
00176                 a = *s->bytestream++;
00177                 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
00178             }
00179             ptr += linesize;
00180         }
00181         break;
00182     }
00183     *picture   = *(AVFrame*)&s->picture;
00184     *data_size = sizeof(AVPicture);
00185 
00186     return s->bytestream - s->bytestream_start;
00187 }
00188 
00189 
00190 #if CONFIG_PGM_DECODER
00191 AVCodec pgm_decoder = {
00192     "pgm",
00193     AVMEDIA_TYPE_VIDEO,
00194     CODEC_ID_PGM,
00195     sizeof(PNMContext),
00196     ff_pnm_init,
00197     NULL,
00198     ff_pnm_end,
00199     pnm_decode_frame,
00200     CODEC_CAP_DR1,
00201     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
00202     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00203 };
00204 #endif
00205 
00206 #if CONFIG_PGMYUV_DECODER
00207 AVCodec pgmyuv_decoder = {
00208     "pgmyuv",
00209     AVMEDIA_TYPE_VIDEO,
00210     CODEC_ID_PGMYUV,
00211     sizeof(PNMContext),
00212     ff_pnm_init,
00213     NULL,
00214     ff_pnm_end,
00215     pnm_decode_frame,
00216     CODEC_CAP_DR1,
00217     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00218     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00219 };
00220 #endif
00221 
00222 #if CONFIG_PPM_DECODER
00223 AVCodec ppm_decoder = {
00224     "ppm",
00225     AVMEDIA_TYPE_VIDEO,
00226     CODEC_ID_PPM,
00227     sizeof(PNMContext),
00228     ff_pnm_init,
00229     NULL,
00230     ff_pnm_end,
00231     pnm_decode_frame,
00232     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 pbm_decoder = {
00240     "pbm",
00241     AVMEDIA_TYPE_VIDEO,
00242     CODEC_ID_PBM,
00243     sizeof(PNMContext),
00244     ff_pnm_init,
00245     NULL,
00246     ff_pnm_end,
00247     pnm_decode_frame,
00248     CODEC_CAP_DR1,
00249     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00250     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00251 };
00252 #endif
00253 
00254 #if CONFIG_PAM_DECODER
00255 AVCodec pam_decoder = {
00256     "pam",
00257     AVMEDIA_TYPE_VIDEO,
00258     CODEC_ID_PAM,
00259     sizeof(PNMContext),
00260     ff_pnm_init,
00261     NULL,
00262     ff_pnm_end,
00263     pnm_decode_frame,
00264     CODEC_CAP_DR1,
00265     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00266     .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
00267 };
00268 #endif

Generated on Fri Sep 16 2011 17:17:41 for FFmpeg by  doxygen 1.7.1