- n = avctx->width;
- ptr = p->data[0];
- linesize = p->linesize[0];
- if(s->bytestream + n*avctx->height*3/2 > s->bytestream_end)
- return -1;
- for(i = 0; i < avctx->height; i++) {
- memcpy(ptr, s->bytestream, n);
- s->bytestream += n;
- ptr += linesize;
- }
- ptr1 = p->data[1];
- ptr2 = p->data[2];
- n >>= 1;
- h = avctx->height >> 1;
- for(i = 0; i < h; i++) {
- memcpy(ptr1, s->bytestream, n);
- s->bytestream += n;
- memcpy(ptr2, s->bytestream, n);
- s->bytestream += n;
- ptr1 += p->linesize[1];
- ptr2 += p->linesize[2];
+int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
+{
+ char buf1[32], tuple_type[32];
+ int h, w, depth, maxval;
+
+ pnm_get(s, buf1, sizeof(buf1));
+ s->type= buf1[1]-'0';
+ if(buf1[0] != 'P')
+ return AVERROR_INVALIDDATA;
+
+ if (s->type==1 || s->type==4) {
+ avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
+ } else if (s->type==2 || s->type==5) {
+ if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
+ avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+ else
+ avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+ } else if (s->type==3 || s->type==6) {
+ avctx->pix_fmt = AV_PIX_FMT_RGB24;
+ } else if (s->type==7) {
+ w = -1;
+ h = -1;
+ maxval = -1;
+ depth = -1;
+ tuple_type[0] = '\0';
+ for (;;) {
+ pnm_get(s, buf1, sizeof(buf1));
+ if (!strcmp(buf1, "WIDTH")) {
+ pnm_get(s, buf1, sizeof(buf1));
+ w = strtol(buf1, NULL, 10);
+ } else if (!strcmp(buf1, "HEIGHT")) {
+ pnm_get(s, buf1, sizeof(buf1));
+ h = strtol(buf1, NULL, 10);
+ } else if (!strcmp(buf1, "DEPTH")) {
+ pnm_get(s, buf1, sizeof(buf1));
+ depth = strtol(buf1, NULL, 10);
+ } else if (!strcmp(buf1, "MAXVAL")) {
+ pnm_get(s, buf1, sizeof(buf1));
+ maxval = strtol(buf1, NULL, 10);
+ } else if (!strcmp(buf1, "TUPLTYPE") ||
+ /* libavcodec used to write invalid files */
+ !strcmp(buf1, "TUPLETYPE")) {
+ pnm_get(s, tuple_type, sizeof(tuple_type));
+ } else if (!strcmp(buf1, "ENDHDR")) {
+ break;
+ } else {
+ return AVERROR_INVALIDDATA;