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