3 * Copyright (c) 2011 Anatoly Nenashev
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 typedef struct MXpegDecodeContext {
32 MJpegDecodeContext jpg;
33 AVFrame picture[2]; /* pictures array */
34 int picture_index; /* index of current picture */
35 int got_sof_data; /* true if SOF data successfully parsed */
36 int got_mxm_bitmask; /* true if MXM bitmask available */
37 uint8_t *mxm_bitmask; /* bitmask buffer */
38 unsigned bitmask_size; /* size of bitmask */
39 int has_complete_frame; /* true if has complete frame */
40 uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
41 unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
44 static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
46 MXpegDecodeContext *s = avctx->priv_data;
48 s->picture[0].reference = s->picture[1].reference = 3;
49 s->jpg.picture_ptr = &s->picture[0];
50 return ff_mjpeg_decode_init(avctx);
53 static int mxpeg_decode_app(MXpegDecodeContext *s,
54 const uint8_t *buf_ptr, int buf_size)
59 len = AV_RB16(buf_ptr);
60 skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
65 static int mxpeg_decode_mxm(MXpegDecodeContext *s,
66 const uint8_t *buf_ptr, int buf_size)
68 unsigned bitmask_size, mb_count;
71 s->mb_width = AV_RL16(buf_ptr+4);
72 s->mb_height = AV_RL16(buf_ptr+6);
73 mb_count = s->mb_width * s->mb_height;
75 bitmask_size = (mb_count + 7) >> 3;
76 if (bitmask_size > buf_size - 12) {
77 av_log(s->jpg.avctx, AV_LOG_ERROR,
78 "MXM bitmask is not complete\n");
79 return AVERROR(EINVAL);
82 if (s->bitmask_size != bitmask_size) {
83 av_freep(&s->mxm_bitmask);
84 s->mxm_bitmask = av_malloc(bitmask_size);
85 if (!s->mxm_bitmask) {
86 av_log(s->jpg.avctx, AV_LOG_ERROR,
87 "MXM bitmask memory allocation error\n");
88 return AVERROR(ENOMEM);
91 av_freep(&s->completion_bitmask);
92 s->completion_bitmask = av_mallocz(bitmask_size);
93 if (!s->completion_bitmask) {
94 av_log(s->jpg.avctx, AV_LOG_ERROR,
95 "Completion bitmask memory allocation error\n");
96 return AVERROR(ENOMEM);
99 s->bitmask_size = bitmask_size;
102 memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
103 s->got_mxm_bitmask = 1;
105 if (!s->has_complete_frame) {
106 uint8_t completion_check = 0xFF;
107 for (i = 0; i < bitmask_size; ++i) {
108 s->completion_bitmask[i] |= s->mxm_bitmask[i];
109 completion_check &= s->completion_bitmask[i];
111 s->has_complete_frame = !(completion_check ^ 0xFF);
117 static int mxpeg_decode_com(MXpegDecodeContext *s,
118 const uint8_t *buf_ptr, int buf_size)
123 len = AV_RB16(buf_ptr);
124 if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
125 ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
127 skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
132 static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
133 AVFrame *reference_ptr)
135 if ((jpg->width + 0x0F)>>4 != s->mb_width ||
136 (jpg->height + 0x0F)>>4 != s->mb_height) {
137 av_log(jpg->avctx, AV_LOG_ERROR,
138 "Picture dimensions stored in SOF and MXM mismatch\n");
139 return AVERROR(EINVAL);
142 if (reference_ptr->data[0]) {
144 for (i = 0; i < MAX_COMPONENTS; ++i) {
145 if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
146 reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
147 av_log(jpg->avctx, AV_LOG_ERROR,
148 "Dimensions of current and reference picture mismatch\n");
149 return AVERROR(EINVAL);
157 static int mxpeg_decode_frame(AVCodecContext *avctx,
158 void *data, int *data_size,
161 const uint8_t *buf = avpkt->data;
162 int buf_size = avpkt->size;
163 MXpegDecodeContext *s = avctx->priv_data;
164 MJpegDecodeContext *jpg = &s->jpg;
165 const uint8_t *buf_end, *buf_ptr;
166 const uint8_t *unescaped_buf_ptr;
167 int unescaped_buf_size;
169 AVFrame *picture = data;
173 buf_end = buf + buf_size;
174 jpg->got_picture = 0;
175 s->got_mxm_bitmask = 0;
176 while (buf_ptr < buf_end) {
177 start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
178 &unescaped_buf_ptr, &unescaped_buf_size);
182 init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
184 if (start_code >= APP0 && start_code <= APP15) {
185 mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
188 switch (start_code) {
190 if (jpg->got_picture) //emulating EOI
196 ret = ff_mjpeg_decode_dqt(jpg);
198 av_log(avctx, AV_LOG_ERROR,
199 "quantization table decode error\n");
204 ret = ff_mjpeg_decode_dht(jpg);
206 av_log(avctx, AV_LOG_ERROR,
207 "huffman table decode error\n");
212 ret = mxpeg_decode_com(s, unescaped_buf_ptr,
219 ret = ff_mjpeg_decode_sof(jpg);
221 av_log(avctx, AV_LOG_ERROR,
222 "SOF data decode error\n");
225 if (jpg->interlaced) {
226 av_log(avctx, AV_LOG_ERROR,
227 "Interlaced mode not supported in MxPEG\n");
228 return AVERROR(EINVAL);
233 if (!s->got_sof_data) {
234 av_log(avctx, AV_LOG_WARNING,
235 "Can not process SOS without SOF data, skipping\n");
238 if (!jpg->got_picture) {
239 if (jpg->first_picture) {
240 av_log(avctx, AV_LOG_WARNING,
241 "First picture has no SOF, skipping\n");
244 if (!s->got_mxm_bitmask){
245 av_log(avctx, AV_LOG_WARNING,
246 "Non-key frame has no MXM, skipping\n");
249 /* use stored SOF data to allocate current picture */
250 if (jpg->picture_ptr->data[0])
251 avctx->release_buffer(avctx, jpg->picture_ptr);
252 if (avctx->get_buffer(avctx, jpg->picture_ptr) < 0) {
253 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
254 return AVERROR(ENOMEM);
256 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
257 jpg->picture_ptr->key_frame = 0;
258 jpg->got_picture = 1;
260 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
261 jpg->picture_ptr->key_frame = 1;
264 if (s->got_mxm_bitmask) {
265 AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
266 if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
269 /* allocate dummy reference picture if needed */
270 if (!reference_ptr->data[0] &&
271 avctx->get_buffer(avctx, reference_ptr) < 0) {
272 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
273 return AVERROR(ENOMEM);
276 ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
277 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
280 ret = ff_mjpeg_decode_sos(jpg, NULL, NULL);
281 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
288 buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
294 if (jpg->got_picture) {
295 *data_size = sizeof(AVFrame);
296 *picture = *jpg->picture_ptr;
297 s->picture_index ^= 1;
298 jpg->picture_ptr = &s->picture[s->picture_index];
300 if (!s->has_complete_frame) {
301 if (!s->got_mxm_bitmask)
302 s->has_complete_frame = 1;
308 return buf_ptr - buf;
311 static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
313 MXpegDecodeContext *s = avctx->priv_data;
314 MJpegDecodeContext *jpg = &s->jpg;
317 jpg->picture_ptr = NULL;
318 ff_mjpeg_decode_end(avctx);
320 for (i = 0; i < 2; ++i) {
321 if (s->picture[i].data[0])
322 avctx->release_buffer(avctx, &s->picture[i]);
325 av_freep(&s->mxm_bitmask);
326 av_freep(&s->completion_bitmask);
331 AVCodec ff_mxpeg_decoder = {
333 .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
334 .type = AVMEDIA_TYPE_VIDEO,
335 .id = CODEC_ID_MXPEG,
336 .priv_data_size = sizeof(MXpegDecodeContext),
337 .init = mxpeg_decode_init,
338 .close = mxpeg_decode_end,
339 .decode = mxpeg_decode_frame,
340 .capabilities = CODEC_CAP_DR1,