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
25 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
26 const AVFrame *p, int *got_packet)
28 uint8_t *bytestream_start, *bytestream, *bytestream_end;
29 int i, h, w, n, linesize, depth, maxval, ret;
30 const char *tuple_type;
35 switch (avctx->pix_fmt) {
36 case AV_PIX_FMT_MONOBLACK:
40 tuple_type = "BLACKANDWHITE";
42 case AV_PIX_FMT_GRAY8:
46 tuple_type = "GRAYSCALE";
48 case AV_PIX_FMT_GRAY16BE:
52 tuple_type = "GRAYSCALE";
54 case AV_PIX_FMT_GRAY8A:
58 tuple_type = "GRAYSCALE_ALPHA";
60 case AV_PIX_FMT_RGB24:
70 tuple_type = "RGB_ALPHA";
72 case AV_PIX_FMT_RGB48BE:
78 case AV_PIX_FMT_RGBA64BE:
82 tuple_type = "RGB_ALPHA";
88 if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
92 bytestream = pkt->data;
93 bytestream_end = pkt->data + pkt->size;
95 snprintf(bytestream, bytestream_end - bytestream,
96 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
97 w, h, depth, maxval, tuple_type);
98 bytestream += strlen(bytestream);
101 linesize = p->linesize[0];
103 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
105 for (i = 0; i < h; i++) {
106 for (j = 0; j < w; j++)
107 *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
111 for (i = 0; i < h; i++) {
112 memcpy(bytestream, ptr, n);
118 pkt->size = bytestream - bytestream_start;
119 pkt->flags |= AV_PKT_FLAG_KEY;
124 static av_cold int pam_encode_init(AVCodecContext *avctx)
126 avctx->coded_frame = av_frame_alloc();
127 if (!avctx->coded_frame)
128 return AVERROR(ENOMEM);
130 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
131 avctx->coded_frame->key_frame = 1;
136 static av_cold int pam_encode_close(AVCodecContext *avctx)
138 av_frame_free(&avctx->coded_frame);
142 AVCodec ff_pam_encoder = {
144 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
145 .type = AVMEDIA_TYPE_VIDEO,
146 .id = AV_CODEC_ID_PAM,
147 .init = pam_encode_init,
148 .close = pam_encode_close,
149 .encode2 = pam_encode_frame,
150 .pix_fmts = (const enum AVPixelFormat[]){
151 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