Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/mjpeg_parser.c |
Date: | 2022-07-04 00:18:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 48 | 59 | 81.4% |
Branches: | 30 | 46 | 65.2% |
Line | Branch | Exec | 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 | |||
31 | typedef struct MJPEGParserContext{ | ||
32 | ParseContext pc; | ||
33 | int size; | ||
34 | }MJPEGParserContext; | ||
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 | 160 | static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){ | |
41 | 160 | ParseContext *pc= &m->pc; | |
42 | int vop_found, i; | ||
43 | uint32_t state; | ||
44 | |||
45 | 160 | vop_found= pc->frame_start_found; | |
46 | 160 | state= pc->state; | |
47 | |||
48 | 160 | i=0; | |
49 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 154 times.
|
160 | if(!vop_found){ |
50 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | for(i=0; i<buf_size;){ |
51 | 24 | state= (state<<8) | buf[i]; | |
52 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
24 | if(state>=0xFFC00000 && state<=0xFFFEFFFF){ |
53 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){ |
54 | 6 | i++; | |
55 | 6 | vop_found=1; | |
56 | 6 | break; | |
57 | ✗ | }else if(state<0xFFD00000 || state>0xFFD9FFFF){ | |
58 | ✗ | m->size= (state&0xFFFF)-1; | |
59 | } | ||
60 | } | ||
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | 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 | 18 | i++; | |
69 | } | ||
70 | } | ||
71 | |||
72 |
1/2✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
|
160 | if(vop_found){ |
73 | /* EOF considered as end of frame */ | ||
74 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 148 times.
|
160 | if (buf_size == 0) |
75 | 12 | return 0; | |
76 |
2/2✓ Branch 0 taken 583100 times.
✓ Branch 1 taken 148 times.
|
583248 | for(; i<buf_size;){ |
77 | 583100 | state= (state<<8) | buf[i]; | |
78 |
3/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 583072 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
583100 | if(state>=0xFFC00000 && state<=0xFFFEFFFF){ |
79 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
28 | if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){ |
80 | ✗ | pc->frame_start_found=0; | |
81 | ✗ | pc->state=0; | |
82 | ✗ | return i-3; | |
83 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
28 | } else if((state>>16)==0xFFD9 && (state&0xFFFF)!=0xFFD8){ |
84 | ✗ | state= 0xFFD900|(state&0xFF); | |
85 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
28 | } else if(state<0xFFD00000 || state>0xFFD9FFFF){ |
86 | 28 | m->size= (state&0xFFFF)-1; | |
87 | } | ||
88 | } | ||
89 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 583072 times.
|
583100 | if(m->size>0){ |
90 | 28 | int size= FFMIN(buf_size-i, m->size); | |
91 | 28 | i+=size; | |
92 | 28 | m->size-=size; | |
93 | 28 | state=0; | |
94 | 28 | continue; | |
95 | }else | ||
96 | 583072 | i++; | |
97 | } | ||
98 | } | ||
99 | 148 | pc->frame_start_found= vop_found; | |
100 | 148 | pc->state= state; | |
101 | 148 | return END_NOT_FOUND; | |
102 | } | ||
103 | |||
104 | 1874 | static int jpeg_parse(AVCodecParserContext *s, | |
105 | AVCodecContext *avctx, | ||
106 | const uint8_t **poutbuf, int *poutbuf_size, | ||
107 | const uint8_t *buf, int buf_size) | ||
108 | { | ||
109 | 1874 | MJPEGParserContext *m = s->priv_data; | |
110 | 1874 | ParseContext *pc = &m->pc; | |
111 | int next; | ||
112 | |||
113 |
2/2✓ Branch 0 taken 1714 times.
✓ Branch 1 taken 160 times.
|
1874 | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
114 | 1714 | next= buf_size; | |
115 | }else{ | ||
116 | 160 | next= find_frame_end(m, buf, buf_size); | |
117 | |||
118 |
2/2✓ Branch 1 taken 148 times.
✓ Branch 2 taken 12 times.
|
160 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
119 | 148 | *poutbuf = NULL; | |
120 | 148 | *poutbuf_size = 0; | |
121 | 148 | return buf_size; | |
122 | } | ||
123 | } | ||
124 | |||
125 | 1726 | *poutbuf = buf; | |
126 | 1726 | *poutbuf_size = buf_size; | |
127 | 1726 | return next; | |
128 | } | ||
129 | |||
130 | |||
131 | const AVCodecParser ff_mjpeg_parser = { | ||
132 | .codec_ids = { AV_CODEC_ID_MJPEG, AV_CODEC_ID_JPEGLS }, | ||
133 | .priv_data_size = sizeof(MJPEGParserContext), | ||
134 | .parser_parse = jpeg_parse, | ||
135 | .parser_close = ff_parse_close, | ||
136 | }; | ||
137 |