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