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