libavcodec/pnm.c
5b6d5596
 /*
  * PNM image format
406792e7
  * Copyright (c) 2002, 2003 Fabrice Bellard
5b6d5596
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
5b6d5596
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
5b6d5596
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
5b6d5596
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5b6d5596
  */
d3067047
 
7ffe76e5
 #include "libavutil/imgutils.h"
5b6d5596
 #include "avcodec.h"
426a189b
 #include "pnm.h"
5b6d5596
 
9ac2e797
 static inline int pnm_space(int c)
 {
ccd425e7
     return c == ' ' || c == '\n' || c == '\r' || c == '\t';
5b6d5596
 }
 
9ac2e797
 static void pnm_get(PNMContext *sc, char *str, int buf_size)
99f06236
 {
9ac2e797
     char *s;
     int c;
 
     /* skip spaces and comments */
d3067047
     for (;;) {
9ac2e797
         c = *sc->bytestream++;
         if (c == '#')  {
d3067047
             do {
9ac2e797
                 c = *sc->bytestream++;
             } while (c != '\n' && sc->bytestream < sc->bytestream_end);
         } else if (!pnm_space(c)) {
             break;
         }
5b6d5596
     }
115329f1
 
9ac2e797
     s = str;
     while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
         if ((s - str)  < buf_size - 1)
             *s++ = c;
         c = *sc->bytestream++;
     }
     *s = '\0';
 }
5b6d5596
 
d3067047
 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
 {
9ac2e797
     char buf1[32], tuple_type[32];
     int h, w, depth, maxval;
 
     pnm_get(s, buf1, sizeof(buf1));
63538a96
     s->type= buf1[1]-'0';
     if(buf1[0] != 'P')
         return -1;
 
     if (s->type==1 || s->type==4) {
9ac2e797
         avctx->pix_fmt = PIX_FMT_MONOWHITE;
63538a96
     } else if (s->type==2 || s->type==5) {
9ac2e797
         if (avctx->codec_id == CODEC_ID_PGMYUV)
             avctx->pix_fmt = PIX_FMT_YUV420P;
         else
             avctx->pix_fmt = PIX_FMT_GRAY8;
63538a96
     } else if (s->type==3 || s->type==6) {
9ac2e797
         avctx->pix_fmt = PIX_FMT_RGB24;
63538a96
     } else if (s->type==7) {
d3067047
         w      = -1;
         h      = -1;
9ac2e797
         maxval = -1;
d3067047
         depth  = -1;
9ac2e797
         tuple_type[0] = '\0';
d3067047
         for (;;) {
9ac2e797
             pnm_get(s, buf1, sizeof(buf1));
             if (!strcmp(buf1, "WIDTH")) {
                 pnm_get(s, buf1, sizeof(buf1));
                 w = strtol(buf1, NULL, 10);
             } else if (!strcmp(buf1, "HEIGHT")) {
                 pnm_get(s, buf1, sizeof(buf1));
                 h = strtol(buf1, NULL, 10);
             } else if (!strcmp(buf1, "DEPTH")) {
                 pnm_get(s, buf1, sizeof(buf1));
                 depth = strtol(buf1, NULL, 10);
             } else if (!strcmp(buf1, "MAXVAL")) {
                 pnm_get(s, buf1, sizeof(buf1));
                 maxval = strtol(buf1, NULL, 10);
c1bc1967
             } else if (!strcmp(buf1, "TUPLTYPE") ||
66b9d706
                        /* libavcodec used to write invalid files */
c1bc1967
                        !strcmp(buf1, "TUPLETYPE")) {
9ac2e797
                 pnm_get(s, tuple_type, sizeof(tuple_type));
             } else if (!strcmp(buf1, "ENDHDR")) {
                 break;
             } else {
1cd89f27
                 return -1;
5b6d5596
             }
         }
9ac2e797
         /* check that all tags are present */
e16f217c
         if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
1cd89f27
             return -1;
311c7008
 
d3067047
         avctx->width  = w;
9ac2e797
         avctx->height = h;
190a0998
         s->maxval     = maxval;
9ac2e797
         if (depth == 1) {
871e2f4f
             if (maxval == 1) {
d9b3097b
                 avctx->pix_fmt = PIX_FMT_MONOBLACK;
19b945dc
             } else if (maxval == 255) {
9ac2e797
                 avctx->pix_fmt = PIX_FMT_GRAY8;
19b945dc
             } else {
871e2f4f
                 avctx->pix_fmt = PIX_FMT_GRAY16BE;
             }
328e7932
         } else if (depth == 2) {
             if (maxval == 255)
                 avctx->pix_fmt = PIX_FMT_GRAY8A;
9ac2e797
         } else if (depth == 3) {
0605f5c8
             if (maxval < 256) {
9ac2e797
             avctx->pix_fmt = PIX_FMT_RGB24;
0605f5c8
             } else {
a44b63f6
                 avctx->pix_fmt = PIX_FMT_RGB48BE;
0605f5c8
             }
9ac2e797
         } else if (depth == 4) {
febf75f3
             if (maxval < 256) {
b614c147
                 avctx->pix_fmt = PIX_FMT_RGBA;
febf75f3
             } else {
00430075
                 avctx->pix_fmt = PIX_FMT_RGBA64BE;
febf75f3
             }
9ac2e797
         } else {
             return -1;
311c7008
         }
9ac2e797
         return 0;
     } else {
0ecca7a4
         return -1;
     }
9ac2e797
     pnm_get(s, buf1, sizeof(buf1));
     avctx->width = atoi(buf1);
     if (avctx->width <= 0)
         return -1;
     pnm_get(s, buf1, sizeof(buf1));
     avctx->height = atoi(buf1);
0257ac8f
     if(avctx->height <= 0 || av_image_check_size(avctx->width, avctx->height, 0, avctx))
5b6d5596
         return -1;
d9b3097b
     if (avctx->pix_fmt != PIX_FMT_MONOWHITE && avctx->pix_fmt != PIX_FMT_MONOBLACK) {
9ac2e797
         pnm_get(s, buf1, sizeof(buf1));
         s->maxval = atoi(buf1);
ebc34883
         if (s->maxval <= 0) {
             av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
8219782a
             s->maxval = 255;
ebc34883
         }
0605f5c8
         if (s->maxval >= 256) {
             if (avctx->pix_fmt == PIX_FMT_GRAY8) {
8fd64adf
                 avctx->pix_fmt = PIX_FMT_GRAY16BE;
                 if (s->maxval != 65535)
                     avctx->pix_fmt = PIX_FMT_GRAY16;
5a92cc66
             } else if (avctx->pix_fmt == PIX_FMT_RGB24) {
00182190
                 if (s->maxval > 255)
                     avctx->pix_fmt = PIX_FMT_RGB48BE;
0605f5c8
             } else {
00182190
                 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
0605f5c8
                 avctx->pix_fmt = PIX_FMT_NONE;
                 return -1;
             }
5b6d5596
         }
63538a96
     }else
         s->maxval=1;
9ac2e797
     /* more check if YUV420 */
     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
         if ((avctx->width & 1) != 0)
             return -1;
         h = (avctx->height * 2);
         if ((h % 3) != 0)
             return -1;
         h /= 3;
         avctx->height = h;
5b6d5596
     }
9ac2e797
     return 0;
5b6d5596
 }
8767a009
 
 av_cold int ff_pnm_end(AVCodecContext *avctx)
 {
     PNMContext *s = avctx->priv_data;
 
     if (s->picture.data[0])
         avctx->release_buffer(avctx, &s->picture);
 
     return 0;
 }
 
 av_cold int ff_pnm_init(AVCodecContext *avctx)
 {
     PNMContext *s = avctx->priv_data;
 
562b6c74
     avcodec_get_frame_defaults(&s->picture);
     avctx->coded_frame = &s->picture;
8767a009
 
     return 0;
 }