-
Notifications
You must be signed in to change notification settings - Fork 0
/
me.c
156 lines (128 loc) · 3.7 KB
/
me.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dsp.h"
#include "me.h"
/* Motion estimation for 8x8 block */
static void me_block_8x8(struct c63_common *cm, int mb_x, int mb_y,
uint8_t *orig, uint8_t *ref, int color_component)
{
struct macroblock *mb =
&cm->curframe->mbs[color_component][mb_y*cm->padw[color_component]/8+mb_x];
int range = cm->me_search_range;
/* Quarter resolution for chroma channels. */
if (color_component > 0) { range /= 2; }
int left = mb_x * 8 - range;
int top = mb_y * 8 - range;
int right = mb_x * 8 + range;
int bottom = mb_y * 8 + range;
int w = cm->padw[color_component];
int h = cm->padh[color_component];
/* Make sure we are within bounds of reference frame. TODO: Support partial
frame bounds. */
if (left < 0) { left = 0; }
if (top < 0) { top = 0; }
if (right > (w - 8)) { right = w - 8; }
if (bottom > (h - 8)) { bottom = h - 8; }
int x, y;
int mx = mb_x * 8;
int my = mb_y * 8;
int best_sad = INT_MAX;
for (y = top; y < bottom; ++y)
{
for (x = left; x < right; ++x)
{
int sad;
sad_block_8x8(orig + my*w+mx, ref + y*w+x, w, &sad);
/* printf("(%4d,%4d) - %d\n", x, y, sad); */
if (sad < best_sad)
{
mb->mv_x = x - mx;
mb->mv_y = y - my;
best_sad = sad;
}
}
}
/* Here, there should be a threshold on SAD that checks if the motion vector
is cheaper than intraprediction. We always assume MV to be beneficial */
/* printf("Using motion vector (%d, %d) with SAD %d\n", mb->mv_x, mb->mv_y,
best_sad); */
mb->use_mv = 1;
}
void c63_motion_estimate(struct c63_common *cm)
{
/* Compare this frame with previous reconstructed frame */
int mb_x, mb_y;
/* Luma */
for (mb_y = 0; mb_y < cm->mb_rows; ++mb_y)
{
for (mb_x = 0; mb_x < cm->mb_cols; ++mb_x)
{
me_block_8x8(cm, mb_x, mb_y, cm->curframe->orig->Y,
cm->refframe->recons->Y, Y_COMPONENT);
}
}
/* Chroma */
for (mb_y = 0; mb_y < cm->mb_rows / 2; ++mb_y)
{
for (mb_x = 0; mb_x < cm->mb_cols / 2; ++mb_x)
{
me_block_8x8(cm, mb_x, mb_y, cm->curframe->orig->U,
cm->refframe->recons->U, U_COMPONENT);
me_block_8x8(cm, mb_x, mb_y, cm->curframe->orig->V,
cm->refframe->recons->V, V_COMPONENT);
}
}
}
/* Motion compensation for 8x8 block */
static void mc_block_8x8(struct c63_common *cm, int mb_x, int mb_y,
uint8_t *predicted, uint8_t *ref, int color_component)
{
struct macroblock *mb =
&cm->curframe->mbs[color_component][mb_y*cm->padw[color_component]/8+mb_x];
if (!mb->use_mv) { return; }
int left = mb_x * 8;
int top = mb_y * 8;
int right = left + 8;
int bottom = top + 8;
int w = cm->padw[color_component];
/* Copy block from ref mandated by MV */
int x, y;
for (y = top; y < bottom; ++y)
{
for (x = left; x < right; ++x)
{
predicted[y*w+x] = ref[(y + mb->mv_y) * w + (x + mb->mv_x)];
}
}
}
void c63_motion_compensate(struct c63_common *cm)
{
int mb_x, mb_y;
/* Luma */
for (mb_y = 0; mb_y < cm->mb_rows; ++mb_y)
{
for (mb_x = 0; mb_x < cm->mb_cols; ++mb_x)
{
mc_block_8x8(cm, mb_x, mb_y, cm->curframe->predicted->Y,
cm->refframe->recons->Y, Y_COMPONENT);
}
}
/* Chroma */
for (mb_y = 0; mb_y < cm->mb_rows / 2; ++mb_y)
{
for (mb_x = 0; mb_x < cm->mb_cols / 2; ++mb_x)
{
mc_block_8x8(cm, mb_x, mb_y, cm->curframe->predicted->U,
cm->refframe->recons->U, U_COMPONENT);
mc_block_8x8(cm, mb_x, mb_y, cm->curframe->predicted->V,
cm->refframe->recons->V, V_COMPONENT);
}
}
}