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 #include "parser_internal.h"
31 
32 typedef struct MJPEGParserContext{
34  int size;
36 
37 /**
38  * Find the end of the current frame in the bitstream.
39  * @return the position of the first byte of the next frame, or -1
40  */
41 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
42  ParseContext *pc= &m->pc;
43  int vop_found, i;
44  uint32_t state;
45 
46  vop_found= pc->frame_start_found;
47  state= pc->state;
48 
49  i=0;
50  if(!vop_found){
51  for(i=0; i<buf_size;){
52  state= (state<<8) | buf[i];
53  if(state>=0xFFC00000 && state<=0xFFFEFFFF){
54  if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
55  i++;
56  vop_found=1;
57  break;
58  }else if(state<0xFFD00000 || state>0xFFD9FFFF){
59  m->size= (state&0xFFFF)-1;
60  }
61  }
62  if(m->size>0){
63  int size= FFMIN(buf_size-i, m->size);
64  i+=size;
65  m->size-=size;
66  state=0;
67  continue;
68  }else
69  i++;
70  }
71  }
72 
73  if(vop_found){
74  /* EOF considered as end of frame */
75  if (buf_size == 0)
76  return 0;
77  for(; i<buf_size;){
78  state= (state<<8) | buf[i];
79  if(state>=0xFFC00000 && state<=0xFFFEFFFF){
80  if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
81  pc->frame_start_found=0;
82  pc->state=0;
83  return i-3;
84  } else if((state>>16)==0xFFD9 && (state&0xFFFF)!=0xFFD8){
85  state= 0xFFD900|(state&0xFF);
86  } else if(state<0xFFD00000 || state>0xFFD9FFFF){
87  m->size= (state&0xFFFF)-1;
88  }
89  }
90  if(m->size>0){
91  int size= FFMIN(buf_size-i, m->size);
92  i+=size;
93  m->size-=size;
94  state=0;
95  continue;
96  }else
97  i++;
98  }
99  }
100  pc->frame_start_found= vop_found;
101  pc->state= state;
102  return END_NOT_FOUND;
103 }
104 
106  AVCodecContext *avctx,
107  const uint8_t **poutbuf, int *poutbuf_size,
108  const uint8_t *buf, int buf_size)
109 {
110  MJPEGParserContext *m = s->priv_data;
111  ParseContext *pc = &m->pc;
112  int next;
113 
114  if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
115  next= buf_size;
116  }else{
117  next= find_frame_end(m, buf, buf_size);
118 
119  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
120  *poutbuf = NULL;
121  *poutbuf_size = 0;
122  return buf_size;
123  }
124  }
125 
126  *poutbuf = buf;
127  *poutbuf_size = buf_size;
128  return next;
129 }
130 
131 
134  .priv_data_size = sizeof(MJPEGParserContext),
135  .parse = jpeg_parse,
137 };
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
parser_internal.h
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:33
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
ParseContext
Definition: parser.h:28
MJPEGParserContext::size
int size
Definition: mjpeg_parser.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:198
NULL
#define NULL
Definition: coverity.c:32
state
static struct @541 state
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: apv_parser.c:46
MJPEGParserContext
Definition: mjpeg_parser.c:32
size
int size
Definition: twinvq_data.h:10344
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:211
FFCodecParser
Definition: parser_internal.h:29
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2609
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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:41
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
AVCodecParserContext
Definition: avcodec.h:2575
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AV_CODEC_ID_JPEGLS
@ AV_CODEC_ID_JPEGLS
Definition: codec_id.h:63
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:105
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
ff_mjpeg_parser
const FFCodecParser ff_mjpeg_parser
Definition: mjpeg_parser.c:132