pixfmt: add AV_ prefixes to PIX_FMT_*
[libav.git] / libavcodec / pamenc.c
CommitLineData
638783bf
DB
1/*
2 * PAM image format
3 * Copyright (c) 2002, 2003 Fabrice Bellard
4 *
2912e87a 5 * This file is part of Libav.
638783bf 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
638783bf
DB
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
638783bf
DB
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
638783bf
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "bytestream.h"
e9b380a7 24#include "internal.h"
638783bf
DB
25#include "pnm.h"
26
27
e9b380a7
AK
28static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29 const AVFrame *pict, int *got_packet)
638783bf
DB
30{
31 PNMContext *s = avctx->priv_data;
562b6c74 32 AVFrame * const p = &s->picture;
e9b380a7 33 int i, h, w, n, linesize, depth, maxval, ret;
638783bf
DB
34 const char *tuple_type;
35 uint8_t *ptr;
36
e9b380a7
AK
37 if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
38 avctx->width,
39 avctx->height) + 200)) < 0) {
638783bf 40 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
e9b380a7 41 return ret;
638783bf
DB
42 }
43
44 *p = *pict;
975a1447 45 p->pict_type = AV_PICTURE_TYPE_I;
638783bf
DB
46 p->key_frame = 1;
47
48 s->bytestream_start =
e9b380a7
AK
49 s->bytestream = pkt->data;
50 s->bytestream_end = pkt->data + pkt->size;
638783bf
DB
51
52 h = avctx->height;
53 w = avctx->width;
54 switch (avctx->pix_fmt) {
55 case PIX_FMT_MONOWHITE:
56 n = (w + 7) >> 3;
57 depth = 1;
58 maxval = 1;
59 tuple_type = "BLACKANDWHITE";
60 break;
61 case PIX_FMT_GRAY8:
62 n = w;
63 depth = 1;
64 maxval = 255;
65 tuple_type = "GRAYSCALE";
66 break;
67 case PIX_FMT_RGB24:
68 n = w * 3;
69 depth = 3;
70 maxval = 255;
71 tuple_type = "RGB";
72 break;
73 case 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,
66b9d706 83 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
638783bf
DB
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 == 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 }
e9b380a7
AK
109
110 pkt->size = s->bytestream - s->bytestream_start;
111 pkt->flags |= AV_PKT_FLAG_KEY;
112 *got_packet = 1;
113 return 0;
638783bf
DB
114}
115
116
d36beb3f 117AVCodec ff_pam_encoder = {
ec6402b7
AK
118 .name = "pam",
119 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 120 .id = AV_CODEC_ID_PAM,
ec6402b7
AK
121 .priv_data_size = sizeof(PNMContext),
122 .init = ff_pnm_init,
e9b380a7 123 .encode2 = pam_encode_frame,
00c3b67b
MS
124 .pix_fmts = (const enum PixelFormat[]){
125 PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE,
126 PIX_FMT_NONE
127 },
128 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
638783bf 129};