3 * Copyright (c) 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "parser.h" //for ParseContext
24 typedef struct PNMContext {
26 uint8_t *bytestream_start;
27 uint8_t *bytestream_end;
29 int maxval; ///< maximum value of a pixel
32 static inline int pnm_space(int c)
34 return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
37 static void pnm_get(PNMContext *sc, char *str, int buf_size)
42 /* skip spaces and comments */
44 c = *sc->bytestream++;
47 c = *sc->bytestream++;
48 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
49 } else if (!pnm_space(c)) {
55 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
56 if ((s - str) < buf_size - 1)
58 c = *sc->bytestream++;
63 static int common_init(AVCodecContext *avctx){
64 PNMContext *s = avctx->priv_data;
66 avcodec_get_frame_defaults((AVFrame*)&s->picture);
67 avctx->coded_frame= (AVFrame*)&s->picture;
72 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
73 char buf1[32], tuple_type[32];
74 int h, w, depth, maxval;
76 pnm_get(s, buf1, sizeof(buf1));
77 if (!strcmp(buf1, "P4")) {
78 avctx->pix_fmt = PIX_FMT_MONOWHITE;
79 } else if (!strcmp(buf1, "P5")) {
80 if (avctx->codec_id == CODEC_ID_PGMYUV)
81 avctx->pix_fmt = PIX_FMT_YUV420P;
83 avctx->pix_fmt = PIX_FMT_GRAY8;
84 } else if (!strcmp(buf1, "P6")) {
85 avctx->pix_fmt = PIX_FMT_RGB24;
86 } else if (!strcmp(buf1, "P7")) {
93 pnm_get(s, buf1, sizeof(buf1));
94 if (!strcmp(buf1, "WIDTH")) {
95 pnm_get(s, buf1, sizeof(buf1));
96 w = strtol(buf1, NULL, 10);
97 } else if (!strcmp(buf1, "HEIGHT")) {
98 pnm_get(s, buf1, sizeof(buf1));
99 h = strtol(buf1, NULL, 10);
100 } else if (!strcmp(buf1, "DEPTH")) {
101 pnm_get(s, buf1, sizeof(buf1));
102 depth = strtol(buf1, NULL, 10);
103 } else if (!strcmp(buf1, "MAXVAL")) {
104 pnm_get(s, buf1, sizeof(buf1));
105 maxval = strtol(buf1, NULL, 10);
106 } else if (!strcmp(buf1, "TUPLETYPE")) {
107 pnm_get(s, tuple_type, sizeof(tuple_type));
108 } else if (!strcmp(buf1, "ENDHDR")) {
114 /* check that all tags are present */
115 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
122 avctx->pix_fmt = PIX_FMT_MONOWHITE;
124 avctx->pix_fmt = PIX_FMT_GRAY8;
125 } else if (depth == 3) {
126 avctx->pix_fmt = PIX_FMT_RGB24;
127 } else if (depth == 4) {
128 avctx->pix_fmt = PIX_FMT_RGB32;
136 pnm_get(s, buf1, sizeof(buf1));
137 avctx->width = atoi(buf1);
138 if (avctx->width <= 0)
140 pnm_get(s, buf1, sizeof(buf1));
141 avctx->height = atoi(buf1);
142 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
144 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
145 pnm_get(s, buf1, sizeof(buf1));
146 s->maxval = atoi(buf1);
147 if(s->maxval >= 256 && avctx->pix_fmt == PIX_FMT_GRAY8) {
148 avctx->pix_fmt = PIX_FMT_GRAY16BE;
149 if (s->maxval != 65535)
150 avctx->pix_fmt = PIX_FMT_GRAY16;
153 /* more check if YUV420 */
154 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
155 if ((avctx->width & 1) != 0)
157 h = (avctx->height * 2);
166 static int pnm_decode_frame(AVCodecContext *avctx,
167 void *data, int *data_size,
168 uint8_t *buf, int buf_size)
170 PNMContext * const s = avctx->priv_data;
171 AVFrame *picture = data;
172 AVFrame * const p= (AVFrame*)&s->picture;
173 int i, n, linesize, h, upgrade = 0;
178 s->bytestream_end= buf + buf_size;
180 if(pnm_decode_header(avctx, s) < 0)
184 avctx->release_buffer(avctx, p);
187 if(avctx->get_buffer(avctx, p) < 0){
188 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
191 p->pict_type= FF_I_TYPE;
194 switch(avctx->pix_fmt) {
198 n = avctx->width * 3;
205 case PIX_FMT_GRAY16BE:
206 case PIX_FMT_GRAY16LE:
207 n = avctx->width * 2;
208 if (s->maxval < 65535)
211 case PIX_FMT_MONOWHITE:
212 case PIX_FMT_MONOBLACK:
213 n = (avctx->width + 7) >> 3;
216 linesize = p->linesize[0];
217 if(s->bytestream + n*avctx->height > s->bytestream_end)
219 for(i = 0; i < avctx->height; i++) {
221 memcpy(ptr, s->bytestream, n);
222 else if (upgrade == 1) {
223 unsigned int j, f = (255*128 + s->maxval/2) / s->maxval;
225 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
226 } else if (upgrade == 2) {
227 unsigned int j, v, f = (65535*32768 + s->maxval/2) / s->maxval;
228 for (j=0; j<n/2; j++) {
229 v = be2me_16(((uint16_t *)s->bytestream)[j]);
230 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
237 case PIX_FMT_YUV420P:
239 unsigned char *ptr1, *ptr2;
243 linesize = p->linesize[0];
244 if(s->bytestream + n*avctx->height*3/2 > s->bytestream_end)
246 for(i = 0; i < avctx->height; i++) {
247 memcpy(ptr, s->bytestream, n);
254 h = avctx->height >> 1;
255 for(i = 0; i < h; i++) {
256 memcpy(ptr1, s->bytestream, n);
258 memcpy(ptr2, s->bytestream, n);
260 ptr1 += p->linesize[1];
261 ptr2 += p->linesize[2];
267 linesize = p->linesize[0];
268 if(s->bytestream + avctx->width*avctx->height*4 > s->bytestream_end)
270 for(i = 0; i < avctx->height; i++) {
273 for(j = 0;j < avctx->width; j++) {
274 r = *s->bytestream++;
275 g = *s->bytestream++;
276 b = *s->bytestream++;
277 a = *s->bytestream++;
278 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
284 *picture= *(AVFrame*)&s->picture;
285 *data_size = sizeof(AVPicture);
287 return s->bytestream - s->bytestream_start;
290 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
291 PNMContext *s = avctx->priv_data;
292 AVFrame *pict = data;
293 AVFrame * const p= (AVFrame*)&s->picture;
294 int i, h, h1, c, n, linesize;
295 uint8_t *ptr, *ptr1, *ptr2;
297 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
298 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
303 p->pict_type= FF_I_TYPE;
307 s->bytestream= outbuf;
308 s->bytestream_end= outbuf+buf_size;
312 switch(avctx->pix_fmt) {
313 case PIX_FMT_MONOWHITE:
315 n = (avctx->width + 7) >> 3;
321 case PIX_FMT_GRAY16BE:
323 n = avctx->width * 2;
327 n = avctx->width * 3;
329 case PIX_FMT_YUV420P:
337 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
339 c, avctx->width, h1);
340 s->bytestream += strlen(s->bytestream);
341 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
342 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
343 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE) ? 255 : 65535);
344 s->bytestream += strlen(s->bytestream);
348 linesize = p->linesize[0];
350 memcpy(s->bytestream, ptr, n);
355 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
361 memcpy(s->bytestream, ptr1, n);
363 memcpy(s->bytestream, ptr2, n);
365 ptr1 += p->linesize[1];
366 ptr2 += p->linesize[2];
369 return s->bytestream - s->bytestream_start;
372 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
373 PNMContext *s = avctx->priv_data;
374 AVFrame *pict = data;
375 AVFrame * const p= (AVFrame*)&s->picture;
376 int i, h, w, n, linesize, depth, maxval;
377 const char *tuple_type;
380 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
381 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
386 p->pict_type= FF_I_TYPE;
390 s->bytestream= outbuf;
391 s->bytestream_end= outbuf+buf_size;
395 switch(avctx->pix_fmt) {
396 case PIX_FMT_MONOWHITE:
400 tuple_type = "BLACKANDWHITE";
406 tuple_type = "GRAYSCALE";
418 tuple_type = "RGB_ALPHA";
423 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
424 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
425 w, h, depth, maxval, tuple_type);
426 s->bytestream += strlen(s->bytestream);
429 linesize = p->linesize[0];
431 if (avctx->pix_fmt == PIX_FMT_RGB32) {
437 v = ((uint32_t *)ptr)[j];
438 *s->bytestream++ = v >> 16;
439 *s->bytestream++ = v >> 8;
440 *s->bytestream++ = v;
441 *s->bytestream++ = v >> 24;
447 memcpy(s->bytestream, ptr, n);
452 return s->bytestream - s->bytestream_start;
456 static int pnm_probe(AVProbeData *pd)
458 const char *p = pd->buf;
459 if (pd->buf_size >= 8 &&
461 p[1] >= '4' && p[1] <= '6' &&
463 return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
468 static int pgmyuv_probe(AVProbeData *pd)
470 if (match_ext(pd->filename, "pgmyuv"))
471 return AVPROBE_SCORE_MAX;
476 static int pam_probe(AVProbeData *pd)
478 const char *p = pd->buf;
479 if (pd->buf_size >= 8 &&
483 return AVPROBE_SCORE_MAX;
489 #ifdef CONFIG_PNM_PARSER
490 static int pnm_parse(AVCodecParserContext *s,
491 AVCodecContext *avctx,
492 uint8_t **poutbuf, int *poutbuf_size,
493 const uint8_t *buf, int buf_size)
495 ParseContext *pc = s->priv_data;
499 for(; pc->overread>0; pc->overread--){
500 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
504 pnmctx.bytestream_start=
505 pnmctx.bytestream= pc->buffer;
506 pnmctx.bytestream_end= pc->buffer + pc->index;
508 pnmctx.bytestream_start=
509 pnmctx.bytestream= (uint8_t *) buf; /* casts avoid warnings */
510 pnmctx.bytestream_end= (uint8_t *) buf + buf_size;
512 if(pnm_decode_header(avctx, &pnmctx) < 0){
513 if(pnmctx.bytestream < pnmctx.bytestream_end){
523 if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
524 memcpy(pc->buffer + pc->index, buf, pc->index);
525 pc->index += pc->index;
527 buf_size -= pc->index;
533 next= pnmctx.bytestream - pnmctx.bytestream_start
534 + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
535 if(pnmctx.bytestream_start!=buf)
541 if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
546 *poutbuf = (uint8_t *)buf;
547 *poutbuf_size = buf_size;
551 AVCodecParser pnm_parser = {
552 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
553 sizeof(ParseContext),
558 #endif /* CONFIG_PNM_PARSER */
560 #ifdef CONFIG_PGM_ENCODER
561 AVCodec pgm_encoder = {
570 .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, -1},
572 #endif // CONFIG_PGM_ENCODER
574 #ifdef CONFIG_PGMYUV_ENCODER
575 AVCodec pgmyuv_encoder = {
584 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
586 #endif // CONFIG_PGMYUV_ENCODER
588 #ifdef CONFIG_PPM_ENCODER
589 AVCodec ppm_encoder = {
598 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
600 #endif // CONFIG_PPM_ENCODER
602 #ifdef CONFIG_PBM_ENCODER
603 AVCodec pbm_encoder = {
612 .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
614 #endif // CONFIG_PBM_ENCODER
616 #ifdef CONFIG_PAM_ENCODER
617 AVCodec pam_encoder = {
626 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},
628 #endif // CONFIG_PAM_ENCODER