annotate pnm.c @ 5507:4be3d4a1c94b libavcodec

kill warning patch by Carl Eugen Hoyos [cehoyos * ag or at]
author benoit
date Wed, 08 Aug 2007 07:04:23 +0000
parents 15577904cb95
children c93570aeb3eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4978
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
1 /*
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
2 * PNM image format
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
3 * Copyright (c) 2002, 2003 Fabrice Bellard.
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
4 *
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
5 * This file is part of FFmpeg.
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
6 *
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
11 *
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
15 * Lesser General Public License for more details.
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
16 *
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
20 */
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
21 #include "avcodec.h"
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
22 #include "pnm.h"
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
23
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
24 static inline int pnm_space(int c)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
25 {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
26 return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
27 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
28
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
29 static void pnm_get(PNMContext *sc, char *str, int buf_size)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
30 {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
31 char *s;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
32 int c;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
33
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
34 /* skip spaces and comments */
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
35 for(;;) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
36 c = *sc->bytestream++;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
37 if (c == '#') {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
38 do {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
39 c = *sc->bytestream++;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
40 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
41 } else if (!pnm_space(c)) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
42 break;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
43 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
44 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
45
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
46 s = str;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
47 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
48 if ((s - str) < buf_size - 1)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
49 *s++ = c;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
50 c = *sc->bytestream++;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
51 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
52 *s = '\0';
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
53 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
54
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
55 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
56 char buf1[32], tuple_type[32];
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
57 int h, w, depth, maxval;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
58
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
59 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
60 if (!strcmp(buf1, "P4")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
61 avctx->pix_fmt = PIX_FMT_MONOWHITE;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
62 } else if (!strcmp(buf1, "P5")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
63 if (avctx->codec_id == CODEC_ID_PGMYUV)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
64 avctx->pix_fmt = PIX_FMT_YUV420P;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
65 else
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
66 avctx->pix_fmt = PIX_FMT_GRAY8;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
67 } else if (!strcmp(buf1, "P6")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
68 avctx->pix_fmt = PIX_FMT_RGB24;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
69 } else if (!strcmp(buf1, "P7")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
70 w = -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
71 h = -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
72 maxval = -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
73 depth = -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
74 tuple_type[0] = '\0';
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
75 for(;;) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
76 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
77 if (!strcmp(buf1, "WIDTH")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
78 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
79 w = strtol(buf1, NULL, 10);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
80 } else if (!strcmp(buf1, "HEIGHT")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
81 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
82 h = strtol(buf1, NULL, 10);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
83 } else if (!strcmp(buf1, "DEPTH")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
84 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
85 depth = strtol(buf1, NULL, 10);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
86 } else if (!strcmp(buf1, "MAXVAL")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
87 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
88 maxval = strtol(buf1, NULL, 10);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
89 } else if (!strcmp(buf1, "TUPLETYPE")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
90 pnm_get(s, tuple_type, sizeof(tuple_type));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
91 } else if (!strcmp(buf1, "ENDHDR")) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
92 break;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
93 } else {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
94 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
95 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
96 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
97 /* check that all tags are present */
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
98 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
99 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
100
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
101 avctx->width = w;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
102 avctx->height = h;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
103 if (depth == 1) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
104 if (maxval == 1)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
105 avctx->pix_fmt = PIX_FMT_MONOWHITE;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
106 else
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
107 avctx->pix_fmt = PIX_FMT_GRAY8;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
108 } else if (depth == 3) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
109 avctx->pix_fmt = PIX_FMT_RGB24;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
110 } else if (depth == 4) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
111 avctx->pix_fmt = PIX_FMT_RGB32;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
112 } else {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
113 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
114 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
115 return 0;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
116 } else {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
117 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
118 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
119 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
120 avctx->width = atoi(buf1);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
121 if (avctx->width <= 0)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
122 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
123 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
124 avctx->height = atoi(buf1);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
125 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
126 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
127 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
128 pnm_get(s, buf1, sizeof(buf1));
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
129 s->maxval = atoi(buf1);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
130 if(s->maxval >= 256 && avctx->pix_fmt == PIX_FMT_GRAY8) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
131 avctx->pix_fmt = PIX_FMT_GRAY16BE;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
132 if (s->maxval != 65535)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
133 avctx->pix_fmt = PIX_FMT_GRAY16;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
134 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
135 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
136 /* more check if YUV420 */
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
137 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
138 if ((avctx->width & 1) != 0)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
139 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
140 h = (avctx->height * 2);
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
141 if ((h % 3) != 0)
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
142 return -1;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
143 h /= 3;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
144 avctx->height = h;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
145 }
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
146 return 0;
95934eef9589 move pnm parser in its own file
aurel
parents:
diff changeset
147 }