[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 183 - (view) (download)

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

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