00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/imgutils.h"
00023 #include "avcodec.h"
00024 #include "pnm.h"
00025
00026 static inline int pnm_space(int c)
00027 {
00028 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
00029 }
00030
00031 static void pnm_get(PNMContext *sc, char *str, int buf_size)
00032 {
00033 char *s;
00034 int c;
00035
00036
00037 for (;;) {
00038 c = *sc->bytestream++;
00039 if (c == '#') {
00040 do {
00041 c = *sc->bytestream++;
00042 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
00043 } else if (!pnm_space(c)) {
00044 break;
00045 }
00046 }
00047
00048 s = str;
00049 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
00050 if ((s - str) < buf_size - 1)
00051 *s++ = c;
00052 c = *sc->bytestream++;
00053 }
00054 *s = '\0';
00055 }
00056
00057 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
00058 {
00059 char buf1[32], tuple_type[32];
00060 int h, w, depth, maxval;
00061
00062 pnm_get(s, buf1, sizeof(buf1));
00063 s->type= buf1[1]-'0';
00064 if(buf1[0] != 'P')
00065 return -1;
00066
00067 if (s->type==1 || s->type==4) {
00068 avctx->pix_fmt = PIX_FMT_MONOWHITE;
00069 } else if (s->type==2 || s->type==5) {
00070 if (avctx->codec_id == CODEC_ID_PGMYUV)
00071 avctx->pix_fmt = PIX_FMT_YUV420P;
00072 else
00073 avctx->pix_fmt = PIX_FMT_GRAY8;
00074 } else if (s->type==3 || s->type==6) {
00075 avctx->pix_fmt = PIX_FMT_RGB24;
00076 } else if (s->type==7) {
00077 w = -1;
00078 h = -1;
00079 maxval = -1;
00080 depth = -1;
00081 tuple_type[0] = '\0';
00082 for (;;) {
00083 pnm_get(s, buf1, sizeof(buf1));
00084 if (!strcmp(buf1, "WIDTH")) {
00085 pnm_get(s, buf1, sizeof(buf1));
00086 w = strtol(buf1, NULL, 10);
00087 } else if (!strcmp(buf1, "HEIGHT")) {
00088 pnm_get(s, buf1, sizeof(buf1));
00089 h = strtol(buf1, NULL, 10);
00090 } else if (!strcmp(buf1, "DEPTH")) {
00091 pnm_get(s, buf1, sizeof(buf1));
00092 depth = strtol(buf1, NULL, 10);
00093 } else if (!strcmp(buf1, "MAXVAL")) {
00094 pnm_get(s, buf1, sizeof(buf1));
00095 maxval = strtol(buf1, NULL, 10);
00096 } else if (!strcmp(buf1, "TUPLETYPE")) {
00097 pnm_get(s, tuple_type, sizeof(tuple_type));
00098 } else if (!strcmp(buf1, "ENDHDR")) {
00099 break;
00100 } else {
00101 return -1;
00102 }
00103 }
00104
00105 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
00106 return -1;
00107
00108 avctx->width = w;
00109 avctx->height = h;
00110 s->maxval = maxval;
00111 if (depth == 1) {
00112 if (maxval == 1)
00113 avctx->pix_fmt = PIX_FMT_MONOWHITE;
00114 else
00115 avctx->pix_fmt = PIX_FMT_GRAY8;
00116 } else if (depth == 3) {
00117 if (maxval < 256) {
00118 avctx->pix_fmt = PIX_FMT_RGB24;
00119 } else {
00120 av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
00121 avctx->pix_fmt = PIX_FMT_NONE;
00122 return -1;
00123 }
00124 } else if (depth == 4) {
00125 avctx->pix_fmt = PIX_FMT_RGB32;
00126 } else {
00127 return -1;
00128 }
00129 return 0;
00130 } else {
00131 return -1;
00132 }
00133 pnm_get(s, buf1, sizeof(buf1));
00134 avctx->width = atoi(buf1);
00135 if (avctx->width <= 0)
00136 return -1;
00137 pnm_get(s, buf1, sizeof(buf1));
00138 avctx->height = atoi(buf1);
00139 if(avctx->height <= 0 || av_image_check_size(avctx->width, avctx->height, 0, avctx))
00140 return -1;
00141 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
00142 pnm_get(s, buf1, sizeof(buf1));
00143 s->maxval = atoi(buf1);
00144 if (s->maxval <= 0) {
00145 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
00146 s->maxval = 255;
00147 }
00148 if (s->maxval >= 256) {
00149 if (avctx->pix_fmt == PIX_FMT_GRAY8) {
00150 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00151 if (s->maxval != 65535)
00152 avctx->pix_fmt = PIX_FMT_GRAY16;
00153 } else if (avctx->pix_fmt == PIX_FMT_RGB24) {
00154 if (s->maxval > 255)
00155 avctx->pix_fmt = PIX_FMT_RGB48BE;
00156 } else {
00157 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
00158 avctx->pix_fmt = PIX_FMT_NONE;
00159 return -1;
00160 }
00161 }
00162 }else
00163 s->maxval=1;
00164
00165 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
00166 if ((avctx->width & 1) != 0)
00167 return -1;
00168 h = (avctx->height * 2);
00169 if ((h % 3) != 0)
00170 return -1;
00171 h /= 3;
00172 avctx->height = h;
00173 }
00174 return 0;
00175 }
00176
00177 av_cold int ff_pnm_end(AVCodecContext *avctx)
00178 {
00179 PNMContext *s = avctx->priv_data;
00180
00181 if (s->picture.data[0])
00182 avctx->release_buffer(avctx, &s->picture);
00183
00184 return 0;
00185 }
00186
00187 av_cold int ff_pnm_init(AVCodecContext *avctx)
00188 {
00189 PNMContext *s = avctx->priv_data;
00190
00191 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00192 avctx->coded_frame = (AVFrame*)&s->picture;
00193
00194 return 0;
00195 }