-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodec.c
executable file
·172 lines (145 loc) · 3.46 KB
/
codec.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
#include "misc.h"
#include "codec.h"
#include "cif.h"
int debugm = 0;
int debug, trace_mv = 0;
int annex = 0;
extern int Decode;
extern PICTURE *pic;
int frame_num = 0;
int force_intra;
int use_gob;
int select_picture_type(CONTEXT context);
int encode_image(PICTURE *pic);
int decode_image(PICTURE *pic);
int codec_encode_image(PICTURE *pic,IMAGE *image)
{
if (!image) return -1;
trace("codec_encode_image(%p)\n", image);
printf("frame %d\n",pic->tr);
memset(&pic->stat, 0, sizeof(pic->stat));
force_intra = load_picture_image(pic, image);
pic->pquant = pic->image.codec_context.op_quant;
pic->ptype = select_picture_type(pic->image.codec_context);
ms_new_frame(pic);
write_h263_picture(pic);
encode_image(pic);
trace("framestat %d\n", frame_num);
trace_stat(&pic->stat);
decode_image(pic);
pic = pic->prev;
trace("\n");
frame_num++;
return 0;
}
int codec_decode_picture(PICTURE *pic)
{
if (!pic) return -1;
trace("codec_decode_picture(%p)\n", pic);
memset(&pic->stat, 0, sizeof(pic->stat));
decode_image(pic);
if (Decode)
yuv_write_image(pic->image.codec_context, frame_num, &(pic->image));
trace("\n");
frame_num++;
return 0;
}
int encode_image(PICTURE *pic)
{
int i, j;
MACROBLOCK *mb;
int statcnt;
int framesize;
framesize = bitcount();
trace("frame %d: encode_image(%p)\n", frame_num, pic);
pic->tr = pic->prev->tr + 1;
for (i=0; i<pic->nmy; i++)
for (j=0; j<pic->nmx; j++) {
trace("\nMB %d %d\n", i, j);
mb = &pic->mb[i][j];
load_mb_data(mb, 0, 0);
switch (pic->ptype) {
case PCT_INTRA:
frame_I_encode_MB(mb);
break;
case PCT_INTER:
frame_P_encode_MB(mb);
break;
}
write_h263_mb(mb);
}
alignbits();
framesize = bitcount()-framesize;
pic->stat.framesize = framesize;
return 0;
}
int decode_image(PICTURE *pic)
{
int i, j;
MACROBLOCK *mb;
pic->tr = pic->prev->tr + 1;
trace("decode_image(%p)\n", pic);
for (i=0; i<pic->nmy; i++)
for (j=0; j<pic->nmx; j++) {
mb = &pic->mb[i][j];
if (Decode) {
trace("MACROBLOCK[%d][%d]\n", i, j);
read_h263_mb(mb);
}
switch (pic->ptype) {
case PCT_INTRA:
frame_I_decode_MB(mb);
break;
case PCT_INTER:
frame_P_decode_MB(mb);
break;
}
write_mb_data(mb);
}
if (trace_mv && pic->ptype == PCT_INTER)
trace_pic_mv(pic);
return 0;
}
int select_picture_type(CONTEXT context)
{
int intracycle = 30;
if (force_intra || !(frame_num % context.insert_intra_cycle)) {
force_intra = 0;
return PCT_INTRA;
} else {
return PCT_INTER;
}
}
extern FILE *_t_stream;
extern int debug;
void codec_init(CONTEXT context)
{
_t_stream = context.t_stream;
debug = context.debug;
trace("codec_init()\n\n");
pic = malloc(sizeof(PICTURE));
memset(pic,0,sizeof(*pic));
pic->prev = malloc(sizeof(PICTURE));
memset(pic->prev,0,sizeof(*(pic->prev)));
pic->prev->prev = pic;
pic->image.codec_context.debug = 5;
pic->image.codec_context.op_quant = 1;
pic->image.codec_context.motest_alg = MOTEST_CBOSA;
memcpy(&pic->image.codec_context, &context, sizeof(context));
memcpy(&pic->prev->image.codec_context, &context, sizeof(context));
initbits(context);
initgetbits(context);
}
int codec_finit()
{
if (!Decode) {
trace("EOS:\t"); putbits(22, 0x00003f);
alignbits();
}
trace("codec_finit()\n\n");
finit_picture(pic->prev);
free(pic->prev);
finit_picture(pic);
free(pic);
return 0;
}