FFmpeg
mjpeg_parser.c
Go to the documentation of this file.
1 /*
2  * MJPEG parser
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  * Copyright (c) 2003-2004 Michael Niedermayer
6  *
7  * This file is part of FFmpeg.
8  *
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.
13  *
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.
18  *
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
22  */
23 
24 /**
25  * @file
26  * MJPEG parser.
27  */
28 
29 #include "parser.h"
30 
31 typedef struct MJPEGParserContext{
33  int size;
35 
36 /**
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
39  */
40 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
41  ParseContext *pc= &m->pc;
42  int vop_found, i;
43  uint32_t state;
44 
45  vop_found= pc->frame_start_found;
46  state= pc->state;
47 
48  i=0;
49  if(!vop_found){
50  for(i=0; i<buf_size;){
51  state= (state<<8) | buf[i];
52  if(state>=0xFFC00000 && state<=0xFFFEFFFF){
53  if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
54  i++;
55  vop_found=1;
56  break;
57  }else if(state<0xFFD00000 || state>0xFFD9FFFF){
58  m->size= (state&0xFFFF)-1;
59  }
60  }
61  if(m->size>0){
62  int size= FFMIN(buf_size-i, m->size);
63  i+=size;
64  m->size-=size;
65  state=0;
66  continue;
67  }else
68  i++;
69  }
70  }
71 
72  if(vop_found){
73  /* EOF considered as end of frame */
74  if (buf_size == 0)
75  return 0;
76  for(; i<buf_size;){
77  state= (state<<8) | buf[i];
78  if(state>=0xFFC00000 && state<=0xFFFEFFFF){
79  if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
80  pc->frame_start_found=0;
81  pc->state=0;
82  return i-3;
83  } else if((state>>16)==0xFFD9 && (state&0xFFFF)!=0xFFD8){
84  state= 0xFFD900|(state&0xFF);
85  } else if(state<0xFFD00000 || state>0xFFD9FFFF){
86  m->size= (state&0xFFFF)-1;
87  }
88  }
89  if(m->size>0){
90  int size= FFMIN(buf_size-i, m->size);
91  i+=size;
92  m->size-=size;
93  state=0;
94  continue;
95  }else
96  i++;
97  }
98  }
99  pc->frame_start_found= vop_found;
100  pc->state= state;
101  return END_NOT_FOUND;
102 }
103 
105  AVCodecContext *avctx,
106  const uint8_t **poutbuf, int *poutbuf_size,
107  const uint8_t *buf, int buf_size)
108 {
109  MJPEGParserContext *m = s->priv_data;
110  ParseContext *pc = &m->pc;
111  int next;
112 
113  if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
114  next= buf_size;
115  }else{
116  next= find_frame_end(m, buf, buf_size);
117 
118  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
119  *poutbuf = NULL;
120  *poutbuf_size = 0;
121  return buf_size;
122  }
123  }
124 
125  *poutbuf = buf;
126  *poutbuf_size = buf_size;
127  return next;
128 }
129 
130 
133  .priv_data_size = sizeof(MJPEGParserContext),
134  .parser_parse = jpeg_parse,
135  .parser_close = ff_parse_close,
136 };
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:284
ff_mjpeg_parser
const AVCodecParser ff_mjpeg_parser
Definition: mjpeg_parser.c:131
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
MJPEGParserContext::pc
ParseContext pc
Definition: mjpeg_parser.c:32
ParseContext
Definition: parser.h:28
MJPEGParserContext::size
int size
Definition: mjpeg_parser.c:33
s
#define s(width, name)
Definition: cbs_vp9.c:256
NULL
#define NULL
Definition: coverity.c:32
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
MJPEGParserContext
Definition: mjpeg_parser.c:31
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2949
size
int size
Definition: twinvq_data.h:10344
state
static struct @327 state
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:199
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:57
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2823
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
find_frame_end
static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: mjpeg_parser.c:40
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
AVCodecParserContext
Definition: avcodec.h:2789
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AV_CODEC_ID_JPEGLS
@ AV_CODEC_ID_JPEGLS
Definition: codec_id.h:61
jpeg_parse
static int jpeg_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: mjpeg_parser.c:104
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2948