/src/ffmpeg/libavcodec/mxpegdec.c
Line | Count | Source (jump to first uncovered line) |
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 | | static av_cold int mxpeg_decode_end(AVCodecContext *avctx) |
48 | 5.49k | { |
49 | 5.49k | MXpegDecodeContext *s = avctx->priv_data; |
50 | 5.49k | int i; |
51 | | |
52 | 5.49k | ff_mjpeg_decode_end(avctx); |
53 | | |
54 | 16.4k | for (i = 0; i < 2; ++i) |
55 | 10.9k | av_frame_free(&s->picture[i]); |
56 | | |
57 | 5.49k | s->bitmask_size = 0; |
58 | 5.49k | av_freep(&s->mxm_bitmask); |
59 | 5.49k | av_freep(&s->completion_bitmask); |
60 | | |
61 | 5.49k | return 0; |
62 | 5.49k | } |
63 | | |
64 | | static av_cold int mxpeg_decode_init(AVCodecContext *avctx) |
65 | 5.49k | { |
66 | 5.49k | MXpegDecodeContext *s = avctx->priv_data; |
67 | | |
68 | 5.49k | s->picture[0] = av_frame_alloc(); |
69 | 5.49k | s->picture[1] = av_frame_alloc(); |
70 | 5.49k | if (!s->picture[0] || !s->picture[1]) |
71 | 0 | return AVERROR(ENOMEM); |
72 | | |
73 | 5.49k | s->jpg.picture_ptr = s->picture[0]; |
74 | 5.49k | return ff_mjpeg_decode_init(avctx); |
75 | 5.49k | } |
76 | | |
77 | | static int mxpeg_decode_app(MXpegDecodeContext *s, |
78 | | const uint8_t *buf_ptr, int buf_size) |
79 | 7.17k | { |
80 | 7.17k | int len; |
81 | 7.17k | if (buf_size < 2) |
82 | 242 | return 0; |
83 | 6.93k | len = AV_RB16(buf_ptr); |
84 | 6.93k | skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size)); |
85 | | |
86 | 6.93k | return 0; |
87 | 7.17k | } |
88 | | |
89 | | static int mxpeg_decode_mxm(MXpegDecodeContext *s, |
90 | | const uint8_t *buf_ptr, int buf_size) |
91 | 5.01k | { |
92 | 5.01k | unsigned bitmask_size, mb_count; |
93 | 5.01k | int i; |
94 | | |
95 | 5.01k | s->mb_width = AV_RL16(buf_ptr+4); |
96 | 5.01k | s->mb_height = AV_RL16(buf_ptr+6); |
97 | 5.01k | mb_count = s->mb_width * s->mb_height; |
98 | | |
99 | 5.01k | bitmask_size = (mb_count + 7) >> 3; |
100 | 5.01k | if (bitmask_size > buf_size - 12) { |
101 | 248 | av_log(s->jpg.avctx, AV_LOG_ERROR, |
102 | 248 | "MXM bitmask is not complete\n"); |
103 | 248 | return AVERROR(EINVAL); |
104 | 248 | } |
105 | | |
106 | 4.76k | if (s->bitmask_size != bitmask_size) { |
107 | 964 | s->bitmask_size = 0; |
108 | 964 | av_freep(&s->mxm_bitmask); |
109 | 964 | s->mxm_bitmask = av_malloc(bitmask_size); |
110 | 964 | if (!s->mxm_bitmask) { |
111 | 0 | av_log(s->jpg.avctx, AV_LOG_ERROR, |
112 | 0 | "MXM bitmask memory allocation error\n"); |
113 | 0 | return AVERROR(ENOMEM); |
114 | 0 | } |
115 | | |
116 | 964 | av_freep(&s->completion_bitmask); |
117 | 964 | s->completion_bitmask = av_mallocz(bitmask_size); |
118 | 964 | if (!s->completion_bitmask) { |
119 | 0 | av_log(s->jpg.avctx, AV_LOG_ERROR, |
120 | 0 | "Completion bitmask memory allocation error\n"); |
121 | 0 | return AVERROR(ENOMEM); |
122 | 0 | } |
123 | | |
124 | 964 | s->bitmask_size = bitmask_size; |
125 | 964 | } |
126 | | |
127 | 4.76k | memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size); |
128 | 4.76k | s->got_mxm_bitmask = 1; |
129 | | |
130 | 4.76k | if (!s->has_complete_frame) { |
131 | 1.12k | uint8_t completion_check = 0xFF; |
132 | 53.0k | for (i = 0; i < bitmask_size; ++i) { |
133 | 51.9k | s->completion_bitmask[i] |= s->mxm_bitmask[i]; |
134 | 51.9k | completion_check &= s->completion_bitmask[i]; |
135 | 51.9k | } |
136 | 1.12k | s->has_complete_frame = !(completion_check ^ 0xFF); |
137 | 1.12k | } |
138 | | |
139 | 4.76k | return 0; |
140 | 4.76k | } |
141 | | |
142 | | static int mxpeg_decode_com(MXpegDecodeContext *s, |
143 | | const uint8_t *buf_ptr, int buf_size) |
144 | 6.50k | { |
145 | 6.50k | int len, ret = 0; |
146 | 6.50k | if (buf_size < 2) |
147 | 213 | return 0; |
148 | 6.28k | len = AV_RB16(buf_ptr); |
149 | 6.28k | if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) { |
150 | 5.01k | ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2); |
151 | 5.01k | } |
152 | 6.28k | skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size)); |
153 | | |
154 | 6.28k | return ret; |
155 | 6.50k | } |
156 | | |
157 | | static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, |
158 | | AVFrame *reference_ptr) |
159 | 4.49k | { |
160 | 4.49k | if ((jpg->width + 0x0F)>>4 != s->mb_width || |
161 | 4.49k | (jpg->height + 0x0F)>>4 != s->mb_height) { |
162 | 1.33k | av_log(jpg->avctx, AV_LOG_ERROR, |
163 | 1.33k | "Picture dimensions stored in SOF and MXM mismatch\n"); |
164 | 1.33k | return AVERROR(EINVAL); |
165 | 1.33k | } |
166 | | |
167 | 3.16k | if (reference_ptr->data[0]) { |
168 | 3.03k | int i; |
169 | 13.6k | for (i = 0; i < MAX_COMPONENTS; ++i) { |
170 | 11.0k | if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) || |
171 | 11.0k | reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) { |
172 | 442 | av_log(jpg->avctx, AV_LOG_ERROR, |
173 | 442 | "Dimensions of current and reference picture mismatch\n"); |
174 | 442 | return AVERROR(EINVAL); |
175 | 442 | } |
176 | 11.0k | } |
177 | 3.03k | } |
178 | | |
179 | 2.72k | return 0; |
180 | 3.16k | } |
181 | | |
182 | | static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
183 | | int *got_frame, AVPacket *avpkt) |
184 | 169k | { |
185 | 169k | const uint8_t *buf = avpkt->data; |
186 | 169k | int buf_size = avpkt->size; |
187 | 169k | MXpegDecodeContext *s = avctx->priv_data; |
188 | 169k | MJpegDecodeContext *jpg = &s->jpg; |
189 | 169k | const uint8_t *buf_end, *buf_ptr; |
190 | 169k | const uint8_t *unescaped_buf_ptr; |
191 | 169k | int unescaped_buf_size; |
192 | 169k | int start_code; |
193 | 169k | int ret; |
194 | | |
195 | 169k | if (avctx->skip_frame == AVDISCARD_ALL) |
196 | 304 | return AVERROR_PATCHWELCOME; |
197 | | |
198 | 169k | buf_ptr = buf; |
199 | 169k | buf_end = buf + buf_size; |
200 | 169k | jpg->got_picture = 0; |
201 | 169k | s->got_mxm_bitmask = 0; |
202 | 169k | s->got_sof_data = !!s->got_sof_data; |
203 | 295k | while (buf_ptr < buf_end) { |
204 | 237k | start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end, |
205 | 237k | &unescaped_buf_ptr, &unescaped_buf_size); |
206 | 237k | if (start_code < 0) |
207 | 38.0k | goto the_end; |
208 | 199k | { |
209 | 199k | init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8); |
210 | | |
211 | 199k | if (start_code >= APP0 && start_code <= APP15) { |
212 | 7.17k | mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size); |
213 | 7.17k | } |
214 | | |
215 | 199k | switch (start_code) { |
216 | 1.51k | case SOI: |
217 | 1.51k | if (jpg->got_picture) //emulating EOI |
218 | 225 | goto the_end; |
219 | 1.28k | break; |
220 | 1.28k | case EOI: |
221 | 291 | goto the_end; |
222 | 3.90k | case DQT: |
223 | 3.90k | ret = ff_mjpeg_decode_dqt(jpg); |
224 | 3.90k | if (ret < 0) { |
225 | 971 | av_log(avctx, AV_LOG_ERROR, |
226 | 971 | "quantization table decode error\n"); |
227 | 971 | return ret; |
228 | 971 | } |
229 | 2.93k | break; |
230 | 7.24k | case DHT: |
231 | 7.24k | ret = ff_mjpeg_decode_dht(jpg); |
232 | 7.24k | if (ret < 0) { |
233 | 1.94k | av_log(avctx, AV_LOG_ERROR, |
234 | 1.94k | "huffman table decode error\n"); |
235 | 1.94k | return ret; |
236 | 1.94k | } |
237 | 5.29k | break; |
238 | 6.50k | case COM: |
239 | 6.50k | ret = mxpeg_decode_com(s, unescaped_buf_ptr, |
240 | 6.50k | unescaped_buf_size); |
241 | 6.50k | if (ret < 0) |
242 | 248 | return ret; |
243 | 6.25k | break; |
244 | 138k | case SOF0: |
245 | 138k | if (s->got_sof_data > 1) { |
246 | 1.54k | av_log(avctx, AV_LOG_ERROR, |
247 | 1.54k | "Multiple SOF in a frame\n"); |
248 | 1.54k | return AVERROR_INVALIDDATA; |
249 | 1.54k | } |
250 | 136k | ret = ff_mjpeg_decode_sof(jpg); |
251 | 136k | if (ret < 0) { |
252 | 66.4k | av_log(avctx, AV_LOG_ERROR, |
253 | 66.4k | "SOF data decode error\n"); |
254 | 66.4k | s->got_sof_data = 0; |
255 | 66.4k | return ret; |
256 | 66.4k | } |
257 | 70.0k | if (jpg->interlaced) { |
258 | 242 | av_log(avctx, AV_LOG_ERROR, |
259 | 242 | "Interlaced mode not supported in MxPEG\n"); |
260 | 242 | s->got_sof_data = 0; |
261 | 242 | return AVERROR(EINVAL); |
262 | 242 | } |
263 | 69.8k | s->got_sof_data ++; |
264 | 69.8k | break; |
265 | 23.4k | case SOS: |
266 | 23.4k | if (!s->got_sof_data) { |
267 | 3.05k | av_log(avctx, AV_LOG_WARNING, |
268 | 3.05k | "Can not process SOS without SOF data, skipping\n"); |
269 | 3.05k | break; |
270 | 3.05k | } |
271 | 20.3k | if (!jpg->got_picture) { |
272 | 2.17k | if (jpg->first_picture) { |
273 | 0 | av_log(avctx, AV_LOG_WARNING, |
274 | 0 | "First picture has no SOF, skipping\n"); |
275 | 0 | break; |
276 | 0 | } |
277 | 2.17k | if (!s->got_mxm_bitmask){ |
278 | 1.34k | av_log(avctx, AV_LOG_WARNING, |
279 | 1.34k | "Non-key frame has no MXM, skipping\n"); |
280 | 1.34k | break; |
281 | 1.34k | } |
282 | | /* use stored SOF data to allocate current picture */ |
283 | 832 | av_frame_unref(jpg->picture_ptr); |
284 | 832 | if ((ret = ff_get_buffer(avctx, jpg->picture_ptr, |
285 | 832 | AV_GET_BUFFER_FLAG_REF)) < 0) |
286 | 0 | return ret; |
287 | 832 | jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P; |
288 | 832 | jpg->picture_ptr->flags &= ~AV_FRAME_FLAG_KEY; |
289 | 832 | jpg->got_picture = 1; |
290 | 18.1k | } else { |
291 | 18.1k | jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I; |
292 | 18.1k | jpg->picture_ptr->flags |= AV_FRAME_FLAG_KEY; |
293 | 18.1k | } |
294 | | |
295 | 19.0k | if (s->got_mxm_bitmask) { |
296 | 4.49k | AVFrame *reference_ptr = s->picture[s->picture_index ^ 1]; |
297 | 4.49k | if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0) |
298 | 1.77k | break; |
299 | | |
300 | | /* allocate dummy reference picture if needed */ |
301 | 2.72k | if (!reference_ptr->data[0] && |
302 | 2.72k | (ret = ff_get_buffer(avctx, reference_ptr, |
303 | 125 | AV_GET_BUFFER_FLAG_REF)) < 0) |
304 | 0 | return ret; |
305 | | |
306 | 2.72k | ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, s->bitmask_size, reference_ptr); |
307 | 2.72k | if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) |
308 | 201 | return ret; |
309 | 14.5k | } else { |
310 | 14.5k | ret = ff_mjpeg_decode_sos(jpg, NULL, 0, NULL); |
311 | 14.5k | if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) |
312 | 261 | return ret; |
313 | 14.5k | } |
314 | | |
315 | 16.7k | break; |
316 | 199k | } |
317 | | |
318 | 126k | buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3; |
319 | 126k | } |
320 | | |
321 | 126k | } |
322 | | |
323 | 97.1k | the_end: |
324 | 97.1k | if (jpg->got_picture) { |
325 | 63.5k | int ret = av_frame_ref(rframe, jpg->picture_ptr); |
326 | 63.5k | if (ret < 0) |
327 | 0 | return ret; |
328 | 63.5k | *got_frame = 1; |
329 | | |
330 | 63.5k | s->picture_index ^= 1; |
331 | 63.5k | jpg->picture_ptr = s->picture[s->picture_index]; |
332 | | |
333 | 63.5k | if (!s->has_complete_frame) { |
334 | 2.47k | if (!s->got_mxm_bitmask) |
335 | 2.16k | s->has_complete_frame = 1; |
336 | 305 | else |
337 | 305 | *got_frame = 0; |
338 | 2.47k | } |
339 | 63.5k | } |
340 | | |
341 | 97.1k | return buf_ptr - buf; |
342 | 97.1k | } |
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 | | }; |