• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/rv30.c

Go to the documentation of this file.
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 (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->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     /* all vertical edges are filtered first
00158      * and horizontal edges are filtered on the next iteration
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->qscale_table[mb_pos]];
00163         if(mb_x)
00164             left_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - 1]];
00165         for(j = 0; j < 16; j += 4){
00166             Y = s->current_picture_ptr->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->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->qscale_table[mb_pos]];
00205         if(row)
00206             top_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - s->mb_stride]];
00207         for(j = 4*!row; j < 16; j += 4){
00208             Y = s->current_picture_ptr->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->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         return AVERROR(EINVAL);
00265     }
00266     r->parse_slice_header = rv30_parse_slice_header;
00267     r->decode_intra_types = rv30_decode_intra_types;
00268     r->decode_mb_info     = rv30_decode_mb_info;
00269     r->loop_filter        = rv30_loop_filter;
00270     r->luma_dc_quant_i = rv30_luma_dc_quant;
00271     r->luma_dc_quant_p = rv30_luma_dc_quant;
00272     return 0;
00273 }
00274 
00275 AVCodec ff_rv30_decoder = {
00276     "rv30",
00277     AVMEDIA_TYPE_VIDEO,
00278     CODEC_ID_RV30,
00279     sizeof(RV34DecContext),
00280     rv30_decode_init,
00281     NULL,
00282     ff_rv34_decode_end,
00283     ff_rv34_decode_frame,
00284     CODEC_CAP_DR1 | CODEC_CAP_DELAY,
00285     .flush = ff_mpeg_flush,
00286     .long_name = NULL_IF_CONFIG_SMALL("RealVideo 3.0"),
00287     .pix_fmts= ff_pixfmt_list_420,
00288 };

Generated on Fri Feb 22 2013 07:24:28 for FFmpeg by  doxygen 1.7.1