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 #include "sp5x.h"
00031
00032
00033 static int sp5x_decode_frame(AVCodecContext *avctx,
00034 void *data, int *data_size,
00035 uint8_t *buf, int buf_size)
00036 {
00037 #if 0
00038 MJpegDecodeContext *s = avctx->priv_data;
00039 #endif
00040 const int qscale = 5;
00041 uint8_t *buf_ptr, *buf_end, *recoded;
00042 int i = 0, j = 0;
00043
00044 if (!avctx->width || !avctx->height)
00045 return -1;
00046
00047 buf_ptr = buf;
00048 buf_end = buf + buf_size;
00049
00050 #if 1
00051 recoded = av_mallocz(buf_size + 1024);
00052 if (!recoded)
00053 return -1;
00054
00055
00056 recoded[j++] = 0xFF;
00057 recoded[j++] = 0xD8;
00058
00059 memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
00060 memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
00061 memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
00062 j += sizeof(sp5x_data_dqt);
00063
00064 memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
00065 j += sizeof(sp5x_data_dht);
00066
00067 memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
00068 AV_WB16(recoded+j+5, avctx->coded_height);
00069 AV_WB16(recoded+j+7, avctx->coded_width);
00070 j += sizeof(sp5x_data_sof);
00071
00072 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
00073 j += sizeof(sp5x_data_sos);
00074
00075 if(avctx->codec_id==CODEC_ID_AMV)
00076 for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
00077 recoded[j++] = buf[i];
00078 else
00079 for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
00080 {
00081 recoded[j++] = buf[i];
00082 if (buf[i] == 0xff)
00083 recoded[j++] = 0;
00084 }
00085
00086
00087 recoded[j++] = 0xFF;
00088 recoded[j++] = 0xD9;
00089
00090 avctx->flags &= ~CODEC_FLAG_EMU_EDGE;
00091 i = ff_mjpeg_decode_frame(avctx, data, data_size, recoded, j);
00092
00093 av_free(recoded);
00094
00095 #else
00096
00097 s->bits = 8;
00098 s->width = avctx->coded_width;
00099 s->height = avctx->coded_height;
00100 s->nb_components = 3;
00101 s->component_id[0] = 0;
00102 s->h_count[0] = 2;
00103 s->v_count[0] = 2;
00104 s->quant_index[0] = 0;
00105 s->component_id[1] = 1;
00106 s->h_count[1] = 1;
00107 s->v_count[1] = 1;
00108 s->quant_index[1] = 1;
00109 s->component_id[2] = 2;
00110 s->h_count[2] = 1;
00111 s->v_count[2] = 1;
00112 s->quant_index[2] = 1;
00113 s->h_max = 2;
00114 s->v_max = 2;
00115
00116 s->qscale_table = av_mallocz((s->width+15)/16);
00117 avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
00118 s->interlaced = 0;
00119
00120 s->picture.reference = 0;
00121 if (avctx->get_buffer(avctx, &s->picture) < 0)
00122 {
00123 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00124 return -1;
00125 }
00126
00127 s->picture.pict_type = I_TYPE;
00128 s->picture.key_frame = 1;
00129
00130 for (i = 0; i < 3; i++)
00131 s->linesize[i] = s->picture.linesize[i] << s->interlaced;
00132
00133
00134 for (i = 0; i < 64; i++)
00135 {
00136 j = s->scantable.permutated[i];
00137 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
00138 }
00139 s->qscale[0] = FFMAX(
00140 s->quant_matrixes[0][s->scantable.permutated[1]],
00141 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
00142
00143 for (i = 0; i < 64; i++)
00144 {
00145 j = s->scantable.permutated[i];
00146 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
00147 }
00148 s->qscale[1] = FFMAX(
00149 s->quant_matrixes[1][s->scantable.permutated[1]],
00150 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
00151
00152
00153
00154
00155 s->comp_index[0] = 0;
00156 s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
00157 s->h_scount[0] = s->h_count[0];
00158 s->v_scount[0] = s->v_count[0];
00159 s->dc_index[0] = 0;
00160 s->ac_index[0] = 0;
00161
00162 s->comp_index[1] = 1;
00163 s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
00164 s->h_scount[1] = s->h_count[1];
00165 s->v_scount[1] = s->v_count[1];
00166 s->dc_index[1] = 1;
00167 s->ac_index[1] = 1;
00168
00169 s->comp_index[2] = 2;
00170 s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
00171 s->h_scount[2] = s->h_count[2];
00172 s->v_scount[2] = s->v_count[2];
00173 s->dc_index[2] = 1;
00174 s->ac_index[2] = 1;
00175
00176 for (i = 0; i < 3; i++)
00177 s->last_dc[i] = 1024;
00178
00179 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
00180 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
00181
00182 init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
00183
00184 return mjpeg_decode_scan(s);
00185 #endif
00186
00187 return i;
00188 }
00189
00190 AVCodec sp5x_decoder = {
00191 "sp5x",
00192 CODEC_TYPE_VIDEO,
00193 CODEC_ID_SP5X,
00194 sizeof(MJpegDecodeContext),
00195 ff_mjpeg_decode_init,
00196 NULL,
00197 ff_mjpeg_decode_end,
00198 sp5x_decode_frame,
00199 CODEC_CAP_DR1,
00200 NULL
00201 };
00202
00203 AVCodec amv_decoder = {
00204 "amv",
00205 CODEC_TYPE_VIDEO,
00206 CODEC_ID_AMV,
00207 sizeof(MJpegDecodeContext),
00208 ff_mjpeg_decode_init,
00209 NULL,
00210 ff_mjpeg_decode_end,
00211 sp5x_decode_frame
00212 };