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