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