The encoders in pnmenc.c now depend on the init/end functions in pnm.c.
[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_decode_frame(AVCodecContext *avctx, void *data,
28 int *data_size, AVPacket *avpkt)
29 {
30 const uint8_t *buf = avpkt->data;
31 int buf_size = avpkt->size;
32 PNMContext * const s = avctx->priv_data;
33 AVFrame *picture = data;
34 AVFrame * const p = (AVFrame*)&s->picture;
35 int i, n, linesize, h, upgrade = 0;
36 unsigned char *ptr;
37
38 s->bytestream_start =
39 s->bytestream = buf;
40 s->bytestream_end = buf + buf_size;
41
42 if (ff_pnm_decode_header(avctx, s) < 0)
43 return -1;
44
45 if (p->data[0])
46 avctx->release_buffer(avctx, p);
47
48 p->reference = 0;
49 if (avctx->get_buffer(avctx, p) < 0) {
50 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
51 return -1;
52 }
53 p->pict_type = FF_I_TYPE;
54 p->key_frame = 1;
55
56 switch (avctx->pix_fmt) {
57 default:
58 return -1;
59 case PIX_FMT_RGB48BE:
60 n = avctx->width * 6;
61 goto do_read;
62 case PIX_FMT_RGB24:
63 n = avctx->width * 3;
64 goto do_read;
65 case PIX_FMT_GRAY8:
66 n = avctx->width;
67 if (s->maxval < 255)
68 upgrade = 1;
69 goto do_read;
70 case PIX_FMT_GRAY16BE:
71 case PIX_FMT_GRAY16LE:
72 n = avctx->width * 2;
73 if (s->maxval < 65535)
74 upgrade = 2;
75 goto do_read;
76 case PIX_FMT_MONOWHITE:
77 case PIX_FMT_MONOBLACK:
78 n = (avctx->width + 7) >> 3;
79 do_read:
80 ptr = p->data[0];
81 linesize = p->linesize[0];
82 if (s->bytestream + n * avctx->height > s->bytestream_end)
83 return -1;
84 for (i = 0; i < avctx->height; i++) {
85 if (!upgrade)
86 memcpy(ptr, s->bytestream, n);
87 else if (upgrade == 1) {
88 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
89 for (j = 0; j < n; j++)
90 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
91 } else if (upgrade == 2) {
92 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
93 for (j = 0; j < n / 2; j++) {
94 v = be2me_16(((uint16_t *)s->bytestream)[j]);
95 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
96 }
97 }
98 s->bytestream += n;
99 ptr += linesize;
100 }
101 break;
102 case PIX_FMT_YUV420P:
103 {
104 unsigned char *ptr1, *ptr2;
105
106 n = avctx->width;
107 ptr = p->data[0];
108 linesize = p->linesize[0];
109 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
110 return -1;
111 for (i = 0; i < avctx->height; i++) {
112 memcpy(ptr, s->bytestream, n);
113 s->bytestream += n;
114 ptr += linesize;
115 }
116 ptr1 = p->data[1];
117 ptr2 = p->data[2];
118 n >>= 1;
119 h = avctx->height >> 1;
120 for (i = 0; i < h; i++) {
121 memcpy(ptr1, s->bytestream, n);
122 s->bytestream += n;
123 memcpy(ptr2, s->bytestream, n);
124 s->bytestream += n;
125 ptr1 += p->linesize[1];
126 ptr2 += p->linesize[2];
127 }
128 }
129 break;
130 case PIX_FMT_RGB32:
131 ptr = p->data[0];
132 linesize = p->linesize[0];
133 if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
134 return -1;
135 for (i = 0; i < avctx->height; i++) {
136 int j, r, g, b, a;
137
138 for (j = 0; j < avctx->width; j++) {
139 r = *s->bytestream++;
140 g = *s->bytestream++;
141 b = *s->bytestream++;
142 a = *s->bytestream++;
143 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
144 }
145 ptr += linesize;
146 }
147 break;
148 }
149 *picture = *(AVFrame*)&s->picture;
150 *data_size = sizeof(AVPicture);
151
152 return s->bytestream - s->bytestream_start;
153 }
154
155 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
156 int buf_size, void *data)
157 {
158 PNMContext *s = avctx->priv_data;
159 AVFrame *pict = data;
160 AVFrame * const p = (AVFrame*)&s->picture;
161 int i, h, h1, c, n, linesize;
162 uint8_t *ptr, *ptr1, *ptr2;
163
164 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
165 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
166 return -1;
167 }
168
169 *p = *pict;
170 p->pict_type = FF_I_TYPE;
171 p->key_frame = 1;
172
173 s->bytestream_start =
174 s->bytestream = outbuf;
175 s->bytestream_end = outbuf + buf_size;
176
177 h = avctx->height;
178 h1 = h;
179 switch (avctx->pix_fmt) {
180 case PIX_FMT_MONOWHITE:
181 c = '4';
182 n = (avctx->width + 7) >> 3;
183 break;
184 case PIX_FMT_GRAY8:
185 c = '5';
186 n = avctx->width;
187 break;
188 case PIX_FMT_GRAY16BE:
189 c = '5';
190 n = avctx->width * 2;
191 break;
192 case PIX_FMT_RGB24:
193 c = '6';
194 n = avctx->width * 3;
195 break;
196 case PIX_FMT_RGB48BE:
197 c = '6';
198 n = avctx->width * 6;
199 break;
200 case PIX_FMT_YUV420P:
201 c = '5';
202 n = avctx->width;
203 h1 = (h * 3) / 2;
204 break;
205 default:
206 return -1;
207 }
208 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
209 "P%c\n%d %d\n", c, avctx->width, h1);
210 s->bytestream += strlen(s->bytestream);
211 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
212 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
213 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
214 s->bytestream += strlen(s->bytestream);
215 }
216
217 ptr = p->data[0];
218 linesize = p->linesize[0];
219 for (i = 0; i < h; i++) {
220 memcpy(s->bytestream, ptr, n);
221 s->bytestream += n;
222 ptr += linesize;
223 }
224
225 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
226 h >>= 1;
227 n >>= 1;
228 ptr1 = p->data[1];
229 ptr2 = p->data[2];
230 for (i = 0; i < h; i++) {
231 memcpy(s->bytestream, ptr1, n);
232 s->bytestream += n;
233 memcpy(s->bytestream, ptr2, n);
234 s->bytestream += n;
235 ptr1 += p->linesize[1];
236 ptr2 += p->linesize[2];
237 }
238 }
239 return s->bytestream - s->bytestream_start;
240 }
241
242 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
243 int buf_size, void *data)
244 {
245 PNMContext *s = avctx->priv_data;
246 AVFrame *pict = data;
247 AVFrame * const p = (AVFrame*)&s->picture;
248 int i, h, w, n, linesize, depth, maxval;
249 const char *tuple_type;
250 uint8_t *ptr;
251
252 if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
253 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
254 return -1;
255 }
256
257 *p = *pict;
258 p->pict_type = FF_I_TYPE;
259 p->key_frame = 1;
260
261 s->bytestream_start =
262 s->bytestream = outbuf;
263 s->bytestream_end = outbuf+buf_size;
264
265 h = avctx->height;
266 w = avctx->width;
267 switch (avctx->pix_fmt) {
268 case PIX_FMT_MONOWHITE:
269 n = (w + 7) >> 3;
270 depth = 1;
271 maxval = 1;
272 tuple_type = "BLACKANDWHITE";
273 break;
274 case PIX_FMT_GRAY8:
275 n = w;
276 depth = 1;
277 maxval = 255;
278 tuple_type = "GRAYSCALE";
279 break;
280 case PIX_FMT_RGB24:
281 n = w * 3;
282 depth = 3;
283 maxval = 255;
284 tuple_type = "RGB";
285 break;
286 case PIX_FMT_RGB32:
287 n = w * 4;
288 depth = 4;
289 maxval = 255;
290 tuple_type = "RGB_ALPHA";
291 break;
292 default:
293 return -1;
294 }
295 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
296 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
297 w, h, depth, maxval, tuple_type);
298 s->bytestream += strlen(s->bytestream);
299
300 ptr = p->data[0];
301 linesize = p->linesize[0];
302
303 if (avctx->pix_fmt == PIX_FMT_RGB32) {
304 int j;
305 unsigned int v;
306
307 for (i = 0; i < h; i++) {
308 for (j = 0; j < w; j++) {
309 v = ((uint32_t *)ptr)[j];
310 bytestream_put_be24(&s->bytestream, v);
311 *s->bytestream++ = v >> 24;
312 }
313 ptr += linesize;
314 }
315 } else {
316 for (i = 0; i < h; i++) {
317 memcpy(s->bytestream, ptr, n);
318 s->bytestream += n;
319 ptr += linesize;
320 }
321 }
322 return s->bytestream - s->bytestream_start;
323 }
324
325
326 #if CONFIG_PGM_DECODER
327 AVCodec pgm_decoder = {
328 "pgm",
329 CODEC_TYPE_VIDEO,
330 CODEC_ID_PGM,
331 sizeof(PNMContext),
332 ff_pnm_init,
333 NULL,
334 ff_pnm_end,
335 pnm_decode_frame,
336 CODEC_CAP_DR1,
337 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
338 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
339 };
340 #endif
341
342 #if CONFIG_PGM_ENCODER
343 AVCodec pgm_encoder = {
344 "pgm",
345 CODEC_TYPE_VIDEO,
346 CODEC_ID_PGM,
347 sizeof(PNMContext),
348 ff_pnm_init,
349 pnm_encode_frame,
350 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
351 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
352 };
353 #endif
354
355 #if CONFIG_PGMYUV_DECODER
356 AVCodec pgmyuv_decoder = {
357 "pgmyuv",
358 CODEC_TYPE_VIDEO,
359 CODEC_ID_PGMYUV,
360 sizeof(PNMContext),
361 ff_pnm_init,
362 NULL,
363 ff_pnm_end,
364 pnm_decode_frame,
365 CODEC_CAP_DR1,
366 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
367 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
368 };
369 #endif
370
371 #if CONFIG_PGMYUV_ENCODER
372 AVCodec pgmyuv_encoder = {
373 "pgmyuv",
374 CODEC_TYPE_VIDEO,
375 CODEC_ID_PGMYUV,
376 sizeof(PNMContext),
377 ff_pnm_init,
378 pnm_encode_frame,
379 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
380 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
381 };
382 #endif
383
384 #if CONFIG_PPM_DECODER
385 AVCodec ppm_decoder = {
386 "ppm",
387 CODEC_TYPE_VIDEO,
388 CODEC_ID_PPM,
389 sizeof(PNMContext),
390 ff_pnm_init,
391 NULL,
392 ff_pnm_end,
393 pnm_decode_frame,
394 CODEC_CAP_DR1,
395 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
396 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
397 };
398 #endif
399
400 #if CONFIG_PPM_ENCODER
401 AVCodec ppm_encoder = {
402 "ppm",
403 CODEC_TYPE_VIDEO,
404 CODEC_ID_PPM,
405 sizeof(PNMContext),
406 ff_pnm_init,
407 pnm_encode_frame,
408 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
409 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
410 };
411 #endif
412
413 #if CONFIG_PBM_DECODER
414 AVCodec pbm_decoder = {
415 "pbm",
416 CODEC_TYPE_VIDEO,
417 CODEC_ID_PBM,
418 sizeof(PNMContext),
419 ff_pnm_init,
420 NULL,
421 ff_pnm_end,
422 pnm_decode_frame,
423 CODEC_CAP_DR1,
424 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
425 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
426 };
427 #endif
428
429 #if CONFIG_PBM_ENCODER
430 AVCodec pbm_encoder = {
431 "pbm",
432 CODEC_TYPE_VIDEO,
433 CODEC_ID_PBM,
434 sizeof(PNMContext),
435 ff_pnm_init,
436 pnm_encode_frame,
437 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
438 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
439 };
440 #endif
441
442 #if CONFIG_PAM_DECODER
443 AVCodec pam_decoder = {
444 "pam",
445 CODEC_TYPE_VIDEO,
446 CODEC_ID_PAM,
447 sizeof(PNMContext),
448 ff_pnm_init,
449 NULL,
450 ff_pnm_end,
451 pnm_decode_frame,
452 CODEC_CAP_DR1,
453 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
454 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
455 };
456 #endif
457
458 #if CONFIG_PAM_ENCODER
459 AVCodec pam_encoder = {
460 "pam",
461 CODEC_TYPE_VIDEO,
462 CODEC_ID_PAM,
463 sizeof(PNMContext),
464 ff_pnm_init,
465 pam_encode_frame,
466 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
467 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
468 };
469 #endif