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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
00063 
00064 skip_bits(&hgb, 32); /* reserved zeros */
00065 
00066 if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
00067 {
00068 av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
00069 return 0;
00070 }
00071 
00072 field_size = get_bits_long(&hgb, 32); /* field size */
00073 av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
00074 skip_bits(&hgb, 32); /* padded field size */
00075 second_field_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "second_field_offs is %d and size is %d\n");
00076 av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
00077 
00078 dqt_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dqt is %d and size is %d\n");
00079 av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
00080 if (dqt_offs)
00081 {
00082 init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
00083 s->start_code = DQT;
00084 if (ff_mjpeg_decode_dqt(s) < 0 &&
00085 (avctx->err_recognition & AV_EF_EXPLODE))
00086 return AVERROR_INVALIDDATA;
00087 }
00088 
00089 dht_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dht is %d and size is %d\n");
00090 av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
00091 if (dht_offs)
00092 {
00093 init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
00094 s->start_code = DHT;
00095 ff_mjpeg_decode_dht(s);
00096 }
00097 
00098 sof_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00099 av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
00100 if (sof_offs)
00101 {
00102 init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
00103 s->start_code = SOF0;
00104 if (ff_mjpeg_decode_sof(s) < 0)
00105 return -1;
00106 }
00107 
00108 sos_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sos is %d and size is %d\n");
00109 av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
00110 sod_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00111 av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
00112 if (sos_offs)
00113 {
00114 // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
00115 init_get_bits(&s->gb, buf_ptr+sos_offs, field_size*8);
00116 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
00117 s->start_code = SOS;
00118 if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&
00119 (avctx->err_recognition & AV_EF_EXPLODE))
00120 return AVERROR_INVALIDDATA;
00121 }
00122 
00123 if (s->interlaced) {
00124 s->bottom_field ^= 1;
00125 /* if not bottom field, do not output image yet */
00126 if (s->bottom_field != s->interlace_polarity && second_field_offs)
00127 {
00128 buf_ptr = buf + second_field_offs;
00129 second_field_offs = 0;
00130 goto read_header;
00131 }
00132 }
00133 
00134 //XXX FIXME factorize, this looks very similar to the EOI code
00135 
00136 *picture= *s->picture_ptr;
00137 *data_size = sizeof(AVFrame);
00138 
00139 if(!s->lossless){
00140 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
00141 picture->qstride= 0;
00142 picture->qscale_table= s->qscale_table;
00143 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
00144 if(avctx->debug & FF_DEBUG_QP)
00145 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
00146 picture->quality*= FF_QP2LAMBDA;
00147 }
00148 
00149 return buf_ptr - buf;
00150 }
00151 
00152 AVCodec ff_mjpegb_decoder = {
00153 .name = "mjpegb",
00154 .type = AVMEDIA_TYPE_VIDEO,
00155 .id = CODEC_ID_MJPEGB,
00156 .priv_data_size = sizeof(MJpegDecodeContext),
00157 .init = ff_mjpeg_decode_init,
00158 .close = ff_mjpeg_decode_end,
00159 .decode = mjpegb_decode_frame,
00160 .capabilities = CODEC_CAP_DR1,
00161 .max_lowres = 3,
00162 .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
00163 };

Generated on Fri Oct 26 02:43:36 2012 for FFmpeg by doxygen 1.5.8

AltStyle によって変換されたページ (->オリジナル) /