Libav
|
00001 /* 00002 * RV30 decoder 00003 * Copyright (c) 2007 Konstantin Shishkov 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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(rpr){ 00055 w = r->s.avctx->extradata[6 + rpr*2] << 2; 00056 h = r->s.avctx->extradata[7 + rpr*2] << 2; 00057 } 00058 si->width = w; 00059 si->height = h; 00060 mb_size = ((w + 15) >> 4) * ((h + 15) >> 4); 00061 mb_bits = ff_rv34_get_start_offset(gb, mb_size); 00062 si->start = get_bits(gb, mb_bits); 00063 skip_bits1(gb); 00064 return 0; 00065 } 00066 00070 static int rv30_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int8_t *dst) 00071 { 00072 int i, j, k; 00073 00074 for(i = 0; i < 4; i++, dst += r->intra_types_stride - 4){ 00075 for(j = 0; j < 4; j+= 2){ 00076 int code = svq3_get_ue_golomb(gb) << 1; 00077 if(code >= 81*2){ 00078 av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction code\n"); 00079 return -1; 00080 } 00081 for(k = 0; k < 2; k++){ 00082 int A = dst[-r->intra_types_stride] + 1; 00083 int B = dst[-1] + 1; 00084 *dst++ = rv30_itype_from_context[A * 90 + B * 9 + rv30_itype_code[code + k]]; 00085 if(dst[-1] == 9){ 00086 av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction mode\n"); 00087 return -1; 00088 } 00089 } 00090 } 00091 } 00092 return 0; 00093 } 00094 00098 static int rv30_decode_mb_info(RV34DecContext *r) 00099 { 00100 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 }; 00101 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 }; 00102 MpegEncContext *s = &r->s; 00103 GetBitContext *gb = &s->gb; 00104 int code = svq3_get_ue_golomb(gb); 00105 00106 if(code > 11){ 00107 av_log(s->avctx, AV_LOG_ERROR, "Incorrect MB type code\n"); 00108 return -1; 00109 } 00110 if(code > 5){ 00111 av_log(s->avctx, AV_LOG_ERROR, "dquant needed\n"); 00112 code -= 6; 00113 } 00114 if(s->pict_type != FF_B_TYPE) 00115 return rv30_p_types[code]; 00116 else 00117 return rv30_b_types[code]; 00118 } 00119 00120 static inline void rv30_weak_loop_filter(uint8_t *src, const int step, 00121 const int stride, const int lim) 00122 { 00123 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; 00124 int i, diff; 00125 00126 for(i = 0; i < 4; i++){ 00127 diff = ((src[-2*step] - src[1*step]) - (src[-1*step] - src[0*step])*4) >> 3; 00128 diff = av_clip(diff, -lim, lim); 00129 src[-1*step] = cm[src[-1*step] + diff]; 00130 src[ 0*step] = cm[src[ 0*step] - diff]; 00131 src += stride; 00132 } 00133 } 00134 00135 static void rv30_loop_filter(RV34DecContext *r, int row) 00136 { 00137 MpegEncContext *s = &r->s; 00138 int mb_pos, mb_x; 00139 int i, j, k; 00140 uint8_t *Y, *C; 00141 int loc_lim, cur_lim, left_lim = 0, top_lim = 0; 00142 00143 mb_pos = row * s->mb_stride; 00144 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ 00145 int mbtype = s->current_picture_ptr->mb_type[mb_pos]; 00146 if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype)) 00147 r->deblock_coefs[mb_pos] = 0xFFFF; 00148 if(IS_INTRA(mbtype)) 00149 r->cbp_chroma[mb_pos] = 0xFF; 00150 } 00151 00152 /* all vertical edges are filtered first 00153 * and horizontal edges are filtered on the next iteration 00154 */ 00155 mb_pos = row * s->mb_stride; 00156 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ 00157 cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos]]; 00158 if(mb_x) 00159 left_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - 1]]; 00160 for(j = 0; j < 16; j += 4){ 00161 Y = s->current_picture_ptr->data[0] + mb_x*16 + (row*16 + j) * s->linesize + 4 * !mb_x; 00162 for(i = !mb_x; i < 4; i++, Y += 4){ 00163 int ij = i + j; 00164 loc_lim = 0; 00165 if(r->deblock_coefs[mb_pos] & (1 << ij)) 00166 loc_lim = cur_lim; 00167 else if(!i && r->deblock_coefs[mb_pos - 1] & (1 << (ij + 3))) 00168 loc_lim = left_lim; 00169 else if( i && r->deblock_coefs[mb_pos] & (1 << (ij - 1))) 00170 loc_lim = cur_lim; 00171 if(loc_lim) 00172 rv30_weak_loop_filter(Y, 1, s->linesize, loc_lim); 00173 } 00174 } 00175 for(k = 0; k < 2; k++){ 00176 int cur_cbp, left_cbp = 0; 00177 cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF; 00178 if(mb_x) 00179 left_cbp = (r->cbp_chroma[mb_pos - 1] >> (k*4)) & 0xF; 00180 for(j = 0; j < 8; j += 4){ 00181 C = s->current_picture_ptr->data[k+1] + mb_x*8 + (row*8 + j) * s->uvlinesize + 4 * !mb_x; 00182 for(i = !mb_x; i < 2; i++, C += 4){ 00183 int ij = i + (j >> 1); 00184 loc_lim = 0; 00185 if(cur_cbp && (1 << ij)) 00186 loc_lim = cur_lim; 00187 else if(!i && left_cbp & (1 << (ij + 1))) 00188 loc_lim = left_lim; 00189 else if( i && cur_cbp & (1 << (ij - 1))) 00190 loc_lim = cur_lim; 00191 if(loc_lim) 00192 rv30_weak_loop_filter(C, 1, s->uvlinesize, loc_lim); 00193 } 00194 } 00195 } 00196 } 00197 mb_pos = row * s->mb_stride; 00198 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ 00199 cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos]]; 00200 if(row) 00201 top_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - s->mb_stride]]; 00202 for(j = 4*!row; j < 16; j += 4){ 00203 Y = s->current_picture_ptr->data[0] + mb_x*16 + (row*16 + j) * s->linesize; 00204 for(i = 0; i < 4; i++, Y += 4){ 00205 int ij = i + j; 00206 loc_lim = 0; 00207 if(r->deblock_coefs[mb_pos] & (1 << ij)) 00208 loc_lim = cur_lim; 00209 else if(!j && r->deblock_coefs[mb_pos - s->mb_stride] & (1 << (ij + 12))) 00210 loc_lim = top_lim; 00211 else if( j && r->deblock_coefs[mb_pos] & (1 << (ij - 4))) 00212 loc_lim = cur_lim; 00213 if(loc_lim) 00214 rv30_weak_loop_filter(Y, s->linesize, 1, loc_lim); 00215 } 00216 } 00217 for(k = 0; k < 2; k++){ 00218 int cur_cbp, top_cbp = 0; 00219 cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF; 00220 if(row) 00221 top_cbp = (r->cbp_chroma[mb_pos - s->mb_stride] >> (k*4)) & 0xF; 00222 for(j = 4*!row; j < 8; j += 4){ 00223 C = s->current_picture_ptr->data[k+1] + mb_x*8 + (row*8 + j) * s->uvlinesize; 00224 for(i = 0; i < 2; i++, C += 4){ 00225 int ij = i + (j >> 1); 00226 loc_lim = 0; 00227 if(r->cbp_chroma[mb_pos] && (1 << ij)) 00228 loc_lim = cur_lim; 00229 else if(!j && top_cbp & (1 << (ij + 2))) 00230 loc_lim = top_lim; 00231 else if( j && cur_cbp & (1 << (ij - 2))) 00232 loc_lim = cur_lim; 00233 if(loc_lim) 00234 rv30_weak_loop_filter(C, s->uvlinesize, 1, loc_lim); 00235 } 00236 } 00237 } 00238 } 00239 } 00240 00244 static av_cold int rv30_decode_init(AVCodecContext *avctx) 00245 { 00246 RV34DecContext *r = avctx->priv_data; 00247 00248 r->rv30 = 1; 00249 ff_rv34_decode_init(avctx); 00250 if(avctx->extradata_size < 2){ 00251 av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n"); 00252 return -1; 00253 } 00254 r->rpr = (avctx->extradata[1] & 7) >> 1; 00255 r->rpr = FFMIN(r->rpr + 1, 3); 00256 if(avctx->extradata_size - 8 < (r->rpr - 1) * 2){ 00257 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata - need at least %d bytes, got %d\n", 00258 6 + r->rpr * 2, avctx->extradata_size); 00259 return AVERROR(EINVAL); 00260 } 00261 r->parse_slice_header = rv30_parse_slice_header; 00262 r->decode_intra_types = rv30_decode_intra_types; 00263 r->decode_mb_info = rv30_decode_mb_info; 00264 r->loop_filter = rv30_loop_filter; 00265 r->luma_dc_quant_i = rv30_luma_dc_quant; 00266 r->luma_dc_quant_p = rv30_luma_dc_quant; 00267 return 0; 00268 } 00269 00270 AVCodec rv30_decoder = { 00271 "rv30", 00272 AVMEDIA_TYPE_VIDEO, 00273 CODEC_ID_RV30, 00274 sizeof(RV34DecContext), 00275 rv30_decode_init, 00276 NULL, 00277 ff_rv34_decode_end, 00278 ff_rv34_decode_frame, 00279 CODEC_CAP_DR1 | CODEC_CAP_DELAY, 00280 .flush = ff_mpeg_flush, 00281 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 3.0"), 00282 .pix_fmts= ff_pixfmt_list_420, 00283 };