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 "mjpeg.h" | ||
32 | #include "mjpegdec.h" | ||
33 | |||
34 | typedef struct MXpegDecodeContext { | ||
35 | MJpegDecodeContext jpg; | ||
36 | AVFrame *picture[2]; /* pictures array */ | ||
37 | int picture_index; /* index of current picture */ | ||
38 | int got_sof_data; /* true if SOF data successfully parsed */ | ||
39 | int got_mxm_bitmask; /* true if MXM bitmask available */ | ||
40 | uint8_t *mxm_bitmask; /* bitmask buffer */ | ||
41 | unsigned bitmask_size; /* size of bitmask */ | ||
42 | int has_complete_frame; /* true if has complete frame */ | ||
43 | uint8_t *completion_bitmask; /* completion bitmask of macroblocks */ | ||
44 | unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */ | ||
45 | } MXpegDecodeContext; | ||
46 | |||
47 | 2 | static av_cold int mxpeg_decode_end(AVCodecContext *avctx) | |
48 | { | ||
49 | 2 | MXpegDecodeContext *s = avctx->priv_data; | |
50 | int i; | ||
51 | |||
52 | 2 | ff_mjpeg_decode_end(avctx); | |
53 | |||
54 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (i = 0; i < 2; ++i) |
55 | 4 | av_frame_free(&s->picture[i]); | |
56 | |||
57 | 2 | s->bitmask_size = 0; | |
58 | 2 | av_freep(&s->mxm_bitmask); | |
59 | 2 | av_freep(&s->completion_bitmask); | |
60 | |||
61 | 2 | return 0; | |
62 | } | ||
63 | |||
64 | 2 | static av_cold int mxpeg_decode_init(AVCodecContext *avctx) | |
65 | { | ||
66 | 2 | MXpegDecodeContext *s = avctx->priv_data; | |
67 | |||
68 | 2 | s->picture[0] = av_frame_alloc(); | |
69 | 2 | s->picture[1] = av_frame_alloc(); | |
70 |
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]) |
71 | ✗ | return AVERROR(ENOMEM); | |
72 | |||
73 | 2 | s->jpg.picture_ptr = s->picture[0]; | |
74 | 2 | return ff_mjpeg_decode_init(avctx); | |
75 | } | ||
76 | |||
77 | 31 | static int mxpeg_decode_app(MXpegDecodeContext *s, | |
78 | const uint8_t *buf_ptr, int buf_size) | ||
79 | { | ||
80 | int len; | ||
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (buf_size < 2) |
82 | ✗ | return 0; | |
83 | 31 | len = AV_RB16(buf_ptr); | |
84 | 31 | skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size)); | |
85 | |||
86 | 31 | return 0; | |
87 | } | ||
88 | |||
89 | 31 | static int mxpeg_decode_mxm(MXpegDecodeContext *s, | |
90 | const uint8_t *buf_ptr, int buf_size) | ||
91 | { | ||
92 | unsigned bitmask_size, mb_count; | ||
93 | int i; | ||
94 | |||
95 | 31 | s->mb_width = AV_RL16(buf_ptr+4); | |
96 | 31 | s->mb_height = AV_RL16(buf_ptr+6); | |
97 | 31 | mb_count = s->mb_width * s->mb_height; | |
98 | |||
99 | 31 | bitmask_size = (mb_count + 7) >> 3; | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (bitmask_size > buf_size - 12) { |
101 | ✗ | av_log(s->jpg.avctx, AV_LOG_ERROR, | |
102 | "MXM bitmask is not complete\n"); | ||
103 | ✗ | return AVERROR(EINVAL); | |
104 | } | ||
105 | |||
106 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
|
31 | if (s->bitmask_size != bitmask_size) { |
107 | 2 | s->bitmask_size = 0; | |
108 | 2 | av_freep(&s->mxm_bitmask); | |
109 | 2 | s->mxm_bitmask = av_malloc(bitmask_size); | |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->mxm_bitmask) { |
111 | ✗ | av_log(s->jpg.avctx, AV_LOG_ERROR, | |
112 | "MXM bitmask memory allocation error\n"); | ||
113 | ✗ | return AVERROR(ENOMEM); | |
114 | } | ||
115 | |||
116 | 2 | av_freep(&s->completion_bitmask); | |
117 | 2 | s->completion_bitmask = av_mallocz(bitmask_size); | |
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->completion_bitmask) { |
119 | ✗ | av_log(s->jpg.avctx, AV_LOG_ERROR, | |
120 | "Completion bitmask memory allocation error\n"); | ||
121 | ✗ | return AVERROR(ENOMEM); | |
122 | } | ||
123 | |||
124 | 2 | s->bitmask_size = bitmask_size; | |
125 | } | ||
126 | |||
127 | 31 | memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size); | |
128 | 31 | s->got_mxm_bitmask = 1; | |
129 | |||
130 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
|
31 | if (!s->has_complete_frame) { |
131 | 2 | uint8_t completion_check = 0xFF; | |
132 |
2/2✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 2 times.
|
1202 | for (i = 0; i < bitmask_size; ++i) { |
133 | 1200 | s->completion_bitmask[i] |= s->mxm_bitmask[i]; | |
134 | 1200 | completion_check &= s->completion_bitmask[i]; | |
135 | } | ||
136 | 2 | s->has_complete_frame = !(completion_check ^ 0xFF); | |
137 | } | ||
138 | |||
139 | 31 | return 0; | |
140 | } | ||
141 | |||
142 | 64 | static int mxpeg_decode_com(MXpegDecodeContext *s, | |
143 | const uint8_t *buf_ptr, int buf_size) | ||
144 | { | ||
145 | 64 | int len, ret = 0; | |
146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (buf_size < 2) |
147 | ✗ | return 0; | |
148 | 64 | len = AV_RB16(buf_ptr); | |
149 |
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)) { |
150 | 31 | ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2); | |
151 | } | ||
152 | 64 | skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size)); | |
153 | |||
154 | 64 | return ret; | |
155 | } | ||
156 | |||
157 | 31 | static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, | |
158 | AVFrame *reference_ptr) | ||
159 | { | ||
160 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if ((jpg->width + 0x0F)>>4 != s->mb_width || |
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | (jpg->height + 0x0F)>>4 != s->mb_height) { |
162 | ✗ | av_log(jpg->avctx, AV_LOG_ERROR, | |
163 | "Picture dimensions stored in SOF and MXM mismatch\n"); | ||
164 | ✗ | return AVERROR(EINVAL); | |
165 | } | ||
166 | |||
167 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
|
31 | if (reference_ptr->data[0]) { |
168 | int i; | ||
169 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 29 times.
|
145 | for (i = 0; i < MAX_COMPONENTS; ++i) { |
170 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) || |
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
|
116 | reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) { |
172 | ✗ | av_log(jpg->avctx, AV_LOG_ERROR, | |
173 | "Dimensions of current and reference picture mismatch\n"); | ||
174 | ✗ | return AVERROR(EINVAL); | |
175 | } | ||
176 | } | ||
177 | } | ||
178 | |||
179 | 31 | return 0; | |
180 | } | ||
181 | |||
182 | 31 | static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
183 | int *got_frame, AVPacket *avpkt) | ||
184 | { | ||
185 | 31 | const uint8_t *buf = avpkt->data; | |
186 | 31 | int buf_size = avpkt->size; | |
187 | 31 | MXpegDecodeContext *s = avctx->priv_data; | |
188 | 31 | MJpegDecodeContext *jpg = &s->jpg; | |
189 | const uint8_t *buf_end, *buf_ptr; | ||
190 | const uint8_t *unescaped_buf_ptr; | ||
191 | int unescaped_buf_size; | ||
192 | int start_code; | ||
193 | int ret; | ||
194 | |||
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (avctx->skip_frame == AVDISCARD_ALL) |
196 | ✗ | return AVERROR_PATCHWELCOME; | |
197 | |||
198 | 31 | buf_ptr = buf; | |
199 | 31 | buf_end = buf + buf_size; | |
200 | 31 | jpg->got_picture = 0; | |
201 | 31 | s->got_mxm_bitmask = 0; | |
202 | 31 | s->got_sof_data = !!s->got_sof_data; | |
203 |
1/2✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
|
202 | while (buf_ptr < buf_end) { |
204 | 202 | start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end, | |
205 | &unescaped_buf_ptr, &unescaped_buf_size); | ||
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
|
202 | if (start_code < 0) |
207 | ✗ | goto the_end; | |
208 | { | ||
209 | 202 | init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8); | |
210 | |||
211 |
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) { |
212 | 31 | mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size); | |
213 | } | ||
214 | |||
215 |
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) { |
216 | 31 | case SOI: | |
217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (jpg->got_picture) //emulating EOI |
218 | ✗ | goto the_end; | |
219 | 31 | break; | |
220 | 31 | case EOI: | |
221 | 31 | goto the_end; | |
222 | 4 | case DQT: | |
223 | 4 | ret = ff_mjpeg_decode_dqt(jpg); | |
224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) { |
225 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
226 | "quantization table decode error\n"); | ||
227 | ✗ | return ret; | |
228 | } | ||
229 | 4 | break; | |
230 | 8 | case DHT: | |
231 | 8 | ret = ff_mjpeg_decode_dht(jpg); | |
232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) { |
233 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
234 | "huffman table decode error\n"); | ||
235 | ✗ | return ret; | |
236 | } | ||
237 | 8 | break; | |
238 | 64 | case COM: | |
239 | 64 | ret = mxpeg_decode_com(s, unescaped_buf_ptr, | |
240 | unescaped_buf_size); | ||
241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (ret < 0) |
242 | ✗ | return ret; | |
243 | 64 | break; | |
244 | 2 | case SOF0: | |
245 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->got_sof_data > 1) { |
246 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
247 | "Multiple SOF in a frame\n"); | ||
248 | ✗ | return AVERROR_INVALIDDATA; | |
249 | } | ||
250 | 2 | ret = ff_mjpeg_decode_sof(jpg); | |
251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
252 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
253 | "SOF data decode error\n"); | ||
254 | ✗ | s->got_sof_data = 0; | |
255 | ✗ | return ret; | |
256 | } | ||
257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (jpg->interlaced) { |
258 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
259 | "Interlaced mode not supported in MxPEG\n"); | ||
260 | ✗ | s->got_sof_data = 0; | |
261 | ✗ | return AVERROR(EINVAL); | |
262 | } | ||
263 | 2 | s->got_sof_data ++; | |
264 | 2 | break; | |
265 | 31 | case SOS: | |
266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!s->got_sof_data) { |
267 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
268 | "Can not process SOS without SOF data, skipping\n"); | ||
269 | ✗ | break; | |
270 | } | ||
271 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
|
31 | if (!jpg->got_picture) { |
272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (jpg->first_picture) { |
273 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
274 | "First picture has no SOF, skipping\n"); | ||
275 | ✗ | break; | |
276 | } | ||
277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (!s->got_mxm_bitmask){ |
278 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
279 | "Non-key frame has no MXM, skipping\n"); | ||
280 | ✗ | break; | |
281 | } | ||
282 | /* use stored SOF data to allocate current picture */ | ||
283 | 29 | av_frame_unref(jpg->picture_ptr); | |
284 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
|
29 | if ((ret = ff_get_buffer(avctx, jpg->picture_ptr, |
285 | AV_GET_BUFFER_FLAG_REF)) < 0) | ||
286 | ✗ | return ret; | |
287 | 29 | jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P; | |
288 | 29 | jpg->picture_ptr->flags &= ~AV_FRAME_FLAG_KEY; | |
289 | 29 | jpg->got_picture = 1; | |
290 | } else { | ||
291 | 2 | jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I; | |
292 | 2 | jpg->picture_ptr->flags |= AV_FRAME_FLAG_KEY; | |
293 | } | ||
294 | |||
295 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (s->got_mxm_bitmask) { |
296 | 31 | AVFrame *reference_ptr = s->picture[s->picture_index ^ 1]; | |
297 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
|
31 | if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0) |
298 | ✗ | break; | |
299 | |||
300 | /* allocate dummy reference picture if needed */ | ||
301 |
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] && |
302 | 2 | (ret = ff_get_buffer(avctx, reference_ptr, | |
303 | AV_GET_BUFFER_FLAG_REF)) < 0) | ||
304 | ✗ | return ret; | |
305 | |||
306 | 31 | ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, s->bitmask_size, reference_ptr); | |
307 |
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)) |
308 | ✗ | return ret; | |
309 | } else { | ||
310 | ✗ | ret = ff_mjpeg_decode_sos(jpg, NULL, 0, NULL); | |
311 | ✗ | if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) | |
312 | ✗ | return ret; | |
313 | } | ||
314 | |||
315 | 31 | break; | |
316 | } | ||
317 | |||
318 | 171 | buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3; | |
319 | } | ||
320 | |||
321 | } | ||
322 | |||
323 | ✗ | the_end: | |
324 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (jpg->got_picture) { |
325 | 31 | int ret = av_frame_ref(rframe, jpg->picture_ptr); | |
326 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (ret < 0) |
327 | ✗ | return ret; | |
328 | 31 | *got_frame = 1; | |
329 | |||
330 | 31 | s->picture_index ^= 1; | |
331 | 31 | jpg->picture_ptr = s->picture[s->picture_index]; | |
332 | |||
333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (!s->has_complete_frame) { |
334 | ✗ | if (!s->got_mxm_bitmask) | |
335 | ✗ | s->has_complete_frame = 1; | |
336 | else | ||
337 | ✗ | *got_frame = 0; | |
338 | } | ||
339 | } | ||
340 | |||
341 | 31 | return buf_ptr - buf; | |
342 | } | ||
343 | |||
344 | const FFCodec ff_mxpeg_decoder = { | ||
345 | .p.name = "mxpeg", | ||
346 | CODEC_LONG_NAME("Mobotix MxPEG video"), | ||
347 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
348 | .p.id = AV_CODEC_ID_MXPEG, | ||
349 | .priv_data_size = sizeof(MXpegDecodeContext), | ||
350 | .init = mxpeg_decode_init, | ||
351 | .close = mxpeg_decode_end, | ||
352 | FF_CODEC_DECODE_CB(mxpeg_decode_frame), | ||
353 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
354 | .p.max_lowres = 3, | ||
355 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
356 | }; | ||
357 |