[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 231 - (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 |= XVID_MPEGQUANT; // Use MPEG quant
76 :     frame->quant_inter_matrix = NULL; // Use default quant matrix
77 :     frame->quant_intra_matrix = NULL;
78 :     }
79 :    
80 :    
81 :     int Encode(char *in, int width, int height, char *out)
82 :     {
83 :     XVID_ENC_PARAM param;
84 :     XVID_INIT_PARAM init_param;
85 :     XVID_ENC_FRAME frame;
86 :     XVID_ENC_STATS stats;
87 :     int num=0; // Encoded frames
88 :     int temp;
89 :    
90 :     FILE *fpi=fopen(in,"rb"),
91 :     *fpo=fopen(out,"wb");
92 :     if (fpi == NULL || fpo==NULL){
93 :     if (fpi)
94 :     fclose(fpi);
95 :     if (fpo)
96 :     fclose(fpo);
97 :     return(ERR_FILE);
98 :     }
99 :    
100 :     if (height*width*3 > sizeof(inBuf)){
101 :     fclose(fpi);
102 :     fclose(fpo);
103 :     return(ERR_MEMORY);
104 :     }
105 :    
106 :     // get Xvid core status
107 :     xvid_init(0, 0, &init_param, NULL);
108 :     // Check API Version is 2.1?
109 :     if (init_param.api_version != ((2<<16)|(1)))
110 :     return(ERR_VERSION);
111 :    
112 :    
113 :     param.width = width;
114 :     param.height = height;
115 :     set_enc_param(&param);
116 :    
117 :     // Init Encode
118 :     temp=xvid_encore(0, XVID_ENC_CREATE, &param, NULL);
119 :    
120 :     // Encode Frame
121 :     temp=fread(inBuf, 1, width*height*3/2, fpi); // Read YUV data
122 :     while(temp == width*height*3/2){
123 : chenm001 185 //printf("Frames=%d\n",num);
124 : chenm001 182 set_enc_frame(&frame);
125 :     if (!(num%param.max_key_interval))
126 :     frame.intra = 1; // Encode as I-frame
127 :     else
128 :     frame.intra = 0; // Encode as P-frame
129 :     xvid_encore(param.handle, XVID_ENC_ENCODE, &frame, &stats);
130 :     fwrite(outBuf, 1, frame.length, fpo);
131 :     temp=fread(inBuf, 1, width*height*3/2, fpi); // Read next YUV data
132 : chenm001 185 num++;
133 : chenm001 182 }
134 :    
135 :     // Free Encode Core
136 :     if (param.handle)
137 :     xvid_encore(param.handle, XVID_ENC_DESTROY, NULL, NULL);
138 :    
139 :     fclose(fpi);
140 :     fclose(fpo);
141 :     return(ERR_OK);
142 :     }
143 :    
144 :     int Decode(char *in, int width, int height, char *out)
145 :     {
146 :     FILE *fpi=fopen(in,"rb"),
147 :     *fpo=fopen(out,"wb");
148 :     if (fpi == NULL || fpo==NULL){
149 :     if (fpi)
150 :     fclose(fpi);
151 :     if (fpo)
152 :     fclose(fpo);
153 :     return(ERR_FILE);
154 :     }
155 :     fclose(fpi);
156 :     fclose(fpo);
157 :     return(ERR_OK);
158 :     }
159 :    

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