2007-05-10 22:37:36 +00:00
|
|
|
/*
|
|
|
|
* MJPEG parser
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard
|
2007-05-10 22:37:36 +00:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2007-05-10 22:37:36 +00:00
|
|
|
* MJPEG parser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "parser.h"
|
|
|
|
|
2011-10-29 20:27:01 +00:00
|
|
|
typedef struct MJPEGParserContext{
|
|
|
|
ParseContext pc;
|
2011-10-29 21:54:21 +00:00
|
|
|
int size;
|
2011-10-29 20:27:01 +00:00
|
|
|
}MJPEGParserContext;
|
2007-05-10 22:37:36 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-07 12:03:53 +00:00
|
|
|
* Find the end of the current frame in the bitstream.
|
2007-05-10 22:37:36 +00:00
|
|
|
* @return the position of the first byte of the next frame, or -1
|
|
|
|
*/
|
2011-10-29 20:27:01 +00:00
|
|
|
static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
|
|
|
|
ParseContext *pc= &m->pc;
|
2007-05-10 22:37:36 +00:00
|
|
|
int vop_found, i;
|
2011-10-29 21:54:21 +00:00
|
|
|
uint32_t state;
|
2007-05-10 22:37:36 +00:00
|
|
|
|
|
|
|
vop_found= pc->frame_start_found;
|
|
|
|
state= pc->state;
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
if(!vop_found){
|
2011-10-29 21:54:21 +00:00
|
|
|
for(i=0; i<buf_size;){
|
2007-05-10 22:37:36 +00:00
|
|
|
state= (state<<8) | buf[i];
|
2011-10-29 21:54:21 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-05-10 22:37:36 +00:00
|
|
|
}
|
2011-10-29 21:54:21 +00:00
|
|
|
if(m->size>0){
|
|
|
|
int size= FFMIN(buf_size-i, m->size);
|
|
|
|
i+=size;
|
|
|
|
m->size-=size;
|
|
|
|
state=0;
|
|
|
|
continue;
|
|
|
|
}else
|
|
|
|
i++;
|
2007-05-10 22:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vop_found){
|
|
|
|
/* EOF considered as end of frame */
|
|
|
|
if (buf_size == 0)
|
|
|
|
return 0;
|
2011-10-29 21:54:21 +00:00
|
|
|
for(; i<buf_size;){
|
2007-05-10 22:37:36 +00:00
|
|
|
state= (state<<8) | buf[i];
|
2011-10-29 21:54:21 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-05-10 22:37:36 +00:00
|
|
|
}
|
2011-10-29 21:54:21 +00:00
|
|
|
if(m->size>0){
|
|
|
|
int size= FFMIN(buf_size-i, m->size);
|
|
|
|
i+=size;
|
|
|
|
m->size-=size;
|
|
|
|
state=0;
|
|
|
|
continue;
|
|
|
|
}else
|
|
|
|
i++;
|
2007-05-10 22:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pc->frame_start_found= vop_found;
|
|
|
|
pc->state= state;
|
|
|
|
return END_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jpeg_parse(AVCodecParserContext *s,
|
2009-08-02 08:47:02 +00:00
|
|
|
AVCodecContext *avctx,
|
|
|
|
const uint8_t **poutbuf, int *poutbuf_size,
|
|
|
|
const uint8_t *buf, int buf_size)
|
2007-05-10 22:37:36 +00:00
|
|
|
{
|
2011-10-29 20:27:01 +00:00
|
|
|
MJPEGParserContext *m = s->priv_data;
|
|
|
|
ParseContext *pc = &m->pc;
|
2007-05-10 22:37:36 +00:00
|
|
|
int next;
|
|
|
|
|
2009-08-02 08:46:11 +00:00
|
|
|
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
|
|
|
|
next= buf_size;
|
|
|
|
}else{
|
2011-10-29 20:27:01 +00:00
|
|
|
next= find_frame_end(m, buf, buf_size);
|
2007-05-10 22:37:36 +00:00
|
|
|
|
2009-08-02 08:46:43 +00:00
|
|
|
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
|
|
|
|
*poutbuf = NULL;
|
|
|
|
*poutbuf_size = 0;
|
|
|
|
return buf_size;
|
|
|
|
}
|
2009-08-02 08:46:11 +00:00
|
|
|
}
|
2007-05-10 22:37:36 +00:00
|
|
|
|
|
|
|
*poutbuf = buf;
|
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodecParser ff_mjpeg_parser = {
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_ids = { AV_CODEC_ID_MJPEG },
|
2011-11-03 01:01:37 +00:00
|
|
|
.priv_data_size = sizeof(MJPEGParserContext),
|
2011-11-02 08:34:41 +00:00
|
|
|
.parser_parse = jpeg_parse,
|
|
|
|
.parser_close = ff_parse_close,
|
2007-05-10 22:37:36 +00:00
|
|
|
};
|