7 #include <libavcodec/avcodec.h>
8 #include <libavutil/mathematics.h>
40 /* input file function pointers */
41 int (*p_open_infile
)( char *psz_filename
, hnd_t
*p_handle
, config_t
*p_config
);
42 int (*p_read_frame
)( hnd_t handle
, picture_t
*p_pic
, int i_frame
);
43 int (*p_close_infile
)( hnd_t handle
);
45 /* output file function pointers */
46 static int (*p_open_outfile
)( char *psz_filename
, hnd_t
*p_handle
, int i_compression
);
47 // static int (*p_set_outfile_param)( hnd_t handle, config_t *p_config );
48 static int (*p_write_image
)( hnd_t handle
, picture_t
*p_pic
, config_t
*p_config
);
49 static int (*p_close_outfile
)( hnd_t handle
);
51 static int parse_options( int argc
, char **argv
, config_t
*config
, cli_opt_t
*opt
);
52 static int grab_frames( config_t
*config
, cli_opt_t
*opt
);
54 int main(int argc
, char **argv
)
60 parse_options(argc
, argv
, &config
, &opt
);
62 ret
= grab_frames( &config
, &opt
);
67 static void show_help(void)
70 HELP( "Syntax: frameshot [options] infile\n"
72 "Infile is a raw bitstream of one of the following codecs:\n"
77 " -h, --help Displays this message.\n"
79 HELP( " -f, --frames <int,int,...> Frames numbers to grab.\n" );
80 HELP( " -o, --output <string> Prefix to use for each output image.\n" );
81 HELP( " -z, --compression <integer> Ammount of compression to use.\n" );
82 HELP( " -1, --fast Use fastest compression.\n" );
83 HELP( " -9, --best Use best (slowest) compression.\n" );
87 static int parse_options( int argc
, char **argv
, config_t
*config
, cli_opt_t
*opt
)
89 char *psz_filename
= NULL
;
90 int i_zlevel
= Z_DEFAULT_COMPRESSION
;
94 memset( opt
, 0, sizeof(*opt
) );
96 /* Default input driver */
97 p_open_infile
= open_file_y4m
;
98 p_read_frame
= read_frame_y4m
;
99 p_close_infile
= close_file_y4m
;
101 /* Default output driver */
102 p_open_outfile
= open_file_png
;
103 p_write_image
= write_image_png
;
104 p_close_outfile
= close_file_png
;
108 int long_options_index
= -1;
109 static struct option long_options
[] =
111 { "fast", no_argument
, NULL
, '1' },
112 { "best", no_argument
, NULL
, '9' },
113 { "frames", required_argument
, NULL
, 'f' },
114 { "help", no_argument
, NULL
, 'h' },
115 { "output", required_argument
, NULL
, 'o' },
116 { "compression", required_argument
, NULL
, 'z' },
120 int c
= getopt_long( argc
, argv
, "19f:ho:z:",
121 long_options
, &long_options_index
);
131 i_zlevel
= Z_BEST_SPEED
;
134 i_zlevel
= Z_BEST_COMPRESSION
;
141 i_zlevel
= atoi(optarg
);
142 if( i_zlevel
< 0 || i_zlevel
> 9)
143 i_zlevel
= Z_DEFAULT_COMPRESSION
;
152 /* Get the input file name */
153 if( optind
> argc
- 1 )
155 fprintf( stderr
, "[error]: No input file.\n" );
159 psz_filename
= argv
[optind
++];
161 psz
= strrchr( psz_filename
, '.' );
162 if( !strncasecmp( psz
, ".y4m", 4 ) )
167 char *psz_outname
= strdup(psz_filename
);
168 char *psz_ext
= strrchr( psz_outname
, '.' );
169 psz_ext
[1] = 'p'; psz_ext
[2] = 'n'; psz_ext
[3] = 'g'; psz_ext
[4] = 0x00;
170 p_open_outfile( psz_outname
, &opt
->hout
, i_zlevel
);
176 p_open_infile
= open_file_y4m
;
177 p_read_frame
= read_frame_y4m
;
178 p_close_infile
= close_file_y4m
;
181 if( p_open_infile( psz_filename
, &opt
->hin
, config
) )
183 fprintf( stderr
, "ERROR: could not open input file '%s'\n", psz_filename
);
190 static int grab_frames( config_t
*config
, cli_opt_t
*opt
)
194 pic
.img
.plane
[0] = malloc(3 * config
->i_width
* config
->i_height
/ 2);
195 pic
.img
.plane
[1] = pic
.img
.plane
[0] + config
->i_width
* config
->i_height
;
196 pic
.img
.plane
[2] = pic
.img
.plane
[1] + config
->i_width
* config
->i_height
/ 4;
197 pic
.img
.plane
[3] = NULL
;
199 pic
.img
.i_stride
[0] = config
->i_width
;
200 pic
.img
.i_stride
[1] = pic
.img
.i_stride
[2] = config
->i_width
/ 2;
201 pic
.img
.i_stride
[3] = 0;
203 p_read_frame( opt
->hin
, &pic
, 1 );
205 p_write_image( opt
->hout
, &pic
, config
);
207 p_close_infile( opt
->hin
);
208 p_close_outfile( opt
->hout
);