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
 
b5f536d2
 #include "libavutil/pixdesc.h"
9ac2e797
 #include "avcodec.h"
1ea57550
 #include "internal.h"
9ac2e797
 
1ea57550
 static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
a5155294
                             const AVFrame *p, int *got_packet)
d3067047
 {
afa21a12
     uint8_t *bytestream, *bytestream_start, *bytestream_end;
1ea57550
     int i, h, h1, c, n, linesize, ret;
9ac2e797
     uint8_t *ptr, *ptr1, *ptr2;
 
6a74c48c
     if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt,
1ea57550
                                                        avctx->width,
ae2c33b0
                                                        avctx->height) + 200)) < 0)
1ea57550
         return ret;
9ac2e797
 
afa21a12
     bytestream_start =
     bytestream       = pkt->data;
     bytestream_end   = pkt->data + pkt->size;
9ac2e797
 
d3067047
     h  = avctx->height;
9ac2e797
     h1 = h;
d3067047
     switch (avctx->pix_fmt) {
716d413c
     case AV_PIX_FMT_MONOWHITE:
d3067047
         c  = '4';
         n  = (avctx->width + 7) >> 3;
9ac2e797
         break;
716d413c
     case AV_PIX_FMT_GRAY8:
d3067047
         c  = '5';
         n  = avctx->width;
9ac2e797
         break;
716d413c
     case AV_PIX_FMT_GRAY16BE:
d3067047
         c  = '5';
         n  = avctx->width * 2;
9ac2e797
         break;
716d413c
     case AV_PIX_FMT_RGB24:
d3067047
         c  = '6';
         n  = avctx->width * 3;
9ac2e797
         break;
716d413c
     case AV_PIX_FMT_RGB48BE:
d3067047
         c  = '6';
         n  = avctx->width * 6;
00182190
         break;
716d413c
     case AV_PIX_FMT_YUV420P:
6ddb03ca
         if (avctx->width & 1 || avctx->height & 1) {
             av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
a41340f8
             return AVERROR(EINVAL);
         }
d3067047
         c  = '5';
         n  = avctx->width;
9ac2e797
         h1 = (h * 3) / 2;
         break;
b5f536d2
     case AV_PIX_FMT_YUV420P16BE:
         c  = '5';
         n  = avctx->width * 2;
         h1 = (h * 3) / 2;
         break;
9ac2e797
     default:
         return -1;
     }
afa21a12
     snprintf(bytestream, bytestream_end - bytestream,
d3067047
              "P%c\n%d %d\n", c, avctx->width, h1);
afa21a12
     bytestream += strlen(bytestream);
716d413c
     if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
5ecf8189
         int maxdepth = (1 << (av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1)) - 1;
afa21a12
         snprintf(bytestream, bytestream_end - bytestream,
b5f536d2
                  "%d\n", maxdepth);
afa21a12
         bytestream += strlen(bytestream);
9ac2e797
     }
 
d3067047
     ptr      = p->data[0];
9ac2e797
     linesize = p->linesize[0];
d3067047
     for (i = 0; i < h; i++) {
afa21a12
         memcpy(bytestream, ptr, n);
         bytestream += n;
         ptr        += linesize;
9ac2e797
     }
 
b5f536d2
     if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
9ac2e797
         h >>= 1;
         n >>= 1;
         ptr1 = p->data[1];
         ptr2 = p->data[2];
d3067047
         for (i = 0; i < h; i++) {
afa21a12
             memcpy(bytestream, ptr1, n);
             bytestream += n;
             memcpy(bytestream, ptr2, n);
             bytestream += n;
9ac2e797
                 ptr1 += p->linesize[1];
                 ptr2 += p->linesize[2];
         }
     }
afa21a12
     pkt->size   = bytestream - bytestream_start;
1ea57550
     pkt->flags |= AV_PKT_FLAG_KEY;
     *got_packet = 1;
 
     return 0;
9ac2e797
 }
 
afa21a12
 static av_cold int pnm_encode_init(AVCodecContext *avctx)
 {
     avctx->coded_frame = av_frame_alloc();
     if (!avctx->coded_frame)
         return AVERROR(ENOMEM);
 
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
     avctx->coded_frame->key_frame = 1;
 
     return 0;
 }
 
 static av_cold int pnm_encode_close(AVCodecContext *avctx)
 {
     av_frame_free(&avctx->coded_frame);
     return 0;
 }
9ac2e797
 
b250f9c6
 #if CONFIG_PGM_ENCODER
e7e2df27
 AVCodec ff_pgm_encoder = {
ec6402b7
     .name           = "pgm",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PGM,
afa21a12
     .init           = pnm_encode_init,
     .close          = pnm_encode_close,
1ea57550
     .encode2        = pnm_encode_frame,
716d413c
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE
00c3b67b
     },
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PGMYUV_ENCODER
e7e2df27
 AVCodec ff_pgmyuv_encoder = {
ec6402b7
     .name           = "pgmyuv",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PGMYUV,
afa21a12
     .init           = pnm_encode_init,
     .close          = pnm_encode_close,
1ea57550
     .encode2        = pnm_encode_frame,
b5f536d2
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE
     },
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PPM_ENCODER
e7e2df27
 AVCodec ff_ppm_encoder = {
ec6402b7
     .name           = "ppm",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PPM,
afa21a12
     .init           = pnm_encode_init,
     .close          = pnm_encode_close,
1ea57550
     .encode2        = pnm_encode_frame,
716d413c
     .pix_fmts       = (const enum AVPixelFormat[]){
         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE
00c3b67b
     },
9ac2e797
 };
d3067047
 #endif
9ac2e797
 
b250f9c6
 #if CONFIG_PBM_ENCODER
e7e2df27
 AVCodec ff_pbm_encoder = {
ec6402b7
     .name           = "pbm",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_PBM,
afa21a12
     .init           = pnm_encode_init,
     .close          = pnm_encode_close,
1ea57550
     .encode2        = pnm_encode_frame,
716d413c
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
                                                   AV_PIX_FMT_NONE },
9ac2e797
 };
d3067047
 #endif