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
00032 static int mjpegb_decode_frame(AVCodecContext *avctx,
00033 void *data, int *data_size,
00034 const uint8_t *buf, int buf_size)
00035 {
00036 MJpegDecodeContext *s = avctx->priv_data;
00037 const uint8_t *buf_end, *buf_ptr;
00038 AVFrame *picture = data;
00039 GetBitContext hgb;
00040 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
00041 uint32_t field_size, sod_offs;
00042
00043 buf_ptr = buf;
00044 buf_end = buf + buf_size;
00045
00046 read_header:
00047
00048 s->restart_interval = 0;
00049 s->restart_count = 0;
00050 s->mjpb_skiptosod = 0;
00051
00052 if (buf_end - buf_ptr >= 1 << 28)
00053 return AVERROR_INVALIDDATA;
00054
00055 init_get_bits(&hgb, buf_ptr, (buf_end - buf_ptr)*8);
00056
00057 skip_bits(&hgb, 32);
00058
00059 if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
00060 {
00061 av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
00062 return 0;
00063 }
00064
00065 field_size = get_bits_long(&hgb, 32);
00066 av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
00067 skip_bits(&hgb, 32);
00068 second_field_offs = get_bits_long(&hgb, 32);
00069 av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
00070
00071 dqt_offs = get_bits_long(&hgb, 32);
00072 av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
00073 if (dqt_offs)
00074 {
00075 init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
00076 s->start_code = DQT;
00077 ff_mjpeg_decode_dqt(s);
00078 }
00079
00080 dht_offs = get_bits_long(&hgb, 32);
00081 av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
00082 if (dht_offs)
00083 {
00084 init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
00085 s->start_code = DHT;
00086 ff_mjpeg_decode_dht(s);
00087 }
00088
00089 sof_offs = get_bits_long(&hgb, 32);
00090 av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
00091 if (sof_offs)
00092 {
00093 init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
00094 s->start_code = SOF0;
00095 if (ff_mjpeg_decode_sof(s) < 0)
00096 return -1;
00097 }
00098
00099 sos_offs = get_bits_long(&hgb, 32);
00100 av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
00101 sod_offs = get_bits_long(&hgb, 32);
00102 av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
00103 if (sos_offs)
00104 {
00105 init_get_bits(&s->gb, buf_ptr + sos_offs,
00106 8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
00107 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
00108 s->start_code = SOS;
00109 ff_mjpeg_decode_sos(s);
00110 }
00111
00112 if (s->interlaced) {
00113 s->bottom_field ^= 1;
00114
00115 if (s->bottom_field != s->interlace_polarity && second_field_offs)
00116 {
00117 buf_ptr = buf + second_field_offs;
00118 second_field_offs = 0;
00119 goto read_header;
00120 }
00121 }
00122
00123
00124
00125 *picture= s->picture;
00126 *data_size = sizeof(AVFrame);
00127
00128 if(!s->lossless){
00129 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
00130 picture->qstride= 0;
00131 picture->qscale_table= s->qscale_table;
00132 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
00133 if(avctx->debug & FF_DEBUG_QP)
00134 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
00135 picture->quality*= FF_QP2LAMBDA;
00136 }
00137
00138 return buf_ptr - buf;
00139 }
00140
00141 AVCodec mjpegb_decoder = {
00142 "mjpegb",
00143 CODEC_TYPE_VIDEO,
00144 CODEC_ID_MJPEGB,
00145 sizeof(MJpegDecodeContext),
00146 ff_mjpeg_decode_init,
00147 NULL,
00148 ff_mjpeg_decode_end,
00149 mjpegb_decode_frame,
00150 CODEC_CAP_DR1,
00151 NULL,
00152 .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
00153 };