3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
28 const AVFrame *pict, int *got_packet)
30 PNMContext *s = avctx->priv_data;
31 AVFrame * const p = &s->picture;
32 int i, h, w, n, linesize, depth, maxval, ret;
33 const char *tuple_type;
38 switch (avctx->pix_fmt) {
39 case PIX_FMT_MONOBLACK:
43 tuple_type = "BLACKANDWHITE";
49 tuple_type = "GRAYSCALE";
51 case PIX_FMT_GRAY16BE:
55 tuple_type = "GRAYSCALE";
61 tuple_type = "GRAYSCALE_ALPHA";
73 tuple_type = "RGB_ALPHA";
81 case PIX_FMT_RGBA64BE:
85 tuple_type = "RGB_ALPHA";
91 if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
95 p->pict_type = AV_PICTURE_TYPE_I;
99 s->bytestream = pkt->data;
100 s->bytestream_end = pkt->data + pkt->size;
102 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
103 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
104 w, h, depth, maxval, tuple_type);
105 s->bytestream += strlen(s->bytestream);
108 linesize = p->linesize[0];
110 if (avctx->pix_fmt == PIX_FMT_MONOBLACK){
112 for (i = 0; i < h; i++) {
113 for (j = 0; j < w; j++)
114 *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
118 for (i = 0; i < h; i++) {
119 memcpy(s->bytestream, ptr, n);
125 pkt->size = s->bytestream - s->bytestream_start;
126 pkt->flags |= AV_PKT_FLAG_KEY;
132 AVCodec ff_pam_encoder = {
134 .type = AVMEDIA_TYPE_VIDEO,
135 .id = AV_CODEC_ID_PAM,
136 .priv_data_size = sizeof(PNMContext),
138 .encode2 = pam_encode_frame,
139 .pix_fmts = (const enum PixelFormat[]){
140 PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE, PIX_FMT_GRAY8, PIX_FMT_GRAY8A, PIX_FMT_GRAY16BE, PIX_FMT_MONOBLACK, PIX_FMT_NONE
142 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),