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