]> git.sesse.net Git - ffmpeg/blob - libavcodec/pamenc.c
Merge commit 'd4c2a3740fb95f952a87ba320d2bf31f126bdf68'
[ffmpeg] / libavcodec / pamenc.c
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 "avcodec.h"
23 #include "internal.h"
24 #include "pnm.h"
25
26
27 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
28                             const AVFrame *p, int *got_packet)
29 {
30     PNMContext *s     = avctx->priv_data;
31     int i, h, w, n, linesize, depth, maxval, ret;
32     const char *tuple_type;
33     uint8_t *ptr;
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_RGB24:
63         n          = w * 3;
64         depth      = 3;
65         maxval     = 255;
66         tuple_type = "RGB";
67         break;
68     case AV_PIX_FMT_RGBA:
69         n          = w * 4;
70         depth      = 4;
71         maxval     = 255;
72         tuple_type = "RGB_ALPHA";
73         break;
74     case AV_PIX_FMT_RGB48BE:
75         n          = w * 6;
76         depth      = 3;
77         maxval     = 0xFFFF;
78         tuple_type = "RGB";
79         break;
80     case AV_PIX_FMT_RGBA64BE:
81         n          = w * 8;
82         depth      = 4;
83         maxval     = 0xFFFF;
84         tuple_type = "RGB_ALPHA";
85         break;
86     default:
87         return -1;
88     }
89
90     if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
91         return ret;
92
93     s->bytestream_start =
94     s->bytestream       = pkt->data;
95     s->bytestream_end   = pkt->data + pkt->size;
96
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);
101
102     ptr      = p->data[0];
103     linesize = p->linesize[0];
104
105     if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
106         int j;
107         for (i = 0; i < h; i++) {
108             for (j = 0; j < w; j++)
109                 *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
110             ptr += linesize;
111         }
112     } else {
113         for (i = 0; i < h; i++) {
114             memcpy(s->bytestream, ptr, n);
115             s->bytestream += n;
116             ptr           += linesize;
117         }
118     }
119
120     pkt->size   = s->bytestream - s->bytestream_start;
121     pkt->flags |= AV_PKT_FLAG_KEY;
122     *got_packet = 1;
123     return 0;
124 }
125
126
127 AVCodec ff_pam_encoder = {
128     .name           = "pam",
129     .type           = AVMEDIA_TYPE_VIDEO,
130     .id             = AV_CODEC_ID_PAM,
131     .priv_data_size = sizeof(PNMContext),
132     .encode2        = pam_encode_frame,
133     .pix_fmts       = (const enum AVPixelFormat[]){
134         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
135     },
136     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
137 };