/src/ffmpeg/libavcodec/mjpeg_parser.c
Line | Count | Source |
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{ |
33 | | ParseContext pc; |
34 | | int size; |
35 | | }MJPEGParserContext; |
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 | 1.68M | static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){ |
42 | 1.68M | ParseContext *pc= &m->pc; |
43 | 1.68M | int vop_found, i; |
44 | 1.68M | uint32_t state; |
45 | | |
46 | 1.68M | vop_found= pc->frame_start_found; |
47 | 1.68M | state= pc->state; |
48 | | |
49 | 1.68M | i=0; |
50 | 1.68M | if(!vop_found){ |
51 | 108M | for(i=0; i<buf_size;){ |
52 | 107M | state= (state<<8) | buf[i]; |
53 | 107M | if(state>=0xFFC00000 && state<=0xFFFEFFFF){ |
54 | 797k | if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){ |
55 | 673k | i++; |
56 | 673k | vop_found=1; |
57 | 673k | break; |
58 | 673k | }else if(state<0xFFD00000 || state>0xFFD9FFFF){ |
59 | 117k | m->size= (state&0xFFFF)-1; |
60 | 117k | } |
61 | 797k | } |
62 | 106M | if(m->size>0){ |
63 | 741k | int size= FFMIN(buf_size-i, m->size); |
64 | 741k | i+=size; |
65 | 741k | m->size-=size; |
66 | 741k | state=0; |
67 | 741k | continue; |
68 | 741k | }else |
69 | 105M | i++; |
70 | 106M | } |
71 | 1.58M | } |
72 | | |
73 | 1.68M | if(vop_found){ |
74 | | /* EOF considered as end of frame */ |
75 | 773k | if (buf_size == 0) |
76 | 4.16k | return 0; |
77 | 85.7M | for(; i<buf_size;){ |
78 | 85.6M | state= (state<<8) | buf[i]; |
79 | 85.6M | if(state>=0xFFC00000 && state<=0xFFFEFFFF){ |
80 | 7.24M | if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){ |
81 | 671k | pc->frame_start_found=0; |
82 | 671k | pc->state=0; |
83 | 671k | return i-3; |
84 | 6.57M | } else if((state>>16)==0xFFD9 && (state&0xFFFF)!=0xFFD8){ |
85 | 5.66M | state= 0xFFD900|(state&0xFF); |
86 | 5.66M | } else if(state<0xFFD00000 || state>0xFFD9FFFF){ |
87 | 489k | m->size= (state&0xFFFF)-1; |
88 | 489k | } |
89 | 7.24M | } |
90 | 84.9M | if(m->size>0){ |
91 | 181k | int size= FFMIN(buf_size-i, m->size); |
92 | 181k | i+=size; |
93 | 181k | m->size-=size; |
94 | 181k | state=0; |
95 | 181k | continue; |
96 | 181k | }else |
97 | 84.8M | i++; |
98 | 84.9M | } |
99 | 769k | } |
100 | 1.00M | pc->frame_start_found= vop_found; |
101 | 1.00M | pc->state= state; |
102 | 1.00M | return END_NOT_FOUND; |
103 | 1.68M | } |
104 | | |
105 | | static int jpeg_parse(AVCodecParserContext *s, |
106 | | AVCodecContext *avctx, |
107 | | const uint8_t **poutbuf, int *poutbuf_size, |
108 | | const uint8_t *buf, int buf_size) |
109 | 1.68M | { |
110 | 1.68M | MJPEGParserContext *m = s->priv_data; |
111 | 1.68M | ParseContext *pc = &m->pc; |
112 | 1.68M | int next; |
113 | | |
114 | 1.68M | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
115 | 1.07k | next= buf_size; |
116 | 1.68M | }else{ |
117 | 1.68M | next= find_frame_end(m, buf, buf_size); |
118 | | |
119 | 1.68M | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
120 | 1.00M | *poutbuf = NULL; |
121 | 1.00M | *poutbuf_size = 0; |
122 | 1.00M | return buf_size; |
123 | 1.00M | } |
124 | 1.68M | } |
125 | | |
126 | 682k | *poutbuf = buf; |
127 | 682k | *poutbuf_size = buf_size; |
128 | 682k | return next; |
129 | 1.68M | } |
130 | | |
131 | | |
132 | | const FFCodecParser ff_mjpeg_parser = { |
133 | | PARSER_CODEC_LIST(AV_CODEC_ID_MJPEG, AV_CODEC_ID_JPEGLS), |
134 | | .priv_data_size = sizeof(MJPEGParserContext), |
135 | | .parse = jpeg_parse, |
136 | | .close = ff_parse_close, |
137 | | }; |