00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
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; 
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     s->got_picture = 0;
00056 
00057 read_header:
00058     
00059     s->restart_interval = 0;
00060     s->restart_count = 0;
00061     s->mjpb_skiptosod = 0;
00062 
00063     if (buf_end - buf_ptr >= 1 << 28)
00064         return AVERROR_INVALIDDATA;
00065 
00066     init_get_bits(&hgb, buf_ptr, (buf_end - buf_ptr)*8);
00067 
00068     skip_bits(&hgb, 32); 
00069 
00070     if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
00071     {
00072         av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
00073         return AVERROR_INVALIDDATA;
00074     }
00075 
00076     field_size = get_bits_long(&hgb, 32); 
00077     av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
00078     skip_bits(&hgb, 32); 
00079     second_field_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "second_field_offs is %d and size is %d\n");
00080     av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
00081 
00082     dqt_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dqt is %d and size is %d\n");
00083     av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
00084     if (dqt_offs)
00085     {
00086         init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
00087         s->start_code = DQT;
00088         if (ff_mjpeg_decode_dqt(s) < 0 &&
00089             (avctx->err_recognition & AV_EF_EXPLODE))
00090           return AVERROR_INVALIDDATA;
00091     }
00092 
00093     dht_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dht is %d and size is %d\n");
00094     av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
00095     if (dht_offs)
00096     {
00097         init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
00098         s->start_code = DHT;
00099         ff_mjpeg_decode_dht(s);
00100     }
00101 
00102     sof_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00103     av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
00104     if (sof_offs)
00105     {
00106         init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
00107         s->start_code = SOF0;
00108         if (ff_mjpeg_decode_sof(s) < 0)
00109             return -1;
00110     }
00111 
00112     sos_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sos is %d and size is %d\n");
00113     av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
00114     sod_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00115     av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
00116     if (sos_offs)
00117     {
00118         init_get_bits(&s->gb, buf_ptr + sos_offs,
00119                       8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
00120         s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
00121         s->start_code = SOS;
00122         if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
00123             (avctx->err_recognition & AV_EF_EXPLODE))
00124           return AVERROR_INVALIDDATA;
00125     }
00126 
00127     if (s->interlaced) {
00128         s->bottom_field ^= 1;
00129         
00130         if (s->bottom_field != s->interlace_polarity && second_field_offs)
00131         {
00132             buf_ptr = buf + second_field_offs;
00133             goto read_header;
00134             }
00135     }
00136 
00137     
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             = AV_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 };