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 "pgm",
00200 AVMEDIA_TYPE_VIDEO,
00201 CODEC_ID_PGM,
00202 sizeof(PNMContext),
00203 ff_pnm_init,
00204 NULL,
00205 ff_pnm_end,
00206 pnm_decode_frame,
00207 CODEC_CAP_DR1,
00208 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
00209 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00210 };
00211 #endif
00212
00213 #if CONFIG_PGMYUV_DECODER
00214 AVCodec ff_pgmyuv_decoder = {
00215 "pgmyuv",
00216 AVMEDIA_TYPE_VIDEO,
00217 CODEC_ID_PGMYUV,
00218 sizeof(PNMContext),
00219 ff_pnm_init,
00220 NULL,
00221 ff_pnm_end,
00222 pnm_decode_frame,
00223 CODEC_CAP_DR1,
00224 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00225 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00226 };
00227 #endif
00228
00229 #if CONFIG_PPM_DECODER
00230 AVCodec ff_ppm_decoder = {
00231 "ppm",
00232 AVMEDIA_TYPE_VIDEO,
00233 CODEC_ID_PPM,
00234 sizeof(PNMContext),
00235 ff_pnm_init,
00236 NULL,
00237 ff_pnm_end,
00238 pnm_decode_frame,
00239 CODEC_CAP_DR1,
00240 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
00241 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
00242 };
00243 #endif
00244
00245 #if CONFIG_PBM_DECODER
00246 AVCodec ff_pbm_decoder = {
00247 "pbm",
00248 AVMEDIA_TYPE_VIDEO,
00249 CODEC_ID_PBM,
00250 sizeof(PNMContext),
00251 ff_pnm_init,
00252 NULL,
00253 ff_pnm_end,
00254 pnm_decode_frame,
00255 CODEC_CAP_DR1,
00256 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00257 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00258 };
00259 #endif
00260
00261 #if CONFIG_PAM_DECODER
00262 AVCodec ff_pam_decoder = {
00263 "pam",
00264 AVMEDIA_TYPE_VIDEO,
00265 CODEC_ID_PAM,
00266 sizeof(PNMContext),
00267 ff_pnm_init,
00268 NULL,
00269 ff_pnm_end,
00270 pnm_decode_frame,
00271 CODEC_CAP_DR1,
00272 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
00273 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
00274 };
00275 #endif