3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 typedef struct MJPEGParserContext{
37 * Find the end of the current frame in the bitstream.
38 * @return the position of the first byte of the next frame, or -1
40 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
41 ParseContext *pc= &m->pc;
45 vop_found= pc->frame_start_found;
50 for(i=0; i<buf_size;){
51 state= (state<<8) | buf[i];
52 if(state>=0xFFC00000 && state<=0xFFFEFFFF){
53 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
57 }else if(state<0xFFD00000 || state>0xFFD9FFFF){
58 m->size= (state&0xFFFF)-1;
62 int size= FFMIN(buf_size-i, m->size);
73 /* EOF considered as end of frame */
77 state= (state<<8) | buf[i];
78 if(state>=0xFFC00000 && state<=0xFFFEFFFF){
79 if(state>=0xFFD80000 && state<=0xFFD8FFFF){
80 pc->frame_start_found=0;
83 } else if(state<0xFFD00000 || state>0xFFD9FFFF){
84 m->size= (state&0xFFFF)-1;
88 int size= FFMIN(buf_size-i, m->size);
97 pc->frame_start_found= vop_found;
102 static int jpeg_parse(AVCodecParserContext *s,
103 AVCodecContext *avctx,
104 const uint8_t **poutbuf, int *poutbuf_size,
105 const uint8_t *buf, int buf_size)
107 MJPEGParserContext *m = s->priv_data;
108 ParseContext *pc = &m->pc;
111 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
114 next= find_frame_end(m, buf, buf_size);
116 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
124 *poutbuf_size = buf_size;
129 AVCodecParser ff_mjpeg_parser = {
130 .codec_ids = { AV_CODEC_ID_MJPEG, AV_CODEC_ID_JPEGLS },
131 .priv_data_size = sizeof(MJPEGParserContext),
132 .parser_parse = jpeg_parse,
133 .parser_close = ff_parse_close,