[svn] / branches / dev-api-4 / xvidcore / rawdec / rawdec.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/rawdec/rawdec.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 887 - (view) (download)

1 : suxen_drol 887 /*
2 :     rawdec: raw mpeg-4 bitstream decoder
3 :     (c)2001-2003 peter ross <pross@xvid.org>
4 :    
5 :     */
6 :    
7 :    
8 :     #include <stdio.h>
9 :     #include <stdlib.h>
10 :     #include <string.h>
11 :    
12 :     #include <xvid.h>
13 :    
14 :    
15 :     const static char * xvid_err(int err)
16 :     {
17 :     if (err == XVID_ERR_MEMORY) return "mem";
18 :     if (err == XVID_ERR_FORMAT) return "format";
19 :     if (err == XVID_ERR_VERSION) return "version";
20 :     return "unknown";
21 :     }
22 :    
23 :    
24 :     /* returns true if the string contains scanf %'s chars
25 :     (ignores %% sequences) */
26 :    
27 :     static int detect_sscanf(const char *s)
28 :     {
29 :     int percent = 0;
30 :     while(*s){
31 :     if (percent){
32 :     if (*s!='%')
33 :     return 1;
34 :     percent=0;
35 :     }
36 :     if (*s=='%')
37 :     percent = 1;
38 :     s++;
39 :     }
40 :     return 0;
41 :     }
42 :    
43 :    
44 :     int main(int argc, char **argv)
45 :     {
46 :     char tmp[1024];
47 :     FILE * fbs, *fpgm;
48 :     int fscanf;
49 :     void * bs;
50 :     int quit;
51 :     int length;
52 :     int pos;
53 :     int frameno;
54 :     int result;
55 :     int width, height;
56 :    
57 :     xvid_gbl_info_t info;
58 :     xvid_gbl_init_t init;
59 :     xvid_dec_create_t create;
60 :     xvid_dec_frame_t frame;
61 :     xvid_dec_stats_t stats;
62 :    
63 :     memset(&info, 0, sizeof(info));
64 :     info.version = XVID_VERSION;
65 :    
66 :     result = xvid_global(0, XVID_GBL_INFO, &info, 0);
67 :     if (result < 0)
68 :     {
69 :    
70 :     printf("xvid_gbl_info error: %s\n", xvid_err(result));
71 :     return EXIT_FAILURE;
72 :     }
73 :     printf("xvidcore version %d.%d.%d (\"%s\")\n",
74 :     XVID_MAJOR(info.actual_version),
75 :     XVID_MINOR(info.actual_version),
76 :     XVID_PATCH(info.actual_version),
77 :     info.build);
78 :     printf("cpu_flags:");
79 :     if ((info.cpu_flags & XVID_CPU_ASM)) printf(" ASM");
80 :     if ((info.cpu_flags & XVID_CPU_MMX)) printf(" MMX");
81 :     if ((info.cpu_flags & XVID_CPU_MMXEXT)) printf(" MMXEXT");
82 :     if ((info.cpu_flags & XVID_CPU_3DNOW)) printf(" 3DNOW");
83 :     if ((info.cpu_flags & XVID_CPU_3DNOWEXT)) printf(" 3DNOWEXT");
84 :     if ((info.cpu_flags & XVID_CPU_SSE)) printf(" SSE");
85 :     if ((info.cpu_flags & XVID_CPU_SSE2)) printf(" SSE2");
86 :     printf("\n");
87 :     printf("num_threads: %i\n", info.num_threads);
88 :    
89 :     if (argc != 3 && argc != 5)
90 :     {
91 :     fprintf(stderr,
92 :     "\nusage: rawdec bitstream outfile [width height]\n"
93 :     "\n"
94 :     "eg: rawdec foo.m4v [-|out.pgm|out%%05i.pgm]\n");
95 :    
96 :     return -1;
97 :     }
98 :    
99 :     /* open bitstream file & get length */
100 :    
101 :     fbs = fopen(argv[1], "rb");
102 :     if (fbs == NULL)
103 :     {
104 :     fprintf(stderr, "fatal: \"%s\" not found", argv[1]);
105 :     return -1;
106 :     }
107 :    
108 :     if (argc == 5)
109 :     {
110 :     width = atoi(argv[3]);
111 :     height = atoi(argv[4]);
112 :     }
113 :     else
114 :     {
115 :     width = height = 0; /* auto detect */
116 :     }
117 :    
118 :     fseek(fbs, 0, SEEK_END);
119 :     length = ftell(fbs);
120 :     fseek(fbs, 0, SEEK_SET);
121 :    
122 :     /* XXX: load entire bitstream file into memory */
123 :    
124 :     bs = malloc(length);
125 :     if (bs == NULL)
126 :     {
127 :     fprintf(stderr, "fatal: bitstream malloc failed\n");
128 :     return -1;
129 :     }
130 :    
131 :     fread(bs, length, 1, fbs);
132 :     fclose(fbs);
133 :    
134 :    
135 :     /* otuput file stuff */
136 :     fscanf = detect_sscanf(argv[2]);
137 :     if (argv[2][0] != '-' && !fscanf)
138 :     {
139 :     fpgm = fopen(argv[2], "wb");
140 :     if (fpgm == NULL){
141 :     fprintf(stderr,"aborting: \"%s\" write error\n", argv[2]);
142 :     free(bs);
143 :     return EXIT_FAILURE;
144 :     }
145 :     }
146 :    
147 :    
148 :     /* init globals
149 :     note: init.cpu_flags is optional
150 :     */
151 :    
152 :     memset(&init, 0, sizeof(init));
153 :     init.version = XVID_VERSION;
154 :     //init.cpu_flags = XVID_CPU_FORCE;
155 :     result = xvid_global(0, XVID_GBL_INIT, &init, NULL);
156 :     if (result < 0)
157 :     {
158 :     free(bs);
159 :     printf("xvid_gbl_init error: %s\n", xvid_err(result));
160 :     return EXIT_FAILURE;
161 :     }
162 :    
163 :    
164 :     /* create decoder instance */
165 :    
166 :     memset(&create, 0, sizeof(create));
167 :     create.version = XVID_VERSION;
168 :     create.width = width;
169 :     create.height = height;
170 :     result = xvid_decore(0, XVID_DEC_CREATE, &create, NULL);
171 :     if (result < 0)
172 :     {
173 :     free(bs);
174 :     fprintf(stderr, "xvid_dec_create error: %s\n", xvid_err(result));
175 :     return EXIT_FAILURE;
176 :     }
177 :    
178 :    
179 :     /* init other struct; we only have to do this once */
180 :     memset(&frame, 0, sizeof(frame));
181 :     frame.version = XVID_VERSION;
182 :    
183 :     memset(&stats, 0, sizeof(stats));
184 :     stats.version = XVID_VERSION;
185 :    
186 :    
187 :     /* decode frames ... */
188 :     quit = 0;
189 :     pos = 0;
190 :     frameno = 0;
191 :     while (!quit)
192 :     {
193 :     int y;
194 :    
195 :     if (pos < length)
196 :     {
197 :     frame.bitstream = (void*)((unsigned long)bs + pos);
198 :     frame.length = length - pos;
199 :     }else{
200 :     printf("**FLUSH LAST FRAME\n");
201 :     frame.length = -1; /* flush last frame */
202 :     quit = 1;
203 :     }
204 :     /* xvid will return pointers to it's internal image buffers */
205 :     frame.output.csp = XVID_CSP_INTERNAL;
206 :    
207 :     result = xvid_decore(create.handle, XVID_DEC_DECODE, &frame, &stats);
208 :     fprintf(stdout," #%i [%i/%i] length=%i\n", frameno, pos, length, frame.length); fflush(stderr);
209 :     if (result < 0)
210 :     {
211 :     fprintf(stderr, "xvid_dec_decode error: %s\n", xvid_err(result));
212 :     break;
213 :     }
214 :    
215 :     /* increase position by the ammount consumed for this frame */
216 :     pos += result;
217 :    
218 :    
219 :     if (stats.type == XVID_TYPE_NOTHING) /* no output */
220 :     {
221 :     continue;
222 :     }
223 :    
224 :     if (stats.type == XVID_TYPE_VOL) /* vol/resize */
225 :     {
226 :     printf("**RESIZE** %i,%i\n", stats.data.vol.width, stats.data.vol.height);
227 :     width = stats.data.vol.width;
228 :     height = stats.data.vol.height;
229 :     printf("%i\n", frame.length);
230 :     continue;
231 :     }
232 :    
233 :     /* write output image to pgm file */
234 :     if (argv[2][0] == '-')
235 :     {
236 :     fpgm = stdout;
237 :     }else if (fscanf) {
238 :     sprintf(tmp,argv[2], frameno);
239 :     fpgm = fopen(tmp, "wb");
240 :     if (fpgm == NULL) {
241 :     fprintf(stderr,"aborting: \"%s\" write error\n", tmp);
242 :     break;
243 :     }
244 :     }
245 :    
246 :     sprintf(tmp, "P5\n%i %i\n255\n", width, height * 3 / 2);
247 :     fwrite(tmp, strlen(tmp) , 1, fpgm);
248 :    
249 :     for (y = height; y; y--) {
250 :     fwrite(frame.output.plane[0], width, 1, fpgm);
251 :     frame.output.plane[0] = (void*)((char *)frame.output.plane[0] + frame.output.stride[0]);
252 :     }
253 :    
254 :     for (y = height >> 1; y; y--) {
255 :     fwrite(frame.output.plane[1], width / 2, 1, fpgm);
256 :     frame.output.plane[1] = (void*)((char *)frame.output.plane[1] + frame.output.stride[1]);
257 :    
258 :     fwrite(frame.output.plane[2], width / 2, 1, fpgm);
259 :     frame.output.plane[2] = (void*)((char *)frame.output.plane[2] + frame.output.stride[2]);
260 :     }
261 :    
262 :     if (argv[2][0] != '-' && fscanf) {
263 :     fclose(fpgm);
264 :     }
265 :    
266 :    
267 :     /* finally, increment frame num */
268 :     frameno++;
269 :     }
270 :    
271 :    
272 :     /* cleanup & exit */
273 :    
274 :     if (argv[2][0] != '-' && !fscanf)
275 :     {
276 :     fclose(fpgm);
277 :     }
278 :    
279 :     free(bs);
280 :    
281 :     result = xvid_decore(create.handle, XVID_DEC_DESTROY, NULL, NULL);
282 :     if (result < 0)
283 :     {
284 :     fprintf(stderr, "xvid_dec_destroy error: %s\n", xvid_err(result));
285 :     return EXIT_FAILURE;
286 :     }
287 :    
288 :     return EXIT_SUCCESS;
289 :     }

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