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

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