FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mxpegdec.c
Date: 2026-03-13 22:30:28
Exec Total Coverage
Lines: 141 192 73.4%
Functions: 7 7 100.0%
Branches: 67 112 59.8%

Line Branch Exec Source
1 /*
2 * MxPEG decoder
3 * Copyright (c) 2011 Anatoly Nenashev
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /**
24 * @file
25 * MxPEG decoder
26 */
27
28 #include "libavutil/mem.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31 #include "hpeldsp.h"
32 #include "mjpeg.h"
33 #include "mjpegdec.h"
34
35 typedef struct MXpegDecodeContext {
36 MJpegDecodeContext jpg;
37 AVFrame *picture[2]; /* pictures array */
38 int picture_index; /* index of current picture */
39 int got_sof_data; /* true if SOF data successfully parsed */
40 int got_mxm_bitmask; /* true if MXM bitmask available */
41 uint8_t *mxm_bitmask; /* bitmask buffer */
42 unsigned bitmask_size; /* size of bitmask */
43 int has_complete_frame; /* true if has complete frame */
44 uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
45 unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
46 } MXpegDecodeContext;
47
48 2 static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
49 {
50 2 MXpegDecodeContext *s = avctx->priv_data;
51 int i;
52
53 2 ff_mjpeg_decode_end(avctx);
54
55
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 0; i < 2; ++i)
56 4 av_frame_free(&s->picture[i]);
57
58 2 s->bitmask_size = 0;
59 2 av_freep(&s->mxm_bitmask);
60 2 av_freep(&s->completion_bitmask);
61
62 2 return 0;
63 }
64
65 2 static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
66 {
67 2 MXpegDecodeContext *s = avctx->priv_data;
68 HpelDSPContext hdsp;
69
70 2 ff_hpeldsp_init(&hdsp, avctx->flags);
71 2 s->jpg.copy_block = hdsp.put_pixels_tab[1][0];
72
73 2 s->picture[0] = av_frame_alloc();
74 2 s->picture[1] = av_frame_alloc();
75
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!s->picture[0] || !s->picture[1])
76 return AVERROR(ENOMEM);
77
78 2 s->jpg.picture_ptr = s->picture[0];
79 2 return ff_mjpeg_decode_init(avctx);
80 }
81
82 31 static int mxpeg_decode_app(MXpegDecodeContext *s,
83 const uint8_t *buf_ptr, int buf_size)
84 {
85 int len;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (buf_size < 2)
87 return 0;
88 31 len = AV_RB16(buf_ptr);
89 31 bytestream2_skipu(&s->jpg.gB, FFMIN(len, buf_size));
90
91 31 return 0;
92 }
93
94 31 static int mxpeg_decode_mxm(MXpegDecodeContext *s,
95 const uint8_t *buf_ptr, int buf_size)
96 {
97 unsigned bitmask_size, mb_count;
98 int i;
99
100 31 s->mb_width = AV_RL16(buf_ptr+4);
101 31 s->mb_height = AV_RL16(buf_ptr+6);
102 31 mb_count = s->mb_width * s->mb_height;
103
104 31 bitmask_size = (mb_count + 7) >> 3;
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (bitmask_size > buf_size - 12) {
106 av_log(s->jpg.avctx, AV_LOG_ERROR,
107 "MXM bitmask is not complete\n");
108 return AVERROR(EINVAL);
109 }
110
111
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (s->bitmask_size != bitmask_size) {
112 2 s->bitmask_size = 0;
113 2 av_freep(&s->mxm_bitmask);
114 2 s->mxm_bitmask = av_malloc(bitmask_size);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->mxm_bitmask) {
116 av_log(s->jpg.avctx, AV_LOG_ERROR,
117 "MXM bitmask memory allocation error\n");
118 return AVERROR(ENOMEM);
119 }
120
121 2 av_freep(&s->completion_bitmask);
122 2 s->completion_bitmask = av_mallocz(bitmask_size);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->completion_bitmask) {
124 av_log(s->jpg.avctx, AV_LOG_ERROR,
125 "Completion bitmask memory allocation error\n");
126 return AVERROR(ENOMEM);
127 }
128
129 2 s->bitmask_size = bitmask_size;
130 }
131
132 31 memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
133 31 s->got_mxm_bitmask = 1;
134
135
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (!s->has_complete_frame) {
136 2 uint8_t completion_check = 0xFF;
137
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 2 times.
1202 for (i = 0; i < bitmask_size; ++i) {
138 1200 s->completion_bitmask[i] |= s->mxm_bitmask[i];
139 1200 completion_check &= s->completion_bitmask[i];
140 }
141 2 s->has_complete_frame = !(completion_check ^ 0xFF);
142 }
143
144 31 return 0;
145 }
146
147 64 static int mxpeg_decode_com(MXpegDecodeContext *s,
148 const uint8_t *buf_ptr, int buf_size)
149 {
150 64 int len, ret = 0;
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (buf_size < 2)
152 return 0;
153 64 len = AV_RB16(buf_ptr);
154
4/6
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✓ Branch 5 taken 33 times.
64 if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
155 31 ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
156 }
157 64 bytestream2_skipu(&s->jpg.gB, FFMIN(len, buf_size));
158
159 64 return ret;
160 }
161
162 31 static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
163 AVFrame *reference_ptr)
164 {
165
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if ((jpg->width + 0x0F)>>4 != s->mb_width ||
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (jpg->height + 0x0F)>>4 != s->mb_height) {
167 av_log(jpg->avctx, AV_LOG_ERROR,
168 "Picture dimensions stored in SOF and MXM mismatch\n");
169 return AVERROR(EINVAL);
170 }
171
172
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
31 if (reference_ptr->data[0]) {
173 int i;
174
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 29 times.
145 for (i = 0; i < MAX_COMPONENTS; ++i) {
175
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
177 av_log(jpg->avctx, AV_LOG_ERROR,
178 "Dimensions of current and reference picture mismatch\n");
179 return AVERROR(EINVAL);
180 }
181 }
182
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (reference_ptr->width != jpg->picture_ptr->width ||
183
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 reference_ptr->height != jpg->picture_ptr->height ||
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 reference_ptr->format != jpg->picture_ptr->format) {
185 av_log(jpg->avctx, AV_LOG_ERROR, "Reference mismatching\n");
186 return AVERROR_INVALIDDATA;
187 }
188 }
189
190 31 return 0;
191 }
192
193 31 static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
194 int *got_frame, AVPacket *avpkt)
195 {
196 31 const uint8_t *buf = avpkt->data;
197 31 int buf_size = avpkt->size;
198 31 MXpegDecodeContext *s = avctx->priv_data;
199 31 MJpegDecodeContext *jpg = &s->jpg;
200 const uint8_t *buf_end, *buf_ptr;
201 int start_code;
202 int ret;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (avctx->skip_frame == AVDISCARD_ALL)
205 return AVERROR_PATCHWELCOME;
206
207 31 buf_ptr = buf;
208 31 buf_end = buf + buf_size;
209 31 jpg->got_picture = 0;
210 31 s->got_mxm_bitmask = 0;
211 31 s->got_sof_data = !!s->got_sof_data;
212
1/2
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
202 while (buf_ptr < buf_end) {
213 202 start_code = ff_mjpeg_find_marker(&buf_ptr, buf_end);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (start_code < 0)
215 goto the_end;
216
217 202 int bytes_left = buf_end - buf_ptr;
218 202 bytestream2_init(&jpg->gB, buf_ptr, bytes_left);
219
220
4/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 107 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 64 times.
202 if (start_code >= APP0 && start_code <= APP15) {
221 31 mxpeg_decode_app(s, buf_ptr, bytes_left);
222 }
223
224
8/8
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 64 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 31 times.
✓ Branch 7 taken 31 times.
202 switch (start_code) {
225 31 case SOI:
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (jpg->got_picture) //emulating EOI
227 goto the_end;
228 31 break;
229 31 case EOI:
230 31 goto the_end;
231 4 case DQT:
232 4 ret = ff_mjpeg_decode_dqt(jpg);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
234 av_log(avctx, AV_LOG_ERROR,
235 "quantization table decode error\n");
236 return ret;
237 }
238 4 break;
239 8 case DHT:
240 8 ret = ff_mjpeg_decode_dht(jpg);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0) {
242 av_log(avctx, AV_LOG_ERROR,
243 "huffman table decode error\n");
244 return ret;
245 }
246 8 break;
247 64 case COM:
248 64 ret = mxpeg_decode_com(s, buf_ptr, bytes_left);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (ret < 0)
250 return ret;
251 64 break;
252 2 case SOF0:
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->got_sof_data > 1) {
254 av_log(avctx, AV_LOG_ERROR,
255 "Multiple SOF in a frame\n");
256 return AVERROR_INVALIDDATA;
257 }
258 2 ret = ff_mjpeg_decode_sof(jpg);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
260 av_log(avctx, AV_LOG_ERROR,
261 "SOF data decode error\n");
262 s->got_sof_data = 0;
263 return ret;
264 }
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (jpg->interlaced) {
266 av_log(avctx, AV_LOG_ERROR,
267 "Interlaced mode not supported in MxPEG\n");
268 s->got_sof_data = 0;
269 return AVERROR(EINVAL);
270 }
271 2 s->got_sof_data ++;
272 2 break;
273 31 case SOS:
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!s->got_sof_data) {
275 av_log(avctx, AV_LOG_WARNING,
276 "Can not process SOS without SOF data, skipping\n");
277 break;
278 }
279
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
31 if (!jpg->got_picture) {
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (jpg->first_picture) {
281 av_log(avctx, AV_LOG_WARNING,
282 "First picture has no SOF, skipping\n");
283 break;
284 }
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!s->got_mxm_bitmask){
286 av_log(avctx, AV_LOG_WARNING,
287 "Non-key frame has no MXM, skipping\n");
288 break;
289 }
290 /* use stored SOF data to allocate current picture */
291 29 av_frame_unref(jpg->picture_ptr);
292
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
293 AV_GET_BUFFER_FLAG_REF)) < 0)
294 return ret;
295 29 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
296 29 jpg->picture_ptr->flags &= ~AV_FRAME_FLAG_KEY;
297 29 jpg->got_picture = 1;
298 } else {
299 2 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
300 2 jpg->picture_ptr->flags |= AV_FRAME_FLAG_KEY;
301 }
302
303
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (s->got_mxm_bitmask) {
304 31 AVFrame *reference_ptr = s->picture[s->picture_index ^ 1];
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
306 break;
307
308 /* allocate dummy reference picture if needed */
309
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
33 if (!reference_ptr->data[0] &&
310 2 (ret = ff_get_buffer(avctx, reference_ptr,
311 AV_GET_BUFFER_FLAG_REF)) < 0)
312 return ret;
313
314 31 jpg->mb_bitmask = s->mxm_bitmask;
315 31 jpg->mb_bitmask_size = s->bitmask_size;
316 31 jpg->reference = reference_ptr;
317 31 ret = ff_mjpeg_decode_sos(jpg);
318
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
319 return ret;
320 } else {
321 jpg->mb_bitmask = NULL;
322 jpg->reference = NULL;
323 ret = ff_mjpeg_decode_sos(jpg);
324 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
325 return ret;
326 }
327
328 31 break;
329 }
330
331 171 buf_ptr += bytestream2_tell(&jpg->gB);
332 }
333
334 the_end:
335
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (jpg->got_picture) {
336 31 int ret = av_frame_ref(rframe, jpg->picture_ptr);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
338 return ret;
339 31 *got_frame = 1;
340
341 31 s->picture_index ^= 1;
342 31 jpg->picture_ptr = s->picture[s->picture_index];
343
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!s->has_complete_frame) {
345 if (!s->got_mxm_bitmask)
346 s->has_complete_frame = 1;
347 else
348 *got_frame = 0;
349 }
350 }
351
352 31 return buf_ptr - buf;
353 }
354
355 const FFCodec ff_mxpeg_decoder = {
356 .p.name = "mxpeg",
357 CODEC_LONG_NAME("Mobotix MxPEG video"),
358 .p.type = AVMEDIA_TYPE_VIDEO,
359 .p.id = AV_CODEC_ID_MXPEG,
360 .priv_data_size = sizeof(MXpegDecodeContext),
361 .init = mxpeg_decode_init,
362 .close = mxpeg_decode_end,
363 FF_CODEC_DECODE_CB(mxpeg_decode_frame),
364 .p.capabilities = AV_CODEC_CAP_DR1,
365 .p.max_lowres = 3,
366 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
367 };
368