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