FFmpeg
Loading...
Searching...
No Matches
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
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 */
41static 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){
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
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:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_JPEGLS
Definition codec_id.h:61
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
#define FFMIN(a, b)
Definition macros.h:49
const FFCodecParser ff_mjpeg_parser
static int jpeg_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
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:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
ParseContext pc
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
int size