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
00056 read_header:
00057
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_end - buf_ptr)*8);
00066
00067 skip_bits(&hgb, 32);
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 0;
00073 }
00074
00075 field_size = get_bits_long(&hgb, 32);
00076 av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
00077 skip_bits(&hgb, 32);
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 ff_mjpeg_decode_dqt(s);
00088 }
00089
00090 dht_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "dht is %d and size is %d\n");
00091 av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
00092 if (dht_offs)
00093 {
00094 init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
00095 s->start_code = DHT;
00096 ff_mjpeg_decode_dht(s);
00097 }
00098
00099 sof_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00100 av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
00101 if (sof_offs)
00102 {
00103 init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
00104 s->start_code = SOF0;
00105 if (ff_mjpeg_decode_sof(s) < 0)
00106 return -1;
00107 }
00108
00109 sos_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sos is %d and size is %d\n");
00110 av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
00111 sod_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "sof is %d and size is %d\n");
00112 av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
00113 if (sos_offs)
00114 {
00115 init_get_bits(&s->gb, buf_ptr + sos_offs,
00116 8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
00117 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
00118 s->start_code = SOS;
00119 ff_mjpeg_decode_sos(s);
00120 }
00121
00122 if (s->interlaced) {
00123 s->bottom_field ^= 1;
00124
00125 if (s->bottom_field != s->interlace_polarity && second_field_offs)
00126 {
00127 buf_ptr = buf + second_field_offs;
00128 second_field_offs = 0;
00129 goto read_header;
00130 }
00131 }
00132
00133
00134
00135 *picture= s->picture;
00136 *data_size = sizeof(AVFrame);
00137
00138 if(!s->lossless){
00139 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
00140 picture->qstride= 0;
00141 picture->qscale_table= s->qscale_table;
00142 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
00143 if(avctx->debug & FF_DEBUG_QP)
00144 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
00145 picture->quality*= FF_QP2LAMBDA;
00146 }
00147
00148 return buf_ptr - buf;
00149 }
00150
00151 AVCodec mjpegb_decoder = {
00152 "mjpegb",
00153 AVMEDIA_TYPE_VIDEO,
00154 CODEC_ID_MJPEGB,
00155 sizeof(MJpegDecodeContext),
00156 ff_mjpeg_decode_init,
00157 NULL,
00158 ff_mjpeg_decode_end,
00159 mjpegb_decode_frame,
00160 CODEC_CAP_DR1,
00161 NULL,
00162 .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
00163 };