libavcodec/mjpeg_parser.c
b16560a3
 /*
  * MJPEG parser
406792e7
  * Copyright (c) 2000, 2001 Fabrice Bellard
b16560a3
  * Copyright (c) 2003 Alex Beregszaszi
  * Copyright (c) 2003-2004 Michael Niedermayer
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
b16560a3
  * MJPEG parser.
  */
 
 #include "parser.h"
 
429b3cd6
 typedef struct MJPEGParserContext{
     ParseContext pc;
1af3571e
     int size;
429b3cd6
 }MJPEGParserContext;
b16560a3
 
 /**
58c42af7
  * Find the end of the current frame in the bitstream.
b16560a3
  * @return the position of the first byte of the next frame, or -1
  */
429b3cd6
 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
     ParseContext *pc= &m->pc;
b16560a3
     int vop_found, i;
1af3571e
     uint32_t state;
b16560a3
 
     vop_found= pc->frame_start_found;
     state= pc->state;
 
     i=0;
     if(!vop_found){
1af3571e
         for(i=0; i<buf_size;){
b16560a3
             state= (state<<8) | buf[i];
1af3571e
             if(state>=0xFFC00000 && state<=0xFFFEFFFF){
                 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
                     i++;
                     vop_found=1;
                     break;
                 }else if(state<0xFFD00000 || state>0xFFD9FFFF){
                     m->size= (state&0xFFFF)-1;
                 }
b16560a3
             }
1af3571e
             if(m->size>0){
                 int size= FFMIN(buf_size-i, m->size);
                 i+=size;
                 m->size-=size;
                 state=0;
                 continue;
             }else
                 i++;
b16560a3
         }
     }
 
     if(vop_found){
         /* EOF considered as end of frame */
         if (buf_size == 0)
             return 0;
1af3571e
         for(; i<buf_size;){
b16560a3
             state= (state<<8) | buf[i];
1af3571e
             if(state>=0xFFC00000 && state<=0xFFFEFFFF){
                 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
                     pc->frame_start_found=0;
                     pc->state=0;
                     return i-3;
                 } else if(state<0xFFD00000 || state>0xFFD9FFFF){
                     m->size= (state&0xFFFF)-1;
                 }
b16560a3
             }
1af3571e
             if(m->size>0){
                 int size= FFMIN(buf_size-i, m->size);
                 i+=size;
                 m->size-=size;
                 state=0;
                 continue;
             }else
                 i++;
b16560a3
         }
     }
     pc->frame_start_found= vop_found;
     pc->state= state;
     return END_NOT_FOUND;
 }
 
 static int jpeg_parse(AVCodecParserContext *s,
6b620372
                       AVCodecContext *avctx,
                       const uint8_t **poutbuf, int *poutbuf_size,
                       const uint8_t *buf, int buf_size)
b16560a3
 {
429b3cd6
     MJPEGParserContext *m = s->priv_data;
     ParseContext *pc = &m->pc;
b16560a3
     int next;
 
4546bf41
     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
         next= buf_size;
     }else{
429b3cd6
         next= find_frame_end(m, buf, buf_size);
b16560a3
 
ee5b26f5
         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
             *poutbuf = NULL;
             *poutbuf_size = 0;
             return buf_size;
         }
4546bf41
     }
b16560a3
 
     *poutbuf = buf;
     *poutbuf_size = buf_size;
     return next;
 }
 
 
e7e2df27
 AVCodecParser ff_mjpeg_parser = {
5511ad14
     .codec_ids      = { CODEC_ID_MJPEG },
988f585f
     .priv_data_size = sizeof(MJPEGParserContext),
5511ad14
     .parser_parse   = jpeg_parse,
     .parser_close   = ff_parse_close,
b16560a3
 };