[svn] / trunk / xvidcore / examples / ex1 / ex1.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/examples/ex1/ex1.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 222 - (view) (download)

1 : chenm001 182 /*******************************************************************************
2 :     * This file is a example for how to use the xvid core to compress YUV file
3 :     *
4 : chenm001 185 * 0.01b 28.05.2002 chenm001<chenm001@163.com>
5 :     * fix a little bug for encode only codec I frame
6 : chenm001 183 * 0.01a 27.05.2002 chenm001<chenm001@163.com>
7 :     * fix a little bug for BFRAMES define locate
8 : chenm001 182 * 0.01 23.05.2002 chenm001<chenm001@163.com>
9 :     * the BFRAME option must be match to core compile option
10 :     *******************************************************************************/
11 :    
12 : chenm001 183 #define BFRAMES
13 : chenm001 182 #include "ex1.h"
14 :    
15 :     int Encode(char *, int, int, char *);
16 :     int Decode(char *, int, int, char *);
17 :    
18 :     int main(int argc, char *argv[])
19 :     {
20 :     if (argc<6){
21 :     printf("Usage:\n\t%s {Encode|Decode} infile width heigh outfile\n",argv[0]);
22 :     return(ERR_NO_OPT);
23 :     }
24 :     switch(toupper(argv[1][0])){
25 :     case 'E':
26 :     Encode(argv[2], atoi(argv[3]), atoi(argv[4]), argv[5]);
27 :     break;
28 :     case 'D':
29 :     Decode(argv[2], atoi(argv[3]), atoi(argv[4]), argv[5]);
30 :     break;
31 :     default:
32 :     printf("Error option: %c",argv[1][0]);
33 :     return(ERR_OPT_NOT_SUPPORT);
34 :     }
35 :     return(ERR_OK);
36 :     }
37 :    
38 :     /************************************************************
39 :     * Encode Example Start
40 :     ************************************************************/
41 :     char inBuf[BUF_LEN]; // Image Buffer
42 :     char outBuf[BUF_LEN];
43 :    
44 :     void set_enc_param(XVID_ENC_PARAM *param)
45 :     {
46 :     param->rc_bitrate = 1500*1000; // Bitrate(in bps)
47 :     param->rc_buffer = 100;
48 :     param->rc_averaging_period = 100;
49 :     param->rc_reaction_delay_factor = 16;
50 :    
51 :     param->fincr = 1; // 15 fps
52 :     param->fbase = 15;
53 :    
54 :     param->min_quantizer = 1;
55 :     param->max_quantizer = 31;
56 :     param->max_key_interval = 100;
57 :    
58 : chenm001 222 #ifdef BFRAMES
59 : chenm001 182 param->max_bframes = 0; // Disable B-frame
60 : chenm001 222 #endif
61 : chenm001 182 }
62 :    
63 :     void set_enc_frame(XVID_ENC_FRAME *frame)
64 :     {
65 :     // set encode frame param
66 :     frame->general = XVID_HALFPEL;
67 :     frame->general |= XVID_INTER4V;
68 :     frame->motion = PMV_HALFPELDIAMOND8;
69 :     frame->image = inBuf;
70 :     frame->bitstream = outBuf;
71 :     frame->length = BUF_LEN;
72 :     frame->colorspace = XVID_CSP_YV12; // the test.yuv format is YV12
73 :     frame->quant = 0; // CBR mode
74 :    
75 :     frame->general = 0;
76 :     frame->general |= XVID_MPEGQUANT; // Use MPEG quant
77 :     frame->quant_inter_matrix = NULL; // Use default quant matrix
78 :     frame->quant_intra_matrix = NULL;
79 :     }
80 :    
81 :    
82 :     int Encode(char *in, int width, int height, char *out)
83 :     {
84 :     XVID_ENC_PARAM param;
85 :     XVID_INIT_PARAM init_param;
86 :     XVID_ENC_FRAME frame;
87 :     XVID_ENC_STATS stats;
88 :     int num=0; // Encoded frames
89 :     int temp;
90 :    
91 :     FILE *fpi=fopen(in,"rb"),
92 :     *fpo=fopen(out,"wb");
93 :     if (fpi == NULL || fpo==NULL){
94 :     if (fpi)
95 :     fclose(fpi);
96 :     if (fpo)
97 :     fclose(fpo);
98 :     return(ERR_FILE);
99 :     }
100 :    
101 :     if (height*width*3 > sizeof(inBuf)){
102 :     fclose(fpi);
103 :     fclose(fpo);
104 :     return(ERR_MEMORY);
105 :     }
106 :    
107 :     // get Xvid core status
108 :     xvid_init(0, 0, &init_param, NULL);
109 :     // Check API Version is 2.1?
110 :     if (init_param.api_version != ((2<<16)|(1)))
111 :     return(ERR_VERSION);
112 :    
113 :    
114 :     param.width = width;
115 :     param.height = height;
116 :     set_enc_param(&param);
117 :    
118 :     // Init Encode
119 :     temp=xvid_encore(0, XVID_ENC_CREATE, &param, NULL);
120 :    
121 :     // Encode Frame
122 :     temp=fread(inBuf, 1, width*height*3/2, fpi); // Read YUV data
123 :     while(temp == width*height*3/2){
124 : chenm001 185 //printf("Frames=%d\n",num);
125 : chenm001 182 set_enc_frame(&frame);
126 :     if (!(num%param.max_key_interval))
127 :     frame.intra = 1; // Encode as I-frame
128 :     else
129 :     frame.intra = 0; // Encode as P-frame
130 :     xvid_encore(param.handle, XVID_ENC_ENCODE, &frame, &stats);
131 :     fwrite(outBuf, 1, frame.length, fpo);
132 :     temp=fread(inBuf, 1, width*height*3/2, fpi); // Read next YUV data
133 : chenm001 185 num++;
134 : chenm001 182 }
135 :    
136 :     // Free Encode Core
137 :     if (param.handle)
138 :     xvid_encore(param.handle, XVID_ENC_DESTROY, NULL, NULL);
139 :    
140 :     fclose(fpi);
141 :     fclose(fpo);
142 :     return(ERR_OK);
143 :     }
144 :    
145 :     int Decode(char *in, int width, int height, char *out)
146 :     {
147 :     FILE *fpi=fopen(in,"rb"),
148 :     *fpo=fopen(out,"wb");
149 :     if (fpi == NULL || fpo==NULL){
150 :     if (fpi)
151 :     fclose(fpi);
152 :     if (fpo)
153 :     fclose(fpo);
154 :     return(ERR_FILE);
155 :     }
156 :     fclose(fpi);
157 :     fclose(fpo);
158 :     return(ERR_OK);
159 :     }
160 :    

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4