libavcodec/pnmenc.c
9ac2e797
 /*
  * PNM image format
406792e7
  * Copyright (c) 2002, 2003 Fabrice Bellard
9ac2e797
  *
  * 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
  */
d3067047
 
9ac2e797
 #include "avcodec.h"
2c124cb6
 #include "bytestream.h"
9ac2e797
 #include "pnm.h"
 
 
d3067047
 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
                             int buf_size, void *data)
 {
     PNMContext *s     = avctx->priv_data;
     AVFrame *pict     = data;
     AVFrame * const p = (AVFrame*)&s->picture;
9ac2e797
     int i, h, h1, c, n, linesize;
     uint8_t *ptr, *ptr1, *ptr2;
 
d3067047
     if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
9ac2e797
         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
         return -1;
     }
 
d3067047
     *p           = *pict;
ce5e49b0
     p->pict_type = AV_PICTURE_TYPE_I;
d3067047
     p->key_frame = 1;
9ac2e797
 
d3067047
     s->bytestream_start =
     s->bytestream       = outbuf;
     s->bytestream_end   = outbuf + buf_size;
9ac2e797
 
d3067047
     h  = avctx->height;
9ac2e797
     h1 = h;
d3067047
     switch (avctx->pix_fmt) {
9ac2e797
     case PIX_FMT_MONOWHITE:
d3067047
         c  = '4';
         n  = (avctx->width + 7) >> 3;
9ac2e797
         break;
     case PIX_FMT_GRAY8:
d3067047
         c  = '5';
         n  = avctx->width;
9ac2e797
         break;
     case PIX_FMT_GRAY16BE:
d3067047
         c  = '5';
         n  = avctx->width * 2;
9ac2e797
         break;
     case PIX_FMT_RGB24:
d3067047
         c  = '6';
         n  = avctx->width * 3;
9ac2e797
         break;
00182190
     case PIX_FMT_RGB48BE:
d3067047
         c  = '6';
         n  = avctx->width * 6;
00182190
         break;
9ac2e797
     case PIX_FMT_YUV420P:
d3067047
         c  = '5';
         n  = avctx->width;
9ac2e797
         h1 = (h * 3) / 2;
         break;
     default:
         return -1;
     }
     snprintf(s->bytestream, s->bytestream_end - s->bytestream,
d3067047
              "P%c\n%d %d\n", c, avctx->width, h1);
9ac2e797
     s->bytestream += strlen(s->bytestream);
     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
         snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00182190
                  "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
9ac2e797
         s->bytestream += strlen(s->bytestream);
     }
 
d3067047
     ptr      = p->data[0];
9ac2e797
     linesize = p->linesize[0];
d3067047
     for (i = 0; i < h; i++) {
9ac2e797
         memcpy(s->bytestream, ptr, n);
         s->bytestream += n;
d3067047
         ptr           += linesize;
9ac2e797
     }
 
     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
         h >>= 1;
         n >>= 1;
         ptr1 = p->data[1];
         ptr2 = p->data[2];
d3067047
         for (i = 0; i < h; i++) {
9ac2e797
             memcpy(s->bytestream, ptr1, n);
             s->bytestream += n;
             memcpy(s->bytestream, ptr2, n);
             s->bytestream += n;
                 ptr1 += p->linesize[1];
                 ptr2 += p->linesize[2];
         }
     }
     return s->bytestream - s->bytestream_start;
 }
 
 
b250f9c6
 #if CONFIG_PGM_ENCODER
e7e2df27
 AVCodec ff_pgm_encoder = {
ec6402b7
     .name           = "pgm",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_PGM,
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .encode         = pnm_encode_frame,
d3067047
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PGMYUV_ENCODER
e7e2df27
 AVCodec ff_pgmyuv_encoder = {
ec6402b7
     .name           = "pgmyuv",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_PGMYUV,
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .encode         = pnm_encode_frame,
d3067047
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PPM_ENCODER
e7e2df27
 AVCodec ff_ppm_encoder = {
ec6402b7
     .name           = "ppm",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_PPM,
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .encode         = pnm_encode_frame,
d3067047
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PBM_ENCODER
e7e2df27
 AVCodec ff_pbm_encoder = {
ec6402b7
     .name           = "pbm",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_PBM,
     .priv_data_size = sizeof(PNMContext),
     .init           = ff_pnm_init,
     .encode         = pnm_encode_frame,
d3067047
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
9ac2e797
 };
d3067047
 #endif