]> git.sesse.net Git - ffmpeg/blob - libavcodec/pamenc.c
dba47717e2526929675b6daf56010c1d087831d8
[ffmpeg] / libavcodec / pamenc.c
1 /*
2  * PAM image format
3  * Copyright (c) 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "pnm.h"
26
27
28 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29                             const AVFrame *pict, int *got_packet)
30 {
31     PNMContext *s     = avctx->priv_data;
32     AVFrame * const p = &s->picture;
33     int i, h, w, n, linesize, depth, maxval, ret;
34     const char *tuple_type;
35     uint8_t *ptr;
36
37     if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
38                                                        avctx->width,
39                                                        avctx->height) + 200)) < 0) {
40         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
41         return ret;
42     }
43
44     *p           = *pict;
45     p->pict_type = AV_PICTURE_TYPE_I;
46     p->key_frame = 1;
47
48     s->bytestream_start =
49     s->bytestream       = pkt->data;
50     s->bytestream_end   = pkt->data + pkt->size;
51
52     h = avctx->height;
53     w = avctx->width;
54     switch (avctx->pix_fmt) {
55     case AV_PIX_FMT_MONOWHITE:
56         n          = (w + 7) >> 3;
57         depth      = 1;
58         maxval     = 1;
59         tuple_type = "BLACKANDWHITE";
60         break;
61     case AV_PIX_FMT_GRAY8:
62         n          = w;
63         depth      = 1;
64         maxval     = 255;
65         tuple_type = "GRAYSCALE";
66         break;
67     case AV_PIX_FMT_RGB24:
68         n          = w * 3;
69         depth      = 3;
70         maxval     = 255;
71         tuple_type = "RGB";
72         break;
73     case AV_PIX_FMT_RGB32:
74         n          = w * 4;
75         depth      = 4;
76         maxval     = 255;
77         tuple_type = "RGB_ALPHA";
78         break;
79     default:
80         return -1;
81     }
82     snprintf(s->bytestream, s->bytestream_end - s->bytestream,
83              "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
84              w, h, depth, maxval, tuple_type);
85     s->bytestream += strlen(s->bytestream);
86
87     ptr      = p->data[0];
88     linesize = p->linesize[0];
89
90     if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
91         int j;
92         unsigned int v;
93
94         for (i = 0; i < h; i++) {
95             for (j = 0; j < w; j++) {
96                 v = ((uint32_t *)ptr)[j];
97                 bytestream_put_be24(&s->bytestream, v);
98                 *s->bytestream++ = v >> 24;
99             }
100             ptr += linesize;
101         }
102     } else {
103         for (i = 0; i < h; i++) {
104             memcpy(s->bytestream, ptr, n);
105             s->bytestream += n;
106             ptr           += linesize;
107         }
108     }
109
110     pkt->size   = s->bytestream - s->bytestream_start;
111     pkt->flags |= AV_PKT_FLAG_KEY;
112     *got_packet = 1;
113     return 0;
114 }
115
116
117 AVCodec ff_pam_encoder = {
118     .name           = "pam",
119     .type           = AVMEDIA_TYPE_VIDEO,
120     .id             = AV_CODEC_ID_PAM,
121     .priv_data_size = sizeof(PNMContext),
122     .init           = ff_pnm_init,
123     .encode2        = pam_encode_frame,
124     .pix_fmts       = (const enum AVPixelFormat[]){
125         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
126         AV_PIX_FMT_NONE
127     },
128     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
129 };