libavcodec/pnmdec.c
318888f8
 /*
  * PNM image format
  * Copyright (c) 2002, 2003 Fabrice Bellard
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * 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
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "avcodec.h"
594d4d5d
 #include "internal.h"
63538a96
 #include "put_bits.h"
318888f8
 #include "pnm.h"
 
 
 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
df9b9567
                             int *got_frame, AVPacket *avpkt)
318888f8
 {
     const uint8_t *buf   = avpkt->data;
     int buf_size         = avpkt->size;
     PNMContext * const s = avctx->priv_data;
     AVFrame *picture     = data;
562b6c74
     AVFrame * const p    = &s->picture;
4a745b41
     int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
318888f8
     unsigned char *ptr;
e2820d99
     int components, sample_len, ret;
318888f8
 
     s->bytestream_start =
07cdd394
     s->bytestream       = (uint8_t *)buf;
     s->bytestream_end   = (uint8_t *)buf + buf_size;
318888f8
 
380242ca
     if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
         return ret;
318888f8
 
     if (p->data[0])
         avctx->release_buffer(avctx, p);
 
     p->reference = 0;
874c5b02
     if ((ret = ff_get_buffer(avctx, p)) < 0) {
318888f8
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
e2820d99
         return ret;
318888f8
     }
ce5e49b0
     p->pict_type = AV_PICTURE_TYPE_I;
318888f8
     p->key_frame = 1;
 
     switch (avctx->pix_fmt) {
     default:
380242ca
         return AVERROR(EINVAL);
ac627b3d
     case AV_PIX_FMT_RGBA64BE:
00430075
         n = avctx->width * 8;
         components=4;
         sample_len=16;
         goto do_read;
716d413c
     case AV_PIX_FMT_RGB48BE:
318888f8
         n = avctx->width * 6;
63538a96
         components=3;
         sample_len=16;
318888f8
         goto do_read;
ac627b3d
     case AV_PIX_FMT_RGBA:
b614c147
         n = avctx->width * 4;
         components=4;
         sample_len=8;
         goto do_read;
716d413c
     case AV_PIX_FMT_RGB24:
318888f8
         n = avctx->width * 3;
63538a96
         components=3;
         sample_len=8;
318888f8
         goto do_read;
716d413c
     case AV_PIX_FMT_GRAY8:
318888f8
         n = avctx->width;
63538a96
         components=1;
         sample_len=8;
318888f8
         if (s->maxval < 255)
             upgrade = 1;
         goto do_read;
ac627b3d
     case AV_PIX_FMT_GRAY8A:
328e7932
         n = avctx->width * 2;
         components=2;
         sample_len=8;
         goto do_read;
716d413c
     case AV_PIX_FMT_GRAY16BE:
     case AV_PIX_FMT_GRAY16LE:
318888f8
         n = avctx->width * 2;
63538a96
         components=1;
         sample_len=16;
318888f8
         if (s->maxval < 65535)
             upgrade = 2;
         goto do_read;
716d413c
     case AV_PIX_FMT_MONOWHITE:
     case AV_PIX_FMT_MONOBLACK:
318888f8
         n = (avctx->width + 7) >> 3;
63538a96
         components=1;
         sample_len=1;
4a745b41
         is_mono = 1;
318888f8
     do_read:
         ptr      = p->data[0];
         linesize = p->linesize[0];
         if (s->bytestream + n * avctx->height > s->bytestream_end)
e2820d99
             return AVERROR_INVALIDDATA;
d9b3097b
         if(s->type < 4 || (is_mono && s->type==7)){
63538a96
             for (i=0; i<avctx->height; i++) {
                 PutBitContext pb;
                 init_put_bits(&pb, ptr, linesize);
                 for(j=0; j<avctx->width * components; j++){
                     unsigned int c=0;
                     int v=0;
d9b3097b
                     if(s->type < 4)
63538a96
                     while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
                         s->bytestream++;
                     if(s->bytestream >= s->bytestream_end)
e2820d99
                         return AVERROR_INVALIDDATA;
4a745b41
                     if (is_mono) {
                         /* read a single digit */
d9b3097b
                         v = (*s->bytestream++)&1;
4a745b41
                     } else {
554d3ad7
                         /* read a sequence of digits */
                         do {
                             v = 10*v + c;
                             c = (*s->bytestream++) - '0';
                         } while (c <= 9);
4a745b41
                     }
63538a96
                     put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
                 }
                 flush_put_bits(&pb);
                 ptr+= linesize;
             }
         }else{
318888f8
         for (i = 0; i < avctx->height; i++) {
             if (!upgrade)
                 memcpy(ptr, s->bytestream, n);
             else if (upgrade == 1) {
                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
                 for (j = 0; j < n; j++)
                     ptr[j] = (s->bytestream[j] * f + 64) >> 7;
             } else if (upgrade == 2) {
                 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
                 for (j = 0; j < n / 2; j++) {
8fc0162a
                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
318888f8
                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
                 }
             }
             s->bytestream += n;
             ptr           += linesize;
         }
63538a96
         }
318888f8
         break;
716d413c
     case AV_PIX_FMT_YUV420P:
b5f536d2
     case AV_PIX_FMT_YUV420P9BE:
     case AV_PIX_FMT_YUV420P10BE:
318888f8
         {
             unsigned char *ptr1, *ptr2;
 
             n        = avctx->width;
             ptr      = p->data[0];
             linesize = p->linesize[0];
b5f536d2
             if (s->maxval >= 256)
                 n *= 2;
318888f8
             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
e2820d99
                 return AVERROR_INVALIDDATA;
318888f8
             for (i = 0; i < avctx->height; i++) {
                 memcpy(ptr, s->bytestream, n);
                 s->bytestream += n;
                 ptr           += linesize;
             }
             ptr1 = p->data[1];
             ptr2 = p->data[2];
             n >>= 1;
             h = avctx->height >> 1;
             for (i = 0; i < h; i++) {
                 memcpy(ptr1, s->bytestream, n);
                 s->bytestream += n;
                 memcpy(ptr2, s->bytestream, n);
                 s->bytestream += n;
                 ptr1 += p->linesize[1];
                 ptr2 += p->linesize[2];
             }
         }
         break;
b5f536d2
     case AV_PIX_FMT_YUV420P16:
         {
             uint16_t *ptr1, *ptr2;
             const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
             unsigned int j, v;
 
             n        = avctx->width * 2;
             ptr      = p->data[0];
             linesize = p->linesize[0];
             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
                 return AVERROR_INVALIDDATA;
             for (i = 0; i < avctx->height; i++) {
                 for (j = 0; j < n / 2; j++) {
                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                     ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
                 }
                 s->bytestream += n;
                 ptr           += linesize;
             }
             ptr1 = (uint16_t*)p->data[1];
             ptr2 = (uint16_t*)p->data[2];
             n >>= 1;
             h = avctx->height >> 1;
             for (i = 0; i < h; i++) {
                 for (j = 0; j < n / 2; j++) {
                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                     ptr1[j] = (v * f + 16384) >> 15;
                 }
                 s->bytestream += n;
 
                 for (j = 0; j < n / 2; j++) {
                     v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
                     ptr2[j] = (v * f + 16384) >> 15;
                 }
                 s->bytestream += n;
 
                 ptr1 += p->linesize[1] / 2;
                 ptr2 += p->linesize[2] / 2;
             }
         }
         break;
318888f8
     }
324deaa2
     *picture   = s->picture;
df9b9567
     *got_frame = 1;
318888f8
 
     return s->bytestream - s->bytestream_start;
 }
 
 
 #if CONFIG_PGM_DECODER
e7e2df27
 AVCodec ff_pgm_decoder = {
ec6402b7
     .name           = "pgm",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PGM,
ec6402b7
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
318888f8
 };
 #endif
 
 #if CONFIG_PGMYUV_DECODER
e7e2df27
 AVCodec ff_pgmyuv_decoder = {
ec6402b7
     .name           = "pgmyuv",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PGMYUV,
ec6402b7
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
318888f8
 };
 #endif
 
 #if CONFIG_PPM_DECODER
e7e2df27
 AVCodec ff_ppm_decoder = {
ec6402b7
     .name           = "ppm",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PPM,
ec6402b7
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
318888f8
 };
 #endif
 
 #if CONFIG_PBM_DECODER
e7e2df27
 AVCodec ff_pbm_decoder = {
ec6402b7
     .name           = "pbm",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PBM,
ec6402b7
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
318888f8
 };
 #endif
 
 #if CONFIG_PAM_DECODER
e7e2df27
 AVCodec ff_pam_decoder = {
ec6402b7
     .name           = "pam",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PAM,
ec6402b7
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
318888f8
 };
 #endif