view h264dec.c @ 3:0b056460c67d

changed code to use VSs
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 29 Oct 2012 16:44:27 +0100
parents 897f711a7157
children c8259123d224
line source
1 /*
2 * H264 decoder main
3 */
5 #include "config.h"
6 #include "libavcodec/h264.h"
8 #include <string.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <unistd.h>
13 #include <getopt.h>
14 #include <fcntl.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <time.h>
21 #include <assert.h>
23 #include "VSs_impl/VSs.h"
25 static const char program_name[] = "h264dec";
26 static const int program_birth_year = 2010;
28 static const char *file_name;
29 static int ifile, ofile;
30 static int no_arch =0;
31 static int parallel = 1;
32 static int frame_width = 0;
33 static int frame_height = 0;
35 static void av_exit(int ret)
36 {
37 //do some free calls
38 #undef exit
39 exit(ret);
40 }
42 static void opt_input_file(const char *filename)
43 {
44 /* open the input file */
45 ifile = open(filename, O_RDONLY, 0666);
46 if (ifile < 0){
47 fprintf(stderr, "Failed to open %s\n", filename);
48 av_exit(-1);
49 }
51 //parse first frame to get resolution (other information available but not used)
52 H264Slice slice;
53 PictureInfo pi;
54 GetBitContext gb = {0,};
55 ParserContext *pc;
56 NalContext *nc;
58 pc = get_parse_context(ifile);
59 nc = get_nal_context(0, 0);
61 memset(&slice, 0, sizeof(H264Slice));
62 slice.current_picture_info=&pi;
64 av_read_frame_internal(pc, &gb);
65 decode_nal_units(nc, &slice, &gb);
67 frame_width = nc->width;
68 frame_height= nc->height;
70 //clean up
71 av_freep(&gb.raw);
72 if (gb.rbsp)
73 av_freep(&gb.rbsp);
74 free_parse_context(pc);
75 free_nal_context(nc);
77 //rewind file
78 int offset;
79 if ( (offset=lseek(ifile, 0, SEEK_SET)) ){
80 fprintf(stderr, "Rewind input file %s failed at offset %d\n", filename, offset);
81 }
83 }
85 static void opt_output_file(const char *filename)
86 {
87 if (filename){
88 if (!strcmp(filename, "-"))
89 filename = "pipe:";
91 ofile = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666);
92 }else{
93 ofile =0;
94 }
95 }
97 static void show_usage(void)
98 {
99 printf("usage: ffmpeg [options] -i infile }...\n");
100 printf("\n");
101 }
103 static struct option long_options[] = {
104 {"static-sched", 0, 0, 0},
105 {"static-mbd", 0, 0, 0},
106 {"numamap", 0, 0, 0},
107 {"no-mbd", 0, 0, 0},
108 {"static-3d", 0, 0, 0},
109 {"slice-bufs", 1, 0, 0},
110 {"smt", 0, 0, 0},
111 {"noarch", 0, 0, 'a'},
112 {"display", 0, 0, 'd'},
113 {"fullscreen", 0, 0, 'f'},
114 {"numframes", 1, 0, 'n'},
115 {"use-ppe-ed", 1, 0, 'p'},
116 {"sequential", 0, 0, 's'},
117 {"threads", 1, 0, 't'},
118 {"verbose", 1, 0, 'v'},
119 {"wave-order", 1, 0, 'w'},
120 {"smb-size", 1, 0, 'z'},
121 {"pipe-bufs", 1, 0, 'e'},
122 {0, 0, 0, 0}
123 };
125 static h264_options cli_opts;
126 static void parse_cmd(int argc, char **argv)
127 {
128 int c;
129 int digit_optind = 0;
130 int option_index = 0;
131 char ofile_name[1024];
132 extern char *optarg;
133 extern int optind, optopt;
135 cli_opts.statsched =0;
136 cli_opts.numamap =0;
137 cli_opts.statmbd =0;
138 cli_opts.no_mbd= 0;
139 cli_opts.numframes = INT_MAX;
140 cli_opts.display=0;
141 cli_opts.fullscreen=0;
142 cli_opts.verbose=0;
143 cli_opts.ppe_ed=0;
144 cli_opts.profile=0;
145 cli_opts.threads = 1;
146 cli_opts.smb_size[0] = cli_opts.smb_size[1] = 1;
147 cli_opts.wave_order=0;
148 cli_opts.static_3d=0;
149 cli_opts.pipe_bufs=8;
150 cli_opts.slice_bufs=1;
151 cli_opts.smt= 0;
152 while ((c = getopt_long(argc, argv, "ade:fi:n:o:p:st:vwz:", long_options, &option_index)) != -1 ){
153 int this_option_optind = optind ? optind : 1;
155 switch (c){
156 case 0:
157 if (option_index==0){
158 cli_opts.statsched=1;
159 }else if (option_index==1){
160 cli_opts.statmbd= 1;
161 }else if (option_index==2){
162 cli_opts.numamap= 1;
163 }else if (option_index==3){
164 cli_opts.no_mbd= 1;
165 }else if (option_index==4){
166 cli_opts.static_3d= 1;
167 }else if (option_index==5){
168 cli_opts.slice_bufs= (unsigned) atoi(optarg);
169 }else if (option_index==6){
170 cli_opts.smt= 1;
171 }
172 break;
173 case '0':
174 case '1':
175 case '2':
176 if (digit_optind != 0 && digit_optind != this_option_optind)
177 printf("digits occur in two different argv-elements.\n");
178 digit_optind = this_option_optind;
179 printf("option %c\n", c);
180 break;
181 case 'a':
182 no_arch=1;
183 break;
184 case 'd':
185 cli_opts.display=1;
186 break;
187 case 'f':
188 cli_opts.fullscreen=1;
189 break;
190 case 'i':
191 file_name = (const char *)optarg;
192 opt_input_file(file_name);
193 break;
194 case 'n':
195 cli_opts.numframes = (unsigned) atoi(optarg);
196 break;
197 case 'o':
198 strcpy(ofile_name, optarg);
199 opt_output_file(ofile_name);
200 break;
201 case 'p':
202 cli_opts.profile = (unsigned) atoi(optarg);
203 break;
204 case 's':
205 cli_opts.threads = 0;
206 parallel = 0;
207 break;
208 case 't':
209 cli_opts.threads = atoi(optarg);
210 if (cli_opts.threads<=0){
211 fprintf(stderr, "Option -%c requires thread numbers > 0\n", c);
212 av_exit(-1);
213 }
214 break;
215 case 'v':
216 cli_opts.verbose = 1;
217 break;
218 case 'w':
219 cli_opts.wave_order = 1;
220 break;
221 case 'z': // only useful in ompss
222 if (argc < optind +1){
223 fprintf(stderr, "Option -%c (--smb-size) requires 2 arguments\n", c);
224 av_exit(-1);
225 }
226 optind--;
227 for (int i=0; i<2; i++){
228 cli_opts.smb_size[i] = atoi(argv[optind++]);
229 if (!(cli_opts.smb_size > 0)){
230 fprintf(stderr, "Option -%c (--smb-size) requires dimensions > 0\n", c);
231 av_exit(-1);
232 }
233 }
234 break;
235 case 'e':
236 cli_opts.pipe_bufs = atoi(optarg);
237 break;
238 case ':':
239 fprintf(stderr, "Option -%c requires an operand\n", optopt);
240 av_exit(-1);
241 break;
242 case '?':
243 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
244 av_exit(-1);
245 break;
246 }
247 }
249 }
251 int main(int argc, char **argv)
252 {
253 /* parse options */
254 parse_cmd(argc, argv);
256 if(!ifile ) {
257 show_usage();
258 av_exit(1);
259 }
261 H264Context *h = get_h264dec_context(file_name, ifile, ofile, frame_width, frame_height, &cli_opts);
262 #if OMPSS
263 VSs__create_seed_slave_and_do_work( &h264_decode_ompss , (void*)h );
264 #else
265 if (parallel){
266 if (ARCH_CELL && !no_arch){
267 if (h264_decode_cell( h ) < 0)
268 av_exit(-1);
269 }else{
270 if (h264_decode_pthread( h ) < 0)
271 av_exit(1);
272 }
273 }else{
274 if (ARCH_CELL && !no_arch){
275 if (h264_decode_cell_seq( h ) < 0)
276 av_exit(1);
277 }else{
278 if (h264_decode_seq( h ) < 0)
279 av_exit(1);
280 }
281 }
282 #endif
283 free_h264dec_context(h);
284 close(ifile);
285 close(ofile);
287 return 0;
288 }