]> git.sesse.net Git - x264/blob - gtk/x264_gtk_encode_encode.c
Update file headers throughout x264
[x264] / gtk / x264_gtk_encode_encode.c
1 /*****************************************************************************
2  * x264_gtk_encode_encode.c: h264 gtk encoder frontend
3  *****************************************************************************
4  * Copyright (C) 2006 Vincent Torri
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #include <string.h>
22 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __DragonFly__
23 #  include <inttypes.h>
24 #else
25 #  include <stdint.h>
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include "../x264.h"
31 #include "x264_gtk_i18n.h"
32 #include "x264_gtk_demuxers.h"
33 #include "x264_gtk_encode_private.h"
34
35
36 #define DATA_MAX 3000000
37 uint8_t data[DATA_MAX];
38
39 int64_t x264_mdate (void);
40
41 /* input interface */
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 );
46
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);
53
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);
56
57
58 gpointer
59 x264_gtk_encode_encode (X264_Thread_Data *thread_data)
60 {
61   GIOStatus       status;
62   gsize           size;
63   X264_Pipe_Data  pipe_data;
64   x264_param_t   *param;
65   x264_picture_t  pic;
66   x264_t         *h;
67   hnd_t           hin;
68   hnd_t           hout;
69   int             i_frame;
70   int             i_frame_total;
71   int64_t         i_start;
72   int64_t         i_end;
73   int64_t         i_file;
74   int             i_frame_size;
75   int             i_progress;
76   int             err;
77
78   g_print (_("encoding...\n"));
79   param = thread_data->param;
80   err = x264_set_drivers (thread_data->in_container, thread_data->out_container);
81   if (err < 0) {
82     GtkWidget *no_driver;
83     no_driver = gtk_message_dialog_new (GTK_WINDOW(thread_data->dialog),
84                                         GTK_DIALOG_DESTROY_WITH_PARENT,
85                                         GTK_MESSAGE_ERROR,
86                                         GTK_BUTTONS_CLOSE,
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);
91     return NULL;
92   }
93
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 );
96     return NULL;
97   }
98
99   p_open_outfile ((char *)thread_data->file_output, &hout);
100
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;
106
107   if ((h = x264_encoder_open (param)) == NULL)
108     {
109       fprintf (stderr, _("x264_encoder_open failed\n"));
110       p_close_infile (hin);
111       p_close_outfile (hout);
112       g_free (param);
113
114       return NULL;
115     }
116
117   if (p_set_outfile_param (hout, param))
118     {
119       fprintf (stderr, _("can't set outfile param\n"));
120       p_close_infile (hin);
121       p_close_outfile (hout);
122       g_free (param);
123
124       return NULL;
125     }
126
127   /* Create a new pic */
128   x264_picture_alloc (&pic, X264_CSP_I420, param->i_width, param->i_height );
129
130   i_start = x264_mdate();
131
132   /* Encode frames */
133   for (i_frame = 0, i_file = 0, i_progress = 0;
134        ((i_frame < i_frame_total) || (i_frame_total == 0)); )
135     {
136       if (p_read_frame (&pic, hin, i_frame))
137         break;
138
139       pic.i_pts = (int64_t)i_frame * param->i_fps_den;
140
141       i_file += x264_encode_frame (h, hout, &pic);
142
143       i_frame++;
144
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))
149         {
150           int64_t i_elapsed = x264_mdate () - i_start;
151
152           if (i_frame_total)
153             {
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),
161                                                  &size, NULL);
162               if (status != G_IO_STATUS_NORMAL) {
163                 g_print (_("Error ! %d %d %d\n"), status, (int)sizeof (X264_Pipe_Data), (int)size);
164               }
165               else {
166                 /* we force the GIOChannel to write to the pipeline */
167                 status = g_io_channel_flush (thread_data->io_write,
168                                              NULL);
169                 if (status != G_IO_STATUS_NORMAL) {
170                   g_print (_("Error ! %d\n"), status);
171                 }
172               }
173             }
174         }
175     }
176   /* Flush delayed B-frames */
177   do {
178     i_file += i_frame_size = x264_encode_frame (h, hout, NULL);
179   } while (i_frame_size);
180
181   i_end = x264_mdate ();
182   x264_picture_clean (&pic);
183   x264_encoder_close (h);
184   fprintf (stderr, "\n");
185
186   p_close_infile (hin);
187   p_close_outfile (hout);
188
189   if (i_frame > 0) {
190     double fps = (double)i_frame * (double)1000000 /
191       (double)(i_end - i_start);
192
193     fprintf (stderr, _("encoded %d frames, %.2f fps, %.2f kb/s\n"),
194              i_frame, fps,
195              (double) i_file * 8 * param->i_fps_num /
196              ((double) param->i_fps_den * i_frame * 1000));
197   }
198
199   gtk_widget_set_sensitive (thread_data->end_button, TRUE);
200   gtk_widget_hide (thread_data->button);
201   return NULL;
202 }
203
204 static int
205 x264_set_drivers (X264_Demuxer_Type in_container, gint out_container)
206 {
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;
216     break;
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;
222     break;
223 #ifdef AVIS_INPUT
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;
230     break;
231 #endif
232   default: /* Unknown */
233     return -1;
234   }
235
236   switch (out_container) {
237   case 0:
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;
244     break;
245   case 1:
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;
252     break;
253 #ifdef MP4_OUTPUT
254   case 2:
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;
260     break;
261 #endif
262   default:
263     return -2;
264   }
265
266   return 1;
267 }
268
269 static int
270 x264_encode_frame (x264_t *h, void *handle, x264_picture_t *pic)
271 {
272   x264_picture_t pic_out;
273   x264_nal_t    *nal;
274   int            i_nal;
275   int            i;
276   int            i_file = 0;
277
278   /* Do not force any parameters */
279   if (pic)
280     {
281       pic->i_type = X264_TYPE_AUTO;
282       pic->i_qpplus1 = 0;
283     }
284   if (x264_encoder_encode (h, &nal, &i_nal, pic, &pic_out) < 0)
285     {
286       fprintf (stderr, _("x264_encoder_encode failed\n"));
287     }
288
289   for (i = 0; i < i_nal; i++)
290     {
291       int i_size;
292       int i_data;
293
294       i_data = DATA_MAX;
295       if ((i_size = x264_nal_encode (data, &i_data, 1, &nal[i])) > 0 )
296         {
297           i_file += p_write_nalu (handle, data, i_size);
298         }
299       else if (i_size < 0)
300         {
301           fprintf (stderr, _("need to increase buffer size (size=%d)\n"), -i_size);
302         }
303     }
304   if (i_nal)
305     p_set_eop (handle, &pic_out);
306
307   return i_file;
308 }