libavcodec/pnm.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 "libavutil/imgutils.h"
00023 #include "avcodec.h"
00024 #include "pnm.h"
00025 
00026 static inline int pnm_space(int c)
00027 {
00028     return c == ' ' || c == '\n' || c == '\r' || c == '\t';
00029 }
00030 
00031 static void pnm_get(PNMContext *sc, char *str, int buf_size)
00032 {
00033     char *s;
00034     int c;
00035 
00036     /* skip spaces and comments */
00037     for (;;) {
00038         c = *sc->bytestream++;
00039         if (c == '#')  {
00040             do {
00041                 c = *sc->bytestream++;
00042             } while (c != '\n' && sc->bytestream < sc->bytestream_end);
00043         } else if (!pnm_space(c)) {
00044             break;
00045         }
00046     }
00047 
00048     s = str;
00049     while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
00050         if ((s - str)  < buf_size - 1)
00051             *s++ = c;
00052         c = *sc->bytestream++;
00053     }
00054     *s = '\0';
00055 }
00056 
00057 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
00058 {
00059     char buf1[32], tuple_type[32];
00060     int h, w, depth, maxval;
00061 
00062     pnm_get(s, buf1, sizeof(buf1));
00063     s->type= buf1[1]-'0';
00064     if(buf1[0] != 'P')
00065         return -1;
00066 
00067     if (s->type==1 || s->type==4) {
00068         avctx->pix_fmt = PIX_FMT_MONOWHITE;
00069     } else if (s->type==2 || s->type==5) {
00070         if (avctx->codec_id == CODEC_ID_PGMYUV)
00071             avctx->pix_fmt = PIX_FMT_YUV420P;
00072         else
00073             avctx->pix_fmt = PIX_FMT_GRAY8;
00074     } else if (s->type==3 || s->type==6) {
00075         avctx->pix_fmt = PIX_FMT_RGB24;
00076     } else if (s->type==7) {
00077         w      = -1;
00078         h      = -1;
00079         maxval = -1;
00080         depth  = -1;
00081         tuple_type[0] = '\0';
00082         for (;;) {
00083             pnm_get(s, buf1, sizeof(buf1));
00084             if (!strcmp(buf1, "WIDTH")) {
00085                 pnm_get(s, buf1, sizeof(buf1));
00086                 w = strtol(buf1, NULL, 10);
00087             } else if (!strcmp(buf1, "HEIGHT")) {
00088                 pnm_get(s, buf1, sizeof(buf1));
00089                 h = strtol(buf1, NULL, 10);
00090             } else if (!strcmp(buf1, "DEPTH")) {
00091                 pnm_get(s, buf1, sizeof(buf1));
00092                 depth = strtol(buf1, NULL, 10);
00093             } else if (!strcmp(buf1, "MAXVAL")) {
00094                 pnm_get(s, buf1, sizeof(buf1));
00095                 maxval = strtol(buf1, NULL, 10);
00096             } else if (!strcmp(buf1, "TUPLTYPE") ||
00097                        /* libavcodec used to write invalid files */
00098                        !strcmp(buf1, "TUPLETYPE")) {
00099                 pnm_get(s, tuple_type, sizeof(tuple_type));
00100             } else if (!strcmp(buf1, "ENDHDR")) {
00101                 break;
00102             } else {
00103                 return -1;
00104             }
00105         }
00106         /* check that all tags are present */
00107         if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
00108             return -1;
00109 
00110         avctx->width  = w;
00111         avctx->height = h;
00112         s->maxval     = maxval;
00113         if (depth == 1) {
00114             if (maxval == 1) {
00115                 avctx->pix_fmt = PIX_FMT_MONOWHITE;
00116             } else if (maxval == 255) {
00117                 avctx->pix_fmt = PIX_FMT_GRAY8;
00118             } else {
00119                 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00120             }
00121         } else if (depth == 2) {
00122             if (maxval == 255)
00123                 avctx->pix_fmt = PIX_FMT_GRAY8A;
00124         } else if (depth == 3) {
00125             if (maxval < 256) {
00126             avctx->pix_fmt = PIX_FMT_RGB24;
00127             } else {
00128                 avctx->pix_fmt = PIX_FMT_RGB48BE;
00129             }
00130         } else if (depth == 4) {
00131             if (maxval < 256) {
00132                 avctx->pix_fmt = PIX_FMT_RGBA;
00133             } else {
00134                 avctx->pix_fmt = PIX_FMT_RGBA64BE;
00135             }
00136         } else {
00137             return -1;
00138         }
00139         return 0;
00140     } else {
00141         return -1;
00142     }
00143     pnm_get(s, buf1, sizeof(buf1));
00144     avctx->width = atoi(buf1);
00145     if (avctx->width <= 0)
00146         return -1;
00147     pnm_get(s, buf1, sizeof(buf1));
00148     avctx->height = atoi(buf1);
00149     if(avctx->height <= 0 || av_image_check_size(avctx->width, avctx->height, 0, avctx))
00150         return -1;
00151     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
00152         pnm_get(s, buf1, sizeof(buf1));
00153         s->maxval = atoi(buf1);
00154         if (s->maxval <= 0) {
00155             av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
00156             s->maxval = 255;
00157         }
00158         if (s->maxval >= 256) {
00159             if (avctx->pix_fmt == PIX_FMT_GRAY8) {
00160                 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00161                 if (s->maxval != 65535)
00162                     avctx->pix_fmt = PIX_FMT_GRAY16;
00163             } else if (avctx->pix_fmt == PIX_FMT_RGB24) {
00164                 if (s->maxval > 255)
00165                     avctx->pix_fmt = PIX_FMT_RGB48BE;
00166             } else {
00167                 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
00168                 avctx->pix_fmt = PIX_FMT_NONE;
00169                 return -1;
00170             }
00171         }
00172     }else
00173         s->maxval=1;
00174     /* more check if YUV420 */
00175     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
00176         if ((avctx->width & 1) != 0)
00177             return -1;
00178         h = (avctx->height * 2);
00179         if ((h % 3) != 0)
00180             return -1;
00181         h /= 3;
00182         avctx->height = h;
00183     }
00184     return 0;
00185 }
00186 
00187 av_cold int ff_pnm_end(AVCodecContext *avctx)
00188 {
00189     PNMContext *s = avctx->priv_data;
00190 
00191     if (s->picture.data[0])
00192         avctx->release_buffer(avctx, &s->picture);
00193 
00194     return 0;
00195 }
00196 
00197 av_cold int ff_pnm_init(AVCodecContext *avctx)
00198 {
00199     PNMContext *s = avctx->priv_data;
00200 
00201     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00202     avctx->coded_frame = (AVFrame*)&s->picture;
00203 
00204     return 0;
00205 }