1/* 2 * PAM image format 3 * Copyright (c) 2002, 2003 Fabrice Bellard 4 * 5 * This file is part of FFmpeg. 6 * 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. 11 * 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. 16 * 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 20 */ 21 22#include "libavutil/avassert.h" 23#include "avcodec.h" 24#include "codec_internal.h" 25#include "encode.h" 26 27static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, 28 const AVFrame *p, int *got_packet) 29{ 30 int i, h, w, n, linesize, depth, maxval, ret, header_size; 31 uint8_t *bytestream, *ptr; 32 const char *tuple_type; 33 char header[100]; 34 35 h = avctx->height; 36 w = avctx->width; 37 switch (avctx->pix_fmt) { 38 case AV_PIX_FMT_MONOBLACK: 39 n = w; 40 depth = 1; 41 maxval = 1; 42 tuple_type = "BLACKANDWHITE"; 43 break; 44 case AV_PIX_FMT_GRAY8: 45 n = w; 46 depth = 1; 47 maxval = 255; 48 tuple_type = "GRAYSCALE"; 49 break; 50 case AV_PIX_FMT_GRAY16BE: 51 n = w * 2; 52 depth = 1; 53 maxval = 0xFFFF; 54 tuple_type = "GRAYSCALE"; 55 break; 56 case AV_PIX_FMT_GRAY8A: 57 n = w * 2; 58 depth = 2; 59 maxval = 255; 60 tuple_type = "GRAYSCALE_ALPHA"; 61 break; 62 case AV_PIX_FMT_YA16BE: 63 n = w * 4; 64 depth = 2; 65 maxval = 0xFFFF; 66 tuple_type = "GRAYSCALE_ALPHA"; 67 break; 68 case AV_PIX_FMT_RGB24: 69 n = w * 3; 70 depth = 3; 71 maxval = 255; 72 tuple_type = "RGB"; 73 break; 74 case AV_PIX_FMT_RGBA: 75 n = w * 4; 76 depth = 4; 77 maxval = 255; 78 tuple_type = "RGB_ALPHA"; 79 break; 80 case AV_PIX_FMT_RGB48BE: 81 n = w * 6; 82 depth = 3; 83 maxval = 0xFFFF; 84 tuple_type = "RGB"; 85 break; 86 case AV_PIX_FMT_RGBA64BE: 87 n = w * 8; 88 depth = 4; 89 maxval = 0xFFFF; 90 tuple_type = "RGB_ALPHA"; 91 break; 92 default: 93 return -1; 94 } 95 96 header_size = snprintf(header, sizeof(header), 97 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n", 98 w, h, depth, maxval, tuple_type); 99 av_assert1(header_size < sizeof(header)); 100 101 if ((ret = ff_get_encode_buffer(avctx, pkt, n*h + header_size, 0)) < 0) 102 return ret; 103 104 bytestream = pkt->data; 105 memcpy(bytestream, header, header_size); 106 bytestream += header_size; 107 108 ptr = p->data[0]; 109 linesize = p->linesize[0]; 110 111 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){ 112 int j; 113 for (i = 0; i < h; i++) { 114 for (j = 0; j < w; j++) 115 *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1; 116 ptr += linesize; 117 } 118 } else { 119 for (i = 0; i < h; i++) { 120 memcpy(bytestream, ptr, n); 121 bytestream += n; 122 ptr += linesize; 123 } 124 } 125 126 *got_packet = 1; 127 return 0; 128} 129 130const FFCodec ff_pam_encoder = { 131 .p.name = "pam", 132 .p.long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"), 133 .p.type = AVMEDIA_TYPE_VIDEO, 134 .p.id = AV_CODEC_ID_PAM, 135 .p.capabilities = AV_CODEC_CAP_DR1, 136 FF_CODEC_ENCODE_CB(pam_encode_frame), 137 .p.pix_fmts = (const enum AVPixelFormat[]){ 138 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, 139 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE, 140 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, 141 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE, 142 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE 143 }, 144 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, 145}; 146