3 * Copyright (c) 2011 Anatoly Nenashev
5 * This file is part of FFmpeg.
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.
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.
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
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 ff_mjpeg_decode_init(avctx);
55 static int mxpeg_decode_app(MXpegDecodeContext *s,
56 const uint8_t *buf_ptr, int buf_size)
61 len = AV_RB16(buf_ptr);
62 skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
67 static int mxpeg_decode_mxm(MXpegDecodeContext *s,
68 const uint8_t *buf_ptr, int buf_size)
70 unsigned bitmask_size, mb_count;
73 s->mb_width = AV_RL16(buf_ptr+4);
74 s->mb_height = AV_RL16(buf_ptr+6);
75 mb_count = s->mb_width * s->mb_height;
77 bitmask_size = (mb_count + 7) >> 3;
78 if (bitmask_size > buf_size - 12) {
79 av_log(s->jpg.avctx, AV_LOG_ERROR,
80 "MXM bitmask is not complete\n");
81 return AVERROR(EINVAL);
84 if (s->bitmask_size != bitmask_size) {
86 av_freep(&s->mxm_bitmask);
87 s->mxm_bitmask = av_malloc(bitmask_size);
88 if (!s->mxm_bitmask) {
89 av_log(s->jpg.avctx, AV_LOG_ERROR,
90 "MXM bitmask memory allocation error\n");
91 return AVERROR(ENOMEM);
94 av_freep(&s->completion_bitmask);
95 s->completion_bitmask = av_mallocz(bitmask_size);
96 if (!s->completion_bitmask) {
97 av_log(s->jpg.avctx, AV_LOG_ERROR,
98 "Completion bitmask memory allocation error\n");
99 return AVERROR(ENOMEM);
102 s->bitmask_size = bitmask_size;
105 memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
106 s->got_mxm_bitmask = 1;
108 if (!s->has_complete_frame) {
109 uint8_t completion_check = 0xFF;
110 for (i = 0; i < bitmask_size; ++i) {
111 s->completion_bitmask[i] |= s->mxm_bitmask[i];
112 completion_check &= s->completion_bitmask[i];
114 s->has_complete_frame = !(completion_check ^ 0xFF);
120 static int mxpeg_decode_com(MXpegDecodeContext *s,
121 const uint8_t *buf_ptr, int buf_size)
126 len = AV_RB16(buf_ptr);
127 if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
128 ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
130 skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
135 static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
136 AVFrame *reference_ptr)
138 if ((jpg->width + 0x0F)>>4 != s->mb_width ||
139 (jpg->height + 0x0F)>>4 != s->mb_height) {
140 av_log(jpg->avctx, AV_LOG_ERROR,
141 "Picture dimensions stored in SOF and MXM mismatch\n");
142 return AVERROR(EINVAL);
145 if (reference_ptr->data[0]) {
147 for (i = 0; i < MAX_COMPONENTS; ++i) {
148 if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
149 reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
150 av_log(jpg->avctx, AV_LOG_ERROR,
151 "Dimensions of current and reference picture mismatch\n");
152 return AVERROR(EINVAL);
160 static int mxpeg_decode_frame(AVCodecContext *avctx,
161 void *data, int *data_size,
164 const uint8_t *buf = avpkt->data;
165 int buf_size = avpkt->size;
166 MXpegDecodeContext *s = avctx->priv_data;
167 MJpegDecodeContext *jpg = &s->jpg;
168 const uint8_t *buf_end, *buf_ptr;
169 const uint8_t *unescaped_buf_ptr;
170 int unescaped_buf_size;
172 AVFrame *picture = data;
176 buf_end = buf + buf_size;
177 jpg->got_picture = 0;
178 s->got_mxm_bitmask = 0;
179 while (buf_ptr < buf_end) {
180 start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
181 &unescaped_buf_ptr, &unescaped_buf_size);
185 init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
187 if (start_code >= APP0 && start_code <= APP15) {
188 mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
191 switch (start_code) {
193 if (jpg->got_picture) //emulating EOI
199 ret = ff_mjpeg_decode_dqt(jpg);
201 av_log(avctx, AV_LOG_ERROR,
202 "quantization table decode error\n");
207 ret = ff_mjpeg_decode_dht(jpg);
209 av_log(avctx, AV_LOG_ERROR,
210 "huffman table decode error\n");
215 ret = mxpeg_decode_com(s, unescaped_buf_ptr,
222 ret = ff_mjpeg_decode_sof(jpg);
224 av_log(avctx, AV_LOG_ERROR,
225 "SOF data decode error\n");
228 if (jpg->interlaced) {
229 av_log(avctx, AV_LOG_ERROR,
230 "Interlaced mode not supported in MxPEG\n");
231 return AVERROR(EINVAL);
236 if (!s->got_sof_data) {
237 av_log(avctx, AV_LOG_WARNING,
238 "Can not process SOS without SOF data, skipping\n");
241 if (!jpg->got_picture) {
242 if (jpg->first_picture) {
243 av_log(avctx, AV_LOG_WARNING,
244 "First picture has no SOF, skipping\n");
247 if (!s->got_mxm_bitmask){
248 av_log(avctx, AV_LOG_WARNING,
249 "Non-key frame has no MXM, skipping\n");
252 /* use stored SOF data to allocate current picture */
253 if (jpg->picture_ptr->data[0])
254 avctx->release_buffer(avctx, jpg->picture_ptr);
255 if (avctx->get_buffer(avctx, jpg->picture_ptr) < 0) {
256 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
257 return AVERROR(ENOMEM);
259 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
260 jpg->picture_ptr->key_frame = 0;
261 jpg->got_picture = 1;
263 jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
264 jpg->picture_ptr->key_frame = 1;
267 if (s->got_mxm_bitmask) {
268 AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
269 if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
272 /* allocate dummy reference picture if needed */
273 if (!reference_ptr->data[0] &&
274 avctx->get_buffer(avctx, reference_ptr) < 0) {
275 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
276 return AVERROR(ENOMEM);
279 ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
281 ff_mjpeg_decode_sos(jpg, NULL, NULL);
287 buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
293 if (jpg->got_picture) {
294 *data_size = sizeof(AVFrame);
295 *picture = *jpg->picture_ptr;
296 s->picture_index ^= 1;
297 jpg->picture_ptr = &s->picture[s->picture_index];
299 if (!s->has_complete_frame) {
300 if (!s->got_mxm_bitmask)
301 s->has_complete_frame = 1;
307 return buf_ptr - buf;
310 static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
312 MXpegDecodeContext *s = avctx->priv_data;
313 MJpegDecodeContext *jpg = &s->jpg;
316 jpg->picture_ptr = NULL;
317 ff_mjpeg_decode_end(avctx);
319 for (i = 0; i < 2; ++i) {
320 if (s->picture[i].data[0])
321 avctx->release_buffer(avctx, &s->picture[i]);
324 av_freep(&s->mxm_bitmask);
325 av_freep(&s->completion_bitmask);
330 AVCodec ff_mxpeg_decoder = {
332 .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
333 .type = AVMEDIA_TYPE_VIDEO,
334 .id = CODEC_ID_MXPEG,
335 .priv_data_size = sizeof(MXpegDecodeContext),
336 .init = mxpeg_decode_init,
337 .close = mxpeg_decode_end,
338 .decode = mxpeg_decode_frame,
339 .capabilities = CODEC_CAP_DR1,