Split the decoders from pnmen.c off into their own file.
[libav.git] / libavcodec / pnmenc.c
CommitLineData
9ac2e797
AJ
1/*
2 * PNM image format
406792e7 3 * Copyright (c) 2002, 2003 Fabrice Bellard
9ac2e797
AJ
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 */
d3067047 21
9ac2e797 22#include "avcodec.h"
2c124cb6 23#include "bytestream.h"
9ac2e797
AJ
24#include "pnm.h"
25
26
d3067047
DB
27static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
28 int buf_size, void *data)
29{
30 PNMContext *s = avctx->priv_data;
31 AVFrame *pict = data;
32 AVFrame * const p = (AVFrame*)&s->picture;
9ac2e797
AJ
33 int i, h, h1, c, n, linesize;
34 uint8_t *ptr, *ptr1, *ptr2;
35
d3067047 36 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
9ac2e797
AJ
37 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
38 return -1;
39 }
40
d3067047
DB
41 *p = *pict;
42 p->pict_type = FF_I_TYPE;
43 p->key_frame = 1;
9ac2e797 44
d3067047
DB
45 s->bytestream_start =
46 s->bytestream = outbuf;
47 s->bytestream_end = outbuf + buf_size;
9ac2e797 48
d3067047 49 h = avctx->height;
9ac2e797 50 h1 = h;
d3067047 51 switch (avctx->pix_fmt) {
9ac2e797 52 case PIX_FMT_MONOWHITE:
d3067047
DB
53 c = '4';
54 n = (avctx->width + 7) >> 3;
9ac2e797
AJ
55 break;
56 case PIX_FMT_GRAY8:
d3067047
DB
57 c = '5';
58 n = avctx->width;
9ac2e797
AJ
59 break;
60 case PIX_FMT_GRAY16BE:
d3067047
DB
61 c = '5';
62 n = avctx->width * 2;
9ac2e797
AJ
63 break;
64 case PIX_FMT_RGB24:
d3067047
DB
65 c = '6';
66 n = avctx->width * 3;
9ac2e797 67 break;
00182190 68 case PIX_FMT_RGB48BE:
d3067047
DB
69 c = '6';
70 n = avctx->width * 6;
00182190 71 break;
9ac2e797 72 case PIX_FMT_YUV420P:
d3067047
DB
73 c = '5';
74 n = avctx->width;
9ac2e797
AJ
75 h1 = (h * 3) / 2;
76 break;
77 default:
78 return -1;
79 }
80 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
d3067047 81 "P%c\n%d %d\n", c, avctx->width, h1);
9ac2e797
AJ
82 s->bytestream += strlen(s->bytestream);
83 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
84 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00182190 85 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
9ac2e797
AJ
86 s->bytestream += strlen(s->bytestream);
87 }
88
d3067047 89 ptr = p->data[0];
9ac2e797 90 linesize = p->linesize[0];
d3067047 91 for (i = 0; i < h; i++) {
9ac2e797
AJ
92 memcpy(s->bytestream, ptr, n);
93 s->bytestream += n;
d3067047 94 ptr += linesize;
9ac2e797
AJ
95 }
96
97 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
98 h >>= 1;
99 n >>= 1;
100 ptr1 = p->data[1];
101 ptr2 = p->data[2];
d3067047 102 for (i = 0; i < h; i++) {
9ac2e797
AJ
103 memcpy(s->bytestream, ptr1, n);
104 s->bytestream += n;
105 memcpy(s->bytestream, ptr2, n);
106 s->bytestream += n;
107 ptr1 += p->linesize[1];
108 ptr2 += p->linesize[2];
109 }
110 }
111 return s->bytestream - s->bytestream_start;
112}
113
d3067047
DB
114static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
115 int buf_size, void *data)
116{
117 PNMContext *s = avctx->priv_data;
118 AVFrame *pict = data;
119 AVFrame * const p = (AVFrame*)&s->picture;
9ac2e797
AJ
120 int i, h, w, n, linesize, depth, maxval;
121 const char *tuple_type;
122 uint8_t *ptr;
123
d3067047 124 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
9ac2e797
AJ
125 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
126 return -1;
127 }
128
d3067047
DB
129 *p = *pict;
130 p->pict_type = FF_I_TYPE;
131 p->key_frame = 1;
9ac2e797 132
d3067047
DB
133 s->bytestream_start =
134 s->bytestream = outbuf;
135 s->bytestream_end = outbuf+buf_size;
9ac2e797
AJ
136
137 h = avctx->height;
138 w = avctx->width;
d3067047 139 switch (avctx->pix_fmt) {
9ac2e797 140 case PIX_FMT_MONOWHITE:
d3067047
DB
141 n = (w + 7) >> 3;
142 depth = 1;
143 maxval = 1;
9ac2e797
AJ
144 tuple_type = "BLACKANDWHITE";
145 break;
146 case PIX_FMT_GRAY8:
d3067047
DB
147 n = w;
148 depth = 1;
149 maxval = 255;
9ac2e797
AJ
150 tuple_type = "GRAYSCALE";
151 break;
152 case PIX_FMT_RGB24:
d3067047
DB
153 n = w * 3;
154 depth = 3;
155 maxval = 255;
9ac2e797
AJ
156 tuple_type = "RGB";
157 break;
158 case PIX_FMT_RGB32:
d3067047
DB
159 n = w * 4;
160 depth = 4;
161 maxval = 255;
9ac2e797
AJ
162 tuple_type = "RGB_ALPHA";
163 break;
164 default:
165 return -1;
166 }
167 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
168 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
169 w, h, depth, maxval, tuple_type);
170 s->bytestream += strlen(s->bytestream);
171
d3067047 172 ptr = p->data[0];
9ac2e797
AJ
173 linesize = p->linesize[0];
174
175 if (avctx->pix_fmt == PIX_FMT_RGB32) {
176 int j;
177 unsigned int v;
178
d3067047
DB
179 for (i = 0; i < h; i++) {
180 for (j = 0; j < w; j++) {
9ac2e797 181 v = ((uint32_t *)ptr)[j];
2c124cb6 182 bytestream_put_be24(&s->bytestream, v);
9ac2e797
AJ
183 *s->bytestream++ = v >> 24;
184 }
185 ptr += linesize;
186 }
187 } else {
d3067047 188 for (i = 0; i < h; i++) {
9ac2e797
AJ
189 memcpy(s->bytestream, ptr, n);
190 s->bytestream += n;
d3067047 191 ptr += linesize;
9ac2e797
AJ
192 }
193 }
194 return s->bytestream - s->bytestream_start;
195}
196
9ac2e797 197
b250f9c6 198#if CONFIG_PGM_ENCODER
9ac2e797
AJ
199AVCodec pgm_encoder = {
200 "pgm",
201 CODEC_TYPE_VIDEO,
202 CODEC_ID_PGM,
203 sizeof(PNMContext),
8767a009 204 ff_pnm_init,
9ac2e797 205 pnm_encode_frame,
d3067047
DB
206 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
207 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
9ac2e797 208};
d3067047 209#endif
9ac2e797 210
b250f9c6 211#if CONFIG_PGMYUV_ENCODER
9ac2e797
AJ
212AVCodec pgmyuv_encoder = {
213 "pgmyuv",
214 CODEC_TYPE_VIDEO,
215 CODEC_ID_PGMYUV,
216 sizeof(PNMContext),
8767a009 217 ff_pnm_init,
9ac2e797 218 pnm_encode_frame,
d3067047
DB
219 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
220 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
9ac2e797 221};
d3067047 222#endif
9ac2e797 223
b250f9c6 224#if CONFIG_PPM_ENCODER
9ac2e797
AJ
225AVCodec ppm_encoder = {
226 "ppm",
227 CODEC_TYPE_VIDEO,
228 CODEC_ID_PPM,
229 sizeof(PNMContext),
8767a009 230 ff_pnm_init,
9ac2e797 231 pnm_encode_frame,
d3067047
DB
232 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
233 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
9ac2e797 234};
d3067047 235#endif
9ac2e797 236
b250f9c6 237#if CONFIG_PBM_ENCODER
9ac2e797
AJ
238AVCodec pbm_encoder = {
239 "pbm",
240 CODEC_TYPE_VIDEO,
241 CODEC_ID_PBM,
242 sizeof(PNMContext),
8767a009 243 ff_pnm_init,
9ac2e797 244 pnm_encode_frame,
d3067047
DB
245 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
246 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
9ac2e797 247};
d3067047 248#endif
9ac2e797 249
b250f9c6 250#if CONFIG_PAM_ENCODER
9ac2e797
AJ
251AVCodec pam_encoder = {
252 "pam",
253 CODEC_TYPE_VIDEO,
254 CODEC_ID_PAM,
255 sizeof(PNMContext),
8767a009 256 ff_pnm_init,
9ac2e797 257 pam_encode_frame,
d3067047
DB
258 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
259 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
9ac2e797 260};
d3067047 261#endif