Parent Directory | Revision Log
Revision 1.7 - (view) (download)
1 : | chenm001 | 1.1 | /******************************************************************************* |
2 : | * This file is a example for how to use the xvid core to compress YUV file | ||
3 : | * | ||
4 : | chenm001 | 1.7 | * 0.02 11.07.2002 chenm001<chenm001@163.com> |
5 : | * add the decode examples | ||
6 : | chenm001 | 1.3 | * 0.01b 28.05.2002 chenm001<chenm001@163.com> |
7 : | * fix a little bug for encode only codec I frame | ||
8 : | chenm001 | 1.2 | * 0.01a 27.05.2002 chenm001<chenm001@163.com> |
9 : | * fix a little bug for BFRAMES define locate | ||
10 : | chenm001 | 1.1 | * 0.01 23.05.2002 chenm001<chenm001@163.com> |
11 : | chenm001 | 1.7 | * Initialize version only support encode examples |
12 : | chenm001 | 1.1 | * the BFRAME option must be match to core compile option |
13 : | *******************************************************************************/ | ||
14 : | |||
15 : | chenm001 | 1.7 | #define BFRAMES |
16 : | #define BFRAMES_DEC | ||
17 : | chenm001 | 1.1 | #include "ex1.h" |
18 : | |||
19 : | int Encode(char *, int, int, char *); | ||
20 : | chenm001 | 1.7 | int Decode(char *, char *); |
21 : | chenm001 | 1.1 | |
22 : | int main(int argc, char *argv[]) | ||
23 : | { | ||
24 : | chenm001 | 1.7 | if (argc<4){ |
25 : | printf("Usage:\n\t%s Encode infile width heigh outfile\n",argv[0]); | ||
26 : | printf("\t%s Decode infile outfile\n",argv[0]); | ||
27 : | chenm001 | 1.1 | return(ERR_NO_OPT); |
28 : | } | ||
29 : | switch(toupper(argv[1][0])){ | ||
30 : | case 'E': | ||
31 : | Encode(argv[2], atoi(argv[3]), atoi(argv[4]), argv[5]); | ||
32 : | break; | ||
33 : | case 'D': | ||
34 : | chenm001 | 1.7 | Decode(argv[2], argv[3]); |
35 : | chenm001 | 1.1 | break; |
36 : | default: | ||
37 : | printf("Error option: %c",argv[1][0]); | ||
38 : | return(ERR_OPT_NOT_SUPPORT); | ||
39 : | } | ||
40 : | return(ERR_OK); | ||
41 : | } | ||
42 : | |||
43 : | /************************************************************ | ||
44 : | * Encode Example Start | ||
45 : | ************************************************************/ | ||
46 : | char inBuf[BUF_LEN]; // Image Buffer | ||
47 : | char outBuf[BUF_LEN]; | ||
48 : | |||
49 : | void set_enc_param(XVID_ENC_PARAM *param) | ||
50 : | { | ||
51 : | param->rc_bitrate = 1500*1000; // Bitrate(in bps) | ||
52 : | param->rc_buffer = 100; | ||
53 : | param->rc_averaging_period = 100; | ||
54 : | param->rc_reaction_delay_factor = 16; | ||
55 : | |||
56 : | param->fincr = 1; // 15 fps | ||
57 : | param->fbase = 15; | ||
58 : | |||
59 : | param->min_quantizer = 1; | ||
60 : | param->max_quantizer = 31; | ||
61 : | param->max_key_interval = 100; | ||
62 : | |||
63 : | chenm001 | 1.4 | #ifdef BFRAMES |
64 : | chenm001 | 1.1 | param->max_bframes = 0; // Disable B-frame |
65 : | chenm001 | 1.4 | #endif |
66 : | chenm001 | 1.1 | } |
67 : | |||
68 : | void set_enc_frame(XVID_ENC_FRAME *frame) | ||
69 : | { | ||
70 : | // set encode frame param | ||
71 : | frame->general = XVID_HALFPEL; | ||
72 : | frame->general |= XVID_INTER4V; | ||
73 : | frame->motion = PMV_HALFPELDIAMOND8; | ||
74 : | frame->image = inBuf; | ||
75 : | frame->bitstream = outBuf; | ||
76 : | frame->length = BUF_LEN; | ||
77 : | frame->colorspace = XVID_CSP_YV12; // the test.yuv format is YV12 | ||
78 : | frame->quant = 0; // CBR mode | ||
79 : | |||
80 : | frame->general |= XVID_MPEGQUANT; // Use MPEG quant | ||
81 : | frame->quant_inter_matrix = NULL; // Use default quant matrix | ||
82 : | frame->quant_intra_matrix = NULL; | ||
83 : | } | ||
84 : | |||
85 : | |||
86 : | int Encode(char *in, int width, int height, char *out) | ||
87 : | { | ||
88 : | XVID_ENC_PARAM param; | ||
89 : | XVID_INIT_PARAM init_param; | ||
90 : | XVID_ENC_FRAME frame; | ||
91 : | XVID_ENC_STATS stats; | ||
92 : | int num=0; // Encoded frames | ||
93 : | int temp; | ||
94 : | |||
95 : | FILE *fpi=fopen(in,"rb"), | ||
96 : | *fpo=fopen(out,"wb"); | ||
97 : | if (fpi == NULL || fpo==NULL){ | ||
98 : | if (fpi) | ||
99 : | fclose(fpi); | ||
100 : | if (fpo) | ||
101 : | fclose(fpo); | ||
102 : | return(ERR_FILE); | ||
103 : | } | ||
104 : | |||
105 : | if (height*width*3 > sizeof(inBuf)){ | ||
106 : | fclose(fpi); | ||
107 : | fclose(fpo); | ||
108 : | return(ERR_MEMORY); | ||
109 : | } | ||
110 : | |||
111 : | // get Xvid core status | ||
112 : | chenm001 | 1.6 | init_param.cpu_flags = 0; |
113 : | chenm001 | 1.1 | xvid_init(0, 0, &init_param, NULL); |
114 : | // Check API Version is 2.1? | ||
115 : | if (init_param.api_version != ((2<<16)|(1))) | ||
116 : | return(ERR_VERSION); | ||
117 : | |||
118 : | |||
119 : | param.width = width; | ||
120 : | param.height = height; | ||
121 : | set_enc_param(¶m); | ||
122 : | |||
123 : | // Init Encode | ||
124 : | temp=xvid_encore(0, XVID_ENC_CREATE, ¶m, NULL); | ||
125 : | |||
126 : | // Encode Frame | ||
127 : | temp=fread(inBuf, 1, width*height*3/2, fpi); // Read YUV data | ||
128 : | while(temp == width*height*3/2){ | ||
129 : | chenm001 | 1.6 | printf("Frames=%d\n",num); |
130 : | chenm001 | 1.1 | set_enc_frame(&frame); |
131 : | if (!(num%param.max_key_interval)) | ||
132 : | frame.intra = 1; // Encode as I-frame | ||
133 : | else | ||
134 : | frame.intra = 0; // Encode as P-frame | ||
135 : | xvid_encore(param.handle, XVID_ENC_ENCODE, &frame, &stats); | ||
136 : | fwrite(outBuf, 1, frame.length, fpo); | ||
137 : | temp=fread(inBuf, 1, width*height*3/2, fpi); // Read next YUV data | ||
138 : | chenm001 | 1.3 | num++; |
139 : | chenm001 | 1.1 | } |
140 : | |||
141 : | // Free Encode Core | ||
142 : | if (param.handle) | ||
143 : | xvid_encore(param.handle, XVID_ENC_DESTROY, NULL, NULL); | ||
144 : | |||
145 : | fclose(fpi); | ||
146 : | fclose(fpo); | ||
147 : | return(ERR_OK); | ||
148 : | } | ||
149 : | |||
150 : | chenm001 | 1.7 | void set_dec_param(XVID_DEC_PARAM *param) |
151 : | chenm001 | 1.1 | { |
152 : | chenm001 | 1.7 | // set to 0 will auto set width & height from encoded bitstream |
153 : | param->height = param->width = 0; | ||
154 : | } | ||
155 : | |||
156 : | int Decode(char *in, char *out) | ||
157 : | { | ||
158 : | int width, height,i=0; | ||
159 : | uint32_t temp; | ||
160 : | long readed; | ||
161 : | XVID_DEC_PARAM param; | ||
162 : | XVID_INIT_PARAM init_param; | ||
163 : | DECODER *dec; | ||
164 : | XVID_DEC_FRAME frame; | ||
165 : | Bitstream bs; | ||
166 : | |||
167 : | chenm001 | 1.1 | FILE *fpi=fopen(in,"rb"), |
168 : | *fpo=fopen(out,"wb"); | ||
169 : | if (fpi == NULL || fpo==NULL){ | ||
170 : | if (fpi) | ||
171 : | fclose(fpi); | ||
172 : | if (fpo) | ||
173 : | fclose(fpo); | ||
174 : | return(ERR_FILE); | ||
175 : | } | ||
176 : | chenm001 | 1.7 | |
177 : | |||
178 : | init_param.cpu_flags = 0; | ||
179 : | xvid_init(0, 0, &init_param, NULL); | ||
180 : | // Check API Version is 2.1? | ||
181 : | if (init_param.api_version != ((2<<16)|(1))) | ||
182 : | return(ERR_VERSION); | ||
183 : | |||
184 : | set_dec_param(¶m); | ||
185 : | |||
186 : | temp = xvid_decore(0, XVID_DEC_CREATE, ¶m, NULL); | ||
187 : | dec = param.handle; | ||
188 : | |||
189 : | if (dec != NULL){ | ||
190 : | // to Get Video width & height | ||
191 : | readed = fread(inBuf, 1, MAX_FRAME_SIZE, fpi); | ||
192 : | BitstreamInit(&bs, inBuf, readed); | ||
193 : | BitstreamReadHeaders(&bs, dec, &temp, &temp, &temp, &temp, &temp); | ||
194 : | width = dec->width; | ||
195 : | height = dec->height; | ||
196 : | xvid_decore(dec, XVID_DEC_DESTROY, NULL, NULL); | ||
197 : | |||
198 : | // recreate new decoder because width & height changed! | ||
199 : | param.width = width; | ||
200 : | param.height = height; | ||
201 : | temp = xvid_decore(0, XVID_DEC_CREATE, ¶m, NULL); | ||
202 : | dec = param.handle; | ||
203 : | |||
204 : | // because START_CODE must be 32bit | ||
205 : | while(readed >= 4){ | ||
206 : | printf("Decode Frame %d\n",i++); | ||
207 : | frame.bitstream = inBuf; | ||
208 : | frame.length = readed; | ||
209 : | frame.image = outBuf; | ||
210 : | frame.stride = width; | ||
211 : | frame.colorspace = XVID_CSP_YV12; // for input video clip color space | ||
212 : | |||
213 : | temp = xvid_decore(dec, XVID_DEC_DECODE, &frame, NULL); | ||
214 : | |||
215 : | // undo unused byte | ||
216 : | fseek(fpi, frame.length - readed - 2, SEEK_CUR); | ||
217 : | |||
218 : | // Write decoded YUV image, size = width * height * 3 / 2 because in I420 CSP | ||
219 : | fwrite(outBuf, 1, width * height * 3 / 2, fpo); | ||
220 : | |||
221 : | // read next frame data | ||
222 : | readed = fread(inBuf, 1, MAX_FRAME_SIZE, fpi); | ||
223 : | } | ||
224 : | |||
225 : | // free decoder | ||
226 : | xvid_decore(dec, XVID_DEC_DESTROY, NULL, NULL); | ||
227 : | } | ||
228 : | |||
229 : | chenm001 | 1.1 | fclose(fpi); |
230 : | fclose(fpo); | ||
231 : | return(ERR_OK); | ||
232 : | } | ||
233 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |