FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpeg_parser.c
Date: 2025-11-02 07:30:46
Exec Total Coverage
Lines: 48 59 81.4%
Functions: 2 2 100.0%
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 #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 181 static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
42 181 ParseContext *pc= &m->pc;
43 int vop_found, i;
44 uint32_t state;
45
46 181 vop_found= pc->frame_start_found;
47 181 state= pc->state;
48
49 181 i=0;
50
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 171 times.
181 if(!vop_found){
51
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 for(i=0; i<buf_size;){
52 40 state= (state<<8) | buf[i];
53
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
40 if(state>=0xFFC00000 && state<=0xFFFEFFFF){
54
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
55 10 i++;
56 10 vop_found=1;
57 10 break;
58 }else if(state<0xFFD00000 || state>0xFFD9FFFF){
59 m->size= (state&0xFFFF)-1;
60 }
61 }
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 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 30 i++;
70 }
71 }
72
73
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 if(vop_found){
74 /* EOF considered as end of frame */
75
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 161 times.
181 if (buf_size == 0)
76 20 return 0;
77
2/2
✓ Branch 0 taken 622753 times.
✓ Branch 1 taken 161 times.
622914 for(; i<buf_size;){
78 622753 state= (state<<8) | buf[i];
79
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 622693 times.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
622753 if(state>=0xFFC00000 && state<=0xFFFEFFFF){
80
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
60 if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
81 pc->frame_start_found=0;
82 pc->state=0;
83 return i-3;
84
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60 } else if((state>>16)==0xFFD9 && (state&0xFFFF)!=0xFFD8){
85 state= 0xFFD900|(state&0xFF);
86
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
60 } else if(state<0xFFD00000 || state>0xFFD9FFFF){
87 60 m->size= (state&0xFFFF)-1;
88 }
89 }
90
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 622693 times.
622753 if(m->size>0){
91 60 int size= FFMIN(buf_size-i, m->size);
92 60 i+=size;
93 60 m->size-=size;
94 60 state=0;
95 60 continue;
96 }else
97 622693 i++;
98 }
99 }
100 161 pc->frame_start_found= vop_found;
101 161 pc->state= state;
102 161 return END_NOT_FOUND;
103 }
104
105 1895 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 {
110 1895 MJPEGParserContext *m = s->priv_data;
111 1895 ParseContext *pc = &m->pc;
112 int next;
113
114
2/2
✓ Branch 0 taken 1714 times.
✓ Branch 1 taken 181 times.
1895 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
115 1714 next= buf_size;
116 }else{
117 181 next= find_frame_end(m, buf, buf_size);
118
119
2/2
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 20 times.
181 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
120 161 *poutbuf = NULL;
121 161 *poutbuf_size = 0;
122 161 return buf_size;
123 }
124 }
125
126 1734 *poutbuf = buf;
127 1734 *poutbuf_size = buf_size;
128 1734 return next;
129 }
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 };
138