1 /*****************************************************************
2  * gmerlin - a general purpose multimedia framework and applications
3  *
4  * Copyright (c) 2001 - 2011 Members of the Gmerlin project
5  * gmerlin-general@lists.sourceforge.net
6  * http://gmerlin.sourceforge.net
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  * *****************************************************************/
21 
22 #include <string.h>
23 
24 #include <gmerlin/pluginregistry.h>
25 #include <gmerlin/utils.h>
26 #include <gmerlin/bggavl.h>
27 #include <gmerlin/cmdline.h>
28 
29 #include <config.h>
30 #include <gmerlin/translation.h>
31 
32 bg_parameter_info_t conversion_parameters[] =
33   {
34     BG_GAVL_PARAM_CONVERSION_QUALITY,
35     BG_GAVL_PARAM_SCALE_MODE,
36     BG_GAVL_PARAM_DEINTERLACE,
37     { /* End of parameters */ }
38   };
39 
40 bg_cfg_section_t * conversion_section = NULL;
41 bg_gavl_video_options_t vopt;
42 
set_video_parameter(void * data,const char * name,const bg_parameter_value_t * v)43 static void set_video_parameter(void * data, const char * name,
44                                 const bg_parameter_value_t * v)
45   {
46   if(!name)
47     return;
48   bg_gavl_video_set_parameter(data, name, v);
49   }
50 
51 
opt_video_options(void * data,int * argc,char *** argv,int arg)52 static void opt_video_options(void * data, int * argc, char *** argv, int arg)
53   {
54   //  fprintf(stderr, "opt_video_options\n");
55 
56   if(arg >= *argc)
57     {
58     fprintf(stderr, "Option -co requires an argument\n");
59     exit(-1);
60     }
61 
62   /* Parse the option string */
63   if(!bg_cmdline_apply_options(conversion_section,
64                                set_video_parameter,
65                                &vopt,
66                                conversion_parameters,
67                                (*argv)[arg]))
68     {
69     fprintf(stderr, "Error parsing option string %s\n", (*argv)[arg]);
70     exit(-1);
71     }
72 
73   bg_cmdline_remove_arg(argc, argv, arg);
74   }
75 
76 //static void opt_help(void * data, int * argc, char *** argv, int arg);
77 
78 static bg_cmdline_arg_t global_options[] =
79   {
80     {
81       .arg =         "-co",
82       .help_arg =    "<options>",
83       .help_string = "Conversion options",
84       .callback =    opt_video_options,
85       .parameters =  conversion_parameters,
86     },
87     { /* End of options */ }
88   };
89 
90 bg_cmdline_app_data_t app_data =
91   {
92     .package =  PACKAGE,
93     .version =  VERSION,
94     .name =     "gmerlin_imgconvert",
95     .synopsis = TRS("[-co options] input_image output_image\n"),
96     .help_before = TRS("Image converter\n"),
97     .args = (bg_cmdline_arg_array_t[]) { { TRS("Options"), global_options },
98                                        {  } },
99   };
100 
main(int argc,char ** argv)101 int main(int argc, char ** argv)
102   {
103   gavl_video_options_t * opt;
104   bg_cfg_registry_t * cfg_reg;
105   bg_cfg_section_t * cfg_section;
106   bg_plugin_registry_t * plugin_reg;
107   const bg_plugin_info_t * plugin_info;
108   /* Plugin handles */
109   bg_image_writer_plugin_t * output_plugin;
110 
111   bg_plugin_handle_t * output_handle = NULL;
112 
113   gavl_video_converter_t * cnv;
114 
115   gavl_video_frame_t * in_frame;
116   gavl_video_frame_t * out_frame;
117 
118   gavl_video_format_t in_format;
119   gavl_video_format_t out_format;
120   char * tmp_path;
121   char ** files;
122   int num_conversions;
123 
124   gavl_metadata_t metadata;
125 
126   conversion_section =
127     bg_cfg_section_create_from_parameters("conversion",
128                                           conversion_parameters);
129 
130   memset(&in_format, 0, sizeof(in_format));
131   memset(&out_format, 0, sizeof(out_format));
132   memset(&metadata, 0, sizeof(metadata));
133 
134   cnv = gavl_video_converter_create();
135   /* Handle options */
136   bg_gavl_video_options_init(&vopt);
137 
138   /* Parse options */
139   bg_cmdline_init(&app_data);
140   bg_cmdline_parse(global_options, &argc, &argv, NULL);
141 
142   files = bg_cmdline_get_locations_from_args(&argc, &argv);
143 
144   if(!files || !files[0] || !files[1] || files[2])
145     {
146     bg_cmdline_print_help(argv[0], 0);
147     return -1;
148     }
149   /* Create registries */
150 
151   cfg_reg = bg_cfg_registry_create();
152   tmp_path =  bg_search_file_read("generic", "config.xml");
153   bg_cfg_registry_load(cfg_reg, tmp_path);
154   if(tmp_path)
155     free(tmp_path);
156 
157   cfg_section = bg_cfg_registry_find_section(cfg_reg, "plugins");
158   plugin_reg = bg_plugin_registry_create(cfg_section);
159 
160   /* Load input image */
161 
162   in_frame = bg_plugin_registry_load_image(plugin_reg,
163                                            files[0],
164                                            &in_format, &metadata);
165   if(!in_frame)
166     {
167     fprintf(stderr, "Couldn't load %s\n", files[0]);
168     return -1;
169     }
170 
171   /* Initialize output plugin */
172   fprintf(stderr, "%s -> %s\n", files[0], files[1]);
173   plugin_info = bg_plugin_find_by_filename(plugin_reg, files[1],
174                                            BG_PLUGIN_IMAGE_WRITER);
175   if(!plugin_info)
176     {
177     fprintf(stderr, "Cannot detect plugin for %s\n", files[1]);
178     return -1;
179     }
180 
181   output_handle = bg_plugin_load(plugin_reg, plugin_info);
182   output_plugin = (bg_image_writer_plugin_t*)(output_handle->plugin);
183 
184   gavl_video_format_copy(&out_format, &in_format);
185 
186   bg_gavl_video_options_set_format(&vopt, &in_format, &out_format);
187 
188   output_plugin->write_header(output_handle->priv, files[1], &out_format, &metadata);
189 
190   /* For testing gavl chroma sampling */
191 #if 0
192   if(in_format.pixelformat == GAVL_YUVJ_420_P)
193     {
194     in_format.pixelformat = GAVL_YUVJ_420_P;
195     in_format.chroma_placement = GAVL_CHROMA_PLACEMENT_MPEG2;
196     //    in_format.interlace_mode = GAVL_INTERLACE_BOTTOM_FIRST;
197     }
198   if(out_format.pixelformat == GAVL_YUVJ_420_P)
199     {
200     out_format.pixelformat = GAVL_YUVJ_420_P;
201     out_format.chroma_placement = GAVL_CHROMA_PLACEMENT_MPEG2;
202     //    out_format.interlace_mode = GAVL_INTERLACE_BOTTOM_FIRST;
203     }
204 #endif
205   fprintf(stderr, "Input format:\n");
206   gavl_video_format_dump(&in_format);
207 
208   fprintf(stderr, "Output format:\n");
209   gavl_video_format_dump(&out_format);
210 
211   gavl_metadata_dump(&metadata, 0);
212 
213   opt = gavl_video_converter_get_options(cnv);
214   gavl_video_options_copy(opt, vopt.opt);
215 
216   num_conversions = gavl_video_converter_init(cnv, &in_format, &out_format);
217   fprintf(stderr, "num_conversions: %d\n",num_conversions);
218 
219   if(num_conversions)
220     {
221 
222     out_frame = gavl_video_frame_create(&out_format);
223     gavl_video_convert(cnv, in_frame, out_frame);
224     output_plugin->write_image(output_handle->priv, out_frame);
225     gavl_video_frame_destroy(out_frame);
226     }
227   else
228     output_plugin->write_image(output_handle->priv, in_frame);
229 
230   fprintf(stderr, "Wrote %s\n", files[1]);
231 
232   gavl_video_frame_destroy(in_frame);
233   bg_plugin_unref(output_handle);
234 
235   bg_plugin_registry_destroy(plugin_reg);
236   bg_cfg_registry_destroy(cfg_reg);
237 
238   bg_gavl_video_options_free(&vopt);
239 
240   gavl_video_converter_destroy(cnv);
241 
242   gavl_metadata_free(&metadata);
243 
244   return 0;
245   }
246