libavcodec/pnmenc.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 "avcodec.h"
00023 #include "pnm.h"
00024 
00025 
00026 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
00027                             int buf_size, void *data)
00028 {
00029     PNMContext *s     = avctx->priv_data;
00030     AVFrame *pict     = data;
00031     AVFrame * const p = (AVFrame*)&s->picture;
00032     int i, h, h1, c, n, linesize;
00033     uint8_t *ptr, *ptr1, *ptr2;
00034 
00035     if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
00036         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
00037         return -1;
00038     }
00039 
00040     *p           = *pict;
00041     p->pict_type = AV_PICTURE_TYPE_I;
00042     p->key_frame = 1;
00043 
00044     s->bytestream_start =
00045     s->bytestream       = outbuf;
00046     s->bytestream_end   = outbuf + buf_size;
00047 
00048     h  = avctx->height;
00049     h1 = h;
00050     switch (avctx->pix_fmt) {
00051     case PIX_FMT_MONOWHITE:
00052         c  = '4';
00053         n  = (avctx->width + 7) >> 3;
00054         break;
00055     case PIX_FMT_GRAY8:
00056         c  = '5';
00057         n  = avctx->width;
00058         break;
00059     case PIX_FMT_GRAY16BE:
00060         c  = '5';
00061         n  = avctx->width * 2;
00062         break;
00063     case PIX_FMT_RGB24:
00064         c  = '6';
00065         n  = avctx->width * 3;
00066         break;
00067     case PIX_FMT_RGB48BE:
00068         c  = '6';
00069         n  = avctx->width * 6;
00070         break;
00071     case PIX_FMT_YUV420P:
00072         c  = '5';
00073         n  = avctx->width;
00074         h1 = (h * 3) / 2;
00075         break;
00076     default:
00077         return -1;
00078     }
00079     snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00080              "P%c\n%d %d\n", c, avctx->width, h1);
00081     s->bytestream += strlen(s->bytestream);
00082     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
00083         snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00084                  "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
00085         s->bytestream += strlen(s->bytestream);
00086     }
00087 
00088     ptr      = p->data[0];
00089     linesize = p->linesize[0];
00090     for (i = 0; i < h; i++) {
00091         memcpy(s->bytestream, ptr, n);
00092         s->bytestream += n;
00093         ptr           += linesize;
00094     }
00095 
00096     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
00097         h >>= 1;
00098         n >>= 1;
00099         ptr1 = p->data[1];
00100         ptr2 = p->data[2];
00101         for (i = 0; i < h; i++) {
00102             memcpy(s->bytestream, ptr1, n);
00103             s->bytestream += n;
00104             memcpy(s->bytestream, ptr2, n);
00105             s->bytestream += n;
00106                 ptr1 += p->linesize[1];
00107                 ptr2 += p->linesize[2];
00108         }
00109     }
00110     return s->bytestream - s->bytestream_start;
00111 }
00112 
00113 
00114 #if CONFIG_PGM_ENCODER
00115 AVCodec ff_pgm_encoder = {
00116     .name           = "pgm",
00117     .type           = AVMEDIA_TYPE_VIDEO,
00118     .id             = CODEC_ID_PGM,
00119     .priv_data_size = sizeof(PNMContext),
00120     .init           = ff_pnm_init,
00121     .encode         = pnm_encode_frame,
00122     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
00123     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00124 };
00125 #endif
00126 
00127 #if CONFIG_PGMYUV_ENCODER
00128 AVCodec ff_pgmyuv_encoder = {
00129     .name           = "pgmyuv",
00130     .type           = AVMEDIA_TYPE_VIDEO,
00131     .id             = CODEC_ID_PGMYUV,
00132     .priv_data_size = sizeof(PNMContext),
00133     .init           = ff_pnm_init,
00134     .encode         = pnm_encode_frame,
00135     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00136     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00137 };
00138 #endif
00139 
00140 #if CONFIG_PPM_ENCODER
00141 AVCodec ff_ppm_encoder = {
00142     .name           = "ppm",
00143     .type           = AVMEDIA_TYPE_VIDEO,
00144     .id             = CODEC_ID_PPM,
00145     .priv_data_size = sizeof(PNMContext),
00146     .init           = ff_pnm_init,
00147     .encode         = pnm_encode_frame,
00148     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
00149     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
00150 };
00151 #endif
00152 
00153 #if CONFIG_PBM_ENCODER
00154 AVCodec ff_pbm_encoder = {
00155     .name           = "pbm",
00156     .type           = AVMEDIA_TYPE_VIDEO,
00157     .id             = CODEC_ID_PBM,
00158     .priv_data_size = sizeof(PNMContext),
00159     .init           = ff_pnm_init,
00160     .encode         = pnm_encode_frame,
00161     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00162     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00163 };
00164 #endif