00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include "parser.h" 
00023 #include "pnm.h"
00024 
00025 
00026 static int pnm_parse(AVCodecParserContext *s, AVCodecContext *avctx,
00027                      const uint8_t **poutbuf, int *poutbuf_size,
00028                      const uint8_t *buf, int buf_size)
00029 {
00030     ParseContext *pc = s->priv_data;
00031     PNMContext pnmctx;
00032     int next;
00033 
00034     for (; pc->overread > 0; pc->overread--) {
00035         pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00036     }
00037 retry:
00038     if (pc->index) {
00039         pnmctx.bytestream_start =
00040         pnmctx.bytestream       = pc->buffer;
00041         pnmctx.bytestream_end   = pc->buffer + pc->index;
00042     } else {
00043         pnmctx.bytestream_start =
00044         pnmctx.bytestream       = (uint8_t *) buf; 
00045         pnmctx.bytestream_end   = (uint8_t *) buf + buf_size;
00046     }
00047     if (ff_pnm_decode_header(avctx, &pnmctx) < 0) {
00048         if (pnmctx.bytestream < pnmctx.bytestream_end) {
00049             if (pc->index) {
00050                 pc->index = 0;
00051             } else {
00052                 buf++;
00053                 buf_size--;
00054             }
00055             goto retry;
00056         }
00057 #if 0
00058         if (pc->index && pc->index * 2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index) {
00059             memcpy(pc->buffer + pc->index, buf, pc->index);
00060             pc->index += pc->index;
00061             buf       += pc->index;
00062             buf_size  -= pc->index;
00063             goto retry;
00064         }
00065 #endif
00066         next = END_NOT_FOUND;
00067     } else {
00068         next = pnmctx.bytestream - pnmctx.bytestream_start
00069                + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00070         if (pnmctx.bytestream_start != buf)
00071             next -= pc->index;
00072         if (next > buf_size)
00073             next = END_NOT_FOUND;
00074     }
00075 
00076     if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00077         *poutbuf      = NULL;
00078         *poutbuf_size = 0;
00079         return buf_size;
00080     }
00081     *poutbuf      = buf;
00082     *poutbuf_size = buf_size;
00083     return next;
00084 }
00085 
00086 AVCodecParser ff_pnm_parser = {
00087     { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
00088     sizeof(ParseContext),
00089     NULL,
00090     pnm_parse,
00091     ff_parse_close,
00092 };