Libav 0.7.1
libavcodec/mxpegdec.c
Go to the documentation of this file.
00001 /*
00002  * MxPEG decoder
00003  * Copyright (c) 2011 Anatoly Nenashev
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 
00028 #include "mjpeg.h"
00029 #include "mjpegdec.h"
00030 
00031 typedef struct MXpegDecodeContext {
00032     MJpegDecodeContext jpg;
00033     AVFrame picture[2]; /* pictures array */
00034     int picture_index; /* index of current picture */
00035     int got_sof_data; /* true if SOF data successfully parsed */
00036     int got_mxm_bitmask; /* true if MXM bitmask available */
00037     uint8_t *mxm_bitmask; /* bitmask buffer */
00038     unsigned bitmask_size; /* size of bitmask */
00039     int has_complete_frame; /* true if has complete frame */
00040     uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
00041     unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
00042 } MXpegDecodeContext;
00043 
00044 static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
00045 {
00046     MXpegDecodeContext *s = avctx->priv_data;
00047 
00048     s->picture[0].reference = s->picture[1].reference = 3;
00049     s->jpg.picture_ptr      = &s->picture[0];
00050     ff_mjpeg_decode_init(avctx);
00051 
00052     return 0;
00053 }
00054 
00055 static int mxpeg_decode_app(MXpegDecodeContext *s,
00056                             const uint8_t *buf_ptr, int buf_size)
00057 {
00058     int len;
00059     if (buf_size < 2)
00060         return 0;
00061     len = AV_RB16(buf_ptr);
00062     skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
00063 
00064     return 0;
00065 }
00066 
00067 static int mxpeg_decode_mxm(MXpegDecodeContext *s,
00068                             const uint8_t *buf_ptr, int buf_size)
00069 {
00070     unsigned bitmask_size, mb_count;
00071     int i;
00072 
00073     s->mb_width  = AV_RL16(buf_ptr+4);
00074     s->mb_height = AV_RL16(buf_ptr+6);
00075     mb_count = s->mb_width * s->mb_height;
00076 
00077     bitmask_size = (mb_count + 7) >> 3;
00078     if (bitmask_size > buf_size - 12) {
00079         av_log(s->jpg.avctx, AV_LOG_ERROR,
00080                "MXM bitmask is not complete\n");
00081         return AVERROR(EINVAL);
00082     }
00083 
00084     if (s->bitmask_size != bitmask_size) {
00085         av_freep(&s->mxm_bitmask);
00086         s->mxm_bitmask = av_malloc(bitmask_size);
00087         if (!s->mxm_bitmask) {
00088             av_log(s->jpg.avctx, AV_LOG_ERROR,
00089                    "MXM bitmask memory allocation error\n");
00090             return AVERROR(ENOMEM);
00091         }
00092 
00093         av_freep(&s->completion_bitmask);
00094         s->completion_bitmask = av_mallocz(bitmask_size);
00095         if (!s->completion_bitmask) {
00096             av_log(s->jpg.avctx, AV_LOG_ERROR,
00097                    "Completion bitmask memory allocation error\n");
00098             return AVERROR(ENOMEM);
00099         }
00100 
00101         s->bitmask_size = bitmask_size;
00102     }
00103 
00104     memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
00105     s->got_mxm_bitmask = 1;
00106 
00107     if (!s->has_complete_frame) {
00108         uint8_t completion_check = 0xFF;
00109         for (i = 0; i < bitmask_size; ++i) {
00110             s->completion_bitmask[i] |= s->mxm_bitmask[i];
00111             completion_check &= s->completion_bitmask[i];
00112         }
00113         s->has_complete_frame = !(completion_check ^ 0xFF);
00114     }
00115 
00116     return 0;
00117 }
00118 
00119 static int mxpeg_decode_com(MXpegDecodeContext *s,
00120                             const uint8_t *buf_ptr, int buf_size)
00121 {
00122     int len, ret = 0;
00123     if (buf_size < 2)
00124         return 0;
00125     len = AV_RB16(buf_ptr);
00126     if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
00127         ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
00128     }
00129     skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
00130 
00131     return ret;
00132 }
00133 
00134 static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
00135                                   AVFrame *reference_ptr)
00136 {
00137     if ((jpg->width + 0x0F)>>4 != s->mb_width ||
00138         (jpg->height + 0x0F)>>4 != s->mb_height) {
00139         av_log(jpg->avctx, AV_LOG_ERROR,
00140                "Picture dimensions stored in SOF and MXM mismatch\n");
00141         return AVERROR(EINVAL);
00142     }
00143 
00144     if (reference_ptr->data[0]) {
00145         int i;
00146         for (i = 0; i < MAX_COMPONENTS; ++i) {
00147             if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
00148                  reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
00149                 av_log(jpg->avctx, AV_LOG_ERROR,
00150                        "Dimensions of current and reference picture mismatch\n");
00151                 return AVERROR(EINVAL);
00152             }
00153         }
00154     }
00155 
00156     return 0;
00157 }
00158 
00159 static int mxpeg_decode_frame(AVCodecContext *avctx,
00160                           void *data, int *data_size,
00161                           AVPacket *avpkt)
00162 {
00163     const uint8_t *buf = avpkt->data;
00164     int buf_size = avpkt->size;
00165     MXpegDecodeContext *s = avctx->priv_data;
00166     MJpegDecodeContext *jpg = &s->jpg;
00167     const uint8_t *buf_end, *buf_ptr;
00168     const uint8_t *unescaped_buf_ptr;
00169     int unescaped_buf_size;
00170     int start_code;
00171     AVFrame *picture = data;
00172     int ret;
00173 
00174     buf_ptr = buf;
00175     buf_end = buf + buf_size;
00176     jpg->got_picture = 0;
00177     s->got_mxm_bitmask = 0;
00178     while (buf_ptr < buf_end) {
00179         start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
00180                                           &unescaped_buf_ptr, &unescaped_buf_size);
00181         if (start_code < 0)
00182             goto the_end;
00183         {
00184             init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
00185 
00186             if (start_code >= APP0 && start_code <= APP15) {
00187                 mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
00188             }
00189 
00190             switch (start_code) {
00191             case SOI:
00192                 if (jpg->got_picture) //emulating EOI
00193                     goto the_end;
00194                 break;
00195             case EOI:
00196                 goto the_end;
00197             case DQT:
00198                 ret = ff_mjpeg_decode_dqt(jpg);
00199                 if (ret < 0) {
00200                     av_log(avctx, AV_LOG_ERROR,
00201                            "quantization table decode error\n");
00202                     return ret;
00203                 }
00204                 break;
00205             case DHT:
00206                 ret = ff_mjpeg_decode_dht(jpg);
00207                 if (ret < 0) {
00208                     av_log(avctx, AV_LOG_ERROR,
00209                            "huffman table decode error\n");
00210                     return ret;
00211                 }
00212                 break;
00213             case COM:
00214                 ret = mxpeg_decode_com(s, unescaped_buf_ptr,
00215                                        unescaped_buf_size);
00216                 if (ret < 0)
00217                     return ret;
00218                 break;
00219             case SOF0:
00220                 s->got_sof_data = 0;
00221                 ret = ff_mjpeg_decode_sof(jpg);
00222                 if (ret < 0) {
00223                     av_log(avctx, AV_LOG_ERROR,
00224                            "SOF data decode error\n");
00225                     return ret;
00226                 }
00227                 if (jpg->interlaced) {
00228                     av_log(avctx, AV_LOG_ERROR,
00229                            "Interlaced mode not supported in MxPEG\n");
00230                     return AVERROR(EINVAL);
00231                 }
00232                 s->got_sof_data = 1;
00233                 break;
00234             case SOS:
00235                 if (!s->got_sof_data) {
00236                     av_log(avctx, AV_LOG_WARNING,
00237                            "Can not process SOS without SOF data, skipping\n");
00238                     break;
00239                 }
00240                 if (!jpg->got_picture) {
00241                     if (jpg->first_picture) {
00242                         av_log(avctx, AV_LOG_WARNING,
00243                                "First picture has no SOF, skipping\n");
00244                         break;
00245                     }
00246                     if (!s->got_mxm_bitmask){
00247                         av_log(avctx, AV_LOG_WARNING,
00248                                "Non-key frame has no MXM, skipping\n");
00249                         break;
00250                     }
00251                     /* use stored SOF data to allocate current picture */
00252                     if (jpg->picture_ptr->data[0])
00253                         avctx->release_buffer(avctx, jpg->picture_ptr);
00254                     if (avctx->get_buffer(avctx, jpg->picture_ptr) < 0) {
00255                         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00256                         return AVERROR(ENOMEM);
00257                     }
00258                     jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
00259                     jpg->picture_ptr->key_frame = 0;
00260                     jpg->got_picture = 1;
00261                 } else {
00262                     jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
00263                     jpg->picture_ptr->key_frame = 1;
00264                 }
00265 
00266                 if (s->got_mxm_bitmask) {
00267                     AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
00268                     if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
00269                         break;
00270 
00271                     /* allocate dummy reference picture if needed */
00272                     if (!reference_ptr->data[0] &&
00273                         avctx->get_buffer(avctx, reference_ptr) < 0) {
00274                         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00275                         return AVERROR(ENOMEM);
00276                     }
00277 
00278                     ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
00279                 } else {
00280                     ff_mjpeg_decode_sos(jpg, NULL, NULL);
00281                 }
00282 
00283                 break;
00284             }
00285 
00286             buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
00287         }
00288 
00289     }
00290 
00291 the_end:
00292     if (jpg->got_picture) {
00293         *data_size = sizeof(AVFrame);
00294         *picture = *jpg->picture_ptr;
00295         s->picture_index ^= 1;
00296         jpg->picture_ptr = &s->picture[s->picture_index];
00297 
00298         if (!s->has_complete_frame) {
00299             if (!s->got_mxm_bitmask)
00300                 s->has_complete_frame = 1;
00301             else
00302                 *data_size = 0;
00303         }
00304     }
00305 
00306     return buf_ptr - buf;
00307 }
00308 
00309 static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
00310 {
00311     MXpegDecodeContext *s = avctx->priv_data;
00312     MJpegDecodeContext *jpg = &s->jpg;
00313     int i;
00314 
00315     jpg->picture_ptr = NULL;
00316     ff_mjpeg_decode_end(avctx);
00317 
00318     for (i = 0; i < 2; ++i) {
00319         if (s->picture[i].data[0])
00320             avctx->release_buffer(avctx, &s->picture[i]);
00321     }
00322 
00323     av_freep(&s->mxm_bitmask);
00324     av_freep(&s->completion_bitmask);
00325 
00326     return 0;
00327 }
00328 
00329 AVCodec ff_mxpeg_decoder = {
00330     .name           = "mxpeg",
00331     .long_name      = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
00332     .type           = AVMEDIA_TYPE_VIDEO,
00333     .id             = CODEC_ID_MXPEG,
00334     .priv_data_size = sizeof(MXpegDecodeContext),
00335     .init           = mxpeg_decode_init,
00336     .close          = mxpeg_decode_end,
00337     .decode         = mxpeg_decode_frame,
00338     .capabilities   = CODEC_CAP_DR1,
00339     .max_lowres     = 3
00340 };