]> git.sesse.net Git - ffmpeg/blob - libavcodec/pnm.c
More spelling errors patch by (Kevin Baragona (kevinmb500 gawab com>)
[ffmpeg] / libavcodec / pnm.c
1 /*
2  * PNM image format
3  * Copyright (c) 2002, 2003 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include "avcodec.h"
20 #include "mpegvideo.h" //only for ParseContext
21
22 typedef struct PNMContext {
23     uint8_t *bytestream;
24     uint8_t *bytestream_start;
25     uint8_t *bytestream_end;
26     AVFrame picture;
27 } PNMContext;
28
29 static inline int pnm_space(int c)  
30 {
31     return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
32 }
33
34 static void pnm_get(PNMContext *sc, char *str, int buf_size) 
35 {
36     char *s;
37     int c;
38     
39     /* skip spaces and comments */
40     for(;;) {
41         c = *sc->bytestream++;
42         if (c == '#')  {
43             do  {
44                 c = *sc->bytestream++;
45             } while (c != '\n' && sc->bytestream < sc->bytestream_end);
46         } else if (!pnm_space(c)) {
47             break;
48         }
49     }
50     
51     s = str;
52     while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
53         if ((s - str)  < buf_size - 1)
54             *s++ = c;
55         c = *sc->bytestream++;
56     }
57     *s = '\0';
58 }
59
60 static int common_init(AVCodecContext *avctx){
61     PNMContext *s = avctx->priv_data;
62
63     avcodec_get_frame_defaults((AVFrame*)&s->picture);
64     avctx->coded_frame= (AVFrame*)&s->picture;
65
66     return 0;
67 }
68
69 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
70     char buf1[32], tuple_type[32];
71     int h, w, depth, maxval;;
72
73     pnm_get(s, buf1, sizeof(buf1));
74     if (!strcmp(buf1, "P4")) {
75         avctx->pix_fmt = PIX_FMT_MONOWHITE;
76     } else if (!strcmp(buf1, "P5")) {
77         if (avctx->codec_id == CODEC_ID_PGMYUV) 
78             avctx->pix_fmt = PIX_FMT_YUV420P;
79         else
80             avctx->pix_fmt = PIX_FMT_GRAY8;
81     } else if (!strcmp(buf1, "P6")) {
82         avctx->pix_fmt = PIX_FMT_RGB24;
83     } else if (!strcmp(buf1, "P7")) {
84         w = -1;
85         h = -1;
86         maxval = -1;
87         depth = -1;
88         tuple_type[0] = '\0';
89         for(;;) {
90             pnm_get(s, buf1, sizeof(buf1));
91             if (!strcmp(buf1, "WIDTH")) {
92                 pnm_get(s, buf1, sizeof(buf1));
93                 w = strtol(buf1, NULL, 10);
94             } else if (!strcmp(buf1, "HEIGHT")) {
95                 pnm_get(s, buf1, sizeof(buf1));
96                 h = strtol(buf1, NULL, 10);
97             } else if (!strcmp(buf1, "DEPTH")) {
98                 pnm_get(s, buf1, sizeof(buf1));
99                 depth = strtol(buf1, NULL, 10);
100             } else if (!strcmp(buf1, "MAXVAL")) {
101                 pnm_get(s, buf1, sizeof(buf1));
102                 maxval = strtol(buf1, NULL, 10);
103             } else if (!strcmp(buf1, "TUPLETYPE")) {
104                 pnm_get(s, tuple_type, sizeof(tuple_type));
105             } else if (!strcmp(buf1, "ENDHDR")) {
106                 break;
107             } else {
108                 return -1;
109             }
110         }
111         /* check that all tags are present */
112         if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
113             return -1;
114                    
115         avctx->width = w;
116         avctx->height = h;
117         if (depth == 1) {
118             if (maxval == 1)
119                 avctx->pix_fmt = PIX_FMT_MONOWHITE;
120             else 
121                 avctx->pix_fmt = PIX_FMT_GRAY8;
122         } else if (depth == 3) {
123             avctx->pix_fmt = PIX_FMT_RGB24;
124         } else if (depth == 4) {
125             avctx->pix_fmt = PIX_FMT_RGBA32;
126         } else {
127             return -1;
128         }
129         return 0;
130     } else {
131         return -1;
132     }
133     pnm_get(s, buf1, sizeof(buf1));
134     avctx->width = atoi(buf1);
135     if (avctx->width <= 0)
136         return -1;
137     pnm_get(s, buf1, sizeof(buf1));
138     avctx->height = atoi(buf1);
139     if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
140         return -1;
141     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
142         pnm_get(s, buf1, sizeof(buf1));
143     }
144
145     /* more check if YUV420 */
146     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
147         if ((avctx->width & 1) != 0)
148             return -1;
149         h = (avctx->height * 2);
150         if ((h % 3) != 0)
151             return -1;
152         h /= 3;
153         avctx->height = h;
154     }
155     return 0;
156 }
157
158 static int pnm_decode_frame(AVCodecContext *avctx, 
159                         void *data, int *data_size,
160                         uint8_t *buf, int buf_size)
161 {
162     PNMContext * const s = avctx->priv_data;
163     AVFrame *picture = data;
164     AVFrame * const p= (AVFrame*)&s->picture;
165     int i, n, linesize, h;
166     unsigned char *ptr;
167
168     s->bytestream_start=
169     s->bytestream= buf;
170     s->bytestream_end= buf + buf_size;
171     
172     if(pnm_decode_header(avctx, s) < 0)
173         return -1;
174     
175     if(p->data[0])
176         avctx->release_buffer(avctx, p);
177
178     p->reference= 0;
179     if(avctx->get_buffer(avctx, p) < 0){
180         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
181         return -1;
182     }
183     p->pict_type= FF_I_TYPE;
184     p->key_frame= 1;
185     
186     switch(avctx->pix_fmt) {
187     default:
188         return -1;
189     case PIX_FMT_RGB24:
190         n = avctx->width * 3;
191         goto do_read;
192     case PIX_FMT_GRAY8:
193         n = avctx->width;
194         goto do_read;
195     case PIX_FMT_MONOWHITE:
196     case PIX_FMT_MONOBLACK:
197         n = (avctx->width + 7) >> 3;
198     do_read:
199         ptr = p->data[0];
200         linesize = p->linesize[0];
201         for(i = 0; i < avctx->height; i++) {
202             memcpy(ptr, s->bytestream, n);
203             s->bytestream += n;
204             ptr += linesize;
205         }
206         break;
207     case PIX_FMT_YUV420P:
208         {
209             unsigned char *ptr1, *ptr2;
210
211             n = avctx->width;
212             ptr = p->data[0];
213             linesize = p->linesize[0];
214             for(i = 0; i < avctx->height; i++) {
215                 memcpy(ptr, s->bytestream, n);
216                 s->bytestream += n;
217                 ptr += linesize;
218             }
219             ptr1 = p->data[1];
220             ptr2 = p->data[2];
221             n >>= 1;
222             h = avctx->height >> 1;
223             for(i = 0; i < h; i++) {
224                 memcpy(ptr1, s->bytestream, n);
225                 s->bytestream += n;
226                 memcpy(ptr2, s->bytestream, n);
227                 s->bytestream += n;
228                 ptr1 += p->linesize[1];
229                 ptr2 += p->linesize[2];
230             }
231         }
232         break;
233     case PIX_FMT_RGBA32:
234         ptr = p->data[0];
235         linesize = p->linesize[0];
236         for(i = 0; i < avctx->height; i++) {
237             int j, r, g, b, a;
238
239             for(j = 0;j < avctx->width; j++) {
240                 r = *s->bytestream++;
241                 g = *s->bytestream++;
242                 b = *s->bytestream++;
243                 a = *s->bytestream++;
244                 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
245             }
246             ptr += linesize;
247         }
248         break;
249     }
250     *picture= *(AVFrame*)&s->picture;
251     *data_size = sizeof(AVPicture);
252
253     return s->bytestream - s->bytestream_start;
254 }
255
256 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
257     PNMContext *s = avctx->priv_data;
258     AVFrame *pict = data;
259     AVFrame * const p= (AVFrame*)&s->picture;
260     int i, h, h1, c, n, linesize;
261     uint8_t *ptr, *ptr1, *ptr2;
262
263     if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
264         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
265         return -1;
266     }
267
268     *p = *pict;
269     p->pict_type= FF_I_TYPE;
270     p->key_frame= 1;
271     
272     s->bytestream_start=
273     s->bytestream= outbuf;
274     s->bytestream_end= outbuf+buf_size;
275
276     h = avctx->height;
277     h1 = h;
278     switch(avctx->pix_fmt) {
279     case PIX_FMT_MONOWHITE:
280         c = '4';
281         n = (avctx->width + 7) >> 3;
282         break;
283     case PIX_FMT_GRAY8:
284         c = '5';
285         n = avctx->width;
286         break;
287     case PIX_FMT_RGB24:
288         c = '6';
289         n = avctx->width * 3;
290         break;
291     case PIX_FMT_YUV420P:
292         c = '5';
293         n = avctx->width;
294         h1 = (h * 3) / 2;
295         break;
296     default:
297         return -1;
298     }
299     snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
300              "P%c\n%d %d\n",
301              c, avctx->width, h1);
302     s->bytestream += strlen(s->bytestream);
303     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
304         snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
305                  "%d\n", 255);
306         s->bytestream += strlen(s->bytestream);
307     }
308
309     ptr = p->data[0];
310     linesize = p->linesize[0];
311     for(i=0;i<h;i++) {
312         memcpy(s->bytestream, ptr, n);
313         s->bytestream += n;
314         ptr += linesize;
315     }
316     
317     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
318         h >>= 1;
319         n >>= 1;
320         ptr1 = p->data[1];
321         ptr2 = p->data[2];
322         for(i=0;i<h;i++) {
323             memcpy(s->bytestream, ptr1, n);
324             s->bytestream += n;
325             memcpy(s->bytestream, ptr2, n);
326             s->bytestream += n;
327                 ptr1 += p->linesize[1];
328                 ptr2 += p->linesize[2];
329         }
330     }
331     return s->bytestream - s->bytestream_start;
332 }
333
334 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
335     PNMContext *s = avctx->priv_data;
336     AVFrame *pict = data;
337     AVFrame * const p= (AVFrame*)&s->picture;
338     int i, h, w, n, linesize, depth, maxval;
339     const char *tuple_type;
340     uint8_t *ptr;
341
342     if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
343         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
344         return -1;
345     }
346
347     *p = *pict;
348     p->pict_type= FF_I_TYPE;
349     p->key_frame= 1;
350     
351     s->bytestream_start=
352     s->bytestream= outbuf;
353     s->bytestream_end= outbuf+buf_size;
354
355     h = avctx->height;
356     w = avctx->width;
357     switch(avctx->pix_fmt) {
358     case PIX_FMT_MONOWHITE:
359         n = (w + 7) >> 3;
360         depth = 1;
361         maxval = 1;
362         tuple_type = "BLACKANDWHITE";
363         break;
364     case PIX_FMT_GRAY8:
365         n = w;
366         depth = 1;
367         maxval = 255;
368         tuple_type = "GRAYSCALE";
369         break;
370     case PIX_FMT_RGB24:
371         n = w * 3;
372         depth = 3;
373         maxval = 255;
374         tuple_type = "RGB";
375         break;
376     case PIX_FMT_RGBA32:
377         n = w * 4;
378         depth = 4;
379         maxval = 255;
380         tuple_type = "RGB_ALPHA";
381         break;
382     default:
383         return -1;
384     }
385     snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
386              "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
387              w, h, depth, maxval, tuple_type);
388     s->bytestream += strlen(s->bytestream);
389     
390     ptr = p->data[0];
391     linesize = p->linesize[0];
392     
393     if (avctx->pix_fmt == PIX_FMT_RGBA32) {
394         int j;
395         unsigned int v;
396
397         for(i=0;i<h;i++) {
398             for(j=0;j<w;j++) {
399                 v = ((uint32_t *)ptr)[j];
400                 *s->bytestream++ = v >> 16;
401                 *s->bytestream++ = v >> 8;
402                 *s->bytestream++ = v;
403                 *s->bytestream++ = v >> 24;
404             }
405             ptr += linesize;
406         }
407     } else {
408         for(i=0;i<h;i++) {
409             memcpy(s->bytestream, ptr, n);
410             s->bytestream += n;
411             ptr += linesize;
412         }
413     }
414     return s->bytestream - s->bytestream_start;
415 }
416
417 #if 0
418 static int pnm_probe(AVProbeData *pd)
419 {
420     const char *p = pd->buf;
421     if (pd->buf_size >= 8 &&
422         p[0] == 'P' &&
423         p[1] >= '4' && p[1] <= '6' &&
424         pnm_space(p[2]) )
425         return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
426     else
427         return 0;
428 }
429
430 static int pgmyuv_probe(AVProbeData *pd)
431 {
432     if (match_ext(pd->filename, "pgmyuv"))
433         return AVPROBE_SCORE_MAX;
434     else
435         return 0;
436 }
437
438 static int pam_probe(AVProbeData *pd)
439 {
440     const char *p = pd->buf;
441     if (pd->buf_size >= 8 &&
442         p[0] == 'P' &&
443         p[1] == '7' &&
444         p[2] == '\n')
445         return AVPROBE_SCORE_MAX;
446     else
447         return 0;
448 }
449 #endif
450
451 static int pnm_parse(AVCodecParserContext *s,
452                            AVCodecContext *avctx,
453                            uint8_t **poutbuf, int *poutbuf_size, 
454                            const uint8_t *buf, int buf_size)
455 {
456     ParseContext *pc = s->priv_data;
457     PNMContext pnmctx;
458     int next;
459
460     for(; pc->overread>0; pc->overread--){
461         pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
462     }
463 retry:
464     if(pc->index){
465         pnmctx.bytestream_start=
466         pnmctx.bytestream= pc->buffer;
467         pnmctx.bytestream_end= pc->buffer + pc->index;
468     }else{
469         pnmctx.bytestream_start=
470         pnmctx.bytestream= buf;
471         pnmctx.bytestream_end= buf + buf_size;
472     }
473     if(pnm_decode_header(avctx, &pnmctx) < 0){
474         if(pnmctx.bytestream < pnmctx.bytestream_end){
475             if(pc->index){
476                 pc->index=0;
477             }else{
478                 buf++;
479                 buf_size--;
480             }
481             goto retry;
482         }
483 #if 0
484         if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
485             memcpy(pc->buffer + pc->index, buf, pc->index);
486             pc->index += pc->index;
487             buf += pc->index;
488             buf_size -= pc->index;
489             goto retry;
490         }
491 #endif
492         next= END_NOT_FOUND;
493     }else{
494         next= pnmctx.bytestream - pnmctx.bytestream_start 
495             + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
496         if(pnmctx.bytestream_start!=buf)
497             next-= pc->index;
498         if(next > buf_size)
499             next= END_NOT_FOUND;
500     }
501     
502     if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
503         *poutbuf = NULL;
504         *poutbuf_size = 0;
505         return buf_size;
506     }
507     *poutbuf = (uint8_t *)buf;
508     *poutbuf_size = buf_size;
509     return next;
510 }
511
512 AVCodecParser pnm_parser = {
513     { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
514     sizeof(ParseContext),
515     NULL,
516     pnm_parse,
517     ff_parse_close,
518 };
519
520 AVCodec pgm_encoder = {
521     "pgm",
522     CODEC_TYPE_VIDEO,
523     CODEC_ID_PGM,
524     sizeof(PNMContext),
525     common_init,
526     pnm_encode_frame,
527     NULL, //encode_end,
528     pnm_decode_frame,
529     .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, -1}, 
530 };
531
532 AVCodec pgmyuv_encoder = {
533     "pgmyuv",
534     CODEC_TYPE_VIDEO,
535     CODEC_ID_PGMYUV,
536     sizeof(PNMContext),
537     common_init,
538     pnm_encode_frame,
539     NULL, //encode_end,
540     pnm_decode_frame,
541     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, 
542 };
543
544 AVCodec ppm_encoder = {
545     "ppm",
546     CODEC_TYPE_VIDEO,
547     CODEC_ID_PPM,
548     sizeof(PNMContext),
549     common_init,
550     pnm_encode_frame,
551     NULL, //encode_end,
552     pnm_decode_frame,
553     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1}, 
554 };
555
556 AVCodec pbm_encoder = {
557     "pbm",
558     CODEC_TYPE_VIDEO,
559     CODEC_ID_PBM,
560     sizeof(PNMContext),
561     common_init,
562     pnm_encode_frame,
563     NULL, //encode_end,
564     pnm_decode_frame,
565     .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1}, 
566 };
567
568 AVCodec pam_encoder = {
569     "pam",
570     CODEC_TYPE_VIDEO,
571     CODEC_ID_PAM,
572     sizeof(PNMContext),
573     common_init,
574     pam_encode_frame,
575     NULL, //encode_end,
576     pnm_decode_frame,
577     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1}, 
578 };