1 /*****************************************************************************
2 * x264_gtk_encode_encode.c: h264 gtk encoder frontend
3 *****************************************************************************
4 * Copyright (C) 2006 Vincent Torri
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *****************************************************************************/
22 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __DragonFly__
23 # include <inttypes.h>
31 #include "x264_gtk_i18n.h"
32 #include "x264_gtk_demuxers.h"
33 #include "x264_gtk_encode_private.h"
36 #define DATA_MAX 3000000
37 uint8_t data[DATA_MAX];
39 int64_t x264_mdate (void);
42 int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param );
43 int (*p_get_frame_total)( hnd_t handle );
44 int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
45 int (*p_close_infile)( hnd_t handle );
47 /* output interface */
48 static int (*p_open_outfile) (char *filename, void **handle);
49 static int (*p_set_outfile_param) (void *handle, x264_param_t *p_param);
50 static int (*p_write_nalu) (void *handle, uint8_t *p_nal, int i_size);
51 static int (*p_set_eop) (void *handle, x264_picture_t *p_picture);
52 static int (*p_close_outfile) (void *handle);
54 static int x264_set_drivers (X264_Demuxer_Type in_container, gint out_container);
55 static int x264_encode_frame (x264_t *h, void *handle, x264_picture_t *pic);
59 x264_gtk_encode_encode (X264_Thread_Data *thread_data)
63 X264_Pipe_Data pipe_data;
78 g_print (_("encoding...\n"));
79 param = thread_data->param;
80 err = x264_set_drivers (thread_data->in_container, thread_data->out_container);
83 no_driver = gtk_message_dialog_new (GTK_WINDOW(thread_data->dialog),
84 GTK_DIALOG_DESTROY_WITH_PARENT,
87 (err == -2) ? _("Error: unknown output file type")
88 : _("Error: unknown input file type"));
89 gtk_dialog_run (GTK_DIALOG (no_driver));
90 gtk_widget_destroy (no_driver);
94 if (p_open_infile (thread_data->file_input, &hin, param)) {
95 fprintf( stderr, _("could not open input file '%s'\n"), thread_data->file_input );
99 p_open_outfile ((char *)thread_data->file_output, &hout);
101 i_frame_total = p_get_frame_total (hin );
102 if (((i_frame_total == 0) || (param->i_frame_total < i_frame_total)) &&
103 (param->i_frame_total > 0))
104 i_frame_total = param->i_frame_total;
105 param->i_frame_total = i_frame_total;
107 if ((h = x264_encoder_open (param)) == NULL)
109 fprintf (stderr, _("x264_encoder_open failed\n"));
110 p_close_infile (hin);
111 p_close_outfile (hout);
117 if (p_set_outfile_param (hout, param))
119 fprintf (stderr, _("can't set outfile param\n"));
120 p_close_infile (hin);
121 p_close_outfile (hout);
127 /* Create a new pic */
128 x264_picture_alloc (&pic, X264_CSP_I420, param->i_width, param->i_height );
130 i_start = x264_mdate();
133 for (i_frame = 0, i_file = 0, i_progress = 0;
134 ((i_frame < i_frame_total) || (i_frame_total == 0)); )
136 if (p_read_frame (&pic, hin, i_frame))
139 pic.i_pts = (int64_t)i_frame * param->i_fps_den;
141 i_file += x264_encode_frame (h, hout, &pic);
145 /* update status line (up to 1000 times per input file) */
146 if (param->i_log_level < X264_LOG_DEBUG &&
147 (i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
148 : i_frame % 10 == 0))
150 int64_t i_elapsed = x264_mdate () - i_start;
154 pipe_data.frame = i_frame;
155 pipe_data.frame_total = i_frame_total;
156 pipe_data.file = i_file;
157 pipe_data.elapsed = i_elapsed;
158 status = g_io_channel_write_chars (thread_data->io_write,
159 (const gchar *)&pipe_data,
160 sizeof (X264_Pipe_Data),
162 if (status != G_IO_STATUS_NORMAL) {
163 g_print (_("Error ! %d %d %d\n"), status, (int)sizeof (X264_Pipe_Data), (int)size);
166 /* we force the GIOChannel to write to the pipeline */
167 status = g_io_channel_flush (thread_data->io_write,
169 if (status != G_IO_STATUS_NORMAL) {
170 g_print (_("Error ! %d\n"), status);
176 /* Flush delayed B-frames */
178 i_file += i_frame_size = x264_encode_frame (h, hout, NULL);
179 } while (i_frame_size);
181 i_end = x264_mdate ();
182 x264_picture_clean (&pic);
183 x264_encoder_close (h);
184 fprintf (stderr, "\n");
186 p_close_infile (hin);
187 p_close_outfile (hout);
190 double fps = (double)i_frame * (double)1000000 /
191 (double)(i_end - i_start);
193 fprintf (stderr, _("encoded %d frames, %.2f fps, %.2f kb/s\n"),
195 (double) i_file * 8 * param->i_fps_num /
196 ((double) param->i_fps_den * i_frame * 1000));
199 gtk_widget_set_sensitive (thread_data->end_button, TRUE);
200 gtk_widget_hide (thread_data->button);
205 x264_set_drivers (X264_Demuxer_Type in_container, gint out_container)
207 switch (in_container) {
208 case X264_DEMUXER_YUV:
209 case X264_DEMUXER_CIF:
210 case X264_DEMUXER_QCIF:
211 /* Default input file driver */
212 p_open_infile = open_file_yuv;
213 p_get_frame_total = get_frame_total_yuv;
214 p_read_frame = read_frame_yuv;
215 p_close_infile = close_file_yuv;
217 case X264_DEMUXER_Y4M:
218 p_open_infile = open_file_y4m;
219 p_get_frame_total = get_frame_total_y4m;
220 p_read_frame = read_frame_y4m;
221 p_close_infile = close_file_y4m;
224 case X264_DEMUXER_AVI:
225 case X264_DEMUXER_AVS:
226 p_open_infile = open_file_avis;
227 p_get_frame_total = get_frame_total_avis;
228 p_read_frame = read_frame_avis;
229 p_close_infile = close_file_avis;
232 default: /* Unknown */
236 switch (out_container) {
238 /* Raw ES output file driver */
239 p_open_outfile = open_file_bsf;
240 p_set_outfile_param = set_param_bsf;
241 p_write_nalu = write_nalu_bsf;
242 p_set_eop = set_eop_bsf;
243 p_close_outfile = close_file_bsf;
246 /* Matroska output file driver */
247 p_open_outfile = open_file_mkv;
248 p_set_outfile_param = set_param_mkv;
249 p_write_nalu = write_nalu_mkv;
250 p_set_eop = set_eop_mkv;
251 p_close_outfile = close_file_mkv;
255 p_open_outfile = open_file_mp4;
256 p_set_outfile_param = set_param_mp4;
257 p_write_nalu = write_nalu_mp4;
258 p_set_eop = set_eop_mp4;
259 p_close_outfile = close_file_mp4;
270 x264_encode_frame (x264_t *h, void *handle, x264_picture_t *pic)
272 x264_picture_t pic_out;
278 /* Do not force any parameters */
281 pic->i_type = X264_TYPE_AUTO;
284 if (x264_encoder_encode (h, &nal, &i_nal, pic, &pic_out) < 0)
286 fprintf (stderr, _("x264_encoder_encode failed\n"));
289 for (i = 0; i < i_nal; i++)
295 if ((i_size = x264_nal_encode (data, &i_data, 1, &nal[i])) > 0 )
297 i_file += p_write_nalu (handle, data, i_size);
301 fprintf (stderr, _("need to increase buffer size (size=%d)\n"), -i_size);
305 p_set_eop (handle, &pic_out);