Mercurial > libavcodec.hg
annotate pnm.c @ 10507:ca71c924b65f libavcodec
cosmetics : fix indentation.
author | jai_menon |
---|---|
date | Tue, 10 Nov 2009 14:37:42 +0000 |
parents | 059265d3cc65 |
children | d6860312274c |
rev | line source |
---|---|
4978 | 1 /* |
2 * PNM image format | |
8629
04423b2f6e0b
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
7865
diff
changeset
|
3 * Copyright (c) 2002, 2003 Fabrice Bellard |
4978 | 4 * |
5 * This file is part of FFmpeg. | |
6 * | |
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. | |
11 * | |
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. | |
16 * | |
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 | |
20 */ | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
21 |
4978 | 22 #include "avcodec.h" |
23 #include "pnm.h" | |
24 | |
25 static inline int pnm_space(int c) | |
26 { | |
6750 | 27 return c == ' ' || c == '\n' || c == '\r' || c == '\t'; |
4978 | 28 } |
29 | |
30 static void pnm_get(PNMContext *sc, char *str, int buf_size) | |
31 { | |
32 char *s; | |
33 int c; | |
34 | |
35 /* skip spaces and comments */ | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
36 for (;;) { |
4978 | 37 c = *sc->bytestream++; |
38 if (c == '#') { | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
39 do { |
4978 | 40 c = *sc->bytestream++; |
41 } while (c != '\n' && sc->bytestream < sc->bytestream_end); | |
42 } else if (!pnm_space(c)) { | |
43 break; | |
44 } | |
45 } | |
46 | |
47 s = str; | |
48 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) { | |
49 if ((s - str) < buf_size - 1) | |
50 *s++ = c; | |
51 c = *sc->bytestream++; | |
52 } | |
53 *s = '\0'; | |
54 } | |
55 | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
56 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s) |
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
57 { |
4978 | 58 char buf1[32], tuple_type[32]; |
59 int h, w, depth, maxval; | |
60 | |
61 pnm_get(s, buf1, sizeof(buf1)); | |
62 if (!strcmp(buf1, "P4")) { | |
63 avctx->pix_fmt = PIX_FMT_MONOWHITE; | |
64 } else if (!strcmp(buf1, "P5")) { | |
65 if (avctx->codec_id == CODEC_ID_PGMYUV) | |
66 avctx->pix_fmt = PIX_FMT_YUV420P; | |
67 else | |
68 avctx->pix_fmt = PIX_FMT_GRAY8; | |
69 } else if (!strcmp(buf1, "P6")) { | |
70 avctx->pix_fmt = PIX_FMT_RGB24; | |
71 } else if (!strcmp(buf1, "P7")) { | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
72 w = -1; |
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
73 h = -1; |
4978 | 74 maxval = -1; |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
75 depth = -1; |
4978 | 76 tuple_type[0] = '\0'; |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
77 for (;;) { |
4978 | 78 pnm_get(s, buf1, sizeof(buf1)); |
79 if (!strcmp(buf1, "WIDTH")) { | |
80 pnm_get(s, buf1, sizeof(buf1)); | |
81 w = strtol(buf1, NULL, 10); | |
82 } else if (!strcmp(buf1, "HEIGHT")) { | |
83 pnm_get(s, buf1, sizeof(buf1)); | |
84 h = strtol(buf1, NULL, 10); | |
85 } else if (!strcmp(buf1, "DEPTH")) { | |
86 pnm_get(s, buf1, sizeof(buf1)); | |
87 depth = strtol(buf1, NULL, 10); | |
88 } else if (!strcmp(buf1, "MAXVAL")) { | |
89 pnm_get(s, buf1, sizeof(buf1)); | |
90 maxval = strtol(buf1, NULL, 10); | |
91 } else if (!strcmp(buf1, "TUPLETYPE")) { | |
92 pnm_get(s, tuple_type, sizeof(tuple_type)); | |
93 } else if (!strcmp(buf1, "ENDHDR")) { | |
94 break; | |
95 } else { | |
96 return -1; | |
97 } | |
98 } | |
99 /* check that all tags are present */ | |
100 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h)) | |
101 return -1; | |
102 | |
10459
a6bb56636f90
whitespace cosmetics: K&R coding style, prettyprinting
diego
parents:
9145
diff
changeset
|
103 avctx->width = w; |
4978 | 104 avctx->height = h; |
105 if (depth == 1) { | |
106 if (maxval == 1) | |
107 avctx->pix_fmt = PIX_FMT_MONOWHITE; | |
108 else | |
109 avctx->pix_fmt = PIX_FMT_GRAY8; | |
110 } else if (depth == 3) { | |
7859
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
111 if (maxval < 256) { |
4978 | 112 avctx->pix_fmt = PIX_FMT_RGB24; |
7859
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
113 } else { |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
114 av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n"); |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
115 avctx->pix_fmt = PIX_FMT_NONE; |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
116 return -1; |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
117 } |
4978 | 118 } else if (depth == 4) { |
119 avctx->pix_fmt = PIX_FMT_RGB32; | |
120 } else { | |
121 return -1; | |
122 } | |
123 return 0; | |
124 } else { | |
125 return -1; | |
126 } | |
127 pnm_get(s, buf1, sizeof(buf1)); | |
128 avctx->width = atoi(buf1); | |
129 if (avctx->width <= 0) | |
130 return -1; | |
131 pnm_get(s, buf1, sizeof(buf1)); | |
132 avctx->height = atoi(buf1); | |
133 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height)) | |
134 return -1; | |
135 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) { | |
136 pnm_get(s, buf1, sizeof(buf1)); | |
137 s->maxval = atoi(buf1); | |
7859
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
138 if (s->maxval >= 256) { |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
139 if (avctx->pix_fmt == PIX_FMT_GRAY8) { |
7865 | 140 avctx->pix_fmt = PIX_FMT_GRAY16BE; |
141 if (s->maxval != 65535) | |
142 avctx->pix_fmt = PIX_FMT_GRAY16; | |
9145
de31b10455cc
pnm: Add missing 'else'. Fixes decoding for 16-bit pgm.
jbr
parents:
9002
diff
changeset
|
143 } else if (avctx->pix_fmt == PIX_FMT_RGB24) { |
9002 | 144 if (s->maxval > 255) |
145 avctx->pix_fmt = PIX_FMT_RGB48BE; | |
7859
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
146 } else { |
9002 | 147 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n"); |
7859
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
148 avctx->pix_fmt = PIX_FMT_NONE; |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
149 return -1; |
48d00b406a26
Return error when trying to decode non-grayscale 16-bit PNM images.
jbr
parents:
6750
diff
changeset
|
150 } |
4978 | 151 } |
152 } | |
153 /* more check if YUV420 */ | |
154 if (avctx->pix_fmt == PIX_FMT_YUV420P) { | |
155 if ((avctx->width & 1) != 0) | |
156 return -1; | |
157 h = (avctx->height * 2); | |
158 if ((h % 3) != 0) | |
159 return -1; | |
160 h /= 3; | |
161 avctx->height = h; | |
162 } | |
163 return 0; | |
164 } | |
10460
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
165 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
166 av_cold int ff_pnm_end(AVCodecContext *avctx) |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
167 { |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
168 PNMContext *s = avctx->priv_data; |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
169 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
170 if (s->picture.data[0]) |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
171 avctx->release_buffer(avctx, &s->picture); |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
172 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
173 return 0; |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
174 } |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
175 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
176 av_cold int ff_pnm_init(AVCodecContext *avctx) |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
177 { |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
178 PNMContext *s = avctx->priv_data; |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
179 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
180 avcodec_get_frame_defaults((AVFrame*)&s->picture); |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
181 avctx->coded_frame = (AVFrame*)&s->picture; |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
182 |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
183 return 0; |
059265d3cc65
Move PNM init/end functions to the PNM common code.
diego
parents:
10459
diff
changeset
|
184 } |