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;
31 static inline int pnm_space(int c)
33 return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
36 static void pnm_get(PNMContext *sc, char *str, int buf_size)
41 /* skip spaces and comments */
43 c = *sc->bytestream++;
46 c = *sc->bytestream++;
47 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
48 } else if (!pnm_space(c)) {
54 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
55 if ((s - str) < buf_size - 1)
57 c = *sc->bytestream++;
62 static int common_init(AVCodecContext *avctx){
63 PNMContext *s = avctx->priv_data;
65 avcodec_get_frame_defaults((AVFrame*)&s->picture);
66 avctx->coded_frame= (AVFrame*)&s->picture;
71 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
72 char buf1[32], tuple_type[32];
73 int h, w, depth, maxval;
75 pnm_get(s, buf1, sizeof(buf1));
76 if (!strcmp(buf1, "P4")) {
77 avctx->pix_fmt = PIX_FMT_MONOWHITE;
78 } else if (!strcmp(buf1, "P5")) {
79 if (avctx->codec_id == CODEC_ID_PGMYUV)
80 avctx->pix_fmt = PIX_FMT_YUV420P;
82 avctx->pix_fmt = PIX_FMT_GRAY8;
83 } else if (!strcmp(buf1, "P6")) {
84 avctx->pix_fmt = PIX_FMT_RGB24;
85 } else if (!strcmp(buf1, "P7")) {
92 pnm_get(s, buf1, sizeof(buf1));
93 if (!strcmp(buf1, "WIDTH")) {
94 pnm_get(s, buf1, sizeof(buf1));
95 w = strtol(buf1, NULL, 10);
96 } else if (!strcmp(buf1, "HEIGHT")) {
97 pnm_get(s, buf1, sizeof(buf1));
98 h = strtol(buf1, NULL, 10);
99 } else if (!strcmp(buf1, "DEPTH")) {
100 pnm_get(s, buf1, sizeof(buf1));
101 depth = strtol(buf1, NULL, 10);
102 } else if (!strcmp(buf1, "MAXVAL")) {
103 pnm_get(s, buf1, sizeof(buf1));
104 maxval = strtol(buf1, NULL, 10);
105 } else if (!strcmp(buf1, "TUPLETYPE")) {
106 pnm_get(s, tuple_type, sizeof(tuple_type));
107 } else if (!strcmp(buf1, "ENDHDR")) {
113 /* check that all tags are present */
114 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
121 avctx->pix_fmt = PIX_FMT_MONOWHITE;
123 avctx->pix_fmt = PIX_FMT_GRAY8;
124 } else if (depth == 3) {
125 avctx->pix_fmt = PIX_FMT_RGB24;
126 } else if (depth == 4) {
127 avctx->pix_fmt = PIX_FMT_RGB32;
135 pnm_get(s, buf1, sizeof(buf1));
136 avctx->width = atoi(buf1);
137 if (avctx->width <= 0)
139 pnm_get(s, buf1, sizeof(buf1));
140 avctx->height = atoi(buf1);
141 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
143 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
144 pnm_get(s, buf1, sizeof(buf1));
145 if(atoi(buf1) == 65535 && avctx->pix_fmt == PIX_FMT_GRAY8)
146 avctx->pix_fmt = PIX_FMT_GRAY16BE;
148 /* more check if YUV420 */
149 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
150 if ((avctx->width & 1) != 0)
152 h = (avctx->height * 2);
161 static int pnm_decode_frame(AVCodecContext *avctx,
162 void *data, int *data_size,
163 uint8_t *buf, int buf_size)
165 PNMContext * const s = avctx->priv_data;
166 AVFrame *picture = data;
167 AVFrame * const p= (AVFrame*)&s->picture;
168 int i, n, linesize, h;
173 s->bytestream_end= buf + buf_size;
175 if(pnm_decode_header(avctx, s) < 0)
179 avctx->release_buffer(avctx, p);
182 if(avctx->get_buffer(avctx, p) < 0){
183 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
186 p->pict_type= FF_I_TYPE;
189 switch(avctx->pix_fmt) {
193 n = avctx->width * 3;
198 case PIX_FMT_GRAY16BE:
199 n = avctx->width * 2;
201 case PIX_FMT_MONOWHITE:
202 case PIX_FMT_MONOBLACK:
203 n = (avctx->width + 7) >> 3;
206 linesize = p->linesize[0];
207 if(s->bytestream + n*avctx->height > s->bytestream_end)
209 for(i = 0; i < avctx->height; i++) {
210 memcpy(ptr, s->bytestream, n);
215 case PIX_FMT_YUV420P:
217 unsigned char *ptr1, *ptr2;
221 linesize = p->linesize[0];
222 if(s->bytestream + n*avctx->height*3/2 > s->bytestream_end)
224 for(i = 0; i < avctx->height; i++) {
225 memcpy(ptr, s->bytestream, n);
232 h = avctx->height >> 1;
233 for(i = 0; i < h; i++) {
234 memcpy(ptr1, s->bytestream, n);
236 memcpy(ptr2, s->bytestream, n);
238 ptr1 += p->linesize[1];
239 ptr2 += p->linesize[2];
245 linesize = p->linesize[0];
246 if(s->bytestream + avctx->width*avctx->height*4 > s->bytestream_end)
248 for(i = 0; i < avctx->height; i++) {
251 for(j = 0;j < avctx->width; j++) {
252 r = *s->bytestream++;
253 g = *s->bytestream++;
254 b = *s->bytestream++;
255 a = *s->bytestream++;
256 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
262 *picture= *(AVFrame*)&s->picture;
263 *data_size = sizeof(AVPicture);
265 return s->bytestream - s->bytestream_start;
268 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
269 PNMContext *s = avctx->priv_data;
270 AVFrame *pict = data;
271 AVFrame * const p= (AVFrame*)&s->picture;
272 int i, h, h1, c, n, linesize;
273 uint8_t *ptr, *ptr1, *ptr2;
275 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
276 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
281 p->pict_type= FF_I_TYPE;
285 s->bytestream= outbuf;
286 s->bytestream_end= outbuf+buf_size;
290 switch(avctx->pix_fmt) {
291 case PIX_FMT_MONOWHITE:
293 n = (avctx->width + 7) >> 3;
299 case PIX_FMT_GRAY16BE:
301 n = avctx->width * 2;
305 n = avctx->width * 3;
307 case PIX_FMT_YUV420P:
315 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
317 c, avctx->width, h1);
318 s->bytestream += strlen(s->bytestream);
319 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
320 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
321 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE) ? 255 : 65535);
322 s->bytestream += strlen(s->bytestream);
326 linesize = p->linesize[0];
328 memcpy(s->bytestream, ptr, n);
333 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
339 memcpy(s->bytestream, ptr1, n);
341 memcpy(s->bytestream, ptr2, n);
343 ptr1 += p->linesize[1];
344 ptr2 += p->linesize[2];
347 return s->bytestream - s->bytestream_start;
350 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
351 PNMContext *s = avctx->priv_data;
352 AVFrame *pict = data;
353 AVFrame * const p= (AVFrame*)&s->picture;
354 int i, h, w, n, linesize, depth, maxval;
355 const char *tuple_type;
358 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
359 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
364 p->pict_type= FF_I_TYPE;
368 s->bytestream= outbuf;
369 s->bytestream_end= outbuf+buf_size;
373 switch(avctx->pix_fmt) {
374 case PIX_FMT_MONOWHITE:
378 tuple_type = "BLACKANDWHITE";
384 tuple_type = "GRAYSCALE";
396 tuple_type = "RGB_ALPHA";
401 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
402 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
403 w, h, depth, maxval, tuple_type);
404 s->bytestream += strlen(s->bytestream);
407 linesize = p->linesize[0];
409 if (avctx->pix_fmt == PIX_FMT_RGB32) {
415 v = ((uint32_t *)ptr)[j];
416 *s->bytestream++ = v >> 16;
417 *s->bytestream++ = v >> 8;
418 *s->bytestream++ = v;
419 *s->bytestream++ = v >> 24;
425 memcpy(s->bytestream, ptr, n);
430 return s->bytestream - s->bytestream_start;
434 static int pnm_probe(AVProbeData *pd)
436 const char *p = pd->buf;
437 if (pd->buf_size >= 8 &&
439 p[1] >= '4' && p[1] <= '6' &&
441 return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
446 static int pgmyuv_probe(AVProbeData *pd)
448 if (match_ext(pd->filename, "pgmyuv"))
449 return AVPROBE_SCORE_MAX;
454 static int pam_probe(AVProbeData *pd)
456 const char *p = pd->buf;
457 if (pd->buf_size >= 8 &&
461 return AVPROBE_SCORE_MAX;
467 #ifdef CONFIG_PNM_PARSER
468 static int pnm_parse(AVCodecParserContext *s,
469 AVCodecContext *avctx,
470 uint8_t **poutbuf, int *poutbuf_size,
471 const uint8_t *buf, int buf_size)
473 ParseContext *pc = s->priv_data;
477 for(; pc->overread>0; pc->overread--){
478 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
482 pnmctx.bytestream_start=
483 pnmctx.bytestream= pc->buffer;
484 pnmctx.bytestream_end= pc->buffer + pc->index;
486 pnmctx.bytestream_start=
487 pnmctx.bytestream= (uint8_t *) buf; /* casts avoid warnings */
488 pnmctx.bytestream_end= (uint8_t *) buf + buf_size;
490 if(pnm_decode_header(avctx, &pnmctx) < 0){
491 if(pnmctx.bytestream < pnmctx.bytestream_end){
501 if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
502 memcpy(pc->buffer + pc->index, buf, pc->index);
503 pc->index += pc->index;
505 buf_size -= pc->index;
511 next= pnmctx.bytestream - pnmctx.bytestream_start
512 + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
513 if(pnmctx.bytestream_start!=buf)
519 if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
524 *poutbuf = (uint8_t *)buf;
525 *poutbuf_size = buf_size;
529 AVCodecParser pnm_parser = {
530 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
531 sizeof(ParseContext),
536 #endif /* CONFIG_PNM_PARSER */
538 #ifdef CONFIG_PGM_ENCODER
539 AVCodec pgm_encoder = {
548 .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, -1},
550 #endif // CONFIG_PGM_ENCODER
552 #ifdef CONFIG_PGMYUV_ENCODER
553 AVCodec pgmyuv_encoder = {
562 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
564 #endif // CONFIG_PGMYUV_ENCODER
566 #ifdef CONFIG_PPM_ENCODER
567 AVCodec ppm_encoder = {
576 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
578 #endif // CONFIG_PPM_ENCODER
580 #ifdef CONFIG_PBM_ENCODER
581 AVCodec pbm_encoder = {
590 .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
592 #endif // CONFIG_PBM_ENCODER
594 #ifdef CONFIG_PAM_ENCODER
595 AVCodec pam_encoder = {
604 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},
606 #endif // CONFIG_PAM_ENCODER