libavcodec/mjpeg_parser.c
Go to the documentation of this file.
00001 /*
00002  * MJPEG parser
00003  * Copyright (c) 2000, 2001 Fabrice Bellard
00004  * Copyright (c) 2003 Alex Beregszaszi
00005  * Copyright (c) 2003-2004 Michael Niedermayer
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00029 #include "parser.h"
00030 
00031 typedef struct MJPEGParserContext{
00032     ParseContext pc;
00033     int size;
00034 }MJPEGParserContext;
00035 
00040 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
00041     ParseContext *pc= &m->pc;
00042     int vop_found, i;
00043     uint32_t state;
00044 
00045     vop_found= pc->frame_start_found;
00046     state= pc->state;
00047 
00048     i=0;
00049     if(!vop_found){
00050         for(i=0; i<buf_size;){
00051             state= (state<<8) | buf[i];
00052             if(state>=0xFFC00000 && state<=0xFFFEFFFF){
00053                 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
00054                     i++;
00055                     vop_found=1;
00056                     break;
00057                 }else if(state<0xFFD00000 || state>0xFFD9FFFF){
00058                     m->size= (state&0xFFFF)-1;
00059                 }
00060             }
00061             if(m->size>0){
00062                 int size= FFMIN(buf_size-i, m->size);
00063                 i+=size;
00064                 m->size-=size;
00065                 state=0;
00066                 continue;
00067             }else
00068                 i++;
00069         }
00070     }
00071 
00072     if(vop_found){
00073         /* EOF considered as end of frame */
00074         if (buf_size == 0)
00075             return 0;
00076         for(; i<buf_size;){
00077             state= (state<<8) | buf[i];
00078             if(state>=0xFFC00000 && state<=0xFFFEFFFF){
00079                 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
00080                     pc->frame_start_found=0;
00081                     pc->state=0;
00082                     return i-3;
00083                 } else if(state<0xFFD00000 || state>0xFFD9FFFF){
00084                     m->size= (state&0xFFFF)-1;
00085                 }
00086             }
00087             if(m->size>0){
00088                 int size= FFMIN(buf_size-i, m->size);
00089                 i+=size;
00090                 m->size-=size;
00091                 state=0;
00092                 continue;
00093             }else
00094                 i++;
00095         }
00096     }
00097     pc->frame_start_found= vop_found;
00098     pc->state= state;
00099     return END_NOT_FOUND;
00100 }
00101 
00102 static int jpeg_parse(AVCodecParserContext *s,
00103                       AVCodecContext *avctx,
00104                       const uint8_t **poutbuf, int *poutbuf_size,
00105                       const uint8_t *buf, int buf_size)
00106 {
00107     MJPEGParserContext *m = s->priv_data;
00108     ParseContext *pc = &m->pc;
00109     int next;
00110 
00111     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00112         next= buf_size;
00113     }else{
00114         next= find_frame_end(m, buf, buf_size);
00115 
00116         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00117             *poutbuf = NULL;
00118             *poutbuf_size = 0;
00119             return buf_size;
00120         }
00121     }
00122 
00123     *poutbuf = buf;
00124     *poutbuf_size = buf_size;
00125     return next;
00126 }
00127 
00128 
00129 AVCodecParser ff_mjpeg_parser = {
00130     .codec_ids      = { CODEC_ID_MJPEG },
00131     .priv_data_size = sizeof(MJPEGParserContext),
00132     .parser_parse   = jpeg_parse,
00133     .parser_close   = ff_parse_close,
00134 };