00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "internal.h"
00024 #include "put_bits.h"
00025 #include "pnm.h"
00026
00027
00028 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
00029 int *got_frame, AVPacket *avpkt)
00030 {
00031 const uint8_t *buf = avpkt->data;
00032 int buf_size = avpkt->size;
00033 PNMContext * const s = avctx->priv_data;
00034 AVFrame *picture = data;
00035 AVFrame * const p = &s->picture;
00036 int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
00037 unsigned char *ptr;
00038 int components, sample_len, ret;
00039
00040 s->bytestream_start =
00041 s->bytestream = (uint8_t *)buf;
00042 s->bytestream_end = (uint8_t *)buf + buf_size;
00043
00044 if (ff_pnm_decode_header(avctx, s) < 0)
00045 return AVERROR_INVALIDDATA;
00046
00047 if (p->data[0])
00048 avctx->release_buffer(avctx, p);
00049
00050 p->reference = 0;
00051 if ((ret = ff_get_buffer(avctx, p)) < 0) {
00052 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00053 return ret;
00054 }
00055 p->pict_type = AV_PICTURE_TYPE_I;
00056 p->key_frame = 1;
00057
00058 switch (avctx->pix_fmt) {
00059 default:
00060 return AVERROR_INVALIDDATA;
00061 case AV_PIX_FMT_RGBA64BE:
00062 n = avctx->width * 8;
00063 components=4;
00064 sample_len=16;
00065 goto do_read;
00066 case AV_PIX_FMT_RGB48BE:
00067 n = avctx->width * 6;
00068 components=3;
00069 sample_len=16;
00070 goto do_read;
00071 case AV_PIX_FMT_RGBA:
00072 n = avctx->width * 4;
00073 components=4;
00074 sample_len=8;
00075 goto do_read;
00076 case AV_PIX_FMT_RGB24:
00077 n = avctx->width * 3;
00078 components=3;
00079 sample_len=8;
00080 goto do_read;
00081 case AV_PIX_FMT_GRAY8:
00082 n = avctx->width;
00083 components=1;
00084 sample_len=8;
00085 if (s->maxval < 255)
00086 upgrade = 1;
00087 goto do_read;
00088 case AV_PIX_FMT_GRAY8A:
00089 n = avctx->width * 2;
00090 components=2;
00091 sample_len=8;
00092 goto do_read;
00093 case AV_PIX_FMT_GRAY16BE:
00094 case AV_PIX_FMT_GRAY16LE:
00095 n = avctx->width * 2;
00096 components=1;
00097 sample_len=16;
00098 if (s->maxval < 65535)
00099 upgrade = 2;
00100 goto do_read;
00101 case AV_PIX_FMT_MONOWHITE:
00102 case AV_PIX_FMT_MONOBLACK:
00103 n = (avctx->width + 7) >> 3;
00104 components=1;
00105 sample_len=1;
00106 is_mono = 1;
00107 do_read:
00108 ptr = p->data[0];
00109 linesize = p->linesize[0];
00110 if (s->bytestream + n * avctx->height > s->bytestream_end)
00111 return AVERROR_INVALIDDATA;
00112 if(s->type < 4 || (is_mono && s->type==7)){
00113 for (i=0; i<avctx->height; i++) {
00114 PutBitContext pb;
00115 init_put_bits(&pb, ptr, linesize);
00116 for(j=0; j<avctx->width * components; j++){
00117 unsigned int c=0;
00118 int v=0;
00119 if(s->type < 4)
00120 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
00121 s->bytestream++;
00122 if(s->bytestream >= s->bytestream_end)
00123 return AVERROR_INVALIDDATA;
00124 if (is_mono) {
00125
00126 v = (*s->bytestream++)&1;
00127 } else {
00128
00129 do {
00130 v = 10*v + c;
00131 c = (*s->bytestream++) - '0';
00132 } while (c <= 9);
00133 }
00134 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
00135 }
00136 flush_put_bits(&pb);
00137 ptr+= linesize;
00138 }
00139 }else{
00140 for (i = 0; i < avctx->height; i++) {
00141 if (!upgrade)
00142 memcpy(ptr, s->bytestream, n);
00143 else if (upgrade == 1) {
00144 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
00145 for (j = 0; j < n; j++)
00146 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
00147 } else if (upgrade == 2) {
00148 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
00149 for (j = 0; j < n / 2; j++) {
00150 v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
00151 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
00152 }
00153 }
00154 s->bytestream += n;
00155 ptr += linesize;
00156 }
00157 }
00158 break;
00159 case AV_PIX_FMT_YUV420P:
00160 {
00161 unsigned char *ptr1, *ptr2;
00162
00163 n = avctx->width;
00164 ptr = p->data[0];
00165 linesize = p->linesize[0];
00166 if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
00167 return AVERROR_INVALIDDATA;
00168 for (i = 0; i < avctx->height; i++) {
00169 memcpy(ptr, s->bytestream, n);
00170 s->bytestream += n;
00171 ptr += linesize;
00172 }
00173 ptr1 = p->data[1];
00174 ptr2 = p->data[2];
00175 n >>= 1;
00176 h = avctx->height >> 1;
00177 for (i = 0; i < h; i++) {
00178 memcpy(ptr1, s->bytestream, n);
00179 s->bytestream += n;
00180 memcpy(ptr2, s->bytestream, n);
00181 s->bytestream += n;
00182 ptr1 += p->linesize[1];
00183 ptr2 += p->linesize[2];
00184 }
00185 }
00186 break;
00187 }
00188 *picture = s->picture;
00189 *got_frame = 1;
00190
00191 return s->bytestream - s->bytestream_start;
00192 }
00193
00194
00195 #if CONFIG_PGM_DECODER
00196 AVCodec ff_pgm_decoder = {
00197 .name = "pgm",
00198 .type = AVMEDIA_TYPE_VIDEO,
00199 .id = AV_CODEC_ID_PGM,
00200 .priv_data_size = sizeof(PNMContext),
00201 .init = ff_pnm_init,
00202 .close = ff_pnm_end,
00203 .decode = pnm_decode_frame,
00204 .capabilities = CODEC_CAP_DR1,
00205 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00206 };
00207 #endif
00208
00209 #if CONFIG_PGMYUV_DECODER
00210 AVCodec ff_pgmyuv_decoder = {
00211 .name = "pgmyuv",
00212 .type = AVMEDIA_TYPE_VIDEO,
00213 .id = AV_CODEC_ID_PGMYUV,
00214 .priv_data_size = sizeof(PNMContext),
00215 .init = ff_pnm_init,
00216 .close = ff_pnm_end,
00217 .decode = pnm_decode_frame,
00218 .capabilities = CODEC_CAP_DR1,
00219 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00220 };
00221 #endif
00222
00223 #if CONFIG_PPM_DECODER
00224 AVCodec ff_ppm_decoder = {
00225 .name = "ppm",
00226 .type = AVMEDIA_TYPE_VIDEO,
00227 .id = AV_CODEC_ID_PPM,
00228 .priv_data_size = sizeof(PNMContext),
00229 .init = ff_pnm_init,
00230 .close = ff_pnm_end,
00231 .decode = pnm_decode_frame,
00232 .capabilities = CODEC_CAP_DR1,
00233 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
00234 };
00235 #endif
00236
00237 #if CONFIG_PBM_DECODER
00238 AVCodec ff_pbm_decoder = {
00239 .name = "pbm",
00240 .type = AVMEDIA_TYPE_VIDEO,
00241 .id = AV_CODEC_ID_PBM,
00242 .priv_data_size = sizeof(PNMContext),
00243 .init = ff_pnm_init,
00244 .close = ff_pnm_end,
00245 .decode = pnm_decode_frame,
00246 .capabilities = CODEC_CAP_DR1,
00247 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00248 };
00249 #endif
00250
00251 #if CONFIG_PAM_DECODER
00252 AVCodec ff_pam_decoder = {
00253 .name = "pam",
00254 .type = AVMEDIA_TYPE_VIDEO,
00255 .id = AV_CODEC_ID_PAM,
00256 .priv_data_size = sizeof(PNMContext),
00257 .init = ff_pnm_init,
00258 .close = ff_pnm_end,
00259 .decode = pnm_decode_frame,
00260 .capabilities = CODEC_CAP_DR1,
00261 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
00262 };
00263 #endif