54bd7a1c5059a4e8e8d96c4cede5324cee571650
[platform/upstream/gstreamer.git] / ext / x264 / gstx264enc.c
1 /* GStreamer H264 encoder plugin
2  * Copyright (C) 2005 Michal Benes <michal.benes@itonis.tv>
3  * Copyright (C) 2005 Josef Zlomek <josef.zlomek@itonis.tv>
4  * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
5  * Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:element-x264enc
25  * @title: x264enc
26  * @see_also: faac
27  *
28  * This element encodes raw video into H264 compressed data,
29  * also otherwise known as MPEG-4 AVC (Advanced Video Codec).
30  *
31  * The #GstX264Enc:pass property controls the type of encoding.  In case of Constant
32  * Bitrate Encoding (actually ABR), the #GstX264Enc:bitrate will determine the quality
33  * of the encoding.  This will similarly be the case if this target bitrate
34  * is to obtained in multiple (2 or 3) pass encoding.
35  * Alternatively, one may choose to perform Constant Quantizer or Quality encoding,
36  * in which case the #GstX264Enc:quantizer property controls much of the outcome, in that case #GstX264Enc:bitrate is the maximum bitrate.
37  *
38  * The H264 profile that is eventually used depends on a few settings.
39  * If #GstX264Enc:dct8x8 is enabled, then High profile is used.
40  * Otherwise, if #GstX264Enc:cabac entropy coding is enabled or #GstX264Enc:bframes
41  * are allowed, then Main Profile is in effect, and otherwise Baseline profile
42  * applies.  The high profile is imposed by default,
43  * which is fine for most software players and settings,
44  * but in some cases (e.g. hardware platforms) a more restricted profile/level
45  * may be necessary. The recommended way to set a profile is to set it in the
46  * downstream caps.
47  *
48  * If a preset/tuning are specified then these will define the default values and
49  * the property defaults will be ignored. After this the option-string property is
50  * applied, followed by the user-set properties, fast first pass restrictions and
51  * finally the profile restrictions.
52  *
53  * > Some settings, including the default settings, may lead to quite
54  * > some latency (i.e. frame buffering) in the encoder. This may cause problems
55  * > with pipeline stalling in non-trivial pipelines, because the encoder latency
56  * > is often considerably higher than the default size of a simple queue
57  * > element. Such problems are caused by one of the queues in the other
58  * > non-x264enc streams/branches filling up and blocking upstream. They can
59  * > be fixed by relaxing the default time/size/buffer limits on the queue
60  * > elements in the non-x264 branches, or using a (single) multiqueue element
61  * > for all branches. Also see the last example below. You can also work around
62  * > this problem by setting the tune=zerolatency property, but this will affect
63  * > overall encoding quality so may not be appropriate for your use case.
64  *
65  * ## Example pipeline
66  * |[
67  * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! x264enc qp-min=18 ! \
68  *   avimux ! filesink location=videotestsrc.avi
69  * ]| This example pipeline will encode a test video source to H264 muxed in an
70  * AVI container, while ensuring a sane minimum quantization factor to avoid
71  * some (excessive) waste. You should ideally never put H264 into an AVI
72  * container (or really anything else, for that matter) - use Matroska or
73  * MP4/QuickTime or MPEG-TS instead.
74  * |[
75  * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! x264enc pass=quant ! \
76  *   matroskamux ! filesink location=videotestsrc.mkv
77  * ]| This example pipeline will encode a test video source to H264 using fixed
78  * quantization, and muxes it in a Matroska container.
79  * |[
80  * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! x264enc pass=5 quantizer=25 speed-preset=6 ! video/x-h264, profile=baseline ! \
81  *   qtmux ! filesink location=videotestsrc.mov
82  * ]| This example pipeline will encode a test video source to H264 using
83  * constant quality at around Q25 using the 'medium' speed/quality preset and
84  * restricting the options used so that the output is H.264 Baseline Profile
85  * compliant and finally multiplexing the output in Quicktime mov format.
86  * |[
87  * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! tee name=t ! queue ! videoconvert ! autovideosink \
88  *   t. ! queue ! x264enc rc-lookahead=5 ! fakesink
89  * ]| This example pipeline will encode a test video source to H264 while
90  * displaying the input material at the same time.  As mentioned above,
91  * specific settings are needed in this case to avoid pipeline stalling.
92  * Depending on goals and context, other approaches are possible, e.g.
93  * tune=zerolatency might be configured, or queue sizes increased.
94  *
95  */
96
97 #ifdef HAVE_CONFIG_H
98 #  include "config.h"
99 #endif
100
101 #include "gstx264enc.h"
102
103 #include <gst/pbutils/pbutils.h>
104 #include <gst/video/video.h>
105 #include <gst/video/gstvideometa.h>
106 #include <gst/video/gstvideopool.h>
107
108 #include <string.h>
109 #include <stdlib.h>
110 #include <gmodule.h>
111
112 GST_DEBUG_CATEGORY_STATIC (x264_enc_debug);
113 #define GST_CAT_DEFAULT x264_enc_debug
114
115 struct _GstX264EncVTable
116 {
117   GModule *module;
118
119 #if X264_BUILD < 153
120   const int *x264_bit_depth;
121 #endif
122   const int *x264_chroma_format;
123   void (*x264_encoder_close) (x264_t *);
124   int (*x264_encoder_delayed_frames) (x264_t *);
125   int (*x264_encoder_encode) (x264_t *, x264_nal_t ** pp_nal, int *pi_nal,
126       x264_picture_t * pic_in, x264_picture_t * pic_out);
127   int (*x264_encoder_headers) (x264_t *, x264_nal_t ** pp_nal, int *pi_nal);
128   void (*x264_encoder_intra_refresh) (x264_t *);
129   int (*x264_encoder_maximum_delayed_frames) (x264_t *);
130   x264_t *(*x264_encoder_open) (x264_param_t *);
131   int (*x264_encoder_reconfig) (x264_t *, x264_param_t *);
132   const x264_level_t (*x264_levels)[];
133   void (*x264_param_apply_fastfirstpass) (x264_param_t *);
134   int (*x264_param_apply_profile) (x264_param_t *, const char *);
135   int (*x264_param_default_preset) (x264_param_t *, const char *preset,
136       const char *tune);
137   int (*x264_param_parse) (x264_param_t *, const char *name, const char *value);
138 };
139
140 static GstX264EncVTable default_vtable;
141
142 static GstX264EncVTable *vtable_8bit = NULL, *vtable_10bit = NULL;
143
144 #if X264_BUILD < 153
145 #define LOAD_SYMBOL(name) G_STMT_START { \
146   if (!g_module_symbol (module, #name, (gpointer *) &vtable->name)) { \
147     GST_ERROR ("Failed to load '" #name "' from '%s'", filename); \
148     goto error; \
149   } \
150 } G_STMT_END;
151
152 #ifdef HAVE_X264_ADDITIONAL_LIBRARIES
153 static GstX264EncVTable *
154 load_x264 (const gchar * filename)
155 {
156   GModule *module;
157   GstX264EncVTable *vtable;
158
159   module = g_module_open (filename, G_MODULE_BIND_LOCAL);
160   if (!module) {
161     GST_ERROR ("Failed to load '%s'", filename);
162     return NULL;
163   }
164
165   vtable = g_new0 (GstX264EncVTable, 1);
166   vtable->module = module;
167
168   if (!g_module_symbol (module, G_STRINGIFY (x264_encoder_open),
169           (gpointer *) & vtable->x264_encoder_open)) {
170     GST_ERROR ("Failed to load '" G_STRINGIFY (x264_encoder_open)
171         "' from '%s'. Incompatible version?", filename);
172     goto error;
173   }
174   LOAD_SYMBOL (x264_bit_depth);
175   LOAD_SYMBOL (x264_chroma_format);
176   LOAD_SYMBOL (x264_encoder_close);
177   LOAD_SYMBOL (x264_encoder_delayed_frames);
178   LOAD_SYMBOL (x264_encoder_encode);
179   LOAD_SYMBOL (x264_encoder_headers);
180   LOAD_SYMBOL (x264_encoder_intra_refresh);
181   LOAD_SYMBOL (x264_encoder_maximum_delayed_frames);
182   LOAD_SYMBOL (x264_encoder_reconfig);
183   LOAD_SYMBOL (x264_levels);
184   LOAD_SYMBOL (x264_param_apply_fastfirstpass);
185   LOAD_SYMBOL (x264_param_apply_profile);
186   LOAD_SYMBOL (x264_param_default_preset);
187   LOAD_SYMBOL (x264_param_parse);
188
189   return vtable;
190
191 error:
192   g_module_close (vtable->module);
193   g_free (vtable);
194   return NULL;
195 }
196
197 static void
198 unload_x264 (GstX264EncVTable * vtable)
199 {
200   if (vtable->module) {
201     g_module_close (vtable->module);
202     g_free (vtable);
203   }
204 }
205 #endif
206
207 #undef LOAD_SYMBOL
208 #endif
209
210 static gboolean
211 gst_x264_enc_add_x264_chroma_format (GstStructure * s,
212     gboolean allow_420, gboolean allow_422, gboolean allow_444)
213 {
214   GValue fmts = G_VALUE_INIT;
215   GValue fmt = G_VALUE_INIT;
216   gboolean ret = FALSE;
217
218   g_value_init (&fmts, GST_TYPE_LIST);
219   g_value_init (&fmt, G_TYPE_STRING);
220
221   if (vtable_8bit) {
222     gint chroma_format = *vtable_8bit->x264_chroma_format;
223
224     GST_INFO ("8-bit depth supported");
225
226     if ((chroma_format == 0 || chroma_format == X264_CSP_I444) && allow_444) {
227       g_value_set_string (&fmt, "Y444");
228       gst_value_list_append_value (&fmts, &fmt);
229     }
230
231     if ((chroma_format == 0 || chroma_format == X264_CSP_I422) && allow_422) {
232       g_value_set_string (&fmt, "Y42B");
233       gst_value_list_append_value (&fmts, &fmt);
234     }
235
236     if ((chroma_format == 0 || chroma_format == X264_CSP_I420) && allow_420) {
237       g_value_set_string (&fmt, "I420");
238       gst_value_list_append_value (&fmts, &fmt);
239       g_value_set_string (&fmt, "YV12");
240       gst_value_list_append_value (&fmts, &fmt);
241       g_value_set_string (&fmt, "NV12");
242       gst_value_list_append_value (&fmts, &fmt);
243     }
244   }
245
246   if (vtable_10bit) {
247     gint chroma_format = *vtable_10bit->x264_chroma_format;
248
249     GST_INFO ("10-bit depth supported");
250
251     if ((chroma_format == 0 || chroma_format == X264_CSP_I444) && allow_444) {
252       if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
253         g_value_set_string (&fmt, "Y444_10LE");
254       else
255         g_value_set_string (&fmt, "Y444_10BE");
256
257       gst_value_list_append_value (&fmts, &fmt);
258     }
259
260     if ((chroma_format == 0 || chroma_format == X264_CSP_I422) && allow_422) {
261       if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
262         g_value_set_string (&fmt, "I422_10LE");
263       else
264         g_value_set_string (&fmt, "I422_10BE");
265
266       gst_value_list_append_value (&fmts, &fmt);
267     }
268
269     if ((chroma_format == 0 || chroma_format == X264_CSP_I420) && allow_420) {
270       if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
271         g_value_set_string (&fmt, "I420_10LE");
272       else
273         g_value_set_string (&fmt, "I420_10BE");
274
275       gst_value_list_append_value (&fmts, &fmt);
276     }
277   }
278
279   if (gst_value_list_get_size (&fmts) != 0) {
280     gst_structure_take_value (s, "format", &fmts);
281     ret = TRUE;
282   } else {
283     g_value_unset (&fmts);
284   }
285
286   g_value_unset (&fmt);
287
288   return ret;
289 }
290
291 #if X264_BUILD < 153
292 static gboolean
293 load_x264_libraries (void)
294 {
295   if (*default_vtable.x264_bit_depth == 8) {
296     vtable_8bit = &default_vtable;
297   } else if (*default_vtable.x264_bit_depth == 10) {
298     vtable_10bit = &default_vtable;
299   }
300 #ifdef HAVE_X264_ADDITIONAL_LIBRARIES
301   {
302     gchar **libraries = g_strsplit (HAVE_X264_ADDITIONAL_LIBRARIES, ":", -1);
303     gchar **p = libraries;
304
305     while (*p && (!vtable_8bit || !vtable_10bit)) {
306       GstX264EncVTable *vtable = load_x264 (*p);
307
308       if (vtable) {
309         if (!vtable_8bit && *vtable->x264_bit_depth == 8) {
310           vtable_8bit = vtable;
311         } else if (!vtable_10bit && *vtable->x264_bit_depth == 10) {
312           vtable_10bit = vtable;
313         } else {
314           unload_x264 (vtable);
315         }
316       }
317
318       p++;
319     }
320     g_strfreev (libraries);
321   }
322 #endif
323
324   if (!vtable_8bit && !vtable_10bit)
325     return FALSE;
326
327   return TRUE;
328 }
329
330 #else /* X264_BUILD >= 153 */
331
332 static gboolean
333 load_x264_libraries (void)
334 {
335 #if X264_BIT_DEPTH == 0         /* all */
336   vtable_8bit = &default_vtable;
337   vtable_10bit = &default_vtable;
338 #elif X264_BIT_DEPTH == 8
339   vtable_8bit = &default_vtable;
340 #elif X264_BIT_DEPTH == 10
341   vtable_10bit = &default_vtable;
342 #else
343 #error "unexpected X264_BIT_DEPTH value"
344 #endif
345
346 #ifdef HAVE_X264_ADDITIONAL_LIBRARIES
347   GST_WARNING ("Ignoring configured additional libraries %s, using libx264 "
348       "version enabled for multiple bit depths",
349       HAVE_X264_ADDITIONAL_LIBRARIES);
350 #endif
351
352   return TRUE;
353 }
354
355 #endif
356
357 enum
358 {
359   ARG_0,
360   ARG_THREADS,
361   ARG_SLICED_THREADS,
362   ARG_SYNC_LOOKAHEAD,
363   ARG_PASS,
364   ARG_QUANTIZER,
365   ARG_MULTIPASS_CACHE_FILE,
366   ARG_BYTE_STREAM,
367   ARG_BITRATE,
368   ARG_INTRA_REFRESH,
369   ARG_VBV_BUF_CAPACITY,
370   ARG_ME,
371   ARG_SUBME,
372   ARG_ANALYSE,
373   ARG_DCT8x8,
374   ARG_REF,
375   ARG_BFRAMES,
376   ARG_B_ADAPT,
377   ARG_B_PYRAMID,
378   ARG_WEIGHTB,
379   ARG_SPS_ID,
380   ARG_AU_NALU,
381   ARG_TRELLIS,
382   ARG_KEYINT_MAX,
383   ARG_CABAC,
384   ARG_QP_MIN,
385   ARG_QP_MAX,
386   ARG_QP_STEP,
387   ARG_IP_FACTOR,
388   ARG_PB_FACTOR,
389   ARG_RC_MB_TREE,
390   ARG_RC_LOOKAHEAD,
391   ARG_NR,
392   ARG_INTERLACED,
393   ARG_OPTION_STRING,
394   ARG_SPEED_PRESET,
395   ARG_PSY_TUNE,
396   ARG_TUNE,
397   ARG_FRAME_PACKING,
398   ARG_INSERT_VUI,
399 };
400
401 #define ARG_THREADS_DEFAULT            0        /* 0 means 'auto' which is 1.5x number of CPU cores */
402 #define ARG_PASS_DEFAULT               0
403 #define ARG_QUANTIZER_DEFAULT          21
404 #define ARG_MULTIPASS_CACHE_FILE_DEFAULT "x264.log"
405 #define ARG_BYTE_STREAM_DEFAULT        FALSE
406 #define ARG_BITRATE_DEFAULT            (2 * 1024)
407 #define ARG_VBV_BUF_CAPACITY_DEFAULT   600
408 #define ARG_ME_DEFAULT                 X264_ME_HEX
409 #define ARG_SUBME_DEFAULT              1
410 #define ARG_ANALYSE_DEFAULT            0
411 #define ARG_DCT8x8_DEFAULT             FALSE
412 #define ARG_REF_DEFAULT                1
413 #define ARG_BFRAMES_DEFAULT            0
414 #define ARG_B_ADAPT_DEFAULT            TRUE
415 #define ARG_B_PYRAMID_DEFAULT          FALSE
416 #define ARG_WEIGHTB_DEFAULT            FALSE
417 #define ARG_SPS_ID_DEFAULT             0
418 #define ARG_AU_NALU_DEFAULT            TRUE
419 #define ARG_TRELLIS_DEFAULT            TRUE
420 #define ARG_KEYINT_MAX_DEFAULT         0
421 #define ARG_CABAC_DEFAULT              TRUE
422 #define ARG_QP_MIN_DEFAULT             10
423 #define ARG_QP_MAX_DEFAULT             51
424 #define ARG_QP_STEP_DEFAULT            4
425 #define ARG_IP_FACTOR_DEFAULT          1.4
426 #define ARG_PB_FACTOR_DEFAULT          1.3
427 #define ARG_NR_DEFAULT                 0
428 #define ARG_INTERLACED_DEFAULT         FALSE
429 #define ARG_SLICED_THREADS_DEFAULT     FALSE
430 #define ARG_SYNC_LOOKAHEAD_DEFAULT     -1
431 #define ARG_RC_MB_TREE_DEFAULT         TRUE
432 #define ARG_RC_LOOKAHEAD_DEFAULT       40
433 #define ARG_INTRA_REFRESH_DEFAULT      FALSE
434 #define ARG_OPTION_STRING_DEFAULT      ""
435 static GString *x264enc_defaults;
436 #define ARG_SPEED_PRESET_DEFAULT       6        /* 'medium' preset - matches x264 CLI default */
437 #define ARG_PSY_TUNE_DEFAULT           0        /* no psy tuning */
438 #define ARG_TUNE_DEFAULT               0        /* no tuning */
439 #define ARG_FRAME_PACKING_DEFAULT      -1       /* automatic (none, or from input caps) */
440 #define ARG_INSERT_VUI_DEFAULT         TRUE
441
442 enum
443 {
444   GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY,
445   GST_X264_ENC_STREAM_FORMAT_AVC,
446   GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM
447 };
448
449 enum
450 {
451   GST_X264_ENC_PASS_CBR = 0,
452   GST_X264_ENC_PASS_QUANT = 0x04,
453   GST_X264_ENC_PASS_QUAL,
454   GST_X264_ENC_PASS_PASS1 = 0x11,
455   GST_X264_ENC_PASS_PASS2,
456   GST_X264_ENC_PASS_PASS3
457 };
458
459 #define GST_X264_ENC_PASS_TYPE (gst_x264_enc_pass_get_type())
460 static GType
461 gst_x264_enc_pass_get_type (void)
462 {
463   static GType pass_type = 0;
464
465   static const GEnumValue pass_types[] = {
466     {GST_X264_ENC_PASS_CBR, "Constant Bitrate Encoding", "cbr"},
467     {GST_X264_ENC_PASS_QUANT, "Constant Quantizer", "quant"},
468     {GST_X264_ENC_PASS_QUAL, "Constant Quality", "qual"},
469     {GST_X264_ENC_PASS_PASS1, "VBR Encoding - Pass 1", "pass1"},
470     {GST_X264_ENC_PASS_PASS2, "VBR Encoding - Pass 2", "pass2"},
471     {GST_X264_ENC_PASS_PASS3, "VBR Encoding - Pass 3", "pass3"},
472     {0, NULL, NULL}
473   };
474
475   if (!pass_type) {
476     pass_type = g_enum_register_static ("GstX264EncPass", pass_types);
477   }
478   return pass_type;
479 }
480
481 #define GST_X264_ENC_ME_TYPE (gst_x264_enc_me_get_type())
482 static GType
483 gst_x264_enc_me_get_type (void)
484 {
485   static GType me_type = 0;
486   static GEnumValue *me_types;
487   int n, i;
488
489   if (me_type != 0)
490     return me_type;
491
492   n = 0;
493   while (x264_motion_est_names[n] != NULL)
494     n++;
495
496   me_types = g_new0 (GEnumValue, n + 1);
497
498   for (i = 0; i < n; i++) {
499     me_types[i].value = i;
500     me_types[i].value_name = x264_motion_est_names[i];
501     me_types[i].value_nick = x264_motion_est_names[i];
502   }
503
504   me_type = g_enum_register_static ("GstX264EncMe", me_types);
505
506   return me_type;
507 }
508
509 #define GST_X264_ENC_ANALYSE_TYPE (gst_x264_enc_analyse_get_type())
510 static GType
511 gst_x264_enc_analyse_get_type (void)
512 {
513   static GType analyse_type = 0;
514   static const GFlagsValue analyse_types[] = {
515     {X264_ANALYSE_I4x4, "i4x4", "i4x4"},
516     {X264_ANALYSE_I8x8, "i8x8", "i8x8"},
517     {X264_ANALYSE_PSUB16x16, "p8x8", "p8x8"},
518     {X264_ANALYSE_PSUB8x8, "p4x4", "p4x4"},
519     {X264_ANALYSE_BSUB16x16, "b8x8", "b8x8"},
520     {0, NULL, NULL},
521   };
522
523   if (!analyse_type) {
524     analyse_type = g_flags_register_static ("GstX264EncAnalyse", analyse_types);
525   }
526   return analyse_type;
527 }
528
529 #define GST_X264_ENC_SPEED_PRESET_TYPE (gst_x264_enc_speed_preset_get_type())
530 static GType
531 gst_x264_enc_speed_preset_get_type (void)
532 {
533   static GType speed_preset_type = 0;
534   static GEnumValue *speed_preset_types;
535   int n, i;
536
537   if (speed_preset_type != 0)
538     return speed_preset_type;
539
540   n = 0;
541   while (x264_preset_names[n] != NULL)
542     n++;
543
544   speed_preset_types = g_new0 (GEnumValue, n + 2);
545
546   speed_preset_types[0].value = 0;
547   speed_preset_types[0].value_name = "No preset";
548   speed_preset_types[0].value_nick = "None";
549
550   for (i = 1; i <= n; i++) {
551     speed_preset_types[i].value = i;
552     speed_preset_types[i].value_name = x264_preset_names[i - 1];
553     speed_preset_types[i].value_nick = x264_preset_names[i - 1];
554   }
555
556   speed_preset_type =
557       g_enum_register_static ("GstX264EncPreset", speed_preset_types);
558
559   return speed_preset_type;
560 }
561
562 static const GFlagsValue tune_types[] = {
563   {0x0, "No tuning", "none"},
564   {0x1, "Still image", "stillimage"},
565   {0x2, "Fast decode", "fastdecode"},
566   {0x4, "Zero latency", "zerolatency"},
567   {0, NULL, NULL},
568 };
569
570 #define GST_X264_ENC_TUNE_TYPE (gst_x264_enc_tune_get_type())
571 static GType
572 gst_x264_enc_tune_get_type (void)
573 {
574   static GType tune_type = 0;
575
576   if (!tune_type) {
577     tune_type = g_flags_register_static ("GstX264EncTune", tune_types + 1);
578   }
579   return tune_type;
580 }
581
582 enum
583 {
584   GST_X264_ENC_TUNE_NONE,
585   GST_X264_ENC_TUNE_FILM,
586   GST_X264_ENC_TUNE_ANIMATION,
587   GST_X264_ENC_TUNE_GRAIN,
588   GST_X264_ENC_TUNE_PSNR,
589   GST_X264_ENC_TUNE_SSIM,
590   GST_X264_ENC_TUNE_LAST
591 };
592
593 static const GEnumValue psy_tune_types[] = {
594   {GST_X264_ENC_TUNE_NONE, "No tuning", "none"},
595   {GST_X264_ENC_TUNE_FILM, "Film", "film"},
596   {GST_X264_ENC_TUNE_ANIMATION, "Animation", "animation"},
597   {GST_X264_ENC_TUNE_GRAIN, "Grain", "grain"},
598   {GST_X264_ENC_TUNE_PSNR, "PSNR", "psnr"},
599   {GST_X264_ENC_TUNE_SSIM, "SSIM", "ssim"},
600   {0, NULL, NULL},
601 };
602
603 #define GST_X264_ENC_PSY_TUNE_TYPE (gst_x264_enc_psy_tune_get_type())
604 static GType
605 gst_x264_enc_psy_tune_get_type (void)
606 {
607   static GType psy_tune_type = 0;
608
609   if (!psy_tune_type) {
610     psy_tune_type =
611         g_enum_register_static ("GstX264EncPsyTune", psy_tune_types);
612   }
613   return psy_tune_type;
614 }
615
616 static void
617 gst_x264_enc_build_tunings_string (GstX264Enc * x264enc)
618 {
619   int i = 1;
620
621   if (x264enc->tunings)
622     g_string_free (x264enc->tunings, TRUE);
623
624   if (x264enc->psy_tune) {
625     x264enc->tunings =
626         g_string_new (psy_tune_types[x264enc->psy_tune].value_nick);
627   } else {
628     x264enc->tunings = g_string_new (NULL);
629   }
630
631   while (tune_types[i].value_name) {
632     if (x264enc->tune & (1 << (i - 1)))
633       g_string_append_printf (x264enc->tunings, "%s%s",
634           x264enc->tunings->len ? "," : "", tune_types[i].value_nick);
635     i++;
636   }
637
638   if (x264enc->tunings->len)
639     GST_DEBUG_OBJECT (x264enc, "Constructed tunings string: %s",
640         x264enc->tunings->str);
641 }
642
643 #define GST_X264_ENC_FRAME_PACKING_TYPE (gst_x264_enc_frame_packing_get_type())
644 static GType
645 gst_x264_enc_frame_packing_get_type (void)
646 {
647   static GType fpa_type = 0;
648
649   static const GEnumValue fpa_types[] = {
650     {-1, "Automatic (use incoming video information)", "auto"},
651     {0, "checkerboard - Left and Right pixels alternate in a checkerboard pattern", "checkerboard"},
652     {1, "column interleaved - Alternating pixel columns represent Left and Right views", "column-interleaved"},
653     {2, "row interleaved - Alternating pixel rows represent Left and Right views", "row-interleaved"},
654     {3, "side by side - The left half of the frame contains the Left eye view, the right half the Right eye view", "side-by-side"},
655     {4, "top bottom - L is on top, R on bottom", "top-bottom"},
656     {5, "frame interleaved - Each frame contains either Left or Right view alternately", "frame-interleaved"},
657     {0, NULL, NULL}
658   };
659
660   if (!fpa_type) {
661     fpa_type = g_enum_register_static ("GstX264EncFramePacking", fpa_types);
662   }
663   return fpa_type;
664 }
665
666 static gint
667 gst_x264_enc_mview_mode_to_frame_packing (GstVideoMultiviewMode mode)
668 {
669   switch (mode) {
670     case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
671       return 0;
672     case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
673       return 1;
674     case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
675       return 2;
676     case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
677       return 3;
678     case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
679       return 4;
680     case GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME:
681       return 5;
682     default:
683       break;
684   }
685
686   return -1;
687 }
688
689 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
690     GST_PAD_SRC,
691     GST_PAD_ALWAYS,
692     GST_STATIC_CAPS ("video/x-h264, "
693         "framerate = (fraction) [0/1, MAX], "
694         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
695         "stream-format = (string) { avc, byte-stream }, "
696         "alignment = (string) au, "
697         "profile = (string) { high-4:4:4, high-4:2:2, high-10, high, main,"
698         " baseline, constrained-baseline, high-4:4:4-intra, high-4:2:2-intra,"
699         " high-10-intra }")
700     );
701
702 static void gst_x264_enc_finalize (GObject * object);
703 static gboolean gst_x264_enc_start (GstVideoEncoder * encoder);
704 static gboolean gst_x264_enc_stop (GstVideoEncoder * encoder);
705 static gboolean gst_x264_enc_flush (GstVideoEncoder * encoder);
706
707 static gboolean gst_x264_enc_init_encoder (GstX264Enc * encoder);
708 static void gst_x264_enc_close_encoder (GstX264Enc * encoder);
709
710 static GstFlowReturn gst_x264_enc_finish (GstVideoEncoder * encoder);
711 static GstFlowReturn gst_x264_enc_handle_frame (GstVideoEncoder * encoder,
712     GstVideoCodecFrame * frame);
713 static void gst_x264_enc_flush_frames (GstX264Enc * encoder, gboolean send);
714 static GstFlowReturn gst_x264_enc_encode_frame (GstX264Enc * encoder,
715     x264_picture_t * pic_in, GstVideoCodecFrame * input_frame, int *i_nal,
716     gboolean send);
717 static gboolean gst_x264_enc_set_format (GstVideoEncoder * video_enc,
718     GstVideoCodecState * state);
719 static gboolean gst_x264_enc_propose_allocation (GstVideoEncoder * encoder,
720     GstQuery * query);
721
722 static void gst_x264_enc_set_property (GObject * object, guint prop_id,
723     const GValue * value, GParamSpec * pspec);
724 static void gst_x264_enc_get_property (GObject * object, guint prop_id,
725     GValue * value, GParamSpec * pspec);
726
727 #define gst_x264_enc_parent_class parent_class
728 G_DEFINE_TYPE_WITH_CODE (GstX264Enc, gst_x264_enc, GST_TYPE_VIDEO_ENCODER,
729     G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
730
731 /* don't forget to free the string after use */
732 static const gchar *
733 gst_x264_enc_build_partitions (gint analyse)
734 {
735   GString *string;
736
737   if (!analyse)
738     return NULL;
739
740   string = g_string_new (NULL);
741   if (analyse & X264_ANALYSE_I4x4)
742     g_string_append (string, "i4x4");
743   if (analyse & X264_ANALYSE_I8x8)
744     g_string_append (string, ",i8x8");
745   if (analyse & X264_ANALYSE_PSUB16x16)
746     g_string_append (string, ",p8x8");
747   if (analyse & X264_ANALYSE_PSUB8x8)
748     g_string_append (string, ",p4x4");
749   if (analyse & X264_ANALYSE_BSUB16x16)
750     g_string_append (string, ",b8x8");
751
752   return (const gchar *) g_string_free (string, FALSE);
753 }
754
755 static void
756 check_formats (const gchar * str, gboolean * has_420, gboolean * has_422,
757     gboolean * has_444)
758 {
759   if (g_str_has_prefix (str, "high-4:4:4"))
760     *has_444 = TRUE;
761   else if (g_str_has_prefix (str, "high-4:2:2"))
762     *has_422 = TRUE;
763   else
764     *has_420 = TRUE;
765 }
766
767
768 /* allowed input caps depending on whether libx264 was built for 8 or 10 bits */
769 static GstCaps *
770 gst_x264_enc_sink_getcaps (GstVideoEncoder * enc, GstCaps * filter)
771 {
772   GstCaps *supported_incaps;
773   GstCaps *allowed;
774   GstCaps *filter_caps, *fcaps;
775   gint i, j, k;
776
777   supported_incaps =
778       gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SINK_PAD (enc));
779
780   /* Allow downstream to specify width/height/framerate/PAR constraints
781    * and forward them upstream for video converters to handle
782    */
783   allowed = gst_pad_get_allowed_caps (enc->srcpad);
784
785   if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
786     fcaps = supported_incaps;
787     goto done;
788   }
789
790   GST_LOG_OBJECT (enc, "template caps %" GST_PTR_FORMAT, supported_incaps);
791   GST_LOG_OBJECT (enc, "allowed caps %" GST_PTR_FORMAT, allowed);
792
793   filter_caps = gst_caps_new_empty ();
794
795   for (i = 0; i < gst_caps_get_size (supported_incaps); i++) {
796     GQuark q_name =
797         gst_structure_get_name_id (gst_caps_get_structure (supported_incaps,
798             i));
799
800     for (j = 0; j < gst_caps_get_size (allowed); j++) {
801       const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
802       const GValue *val;
803       GstStructure *s;
804
805       s = gst_structure_new_id_empty (q_name);
806       if ((val = gst_structure_get_value (allowed_s, "width")))
807         gst_structure_set_value (s, "width", val);
808       if ((val = gst_structure_get_value (allowed_s, "height")))
809         gst_structure_set_value (s, "height", val);
810       if ((val = gst_structure_get_value (allowed_s, "framerate")))
811         gst_structure_set_value (s, "framerate", val);
812       if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
813         gst_structure_set_value (s, "pixel-aspect-ratio", val);
814
815       if ((val = gst_structure_get_value (allowed_s, "profile"))) {
816         gboolean has_420 = FALSE;
817         gboolean has_422 = FALSE;
818         gboolean has_444 = FALSE;
819
820         if (G_VALUE_HOLDS_STRING (val)) {
821           check_formats (g_value_get_string (val), &has_420, &has_422,
822               &has_444);
823         } else if (GST_VALUE_HOLDS_LIST (val)) {
824           for (k = 0; k < gst_value_list_get_size (val); k++) {
825             const GValue *vlist = gst_value_list_get_value (val, k);
826
827             if (G_VALUE_HOLDS_STRING (vlist))
828               check_formats (g_value_get_string (vlist), &has_420, &has_422,
829                   &has_444);
830           }
831         }
832
833         gst_x264_enc_add_x264_chroma_format (s, has_420, has_422, has_444);
834       }
835
836       filter_caps = gst_caps_merge_structure (filter_caps, s);
837     }
838   }
839
840   fcaps = gst_caps_intersect (filter_caps, supported_incaps);
841   gst_caps_unref (filter_caps);
842   gst_caps_unref (supported_incaps);
843
844   if (filter) {
845     GST_LOG_OBJECT (enc, "intersecting with %" GST_PTR_FORMAT, filter);
846     filter_caps = gst_caps_intersect (fcaps, filter);
847     gst_caps_unref (fcaps);
848     fcaps = filter_caps;
849   }
850
851 done:
852   gst_caps_replace (&allowed, NULL);
853
854   GST_LOG_OBJECT (enc, "proxy caps %" GST_PTR_FORMAT, fcaps);
855
856   return fcaps;
857 }
858
859 static gboolean
860 gst_x264_enc_sink_query (GstVideoEncoder * enc, GstQuery * query)
861 {
862   GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (enc);
863   gboolean ret = FALSE;
864
865   GST_DEBUG ("Received %s query on sinkpad, %" GST_PTR_FORMAT,
866       GST_QUERY_TYPE_NAME (query), query);
867
868   switch (GST_QUERY_TYPE (query)) {
869     case GST_QUERY_ACCEPT_CAPS:{
870       GstCaps *acceptable, *caps;
871
872       acceptable = gst_pad_get_pad_template_caps (pad);
873
874       gst_query_parse_accept_caps (query, &caps);
875
876       gst_query_set_accept_caps_result (query,
877           gst_caps_is_subset (caps, acceptable));
878       gst_caps_unref (acceptable);
879       ret = TRUE;
880     }
881       break;
882     default:
883       ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (enc, query);
884       break;
885   }
886
887   return ret;
888 }
889
890 static void
891 gst_x264_enc_class_init (GstX264EncClass * klass)
892 {
893   GObjectClass *gobject_class;
894   GstElementClass *element_class;
895   GstVideoEncoderClass *gstencoder_class;
896   const gchar *partitions = NULL;
897   GstPadTemplate *sink_templ;
898   GstCaps *supported_sinkcaps;
899
900   x264enc_defaults = g_string_new ("");
901
902   gobject_class = G_OBJECT_CLASS (klass);
903   element_class = GST_ELEMENT_CLASS (klass);
904   gstencoder_class = GST_VIDEO_ENCODER_CLASS (klass);
905
906   gobject_class->set_property = gst_x264_enc_set_property;
907   gobject_class->get_property = gst_x264_enc_get_property;
908   gobject_class->finalize = gst_x264_enc_finalize;
909
910   gstencoder_class->set_format = GST_DEBUG_FUNCPTR (gst_x264_enc_set_format);
911   gstencoder_class->handle_frame =
912       GST_DEBUG_FUNCPTR (gst_x264_enc_handle_frame);
913   gstencoder_class->start = GST_DEBUG_FUNCPTR (gst_x264_enc_start);
914   gstencoder_class->stop = GST_DEBUG_FUNCPTR (gst_x264_enc_stop);
915   gstencoder_class->flush = GST_DEBUG_FUNCPTR (gst_x264_enc_flush);
916   gstencoder_class->finish = GST_DEBUG_FUNCPTR (gst_x264_enc_finish);
917   gstencoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_x264_enc_sink_getcaps);
918   gstencoder_class->propose_allocation =
919       GST_DEBUG_FUNCPTR (gst_x264_enc_propose_allocation);
920   gstencoder_class->sink_query = GST_DEBUG_FUNCPTR (gst_x264_enc_sink_query);
921
922   /* options for which we don't use string equivalents */
923   g_object_class_install_property (gobject_class, ARG_PASS,
924       g_param_spec_enum ("pass", "Encoding pass/type",
925           "Encoding pass/type", GST_X264_ENC_PASS_TYPE,
926           ARG_PASS_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
927   g_object_class_install_property (gobject_class, ARG_QUANTIZER,
928       g_param_spec_uint ("quantizer", "Constant Quantizer",
929           "Constant quantizer or quality to apply",
930           0, 50, ARG_QUANTIZER_DEFAULT,
931           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
932   g_object_class_install_property (gobject_class, ARG_BITRATE,
933       g_param_spec_uint ("bitrate", "Bitrate", "Bitrate in kbit/sec", 1,
934           2000 * 1024, ARG_BITRATE_DEFAULT,
935           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
936           GST_PARAM_MUTABLE_PLAYING));
937   g_object_class_install_property (gobject_class, ARG_VBV_BUF_CAPACITY,
938       g_param_spec_uint ("vbv-buf-capacity", "VBV buffer capacity",
939           "Size of the VBV buffer in milliseconds",
940           0, 10000, ARG_VBV_BUF_CAPACITY_DEFAULT,
941           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
942           GST_PARAM_MUTABLE_PLAYING));
943   g_object_class_install_property (gobject_class, ARG_SPEED_PRESET,
944       g_param_spec_enum ("speed-preset", "Speed/quality preset",
945           "Preset name for speed/quality tradeoff options (can affect decode "
946           "compatibility - impose restrictions separately for your target decoder)",
947           GST_X264_ENC_SPEED_PRESET_TYPE, ARG_SPEED_PRESET_DEFAULT,
948           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
949   g_object_class_install_property (gobject_class, ARG_PSY_TUNE,
950       g_param_spec_enum ("psy-tune", "Psychovisual tuning preset",
951           "Preset name for psychovisual tuning options",
952           GST_X264_ENC_PSY_TUNE_TYPE, ARG_PSY_TUNE_DEFAULT,
953           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
954   g_object_class_install_property (gobject_class, ARG_TUNE,
955       g_param_spec_flags ("tune", "Content tuning preset",
956           "Preset name for non-psychovisual tuning options",
957           GST_X264_ENC_TUNE_TYPE, ARG_TUNE_DEFAULT,
958           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
959   g_object_class_install_property (gobject_class, ARG_OPTION_STRING,
960       g_param_spec_string ("option-string", "Option string",
961           "String of x264 options (overridden by element properties)"
962           " in the format \"key1=value1:key2=value2\".",
963           ARG_OPTION_STRING_DEFAULT,
964           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
965
966   g_object_class_install_property (gobject_class, ARG_FRAME_PACKING,
967       g_param_spec_enum ("frame-packing", "Frame Packing",
968           "Set frame packing mode for Stereoscopic content",
969           GST_X264_ENC_FRAME_PACKING_TYPE, ARG_FRAME_PACKING_DEFAULT,
970           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
971
972   g_object_class_install_property (gobject_class, ARG_INSERT_VUI,
973       g_param_spec_boolean ("insert-vui", "Insert VUI",
974           "Insert VUI NAL in stream",
975           ARG_INSERT_VUI_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
976
977   /* options for which we _do_ use string equivalents */
978   g_object_class_install_property (gobject_class, ARG_THREADS,
979       g_param_spec_uint ("threads", "Threads",
980           "Number of threads used by the codec (0 for automatic)",
981           0, G_MAXINT, ARG_THREADS_DEFAULT,
982           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
983   /* NOTE: this first string append doesn't require the ':' delimiter but the
984    * rest do */
985   g_string_append_printf (x264enc_defaults, "threads=%d", ARG_THREADS_DEFAULT);
986   g_object_class_install_property (gobject_class, ARG_SLICED_THREADS,
987       g_param_spec_boolean ("sliced-threads", "Sliced Threads",
988           "Low latency but lower efficiency threading",
989           ARG_SLICED_THREADS_DEFAULT,
990           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
991   g_string_append_printf (x264enc_defaults, ":sliced-threads=%d",
992       ARG_SLICED_THREADS_DEFAULT);
993   g_object_class_install_property (gobject_class, ARG_SYNC_LOOKAHEAD,
994       g_param_spec_int ("sync-lookahead", "Sync Lookahead",
995           "Number of buffer frames for threaded lookahead (-1 for automatic)",
996           -1, 250, ARG_SYNC_LOOKAHEAD_DEFAULT,
997           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
998   g_string_append_printf (x264enc_defaults, ":sync-lookahead=%d",
999       ARG_SYNC_LOOKAHEAD_DEFAULT);
1000   g_object_class_install_property (gobject_class, ARG_MULTIPASS_CACHE_FILE,
1001       g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
1002           "Filename for multipass cache file",
1003           ARG_MULTIPASS_CACHE_FILE_DEFAULT,
1004           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1005   g_string_append_printf (x264enc_defaults, ":stats=%s",
1006       ARG_MULTIPASS_CACHE_FILE_DEFAULT);
1007   g_object_class_install_property (gobject_class, ARG_BYTE_STREAM,
1008       g_param_spec_boolean ("byte-stream", "Byte Stream",
1009           "Generate byte stream format of NALU", ARG_BYTE_STREAM_DEFAULT,
1010           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1011   g_string_append_printf (x264enc_defaults, ":annexb=%d",
1012       ARG_BYTE_STREAM_DEFAULT);
1013   g_object_class_install_property (gobject_class, ARG_INTRA_REFRESH,
1014       g_param_spec_boolean ("intra-refresh", "Intra Refresh",
1015           "Use Periodic Intra Refresh instead of IDR frames",
1016           ARG_INTRA_REFRESH_DEFAULT,
1017           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1018   g_string_append_printf (x264enc_defaults, ":intra-refresh=%d",
1019       ARG_INTRA_REFRESH_DEFAULT);
1020   g_object_class_install_property (gobject_class, ARG_ME,
1021       g_param_spec_enum ("me", "Motion Estimation",
1022           "Integer pixel motion estimation method", GST_X264_ENC_ME_TYPE,
1023           ARG_ME_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1024   g_string_append_printf (x264enc_defaults, ":me=%s",
1025       x264_motion_est_names[ARG_ME_DEFAULT]);
1026   g_object_class_install_property (gobject_class, ARG_SUBME,
1027       g_param_spec_uint ("subme", "Subpixel Motion Estimation",
1028           "Subpixel motion estimation and partition decision quality: 1=fast, 10=best",
1029           1, 10, ARG_SUBME_DEFAULT,
1030           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1031   g_string_append_printf (x264enc_defaults, ":subme=%d", ARG_SUBME_DEFAULT);
1032   g_object_class_install_property (gobject_class, ARG_ANALYSE,
1033       g_param_spec_flags ("analyse", "Analyse", "Partitions to consider",
1034           GST_X264_ENC_ANALYSE_TYPE, ARG_ANALYSE_DEFAULT,
1035           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1036   partitions = gst_x264_enc_build_partitions (ARG_ANALYSE_DEFAULT);
1037   if (partitions) {
1038     g_string_append_printf (x264enc_defaults, ":partitions=%s", partitions);
1039     g_free ((gpointer) partitions);
1040   }
1041   g_object_class_install_property (gobject_class, ARG_DCT8x8,
1042       g_param_spec_boolean ("dct8x8", "DCT8x8",
1043           "Adaptive spatial transform size", ARG_DCT8x8_DEFAULT,
1044           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1045   g_string_append_printf (x264enc_defaults, ":8x8dct=%d", ARG_DCT8x8_DEFAULT);
1046   g_object_class_install_property (gobject_class, ARG_REF,
1047       g_param_spec_uint ("ref", "Reference Frames",
1048           "Number of reference frames",
1049           1, 12, ARG_REF_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1050   g_string_append_printf (x264enc_defaults, ":ref=%d", ARG_REF_DEFAULT);
1051   g_object_class_install_property (gobject_class, ARG_BFRAMES,
1052       g_param_spec_uint ("bframes", "B-Frames",
1053           "Number of B-frames between I and P",
1054           0, 16, ARG_BFRAMES_DEFAULT,
1055           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1056   g_string_append_printf (x264enc_defaults, ":bframes=%d", ARG_BFRAMES_DEFAULT);
1057   g_object_class_install_property (gobject_class, ARG_B_ADAPT,
1058       g_param_spec_boolean ("b-adapt", "B-Adapt",
1059           "Automatically decide how many B-frames to use",
1060           ARG_B_ADAPT_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1061   g_string_append_printf (x264enc_defaults, ":b-adapt=%d", ARG_B_ADAPT_DEFAULT);
1062   g_object_class_install_property (gobject_class, ARG_B_PYRAMID,
1063       g_param_spec_boolean ("b-pyramid", "B-Pyramid",
1064           "Keep some B-frames as references", ARG_B_PYRAMID_DEFAULT,
1065           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1066   g_string_append_printf (x264enc_defaults, ":b-pyramid=%s",
1067       x264_b_pyramid_names[ARG_B_PYRAMID_DEFAULT]);
1068   g_object_class_install_property (gobject_class, ARG_WEIGHTB,
1069       g_param_spec_boolean ("weightb", "Weighted B-Frames",
1070           "Weighted prediction for B-frames", ARG_WEIGHTB_DEFAULT,
1071           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1072   g_string_append_printf (x264enc_defaults, ":weightb=%d", ARG_WEIGHTB_DEFAULT);
1073   g_object_class_install_property (gobject_class, ARG_SPS_ID,
1074       g_param_spec_uint ("sps-id", "SPS ID",
1075           "SPS and PPS ID number",
1076           0, 31, ARG_SPS_ID_DEFAULT,
1077           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1078   g_string_append_printf (x264enc_defaults, ":sps-id=%d", ARG_SPS_ID_DEFAULT);
1079   g_object_class_install_property (gobject_class, ARG_AU_NALU,
1080       g_param_spec_boolean ("aud", "AUD",
1081           "Use AU (Access Unit) delimiter", ARG_AU_NALU_DEFAULT,
1082           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1083   g_string_append_printf (x264enc_defaults, ":aud=%d", ARG_AU_NALU_DEFAULT);
1084   g_object_class_install_property (gobject_class, ARG_TRELLIS,
1085       g_param_spec_boolean ("trellis", "Trellis quantization",
1086           "Enable trellis searched quantization", ARG_TRELLIS_DEFAULT,
1087           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1088   g_string_append_printf (x264enc_defaults, ":trellis=%d", ARG_TRELLIS_DEFAULT);
1089   g_object_class_install_property (gobject_class, ARG_KEYINT_MAX,
1090       g_param_spec_uint ("key-int-max", "Key-frame maximal interval",
1091           "Maximal distance between two key-frames (0 for automatic)",
1092           0, G_MAXINT, ARG_KEYINT_MAX_DEFAULT,
1093           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1094   g_string_append_printf (x264enc_defaults, ":keyint=%d",
1095       ARG_KEYINT_MAX_DEFAULT);
1096   g_object_class_install_property (gobject_class, ARG_CABAC,
1097       g_param_spec_boolean ("cabac", "Use CABAC", "Enable CABAC entropy coding",
1098           ARG_CABAC_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1099   g_string_append_printf (x264enc_defaults, ":cabac=%d", ARG_CABAC_DEFAULT);
1100   g_object_class_install_property (gobject_class, ARG_QP_MIN,
1101       g_param_spec_uint ("qp-min", "Minimum Quantizer",
1102           "Minimum quantizer", 0, 51, ARG_QP_MIN_DEFAULT,
1103           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1104   g_string_append_printf (x264enc_defaults, ":qpmin=%d", ARG_QP_MIN_DEFAULT);
1105   g_object_class_install_property (gobject_class, ARG_QP_MAX,
1106       g_param_spec_uint ("qp-max", "Maximum Quantizer",
1107           "Maximum quantizer", 0, 51, ARG_QP_MAX_DEFAULT,
1108           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1109   g_string_append_printf (x264enc_defaults, ":qpmax=%d", ARG_QP_MAX_DEFAULT);
1110   g_object_class_install_property (gobject_class, ARG_QP_STEP,
1111       g_param_spec_uint ("qp-step", "Maximum Quantizer Difference",
1112           "Maximum quantizer difference between frames",
1113           0, 50, ARG_QP_STEP_DEFAULT,
1114           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1115   g_string_append_printf (x264enc_defaults, ":qpstep=%d", ARG_QP_STEP_DEFAULT);
1116   g_object_class_install_property (gobject_class, ARG_IP_FACTOR,
1117       g_param_spec_float ("ip-factor", "IP-Factor",
1118           "Quantizer factor between I- and P-frames",
1119           0, 2, ARG_IP_FACTOR_DEFAULT,
1120           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1121   g_string_append_printf (x264enc_defaults, ":ip-factor=%f",
1122       ARG_IP_FACTOR_DEFAULT);
1123   g_object_class_install_property (gobject_class, ARG_PB_FACTOR,
1124       g_param_spec_float ("pb-factor", "PB-Factor",
1125           "Quantizer factor between P- and B-frames", 0, 2,
1126           ARG_PB_FACTOR_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1127   g_string_append_printf (x264enc_defaults, ":pb-factor=%f",
1128       ARG_PB_FACTOR_DEFAULT);
1129   g_object_class_install_property (gobject_class, ARG_RC_MB_TREE,
1130       g_param_spec_boolean ("mb-tree", "Macroblock Tree",
1131           "Macroblock-Tree ratecontrol",
1132           ARG_RC_MB_TREE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1133   g_string_append_printf (x264enc_defaults, ":mbtree=%d",
1134       ARG_RC_MB_TREE_DEFAULT);
1135   g_object_class_install_property (gobject_class, ARG_RC_LOOKAHEAD,
1136       g_param_spec_int ("rc-lookahead", "Rate Control Lookahead",
1137           "Number of frames for frametype lookahead", 0, 250,
1138           ARG_RC_LOOKAHEAD_DEFAULT,
1139           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1140   g_string_append_printf (x264enc_defaults, ":rc-lookahead=%d",
1141       ARG_RC_LOOKAHEAD_DEFAULT);
1142   g_object_class_install_property (gobject_class, ARG_NR,
1143       g_param_spec_uint ("noise-reduction", "Noise Reduction",
1144           "Noise reduction strength",
1145           0, 100000, ARG_NR_DEFAULT,
1146           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1147   g_string_append_printf (x264enc_defaults, ":nr=%d", ARG_NR_DEFAULT);
1148   g_object_class_install_property (gobject_class, ARG_INTERLACED,
1149       g_param_spec_boolean ("interlaced", "Interlaced",
1150           "Interlaced material", ARG_INTERLACED_DEFAULT,
1151           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1152   g_string_append_printf (x264enc_defaults, ":interlaced=%d",
1153       ARG_INTERLACED_DEFAULT);
1154
1155   /* append deblock parameters */
1156   g_string_append_printf (x264enc_defaults, ":deblock=0,0");
1157   /* append weighted prediction parameter */
1158   g_string_append_printf (x264enc_defaults, ":weightp=0");
1159
1160   gst_element_class_set_static_metadata (element_class,
1161       "x264enc", "Codec/Encoder/Video", "H264 Encoder",
1162       "Josef Zlomek <josef.zlomek@itonis.tv>, "
1163       "Mark Nauwelaerts <mnauw@users.sf.net>");
1164
1165   supported_sinkcaps = gst_caps_new_simple ("video/x-raw",
1166       "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
1167       "width", GST_TYPE_INT_RANGE, 16, G_MAXINT,
1168       "height", GST_TYPE_INT_RANGE, 16, G_MAXINT, NULL);
1169
1170   gst_x264_enc_add_x264_chroma_format (gst_caps_get_structure
1171       (supported_sinkcaps, 0), TRUE, TRUE, TRUE);
1172
1173   sink_templ = gst_pad_template_new ("sink",
1174       GST_PAD_SINK, GST_PAD_ALWAYS, supported_sinkcaps);
1175
1176   gst_caps_unref (supported_sinkcaps);
1177
1178   gst_element_class_add_pad_template (element_class, sink_templ);
1179   gst_element_class_add_static_pad_template (element_class, &src_factory);
1180 }
1181
1182 static void
1183 gst_x264_enc_log_callback (gpointer private, gint level, const char *format,
1184     va_list args)
1185 {
1186 #ifndef GST_DISABLE_GST_DEBUG
1187   GstDebugLevel gst_level;
1188   GObject *object = (GObject *) private;
1189
1190   switch (level) {
1191     case X264_LOG_NONE:
1192       gst_level = GST_LEVEL_NONE;
1193       break;
1194     case X264_LOG_ERROR:
1195       gst_level = GST_LEVEL_ERROR;
1196       break;
1197     case X264_LOG_WARNING:
1198       gst_level = GST_LEVEL_WARNING;
1199       break;
1200     case X264_LOG_INFO:
1201       gst_level = GST_LEVEL_INFO;
1202       break;
1203     default:
1204       /* push x264enc debug down to our lower levels to avoid some clutter */
1205       gst_level = GST_LEVEL_LOG;
1206       break;
1207   }
1208
1209   gst_debug_log_valist (x264_enc_debug, gst_level, "", "", 0, object, format,
1210       args);
1211 #endif /* GST_DISABLE_GST_DEBUG */
1212 }
1213
1214 /* initialize the new element
1215  * instantiate pads and add them to element
1216  * set functions
1217  * initialize structure
1218  */
1219 static void
1220 gst_x264_enc_init (GstX264Enc * encoder)
1221 {
1222   /* properties */
1223   encoder->threads = ARG_THREADS_DEFAULT;
1224   encoder->sliced_threads = ARG_SLICED_THREADS_DEFAULT;
1225   encoder->sync_lookahead = ARG_SYNC_LOOKAHEAD_DEFAULT;
1226   encoder->pass = ARG_PASS_DEFAULT;
1227   encoder->quantizer = ARG_QUANTIZER_DEFAULT;
1228   encoder->mp_cache_file = g_strdup (ARG_MULTIPASS_CACHE_FILE_DEFAULT);
1229   encoder->byte_stream = ARG_BYTE_STREAM_DEFAULT;
1230   encoder->bitrate = ARG_BITRATE_DEFAULT;
1231   encoder->intra_refresh = ARG_INTRA_REFRESH_DEFAULT;
1232   encoder->vbv_buf_capacity = ARG_VBV_BUF_CAPACITY_DEFAULT;
1233   encoder->me = ARG_ME_DEFAULT;
1234   encoder->subme = ARG_SUBME_DEFAULT;
1235   encoder->analyse = ARG_ANALYSE_DEFAULT;
1236   encoder->dct8x8 = ARG_DCT8x8_DEFAULT;
1237   encoder->ref = ARG_REF_DEFAULT;
1238   encoder->bframes = ARG_BFRAMES_DEFAULT;
1239   encoder->b_adapt = ARG_B_ADAPT_DEFAULT;
1240   encoder->b_pyramid = ARG_B_PYRAMID_DEFAULT;
1241   encoder->weightb = ARG_WEIGHTB_DEFAULT;
1242   encoder->sps_id = ARG_SPS_ID_DEFAULT;
1243   encoder->au_nalu = ARG_AU_NALU_DEFAULT;
1244   encoder->trellis = ARG_TRELLIS_DEFAULT;
1245   encoder->keyint_max = ARG_KEYINT_MAX_DEFAULT;
1246   encoder->cabac = ARG_CABAC_DEFAULT;
1247   encoder->qp_min = ARG_QP_MIN_DEFAULT;
1248   encoder->qp_max = ARG_QP_MAX_DEFAULT;
1249   encoder->qp_step = ARG_QP_STEP_DEFAULT;
1250   encoder->ip_factor = ARG_IP_FACTOR_DEFAULT;
1251   encoder->pb_factor = ARG_PB_FACTOR_DEFAULT;
1252   encoder->mb_tree = ARG_RC_MB_TREE_DEFAULT;
1253   encoder->rc_lookahead = ARG_RC_LOOKAHEAD_DEFAULT;
1254   encoder->noise_reduction = ARG_NR_DEFAULT;
1255   encoder->interlaced = ARG_INTERLACED_DEFAULT;
1256   encoder->option_string = g_string_new (NULL);
1257   encoder->option_string_prop = g_string_new (ARG_OPTION_STRING_DEFAULT);
1258   encoder->speed_preset = ARG_SPEED_PRESET_DEFAULT;
1259   encoder->psy_tune = ARG_PSY_TUNE_DEFAULT;
1260   encoder->tune = ARG_TUNE_DEFAULT;
1261   encoder->frame_packing = ARG_FRAME_PACKING_DEFAULT;
1262   encoder->insert_vui = ARG_INSERT_VUI_DEFAULT;
1263 }
1264
1265 typedef struct
1266 {
1267   GstVideoCodecFrame *frame;
1268   GstVideoFrame vframe;
1269 } FrameData;
1270
1271 static FrameData *
1272 gst_x264_enc_queue_frame (GstX264Enc * enc, GstVideoCodecFrame * frame,
1273     GstVideoInfo * info)
1274 {
1275   GstVideoFrame vframe;
1276   FrameData *fdata;
1277
1278   if (!gst_video_frame_map (&vframe, info, frame->input_buffer, GST_MAP_READ))
1279     return NULL;
1280
1281   fdata = g_slice_new (FrameData);
1282   fdata->frame = gst_video_codec_frame_ref (frame);
1283   fdata->vframe = vframe;
1284
1285   enc->pending_frames = g_list_prepend (enc->pending_frames, fdata);
1286
1287   return fdata;
1288 }
1289
1290 static void
1291 gst_x264_enc_dequeue_frame (GstX264Enc * enc, GstVideoCodecFrame * frame)
1292 {
1293   GList *l;
1294
1295   for (l = enc->pending_frames; l; l = l->next) {
1296     FrameData *fdata = l->data;
1297
1298     if (fdata->frame != frame)
1299       continue;
1300
1301     gst_video_frame_unmap (&fdata->vframe);
1302     gst_video_codec_frame_unref (fdata->frame);
1303     g_slice_free (FrameData, fdata);
1304
1305     enc->pending_frames = g_list_delete_link (enc->pending_frames, l);
1306     return;
1307   }
1308 }
1309
1310 static void
1311 gst_x264_enc_dequeue_all_frames (GstX264Enc * enc)
1312 {
1313   GList *l;
1314
1315   for (l = enc->pending_frames; l; l = l->next) {
1316     FrameData *fdata = l->data;
1317
1318     gst_video_frame_unmap (&fdata->vframe);
1319     gst_video_codec_frame_unref (fdata->frame);
1320     g_slice_free (FrameData, fdata);
1321   }
1322   g_list_free (enc->pending_frames);
1323   enc->pending_frames = NULL;
1324 }
1325
1326 static gboolean
1327 gst_x264_enc_start (GstVideoEncoder * encoder)
1328 {
1329   GstX264Enc *x264enc = GST_X264_ENC (encoder);
1330
1331   x264enc->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY;
1332
1333   /* make sure that we have enough time for first DTS,
1334      this is probably overkill for most streams */
1335   gst_video_encoder_set_min_pts (encoder, GST_SECOND * 60 * 60 * 1000);
1336
1337   return TRUE;
1338 }
1339
1340 static gboolean
1341 gst_x264_enc_stop (GstVideoEncoder * encoder)
1342 {
1343   GstX264Enc *x264enc = GST_X264_ENC (encoder);
1344
1345   gst_x264_enc_flush_frames (x264enc, FALSE);
1346   gst_x264_enc_close_encoder (x264enc);
1347   gst_x264_enc_dequeue_all_frames (x264enc);
1348
1349   if (x264enc->input_state)
1350     gst_video_codec_state_unref (x264enc->input_state);
1351   x264enc->input_state = NULL;
1352
1353   return TRUE;
1354 }
1355
1356
1357 static gboolean
1358 gst_x264_enc_flush (GstVideoEncoder * encoder)
1359 {
1360   GstX264Enc *x264enc = GST_X264_ENC (encoder);
1361
1362   gst_x264_enc_flush_frames (x264enc, FALSE);
1363   gst_x264_enc_close_encoder (x264enc);
1364   gst_x264_enc_dequeue_all_frames (x264enc);
1365
1366   gst_x264_enc_init_encoder (x264enc);
1367
1368   return TRUE;
1369 }
1370
1371 static void
1372 gst_x264_enc_finalize (GObject * object)
1373 {
1374   GstX264Enc *encoder = GST_X264_ENC (object);
1375
1376   if (encoder->input_state)
1377     gst_video_codec_state_unref (encoder->input_state);
1378   encoder->input_state = NULL;
1379
1380 #define FREE_STRING(ptr) \
1381   if (ptr) \
1382     g_string_free (ptr, TRUE);
1383
1384   FREE_STRING (encoder->tunings);
1385   FREE_STRING (encoder->option_string);
1386   FREE_STRING (encoder->option_string_prop);
1387
1388 #undef FREE_STRING
1389
1390   g_free (encoder->mp_cache_file);
1391   encoder->mp_cache_file = NULL;
1392
1393   gst_x264_enc_close_encoder (encoder);
1394
1395   G_OBJECT_CLASS (parent_class)->finalize (object);
1396 }
1397
1398 /*
1399  * gst_x264_enc_parse_options
1400  * @encoder: Encoder to which options are assigned
1401  * @str: Option string
1402  *
1403  * Parse option string and assign to x264 parameters
1404  *
1405  */
1406 static gboolean
1407 gst_x264_enc_parse_options (GstX264Enc * encoder, const gchar * str)
1408 {
1409   GStrv kvpairs;
1410   guint npairs, i;
1411   gint parse_result = 0, ret = 0;
1412   gchar *options = (gchar *) str;
1413
1414   while (*options == ':')
1415     options++;
1416
1417   kvpairs = g_strsplit (options, ":", 0);
1418   npairs = g_strv_length (kvpairs);
1419
1420   for (i = 0; i < npairs; i++) {
1421     GStrv key_val = g_strsplit (kvpairs[i], "=", 2);
1422
1423     parse_result =
1424         encoder->vtable->x264_param_parse (&encoder->x264param, key_val[0],
1425         key_val[1]);
1426
1427     if (parse_result == X264_PARAM_BAD_NAME) {
1428       GST_ERROR_OBJECT (encoder, "Bad name for option %s=%s",
1429           key_val[0] ? key_val[0] : "", key_val[1] ? key_val[1] : "");
1430     }
1431     if (parse_result == X264_PARAM_BAD_VALUE) {
1432       GST_ERROR_OBJECT (encoder,
1433           "Bad value for option %s=%s (Note: a NULL value for a non-boolean triggers this)",
1434           key_val[0] ? key_val[0] : "", key_val[1] ? key_val[1] : "");
1435     }
1436
1437     g_strfreev (key_val);
1438
1439     if (parse_result)
1440       ret++;
1441   }
1442
1443   g_strfreev (kvpairs);
1444   return !ret;
1445 }
1446
1447 static gint
1448 gst_x264_enc_gst_to_x264_video_format (GstVideoFormat format, gint * nplanes)
1449 {
1450   switch (format) {
1451     case GST_VIDEO_FORMAT_I420:
1452     case GST_VIDEO_FORMAT_YV12:
1453       if (nplanes)
1454         *nplanes = 3;
1455       return X264_CSP_I420;
1456     case GST_VIDEO_FORMAT_I420_10BE:
1457     case GST_VIDEO_FORMAT_I420_10LE:
1458       if (nplanes)
1459         *nplanes = 3;
1460       return X264_CSP_I420 | X264_CSP_HIGH_DEPTH;
1461     case GST_VIDEO_FORMAT_Y42B:
1462       if (nplanes)
1463         *nplanes = 3;
1464       return X264_CSP_I422;
1465     case GST_VIDEO_FORMAT_I422_10BE:
1466     case GST_VIDEO_FORMAT_I422_10LE:
1467       if (nplanes)
1468         *nplanes = 3;
1469       return X264_CSP_I422 | X264_CSP_HIGH_DEPTH;
1470     case GST_VIDEO_FORMAT_Y444:
1471       if (nplanes)
1472         *nplanes = 3;
1473       return X264_CSP_I444;
1474     case GST_VIDEO_FORMAT_Y444_10BE:
1475     case GST_VIDEO_FORMAT_Y444_10LE:
1476       if (nplanes)
1477         *nplanes = 3;
1478       return X264_CSP_I444 | X264_CSP_HIGH_DEPTH;
1479     case GST_VIDEO_FORMAT_NV12:
1480       if (nplanes)
1481         *nplanes = 2;
1482       return X264_CSP_NV12;
1483     default:
1484       g_return_val_if_reached (GST_VIDEO_FORMAT_UNKNOWN);
1485   }
1486 }
1487
1488 /*
1489  * gst_x264_enc_init_encoder
1490  * @encoder:  Encoder which should be initialized.
1491  *
1492  * Initialize x264 encoder.
1493  *
1494  */
1495 static gboolean
1496 gst_x264_enc_init_encoder (GstX264Enc * encoder)
1497 {
1498   guint pass = 0;
1499   GstVideoInfo *info;
1500
1501   if (!encoder->input_state) {
1502     GST_DEBUG_OBJECT (encoder, "Have no input state yet");
1503     return FALSE;
1504   }
1505
1506   info = &encoder->input_state->info;
1507
1508   /* make sure that the encoder is closed */
1509   gst_x264_enc_close_encoder (encoder);
1510
1511   GST_OBJECT_LOCK (encoder);
1512
1513   if (GST_VIDEO_INFO_COMP_DEPTH (info, 0) == 8)
1514     encoder->vtable = vtable_8bit;
1515   else if (GST_VIDEO_INFO_COMP_DEPTH (info, 0) == 10)
1516     encoder->vtable = vtable_10bit;
1517
1518   g_assert (encoder->vtable != NULL);
1519
1520   gst_x264_enc_build_tunings_string (encoder);
1521
1522   /* set x264 parameters and use preset/tuning if present */
1523   GST_DEBUG_OBJECT (encoder, "Applying defaults with preset %s, tunings %s",
1524       encoder->speed_preset ? x264_preset_names[encoder->speed_preset - 1] : "",
1525       encoder->tunings && encoder->tunings->len ? encoder->tunings->str : "");
1526   encoder->vtable->x264_param_default_preset (&encoder->x264param,
1527       encoder->speed_preset ? x264_preset_names[encoder->speed_preset -
1528           1] : NULL, encoder->tunings
1529       && encoder->tunings->len ? encoder->tunings->str : NULL);
1530
1531   /* log callback setup; part of parameters
1532    * this needs to be done again after every *param_default* () call */
1533   encoder->x264param.pf_log = gst_x264_enc_log_callback;
1534   encoder->x264param.p_log_private = encoder;
1535   encoder->x264param.i_log_level = X264_LOG_DEBUG;
1536
1537   /* if no preset nor tuning, use property defaults */
1538   if (!encoder->speed_preset && !encoder->tunings->len) {
1539     GST_DEBUG_OBJECT (encoder, "Applying x264enc_defaults");
1540     if (x264enc_defaults->len
1541         && gst_x264_enc_parse_options (encoder,
1542             x264enc_defaults->str) == FALSE) {
1543       GST_DEBUG_OBJECT (encoder,
1544           "x264enc_defaults string contains errors. This is a bug.");
1545       goto unlock_and_return;
1546     }
1547   } else {
1548     /* When using presets we need to respect the default output format */
1549     encoder->x264param.b_aud = encoder->au_nalu;
1550     encoder->x264param.b_annexb = encoder->byte_stream;
1551   }
1552
1553   /* setup appropriate timebase for gstreamer */
1554   encoder->x264param.i_timebase_num = 1;
1555   encoder->x264param.i_timebase_den = 1000000000;
1556
1557   /* apply option-string property */
1558   if (encoder->option_string_prop && encoder->option_string_prop->len) {
1559     GST_DEBUG_OBJECT (encoder, "Applying option-string: %s",
1560         encoder->option_string_prop->str);
1561     if (gst_x264_enc_parse_options (encoder,
1562             encoder->option_string_prop->str) == FALSE) {
1563       GST_DEBUG_OBJECT (encoder, "Your option-string contains errors.");
1564       goto unlock_and_return;
1565     }
1566   }
1567   /* apply user-set options */
1568   if (encoder->option_string && encoder->option_string->len) {
1569     GST_DEBUG_OBJECT (encoder, "Applying user-set options: %s",
1570         encoder->option_string->str);
1571     if (gst_x264_enc_parse_options (encoder,
1572             encoder->option_string->str) == FALSE) {
1573       GST_DEBUG_OBJECT (encoder, "Failed to parse internal option string. "
1574           "This could be due to use of an old libx264 version. Option string "
1575           "was: %s", encoder->option_string->str);
1576     }
1577   }
1578
1579   /* set up encoder parameters */
1580 #if X264_BUILD >= 153
1581   encoder->x264param.i_bitdepth = GST_VIDEO_INFO_COMP_DEPTH (info, 0);
1582 #endif
1583   encoder->x264param.i_csp =
1584       gst_x264_enc_gst_to_x264_video_format (info->finfo->format,
1585       &encoder->x264_nplanes);
1586   if (info->fps_d == 0 || info->fps_n == 0) {
1587     /* No FPS so must use VFR
1588      * This raises latency apparently see http://mewiki.project357.com/wiki/X264_Encoding_Suggestions */
1589     encoder->x264param.b_vfr_input = TRUE;
1590     if (encoder->keyint_max) {  /* NB the default is 250 setup by x264 itself */
1591       encoder->x264param.i_keyint_max = encoder->keyint_max;
1592     }
1593   } else {
1594     /* FPS available so set it up */
1595     encoder->x264param.b_vfr_input = FALSE;
1596     encoder->x264param.i_fps_num = info->fps_n;
1597     encoder->x264param.i_fps_den = info->fps_d;
1598     encoder->x264param.i_keyint_max =
1599         encoder->keyint_max ? encoder->keyint_max : (10 * info->fps_n /
1600         info->fps_d);
1601   }
1602   encoder->x264param.i_width = info->width;
1603   encoder->x264param.i_height = info->height;
1604   if (info->par_d > 0) {
1605     encoder->x264param.vui.i_sar_width = info->par_n;
1606     encoder->x264param.vui.i_sar_height = info->par_d;
1607   }
1608
1609   if ((((info->height == 576) && ((info->width == 720)
1610                   || (info->width == 704) || (info->width == 352)))
1611           || ((info->height == 288) && (info->width == 352)))
1612       && (info->fps_d == 1) && (info->fps_n == 25)) {
1613     encoder->x264param.vui.i_vidformat = 1;     /* PAL */
1614   } else if ((((info->height == 480) && ((info->width == 720)
1615                   || (info->width == 704) || (info->width == 352)))
1616           || ((info->height == 240) && (info->width == 352)))
1617       && (info->fps_d == 1001) && ((info->fps_n == 30000)
1618           || (info->fps_n == 24000))) {
1619     encoder->x264param.vui.i_vidformat = 2;     /* NTSC */
1620   } else {
1621     encoder->x264param.vui.i_vidformat = 5;     /* unspecified */
1622   }
1623
1624   if (!encoder->insert_vui)
1625     goto skip_vui_parameters;
1626
1627   switch (info->colorimetry.primaries) {
1628     case GST_VIDEO_COLOR_PRIMARIES_BT709:
1629       encoder->x264param.vui.i_colorprim = 1;
1630       break;
1631     case GST_VIDEO_COLOR_PRIMARIES_BT470M:
1632       encoder->x264param.vui.i_colorprim = 4;
1633       break;
1634     case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
1635       encoder->x264param.vui.i_colorprim = 5;
1636       break;
1637     case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
1638       encoder->x264param.vui.i_colorprim = 6;
1639       break;
1640     case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
1641       encoder->x264param.vui.i_colorprim = 7;
1642       break;
1643     case GST_VIDEO_COLOR_PRIMARIES_FILM:
1644       encoder->x264param.vui.i_colorprim = 8;
1645       break;
1646     case GST_VIDEO_COLOR_PRIMARIES_BT2020:
1647       encoder->x264param.vui.i_colorprim = 9;
1648       break;
1649     default:
1650       encoder->x264param.vui.i_colorprim = 2;
1651       break;
1652   }
1653
1654   switch (info->colorimetry.transfer) {
1655     case GST_VIDEO_TRANSFER_BT709:
1656       encoder->x264param.vui.i_transfer = 1;
1657       break;
1658     case GST_VIDEO_TRANSFER_GAMMA22:
1659       encoder->x264param.vui.i_transfer = 4;
1660       break;
1661     case GST_VIDEO_TRANSFER_GAMMA28:
1662       encoder->x264param.vui.i_transfer = 5;
1663       break;
1664     case GST_VIDEO_TRANSFER_SMPTE240M:
1665       encoder->x264param.vui.i_transfer = 7;
1666       break;
1667     case GST_VIDEO_TRANSFER_GAMMA10:
1668       encoder->x264param.vui.i_transfer = 8;
1669       break;
1670     case GST_VIDEO_TRANSFER_LOG100:
1671       encoder->x264param.vui.i_transfer = 9;
1672       break;
1673     case GST_VIDEO_TRANSFER_LOG316:
1674       encoder->x264param.vui.i_transfer = 10;
1675       break;
1676     default:
1677       encoder->x264param.vui.i_transfer = 2;
1678       break;
1679
1680   }
1681
1682   switch (info->colorimetry.matrix) {
1683     case GST_VIDEO_COLOR_MATRIX_RGB:
1684       encoder->x264param.vui.i_colmatrix = 0;
1685       break;
1686     case GST_VIDEO_COLOR_MATRIX_FCC:
1687       encoder->x264param.vui.i_colmatrix = 4;
1688       break;
1689     case GST_VIDEO_COLOR_MATRIX_BT709:
1690       encoder->x264param.vui.i_colmatrix = 1;
1691       break;
1692     case GST_VIDEO_COLOR_MATRIX_BT601:
1693       encoder->x264param.vui.i_colmatrix = 5;
1694       break;
1695     case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
1696       encoder->x264param.vui.i_colmatrix = 7;
1697       break;
1698     case GST_VIDEO_COLOR_MATRIX_BT2020:
1699       encoder->x264param.vui.i_colmatrix = 9;
1700       break;
1701     default:
1702       encoder->x264param.vui.i_colmatrix = 2;
1703       break;
1704   }
1705
1706   if (info->colorimetry.range == GST_VIDEO_COLOR_RANGE_0_255) {
1707     encoder->x264param.vui.b_fullrange = 1;
1708   } else {
1709     encoder->x264param.vui.b_fullrange = 0;
1710   }
1711
1712   switch (info->chroma_site) {
1713     case GST_VIDEO_CHROMA_SITE_MPEG2:
1714       encoder->x264param.vui.i_chroma_loc = 0;
1715       break;
1716     case GST_VIDEO_CHROMA_SITE_JPEG:
1717       encoder->x264param.vui.i_chroma_loc = 1;
1718       break;
1719     case GST_VIDEO_CHROMA_SITE_V_COSITED:
1720       encoder->x264param.vui.i_chroma_loc = 3;
1721       break;
1722     case GST_VIDEO_CHROMA_SITE_DV:
1723       encoder->x264param.vui.i_chroma_loc = 2;
1724       break;
1725     default:
1726       encoder->x264param.vui.i_chroma_loc = 0;
1727       break;
1728   }
1729
1730 skip_vui_parameters:
1731
1732   encoder->x264param.analyse.b_psnr = 0;
1733
1734   /* FIXME 2.0 make configuration more sane and consistent with x264 cmdline:
1735    * + split pass property into a pass property (pass1/2/3 enum) and rc-method
1736    * + bitrate property should only be used in case of CBR method
1737    * + vbv bitrate/buffer should have separate configuration that is then
1738    *   applied independently of the mode:
1739    *    + either using properties (new) vbv-maxrate and (renamed) vbv-bufsize
1740    *    + or dropping vbv-buf-capacity altogether and simply using option-string
1741    */
1742   switch (encoder->pass) {
1743     case GST_X264_ENC_PASS_QUANT:
1744       encoder->x264param.rc.i_rc_method = X264_RC_CQP;
1745       encoder->x264param.rc.i_qp_constant = encoder->quantizer;
1746       break;
1747     case GST_X264_ENC_PASS_QUAL:
1748       encoder->x264param.rc.i_rc_method = X264_RC_CRF;
1749       encoder->x264param.rc.f_rf_constant = encoder->quantizer;
1750       encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1751       encoder->x264param.rc.i_vbv_buffer_size
1752           = encoder->x264param.rc.i_vbv_max_bitrate
1753           * encoder->vbv_buf_capacity / 1000;
1754       break;
1755     case GST_X264_ENC_PASS_CBR:
1756     case GST_X264_ENC_PASS_PASS1:
1757     case GST_X264_ENC_PASS_PASS2:
1758     case GST_X264_ENC_PASS_PASS3:
1759     default:
1760       encoder->x264param.rc.i_rc_method = X264_RC_ABR;
1761       encoder->x264param.rc.i_bitrate = encoder->bitrate;
1762       encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1763       encoder->x264param.rc.i_vbv_buffer_size =
1764           encoder->x264param.rc.i_vbv_max_bitrate
1765           * encoder->vbv_buf_capacity / 1000;
1766       pass = encoder->pass & 0xF;
1767       break;
1768   }
1769
1770   switch (pass) {
1771     case 0:
1772       encoder->x264param.rc.b_stat_read = 0;
1773       encoder->x264param.rc.b_stat_write = 0;
1774       break;
1775     case 1:
1776       encoder->x264param.rc.b_stat_read = 0;
1777       encoder->x264param.rc.b_stat_write = 1;
1778       encoder->vtable->x264_param_apply_fastfirstpass (&encoder->x264param);
1779       encoder->x264param.i_frame_reference = 1;
1780       encoder->x264param.analyse.b_transform_8x8 = 0;
1781       encoder->x264param.analyse.inter = 0;
1782       encoder->x264param.analyse.i_me_method = X264_ME_DIA;
1783       encoder->x264param.analyse.i_subpel_refine =
1784           MIN (2, encoder->x264param.analyse.i_subpel_refine);
1785       encoder->x264param.analyse.i_trellis = 0;
1786       encoder->x264param.analyse.b_fast_pskip = 1;
1787       break;
1788     case 2:
1789       encoder->x264param.rc.b_stat_read = 1;
1790       encoder->x264param.rc.b_stat_write = 0;
1791       break;
1792     case 3:
1793       encoder->x264param.rc.b_stat_read = 1;
1794       encoder->x264param.rc.b_stat_write = 1;
1795       break;
1796   }
1797
1798   if (encoder->peer_profile) {
1799     if (encoder->vtable->x264_param_apply_profile (&encoder->x264param,
1800             encoder->peer_profile))
1801       GST_WARNING_OBJECT (encoder, "Bad downstream profile name: %s",
1802           encoder->peer_profile);
1803   }
1804
1805   /* If using an intra profile, all frames are intra frames */
1806   if (encoder->peer_intra_profile)
1807     encoder->x264param.i_keyint_max = encoder->x264param.i_keyint_min = 1;
1808
1809   /* Enforce level limits if they were in the caps */
1810   if (encoder->peer_level_idc != -1) {
1811     gint i;
1812     const x264_level_t *peer_level = NULL;
1813
1814     for (i = 0; (*encoder->vtable->x264_levels)[i].level_idc; i++) {
1815       if (encoder->peer_level_idc ==
1816           (*encoder->vtable->x264_levels)[i].level_idc) {
1817         int mb_width = (info->width + 15) / 16;
1818         int mb_height = (info->height + 15) / 16;
1819         int mbs = mb_width * mb_height;
1820
1821         if ((*encoder->vtable->x264_levels)[i].frame_size < mbs ||
1822             (*encoder->vtable->x264_levels)[i].frame_size * 8 <
1823             mb_width * mb_width
1824             || (*encoder->vtable->x264_levels)[i].frame_size * 8 <
1825             mb_height * mb_height) {
1826           GST_WARNING_OBJECT (encoder,
1827               "Frame size larger than level %d allows",
1828               encoder->peer_level_idc);
1829           break;
1830         }
1831
1832         if (info->fps_d && (*encoder->vtable->x264_levels)[i].mbps
1833             < (gint64) mbs * info->fps_n / info->fps_d) {
1834           GST_WARNING_OBJECT (encoder,
1835               "Macroblock rate higher than level %d allows",
1836               encoder->peer_level_idc);
1837           break;
1838         }
1839
1840         peer_level = &(*encoder->vtable->x264_levels)[i];
1841         break;
1842       }
1843     }
1844
1845     if (!peer_level)
1846       goto unlock_and_return;
1847
1848     encoder->x264param.i_level_idc = peer_level->level_idc;
1849
1850     encoder->x264param.rc.i_bitrate = MIN (encoder->x264param.rc.i_bitrate,
1851         peer_level->bitrate);
1852     encoder->x264param.rc.i_vbv_max_bitrate =
1853         MIN (encoder->x264param.rc.i_vbv_max_bitrate, peer_level->bitrate);
1854     encoder->x264param.rc.i_vbv_buffer_size =
1855         MIN (encoder->x264param.rc.i_vbv_buffer_size, peer_level->cpb);
1856     encoder->x264param.analyse.i_mv_range =
1857         MIN (encoder->x264param.analyse.i_mv_range, peer_level->mv_range);
1858
1859     if (peer_level->frame_only) {
1860       encoder->x264param.b_interlaced = FALSE;
1861       encoder->x264param.b_fake_interlaced = FALSE;
1862     }
1863   }
1864
1865   if (GST_VIDEO_INFO_IS_INTERLACED (info)) {
1866     encoder->x264param.b_interlaced = TRUE;
1867     if (GST_VIDEO_INFO_INTERLACE_MODE (info) == GST_VIDEO_INTERLACE_MODE_MIXED) {
1868       encoder->x264param.b_pic_struct = TRUE;
1869     }
1870     if (GST_VIDEO_INFO_FIELD_ORDER (info) ==
1871         GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST) {
1872       encoder->x264param.b_tff = TRUE;
1873     } else {
1874       encoder->x264param.b_tff = FALSE;
1875     }
1876   } else {
1877     encoder->x264param.b_interlaced = FALSE;
1878   }
1879
1880   /* Set 3D frame packing */
1881   if (encoder->frame_packing != GST_VIDEO_MULTIVIEW_MODE_NONE)
1882     encoder->x264param.i_frame_packing = encoder->frame_packing;
1883   else
1884     encoder->x264param.i_frame_packing =
1885         gst_x264_enc_mview_mode_to_frame_packing (GST_VIDEO_INFO_MULTIVIEW_MODE
1886         (info));
1887
1888   GST_DEBUG_OBJECT (encoder, "Stereo frame packing = %d",
1889       encoder->x264param.i_frame_packing);
1890
1891   encoder->reconfig = FALSE;
1892
1893   GST_OBJECT_UNLOCK (encoder);
1894
1895   encoder->x264enc = encoder->vtable->x264_encoder_open (&encoder->x264param);
1896   if (!encoder->x264enc) {
1897     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE,
1898         ("Can not initialize x264 encoder."), (NULL));
1899     return FALSE;
1900   }
1901
1902   return TRUE;
1903
1904 unlock_and_return:
1905   GST_OBJECT_UNLOCK (encoder);
1906   return FALSE;
1907 }
1908
1909 /* gst_x264_enc_close_encoder
1910  * @encoder:  Encoder which should close.
1911  *
1912  * Close x264 encoder.
1913  */
1914 static void
1915 gst_x264_enc_close_encoder (GstX264Enc * encoder)
1916 {
1917   if (encoder->x264enc != NULL) {
1918     encoder->vtable->x264_encoder_close (encoder->x264enc);
1919     encoder->x264enc = NULL;
1920   }
1921   encoder->vtable = NULL;
1922 }
1923
1924 static gboolean
1925 gst_x264_enc_set_profile_and_level (GstX264Enc * encoder, GstCaps * caps)
1926 {
1927   x264_nal_t *nal;
1928   int i_nal;
1929   int header_return;
1930   gint sps_ni = 0;
1931   guint8 *sps;
1932   GstStructure *s;
1933   const gchar *profile;
1934   GstCaps *allowed_caps;
1935   GstStructure *s2;
1936   const gchar *allowed_profile;
1937
1938   header_return =
1939       encoder->vtable->x264_encoder_headers (encoder->x264enc, &nal, &i_nal);
1940   if (header_return < 0) {
1941     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 header failed."),
1942         ("x264_encoder_headers return code=%d", header_return));
1943     return FALSE;
1944   }
1945
1946   /* old x264 returns SEI, SPS and PPS, newer one has SEI last */
1947   if (i_nal == 3 && nal[sps_ni].i_type != 7)
1948     sps_ni = 1;
1949
1950   sps = nal[sps_ni].p_payload + 4;
1951   /* skip NAL unit type */
1952   sps++;
1953
1954   gst_codec_utils_h264_caps_set_level_and_profile (caps, sps, 3);
1955
1956   /* Constrained baseline is a strict subset of baseline. If downstream
1957    * wanted baseline and we produced constrained baseline, we can just
1958    * set the profile to baseline in the caps to make negotiation happy.
1959    * Same goes for baseline as subset of main profile and main as a subset
1960    * of high profile.
1961    */
1962   s = gst_caps_get_structure (caps, 0);
1963   profile = gst_structure_get_string (s, "profile");
1964
1965   allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1966
1967   if (allowed_caps == NULL)
1968     goto no_peer;
1969
1970   if (!gst_caps_can_intersect (allowed_caps, caps)) {
1971     allowed_caps = gst_caps_make_writable (allowed_caps);
1972     allowed_caps = gst_caps_truncate (allowed_caps);
1973     s2 = gst_caps_get_structure (allowed_caps, 0);
1974     gst_structure_fixate_field_string (s2, "profile", profile);
1975     allowed_profile = gst_structure_get_string (s2, "profile");
1976     if (!strcmp (allowed_profile, "high")) {
1977       if (!strcmp (profile, "constrained-baseline")
1978           || !strcmp (profile, "baseline") || !strcmp (profile, "main")) {
1979         gst_structure_set (s, "profile", G_TYPE_STRING, "high", NULL);
1980         GST_INFO_OBJECT (encoder, "downstream requested high profile, but "
1981             "encoder will now output %s profile (which is a subset), due "
1982             "to how it's been configured", profile);
1983       }
1984     } else if (!strcmp (allowed_profile, "main")) {
1985       if (!strcmp (profile, "constrained-baseline")
1986           || !strcmp (profile, "baseline")) {
1987         gst_structure_set (s, "profile", G_TYPE_STRING, "main", NULL);
1988         GST_INFO_OBJECT (encoder, "downstream requested main profile, but "
1989             "encoder will now output %s profile (which is a subset), due "
1990             "to how it's been configured", profile);
1991       }
1992     } else if (!strcmp (allowed_profile, "baseline")) {
1993       if (!strcmp (profile, "constrained-baseline"))
1994         gst_structure_set (s, "profile", G_TYPE_STRING, "baseline", NULL);
1995     }
1996   }
1997   gst_caps_unref (allowed_caps);
1998
1999 no_peer:
2000
2001   return TRUE;
2002 }
2003
2004 /*
2005  * Returns: Buffer with the stream headers.
2006  */
2007 static GstBuffer *
2008 gst_x264_enc_header_buf (GstX264Enc * encoder)
2009 {
2010   GstBuffer *buf;
2011   x264_nal_t *nal;
2012   int i_nal;
2013   int header_return;
2014   int i_size;
2015   int nal_size;
2016   guint8 *buffer, *sps;
2017   gulong buffer_size;
2018   gint sei_ni = 2, sps_ni = 0, pps_ni = 1;
2019
2020   if (G_UNLIKELY (encoder->x264enc == NULL))
2021     return NULL;
2022
2023   /* Create avcC header. */
2024
2025   header_return =
2026       encoder->vtable->x264_encoder_headers (encoder->x264enc, &nal, &i_nal);
2027   if (header_return < 0) {
2028     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 header failed."),
2029         ("x264_encoder_headers return code=%d", header_return));
2030     return NULL;
2031   }
2032
2033   /* old x264 returns SEI, SPS and PPS, newer one has SEI last */
2034   if (i_nal == 3 && nal[sps_ni].i_type != 7) {
2035     sei_ni = 0;
2036     sps_ni = 1;
2037     pps_ni = 2;
2038   }
2039
2040   /* x264 is expected to return an SEI (some identification info),
2041    * and SPS and PPS */
2042   if (i_nal != 3 || nal[sps_ni].i_type != 7 || nal[pps_ni].i_type != 8 ||
2043       nal[sps_ni].i_payload < 4 || nal[pps_ni].i_payload < 1) {
2044     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, (NULL),
2045         ("Unexpected x264 header."));
2046     return NULL;
2047   }
2048
2049   GST_MEMDUMP ("SEI", nal[sei_ni].p_payload, nal[sei_ni].i_payload);
2050   GST_MEMDUMP ("SPS", nal[sps_ni].p_payload, nal[sps_ni].i_payload);
2051   GST_MEMDUMP ("PPS", nal[pps_ni].p_payload, nal[pps_ni].i_payload);
2052
2053   /* nal payloads with emulation_prevention_three_byte, and some header data */
2054   buffer_size = (nal[sps_ni].i_payload + nal[pps_ni].i_payload) * 4 + 100;
2055   buffer = g_malloc (buffer_size);
2056
2057   sps = nal[sps_ni].p_payload + 4;
2058   /* skip NAL unit type */
2059   sps++;
2060
2061   buffer[0] = 1;                /* AVC Decoder Configuration Record ver. 1 */
2062   buffer[1] = sps[0];           /* profile_idc                             */
2063   buffer[2] = sps[1];           /* profile_compability                     */
2064   buffer[3] = sps[2];           /* level_idc                               */
2065   buffer[4] = 0xfc | (4 - 1);   /* nal_length_size_minus1                  */
2066
2067   i_size = 5;
2068
2069   buffer[i_size++] = 0xe0 | 1;  /* number of SPSs */
2070
2071   nal_size = nal[sps_ni].i_payload - 4;
2072   memcpy (buffer + i_size + 2, nal[sps_ni].p_payload + 4, nal_size);
2073
2074   GST_WRITE_UINT16_BE (buffer + i_size, nal_size);
2075   i_size += nal_size + 2;
2076
2077   buffer[i_size++] = 1;         /* number of PPSs */
2078
2079   nal_size = nal[pps_ni].i_payload - 4;
2080   memcpy (buffer + i_size + 2, nal[pps_ni].p_payload + 4, nal_size);
2081
2082   GST_WRITE_UINT16_BE (buffer + i_size, nal_size);
2083   i_size += nal_size + 2;
2084
2085   buf = gst_buffer_new_and_alloc (i_size);
2086   gst_buffer_fill (buf, 0, buffer, i_size);
2087
2088   GST_MEMDUMP ("header", buffer, i_size);
2089   g_free (buffer);
2090
2091   return buf;
2092 }
2093
2094 /* gst_x264_enc_set_src_caps
2095  * Returns: TRUE on success.
2096  */
2097 static gboolean
2098 gst_x264_enc_set_src_caps (GstX264Enc * encoder, GstCaps * caps)
2099 {
2100   GstCaps *outcaps;
2101   GstStructure *structure;
2102   GstVideoCodecState *state;
2103   GstTagList *tags;
2104
2105   outcaps = gst_caps_new_empty_simple ("video/x-h264");
2106   structure = gst_caps_get_structure (outcaps, 0);
2107
2108   if (encoder->current_byte_stream == GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY) {
2109     if (encoder->byte_stream) {
2110       encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
2111     } else {
2112       encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_AVC;
2113     }
2114   }
2115   if (encoder->current_byte_stream == GST_X264_ENC_STREAM_FORMAT_AVC) {
2116     GstBuffer *buf = gst_x264_enc_header_buf (encoder);
2117     if (buf != NULL) {
2118       gst_caps_set_simple (outcaps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
2119       gst_buffer_unref (buf);
2120     }
2121     gst_structure_set (structure, "stream-format", G_TYPE_STRING, "avc", NULL);
2122   } else {
2123     gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
2124         NULL);
2125   }
2126   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
2127
2128   if (!gst_x264_enc_set_profile_and_level (encoder, outcaps)) {
2129     gst_caps_unref (outcaps);
2130     return FALSE;
2131   }
2132
2133   state = gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (encoder),
2134       outcaps, encoder->input_state);
2135   GST_DEBUG_OBJECT (encoder, "output caps: %" GST_PTR_FORMAT, state->caps);
2136
2137   /* If set, local frame packing setting overrides any upstream config */
2138   switch (encoder->frame_packing) {
2139     case 0:
2140       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2141           GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
2142       break;
2143     case 1:
2144       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2145           GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED;
2146       break;
2147     case 2:
2148       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2149           GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED;
2150       break;
2151     case 3:
2152       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2153           GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
2154       break;
2155     case 4:
2156       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2157           GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
2158       break;
2159     case 5:
2160       GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
2161           GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
2162       break;
2163     default:
2164       break;
2165   }
2166
2167   gst_video_codec_state_unref (state);
2168
2169   tags = gst_tag_list_new_empty ();
2170   gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "x264",
2171       GST_TAG_ENCODER_VERSION, X264_BUILD,
2172       GST_TAG_MAXIMUM_BITRATE, encoder->bitrate * 1024,
2173       GST_TAG_NOMINAL_BITRATE, encoder->bitrate * 1024, NULL);
2174   gst_video_encoder_merge_tags (GST_VIDEO_ENCODER (encoder), tags,
2175       GST_TAG_MERGE_REPLACE);
2176   gst_tag_list_unref (tags);
2177
2178   return TRUE;
2179 }
2180
2181 static void
2182 gst_x264_enc_set_latency (GstX264Enc * encoder)
2183 {
2184   GstVideoInfo *info = &encoder->input_state->info;
2185   gint max_delayed_frames;
2186   GstClockTime latency;
2187
2188   max_delayed_frames =
2189       encoder->vtable->x264_encoder_maximum_delayed_frames (encoder->x264enc);
2190
2191   if (info->fps_n) {
2192     latency = gst_util_uint64_scale_ceil (GST_SECOND * info->fps_d,
2193         max_delayed_frames, info->fps_n);
2194   } else {
2195     /* FIXME: Assume 25fps. This is better than reporting no latency at
2196      * all and then later failing in live pipelines
2197      */
2198     latency = gst_util_uint64_scale_ceil (GST_SECOND * 1,
2199         max_delayed_frames, 25);
2200   }
2201
2202   GST_INFO_OBJECT (encoder,
2203       "Updating latency to %" GST_TIME_FORMAT " (%d frames)",
2204       GST_TIME_ARGS (latency), max_delayed_frames);
2205
2206   gst_video_encoder_set_latency (GST_VIDEO_ENCODER (encoder), latency, latency);
2207 }
2208
2209 static gboolean
2210 gst_x264_enc_set_format (GstVideoEncoder * video_enc,
2211     GstVideoCodecState * state)
2212 {
2213   GstX264Enc *encoder = GST_X264_ENC (video_enc);
2214   GstVideoInfo *info = &state->info;
2215   GstCaps *template_caps;
2216   GstCaps *allowed_caps = NULL;
2217
2218   /* If the encoder is initialized, do not reinitialize it again if not
2219    * necessary */
2220   if (encoder->x264enc) {
2221     GstVideoInfo *old = &encoder->input_state->info;
2222
2223     if (info->finfo->format == old->finfo->format
2224         && info->width == old->width && info->height == old->height
2225         && info->fps_n == old->fps_n && info->fps_d == old->fps_d
2226         && info->par_n == old->par_n && info->par_d == old->par_d) {
2227       gst_video_codec_state_unref (encoder->input_state);
2228       encoder->input_state = gst_video_codec_state_ref (state);
2229       return TRUE;
2230     }
2231
2232     /* clear out pending frames */
2233     gst_x264_enc_flush_frames (encoder, TRUE);
2234
2235     encoder->sps_id++;
2236   }
2237
2238   if (encoder->input_state)
2239     gst_video_codec_state_unref (encoder->input_state);
2240   encoder->input_state = gst_video_codec_state_ref (state);
2241
2242   encoder->peer_profile = NULL;
2243   encoder->peer_intra_profile = FALSE;
2244   encoder->peer_level_idc = -1;
2245
2246   template_caps = gst_static_pad_template_get_caps (&src_factory);
2247   allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
2248
2249   /* Output byte-stream if downstream has ANY caps, it's what people expect,
2250    * and it makes more sense too */
2251   if (allowed_caps == template_caps) {
2252     GST_INFO_OBJECT (encoder,
2253         "downstream has ANY caps, outputting byte-stream");
2254     encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
2255     g_string_append_printf (encoder->option_string, ":annexb=1");
2256     gst_caps_unref (allowed_caps);
2257   } else if (allowed_caps) {
2258     GstStructure *s;
2259     const gchar *profile;
2260     const gchar *level;
2261     const gchar *stream_format;
2262
2263     if (gst_caps_is_empty (allowed_caps)) {
2264       gst_caps_unref (allowed_caps);
2265       gst_caps_unref (template_caps);
2266       return FALSE;
2267     }
2268
2269     allowed_caps = gst_caps_make_writable (allowed_caps);
2270     allowed_caps = gst_caps_fixate (allowed_caps);
2271     s = gst_caps_get_structure (allowed_caps, 0);
2272
2273     profile = gst_structure_get_string (s, "profile");
2274     if (profile) {
2275       /* FIXME - if libx264 ever adds support for FMO, ASO or redundant slices
2276        * make sure constrained profile has a separate case which disables
2277        * those */
2278       if (g_str_has_suffix (profile, "-intra")) {
2279         encoder->peer_intra_profile = TRUE;
2280       }
2281       if (!strcmp (profile, "constrained-baseline") ||
2282           !strcmp (profile, "baseline")) {
2283         encoder->peer_profile = "baseline";
2284       } else if (g_str_has_prefix (profile, "high-10")) {
2285         encoder->peer_profile = "high10";
2286       } else if (g_str_has_prefix (profile, "high-4:2:2")) {
2287         encoder->peer_profile = "high422";
2288       } else if (g_str_has_prefix (profile, "high-4:4:4")) {
2289         encoder->peer_profile = "high444";
2290       } else if (g_str_has_prefix (profile, "high")) {
2291         encoder->peer_profile = "high";
2292       } else if (!strcmp (profile, "main")) {
2293         encoder->peer_profile = "main";
2294       } else {
2295         g_assert_not_reached ();
2296       }
2297     }
2298
2299     level = gst_structure_get_string (s, "level");
2300     if (level) {
2301       encoder->peer_level_idc = gst_codec_utils_h264_get_level_idc (level);
2302     }
2303
2304     stream_format = gst_structure_get_string (s, "stream-format");
2305     encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY;
2306     if (stream_format) {
2307       if (!strcmp (stream_format, "avc")) {
2308         encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_AVC;
2309         g_string_append_printf (encoder->option_string, ":annexb=0");
2310       } else if (!strcmp (stream_format, "byte-stream")) {
2311         encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
2312         g_string_append_printf (encoder->option_string, ":annexb=1");
2313       } else {
2314         /* means we have both in caps and _FROM_PROPERTY should be the option */
2315       }
2316     }
2317
2318     gst_caps_unref (allowed_caps);
2319   }
2320
2321   gst_caps_unref (template_caps);
2322
2323   if (!gst_x264_enc_init_encoder (encoder))
2324     return FALSE;
2325
2326   if (!gst_x264_enc_set_src_caps (encoder, state->caps)) {
2327     gst_x264_enc_close_encoder (encoder);
2328     return FALSE;
2329   }
2330
2331   gst_x264_enc_set_latency (encoder);
2332
2333   return TRUE;
2334 }
2335
2336 static GstFlowReturn
2337 gst_x264_enc_finish (GstVideoEncoder * encoder)
2338 {
2339   gst_x264_enc_flush_frames (GST_X264_ENC (encoder), TRUE);
2340   return GST_FLOW_OK;
2341 }
2342
2343 static gboolean
2344 gst_x264_enc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
2345 {
2346   GstX264Enc *self = GST_X264_ENC (encoder);
2347   GstVideoInfo *info;
2348   guint num_buffers;
2349
2350   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2351
2352   if (!self->input_state)
2353     return FALSE;
2354
2355   if (self->vtable == NULL)
2356     return FALSE;
2357
2358   info = &self->input_state->info;
2359   num_buffers =
2360       self->vtable->x264_encoder_maximum_delayed_frames (self->x264enc) + 1;
2361
2362   gst_query_add_allocation_pool (query, NULL, info->size, num_buffers, 0);
2363
2364   return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
2365       query);
2366 }
2367
2368 static void
2369 gst_x264_enc_add_cc (GstBuffer * buffer, x264_picture_t * pic_in)
2370 {
2371   GstVideoCaptionMeta *cc_meta;
2372   gpointer iter = NULL;
2373
2374   while ((cc_meta =
2375           (GstVideoCaptionMeta *) gst_buffer_iterate_meta_filtered (buffer,
2376               &iter, GST_VIDEO_CAPTION_META_API_TYPE))) {
2377     guint i = pic_in->extra_sei.num_payloads;
2378
2379     if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
2380       continue;
2381
2382     pic_in->extra_sei.num_payloads += 1;
2383
2384     if (!pic_in->extra_sei.payloads)
2385       pic_in->extra_sei.payloads = g_new0 (x264_sei_payload_t, 1);
2386     else
2387       pic_in->extra_sei.payloads =
2388           g_renew (x264_sei_payload_t, pic_in->extra_sei.payloads,
2389           pic_in->extra_sei.num_payloads);
2390
2391     pic_in->extra_sei.sei_free = g_free;
2392
2393     pic_in->extra_sei.payloads[i].payload_size = cc_meta->size + 11;
2394     pic_in->extra_sei.payloads[i].payload =
2395         g_malloc0 (pic_in->extra_sei.payloads[i].payload_size);
2396     pic_in->extra_sei.payloads[i].payload_type = 4;     /* Registered user data */
2397     memcpy (pic_in->extra_sei.payloads[i].payload + 10, cc_meta->data,
2398         cc_meta->size);
2399     pic_in->extra_sei.payloads[i].payload[0] = 181;     /* 8-bits itu_t_t35_country_code */
2400     pic_in->extra_sei.payloads[i].payload[1] = 0;       /* 16-bits itu_t_t35_provider_code */
2401     pic_in->extra_sei.payloads[i].payload[2] = 49;
2402     pic_in->extra_sei.payloads[i].payload[3] = 'G';     /* 32-bits ATSC_user_identifier */
2403     pic_in->extra_sei.payloads[i].payload[4] = 'A';
2404     pic_in->extra_sei.payloads[i].payload[5] = '9';
2405     pic_in->extra_sei.payloads[i].payload[6] = '4';
2406     pic_in->extra_sei.payloads[i].payload[7] = 3;       /* 8-bits ATSC1_data_user_data_type_code */
2407     /* 8-bits:
2408      * 1 bit process_em_data_flag (0)
2409      * 1 bit process_cc_data_flag (1)
2410      * 1 bit additional_data_flag (0)
2411      * 5-bits cc_count
2412      */
2413     pic_in->extra_sei.payloads[i].payload[8] =
2414         ((cc_meta->size / 3) & 0x1f) | 0x40;
2415     pic_in->extra_sei.payloads[i].payload[9] = 0;       /* 8 bits em_data, unused */
2416     pic_in->extra_sei.payloads[i].payload[cc_meta->size + 10] = 255;    /* 8 marker bits */
2417   }
2418 }
2419
2420 /* chain function
2421  * this function does the actual processing
2422  */
2423 static GstFlowReturn
2424 gst_x264_enc_handle_frame (GstVideoEncoder * video_enc,
2425     GstVideoCodecFrame * frame)
2426 {
2427   GstX264Enc *encoder = GST_X264_ENC (video_enc);
2428   GstVideoInfo *info = &encoder->input_state->info;
2429   GstFlowReturn ret;
2430   x264_picture_t pic_in;
2431   gint i_nal, i;
2432   FrameData *fdata;
2433   gint nplanes = encoder->x264_nplanes;
2434
2435   if (G_UNLIKELY (encoder->x264enc == NULL))
2436     goto not_inited;
2437
2438   /* create x264_picture_t from the buffer */
2439   /* mostly taken from mplayer (file ve_x264.c) */
2440
2441   /* set up input picture */
2442   memset (&pic_in, 0, sizeof (pic_in));
2443
2444   fdata = gst_x264_enc_queue_frame (encoder, frame, info);
2445   if (!fdata)
2446     goto invalid_frame;
2447
2448   pic_in.img.i_csp = encoder->x264param.i_csp;
2449   pic_in.img.i_plane = nplanes;
2450   for (i = 0; i < nplanes; i++) {
2451     pic_in.img.plane[i] = GST_VIDEO_FRAME_COMP_DATA (&fdata->vframe, i);
2452     pic_in.img.i_stride[i] = GST_VIDEO_FRAME_COMP_STRIDE (&fdata->vframe, i);
2453   }
2454
2455   pic_in.i_type = X264_TYPE_AUTO;
2456   pic_in.i_pts = frame->pts;
2457   pic_in.opaque = GINT_TO_POINTER (frame->system_frame_number);
2458
2459   if (GST_VIDEO_INFO_INTERLACE_MODE (info) == GST_VIDEO_INTERLACE_MODE_MIXED) {
2460     if ((fdata->vframe.flags & GST_VIDEO_FRAME_FLAG_INTERLACED) == 0) {
2461       pic_in.i_pic_struct = PIC_STRUCT_PROGRESSIVE;
2462     } else if ((fdata->vframe.flags & GST_VIDEO_FRAME_FLAG_RFF) != 0) {
2463       if ((fdata->vframe.flags & GST_VIDEO_FRAME_FLAG_TFF) != 0) {
2464         pic_in.i_pic_struct = PIC_STRUCT_TOP_BOTTOM_TOP;
2465       } else {
2466         pic_in.i_pic_struct = PIC_STRUCT_BOTTOM_TOP_BOTTOM;
2467       }
2468     } else {
2469       if ((fdata->vframe.flags & GST_VIDEO_FRAME_FLAG_TFF) != 0) {
2470         pic_in.i_pic_struct = PIC_STRUCT_TOP_BOTTOM;
2471       } else {
2472         pic_in.i_pic_struct = PIC_STRUCT_BOTTOM_TOP;
2473       }
2474     }
2475   }
2476
2477   gst_x264_enc_add_cc (frame->input_buffer, &pic_in);
2478
2479   ret = gst_x264_enc_encode_frame (encoder, &pic_in, frame, &i_nal, TRUE);
2480
2481   /* input buffer is released later on */
2482   return ret;
2483
2484 /* ERRORS */
2485 not_inited:
2486   {
2487     GST_WARNING_OBJECT (encoder, "Got buffer before set_caps was called");
2488     return GST_FLOW_NOT_NEGOTIATED;
2489   }
2490 invalid_frame:
2491   {
2492     GST_ERROR_OBJECT (encoder, "Failed to map frame");
2493     return GST_FLOW_ERROR;
2494   }
2495 }
2496
2497 static GstFlowReturn
2498 gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
2499     GstVideoCodecFrame * input_frame, int *i_nal, gboolean send)
2500 {
2501   GstVideoCodecFrame *frame = NULL;
2502   GstBuffer *out_buf = NULL;
2503   x264_picture_t pic_out;
2504   x264_nal_t *nal;
2505   int i_size;
2506   int encoder_return;
2507   GstFlowReturn ret = GST_FLOW_OK;
2508   guint8 *data;
2509   gboolean update_latency = FALSE;
2510
2511   if (G_UNLIKELY (encoder->x264enc == NULL)) {
2512     if (input_frame)
2513       gst_video_codec_frame_unref (input_frame);
2514     return GST_FLOW_NOT_NEGOTIATED;
2515   }
2516
2517   GST_OBJECT_LOCK (encoder);
2518   if (encoder->reconfig) {
2519     encoder->reconfig = FALSE;
2520     if (encoder->vtable->x264_encoder_reconfig (encoder->x264enc,
2521             &encoder->x264param) < 0)
2522       GST_WARNING_OBJECT (encoder, "Could not reconfigure");
2523     update_latency = TRUE;
2524   }
2525
2526   if (pic_in && input_frame) {
2527     if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (input_frame)) {
2528       GST_INFO_OBJECT (encoder, "Forcing key frame");
2529       if (encoder->intra_refresh)
2530         encoder->vtable->x264_encoder_intra_refresh (encoder->x264enc);
2531       else
2532         pic_in->i_type = X264_TYPE_IDR;
2533     }
2534   }
2535   GST_OBJECT_UNLOCK (encoder);
2536
2537   if (G_UNLIKELY (update_latency))
2538     gst_x264_enc_set_latency (encoder);
2539
2540   encoder_return = encoder->vtable->x264_encoder_encode (encoder->x264enc,
2541       &nal, i_nal, pic_in, &pic_out);
2542
2543   if (encoder_return < 0) {
2544     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 frame failed."),
2545         ("x264_encoder_encode return code=%d", encoder_return));
2546     ret = GST_FLOW_ERROR;
2547     /* Make sure we finish this frame */
2548     frame = input_frame;
2549     goto out;
2550   }
2551
2552   /* Input frame is now queued */
2553   if (input_frame)
2554     gst_video_codec_frame_unref (input_frame);
2555
2556   if (!*i_nal) {
2557     ret = GST_FLOW_OK;
2558     goto out;
2559   }
2560
2561   i_size = encoder_return;
2562   data = nal[0].p_payload;
2563
2564   frame = gst_video_encoder_get_frame (GST_VIDEO_ENCODER (encoder),
2565       GPOINTER_TO_INT (pic_out.opaque));
2566   g_assert (frame || !send);
2567
2568   if (!send || !frame) {
2569     ret = GST_FLOW_OK;
2570     goto out;
2571   }
2572
2573   out_buf = gst_buffer_new_allocate (NULL, i_size, NULL);
2574   gst_buffer_fill (out_buf, 0, data, i_size);
2575   frame->output_buffer = out_buf;
2576
2577   GST_LOG_OBJECT (encoder,
2578       "output: dts %" G_GINT64_FORMAT " pts %" G_GINT64_FORMAT,
2579       (gint64) pic_out.i_dts, (gint64) pic_out.i_pts);
2580
2581   /* we want to know if x264 is messing around with this */
2582   g_assert (frame->pts == pic_out.i_pts);
2583
2584   frame->dts = pic_out.i_dts;
2585   frame->pts = pic_out.i_pts;
2586
2587   if (pic_out.b_keyframe) {
2588     GST_DEBUG_OBJECT (encoder, "Output keyframe");
2589     GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
2590   }
2591
2592 out:
2593   if (frame) {
2594     gst_x264_enc_dequeue_frame (encoder, frame);
2595     ret = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (encoder), frame);
2596   }
2597
2598   return ret;
2599 }
2600
2601 static void
2602 gst_x264_enc_flush_frames (GstX264Enc * encoder, gboolean send)
2603 {
2604   GstFlowReturn flow_ret;
2605   gint i_nal;
2606
2607   /* first send the remaining frames */
2608   if (encoder->x264enc)
2609     do {
2610       flow_ret = gst_x264_enc_encode_frame (encoder, NULL, NULL, &i_nal, send);
2611     } while (flow_ret == GST_FLOW_OK
2612         && encoder->vtable->x264_encoder_delayed_frames (encoder->x264enc) > 0);
2613 }
2614
2615 static void
2616 gst_x264_enc_reconfig (GstX264Enc * encoder)
2617 {
2618   if (!encoder->vtable)
2619     return;
2620
2621   switch (encoder->pass) {
2622     case GST_X264_ENC_PASS_QUAL:
2623       encoder->x264param.rc.f_rf_constant = encoder->quantizer;
2624       encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
2625       encoder->x264param.rc.i_vbv_buffer_size
2626           = encoder->x264param.rc.i_vbv_max_bitrate
2627           * encoder->vbv_buf_capacity / 1000;
2628       break;
2629     case GST_X264_ENC_PASS_CBR:
2630     case GST_X264_ENC_PASS_PASS1:
2631     case GST_X264_ENC_PASS_PASS2:
2632     case GST_X264_ENC_PASS_PASS3:
2633     default:
2634       encoder->x264param.rc.i_bitrate = encoder->bitrate;
2635       encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
2636       encoder->x264param.rc.i_vbv_buffer_size
2637           = encoder->x264param.rc.i_vbv_max_bitrate
2638           * encoder->vbv_buf_capacity / 1000;
2639       break;
2640   }
2641
2642   encoder->reconfig = TRUE;
2643 }
2644
2645 static void
2646 gst_x264_enc_set_property (GObject * object, guint prop_id,
2647     const GValue * value, GParamSpec * pspec)
2648 {
2649   GstX264Enc *encoder;
2650   GstState state;
2651
2652   const gchar *partitions = NULL;
2653
2654   encoder = GST_X264_ENC (object);
2655
2656   GST_OBJECT_LOCK (encoder);
2657   /* state at least matters for sps, bytestream, pass,
2658    * and so by extension ... */
2659
2660   state = GST_STATE (encoder);
2661   if ((state != GST_STATE_READY && state != GST_STATE_NULL) &&
2662       !(pspec->flags & GST_PARAM_MUTABLE_PLAYING))
2663     goto wrong_state;
2664
2665   switch (prop_id) {
2666     case ARG_PASS:
2667       encoder->pass = g_value_get_enum (value);
2668       break;
2669     case ARG_QUANTIZER:
2670       encoder->quantizer = g_value_get_uint (value);
2671       gst_x264_enc_reconfig (encoder);
2672       break;
2673     case ARG_BITRATE:
2674       encoder->bitrate = g_value_get_uint (value);
2675       gst_x264_enc_reconfig (encoder);
2676       break;
2677     case ARG_VBV_BUF_CAPACITY:
2678       encoder->vbv_buf_capacity = g_value_get_uint (value);
2679       gst_x264_enc_reconfig (encoder);
2680       break;
2681     case ARG_SPEED_PRESET:
2682       encoder->speed_preset = g_value_get_enum (value);
2683       break;
2684     case ARG_PSY_TUNE:
2685       encoder->psy_tune = g_value_get_enum (value);
2686       break;
2687     case ARG_TUNE:
2688       encoder->tune = g_value_get_flags (value);
2689       break;
2690     case ARG_OPTION_STRING:
2691       g_string_assign (encoder->option_string_prop, g_value_get_string (value));
2692       break;
2693     case ARG_THREADS:
2694       encoder->threads = g_value_get_uint (value);
2695       g_string_append_printf (encoder->option_string, ":threads=%d",
2696           encoder->threads);
2697       break;
2698     case ARG_SLICED_THREADS:
2699       encoder->sliced_threads = g_value_get_boolean (value);
2700       g_string_append_printf (encoder->option_string, ":sliced-threads=%d",
2701           encoder->sliced_threads);
2702       break;
2703     case ARG_SYNC_LOOKAHEAD:
2704       encoder->sync_lookahead = g_value_get_int (value);
2705       g_string_append_printf (encoder->option_string, ":sync-lookahead=%d",
2706           encoder->sync_lookahead);
2707       break;
2708     case ARG_MULTIPASS_CACHE_FILE:
2709       g_free (encoder->mp_cache_file);
2710       encoder->mp_cache_file = g_value_dup_string (value);
2711       g_string_append_printf (encoder->option_string, ":stats=%s",
2712           encoder->mp_cache_file);
2713       break;
2714     case ARG_BYTE_STREAM:
2715       encoder->byte_stream = g_value_get_boolean (value);
2716       g_string_append_printf (encoder->option_string, ":annexb=%d",
2717           encoder->byte_stream);
2718       break;
2719     case ARG_INTRA_REFRESH:
2720       encoder->intra_refresh = g_value_get_boolean (value);
2721       g_string_append_printf (encoder->option_string, ":intra-refresh=%d",
2722           encoder->intra_refresh);
2723       break;
2724     case ARG_ME:
2725       encoder->me = g_value_get_enum (value);
2726       g_string_append_printf (encoder->option_string, ":me=%s",
2727           x264_motion_est_names[encoder->me]);
2728       break;
2729     case ARG_SUBME:
2730       encoder->subme = g_value_get_uint (value);
2731       g_string_append_printf (encoder->option_string, ":subme=%d",
2732           encoder->subme);
2733       break;
2734     case ARG_ANALYSE:
2735       encoder->analyse = g_value_get_flags (value);
2736       partitions = gst_x264_enc_build_partitions (encoder->analyse);
2737       if (partitions) {
2738         g_string_append_printf (encoder->option_string, ":partitions=%s",
2739             partitions);
2740         g_free ((gpointer) partitions);
2741       }
2742       break;
2743     case ARG_DCT8x8:
2744       encoder->dct8x8 = g_value_get_boolean (value);
2745       g_string_append_printf (encoder->option_string, ":8x8dct=%d",
2746           encoder->dct8x8);
2747       break;
2748     case ARG_REF:
2749       encoder->ref = g_value_get_uint (value);
2750       g_string_append_printf (encoder->option_string, ":ref=%d", encoder->ref);
2751       break;
2752     case ARG_BFRAMES:
2753       encoder->bframes = g_value_get_uint (value);
2754       g_string_append_printf (encoder->option_string, ":bframes=%d",
2755           encoder->bframes);
2756       break;
2757     case ARG_B_ADAPT:
2758       encoder->b_adapt = g_value_get_boolean (value);
2759       g_string_append_printf (encoder->option_string, ":b-adapt=%d",
2760           encoder->b_adapt);
2761       break;
2762     case ARG_B_PYRAMID:
2763       encoder->b_pyramid = g_value_get_boolean (value);
2764       g_string_append_printf (encoder->option_string, ":b-pyramid=%s",
2765           x264_b_pyramid_names[encoder->b_pyramid]);
2766       break;
2767     case ARG_WEIGHTB:
2768       encoder->weightb = g_value_get_boolean (value);
2769       g_string_append_printf (encoder->option_string, ":weightb=%d",
2770           encoder->weightb);
2771       break;
2772     case ARG_SPS_ID:
2773       encoder->sps_id = g_value_get_uint (value);
2774       g_string_append_printf (encoder->option_string, ":sps-id=%d",
2775           encoder->sps_id);
2776       break;
2777     case ARG_AU_NALU:
2778       encoder->au_nalu = g_value_get_boolean (value);
2779       g_string_append_printf (encoder->option_string, ":aud=%d",
2780           encoder->au_nalu);
2781       break;
2782     case ARG_TRELLIS:
2783       encoder->trellis = g_value_get_boolean (value);
2784       g_string_append_printf (encoder->option_string, ":trellis=%d",
2785           encoder->trellis);
2786       break;
2787     case ARG_KEYINT_MAX:
2788       encoder->keyint_max = g_value_get_uint (value);
2789       g_string_append_printf (encoder->option_string, ":keyint=%d",
2790           encoder->keyint_max);
2791       break;
2792     case ARG_CABAC:
2793       encoder->cabac = g_value_get_boolean (value);
2794       g_string_append_printf (encoder->option_string, ":cabac=%d",
2795           encoder->cabac);
2796       break;
2797     case ARG_QP_MIN:
2798       encoder->qp_min = g_value_get_uint (value);
2799       g_string_append_printf (encoder->option_string, ":qpmin=%d",
2800           encoder->qp_min);
2801       break;
2802     case ARG_QP_MAX:
2803       encoder->qp_max = g_value_get_uint (value);
2804       g_string_append_printf (encoder->option_string, ":qpmax=%d",
2805           encoder->qp_max);
2806       break;
2807     case ARG_QP_STEP:
2808       encoder->qp_step = g_value_get_uint (value);
2809       g_string_append_printf (encoder->option_string, ":qpstep=%d",
2810           encoder->qp_step);
2811       break;
2812     case ARG_IP_FACTOR:
2813       encoder->ip_factor = g_value_get_float (value);
2814       g_string_append_printf (encoder->option_string, ":ip-factor=%f",
2815           encoder->ip_factor);
2816       break;
2817     case ARG_PB_FACTOR:
2818       encoder->pb_factor = g_value_get_float (value);
2819       g_string_append_printf (encoder->option_string, ":pb-factor=%f",
2820           encoder->pb_factor);
2821       break;
2822     case ARG_RC_MB_TREE:
2823       encoder->mb_tree = g_value_get_boolean (value);
2824       g_string_append_printf (encoder->option_string, ":mbtree=%d",
2825           encoder->mb_tree);
2826       break;
2827     case ARG_RC_LOOKAHEAD:
2828       encoder->rc_lookahead = g_value_get_int (value);
2829       g_string_append_printf (encoder->option_string, ":rc-lookahead=%d",
2830           encoder->rc_lookahead);
2831       break;
2832     case ARG_NR:
2833       encoder->noise_reduction = g_value_get_uint (value);
2834       g_string_append_printf (encoder->option_string, ":nr=%d",
2835           encoder->noise_reduction);
2836       break;
2837     case ARG_INTERLACED:
2838       encoder->interlaced = g_value_get_boolean (value);
2839       break;
2840     case ARG_FRAME_PACKING:
2841       encoder->frame_packing = g_value_get_enum (value);
2842       break;
2843     case ARG_INSERT_VUI:
2844       encoder->insert_vui = g_value_get_boolean (value);
2845       break;
2846     default:
2847       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2848       break;
2849   }
2850   GST_OBJECT_UNLOCK (encoder);
2851   return;
2852
2853   /* ERROR */
2854 wrong_state:
2855   {
2856     GST_WARNING_OBJECT (encoder, "setting property in wrong state");
2857     GST_OBJECT_UNLOCK (encoder);
2858   }
2859 }
2860
2861 static void
2862 gst_x264_enc_get_property (GObject * object, guint prop_id,
2863     GValue * value, GParamSpec * pspec)
2864 {
2865   GstX264Enc *encoder;
2866
2867   encoder = GST_X264_ENC (object);
2868
2869   GST_OBJECT_LOCK (encoder);
2870   switch (prop_id) {
2871     case ARG_THREADS:
2872       g_value_set_uint (value, encoder->threads);
2873       break;
2874     case ARG_SLICED_THREADS:
2875       g_value_set_boolean (value, encoder->sliced_threads);
2876       break;
2877     case ARG_SYNC_LOOKAHEAD:
2878       g_value_set_int (value, encoder->sync_lookahead);
2879       break;
2880     case ARG_PASS:
2881       g_value_set_enum (value, encoder->pass);
2882       break;
2883     case ARG_QUANTIZER:
2884       g_value_set_uint (value, encoder->quantizer);
2885       break;
2886     case ARG_MULTIPASS_CACHE_FILE:
2887       g_value_set_string (value, encoder->mp_cache_file);
2888       break;
2889     case ARG_BYTE_STREAM:
2890       g_value_set_boolean (value, encoder->byte_stream);
2891       break;
2892     case ARG_BITRATE:
2893       g_value_set_uint (value, encoder->bitrate);
2894       break;
2895     case ARG_INTRA_REFRESH:
2896       g_value_set_boolean (value, encoder->intra_refresh);
2897       break;
2898     case ARG_VBV_BUF_CAPACITY:
2899       g_value_set_uint (value, encoder->vbv_buf_capacity);
2900       break;
2901     case ARG_ME:
2902       g_value_set_enum (value, encoder->me);
2903       break;
2904     case ARG_SUBME:
2905       g_value_set_uint (value, encoder->subme);
2906       break;
2907     case ARG_ANALYSE:
2908       g_value_set_flags (value, encoder->analyse);
2909       break;
2910     case ARG_DCT8x8:
2911       g_value_set_boolean (value, encoder->dct8x8);
2912       break;
2913     case ARG_REF:
2914       g_value_set_uint (value, encoder->ref);
2915       break;
2916     case ARG_BFRAMES:
2917       g_value_set_uint (value, encoder->bframes);
2918       break;
2919     case ARG_B_ADAPT:
2920       g_value_set_boolean (value, encoder->b_adapt);
2921       break;
2922     case ARG_B_PYRAMID:
2923       g_value_set_boolean (value, encoder->b_pyramid);
2924       break;
2925     case ARG_WEIGHTB:
2926       g_value_set_boolean (value, encoder->weightb);
2927       break;
2928     case ARG_SPS_ID:
2929       g_value_set_uint (value, encoder->sps_id);
2930       break;
2931     case ARG_AU_NALU:
2932       g_value_set_boolean (value, encoder->au_nalu);
2933       break;
2934     case ARG_TRELLIS:
2935       g_value_set_boolean (value, encoder->trellis);
2936       break;
2937     case ARG_KEYINT_MAX:
2938       g_value_set_uint (value, encoder->keyint_max);
2939       break;
2940     case ARG_QP_MIN:
2941       g_value_set_uint (value, encoder->qp_min);
2942       break;
2943     case ARG_QP_MAX:
2944       g_value_set_uint (value, encoder->qp_max);
2945       break;
2946     case ARG_QP_STEP:
2947       g_value_set_uint (value, encoder->qp_step);
2948       break;
2949     case ARG_CABAC:
2950       g_value_set_boolean (value, encoder->cabac);
2951       break;
2952     case ARG_IP_FACTOR:
2953       g_value_set_float (value, encoder->ip_factor);
2954       break;
2955     case ARG_PB_FACTOR:
2956       g_value_set_float (value, encoder->pb_factor);
2957       break;
2958     case ARG_RC_MB_TREE:
2959       g_value_set_boolean (value, encoder->mb_tree);
2960       break;
2961     case ARG_RC_LOOKAHEAD:
2962       g_value_set_int (value, encoder->rc_lookahead);
2963       break;
2964     case ARG_NR:
2965       g_value_set_uint (value, encoder->noise_reduction);
2966       break;
2967     case ARG_INTERLACED:
2968       g_value_set_boolean (value, encoder->interlaced);
2969       break;
2970     case ARG_SPEED_PRESET:
2971       g_value_set_enum (value, encoder->speed_preset);
2972       break;
2973     case ARG_PSY_TUNE:
2974       g_value_set_enum (value, encoder->psy_tune);
2975       break;
2976     case ARG_TUNE:
2977       g_value_set_flags (value, encoder->tune);
2978       break;
2979     case ARG_OPTION_STRING:
2980       g_value_set_string (value, encoder->option_string_prop->str);
2981       break;
2982     case ARG_FRAME_PACKING:
2983       g_value_set_enum (value, encoder->frame_packing);
2984       break;
2985     case ARG_INSERT_VUI:
2986       g_value_set_boolean (value, encoder->insert_vui);
2987       break;
2988     default:
2989       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2990       break;
2991   }
2992   GST_OBJECT_UNLOCK (encoder);
2993 }
2994
2995 static gboolean
2996 plugin_init (GstPlugin * plugin)
2997 {
2998   GST_DEBUG_CATEGORY_INIT (x264_enc_debug, "x264enc", 0,
2999       "h264 encoding element");
3000
3001   GST_INFO ("linked against x264 build: %u", X264_BUILD);
3002
3003   /* Initialize the static GstX264EncVTable which is overriden in load_x264()
3004    * if needed. We can't initialize statically because these values are not
3005    * constant on Windows. */
3006   default_vtable.module = NULL;
3007 #if X264_BUILD < 153
3008   default_vtable.x264_bit_depth = &x264_bit_depth;
3009 #endif
3010   default_vtable.x264_chroma_format = &x264_chroma_format;
3011   default_vtable.x264_encoder_close = x264_encoder_close;
3012   default_vtable.x264_encoder_delayed_frames = x264_encoder_delayed_frames;
3013   default_vtable.x264_encoder_encode = x264_encoder_encode;
3014   default_vtable.x264_encoder_headers = x264_encoder_headers;
3015   default_vtable.x264_encoder_intra_refresh = x264_encoder_intra_refresh;
3016   default_vtable.x264_encoder_maximum_delayed_frames =
3017       x264_encoder_maximum_delayed_frames;
3018   default_vtable.x264_encoder_open = x264_encoder_open;
3019   default_vtable.x264_encoder_reconfig = x264_encoder_reconfig;
3020   default_vtable.x264_levels = &x264_levels;
3021   default_vtable.x264_param_apply_fastfirstpass =
3022       x264_param_apply_fastfirstpass;
3023   default_vtable.x264_param_apply_profile = x264_param_apply_profile;
3024   default_vtable.x264_param_default_preset = x264_param_default_preset;
3025   default_vtable.x264_param_parse = x264_param_parse;
3026
3027   if (!load_x264_libraries ())
3028     return FALSE;
3029
3030   return gst_element_register (plugin, "x264enc",
3031       GST_RANK_PRIMARY, GST_TYPE_X264_ENC);
3032 }
3033
3034 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
3035     GST_VERSION_MINOR,
3036     x264,
3037     "libx264-based H264 plugins",
3038     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)