-
Notifications
You must be signed in to change notification settings - Fork 0
/
c63enc.c
291 lines (231 loc) · 7.27 KB
/
c63enc.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#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 "c63.h"
#include "c63_write.h"
#include "common.h"
#include "me.h"
#include "tables.h"
static char *output_file, *input_file;
FILE *outfile;
static int limit_numframes = 0;
static uint32_t width;
static uint32_t height;
/* getopt */
extern int optind;
extern char *optarg;
/* Read planar YUV frames with 4:2:0 chroma sub-sampling */
static yuv_t* read_yuv(FILE *file, struct c63_common *cm)
{
size_t len = 0;
yuv_t *image = malloc(sizeof(*image));
/* Read Y. The size of Y is the same as the size of the image. The indices
represents the color component (0 is Y, 1 is U, and 2 is V) */
image->Y = calloc(1, cm->padw[Y_COMPONENT]*cm->padh[Y_COMPONENT]);
len += fread(image->Y, 1, width*height, file);
/* Read U. Given 4:2:0 chroma sub-sampling, the size is 1/4 of Y
because (height/2)*(width/2) = (height*width)/4. */
image->U = calloc(1, cm->padw[U_COMPONENT]*cm->padh[U_COMPONENT]);
len += fread(image->U, 1, (width*height)/4, file);
/* Read V. Given 4:2:0 chroma sub-sampling, the size is 1/4 of Y. */
image->V = calloc(1, cm->padw[V_COMPONENT]*cm->padh[V_COMPONENT]);
len += fread(image->V, 1, (width*height)/4, file);
if (ferror(file))
{
perror("ferror");
exit(EXIT_FAILURE);
}
if (feof(file))
{
free(image->Y);
free(image->U);
free(image->V);
free(image);
return NULL;
}
else if (len != width*height*1.5)
{
fprintf(stderr, "Reached end of file, but incorrect bytes read.\n");
fprintf(stderr, "Wrong input? (height: %d width: %d)\n", height, width);
free(image->Y);
free(image->U);
free(image->V);
free(image);
return NULL;
}
return image;
}
static void c63_encode_image(struct c63_common *cm, yuv_t *image)
{
/* Advance to next frame */
destroy_frame(cm->refframe);
cm->refframe = cm->curframe;
cm->curframe = create_frame(cm, image);
/* Check if keyframe */
if (cm->framenum == 0 || cm->frames_since_keyframe == cm->keyframe_interval)
{
cm->curframe->keyframe = 1;
cm->frames_since_keyframe = 0;
fprintf(stderr, " (keyframe) ");
}
else { cm->curframe->keyframe = 0; }
if (!cm->curframe->keyframe)
{
/* Motion Estimation */
c63_motion_estimate(cm);
/* Motion Compensation */
c63_motion_compensate(cm);
}
/* DCT and Quantization */
dct_quantize(image->Y, cm->curframe->predicted->Y, cm->padw[Y_COMPONENT],
cm->padh[Y_COMPONENT], cm->curframe->residuals->Ydct,
cm->quanttbl[Y_COMPONENT]);
dct_quantize(image->U, cm->curframe->predicted->U, cm->padw[U_COMPONENT],
cm->padh[U_COMPONENT], cm->curframe->residuals->Udct,
cm->quanttbl[U_COMPONENT]);
dct_quantize(image->V, cm->curframe->predicted->V, cm->padw[V_COMPONENT],
cm->padh[V_COMPONENT], cm->curframe->residuals->Vdct,
cm->quanttbl[V_COMPONENT]);
/* Reconstruct frame for inter-prediction */
dequantize_idct(cm->curframe->residuals->Ydct, cm->curframe->predicted->Y,
cm->ypw, cm->yph, cm->curframe->recons->Y, cm->quanttbl[Y_COMPONENT]);
dequantize_idct(cm->curframe->residuals->Udct, cm->curframe->predicted->U,
cm->upw, cm->uph, cm->curframe->recons->U, cm->quanttbl[U_COMPONENT]);
dequantize_idct(cm->curframe->residuals->Vdct, cm->curframe->predicted->V,
cm->vpw, cm->vph, cm->curframe->recons->V, cm->quanttbl[V_COMPONENT]);
/* Function dump_image(), found in common.c, can be used here to check if the
prediction is correct */
write_frame(cm);
++cm->framenum;
++cm->frames_since_keyframe;
}
struct c63_common* init_c63_enc(int width, int height)
{
int i;
/* calloc() sets allocated memory to zero */
struct c63_common *cm = calloc(1, sizeof(struct c63_common));
cm->width = width;
cm->height = height;
cm->padw[Y_COMPONENT] = cm->ypw = (uint32_t)(ceil(width/16.0f)*16);
cm->padh[Y_COMPONENT] = cm->yph = (uint32_t)(ceil(height/16.0f)*16);
cm->padw[U_COMPONENT] = cm->upw = (uint32_t)(ceil(width*UX/(YX*8.0f))*8);
cm->padh[U_COMPONENT] = cm->uph = (uint32_t)(ceil(height*UY/(YY*8.0f))*8);
cm->padw[V_COMPONENT] = cm->vpw = (uint32_t)(ceil(width*VX/(YX*8.0f))*8);
cm->padh[V_COMPONENT] = cm->vph = (uint32_t)(ceil(height*VY/(YY*8.0f))*8);
cm->mb_cols = cm->ypw / 8;
cm->mb_rows = cm->yph / 8;
/* 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; // Pixels in every direction
cm->keyframe_interval = 100; // Distance between keyframes
/* Initialize quantization tables */
for (i = 0; i < 64; ++i)
{
cm->quanttbl[Y_COMPONENT][i] = yquanttbl_def[i] / (cm->qp / 10.0);
cm->quanttbl[U_COMPONENT][i] = uvquanttbl_def[i] / (cm->qp / 10.0);
cm->quanttbl[V_COMPONENT][i] = uvquanttbl_def[i] / (cm->qp / 10.0);
}
return cm;
}
void free_c63_enc(struct c63_common* cm)
{
destroy_frame(cm->curframe);
free(cm);
}
static void print_help()
{
printf("Usage: ./c63enc [options] input_file\n");
printf("Commandline options:\n");
printf(" -h Height of images to compress\n");
printf(" -w Width of images to compress\n");
printf(" -o Output file (.c63)\n");
printf(" [-f] Limit number of frames to encode\n");
printf("\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int c;
yuv_t *image;
if (argc == 1) { print_help(); }
while ((c = getopt(argc, argv, "h:w:o:f:i:")) != -1)
{
switch (c)
{
case 'h':
height = atoi(optarg);
break;
case 'w':
width = atoi(optarg);
break;
case 'o':
output_file = optarg;
break;
case 'f':
limit_numframes = atoi(optarg);
break;
default:
print_help();
break;
}
}
if (optind >= argc)
{
fprintf(stderr, "Error getting program options, try --help.\n");
exit(EXIT_FAILURE);
}
outfile = fopen(output_file, "wb");
if (outfile == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
struct c63_common *cm = init_c63_enc(width, height);
cm->e_ctx.fp = outfile;
input_file = argv[optind];
if (limit_numframes) { printf("Limited to %d frames.\n", limit_numframes); }
FILE *infile = fopen(input_file, "rb");
if (infile == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
/* Encode input frames */
int numframes = 0;
while (1)
{
image = read_yuv(infile, cm);
if (!image) { break; }
printf("Encoding frame %d, ", numframes);
c63_encode_image(cm, image);
free(image->Y);
free(image->U);
free(image->V);
free(image);
printf("Done!\n");
++numframes;
if (limit_numframes && numframes >= limit_numframes) { break; }
}
free_c63_enc(cm);
fclose(outfile);
fclose(infile);
//int i, j;
//for (i = 0; i < 2; ++i)
//{
// printf("int freq[] = {");
// for (j = 0; j < ARRAY_SIZE(frequencies[i]); ++j)
// {
// printf("%d, ", frequencies[i][j]);
// }
// printf("};\n");
//}
return EXIT_SUCCESS;
}