00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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 };