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 "dsputil.h"
00029 #include "mpegvideo.h"
00030 #include "golomb.h"
00031
00032 #include "rv34.h"
00033 #include "rv30data.h"
00034
00035
00036 static int rv30_parse_slice_header(RV34DecContext *r, GetBitContext *gb, SliceInfo *si)
00037 {
00038 int mb_bits;
00039 int w = r->s.width, h = r->s.height;
00040 int mb_size;
00041 int rpr;
00042
00043 memset(si, 0, sizeof(SliceInfo));
00044 if(get_bits(gb, 3))
00045 return -1;
00046 si->type = get_bits(gb, 2);
00047 if(si->type == 1) si->type = 0;
00048 if(get_bits1(gb))
00049 return -1;
00050 si->quant = get_bits(gb, 5);
00051 skip_bits1(gb);
00052 si->pts = get_bits(gb, 13);
00053 rpr = get_bits(gb, r->rpr);
00054 if (r->s.avctx->extradata_size < 8 + rpr*2) {
00055 av_log(r->s.avctx, AV_LOG_WARNING,
00056 "Extradata does not contain selected resolution\n");
00057 rpr = 0;
00058 }
00059 if(rpr){
00060 w = r->s.avctx->extradata[6 + rpr*2] << 2;
00061 h = r->s.avctx->extradata[7 + rpr*2] << 2;
00062 }
00063 si->width = w;
00064 si->height = h;
00065 mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
00066 mb_bits = ff_rv34_get_start_offset(gb, mb_size);
00067 si->start = get_bits(gb, mb_bits);
00068 skip_bits1(gb);
00069 return 0;
00070 }
00071
00075 static int rv30_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int8_t *dst)
00076 {
00077 int i, j, k;
00078
00079 for(i = 0; i < 4; i++, dst += r->intra_types_stride - 4){
00080 for(j = 0; j < 4; j+= 2){
00081 int code = svq3_get_ue_golomb(gb) << 1;
00082 if(code >= 81U*2U){
00083 av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction code\n");
00084 return -1;
00085 }
00086 for(k = 0; k < 2; k++){
00087 int A = dst[-r->intra_types_stride] + 1;
00088 int B = dst[-1] + 1;
00089 *dst++ = rv30_itype_from_context[A * 90 + B * 9 + rv30_itype_code[code + k]];
00090 if(dst[-1] == 9){
00091 av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction mode\n");
00092 return -1;
00093 }
00094 }
00095 }
00096 }
00097 return 0;
00098 }
00099
00103 static int rv30_decode_mb_info(RV34DecContext *r)
00104 {
00105 static const int rv30_p_types[6] = { RV34_MB_SKIP, RV34_MB_P_16x16, RV34_MB_P_8x8, -1, RV34_MB_TYPE_INTRA, RV34_MB_TYPE_INTRA16x16 };
00106 static const int rv30_b_types[6] = { RV34_MB_SKIP, RV34_MB_B_DIRECT, RV34_MB_B_FORWARD, RV34_MB_B_BACKWARD, RV34_MB_TYPE_INTRA, RV34_MB_TYPE_INTRA16x16 };
00107 MpegEncContext *s = &r->s;
00108 GetBitContext *gb = &s->gb;
00109 int code = svq3_get_ue_golomb(gb);
00110
00111 if(code > 11U){
00112 av_log(s->avctx, AV_LOG_ERROR, "Incorrect MB type code\n");
00113 return -1;
00114 }
00115 if(code > 5){
00116 av_log(s->avctx, AV_LOG_ERROR, "dquant needed\n");
00117 code -= 6;
00118 }
00119 if(s->pict_type != AV_PICTURE_TYPE_B)
00120 return rv30_p_types[code];
00121 else
00122 return rv30_b_types[code];
00123 }
00124
00125 static inline void rv30_weak_loop_filter(uint8_t *src, const int step,
00126 const int stride, const int lim)
00127 {
00128 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00129 int i, diff;
00130
00131 for(i = 0; i < 4; i++){
00132 diff = ((src[-2*step] - src[1*step]) - (src[-1*step] - src[0*step])*4) >> 3;
00133 diff = av_clip(diff, -lim, lim);
00134 src[-1*step] = cm[src[-1*step] + diff];
00135 src[ 0*step] = cm[src[ 0*step] - diff];
00136 src += stride;
00137 }
00138 }
00139
00140 static void rv30_loop_filter(RV34DecContext *r, int row)
00141 {
00142 MpegEncContext *s = &r->s;
00143 int mb_pos, mb_x;
00144 int i, j, k;
00145 uint8_t *Y, *C;
00146 int loc_lim, cur_lim, left_lim = 0, top_lim = 0;
00147
00148 mb_pos = row * s->mb_stride;
00149 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
00150 int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];
00151 if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
00152 r->deblock_coefs[mb_pos] = 0xFFFF;
00153 if(IS_INTRA(mbtype))
00154 r->cbp_chroma[mb_pos] = 0xFF;
00155 }
00156
00157
00158
00159
00160 mb_pos = row * s->mb_stride;
00161 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
00162 cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos]];
00163 if(mb_x)
00164 left_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos - 1]];
00165 for(j = 0; j < 16; j += 4){
00166 Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize + 4 * !mb_x;
00167 for(i = !mb_x; i < 4; i++, Y += 4){
00168 int ij = i + j;
00169 loc_lim = 0;
00170 if(r->deblock_coefs[mb_pos] & (1 << ij))
00171 loc_lim = cur_lim;
00172 else if(!i && r->deblock_coefs[mb_pos - 1] & (1 << (ij + 3)))
00173 loc_lim = left_lim;
00174 else if( i && r->deblock_coefs[mb_pos] & (1 << (ij - 1)))
00175 loc_lim = cur_lim;
00176 if(loc_lim)
00177 rv30_weak_loop_filter(Y, 1, s->linesize, loc_lim);
00178 }
00179 }
00180 for(k = 0; k < 2; k++){
00181 int cur_cbp, left_cbp = 0;
00182 cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
00183 if(mb_x)
00184 left_cbp = (r->cbp_chroma[mb_pos - 1] >> (k*4)) & 0xF;
00185 for(j = 0; j < 8; j += 4){
00186 C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j) * s->uvlinesize + 4 * !mb_x;
00187 for(i = !mb_x; i < 2; i++, C += 4){
00188 int ij = i + (j >> 1);
00189 loc_lim = 0;
00190 if(cur_cbp && (1 << ij))
00191 loc_lim = cur_lim;
00192 else if(!i && left_cbp & (1 << (ij + 1)))
00193 loc_lim = left_lim;
00194 else if( i && cur_cbp & (1 << (ij - 1)))
00195 loc_lim = cur_lim;
00196 if(loc_lim)
00197 rv30_weak_loop_filter(C, 1, s->uvlinesize, loc_lim);
00198 }
00199 }
00200 }
00201 }
00202 mb_pos = row * s->mb_stride;
00203 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
00204 cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos]];
00205 if(row)
00206 top_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos - s->mb_stride]];
00207 for(j = 4*!row; j < 16; j += 4){
00208 Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
00209 for(i = 0; i < 4; i++, Y += 4){
00210 int ij = i + j;
00211 loc_lim = 0;
00212 if(r->deblock_coefs[mb_pos] & (1 << ij))
00213 loc_lim = cur_lim;
00214 else if(!j && r->deblock_coefs[mb_pos - s->mb_stride] & (1 << (ij + 12)))
00215 loc_lim = top_lim;
00216 else if( j && r->deblock_coefs[mb_pos] & (1 << (ij - 4)))
00217 loc_lim = cur_lim;
00218 if(loc_lim)
00219 rv30_weak_loop_filter(Y, s->linesize, 1, loc_lim);
00220 }
00221 }
00222 for(k = 0; k < 2; k++){
00223 int cur_cbp, top_cbp = 0;
00224 cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
00225 if(row)
00226 top_cbp = (r->cbp_chroma[mb_pos - s->mb_stride] >> (k*4)) & 0xF;
00227 for(j = 4*!row; j < 8; j += 4){
00228 C = s->current_picture_ptr->f.data[k+1] + mb_x*8 + (row*8 + j) * s->uvlinesize;
00229 for(i = 0; i < 2; i++, C += 4){
00230 int ij = i + (j >> 1);
00231 loc_lim = 0;
00232 if(r->cbp_chroma[mb_pos] && (1 << ij))
00233 loc_lim = cur_lim;
00234 else if(!j && top_cbp & (1 << (ij + 2)))
00235 loc_lim = top_lim;
00236 else if( j && cur_cbp & (1 << (ij - 2)))
00237 loc_lim = cur_lim;
00238 if(loc_lim)
00239 rv30_weak_loop_filter(C, s->uvlinesize, 1, loc_lim);
00240 }
00241 }
00242 }
00243 }
00244 }
00245
00249 static av_cold int rv30_decode_init(AVCodecContext *avctx)
00250 {
00251 RV34DecContext *r = avctx->priv_data;
00252
00253 r->rv30 = 1;
00254 ff_rv34_decode_init(avctx);
00255 if(avctx->extradata_size < 2){
00256 av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
00257 return -1;
00258 }
00259 r->rpr = (avctx->extradata[1] & 7) >> 1;
00260 r->rpr = FFMIN(r->rpr + 1, 3);
00261 if(avctx->extradata_size - 8 < (r->rpr - 1) * 2){
00262 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata - need at least %d bytes, got %d\n",
00263 6 + r->rpr * 2, avctx->extradata_size);
00264 }
00265 r->parse_slice_header = rv30_parse_slice_header;
00266 r->decode_intra_types = rv30_decode_intra_types;
00267 r->decode_mb_info = rv30_decode_mb_info;
00268 r->loop_filter = rv30_loop_filter;
00269 r->luma_dc_quant_i = rv30_luma_dc_quant;
00270 r->luma_dc_quant_p = rv30_luma_dc_quant;
00271 return 0;
00272 }
00273
00274 AVCodec ff_rv30_decoder = {
00275 .name = "rv30",
00276 .type = AVMEDIA_TYPE_VIDEO,
00277 .id = CODEC_ID_RV30,
00278 .priv_data_size = sizeof(RV34DecContext),
00279 .init = rv30_decode_init,
00280 .close = ff_rv34_decode_end,
00281 .decode = ff_rv34_decode_frame,
00282 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
00283 CODEC_CAP_FRAME_THREADS,
00284 .flush = ff_mpeg_flush,
00285 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 3.0"),
00286 .pix_fmts = ff_pixfmt_list_420,
00287 .init_thread_copy = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_init_thread_copy),
00288 .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_update_thread_context),
00289 };