]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/pnm.c
merge pam_decode_frame() into pnm_decode_frame()
[frescor/ffmpeg.git] / 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')
113             return -1;
114         avctx->width = w;
115         avctx->height = h;
116         if (depth == 1) {
117             if (maxval == 1)
118                 avctx->pix_fmt = PIX_FMT_MONOWHITE;
119             else 
120                 avctx->pix_fmt = PIX_FMT_GRAY8;
121         } else if (depth == 3) {
122             avctx->pix_fmt = PIX_FMT_RGB24;
123         } else if (depth == 4) {
124             avctx->pix_fmt = PIX_FMT_RGBA32;
125         } else {
126             return -1;
127         }
128         return 0;
129     } else {
130         return -1;
131     }
132     pnm_get(s, buf1, sizeof(buf1));
133     avctx->width = atoi(buf1);
134     if (avctx->width <= 0)
135         return -1;
136     pnm_get(s, buf1, sizeof(buf1));
137     avctx->height = atoi(buf1);
138     if (avctx->height <= 0)
139         return -1;
140     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
141         pnm_get(s, buf1, sizeof(buf1));
142     }
143
144     /* more check if YUV420 */
145     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
146         if ((avctx->width & 1) != 0)
147             return -1;
148         h = (avctx->height * 2);
149         if ((h % 3) != 0)
150             return -1;
151         h /= 3;
152         avctx->height = h;
153     }
154     return 0;
155 }
156
157 static int pnm_decode_frame(AVCodecContext *avctx, 
158                         void *data, int *data_size,
159                         uint8_t *buf, int buf_size)
160 {
161     PNMContext * const s = avctx->priv_data;
162     AVFrame *picture = data;
163     AVFrame * const p= (AVFrame*)&s->picture;
164     int i, n, linesize, h;
165     unsigned char *ptr;
166
167     /* special case for last picture */
168     if (buf_size == 0) {
169         return 0;
170     }
171     
172     s->bytestream_start=
173     s->bytestream= buf;
174     s->bytestream_end= buf + buf_size;
175     
176     if(pnm_decode_header(avctx, s) < 0)
177         return -1;
178     
179     if(p->data[0])
180         avctx->release_buffer(avctx, p);
181
182     p->reference= 0;
183     if(avctx->get_buffer(avctx, p) < 0){
184         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
185         return -1;
186     }
187     p->pict_type= FF_I_TYPE;
188     p->key_frame= 1;
189     
190     switch(avctx->pix_fmt) {
191     default:
192         return -1;
193     case PIX_FMT_RGB24:
194         n = avctx->width * 3;
195         goto do_read;
196     case PIX_FMT_GRAY8:
197         n = avctx->width;
198         goto do_read;
199     case PIX_FMT_MONOWHITE:
200     case PIX_FMT_MONOBLACK:
201         n = (avctx->width + 7) >> 3;
202     do_read:
203         ptr = p->data[0];
204         linesize = p->linesize[0];
205         for(i = 0; i < avctx->height; i++) {
206             memcpy(ptr, s->bytestream, n);
207             s->bytestream += n;
208             ptr += linesize;
209         }
210         break;
211     case PIX_FMT_YUV420P:
212         {
213             unsigned char *ptr1, *ptr2;
214
215             n = avctx->width;
216             ptr = p->data[0];
217             linesize = p->linesize[0];
218             for(i = 0; i < avctx->height; i++) {
219                 memcpy(ptr, s->bytestream, n);
220                 s->bytestream += n;
221                 ptr += linesize;
222             }
223             ptr1 = p->data[1];
224             ptr2 = p->data[2];
225             n >>= 1;
226             h = avctx->height >> 1;
227             for(i = 0; i < h; i++) {
228                 memcpy(ptr1, s->bytestream, n);
229                 s->bytestream += n;
230                 memcpy(ptr2, s->bytestream, n);
231                 s->bytestream += n;
232                 ptr1 += p->linesize[1];
233                 ptr2 += p->linesize[2];
234             }
235         }
236         break;
237     case PIX_FMT_RGBA32:
238         ptr = p->data[0];
239         linesize = p->linesize[0];
240         for(i = 0; i < avctx->height; i++) {
241             int j, r, g, b, a;
242
243             for(j = 0;j < avctx->width; j++) {
244                 r = *s->bytestream++;
245                 g = *s->bytestream++;
246                 b = *s->bytestream++;
247                 a = *s->bytestream++;
248                 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
249             }
250             ptr += linesize;
251         }
252         break;
253     }
254     *picture= *(AVFrame*)&s->picture;
255     *data_size = sizeof(AVPicture);
256
257     return s->bytestream - s->bytestream_start;
258 }
259
260 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
261     PNMContext *s = avctx->priv_data;
262     AVFrame *pict = data;
263     AVFrame * const p= (AVFrame*)&s->picture;
264     int i, h, h1, c, n, linesize;
265     uint8_t *ptr, *ptr1, *ptr2;
266
267     *p = *pict;
268     p->pict_type= FF_I_TYPE;
269     p->key_frame= 1;
270     
271     s->bytestream_start=
272     s->bytestream= outbuf;
273     s->bytestream_end= outbuf+buf_size;
274
275     h = avctx->height;
276     h1 = h;
277     switch(avctx->pix_fmt) {
278     case PIX_FMT_MONOWHITE:
279         c = '4';
280         n = (avctx->width + 7) >> 3;
281         break;
282     case PIX_FMT_GRAY8:
283         c = '5';
284         n = avctx->width;
285         break;
286     case PIX_FMT_RGB24:
287         c = '6';
288         n = avctx->width * 3;
289         break;
290     case PIX_FMT_YUV420P:
291         c = '5';
292         n = avctx->width;
293         h1 = (h * 3) / 2;
294         break;
295     default:
296         return -1;
297     }
298     snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
299              "P%c\n%d %d\n",
300              c, avctx->width, h1);
301     s->bytestream += strlen(s->bytestream);
302     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
303         snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
304                  "%d\n", 255);
305         s->bytestream += strlen(s->bytestream);
306     }
307
308     ptr = p->data[0];
309     linesize = p->linesize[0];
310     for(i=0;i<h;i++) {
311         memcpy(s->bytestream, ptr, n);
312         s->bytestream += n;
313         ptr += linesize;
314     }
315     
316     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
317         h >>= 1;
318         n >>= 1;
319         ptr1 = p->data[1];
320         ptr2 = p->data[2];
321         for(i=0;i<h;i++) {
322             memcpy(s->bytestream, ptr1, n);
323             s->bytestream += n;
324             memcpy(s->bytestream, ptr2, n);
325             s->bytestream += n;
326                 ptr1 += p->linesize[1];
327                 ptr2 += p->linesize[2];
328         }
329     }
330     return s->bytestream - s->bytestream_start;
331 }
332
333 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
334     PNMContext *s = avctx->priv_data;
335     AVFrame *pict = data;
336     AVFrame * const p= (AVFrame*)&s->picture;
337     int i, h, w, n, linesize, depth, maxval;
338     const char *tuple_type;
339     uint8_t *ptr;
340
341     *p = *pict;
342     p->pict_type= FF_I_TYPE;
343     p->key_frame= 1;
344     
345     s->bytestream_start=
346     s->bytestream= outbuf;
347     s->bytestream_end= outbuf+buf_size;
348
349     h = avctx->height;
350     w = avctx->width;
351     switch(avctx->pix_fmt) {
352     case PIX_FMT_MONOWHITE:
353         n = (w + 7) >> 3;
354         depth = 1;
355         maxval = 1;
356         tuple_type = "BLACKANDWHITE";
357         break;
358     case PIX_FMT_GRAY8:
359         n = w;
360         depth = 1;
361         maxval = 255;
362         tuple_type = "GRAYSCALE";
363         break;
364     case PIX_FMT_RGB24:
365         n = w * 3;
366         depth = 3;
367         maxval = 255;
368         tuple_type = "RGB";
369         break;
370     case PIX_FMT_RGBA32:
371         n = w * 4;
372         depth = 4;
373         maxval = 255;
374         tuple_type = "RGB_ALPHA";
375         break;
376     default:
377         return -1;
378     }
379     snprintf(s->bytestream, s->bytestream_end - s->bytestream, 
380              "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
381              w, h, depth, maxval, tuple_type);
382     s->bytestream += strlen(s->bytestream);
383     
384     ptr = p->data[0];
385     linesize = p->linesize[0];
386     
387     if (avctx->pix_fmt == PIX_FMT_RGBA32) {
388         int j;
389         unsigned int v;
390
391         for(i=0;i<h;i++) {
392             for(j=0;j<w;j++) {
393                 v = ((uint32_t *)ptr)[j];
394                 *s->bytestream++ = v >> 16;
395                 *s->bytestream++ = v >> 8;
396                 *s->bytestream++ = v;
397                 *s->bytestream++ = v >> 24;
398             }
399             ptr += linesize;
400         }
401     } else {
402         for(i=0;i<h;i++) {
403             memcpy(s->bytestream, ptr, n);
404             s->bytestream += n;
405             ptr += linesize;
406         }
407     }
408     return s->bytestream - s->bytestream_start;
409 }
410
411 #if 0
412 static int pnm_probe(AVProbeData *pd)
413 {
414     const char *p = pd->buf;
415     if (pd->buf_size >= 8 &&
416         p[0] == 'P' &&
417         p[1] >= '4' && p[1] <= '6' &&
418         pnm_space(p[2]) )
419         return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
420     else
421         return 0;
422 }
423
424 static int pgmyuv_probe(AVProbeData *pd)
425 {
426     if (match_ext(pd->filename, "pgmyuv"))
427         return AVPROBE_SCORE_MAX;
428     else
429         return 0;
430 }
431
432 static int pam_probe(AVProbeData *pd)
433 {
434     const char *p = pd->buf;
435     if (pd->buf_size >= 8 &&
436         p[0] == 'P' &&
437         p[1] == '7' &&
438         p[2] == '\n')
439         return AVPROBE_SCORE_MAX;
440     else
441         return 0;
442 }
443 #endif
444
445 static int pnm_parse(AVCodecParserContext *s,
446                            AVCodecContext *avctx,
447                            uint8_t **poutbuf, int *poutbuf_size, 
448                            const uint8_t *buf, int buf_size)
449 {
450     ParseContext *pc = s->priv_data;
451     PNMContext pnmctx;
452     int next;
453
454     for(; pc->overread>0; pc->overread--){
455         pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
456     }
457 retry:
458     if(pc->index){
459         pnmctx.bytestream_start=
460         pnmctx.bytestream= pc->buffer;
461         pnmctx.bytestream_end= pc->buffer + pc->index;
462     }else{
463         pnmctx.bytestream_start=
464         pnmctx.bytestream= buf;
465         pnmctx.bytestream_end= buf + buf_size;
466     }
467     if(pnm_decode_header(avctx, &pnmctx) < 0){
468         if(pnmctx.bytestream < pnmctx.bytestream_end){
469             if(pc->index){
470                 pc->index=0;
471             }else{
472                 buf++;
473                 buf_size--;
474             }
475             goto retry;
476         }
477 #if 0
478         if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
479             memcpy(pc->buffer + pc->index, buf, pc->index);
480             pc->index += pc->index;
481             buf += pc->index;
482             buf_size -= pc->index;
483             goto retry;
484         }
485 #endif
486         next= END_NOT_FOUND;
487     }else{
488         next= pnmctx.bytestream - pnmctx.bytestream_start 
489             + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
490         if(pnmctx.bytestream_start!=buf)
491             next-= pc->index;
492         if(next > buf_size)
493             next= END_NOT_FOUND;
494     }
495     
496     if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
497         *poutbuf = NULL;
498         *poutbuf_size = 0;
499         return buf_size;
500     }
501     *poutbuf = (uint8_t *)buf;
502     *poutbuf_size = buf_size;
503     return next;
504 }
505
506 AVCodecParser pnm_parser = {
507     { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
508     sizeof(ParseContext),
509     NULL,
510     pnm_parse,
511     ff_parse_close,
512 };
513
514 AVCodec pgm_encoder = {
515     "pgm",
516     CODEC_TYPE_VIDEO,
517     CODEC_ID_PGM,
518     sizeof(PNMContext),
519     common_init,
520     pnm_encode_frame,
521     NULL, //encode_end,
522     pnm_decode_frame,
523     .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, -1}, 
524 };
525
526 AVCodec pgmyuv_encoder = {
527     "pgmyuv",
528     CODEC_TYPE_VIDEO,
529     CODEC_ID_PGMYUV,
530     sizeof(PNMContext),
531     common_init,
532     pnm_encode_frame,
533     NULL, //encode_end,
534     pnm_decode_frame,
535     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, 
536 };
537
538 AVCodec ppm_encoder = {
539     "ppm",
540     CODEC_TYPE_VIDEO,
541     CODEC_ID_PPM,
542     sizeof(PNMContext),
543     common_init,
544     pnm_encode_frame,
545     NULL, //encode_end,
546     pnm_decode_frame,
547     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1}, 
548 };
549
550 AVCodec pbm_encoder = {
551     "pbm",
552     CODEC_TYPE_VIDEO,
553     CODEC_ID_PBM,
554     sizeof(PNMContext),
555     common_init,
556     pnm_encode_frame,
557     NULL, //encode_end,
558     pnm_decode_frame,
559     .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1}, 
560 };
561
562 AVCodec pam_encoder = {
563     "pam",
564     CODEC_TYPE_VIDEO,
565     CODEC_ID_PAM,
566     sizeof(PNMContext),
567     common_init,
568     pam_encode_frame,
569     NULL, //encode_end,
570     pnm_decode_frame,
571     .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1}, 
572 };