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 *p, int *got_packet)
30 PNMContext *s = avctx->priv_data;
31 int i, h, w, n, linesize, depth, maxval, ret;
32 const char *tuple_type;
37 switch (avctx->pix_fmt) {
38 case AV_PIX_FMT_MONOBLACK:
42 tuple_type = "BLACKANDWHITE";
44 case AV_PIX_FMT_GRAY8:
48 tuple_type = "GRAYSCALE";
50 case AV_PIX_FMT_GRAY16BE:
54 tuple_type = "GRAYSCALE";
56 case AV_PIX_FMT_GRAY8A:
60 tuple_type = "GRAYSCALE_ALPHA";
62 case AV_PIX_FMT_RGB24:
72 tuple_type = "RGB_ALPHA";
74 case AV_PIX_FMT_RGB48BE:
80 case AV_PIX_FMT_RGBA64BE:
84 tuple_type = "RGB_ALPHA";
90 if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
94 s->bytestream = pkt->data;
95 s->bytestream_end = pkt->data + pkt->size;
97 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
98 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
99 w, h, depth, maxval, tuple_type);
100 s->bytestream += strlen(s->bytestream);
103 linesize = p->linesize[0];
105 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
107 for (i = 0; i < h; i++) {
108 for (j = 0; j < w; j++)
109 *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
113 for (i = 0; i < h; i++) {
114 memcpy(s->bytestream, ptr, n);
120 pkt->size = s->bytestream - s->bytestream_start;
121 pkt->flags |= AV_PKT_FLAG_KEY;
127 AVCodec ff_pam_encoder = {
129 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
130 .type = AVMEDIA_TYPE_VIDEO,
131 .id = AV_CODEC_ID_PAM,
132 .priv_data_size = sizeof(PNMContext),
133 .encode2 = pam_encode_frame,
134 .pix_fmts = (const enum AVPixelFormat[]){
135 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE