Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mxpegdec.c @ ce5e49b0

History | View | Annotate | Download (11 KB)

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 "mjpeg.h"
29
#include "mjpegdec.h"
30

    
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 */
42
} MXpegDecodeContext;
43

    
44
static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
45
{
46
    MXpegDecodeContext *s = avctx->priv_data;
47

    
48
    s->picture[0].reference = s->picture[1].reference = 3;
49
    s->jpg.picture_ptr      = &s->picture[0];
50
    ff_mjpeg_decode_init(avctx);
51

    
52
    return 0;
53
}
54

    
55
static int mxpeg_decode_app(MXpegDecodeContext *s,
56
                            const uint8_t *buf_ptr, int buf_size)
57
{
58
    int len;
59
    if (buf_size < 2)
60
        return 0;
61
    len = AV_RB16(buf_ptr);
62
    skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
63

    
64
    return 0;
65
}
66

    
67
static int mxpeg_decode_mxm(MXpegDecodeContext *s,
68
                            const uint8_t *buf_ptr, int buf_size)
69
{
70
    unsigned bitmask_size, mb_count;
71
    int i;
72

    
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;
76

    
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);
82
    }
83

    
84
    if (s->bitmask_size != bitmask_size) {
85
        s->bitmask_size = 0;
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);
92
        }
93

    
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);
100
        }
101

    
102
        s->bitmask_size = bitmask_size;
103
    }
104

    
105
    memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
106
    s->got_mxm_bitmask = 1;
107

    
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];
113
        }
114
        s->has_complete_frame = !(completion_check ^ 0xFF);
115
    }
116

    
117
    return 0;
118
}
119

    
120
static int mxpeg_decode_com(MXpegDecodeContext *s,
121
                            const uint8_t *buf_ptr, int buf_size)
122
{
123
    int len, ret = 0;
124
    if (buf_size < 2)
125
        return 0;
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);
129
    }
130
    skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
131

    
132
    return ret;
133
}
134

    
135
static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
136
                                  AVFrame *reference_ptr)
137
{
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);
143
    }
144

    
145
    if (reference_ptr->data[0]) {
146
        int i;
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);
153
            }
154
        }
155
    }
156

    
157
    return 0;
158
}
159

    
160
static int mxpeg_decode_frame(AVCodecContext *avctx,
161
                          void *data, int *data_size,
162
                          AVPacket *avpkt)
163
{
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;
171
    int start_code;
172
    AVFrame *picture = data;
173
    int ret;
174

    
175
    buf_ptr = buf;
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);
182
        if (start_code < 0)
183
            goto the_end;
184
        {
185
            init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
186

    
187
            if (start_code >= APP0 && start_code <= APP15) {
188
                mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
189
            }
190

    
191
            switch (start_code) {
192
            case SOI:
193
                if (jpg->got_picture) //emulating EOI
194
                    goto the_end;
195
                break;
196
            case EOI:
197
                goto the_end;
198
            case DQT:
199
                ret = ff_mjpeg_decode_dqt(jpg);
200
                if (ret < 0) {
201
                    av_log(avctx, AV_LOG_ERROR,
202
                           "quantization table decode error\n");
203
                    return ret;
204
                }
205
                break;
206
            case DHT:
207
                ret = ff_mjpeg_decode_dht(jpg);
208
                if (ret < 0) {
209
                    av_log(avctx, AV_LOG_ERROR,
210
                           "huffman table decode error\n");
211
                    return ret;
212
                }
213
                break;
214
            case COM:
215
                ret = mxpeg_decode_com(s, unescaped_buf_ptr,
216
                                       unescaped_buf_size);
217
                if (ret < 0)
218
                    return ret;
219
                break;
220
            case SOF0:
221
                s->got_sof_data = 0;
222
                ret = ff_mjpeg_decode_sof(jpg);
223
                if (ret < 0) {
224
                    av_log(avctx, AV_LOG_ERROR,
225
                           "SOF data decode error\n");
226
                    return ret;
227
                }
228
                if (jpg->interlaced) {
229
                    av_log(avctx, AV_LOG_ERROR,
230
                           "Interlaced mode not supported in MxPEG\n");
231
                    return AVERROR(EINVAL);
232
                }
233
                s->got_sof_data = 1;
234
                break;
235
            case SOS:
236
                if (!s->got_sof_data) {
237
                    av_log(avctx, AV_LOG_WARNING,
238
                           "Can not process SOS without SOF data, skipping\n");
239
                    break;
240
                }
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");
245
                        break;
246
                    }
247
                    if (!s->got_mxm_bitmask){
248
                        av_log(avctx, AV_LOG_WARNING,
249
                               "Non-key frame has no MXM, skipping\n");
250
                        break;
251
                    }
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);
258
                    }
259
                    jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
260
                    jpg->picture_ptr->key_frame = 0;
261
                    jpg->got_picture = 1;
262
                } else {
263
                    jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
264
                    jpg->picture_ptr->key_frame = 1;
265
                }
266

    
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)
270
                        break;
271

    
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);
277
                    }
278

    
279
                    ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
280
                } else {
281
                    ff_mjpeg_decode_sos(jpg, NULL, NULL);
282
                }
283

    
284
                break;
285
            }
286

    
287
            buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
288
        }
289

    
290
    }
291

    
292
the_end:
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];
298

    
299
        if (!s->has_complete_frame) {
300
            if (!s->got_mxm_bitmask)
301
                s->has_complete_frame = 1;
302
            else
303
                *data_size = 0;
304
        }
305
    }
306

    
307
    return buf_ptr - buf;
308
}
309

    
310
static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
311
{
312
    MXpegDecodeContext *s = avctx->priv_data;
313
    MJpegDecodeContext *jpg = &s->jpg;
314
    int i;
315

    
316
    jpg->picture_ptr = NULL;
317
    ff_mjpeg_decode_end(avctx);
318

    
319
    for (i = 0; i < 2; ++i) {
320
        if (s->picture[i].data[0])
321
            avctx->release_buffer(avctx, &s->picture[i]);
322
    }
323

    
324
    av_freep(&s->mxm_bitmask);
325
    av_freep(&s->completion_bitmask);
326

    
327
    return 0;
328
}
329

    
330
AVCodec ff_mxpeg_decoder = {
331
    .name           = "mxpeg",
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,
340
    .max_lowres     = 3
341
};