3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
26 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
27 const AVFrame *pict, int *got_packet)
29 uint8_t *bytestream_start, *bytestream, *bytestream_end;
30 const AVFrame * const p = pict;
31 int i, h, w, n, linesize, depth, maxval, ret;
32 const char *tuple_type;
35 if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
37 avctx->height) + 200)) < 0) {
38 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
43 bytestream = pkt->data;
44 bytestream_end = pkt->data + pkt->size;
48 switch (avctx->pix_fmt) {
49 case AV_PIX_FMT_MONOWHITE:
53 tuple_type = "BLACKANDWHITE";
55 case AV_PIX_FMT_GRAY8:
59 tuple_type = "GRAYSCALE";
61 case AV_PIX_FMT_RGB24:
67 case AV_PIX_FMT_RGB32:
71 tuple_type = "RGB_ALPHA";
76 snprintf(bytestream, bytestream_end - bytestream,
77 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
78 w, h, depth, maxval, tuple_type);
79 bytestream += strlen(bytestream);
82 linesize = p->linesize[0];
84 if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
88 for (i = 0; i < h; i++) {
89 for (j = 0; j < w; j++) {
90 v = ((uint32_t *)ptr)[j];
91 bytestream_put_be24(&bytestream, v);
92 *bytestream++ = v >> 24;
97 for (i = 0; i < h; i++) {
98 memcpy(bytestream, ptr, n);
104 pkt->size = bytestream - bytestream_start;
105 pkt->flags |= AV_PKT_FLAG_KEY;
110 static av_cold int pam_encode_init(AVCodecContext *avctx)
112 avctx->coded_frame = av_frame_alloc();
113 if (!avctx->coded_frame)
114 return AVERROR(ENOMEM);
116 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
117 avctx->coded_frame->key_frame = 1;
122 static av_cold int pam_encode_close(AVCodecContext *avctx)
124 av_frame_free(&avctx->coded_frame);
128 AVCodec ff_pam_encoder = {
130 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
131 .type = AVMEDIA_TYPE_VIDEO,
132 .id = AV_CODEC_ID_PAM,
133 .init = pam_encode_init,
134 .close = pam_encode_close,
135 .encode2 = pam_encode_frame,
136 .pix_fmts = (const enum AVPixelFormat[]){
137 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,