]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/rv34.c
fix adpcm ima qt decoding, channel at init is 0, correct stereo out since samples...
[ffmpeg] / libavcodec / rv34.c
index 1e1c129290f3dc2af1020e799fc63a4c1705fe7b..7749637dc17f9d6ab36b312bf0a7953481ace120 100644 (file)
@@ -99,7 +99,7 @@ static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t
 /**
  * Initialize all tables.
  */
-static void rv34_init_tables()
+static av_cold void rv34_init_tables()
 {
     int i, j, k;
 
@@ -359,13 +359,6 @@ static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
  * @{
  */
 
-static inline int decode210(GetBitContext *gb){
-    if (get_bits1(gb))
-        return 0;
-    else
-        return 2 - get_bits1(gb);
-}
-
 /**
  * Decode starting slice position.
  * @todo Maybe replace with ff_h263_decode_mba() ?
@@ -473,6 +466,21 @@ static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int
     }
 }
 
+/**
+ * Calculate motion vector component that should be added for direct blocks.
+ */
+static int calc_add_mv(MpegEncContext *s, int dir, int component)
+{
+    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
+    int sum;
+
+    sum = (s->next_picture_ptr->motion_val[0][mv_pos][component] +
+           s->next_picture_ptr->motion_val[0][mv_pos + 1][component] +
+           s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride][component] +
+           s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride + 1][component]) >> 2;
+    return dir ? -(sum >> 1) : ((sum + 1) >> 1);
+}
+
 /**
  * Predict motion vector for B-frame macroblock.
  */
@@ -536,7 +544,11 @@ static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
 
     mx += r->dmv[dir][0];
     my += r->dmv[dir][1];
-    //XXX add vector for bidirectionally predicted blocks
+
+    if(block_type == RV34_MB_B_DIRECT){
+        mx += calc_add_mv(s, dir, 0);
+        my += calc_add_mv(s, dir, 1);
+    }
     for(j = 0; j < 2; j++){
         for(i = 0; i < 2; i++){
             cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
@@ -547,6 +559,8 @@ static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
         fill_rectangle(cur_pic->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);
 }
 
+static const int chroma_coeffs[3] = { 8, 5, 3 };
+
 /**
  * generic motion compensation function
  *
@@ -567,33 +581,26 @@ static inline void rv34_mc(RV34DecContext *r, const int block_type,
 {
     MpegEncContext *s = &r->s;
     uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
-    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
+    int dxy, mx, my, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
     int is16x16 = 1;
 
     if(thirdpel){
-#if 0 /// todo
-        int lx, ly;
-
-        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 3;
-        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 3;
-        lx = ((s->current_picture_ptr->motion_val[dir][mv_pos][0] % 3) + 3) % 3;
-        ly = ((s->current_picture_ptr->motion_val[dir][mv_pos][1] % 3) + 3) % 3;
-        dxy = ly*3 + lx;
-        uvmx =
-#endif
-        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
-        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
-        dxy = ((my & 3) << 2) | (mx & 3);
-        uvmx = mx & 6;
-        uvmy = my & 6;
+        mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
+        my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
+        lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
+        ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
+        uvmx = chroma_coeffs[(3*(mx&1) + lx) >> 1];
+        uvmy = chroma_coeffs[(3*(my&1) + ly) >> 1];
     }else{
         mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
         my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
-        dxy = ((my & 3) << 2) | (mx & 3);
+        lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
+        ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
         uvmx = mx & 6;
         uvmy = my & 6;
     }
+    dxy = ly*4 + lx;
     srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];
     srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];
     srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];
@@ -604,8 +611,8 @@ static inline void rv34_mc(RV34DecContext *r, const int block_type,
     srcY += src_y * s->linesize + src_x;
     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
-    if(   (unsigned)(src_x - !!(mx&3)*2) > s->h_edge_pos - !!(mx&3)*2 - (width <<3) - 3
-       || (unsigned)(src_y - !!(my&3)*2) > s->v_edge_pos - !!(my&3)*2 - (height<<3) - 3){
+    if(   (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 3
+       || (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 3){
         uint8_t *uvbuf= s->edge_emu_buffer + 20 * s->linesize;
 
         srcY -= 2 + 2*s->linesize;
@@ -643,15 +650,21 @@ static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
                         const int width, const int height, int dir)
 {
     rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,
-            r->s.dsp.put_h264_qpel_pixels_tab, r->s.dsp.put_h264_chroma_pixels_tab);
+            r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
+                    : r->s.dsp.put_h264_qpel_pixels_tab,
+            r->s.dsp.put_h264_chroma_pixels_tab);
 }
 
 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
 {
     rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
-            r->s.dsp.put_h264_qpel_pixels_tab, r->s.dsp.put_h264_chroma_pixels_tab);
+            r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
+                    : r->s.dsp.put_h264_qpel_pixels_tab,
+            r->s.dsp.put_h264_chroma_pixels_tab);
     rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
-            r->s.dsp.avg_h264_qpel_pixels_tab, r->s.dsp.avg_h264_chroma_pixels_tab);
+            r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
+                    : r->s.dsp.avg_h264_qpel_pixels_tab,
+            r->s.dsp.avg_h264_chroma_pixels_tab);
 }
 
 /** number of motion vectors in each macroblock type */
@@ -678,7 +691,7 @@ static int rv34_decode_mv(RV34DecContext *r, int block_type)
         fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
         return 0;
     case RV34_MB_SKIP:
-        if(s->pict_type == P_TYPE){
+        if(s->pict_type == FF_P_TYPE){
             fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
             rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
             break;
@@ -766,6 +779,7 @@ static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int
     if(!down){
         if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
         if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
+        if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
     }
     if(!right && up){
         topleft = dst[-stride + 3] * 0x01010101;
@@ -900,9 +914,9 @@ static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
         s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
         r->mb_type[mb_pos] = r->block_type;
         if(r->block_type == RV34_MB_SKIP){
-            if(s->pict_type == P_TYPE)
+            if(s->pict_type == FF_P_TYPE)
                 r->mb_type[mb_pos] = RV34_MB_P_16x16;
-            if(s->pict_type == B_TYPE)
+            if(s->pict_type == FF_B_TYPE)
                 r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
         }
         r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
@@ -998,6 +1012,9 @@ static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types)
 
     s->qscale = r->si.quant;
     cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
+    r->cbp_luma  [s->mb_x + s->mb_y * s->mb_stride] = cbp;
+    r->cbp_chroma[s->mb_x + s->mb_y * s->mb_stride] = cbp >> 16;
+    s->current_picture.qscale_table[s->mb_x + s->mb_y * s->mb_stride] = s->qscale;
 
     if(cbp == -1)
         return -1;
@@ -1087,8 +1104,10 @@ static int rv34_decode_slice(RV34DecContext *r, int end, uint8_t* buf, int buf_s
             r->intra_types_hist = av_realloc(r->intra_types_hist, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
             r->intra_types = r->intra_types_hist + s->b4_stride * 4;
             r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
+            r->cbp_luma   = av_realloc(r->cbp_luma,   r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
+            r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
         }
-        s->pict_type = r->si.type ? r->si.type : I_TYPE;
+        s->pict_type = r->si.type ? r->si.type : FF_I_TYPE;
         if(MPV_frame_start(s, s->avctx) < 0)
             return -1;
         ff_er_frame_start(s);
@@ -1144,7 +1163,7 @@ static int rv34_decode_slice(RV34DecContext *r, int end, uint8_t* buf, int buf_s
 /**
  * Initialize decoder.
  */
-int ff_rv34_decode_init(AVCodecContext *avctx)
+av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
 {
     RV34DecContext *r = avctx->priv_data;
     MpegEncContext *s = &r->s;
@@ -1174,6 +1193,9 @@ int ff_rv34_decode_init(AVCodecContext *avctx)
 
     r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
 
+    r->cbp_luma   = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
+    r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
+
     if(!intra_vlcs[0].cbppattern[0].bits)
         rv34_init_tables();
 
@@ -1248,7 +1270,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx,
             r->loop_filter(r);
         ff_er_frame_end(s);
         MPV_frame_end(s);
-        if (s->pict_type == B_TYPE || s->low_delay) {
+        if (s->pict_type == FF_B_TYPE || s->low_delay) {
             *pict= *(AVFrame*)s->current_picture_ptr;
         } else if (s->last_picture_ptr != NULL) {
             *pict= *(AVFrame*)s->last_picture_ptr;
@@ -1263,7 +1285,7 @@ int ff_rv34_decode_frame(AVCodecContext *avctx,
     return buf_size;
 }
 
-int ff_rv34_decode_end(AVCodecContext *avctx)
+av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
 {
     RV34DecContext *r = avctx->priv_data;