summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2010-01-12 21:36:26 +0000
committerMichael Niedermayer <michaelni@gmx.at>2010-01-12 21:36:26 +0000
commit188d3c510dd166656223afc5fbcf6c424a41e0ca (patch)
tree60b22d5626fce73462f9eff2384a2f1d8247e5fe
parent2bedc0e85499c6d05c230626e22fa9d0c2e511e6 (diff)
Split motion vector prediction off h264.c/h.
Originally committed as revision 21174 to svn://svn.ffmpeg.org/ffmpeg/trunk
-rw-r--r--libavcodec/h264.c92
-rw-r--r--libavcodec/h264.h114
-rw-r--r--libavcodec/h264_direct.c1
-rw-r--r--libavcodec/h264_mvpred.h237
-rw-r--r--libavcodec/svq3.c1
5 files changed, 240 insertions, 205 deletions
diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 9883d65ef6..f768450b2e 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -31,6 +31,7 @@
#include "mpegvideo.h"
#include "h264.h"
#include "h264data.h"
+#include "h264_mvpred.h"
#include "h264_parser.h"
#include "golomb.h"
#include "mathops.h"
@@ -667,97 +668,6 @@ static inline int pred_non_zero_count(H264Context *h, int n){
return i&31;
}
-/**
- * gets the directionally predicted 16x8 MV.
- * @param n the block index
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
-static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
- if(n==0){
- const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
- const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
-
- tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
-
- if(top_ref == ref){
- *mx= B[0];
- *my= B[1];
- return;
- }
- }else{
- const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
- const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
-
- tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
-
- if(left_ref == ref){
- *mx= A[0];
- *my= A[1];
- return;
- }
- }
-
- //RARE
- pred_motion(h, n, 4, list, ref, mx, my);
-}
-
-/**
- * gets the directionally predicted 8x16 MV.
- * @param n the block index
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
-static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
- if(n==0){
- const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
- const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
-
- tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
-
- if(left_ref == ref){
- *mx= A[0];
- *my= A[1];
- return;
- }
- }else{
- const int16_t * C;
- int diagonal_ref;
-
- diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
-
- tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
-
- if(diagonal_ref == ref){
- *mx= C[0];
- *my= C[1];
- return;
- }
- }
-
- //RARE
- pred_motion(h, n, 2, list, ref, mx, my);
-}
-
-static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
- const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
- const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
-
- tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
-
- if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
- || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
- || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
-
- *mx = *my = 0;
- return;
- }
-
- pred_motion(h, 0, 4, 0, 0, mx, my);
-
- return;
-}
-
static inline void write_back_motion(H264Context *h, int mb_type){
MpegEncContext * const s = &h->s;
const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index 883012159d..1c4c956056 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -694,118 +694,4 @@ static inline int get_chroma_qp(H264Context *h, int t, int qscale){
return h->pps.chroma_qp_table[t][qscale];
}
-static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
- const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
- MpegEncContext *s = &h->s;
-
- /* there is no consistent mapping of mvs to neighboring locations that will
- * make mbaff happy, so we can't move all this logic to fill_caches */
- if(FRAME_MBAFF){
- const uint32_t *mb_types = s->current_picture_ptr->mb_type;
- const int16_t *mv;
- *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
- *C = h->mv_cache[list][scan8[0]-2];
-
- if(!MB_FIELD
- && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
- int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
- if(IS_INTERLACED(mb_types[topright_xy])){
-#define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
- const int x4 = X4, y4 = Y4;\
- const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
- if(!USES_LIST(mb_type,list))\
- return LIST_NOT_USED;\
- mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
- h->mv_cache[list][scan8[0]-2][0] = mv[0];\
- h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
- return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
-
- SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
- }
- }
- if(topright_ref == PART_NOT_AVAILABLE
- && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
- && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
- if(!MB_FIELD
- && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
- SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
- }
- if(MB_FIELD
- && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
- && i >= scan8[0]+8){
- // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
- SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
- }
- }
-#undef SET_DIAG_MV
- }
-
- if(topright_ref != PART_NOT_AVAILABLE){
- *C= h->mv_cache[list][ i - 8 + part_width ];
- return topright_ref;
- }else{
- tprintf(s->avctx, "topright MV not available\n");
-
- *C= h->mv_cache[list][ i - 8 - 1 ];
- return h->ref_cache[list][ i - 8 - 1 ];
- }
-}
-
-/**
- * gets the predicted MV.
- * @param n the block index
- * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
- * @param mx the x component of the predicted motion vector
- * @param my the y component of the predicted motion vector
- */
-static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
- const int index8= scan8[n];
- const int top_ref= h->ref_cache[list][ index8 - 8 ];
- const int left_ref= h->ref_cache[list][ index8 - 1 ];
- const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
- const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
- const int16_t * C;
- int diagonal_ref, match_count;
-
- assert(part_width==1 || part_width==2 || part_width==4);
-
-/* mv_cache
- B . . A T T T T
- U . . L . . , .
- U . . L . . . .
- U . . L . . , .
- . . . L . . . .
-*/
-
- diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
- match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
- tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
- if(match_count > 1){ //most common
- *mx= mid_pred(A[0], B[0], C[0]);
- *my= mid_pred(A[1], B[1], C[1]);
- }else if(match_count==1){
- if(left_ref==ref){
- *mx= A[0];
- *my= A[1];
- }else if(top_ref==ref){
- *mx= B[0];
- *my= B[1];
- }else{
- *mx= C[0];
- *my= C[1];
- }
- }else{
- if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
- *mx= A[0];
- *my= A[1];
- }else{
- *mx= mid_pred(A[0], B[0], C[0]);
- *my= mid_pred(A[1], B[1], C[1]);
- }
- }
-
- tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
-}
-
-
#endif /* AVCODEC_H264_H */
diff --git a/libavcodec/h264_direct.c b/libavcodec/h264_direct.c
index 323c4cf263..6b656b1f79 100644
--- a/libavcodec/h264_direct.c
+++ b/libavcodec/h264_direct.c
@@ -30,6 +30,7 @@
#include "avcodec.h"
#include "mpegvideo.h"
#include "h264.h"
+#include "h264_mvpred.h"
#include "rectangle.h"
#if ARCH_X86
diff --git a/libavcodec/h264_mvpred.h b/libavcodec/h264_mvpred.h
new file mode 100644
index 0000000000..f6c503a976
--- /dev/null
+++ b/libavcodec/h264_mvpred.h
@@ -0,0 +1,237 @@
+/*
+ * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
+ * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file libavcodec/h264_mvpred.h
+ * H.264 / AVC / MPEG4 part10 motion vector predicion.
+ * @author Michael Niedermayer <michaelni@gmx.at>
+ */
+
+#include "internal.h"
+#include "avcodec.h"
+#include "h264.h"
+
+//#undef NDEBUG
+#include <assert.h>
+
+static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
+ const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
+ MpegEncContext *s = &h->s;
+
+ /* there is no consistent mapping of mvs to neighboring locations that will
+ * make mbaff happy, so we can't move all this logic to fill_caches */
+ if(FRAME_MBAFF){
+ const uint32_t *mb_types = s->current_picture_ptr->mb_type;
+ const int16_t *mv;
+ *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
+ *C = h->mv_cache[list][scan8[0]-2];
+
+ if(!MB_FIELD
+ && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
+ int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
+ if(IS_INTERLACED(mb_types[topright_xy])){
+#define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
+ const int x4 = X4, y4 = Y4;\
+ const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
+ if(!USES_LIST(mb_type,list))\
+ return LIST_NOT_USED;\
+ mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
+ h->mv_cache[list][scan8[0]-2][0] = mv[0];\
+ h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
+ return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
+
+ SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
+ }
+ }
+ if(topright_ref == PART_NOT_AVAILABLE
+ && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
+ && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
+ if(!MB_FIELD
+ && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
+ SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
+ }
+ if(MB_FIELD
+ && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
+ && i >= scan8[0]+8){
+ // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
+ SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
+ }
+ }
+#undef SET_DIAG_MV
+ }
+
+ if(topright_ref != PART_NOT_AVAILABLE){
+ *C= h->mv_cache[list][ i - 8 + part_width ];
+ return topright_ref;
+ }else{
+ tprintf(s->avctx, "topright MV not available\n");
+
+ *C= h->mv_cache[list][ i - 8 - 1 ];
+ return h->ref_cache[list][ i - 8 - 1 ];
+ }
+}
+
+/**
+ * gets the predicted MV.
+ * @param n the block index
+ * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
+ * @param mx the x component of the predicted motion vector
+ * @param my the y component of the predicted motion vector
+ */
+static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
+ const int index8= scan8[n];
+ const int top_ref= h->ref_cache[list][ index8 - 8 ];
+ const int left_ref= h->ref_cache[list][ index8 - 1 ];
+ const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
+ const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
+ const int16_t * C;
+ int diagonal_ref, match_count;
+
+ assert(part_width==1 || part_width==2 || part_width==4);
+
+/* mv_cache
+ B . . A T T T T
+ U . . L . . , .
+ U . . L . . . .
+ U . . L . . , .
+ . . . L . . . .
+*/
+
+ diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
+ match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
+ tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
+ if(match_count > 1){ //most common
+ *mx= mid_pred(A[0], B[0], C[0]);
+ *my= mid_pred(A[1], B[1], C[1]);
+ }else if(match_count==1){
+ if(left_ref==ref){
+ *mx= A[0];
+ *my= A[1];
+ }else if(top_ref==ref){
+ *mx= B[0];
+ *my= B[1];
+ }else{
+ *mx= C[0];
+ *my= C[1];
+ }
+ }else{
+ if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
+ *mx= A[0];
+ *my= A[1];
+ }else{
+ *mx= mid_pred(A[0], B[0], C[0]);
+ *my= mid_pred(A[1], B[1], C[1]);
+ }
+ }
+
+ tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
+}
+
+/**
+ * gets the directionally predicted 16x8 MV.
+ * @param n the block index
+ * @param mx the x component of the predicted motion vector
+ * @param my the y component of the predicted motion vector
+ */
+static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
+ if(n==0){
+ const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
+ const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
+
+ tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
+
+ if(top_ref == ref){
+ *mx= B[0];
+ *my= B[1];
+ return;
+ }
+ }else{
+ const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
+ const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
+
+ tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
+
+ if(left_ref == ref){
+ *mx= A[0];
+ *my= A[1];
+ return;
+ }
+ }
+
+ //RARE
+ pred_motion(h, n, 4, list, ref, mx, my);
+}
+
+/**
+ * gets the directionally predicted 8x16 MV.
+ * @param n the block index
+ * @param mx the x component of the predicted motion vector
+ * @param my the y component of the predicted motion vector
+ */
+static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
+ if(n==0){
+ const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
+ const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
+
+ tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
+
+ if(left_ref == ref){
+ *mx= A[0];
+ *my= A[1];
+ return;
+ }
+ }else{
+ const int16_t * C;
+ int diagonal_ref;
+
+ diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
+
+ tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
+
+ if(diagonal_ref == ref){
+ *mx= C[0];
+ *my= C[1];
+ return;
+ }
+ }
+
+ //RARE
+ pred_motion(h, n, 2, list, ref, mx, my);
+}
+
+static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
+ const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
+ const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
+
+ tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
+
+ if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
+ || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
+ || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
+
+ *mx = *my = 0;
+ return;
+ }
+
+ pred_motion(h, 0, 4, 0, 0, mx, my);
+
+ return;
+}
diff --git a/libavcodec/svq3.c b/libavcodec/svq3.c
index af9c40d517..87473e5fdc 100644
--- a/libavcodec/svq3.c
+++ b/libavcodec/svq3.c
@@ -47,6 +47,7 @@
#include "h264data.h" //FIXME FIXME FIXME
+#include "h264_mvpred.h"
#include "golomb.h"
#include "rectangle.h"
#include "vdpau_internal.h"