-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.cpp
142 lines (112 loc) · 3.88 KB
/
init.cpp
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
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "init.h"
extern "C" {
#include "dsp.h"
#include "tables.h"
}
static const int Y = Y_COMPONENT;
static const int U = U_COMPONENT;
static const int V = V_COMPONENT;
yuv_t* create_image(struct c63_common* cm)
{
yuv_t* image = (yuv_t*) malloc(sizeof(yuv_t));
image->Y = (uint8_t*) malloc(cm->padw[Y] * cm->padh[Y] * sizeof(uint8_t));
image->U = (uint8_t*) malloc(cm->padw[U] * cm->padh[U] * sizeof(uint8_t));
image->V = (uint8_t*) malloc(cm->padw[V] * cm->padh[V] * sizeof(uint8_t));
return image;
}
void destroy_image(yuv_t* image)
{
free(image->Y);
free(image->U);
free(image->V);
free(image);
}
static struct frame* create_frame(struct c63_common *cm)
{
struct frame *f = (struct frame*) malloc(sizeof(struct frame));
f->recons = create_image(cm);
f->predicted = create_image(cm);
f->residuals = (dct_t*) malloc(sizeof(dct_t));
f->residuals->Ydct = (int16_t*) malloc(cm->ypw * cm->yph * sizeof(int16_t));
f->residuals->Udct = (int16_t*) malloc(cm->upw * cm->uph * sizeof(int16_t));
f->residuals->Vdct = (int16_t*) malloc(cm->vpw * cm->vph * sizeof(int16_t));
f->mbs[Y] = (struct macroblock*) calloc(cm->mb_rows[Y] * cm->mb_cols[Y],
sizeof(struct macroblock));
f->mbs[U] = (struct macroblock*) calloc(cm->mb_rows[U] * cm->mb_cols[U],
sizeof(struct macroblock));
f->mbs[V] = (struct macroblock*) calloc(cm->mb_rows[V] * cm->mb_cols[V],
sizeof(struct macroblock));
return f;
}
static void destroy_frame(struct frame *f)
{
destroy_image(f->recons);
destroy_image(f->predicted);
free(f->residuals->Ydct);
free(f->residuals->Udct);
free(f->residuals->Vdct);
free(f->residuals);
free(f->mbs[Y]);
free(f->mbs[U]);
free(f->mbs[V]);
free(f);
}
void dump_image(yuv_t *image, int w, int h, FILE *fp)
{
fwrite(image->Y, 1, w * h, fp);
fwrite(image->U, 1, w * h / 4, fp);
fwrite(image->V, 1, w * h / 4, fp);
}
struct c63_common* init_c63_common(int width, int height)
{
int i;
/* calloc() sets allocated memory to zero */
struct c63_common *cm = (struct c63_common*) calloc(1, sizeof(struct c63_common));
cm->width = width;
cm->height = height;
cm->padw[Y] = cm->ypw = (uint32_t) (ceil(width / 16.0f) * 16);
cm->padh[Y] = cm->yph = (uint32_t) (ceil(height / 16.0f) * 16);
cm->padw[U] = cm->upw = (uint32_t) (ceil(width * UX / (YX * 8.0f)) * 8);
cm->padh[U] = cm->uph = (uint32_t) (ceil(height * UY / (YY * 8.0f)) * 8);
cm->padw[V] = cm->vpw = (uint32_t) (ceil(width * VX / (YX * 8.0f)) * 8);
cm->padh[V] = cm->vph = (uint32_t) (ceil(height * VY / (YY * 8.0f)) * 8);
cm->mb_cols[Y] = cm->ypw / 8;
cm->mb_cols[U] = cm->mb_cols[Y] / 2;
cm->mb_cols[V] = cm->mb_cols[U];
cm->mb_rows[Y] = cm->yph / 8;
cm->mb_rows[U] = cm->mb_rows[Y] / 2;
cm->mb_rows[V] = cm->mb_rows[U];
/* Quality parameters -- Home exam deliveries should have original values,
i.e., quantization factor should be 25, search range should be 16, and the
keyframe interval should be 100. */
cm->qp = 25; // Constant quantization factor. Range: [1..50]
//cm->me_search_range = 16; // This is now defined in c63.h
cm->keyframe_interval = 100; // Distance between keyframes
/* Initialize quantization tables */
for (i = 0; i < 64; ++i)
{
cm->quanttbl[Y][i] = yquanttbl_def[i] / (cm->qp / 10.0);
cm->quanttbl[U][i] = uvquanttbl_def[i] / (cm->qp / 10.0);
cm->quanttbl[V][i] = uvquanttbl_def[i] / (cm->qp / 10.0);
cm->quanttbl_fp[Y][i] = (uint8_t) (yquanttbl_def[i] / (cm->qp / 10.0));
cm->quanttbl_fp[U][i] = (uint8_t) (uvquanttbl_def[i] / (cm->qp / 10.0));
cm->quanttbl_fp[V][i] = (uint8_t) (uvquanttbl_def[i] / (cm->qp / 10.0));
}
cm->curframe = create_frame(cm);
cm->refframe = create_frame(cm);
return cm;
}
void cleanup_c63_common(struct c63_common* cm)
{
destroy_frame(cm->curframe);
destroy_frame(cm->refframe);
free(cm);
}