• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/mjpegbdec.c

Go to the documentation of this file.
00001 /*
00002  * Apple MJPEG-B decoder
00003  * Copyright (c) 2002 Alex Beregszaszi
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 
00027 #include "avcodec.h"
00028 #include "mjpeg.h"
00029 #include "mjpegdec.h"
00030 
00031 static uint32_t read_offs(AVCodecContext *avctx, GetBitContext *gb, uint32_t size, const char *err_msg){
00032     uint32_t offs= get_bits_long(gb, 32);
00033     if(offs >= size){
00034         av_log(avctx, AV_LOG_WARNING, err_msg, offs, size);
00035         return 0;
00036     }
00037     return offs;
00038 }
00039 
00040 static int mjpegb_decode_frame(AVCodecContext *avctx,
00041                               void *data, int *data_size,
00042                               AVPacket *avpkt)
00043 {
00044     const uint8_t *buf = avpkt->data;
00045     int buf_size = avpkt->size;
00046     MJpegDecodeContext *s = avctx->priv_data;
00047     const uint8_t *buf_end, *buf_ptr;
00048     AVFrame *picture = data;
00049     GetBitContext hgb; /* for the header */
00050     uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
00051     uint32_t field_size, sod_offs;
00052 
00053     buf_ptr = buf;
00054     buf_end = buf + buf_size;
00055 
00056 read_header:
00057     /* reset on every SOI */
00058     s->restart_interval = 0;
00059     s->restart_count = 0;
00060     s->mjpb_skiptosod = 0;
00061 
00062     if (buf_end - buf_ptr >= 1 << 28)
00063         return AVERROR_INVALIDDATA;
00064 
00065     init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
00066 
00067     skip_bits(&hgb, 32); /* reserved zeros */
00068 
00069     if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
00070     {
00071         av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
00072         return AVERROR_INVALIDDATA;
00073     }
00074 
00075     field_size = get_bits_long(&hgb, 32); /* field size */
00076     av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
00077     skip_bits(&hgb, 32); /* padded field size */
00078     second_field_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "second_field_offs is %d and size is %d\n");
00079     av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
00080 
00081     dqt_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dqt is %d and size is %d\n");
00082     av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
00083     if (dqt_offs)
00084     {
00085         init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
00086         s->start_code = DQT;
00087         if (ff_mjpeg_decode_dqt(s) < 0 &&
00088             (avctx->err_recognition & AV_EF_EXPLODE))
00089           return AVERROR_INVALIDDATA;
00090     }
00091 
00092     dht_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dht is %d and size is %d\n");
00093     av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
00094     if (dht_offs)
00095     {
00096         init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
00097         s->start_code = DHT;
00098         ff_mjpeg_decode_dht(s);
00099     }
00100 
00101     sof_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00102     av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
00103     if (sof_offs)
00104     {
00105         init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
00106         s->start_code = SOF0;
00107         if (ff_mjpeg_decode_sof(s) < 0)
00108             return -1;
00109     }
00110 
00111     sos_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sos is %d and size is %d\n");
00112     av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
00113     sod_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00114     av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
00115     if (sos_offs)
00116     {
00117         init_get_bits(&s->gb, buf_ptr + sos_offs,
00118                       8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
00119         s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
00120         s->start_code = SOS;
00121         if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
00122             (avctx->err_recognition & AV_EF_EXPLODE))
00123           return AVERROR_INVALIDDATA;
00124     }
00125 
00126     if (s->interlaced) {
00127         s->bottom_field ^= 1;
00128         /* if not bottom field, do not output image yet */
00129         if (s->bottom_field != s->interlace_polarity && second_field_offs)
00130         {
00131             buf_ptr = buf + second_field_offs;
00132             second_field_offs = 0;
00133             goto read_header;
00134             }
00135     }
00136 
00137     //XXX FIXME factorize, this looks very similar to the EOI code
00138 
00139     *picture= *s->picture_ptr;
00140     *data_size = sizeof(AVFrame);
00141 
00142     if(!s->lossless){
00143         picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
00144         picture->qstride= 0;
00145         picture->qscale_table= s->qscale_table;
00146         memset(picture->qscale_table, picture->quality, (s->width+15)/16);
00147         if(avctx->debug & FF_DEBUG_QP)
00148             av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
00149         picture->quality*= FF_QP2LAMBDA;
00150     }
00151 
00152     return buf_size;
00153 }
00154 
00155 AVCodec ff_mjpegb_decoder = {
00156     .name           = "mjpegb",
00157     .type           = AVMEDIA_TYPE_VIDEO,
00158     .id             = CODEC_ID_MJPEGB,
00159     .priv_data_size = sizeof(MJpegDecodeContext),
00160     .init           = ff_mjpeg_decode_init,
00161     .close          = ff_mjpeg_decode_end,
00162     .decode         = mjpegb_decode_frame,
00163     .capabilities   = CODEC_CAP_DR1,
00164     .max_lowres = 3,
00165     .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
00166 };
Generated on Thu Jan 24 2013 17:08:52 for Libav by doxygen 1.7.1