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 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
22 /**
23 * SECTION:element-x264enc
24 * @see_also: faac
25 *
26 * This element encodes raw video into H264 compressed data,
27 * also otherwise known as MPEG-4 AVC (Advanced Video Codec).
28 *
29 * The #GstX264Enc:pass property controls the type of encoding. In case of Constant
30 * Bitrate Encoding (actually ABR), the #GstX264Enc:bitrate will determine the quality
31 * of the encoding. This will similarly be the case if this target bitrate
32 * is to obtained in multiple (2 or 3) pass encoding.
33 * Alternatively, one may choose to perform Constant Quantizer or Quality encoding,
34 * in which case the #GstX264Enc:quantizer property controls much of the outcome, in that case #GstX264Enc:bitrate is the maximum bitrate.
35 *
36 * The H264 profile that is eventually used depends on a few settings.
37 * If #GstX264Enc:dct8x8 is enabled, then High profile is used.
38 * Otherwise, if #GstX264Enc:cabac entropy coding is enabled or #GstX264Enc:bframes
39 * are allowed, then Main Profile is in effect, and otherwise Baseline profile
40 * applies. No profile is imposed by default, which is fine for most software
41 * players and settings, but in some cases (e.g. hardware platforms) a more
42 * restricted profile/level may be necessary.
43 *
44 * If a preset/tuning are specified then these will define the default values and
45 * the property defaults will be ignored. After this the option-string property is
46 * applied, followed by the user-set properties, fast first pass restrictions and
47 * finally the profile restrictions.
48 *
49 * <note>Some settings, including the default settings, may lead to quite
50 * some latency (i.e. frame buffering) in the encoder. This may cause problems
51 * with pipeline stalling in non-trivial pipelines, because the encoder latency
52 * is often considerably higher than the default size of a simple queue
53 * element. Such problems are caused by one of the queues in the other
54 * non-x264enc streams/branches filling up and blocking upstream. They can
55 * be fixed by relaxing the default time/size/buffer limits on the queue
56 * elements in the non-x264 branches, or using a (single) multiqueue element
57 * for all branches. Also see the last example below.
58 * </note>
59 *
60 * <refsect2>
61 * <title>Example pipeline</title>
62 * |[
63 * gst-launch -v videotestsrc num-buffers=1000 ! x264enc qp-min=18 ! \
64 * avimux ! filesink location=videotestsrc.avi
65 * ]| This example pipeline will encode a test video source to H264 muxed in an
66 * AVI container, while ensuring a sane minimum quantization factor to avoid
67 * some (excessive) waste.
68 * |[
69 * gst-launch -v videotestsrc num-buffers=1000 ! x264enc pass=quant ! \
70 * matroskamux ! filesink location=videotestsrc.avi
71 * ]| This example pipeline will encode a test video source to H264 using fixed
72 * quantization, and muxes it in a Matroska container.
73 * |[
74 * gst-launch -v videotestsrc num-buffers=1000 ! x264enc pass=5 quantizer=25 speed-preset=6 profile=1 ! \
75 * qtmux ! filesink location=videotestsrc.mov
76 * ]| This example pipeline will encode a test video source to H264 using
77 * constant quality at around Q25 using the 'medium' speed/quality preset and
78 * restricting the options used so that the output is H.264 Baseline Profile
79 * compliant and finally multiplexing the output in Quicktime mov format.
80 * |[
81 * gst-launch -v videotestsrc num-buffers=1000 ! tee name=t ! queue ! xvimagesink \
82 * t. ! queue ! x264enc rc-lookahead=5 ! fakesink
83 * ]| This example pipeline will encode a test video source to H264 while
84 * displaying the input material at the same time. As mentioned above,
85 * specific settings are needed in this case to avoid pipeline stalling.
86 * Depending on goals and context, other approaches are possible, e.g.
87 * tune=zerolatency might be configured, or queue sizes increased.
88 * </refsect2>
89 */
91 #ifdef HAVE_CONFIG_H
92 # include "config.h"
93 #endif
95 #include "gstx264enc.h"
97 #if X264_BUILD >= 71
98 #define X264_DELAYED_FRAMES_API
99 #endif
101 #if X264_BUILD >= 76
102 #define X264_ENC_NALS 1
103 #endif
105 #if X264_BUILD >= 69
106 #define X264_MB_RC
107 #endif
109 #if X264_BUILD >= 78
110 /* b-pyramid was available before but was changed from boolean here */
111 #define X264_B_PYRAMID
112 #endif
114 #if X264_BUILD >= 80
115 #define X264_ENH_THREADING
116 #endif
118 #if X264_BUILD >= 82
119 #define X264_INTRA_REFRESH
120 #endif
122 #if X264_BUILD >= 86
123 #define X264_PRESETS
124 #endif
126 #include <string.h>
127 #include <stdlib.h>
129 GST_DEBUG_CATEGORY_STATIC (x264_enc_debug);
130 #define GST_CAT_DEFAULT x264_enc_debug
132 enum
133 {
134 ARG_0,
135 ARG_THREADS,
136 ARG_SLICED_THREADS,
137 ARG_SYNC_LOOKAHEAD,
138 ARG_PASS,
139 ARG_QUANTIZER,
140 ARG_STATS_FILE,
141 ARG_MULTIPASS_CACHE_FILE,
142 ARG_BYTE_STREAM,
143 ARG_BITRATE,
144 ARG_INTRA_REFRESH,
145 ARG_VBV_BUF_CAPACITY,
146 ARG_ME,
147 ARG_SUBME,
148 ARG_ANALYSE,
149 ARG_DCT8x8,
150 ARG_REF,
151 ARG_BFRAMES,
152 ARG_B_ADAPT,
153 ARG_B_PYRAMID,
154 ARG_WEIGHTB,
155 ARG_SPS_ID,
156 ARG_AU_NALU,
157 ARG_TRELLIS,
158 ARG_KEYINT_MAX,
159 ARG_CABAC,
160 ARG_QP_MIN,
161 ARG_QP_MAX,
162 ARG_QP_STEP,
163 ARG_IP_FACTOR,
164 ARG_PB_FACTOR,
165 ARG_RC_MB_TREE,
166 ARG_RC_LOOKAHEAD,
167 ARG_NR,
168 ARG_INTERLACED,
169 ARG_OPTION_STRING,
170 ARG_PROFILE,
171 ARG_SPEED_PRESET,
172 ARG_PSY_TUNE,
173 ARG_TUNE,
174 };
176 #define ARG_THREADS_DEFAULT 0 /* 0 means 'auto' which is 1.5x number of CPU cores */
177 #define ARG_PASS_DEFAULT 0
178 #define ARG_QUANTIZER_DEFAULT 21
179 #define ARG_MULTIPASS_CACHE_FILE_DEFAULT "x264.log"
180 #define ARG_STATS_FILE_DEFAULT ARG_MULTIPASS_CACHE_FILE_DEFAULT
181 #define ARG_BYTE_STREAM_DEFAULT FALSE
182 #define ARG_BITRATE_DEFAULT (2 * 1024)
183 #define ARG_VBV_BUF_CAPACITY_DEFAULT 600
184 #define ARG_ME_DEFAULT X264_ME_HEX
185 #define ARG_SUBME_DEFAULT 1
186 #define ARG_ANALYSE_DEFAULT 0
187 #define ARG_DCT8x8_DEFAULT FALSE
188 #define ARG_REF_DEFAULT 1
189 #define ARG_BFRAMES_DEFAULT 0
190 #define ARG_B_ADAPT_DEFAULT TRUE
191 #define ARG_B_PYRAMID_DEFAULT FALSE
192 #define ARG_WEIGHTB_DEFAULT FALSE
193 #define ARG_SPS_ID_DEFAULT 0
194 #define ARG_AU_NALU_DEFAULT TRUE
195 #define ARG_TRELLIS_DEFAULT TRUE
196 #define ARG_KEYINT_MAX_DEFAULT 0
197 #define ARG_CABAC_DEFAULT TRUE
198 #define ARG_QP_MIN_DEFAULT 10
199 #define ARG_QP_MAX_DEFAULT 51
200 #define ARG_QP_STEP_DEFAULT 4
201 #define ARG_IP_FACTOR_DEFAULT 1.4
202 #define ARG_PB_FACTOR_DEFAULT 1.3
203 #define ARG_NR_DEFAULT 0
204 #define ARG_INTERLACED_DEFAULT FALSE
205 #define ARG_SLICED_THREADS_DEFAULT FALSE
206 #define ARG_SYNC_LOOKAHEAD_DEFAULT -1
207 #define ARG_RC_MB_TREE_DEFAULT TRUE
208 #define ARG_RC_LOOKAHEAD_DEFAULT 40
209 #define ARG_INTRA_REFRESH_DEFAULT FALSE
210 #define ARG_PROFILE_DEFAULT 2 /* 'Main Profile' - matches profile of property defaults */
211 #define ARG_OPTION_STRING_DEFAULT ""
212 static GString *x264enc_defaults;
213 #define ARG_SPEED_PRESET_DEFAULT 6 /* 'medium' preset - matches x264 CLI default */
214 #define ARG_PSY_TUNE_DEFAULT 0 /* no psy tuning */
215 #define ARG_TUNE_DEFAULT 0 /* no tuning */
217 enum
218 {
219 GST_X264_ENC_PASS_CBR = 0,
220 GST_X264_ENC_PASS_QUANT = 0x04,
221 GST_X264_ENC_PASS_QUAL,
222 GST_X264_ENC_PASS_PASS1 = 0x11,
223 GST_X264_ENC_PASS_PASS2,
224 GST_X264_ENC_PASS_PASS3
225 };
227 #define GST_X264_ENC_PASS_TYPE (gst_x264_enc_pass_get_type())
228 static GType
229 gst_x264_enc_pass_get_type (void)
230 {
231 static GType pass_type = 0;
233 static const GEnumValue pass_types[] = {
234 {GST_X264_ENC_PASS_CBR, "Constant Bitrate Encoding", "cbr"},
235 {GST_X264_ENC_PASS_QUANT, "Constant Quantizer (debugging only)", "quant"},
236 {GST_X264_ENC_PASS_QUAL, "Constant Quality", "qual"},
237 {GST_X264_ENC_PASS_PASS1, "VBR Encoding - Pass 1", "pass1"},
238 {GST_X264_ENC_PASS_PASS2, "VBR Encoding - Pass 2", "pass2"},
239 {GST_X264_ENC_PASS_PASS3, "VBR Encoding - Pass 3", "pass3"},
240 {0, NULL, NULL}
241 };
243 if (!pass_type) {
244 pass_type = g_enum_register_static ("GstX264EncPass", pass_types);
245 }
246 return pass_type;
247 }
249 #define GST_X264_ENC_ME_TYPE (gst_x264_enc_me_get_type())
250 static GType
251 gst_x264_enc_me_get_type (void)
252 {
253 static GType me_type = 0;
254 static GEnumValue *me_types;
255 int n, i;
257 if (me_type != 0)
258 return me_type;
260 n = 0;
261 while (x264_motion_est_names[n] != NULL)
262 n++;
264 me_types = g_new0 (GEnumValue, n + 1);
266 for (i = 0; i < n; i++) {
267 me_types[i].value = i;
268 me_types[i].value_name = x264_motion_est_names[i];
269 me_types[i].value_nick = x264_motion_est_names[i];
270 }
272 me_type = g_enum_register_static ("GstX264EncMe", me_types);
274 return me_type;
275 }
277 #define GST_X264_ENC_ANALYSE_TYPE (gst_x264_enc_analyse_get_type())
278 static GType
279 gst_x264_enc_analyse_get_type (void)
280 {
281 static GType analyse_type = 0;
282 static const GFlagsValue analyse_types[] = {
283 {X264_ANALYSE_I4x4, "i4x4", "i4x4"},
284 {X264_ANALYSE_I8x8, "i8x8", "i8x8"},
285 {X264_ANALYSE_PSUB16x16, "p8x8", "p8x8"},
286 {X264_ANALYSE_PSUB8x8, "p4x4", "p4x4"},
287 {X264_ANALYSE_BSUB16x16, "b8x8", "b8x8"},
288 {0, NULL, NULL},
289 };
291 if (!analyse_type) {
292 analyse_type = g_flags_register_static ("GstX264EncAnalyse", analyse_types);
293 }
294 return analyse_type;
295 }
297 #ifdef X264_PRESETS
299 #define GST_X264_ENC_PROFILE_TYPE (gst_x264_enc_profile_get_type())
300 static GType
301 gst_x264_enc_profile_get_type (void)
302 {
303 static GType profile_type = 0;
304 static GEnumValue *profile_types;
305 int n, i;
307 if (profile_type != 0)
308 return profile_type;
310 n = 0;
311 while (x264_profile_names[n] != NULL)
312 n++;
314 profile_types = g_new0 (GEnumValue, n + 2);
316 i = 0;
317 profile_types[i].value = i;
318 profile_types[i].value_name = "No profile";
319 profile_types[i].value_nick = "None";
320 for (i = 1; i <= n; i++) {
321 profile_types[i].value = i;
322 profile_types[i].value_name = x264_profile_names[i - 1];
323 profile_types[i].value_nick = x264_profile_names[i - 1];
324 }
326 profile_type = g_enum_register_static ("GstX264EncProfile", profile_types);
328 return profile_type;
329 }
331 #define GST_X264_ENC_SPEED_PRESET_TYPE (gst_x264_enc_speed_preset_get_type())
332 static GType
333 gst_x264_enc_speed_preset_get_type (void)
334 {
335 static GType speed_preset_type = 0;
336 static GEnumValue *speed_preset_types;
337 int n, i;
339 if (speed_preset_type != 0)
340 return speed_preset_type;
342 n = 0;
343 while (x264_preset_names[n] != NULL)
344 n++;
346 speed_preset_types = g_new0 (GEnumValue, n + 2);
348 speed_preset_types[0].value = 0;
349 speed_preset_types[0].value_name = "No preset";
350 speed_preset_types[0].value_nick = "None";
352 for (i = 1; i <= n; i++) {
353 speed_preset_types[i].value = i;
354 speed_preset_types[i].value_name = x264_preset_names[i - 1];
355 speed_preset_types[i].value_nick = x264_preset_names[i - 1];
356 }
358 speed_preset_type =
359 g_enum_register_static ("GstX264EncPreset", speed_preset_types);
361 return speed_preset_type;
362 }
364 static const GFlagsValue tune_types[] = {
365 {0x0, "No tuning", "none"},
366 {0x1, "Still image", "stillimage"},
367 {0x2, "Fast decode", "fastdecode"},
368 {0x4, "Zero latency (requires constant framerate)", "zerolatency"},
369 {0, NULL, NULL},
370 };
372 #define GST_X264_ENC_TUNE_TYPE (gst_x264_enc_tune_get_type())
373 static GType
374 gst_x264_enc_tune_get_type (void)
375 {
376 static GType tune_type = 0;
378 if (!tune_type) {
379 tune_type = g_flags_register_static ("GstX264EncTune", tune_types + 1);
380 }
381 return tune_type;
382 }
384 enum
385 {
386 GST_X264_ENC_TUNE_NONE,
387 GST_X264_ENC_TUNE_FILM,
388 GST_X264_ENC_TUNE_ANIMATION,
389 GST_X264_ENC_TUNE_GRAIN,
390 GST_X264_ENC_TUNE_PSNR,
391 GST_X264_ENC_TUNE_SSIM,
392 GST_X264_ENC_TUNE_LAST
393 };
395 static const GEnumValue psy_tune_types[] = {
396 {GST_X264_ENC_TUNE_NONE, "No tuning", "none"},
397 {GST_X264_ENC_TUNE_FILM, "Film", "film"},
398 {GST_X264_ENC_TUNE_ANIMATION, "Animation", "animation"},
399 {GST_X264_ENC_TUNE_GRAIN, "Grain", "grain"},
400 {GST_X264_ENC_TUNE_PSNR, "PSNR", "psnr"},
401 {GST_X264_ENC_TUNE_SSIM, "SSIM", "ssim"},
402 {0, NULL, NULL},
403 };
405 #define GST_X264_ENC_PSY_TUNE_TYPE (gst_x264_enc_psy_tune_get_type())
406 static GType
407 gst_x264_enc_psy_tune_get_type (void)
408 {
409 static GType psy_tune_type = 0;
411 if (!psy_tune_type) {
412 psy_tune_type =
413 g_enum_register_static ("GstX264EncPsyTune", psy_tune_types);
414 }
415 return psy_tune_type;
416 }
418 static void
419 gst_x264_enc_build_tunings_string (GstX264Enc * x264enc)
420 {
421 int i = 1;
423 if (x264enc->tunings && x264enc->tunings->len)
424 g_string_free (x264enc->tunings, TRUE);
426 if (x264enc->psy_tune) {
427 x264enc->tunings =
428 g_string_new (psy_tune_types[x264enc->psy_tune].value_nick);
429 } else {
430 x264enc->tunings = g_string_new (NULL);
431 }
433 while (tune_types[i].value_name) {
434 if (x264enc->tune & (1 << (i - 1)))
435 g_string_append_printf (x264enc->tunings, "%s%s",
436 x264enc->tunings->len ? "," : "", tune_types[i].value_nick);
437 i++;
438 }
440 if (x264enc->tunings->len)
441 GST_DEBUG_OBJECT (x264enc, "Constructed tunings string: %s",
442 x264enc->tunings->str);
443 }
445 #endif
448 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
449 GST_PAD_SINK,
450 GST_PAD_ALWAYS,
451 GST_STATIC_CAPS ("video/x-raw-yuv, "
452 "format = (fourcc) { I420, YV12 }, "
453 "framerate = (fraction) [0, MAX], "
454 "width = (int) [ 16, MAX ], " "height = (int) [ 16, MAX ]")
455 );
457 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
458 GST_PAD_SRC,
459 GST_PAD_ALWAYS,
460 GST_STATIC_CAPS ("video/x-h264, "
461 "framerate = (fraction) [0/1, MAX], "
462 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
463 "stream-format = (string) { byte-stream, avc }, "
464 "alignment = (string) { au }")
465 );
467 static void gst_x264_enc_finalize (GObject * object);
468 static void gst_x264_enc_reset (GstX264Enc * encoder);
470 static gboolean gst_x264_enc_init_encoder (GstX264Enc * encoder);
471 static void gst_x264_enc_close_encoder (GstX264Enc * encoder);
473 static gboolean gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps);
474 static GstCaps *gst_x264_enc_sink_get_caps (GstPad * pad);
475 static gboolean gst_x264_enc_sink_event (GstPad * pad, GstEvent * event);
476 static gboolean gst_x264_enc_src_event (GstPad * pad, GstEvent * event);
477 static GstFlowReturn gst_x264_enc_chain (GstPad * pad, GstBuffer * buf);
478 static void gst_x264_enc_flush_frames (GstX264Enc * encoder, gboolean send);
479 static GstFlowReturn gst_x264_enc_encode_frame (GstX264Enc * encoder,
480 x264_picture_t * pic_in, int *i_nal, gboolean send);
481 static GstStateChangeReturn gst_x264_enc_change_state (GstElement * element,
482 GstStateChange transition);
484 static void gst_x264_enc_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec);
486 static void gst_x264_enc_get_property (GObject * object, guint prop_id,
487 GValue * value, GParamSpec * pspec);
489 static void
490 _do_init (GType object_type)
491 {
492 const GInterfaceInfo preset_interface_info = {
493 NULL, /* interface_init */
494 NULL, /* interface_finalize */
495 NULL /* interface_data */
496 };
498 g_type_add_interface_static (object_type, GST_TYPE_PRESET,
499 &preset_interface_info);
500 }
502 GST_BOILERPLATE_FULL (GstX264Enc, gst_x264_enc, GstElement, GST_TYPE_ELEMENT,
503 _do_init);
505 static void
506 gst_x264_enc_base_init (gpointer g_class)
507 {
508 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
510 gst_element_class_set_details_simple (element_class,
511 "x264enc", "Codec/Encoder/Video", "H264 Encoder",
512 "Josef Zlomek <josef.zlomek@itonis.tv>, "
513 "Mark Nauwelaerts <mnauw@users.sf.net>");
515 gst_element_class_add_pad_template (element_class,
516 gst_static_pad_template_get (&src_factory));
517 gst_element_class_add_pad_template (element_class,
518 gst_static_pad_template_get (&sink_factory));
519 }
521 /* don't forget to free the string after use */
522 static const gchar *
523 gst_x264_enc_build_partitions (gint analyse)
524 {
525 GString *string;
527 if (!analyse)
528 return NULL;
530 string = g_string_new (NULL);
531 if (analyse & X264_ANALYSE_I4x4)
532 g_string_append (string, "i4x4");
533 if (analyse & X264_ANALYSE_I8x8)
534 g_string_append (string, ",i8x8");
535 if (analyse & X264_ANALYSE_PSUB16x16)
536 g_string_append (string, ",p8x8");
537 if (analyse & X264_ANALYSE_PSUB8x8)
538 g_string_append (string, ",p4x4");
539 if (analyse & X264_ANALYSE_BSUB16x16)
540 g_string_append (string, ",b8x8");
542 return (const gchar *) g_string_free (string, FALSE);
543 }
545 static void
546 gst_x264_enc_class_init (GstX264EncClass * klass)
547 {
548 GObjectClass *gobject_class;
549 GstElementClass *gstelement_class;
551 const gchar *partitions = NULL;
553 x264enc_defaults = g_string_new ("");
555 gobject_class = (GObjectClass *) klass;
556 gstelement_class = (GstElementClass *) klass;
558 gobject_class->set_property = gst_x264_enc_set_property;
559 gobject_class->get_property = gst_x264_enc_get_property;
560 gobject_class->finalize = gst_x264_enc_finalize;
562 gstelement_class->change_state =
563 GST_DEBUG_FUNCPTR (gst_x264_enc_change_state);
565 /* options for which we don't use string equivalents */
566 g_object_class_install_property (gobject_class, ARG_PASS,
567 g_param_spec_enum ("pass", "Encoding pass/type",
568 "Encoding pass/type", GST_X264_ENC_PASS_TYPE,
569 ARG_PASS_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 g_object_class_install_property (gobject_class, ARG_QUANTIZER,
571 g_param_spec_uint ("quantizer", "Constant Quantizer",
572 "Constant quantizer or quality to apply",
573 1, 50, ARG_QUANTIZER_DEFAULT,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
575 g_object_class_install_property (gobject_class, ARG_BITRATE,
576 g_param_spec_uint ("bitrate", "Bitrate", "Bitrate in kbit/sec", 1,
577 100 * 1024, ARG_BITRATE_DEFAULT,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
579 GST_PARAM_MUTABLE_PLAYING));
580 g_object_class_install_property (gobject_class, ARG_VBV_BUF_CAPACITY,
581 g_param_spec_uint ("vbv-buf-capacity", "VBV buffer capacity",
582 "Size of the VBV buffer in milliseconds",
583 0, 10000, ARG_VBV_BUF_CAPACITY_DEFAULT,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
585 GST_PARAM_MUTABLE_PLAYING));
587 #ifdef X264_PRESETS
588 g_object_class_install_property (gobject_class, ARG_SPEED_PRESET,
589 g_param_spec_enum ("speed-preset", "Speed/quality preset",
590 "Preset name for speed/quality tradeoff options (can affect decode "
591 "compatibility - impose restrictions separately for your target decoder)",
592 GST_X264_ENC_SPEED_PRESET_TYPE, ARG_SPEED_PRESET_DEFAULT,
593 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594 g_object_class_install_property (gobject_class, ARG_PSY_TUNE,
595 g_param_spec_enum ("psy-tune", "Psychovisual tuning preset",
596 "Preset name for psychovisual tuning options",
597 GST_X264_ENC_PSY_TUNE_TYPE, ARG_PSY_TUNE_DEFAULT,
598 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
599 g_object_class_install_property (gobject_class, ARG_TUNE,
600 g_param_spec_flags ("tune", "Content tuning preset",
601 "Preset name for non-psychovisual tuning options",
602 GST_X264_ENC_TUNE_TYPE, ARG_TUNE_DEFAULT,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604 g_object_class_install_property (gobject_class, ARG_PROFILE,
605 g_param_spec_enum ("profile", "H.264 profile",
606 "Apply restrictions to meet H.264 Profile constraints. This will "
607 "override other properties if necessary.",
608 GST_X264_ENC_PROFILE_TYPE, ARG_PROFILE_DEFAULT,
609 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
610 #endif /* X264_PRESETS */
611 g_object_class_install_property (gobject_class, ARG_OPTION_STRING,
612 g_param_spec_string ("option-string", "Option string",
613 "String of x264 options (overridden by element properties)",
614 ARG_OPTION_STRING_DEFAULT,
615 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
617 /* options for which we _do_ use string equivalents */
618 g_object_class_install_property (gobject_class, ARG_THREADS,
619 g_param_spec_uint ("threads", "Threads",
620 "Number of threads used by the codec (0 for automatic)",
621 0, 4, ARG_THREADS_DEFAULT,
622 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
623 /* NOTE: this first string append doesn't require the ':' delimiter but the
624 * rest do */
625 g_string_append_printf (x264enc_defaults, "threads=%d", ARG_THREADS_DEFAULT);
626 #ifdef X264_ENH_THREADING
627 g_object_class_install_property (gobject_class, ARG_SLICED_THREADS,
628 g_param_spec_boolean ("sliced-threads", "Sliced Threads",
629 "Low latency but lower efficiency threading",
630 ARG_SLICED_THREADS_DEFAULT,
631 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
632 g_string_append_printf (x264enc_defaults, ":sliced-threads=%d",
633 ARG_SLICED_THREADS_DEFAULT);
634 g_object_class_install_property (gobject_class, ARG_SYNC_LOOKAHEAD,
635 g_param_spec_int ("sync-lookahead", "Sync Lookahead",
636 "Number of buffer frames for threaded lookahead (-1 for automatic)",
637 -1, 250, ARG_SYNC_LOOKAHEAD_DEFAULT,
638 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
639 g_string_append_printf (x264enc_defaults, ":sync-lookahead=%d",
640 ARG_SYNC_LOOKAHEAD_DEFAULT);
641 #endif
642 g_object_class_install_property (gobject_class, ARG_STATS_FILE,
643 g_param_spec_string ("stats-file", "Stats File",
644 "Filename for multipass statistics (deprecated, use multipass-cache-file)",
645 ARG_STATS_FILE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
646 g_object_class_install_property (gobject_class, ARG_MULTIPASS_CACHE_FILE,
647 g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
648 "Filename for multipass cache file",
649 ARG_MULTIPASS_CACHE_FILE_DEFAULT,
650 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
651 g_string_append_printf (x264enc_defaults, ":stats=%s",
652 ARG_MULTIPASS_CACHE_FILE_DEFAULT);
653 g_object_class_install_property (gobject_class, ARG_BYTE_STREAM,
654 g_param_spec_boolean ("byte-stream", "Byte Stream",
655 "Generate byte stream format of NALU", ARG_BYTE_STREAM_DEFAULT,
656 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
657 g_string_append_printf (x264enc_defaults, ":annexb=%d",
658 ARG_BYTE_STREAM_DEFAULT);
659 #ifdef X264_INTRA_REFRESH
660 g_object_class_install_property (gobject_class, ARG_INTRA_REFRESH,
661 g_param_spec_boolean ("intra-refresh", "Intra Refresh",
662 "Use Periodic Intra Refresh instead of IDR frames",
663 ARG_INTRA_REFRESH_DEFAULT,
664 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
665 g_string_append_printf (x264enc_defaults, ":intra-refresh=%d",
666 ARG_INTRA_REFRESH_DEFAULT);
667 #endif
668 g_object_class_install_property (gobject_class, ARG_ME,
669 g_param_spec_enum ("me", "Motion Estimation",
670 "Integer pixel motion estimation method", GST_X264_ENC_ME_TYPE,
671 ARG_ME_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
672 g_string_append_printf (x264enc_defaults, ":me=%s",
673 x264_motion_est_names[ARG_ME_DEFAULT]);
674 g_object_class_install_property (gobject_class, ARG_SUBME,
675 g_param_spec_uint ("subme", "Subpixel Motion Estimation",
676 "Subpixel motion estimation and partition decision quality: 1=fast, 10=best",
677 1, 10, ARG_SUBME_DEFAULT,
678 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
679 g_string_append_printf (x264enc_defaults, ":subme=%d", ARG_SUBME_DEFAULT);
680 g_object_class_install_property (gobject_class, ARG_ANALYSE,
681 g_param_spec_flags ("analyse", "Analyse", "Partitions to consider",
682 GST_X264_ENC_ANALYSE_TYPE, ARG_ANALYSE_DEFAULT,
683 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
684 partitions = gst_x264_enc_build_partitions (ARG_ANALYSE_DEFAULT);
685 if (partitions) {
686 g_string_append_printf (x264enc_defaults, ":partitions=%s", partitions);
687 g_free ((gpointer) partitions);
688 }
689 g_object_class_install_property (gobject_class, ARG_DCT8x8,
690 g_param_spec_boolean ("dct8x8", "DCT8x8",
691 "Adaptive spatial transform size", ARG_DCT8x8_DEFAULT,
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693 g_string_append_printf (x264enc_defaults, ":8x8dct=%d", ARG_DCT8x8_DEFAULT);
694 g_object_class_install_property (gobject_class, ARG_REF,
695 g_param_spec_uint ("ref", "Reference Frames",
696 "Number of reference frames",
697 1, 12, ARG_REF_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
698 g_string_append_printf (x264enc_defaults, ":ref=%d", ARG_REF_DEFAULT);
699 g_object_class_install_property (gobject_class, ARG_BFRAMES,
700 g_param_spec_uint ("bframes", "B-Frames",
701 "Number of B-frames between I and P",
702 0, 4, ARG_BFRAMES_DEFAULT,
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704 g_string_append_printf (x264enc_defaults, ":bframes=%d", ARG_BFRAMES_DEFAULT);
705 g_object_class_install_property (gobject_class, ARG_B_ADAPT,
706 g_param_spec_boolean ("b-adapt", "B-Adapt",
707 "Automatically decide how many B-frames to use",
708 ARG_B_ADAPT_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
709 g_string_append_printf (x264enc_defaults, ":b-adapt=%d", ARG_B_ADAPT_DEFAULT);
710 g_object_class_install_property (gobject_class, ARG_B_PYRAMID,
711 g_param_spec_boolean ("b-pyramid", "B-Pyramid",
712 "Keep some B-frames as references", ARG_B_PYRAMID_DEFAULT,
713 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
714 #ifdef X264_B_PYRAMID
715 g_string_append_printf (x264enc_defaults, ":b-pyramid=%s",
716 x264_b_pyramid_names[ARG_B_PYRAMID_DEFAULT]);
717 #else
718 g_string_append_printf (x264enc_defaults, ":b-pyramid=%d",
719 ARG_B_PYRAMID_DEFAULT);
720 #endif /* X264_B_PYRAMID */
721 g_object_class_install_property (gobject_class, ARG_WEIGHTB,
722 g_param_spec_boolean ("weightb", "Weighted B-Frames",
723 "Weighted prediction for B-frames", ARG_WEIGHTB_DEFAULT,
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
725 g_string_append_printf (x264enc_defaults, ":weightb=%d", ARG_WEIGHTB_DEFAULT);
726 g_object_class_install_property (gobject_class, ARG_SPS_ID,
727 g_param_spec_uint ("sps-id", "SPS ID",
728 "SPS and PPS ID number",
729 0, 31, ARG_SPS_ID_DEFAULT,
730 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
731 g_string_append_printf (x264enc_defaults, ":sps-id=%d", ARG_SPS_ID_DEFAULT);
732 g_object_class_install_property (gobject_class, ARG_AU_NALU,
733 g_param_spec_boolean ("aud", "AUD",
734 "Use AU (Access Unit) delimiter", ARG_AU_NALU_DEFAULT,
735 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
736 g_string_append_printf (x264enc_defaults, ":aud=%d", ARG_AU_NALU_DEFAULT);
737 g_object_class_install_property (gobject_class, ARG_TRELLIS,
738 g_param_spec_boolean ("trellis", "Trellis quantization",
739 "Enable trellis searched quantization", ARG_TRELLIS_DEFAULT,
740 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
741 g_string_append_printf (x264enc_defaults, ":trellis=%d", ARG_TRELLIS_DEFAULT);
742 g_object_class_install_property (gobject_class, ARG_KEYINT_MAX,
743 g_param_spec_uint ("key-int-max", "Key-frame maximal interval",
744 "Maximal distance between two key-frames (0 for automatic)",
745 0, G_MAXINT, ARG_KEYINT_MAX_DEFAULT,
746 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
747 g_string_append_printf (x264enc_defaults, ":keyint=%d",
748 ARG_KEYINT_MAX_DEFAULT);
749 g_object_class_install_property (gobject_class, ARG_CABAC,
750 g_param_spec_boolean ("cabac", "Use CABAC", "Enable CABAC entropy coding",
751 ARG_CABAC_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 g_string_append_printf (x264enc_defaults, ":cabac=%d", ARG_CABAC_DEFAULT);
753 g_object_class_install_property (gobject_class, ARG_QP_MIN,
754 g_param_spec_uint ("qp-min", "Minimum Quantizer",
755 "Minimum quantizer", 1, 51, ARG_QP_MIN_DEFAULT,
756 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
757 g_string_append_printf (x264enc_defaults, ":qpmin=%d", ARG_QP_MIN_DEFAULT);
758 g_object_class_install_property (gobject_class, ARG_QP_MAX,
759 g_param_spec_uint ("qp-max", "Maximum Quantizer",
760 "Maximum quantizer", 1, 51, ARG_QP_MAX_DEFAULT,
761 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
762 g_string_append_printf (x264enc_defaults, ":qpmax=%d", ARG_QP_MAX_DEFAULT);
763 g_object_class_install_property (gobject_class, ARG_QP_STEP,
764 g_param_spec_uint ("qp-step", "Maximum Quantizer Difference",
765 "Maximum quantizer difference between frames",
766 1, 50, ARG_QP_STEP_DEFAULT,
767 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
768 g_string_append_printf (x264enc_defaults, ":qpstep=%d", ARG_QP_STEP_DEFAULT);
769 g_object_class_install_property (gobject_class, ARG_IP_FACTOR,
770 g_param_spec_float ("ip-factor", "IP-Factor",
771 "Quantizer factor between I- and P-frames",
772 0, 2, ARG_IP_FACTOR_DEFAULT,
773 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
774 g_string_append_printf (x264enc_defaults, ":ip-factor=%f",
775 ARG_IP_FACTOR_DEFAULT);
776 g_object_class_install_property (gobject_class, ARG_PB_FACTOR,
777 g_param_spec_float ("pb-factor", "PB-Factor",
778 "Quantizer factor between P- and B-frames", 0, 2,
779 ARG_PB_FACTOR_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
780 g_string_append_printf (x264enc_defaults, ":pb-factor=%f",
781 ARG_PB_FACTOR_DEFAULT);
782 #ifdef X264_MB_RC
783 g_object_class_install_property (gobject_class, ARG_RC_MB_TREE,
784 g_param_spec_boolean ("mb-tree", "Macroblock Tree",
785 "Macroblock-Tree ratecontrol",
786 ARG_RC_MB_TREE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
787 g_string_append_printf (x264enc_defaults, ":mbtree=%d",
788 ARG_RC_MB_TREE_DEFAULT);
789 g_object_class_install_property (gobject_class, ARG_RC_LOOKAHEAD,
790 g_param_spec_int ("rc-lookahead", "Rate Control Lookahead",
791 "Number of frames for frametype lookahead", 0, 250,
792 ARG_RC_LOOKAHEAD_DEFAULT,
793 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
794 g_string_append_printf (x264enc_defaults, ":rc-lookahead=%d",
795 ARG_RC_LOOKAHEAD_DEFAULT);
796 #endif
797 g_object_class_install_property (gobject_class, ARG_NR,
798 g_param_spec_uint ("noise-reduction", "Noise Reduction",
799 "Noise reduction strength",
800 0, 100000, ARG_NR_DEFAULT,
801 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
802 g_string_append_printf (x264enc_defaults, ":nr=%d", ARG_NR_DEFAULT);
803 g_object_class_install_property (gobject_class, ARG_INTERLACED,
804 g_param_spec_boolean ("interlaced", "Interlaced",
805 "Interlaced material", ARG_INTERLACED_DEFAULT,
806 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
807 g_string_append_printf (x264enc_defaults, ":interlaced=%d",
808 ARG_INTERLACED_DEFAULT);
810 /* append deblock parameters */
811 g_string_append_printf (x264enc_defaults, ":deblock=0,0");
812 /* append weighted prediction parameter */
813 g_string_append_printf (x264enc_defaults, ":weightp=0");
814 }
816 static void
817 gst_x264_enc_log_callback (gpointer private, gint level, const char *format,
818 va_list args)
819 {
820 #ifndef GST_DISABLE_GST_DEBUG
821 GstDebugLevel gst_level;
822 GObject *object = (GObject *) private;
824 switch (level) {
825 case X264_LOG_NONE:
826 gst_level = GST_LEVEL_NONE;
827 break;
828 case X264_LOG_ERROR:
829 gst_level = GST_LEVEL_ERROR;
830 break;
831 case X264_LOG_WARNING:
832 gst_level = GST_LEVEL_WARNING;
833 break;
834 case X264_LOG_INFO:
835 gst_level = GST_LEVEL_INFO;
836 break;
837 default:
838 /* push x264enc debug down to our lower levels to avoid some clutter */
839 gst_level = GST_LEVEL_LOG;
840 break;
841 }
843 gst_debug_log_valist (x264_enc_debug, gst_level, "", "", 0, object, format,
844 args);
845 #endif /* GST_DISABLE_GST_DEBUG */
846 }
848 /* initialize the new element
849 * instantiate pads and add them to element
850 * set functions
851 * initialize structure
852 */
853 static void
854 gst_x264_enc_init (GstX264Enc * encoder, GstX264EncClass * klass)
855 {
856 encoder->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
857 gst_pad_set_setcaps_function (encoder->sinkpad,
858 GST_DEBUG_FUNCPTR (gst_x264_enc_sink_set_caps));
859 gst_pad_set_getcaps_function (encoder->sinkpad,
860 GST_DEBUG_FUNCPTR (gst_x264_enc_sink_get_caps));
861 gst_pad_set_event_function (encoder->sinkpad,
862 GST_DEBUG_FUNCPTR (gst_x264_enc_sink_event));
863 gst_pad_set_chain_function (encoder->sinkpad,
864 GST_DEBUG_FUNCPTR (gst_x264_enc_chain));
865 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
867 encoder->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
868 gst_pad_use_fixed_caps (encoder->srcpad);
869 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
871 gst_pad_set_event_function (encoder->srcpad,
872 GST_DEBUG_FUNCPTR (gst_x264_enc_src_event));
874 /* properties */
875 encoder->threads = ARG_THREADS_DEFAULT;
876 encoder->sliced_threads = ARG_SLICED_THREADS_DEFAULT;
877 encoder->sync_lookahead = ARG_SYNC_LOOKAHEAD_DEFAULT;
878 encoder->pass = ARG_PASS_DEFAULT;
879 encoder->quantizer = ARG_QUANTIZER_DEFAULT;
880 encoder->mp_cache_file = g_strdup (ARG_MULTIPASS_CACHE_FILE_DEFAULT);
881 encoder->byte_stream = ARG_BYTE_STREAM_DEFAULT;
882 encoder->bitrate = ARG_BITRATE_DEFAULT;
883 encoder->intra_refresh = ARG_INTRA_REFRESH_DEFAULT;
884 encoder->vbv_buf_capacity = ARG_VBV_BUF_CAPACITY_DEFAULT;
885 encoder->me = ARG_ME_DEFAULT;
886 encoder->subme = ARG_SUBME_DEFAULT;
887 encoder->analyse = ARG_ANALYSE_DEFAULT;
888 encoder->dct8x8 = ARG_DCT8x8_DEFAULT;
889 encoder->ref = ARG_REF_DEFAULT;
890 encoder->bframes = ARG_BFRAMES_DEFAULT;
891 encoder->b_adapt = ARG_B_ADAPT_DEFAULT;
892 encoder->b_pyramid = ARG_B_PYRAMID_DEFAULT;
893 encoder->weightb = ARG_WEIGHTB_DEFAULT;
894 encoder->sps_id = ARG_SPS_ID_DEFAULT;
895 encoder->au_nalu = ARG_AU_NALU_DEFAULT;
896 encoder->trellis = ARG_TRELLIS_DEFAULT;
897 encoder->keyint_max = ARG_KEYINT_MAX_DEFAULT;
898 encoder->cabac = ARG_CABAC_DEFAULT;
899 encoder->qp_min = ARG_QP_MIN_DEFAULT;
900 encoder->qp_max = ARG_QP_MAX_DEFAULT;
901 encoder->qp_step = ARG_QP_STEP_DEFAULT;
902 encoder->ip_factor = ARG_IP_FACTOR_DEFAULT;
903 encoder->pb_factor = ARG_PB_FACTOR_DEFAULT;
904 encoder->mb_tree = ARG_RC_MB_TREE_DEFAULT;
905 encoder->rc_lookahead = ARG_RC_LOOKAHEAD_DEFAULT;
906 encoder->noise_reduction = ARG_NR_DEFAULT;
907 encoder->interlaced = ARG_INTERLACED_DEFAULT;
908 encoder->profile = ARG_PROFILE_DEFAULT;
909 encoder->option_string = g_string_new (NULL);
910 encoder->option_string_prop = g_string_new (ARG_OPTION_STRING_DEFAULT);
911 encoder->speed_preset = ARG_SPEED_PRESET_DEFAULT;
912 encoder->psy_tune = ARG_PSY_TUNE_DEFAULT;
913 encoder->tune = ARG_TUNE_DEFAULT;
915 /* resources */
916 encoder->delay = g_queue_new ();
917 encoder->buffer_size = 100000;
918 encoder->buffer = g_malloc (encoder->buffer_size);
920 x264_param_default (&encoder->x264param);
922 /* log callback setup; part of parameters */
923 encoder->x264param.pf_log = gst_x264_enc_log_callback;
924 encoder->x264param.p_log_private = encoder;
925 encoder->x264param.i_log_level = X264_LOG_DEBUG;
927 gst_x264_enc_reset (encoder);
928 }
930 static void
931 gst_x264_enc_reset (GstX264Enc * encoder)
932 {
933 encoder->x264enc = NULL;
934 encoder->width = 0;
935 encoder->height = 0;
937 GST_OBJECT_LOCK (encoder);
938 encoder->i_type = X264_TYPE_AUTO;
939 if (encoder->forcekeyunit_event)
940 gst_event_unref (encoder->forcekeyunit_event);
941 encoder->forcekeyunit_event = NULL;
942 GST_OBJECT_UNLOCK (encoder);
943 }
945 static void
946 gst_x264_enc_finalize (GObject * object)
947 {
948 GstX264Enc *encoder = GST_X264_ENC (object);
950 #define FREE_STRING(ptr) \
951 if (ptr) \
952 ptr = (GString *)g_string_free (ptr, TRUE);
954 FREE_STRING (encoder->tunings);
955 FREE_STRING (encoder->option_string);
956 FREE_STRING (encoder->option_string_prop);
958 #undef FREE_STRING
960 g_free (encoder->mp_cache_file);
961 encoder->mp_cache_file = NULL;
962 g_free (encoder->buffer);
963 encoder->buffer = NULL;
964 g_queue_free (encoder->delay);
965 encoder->delay = NULL;
967 gst_x264_enc_close_encoder (encoder);
969 G_OBJECT_CLASS (parent_class)->finalize (object);
970 }
972 /*
973 * gst_x264_enc_parse_options
974 * @encoder: Encoder to which options are assigned
975 * @str: Option string
976 *
977 * Parse option string and assign to x264 parameters
978 *
979 */
980 static gboolean
981 gst_x264_enc_parse_options (GstX264Enc * encoder, const gchar * str)
982 {
983 GStrv kvpairs;
984 guint npairs, i;
985 gint parse_result = 0, ret = 0;
986 gchar *options = (gchar *) str;
988 while (*options == ':')
989 options++;
991 kvpairs = g_strsplit (options, ":", 0);
992 npairs = g_strv_length (kvpairs);
994 for (i = 0; i < npairs; i++) {
995 GStrv key_val = g_strsplit (kvpairs[i], "=", 2);
997 parse_result =
998 x264_param_parse (&encoder->x264param, key_val[0], key_val[1]);
1000 if (parse_result == X264_PARAM_BAD_NAME) {
1001 GST_ERROR_OBJECT (encoder, "Bad name for option %s=%s",
1002 key_val[0] ? key_val[0] : "", key_val[1] ? key_val[1] : "");
1003 }
1004 if (parse_result == X264_PARAM_BAD_VALUE) {
1005 GST_ERROR_OBJECT (encoder,
1006 "Bad value for option %s=%s (Note: a NULL value for a non-boolean triggers this)",
1007 key_val[0] ? key_val[0] : "", key_val[1] ? key_val[1] : "");
1008 }
1010 g_strfreev (key_val);
1012 if (parse_result)
1013 ret++;
1014 }
1016 g_strfreev (kvpairs);
1017 return !ret;
1018 }
1020 /*
1021 * gst_x264_enc_init_encoder
1022 * @encoder: Encoder which should be initialized.
1023 *
1024 * Initialize x264 encoder.
1025 *
1026 */
1027 static gboolean
1028 gst_x264_enc_init_encoder (GstX264Enc * encoder)
1029 {
1030 guint pass = 0;
1032 /* make sure that the encoder is closed */
1033 gst_x264_enc_close_encoder (encoder);
1035 GST_OBJECT_LOCK (encoder);
1037 #ifdef X264_PRESETS
1038 gst_x264_enc_build_tunings_string (encoder);
1040 /* set x264 parameters and use preset/tuning if present */
1041 GST_DEBUG_OBJECT (encoder, "Applying defaults with preset %s, tunings %s",
1042 encoder->speed_preset ? x264_preset_names[encoder->speed_preset - 1] : "",
1043 encoder->tunings && encoder->tunings->len ? encoder->tunings->str : "");
1044 x264_param_default_preset (&encoder->x264param,
1045 encoder->speed_preset ? x264_preset_names[encoder->speed_preset -
1046 1] : NULL, encoder->tunings
1047 && encoder->tunings->len ? encoder->tunings->str : NULL);
1049 /* log callback setup; part of parameters
1050 * this needs to be done again after every *param_default* () call */
1051 encoder->x264param.pf_log = gst_x264_enc_log_callback;
1052 encoder->x264param.p_log_private = encoder;
1053 encoder->x264param.i_log_level = X264_LOG_DEBUG;
1055 /* if no preset nor tuning, use property defaults */
1056 if (!encoder->speed_preset && !encoder->tunings->len) {
1057 #endif /* X264_PRESETS */
1058 GST_DEBUG_OBJECT (encoder, "Applying x264enc_defaults");
1059 if (x264enc_defaults->len
1060 && gst_x264_enc_parse_options (encoder,
1061 x264enc_defaults->str) == FALSE) {
1062 GST_DEBUG_OBJECT (encoder,
1063 "x264enc_defaults string contains errors. This is a bug.");
1064 goto unlock_and_return;
1065 }
1066 #ifdef X264_PRESETS
1067 } else {
1068 /* When using presets we need to respect the default output format */
1069 encoder->x264param.b_aud = encoder->au_nalu;
1070 encoder->x264param.b_annexb = encoder->byte_stream;
1071 }
1072 #endif /* X264_PRESETS */
1074 #if X264_BUILD >= 81
1075 /* setup appropriate timebase for gstreamer */
1076 encoder->x264param.i_timebase_num = 1;
1077 encoder->x264param.i_timebase_den = 1000000000;
1078 #endif
1080 /* apply option-string property */
1081 if (encoder->option_string_prop && encoder->option_string_prop->len) {
1082 GST_DEBUG_OBJECT (encoder, "Applying option-string: %s",
1083 encoder->option_string_prop->str);
1084 if (gst_x264_enc_parse_options (encoder,
1085 encoder->option_string_prop->str) == FALSE) {
1086 GST_DEBUG_OBJECT (encoder, "Your option-string contains errors.");
1087 goto unlock_and_return;
1088 }
1089 }
1090 /* apply user-set options */
1091 if (encoder->option_string && encoder->option_string->len) {
1092 GST_DEBUG_OBJECT (encoder, "Applying user-set options: %s",
1093 encoder->option_string->str);
1094 if (gst_x264_enc_parse_options (encoder,
1095 encoder->option_string->str) == FALSE) {
1096 GST_DEBUG_OBJECT (encoder, "Failed to parse internal option string. "
1097 "This could be due to use of an old libx264 version. Option string "
1098 "was: %s", encoder->option_string->str);
1099 }
1100 }
1102 /* set up encoder parameters */
1103 encoder->x264param.i_fps_num = encoder->fps_num;
1104 encoder->x264param.i_fps_den = encoder->fps_den;
1105 encoder->x264param.i_width = encoder->width;
1106 encoder->x264param.i_height = encoder->height;
1107 if (encoder->par_den > 0) {
1108 encoder->x264param.vui.i_sar_width = encoder->par_num;
1109 encoder->x264param.vui.i_sar_height = encoder->par_den;
1110 }
1111 /* FIXME 0.11 : 2s default keyframe interval seems excessive
1112 * (10s is x264 default) */
1113 encoder->x264param.i_keyint_max = encoder->keyint_max ? encoder->keyint_max :
1114 (2 * encoder->fps_num / encoder->fps_den);
1116 if ((((encoder->height == 576) && ((encoder->width == 720)
1117 || (encoder->width == 704) || (encoder->width == 352)))
1118 || ((encoder->height == 288) && (encoder->width == 352)))
1119 && (encoder->fps_den == 1) && (encoder->fps_num == 25)) {
1120 encoder->x264param.vui.i_vidformat = 1; /* PAL */
1121 } else if ((((encoder->height == 480) && ((encoder->width == 720)
1122 || (encoder->width == 704) || (encoder->width == 352)))
1123 || ((encoder->height == 240) && (encoder->width == 352)))
1124 && (encoder->fps_den == 1001) && ((encoder->fps_num == 30000)
1125 || (encoder->fps_num == 24000))) {
1126 encoder->x264param.vui.i_vidformat = 2; /* NTSC */
1127 } else
1128 encoder->x264param.vui.i_vidformat = 5; /* unspecified */
1130 encoder->x264param.analyse.b_psnr = 0;
1132 switch (encoder->pass) {
1133 case GST_X264_ENC_PASS_QUANT:
1134 encoder->x264param.rc.i_rc_method = X264_RC_CQP;
1135 encoder->x264param.rc.i_qp_constant = encoder->quantizer;
1136 break;
1137 case GST_X264_ENC_PASS_QUAL:
1138 encoder->x264param.rc.i_rc_method = X264_RC_CRF;
1139 encoder->x264param.rc.f_rf_constant = encoder->quantizer;
1140 encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1141 encoder->x264param.rc.i_vbv_buffer_size
1142 = encoder->x264param.rc.i_vbv_max_bitrate
1143 * encoder->vbv_buf_capacity / 1000;
1144 break;
1145 case GST_X264_ENC_PASS_CBR:
1146 case GST_X264_ENC_PASS_PASS1:
1147 case GST_X264_ENC_PASS_PASS2:
1148 case GST_X264_ENC_PASS_PASS3:
1149 default:
1150 encoder->x264param.rc.i_rc_method = X264_RC_ABR;
1151 encoder->x264param.rc.i_bitrate = encoder->bitrate;
1152 encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1153 encoder->x264param.rc.i_vbv_buffer_size
1154 = encoder->x264param.rc.i_vbv_max_bitrate
1155 * encoder->vbv_buf_capacity / 1000;
1156 pass = encoder->pass & 0xF;
1157 break;
1158 }
1160 switch (pass) {
1161 case 0:
1162 encoder->x264param.rc.b_stat_read = 0;
1163 encoder->x264param.rc.b_stat_write = 0;
1164 break;
1165 case 1:
1166 encoder->x264param.rc.b_stat_read = 0;
1167 encoder->x264param.rc.b_stat_write = 1;
1168 #ifdef X264_PRESETS
1169 x264_param_apply_fastfirstpass (&encoder->x264param);
1170 #else
1171 encoder->x264param.i_frame_reference = 1;
1172 encoder->x264param.analyse.b_transform_8x8 = 0;
1173 encoder->x264param.analyse.inter = 0;
1174 encoder->x264param.analyse.i_me_method = X264_ME_DIA;
1175 encoder->x264param.analyse.i_subpel_refine =
1176 MIN (2, encoder->x264param.analyse.i_subpel_refine);
1177 encoder->x264param.analyse.i_trellis = 0;
1178 encoder->x264param.analyse.b_fast_pskip = 1;
1179 #endif /* X264_PRESETS */
1180 break;
1181 case 2:
1182 encoder->x264param.rc.b_stat_read = 1;
1183 encoder->x264param.rc.b_stat_write = 0;
1184 break;
1185 case 3:
1186 encoder->x264param.rc.b_stat_read = 1;
1187 encoder->x264param.rc.b_stat_write = 1;
1188 break;
1189 }
1191 #if X264_BUILD >= 81 && X264_BUILD < 106
1192 /* When vfr is disabled, libx264 ignores buffer timestamps. This causes
1193 * issues with rate control in libx264 with our nanosecond timebase. This
1194 * has been fixed upstream in libx264 but this workaround is required for
1195 * pre-fix versions. */
1196 if (!encoder->x264param.b_vfr_input) {
1197 if (encoder->x264param.i_fps_num == 0) {
1198 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE,
1199 ("Constant framerate is required."),
1200 ("The framerate caps (%d/%d) indicate VFR but VFR is disabled in libx264. (Is the zerolatency tuning in use?)",
1201 encoder->x264param.i_fps_num, encoder->x264param.i_fps_den));
1202 return FALSE;
1203 }
1204 encoder->x264param.i_timebase_num = encoder->x264param.i_fps_den;
1205 encoder->x264param.i_timebase_den = encoder->x264param.i_fps_num;
1206 }
1207 #endif
1209 #ifdef X264_PRESETS
1210 if (encoder->profile
1211 && x264_param_apply_profile (&encoder->x264param,
1212 x264_profile_names[encoder->profile - 1])) {
1213 GST_WARNING_OBJECT (encoder, "Bad profile name: %s",
1214 x264_profile_names[encoder->profile - 1]);
1215 }
1216 #endif /* X264_PRESETS */
1218 encoder->reconfig = FALSE;
1220 GST_OBJECT_UNLOCK (encoder);
1222 encoder->x264enc = x264_encoder_open (&encoder->x264param);
1223 if (!encoder->x264enc) {
1224 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE,
1225 ("Can not initialize x264 encoder."), (NULL));
1226 return FALSE;
1227 }
1229 return TRUE;
1231 unlock_and_return:
1232 GST_OBJECT_UNLOCK (encoder);
1233 return FALSE;
1234 }
1236 /* gst_x264_enc_close_encoder
1237 * @encoder: Encoder which should close.
1238 *
1239 * Close x264 encoder.
1240 */
1241 static void
1242 gst_x264_enc_close_encoder (GstX264Enc * encoder)
1243 {
1244 if (encoder->x264enc != NULL) {
1245 x264_encoder_close (encoder->x264enc);
1246 encoder->x264enc = NULL;
1247 }
1248 }
1250 /*
1251 * Returns: Buffer with the stream headers.
1252 */
1253 static GstBuffer *
1254 gst_x264_enc_header_buf (GstX264Enc * encoder)
1255 {
1256 GstBuffer *buf;
1257 x264_nal_t *nal;
1258 int i_nal;
1259 int header_return;
1260 int i_size;
1261 int nal_size;
1262 #ifndef X264_ENC_NALS
1263 int i_data;
1264 #endif
1265 guint8 *buffer, *sps;
1266 gulong buffer_size;
1267 gint sei_ni = 2, sps_ni = 0, pps_ni = 1;
1269 if (G_UNLIKELY (encoder->x264enc == NULL))
1270 return NULL;
1272 /* Create avcC header. */
1274 header_return = x264_encoder_headers (encoder->x264enc, &nal, &i_nal);
1275 if (header_return < 0) {
1276 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 header failed."),
1277 ("x264_encoder_headers return code=%d", header_return));
1278 return NULL;
1279 }
1281 /* old x264 returns SEI, SPS and PPS, newer one has SEI last */
1282 if (i_nal == 3 && nal[sps_ni].i_type != 7) {
1283 sei_ni = 0;
1284 sps_ni = 1;
1285 pps_ni = 2;
1286 }
1288 /* x264 is expected to return an SEI (some identification info),
1289 * and SPS and PPS */
1290 if (i_nal != 3 || nal[sps_ni].i_type != 7 || nal[pps_ni].i_type != 8 ||
1291 nal[sps_ni].i_payload < 4 || nal[pps_ni].i_payload < 1) {
1292 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, (NULL),
1293 ("Unexpected x264 header."));
1294 return NULL;
1295 }
1297 GST_MEMDUMP ("SEI", nal[sei_ni].p_payload, nal[sei_ni].i_payload);
1298 GST_MEMDUMP ("SPS", nal[sps_ni].p_payload, nal[sps_ni].i_payload);
1299 GST_MEMDUMP ("PPS", nal[pps_ni].p_payload, nal[pps_ni].i_payload);
1301 /* nal payloads with emulation_prevention_three_byte, and some header data */
1302 buffer_size = (nal[sps_ni].i_payload + nal[pps_ni].i_payload) * 4 + 100;
1303 buffer = g_malloc (buffer_size);
1305 /* old style API: nal's are not encapsulated, and have no sync/size prefix,
1306 * new style API: nal's are encapsulated, and have 4-byte size prefix */
1307 #ifndef X264_ENC_NALS
1308 sps = nal[sps_ni].p_payload;
1309 #else
1310 sps = nal[sps_ni].p_payload + 4;
1311 /* skip NAL unit type */
1312 sps++;
1313 #endif
1315 buffer[0] = 1; /* AVC Decoder Configuration Record ver. 1 */
1316 buffer[1] = sps[0]; /* profile_idc */
1317 buffer[2] = sps[1]; /* profile_compability */
1318 buffer[3] = sps[2]; /* level_idc */
1319 buffer[4] = 0xfc | (4 - 1); /* nal_length_size_minus1 */
1321 i_size = 5;
1323 buffer[i_size++] = 0xe0 | 1; /* number of SPSs */
1325 #ifndef X264_ENC_NALS
1326 i_data = buffer_size - i_size - 2;
1327 nal_size = x264_nal_encode (buffer + i_size + 2, &i_data, 0, &nal[sps_ni]);
1328 #else
1329 nal_size = nal[sps_ni].i_payload - 4;
1330 memcpy (buffer + i_size + 2, nal[sps_ni].p_payload + 4, nal_size);
1331 #endif
1332 GST_WRITE_UINT16_BE (buffer + i_size, nal_size);
1333 i_size += nal_size + 2;
1335 buffer[i_size++] = 1; /* number of PPSs */
1337 #ifndef X264_ENC_NALS
1338 i_data = buffer_size - i_size - 2;
1339 nal_size = x264_nal_encode (buffer + i_size + 2, &i_data, 0, &nal[pps_ni]);
1340 #else
1341 nal_size = nal[pps_ni].i_payload - 4;
1342 memcpy (buffer + i_size + 2, nal[pps_ni].p_payload + 4, nal_size);
1343 #endif
1344 GST_WRITE_UINT16_BE (buffer + i_size, nal_size);
1345 i_size += nal_size + 2;
1347 buf = gst_buffer_new_and_alloc (i_size);
1348 memcpy (GST_BUFFER_DATA (buf), buffer, i_size);
1349 g_free (buffer);
1351 GST_MEMDUMP ("header", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1353 return buf;
1354 }
1356 /* gst_x264_enc_set_src_caps
1357 * Returns: TRUE on success.
1358 */
1359 static gboolean
1360 gst_x264_enc_set_src_caps (GstX264Enc * encoder, GstPad * pad, GstCaps * caps)
1361 {
1362 GstBuffer *buf;
1363 GstCaps *outcaps;
1364 GstStructure *structure;
1365 gboolean res;
1367 outcaps = gst_caps_new_simple ("video/x-h264",
1368 "width", G_TYPE_INT, encoder->width,
1369 "height", G_TYPE_INT, encoder->height,
1370 "framerate", GST_TYPE_FRACTION, encoder->fps_num, encoder->fps_den,
1371 "pixel-aspect-ratio", GST_TYPE_FRACTION, encoder->par_num,
1372 encoder->par_den, NULL);
1374 structure = gst_caps_get_structure (outcaps, 0);
1376 if (!encoder->byte_stream) {
1377 buf = gst_x264_enc_header_buf (encoder);
1378 if (buf != NULL) {
1379 gst_caps_set_simple (outcaps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
1380 gst_buffer_unref (buf);
1381 }
1382 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "avc", NULL);
1383 } else {
1384 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
1385 NULL);
1386 }
1387 gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
1389 res = gst_pad_set_caps (pad, outcaps);
1390 gst_caps_unref (outcaps);
1392 return res;
1393 }
1395 static gboolean
1396 gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
1397 {
1398 GstX264Enc *encoder = GST_X264_ENC (GST_OBJECT_PARENT (pad));
1399 GstVideoFormat format;
1400 gint width, height;
1401 gint fps_num, fps_den;
1402 gint par_num, par_den;
1403 gint i;
1405 /* get info from caps */
1406 if (!gst_video_format_parse_caps (caps, &format, &width, &height))
1407 return FALSE;
1408 if (!gst_video_parse_caps_framerate (caps, &fps_num, &fps_den))
1409 return FALSE;
1410 if (!gst_video_parse_caps_pixel_aspect_ratio (caps, &par_num, &par_den)) {
1411 par_num = 1;
1412 par_den = 1;
1413 }
1415 /* If the encoder is initialized, do not reinitialize it again if not
1416 * necessary */
1417 if (encoder->x264enc) {
1418 if (width == encoder->width && height == encoder->height
1419 && fps_num == encoder->fps_num && fps_den == encoder->fps_den
1420 && par_num == encoder->par_num && par_den == encoder->par_den)
1421 return TRUE;
1423 /* clear out pending frames */
1424 gst_x264_enc_flush_frames (encoder, TRUE);
1426 encoder->sps_id++;
1427 }
1429 /* store input description */
1430 encoder->format = format;
1431 encoder->width = width;
1432 encoder->height = height;
1433 encoder->fps_num = fps_num;
1434 encoder->fps_den = fps_den;
1435 encoder->par_num = par_num;
1436 encoder->par_den = par_den;
1438 /* prepare a cached image description */
1439 encoder->image_size = gst_video_format_get_size (encoder->format, width,
1440 height);
1441 for (i = 0; i < 3; ++i) {
1442 /* only offsets now, is shifted later. Offsets will be for Y, U, V so we
1443 * can just feed YV12 as I420 to the decoder later */
1444 encoder->offset[i] = gst_video_format_get_component_offset (encoder->format,
1445 i, width, height);
1446 encoder->stride[i] = gst_video_format_get_row_stride (encoder->format,
1447 i, width);
1448 }
1450 if (!gst_x264_enc_init_encoder (encoder))
1451 return FALSE;
1453 if (!gst_x264_enc_set_src_caps (encoder, encoder->srcpad, caps)) {
1454 gst_x264_enc_close_encoder (encoder);
1455 return FALSE;
1456 }
1458 return TRUE;
1459 }
1461 static GstCaps *
1462 gst_x264_enc_sink_get_caps (GstPad * pad)
1463 {
1464 GstX264Enc *encoder;
1465 GstPad *peer;
1466 GstCaps *caps;
1468 /* If we already have caps return them */
1469 if (GST_PAD_CAPS (pad))
1470 return gst_caps_ref (GST_PAD_CAPS (pad));
1472 encoder = GST_X264_ENC (gst_pad_get_parent (pad));
1473 if (!encoder)
1474 return gst_caps_new_empty ();
1476 peer = gst_pad_get_peer (encoder->srcpad);
1477 if (peer) {
1478 const GstCaps *templcaps;
1479 GstCaps *peercaps;
1480 guint i, n;
1482 peercaps = gst_pad_get_caps (peer);
1484 /* Translate peercaps to YUV */
1485 peercaps = gst_caps_make_writable (peercaps);
1486 n = gst_caps_get_size (peercaps);
1487 for (i = 0; i < n; i++) {
1488 GstStructure *s = gst_caps_get_structure (peercaps, i);
1490 gst_structure_set_name (s, "video/x-raw-yuv");
1491 gst_structure_remove_field (s, "stream-format");
1492 gst_structure_remove_field (s, "alignment");
1493 }
1495 templcaps = gst_pad_get_pad_template_caps (pad);
1497 caps = gst_caps_intersect (peercaps, templcaps);
1498 gst_caps_unref (peercaps);
1499 gst_object_unref (peer);
1500 peer = NULL;
1501 } else {
1502 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
1503 }
1505 gst_object_unref (encoder);
1507 return caps;
1508 }
1510 static gboolean
1511 gst_x264_enc_src_event (GstPad * pad, GstEvent * event)
1512 {
1513 gboolean ret = TRUE;
1514 GstX264Enc *encoder;
1515 gboolean forward = TRUE;
1517 encoder = GST_X264_ENC (gst_pad_get_parent (pad));
1519 switch (GST_EVENT_TYPE (event)) {
1520 case GST_EVENT_CUSTOM_UPSTREAM:{
1521 const GstStructure *s;
1522 s = gst_event_get_structure (event);
1523 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1524 /* Set I frame request */
1525 GST_OBJECT_LOCK (encoder);
1526 encoder->i_type = X264_TYPE_I;
1527 encoder->forcekeyunit_event = gst_event_copy (event);
1528 GST_EVENT_TYPE (encoder->forcekeyunit_event) =
1529 GST_EVENT_CUSTOM_DOWNSTREAM;
1530 GST_OBJECT_UNLOCK (encoder);
1531 forward = FALSE;
1532 gst_event_unref (event);
1533 }
1534 break;
1535 }
1536 default:
1537 break;
1538 }
1540 if (forward)
1541 ret = gst_pad_push_event (encoder->sinkpad, event);
1543 gst_object_unref (encoder);
1544 return ret;
1545 }
1547 static gboolean
1548 gst_x264_enc_sink_event (GstPad * pad, GstEvent * event)
1549 {
1550 gboolean ret;
1551 GstX264Enc *encoder;
1553 encoder = GST_X264_ENC (gst_pad_get_parent (pad));
1555 switch (GST_EVENT_TYPE (event)) {
1556 case GST_EVENT_EOS:
1557 gst_x264_enc_flush_frames (encoder, TRUE);
1558 break;
1559 case GST_EVENT_TAG:{
1560 GstTagList *tags = NULL;
1562 event =
1563 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
1565 gst_event_parse_tag (event, &tags);
1566 /* drop codec/video-codec and replace encoder/encoder-version */
1567 gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1568 gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1569 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "x264",
1570 GST_TAG_ENCODER_VERSION, X264_BUILD, NULL);
1571 /* push is done below */
1572 break;
1573 /* no flushing if flush received,
1574 * buffers in encoder are considered (in the) past */
1575 }
1576 case GST_EVENT_CUSTOM_DOWNSTREAM:{
1577 const GstStructure *s;
1578 s = gst_event_get_structure (event);
1579 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1580 GST_OBJECT_LOCK (encoder);
1581 encoder->i_type = X264_TYPE_I;
1582 GST_OBJECT_UNLOCK (encoder);
1583 }
1584 break;
1585 }
1586 default:
1587 break;
1588 }
1590 ret = gst_pad_push_event (encoder->srcpad, event);
1592 gst_object_unref (encoder);
1593 return ret;
1594 }
1596 /* chain function
1597 * this function does the actual processing
1598 */
1599 static GstFlowReturn
1600 gst_x264_enc_chain (GstPad * pad, GstBuffer * buf)
1601 {
1602 GstX264Enc *encoder = GST_X264_ENC (GST_OBJECT_PARENT (pad));
1603 GstFlowReturn ret;
1604 x264_picture_t pic_in;
1605 gint i_nal, i;
1606 if (G_UNLIKELY (encoder->x264enc == NULL))
1607 goto not_inited;
1609 /* create x264_picture_t from the buffer */
1610 /* mostly taken from mplayer (file ve_x264.c) */
1611 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < encoder->image_size))
1612 goto wrong_buffer_size;
1614 /* remember the timestamp and duration */
1615 g_queue_push_tail (encoder->delay, buf);
1617 /* set up input picture */
1618 memset (&pic_in, 0, sizeof (pic_in));
1620 pic_in.img.i_csp = X264_CSP_I420;
1621 pic_in.img.i_plane = 3;
1622 for (i = 0; i < 3; i++) {
1623 pic_in.img.plane[i] = GST_BUFFER_DATA (buf) + encoder->offset[i];
1624 pic_in.img.i_stride[i] = encoder->stride[i];
1625 }
1627 GST_OBJECT_LOCK (encoder);
1628 pic_in.i_type = encoder->i_type;
1630 /* Reset encoder forced picture type */
1631 encoder->i_type = X264_TYPE_AUTO;
1632 GST_OBJECT_UNLOCK (encoder);
1634 pic_in.i_pts = GST_BUFFER_TIMESTAMP (buf);
1636 ret = gst_x264_enc_encode_frame (encoder, &pic_in, &i_nal, TRUE);
1638 /* input buffer is released later on */
1639 return ret;
1641 /* ERRORS */
1642 not_inited:
1643 {
1644 GST_WARNING_OBJECT (encoder, "Got buffer before set_caps was called");
1645 gst_buffer_unref (buf);
1646 return GST_FLOW_NOT_NEGOTIATED;
1647 }
1648 wrong_buffer_size:
1649 {
1650 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE,
1651 ("Encode x264 frame failed."),
1652 ("Wrong buffer size %d (should be %d)",
1653 GST_BUFFER_SIZE (buf), encoder->image_size));
1654 gst_buffer_unref (buf);
1655 return GST_FLOW_ERROR;
1656 }
1657 }
1659 static GstFlowReturn
1660 gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
1661 int *i_nal, gboolean send)
1662 {
1663 GstBuffer *out_buf = NULL, *in_buf = NULL;
1664 x264_picture_t pic_out;
1665 x264_nal_t *nal;
1666 int i_size;
1667 #ifndef X264_ENC_NALS
1668 int nal_size;
1669 gint i;
1670 #endif
1671 int encoder_return;
1672 GstFlowReturn ret;
1673 GstClockTime duration;
1674 guint8 *data;
1675 GstEvent *forcekeyunit_event = NULL;
1677 if (G_UNLIKELY (encoder->x264enc == NULL))
1678 return GST_FLOW_NOT_NEGOTIATED;
1680 GST_OBJECT_LOCK (encoder);
1681 if (encoder->reconfig) {
1682 encoder->reconfig = FALSE;
1683 if (x264_encoder_reconfig (encoder->x264enc, &encoder->x264param) < 0)
1684 GST_WARNING_OBJECT (encoder, "Could not reconfigure");
1685 }
1686 GST_OBJECT_UNLOCK (encoder);
1688 encoder_return = x264_encoder_encode (encoder->x264enc,
1689 &nal, i_nal, pic_in, &pic_out);
1691 if (encoder_return < 0) {
1692 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 frame failed."),
1693 ("x264_encoder_encode return code=%d", encoder_return));
1694 return GST_FLOW_ERROR;
1695 }
1697 if (!*i_nal) {
1698 return GST_FLOW_OK;
1699 }
1700 #ifndef X264_ENC_NALS
1701 i_size = 0;
1702 for (i = 0; i < *i_nal; i++) {
1703 gint i_data = encoder->buffer_size - i_size - 4;
1705 if (i_data < nal[i].i_payload * 2) {
1706 encoder->buffer_size += 2 * nal[i].i_payload;
1707 encoder->buffer = g_realloc (encoder->buffer, encoder->buffer_size);
1708 i_data = encoder->buffer_size - i_size - 4;
1709 }
1711 nal_size =
1712 x264_nal_encode (encoder->buffer + i_size + 4, &i_data, 0, &nal[i]);
1713 if (encoder->byte_stream)
1714 GST_WRITE_UINT32_BE (encoder->buffer + i_size, 1);
1715 else
1716 GST_WRITE_UINT32_BE (encoder->buffer + i_size, nal_size);
1718 i_size += nal_size + 4;
1719 }
1720 data = encoder->buffer;
1721 #else
1722 i_size = encoder_return;
1723 data = nal[0].p_payload;
1724 #endif
1726 in_buf = g_queue_pop_head (encoder->delay);
1727 if (in_buf) {
1728 duration = GST_BUFFER_DURATION (in_buf);
1729 gst_buffer_unref (in_buf);
1730 } else {
1731 GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, (NULL),
1732 ("Timestamp queue empty."));
1733 return GST_FLOW_ERROR;
1734 }
1736 if (!send)
1737 return GST_FLOW_OK;
1739 ret = gst_pad_alloc_buffer (encoder->srcpad, GST_BUFFER_OFFSET_NONE,
1740 i_size, GST_PAD_CAPS (encoder->srcpad), &out_buf);
1741 if (ret != GST_FLOW_OK)
1742 return ret;
1744 memcpy (GST_BUFFER_DATA (out_buf), data, i_size);
1745 GST_BUFFER_SIZE (out_buf) = i_size;
1747 /* PTS */
1748 /* FIXME ??: maybe use DTS here, since:
1749 * - it is so practiced by other encoders,
1750 * - downstream (e.g. muxers) might not enjoy non-monotone timestamps,
1751 * whereas a decoder can also deal with DTS */
1752 GST_BUFFER_TIMESTAMP (out_buf) = pic_out.i_pts;
1753 GST_BUFFER_DURATION (out_buf) = duration;
1755 #ifdef X264_INTRA_REFRESH
1756 if (pic_out.b_keyframe) {
1757 #else
1758 if (pic_out.i_type == X264_TYPE_IDR) {
1759 #endif
1760 GST_BUFFER_FLAG_UNSET (out_buf, GST_BUFFER_FLAG_DELTA_UNIT);
1761 } else {
1762 GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DELTA_UNIT);
1763 }
1765 GST_OBJECT_LOCK (encoder);
1766 forcekeyunit_event = encoder->forcekeyunit_event;
1767 encoder->forcekeyunit_event = NULL;
1768 GST_OBJECT_UNLOCK (encoder);
1769 if (forcekeyunit_event) {
1770 gst_structure_set (forcekeyunit_event->structure,
1771 "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (out_buf), NULL);
1772 gst_pad_push_event (encoder->srcpad, forcekeyunit_event);
1773 }
1775 return gst_pad_push (encoder->srcpad, out_buf);
1776 }
1778 static void
1779 gst_x264_enc_flush_frames (GstX264Enc * encoder, gboolean send)
1780 {
1781 GstFlowReturn flow_ret;
1782 gint i_nal;
1784 /* first send the remaining frames */
1785 if (encoder->x264enc)
1786 do {
1787 flow_ret = gst_x264_enc_encode_frame (encoder, NULL, &i_nal, send);
1788 #ifdef X264_DELAYED_FRAMES_API
1789 } while (flow_ret == GST_FLOW_OK
1790 && x264_encoder_delayed_frames (encoder->x264enc) > 0);
1791 #else
1792 /* note that this doesn't flush all frames for > 1 delayed frame */
1793 } while (flow_ret == GST_FLOW_OK && i_nal > 0);
1794 #endif
1796 /* in any case, make sure the delay queue is emptied */
1797 while (!g_queue_is_empty (encoder->delay))
1798 gst_buffer_unref (g_queue_pop_head (encoder->delay));
1799 }
1801 static GstStateChangeReturn
1802 gst_x264_enc_change_state (GstElement * element, GstStateChange transition)
1803 {
1804 GstX264Enc *encoder = GST_X264_ENC (element);
1805 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1807 ret = parent_class->change_state (element, transition);
1808 if (ret == GST_STATE_CHANGE_FAILURE)
1809 goto out;
1811 switch (transition) {
1812 case GST_STATE_CHANGE_PAUSED_TO_READY:
1813 gst_x264_enc_flush_frames (encoder, FALSE);
1814 gst_x264_enc_close_encoder (encoder);
1815 gst_x264_enc_reset (encoder);
1816 break;
1817 default:
1818 break;
1819 }
1821 out:
1822 return ret;
1823 }
1827 static void
1828 gst_x264_enc_reconfig (GstX264Enc * encoder)
1829 {
1830 switch (encoder->pass) {
1831 case GST_X264_ENC_PASS_QUAL:
1832 encoder->x264param.rc.f_rf_constant = encoder->quantizer;
1833 encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1834 encoder->x264param.rc.i_vbv_buffer_size
1835 = encoder->x264param.rc.i_vbv_max_bitrate
1836 * encoder->vbv_buf_capacity / 1000;
1837 break;
1838 case GST_X264_ENC_PASS_CBR:
1839 case GST_X264_ENC_PASS_PASS1:
1840 case GST_X264_ENC_PASS_PASS2:
1841 case GST_X264_ENC_PASS_PASS3:
1842 default:
1843 encoder->x264param.rc.i_bitrate = encoder->bitrate;
1844 encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
1845 encoder->x264param.rc.i_vbv_buffer_size
1846 = encoder->x264param.rc.i_vbv_max_bitrate
1847 * encoder->vbv_buf_capacity / 1000;
1848 break;
1849 }
1851 encoder->reconfig = TRUE;
1852 }
1854 static void
1855 gst_x264_enc_set_property (GObject * object, guint prop_id,
1856 const GValue * value, GParamSpec * pspec)
1857 {
1858 GstX264Enc *encoder;
1859 GstState state;
1861 const gchar *partitions = NULL;
1863 encoder = GST_X264_ENC (object);
1865 GST_OBJECT_LOCK (encoder);
1866 /* state at least matters for sps, bytestream, pass,
1867 * and so by extension ... */
1869 state = GST_STATE (encoder);
1870 if ((state != GST_STATE_READY && state != GST_STATE_NULL) &&
1871 !(pspec->flags & GST_PARAM_MUTABLE_PLAYING))
1872 goto wrong_state;
1874 switch (prop_id) {
1875 case ARG_PASS:
1876 encoder->pass = g_value_get_enum (value);
1877 break;
1878 case ARG_QUANTIZER:
1879 encoder->quantizer = g_value_get_uint (value);
1880 gst_x264_enc_reconfig (encoder);
1881 break;
1882 case ARG_BITRATE:
1883 encoder->bitrate = g_value_get_uint (value);
1884 gst_x264_enc_reconfig (encoder);
1885 break;
1886 case ARG_VBV_BUF_CAPACITY:
1887 encoder->vbv_buf_capacity = g_value_get_uint (value);
1888 gst_x264_enc_reconfig (encoder);
1889 break;
1890 case ARG_SPEED_PRESET:
1891 encoder->speed_preset = g_value_get_enum (value);
1892 break;
1893 case ARG_PSY_TUNE:
1894 encoder->psy_tune = g_value_get_enum (value);
1895 break;
1896 case ARG_TUNE:
1897 encoder->tune = g_value_get_flags (value);
1898 break;
1899 case ARG_PROFILE:
1900 encoder->profile = g_value_get_enum (value);
1901 break;
1902 case ARG_OPTION_STRING:
1903 g_string_assign (encoder->option_string_prop, g_value_get_string (value));
1904 break;
1905 case ARG_THREADS:
1906 encoder->threads = g_value_get_uint (value);
1907 g_string_append_printf (encoder->option_string, ":threads=%d",
1908 encoder->threads);
1909 break;
1910 case ARG_SLICED_THREADS:
1911 encoder->sliced_threads = g_value_get_boolean (value);
1912 g_string_append_printf (encoder->option_string, ":sliced-threads=%d",
1913 encoder->sliced_threads);
1914 break;
1915 case ARG_SYNC_LOOKAHEAD:
1916 encoder->sync_lookahead = g_value_get_int (value);
1917 g_string_append_printf (encoder->option_string, ":sync-lookahead=%d",
1918 encoder->sync_lookahead);
1919 break;
1920 case ARG_STATS_FILE:
1921 case ARG_MULTIPASS_CACHE_FILE:
1922 if (encoder->mp_cache_file)
1923 g_free (encoder->mp_cache_file);
1924 encoder->mp_cache_file = g_value_dup_string (value);
1925 g_string_append_printf (encoder->option_string, ":stats=%s",
1926 encoder->mp_cache_file);
1927 break;
1928 case ARG_BYTE_STREAM:
1929 encoder->byte_stream = g_value_get_boolean (value);
1930 g_string_append_printf (encoder->option_string, ":annexb=%d",
1931 encoder->byte_stream);
1932 break;
1933 case ARG_INTRA_REFRESH:
1934 encoder->intra_refresh = g_value_get_boolean (value);
1935 g_string_append_printf (encoder->option_string, ":intra-refresh=%d",
1936 encoder->intra_refresh);
1937 break;
1938 case ARG_ME:
1939 encoder->me = g_value_get_enum (value);
1940 g_string_append_printf (encoder->option_string, ":me=%s",
1941 x264_motion_est_names[encoder->me]);
1942 break;
1943 case ARG_SUBME:
1944 encoder->subme = g_value_get_uint (value);
1945 g_string_append_printf (encoder->option_string, ":subme=%d",
1946 encoder->subme);
1947 break;
1948 case ARG_ANALYSE:
1949 encoder->analyse = g_value_get_flags (value);
1950 partitions = gst_x264_enc_build_partitions (encoder->analyse);
1951 if (partitions) {
1952 g_string_append_printf (encoder->option_string, ":partitions=%s",
1953 partitions);
1954 g_free ((gpointer) partitions);
1955 }
1956 break;
1957 case ARG_DCT8x8:
1958 encoder->dct8x8 = g_value_get_boolean (value);
1959 g_string_append_printf (encoder->option_string, ":8x8dct=%d",
1960 encoder->dct8x8);
1961 break;
1962 case ARG_REF:
1963 encoder->ref = g_value_get_uint (value);
1964 g_string_append_printf (encoder->option_string, ":ref=%d", encoder->ref);
1965 break;
1966 case ARG_BFRAMES:
1967 encoder->bframes = g_value_get_uint (value);
1968 g_string_append_printf (encoder->option_string, ":bframes=%d",
1969 encoder->bframes);
1970 break;
1971 case ARG_B_ADAPT:
1972 encoder->b_adapt = g_value_get_boolean (value);
1973 g_string_append_printf (encoder->option_string, ":b-adapt=%d",
1974 encoder->b_adapt);
1975 break;
1976 case ARG_B_PYRAMID:
1977 encoder->b_pyramid = g_value_get_boolean (value);
1978 #ifdef X264_B_PYRAMID
1979 g_string_append_printf (encoder->option_string, ":b-pyramid=%s",
1980 x264_b_pyramid_names[encoder->b_pyramid]);
1981 #else
1982 g_string_append_printf (encoder->option_string, ":b-pyramid=%d",
1983 encoder->b_pyramid);
1984 #endif /* X264_B_PYRAMID */
1985 break;
1986 case ARG_WEIGHTB:
1987 encoder->weightb = g_value_get_boolean (value);
1988 g_string_append_printf (encoder->option_string, ":weightb=%d",
1989 encoder->weightb);
1990 break;
1991 case ARG_SPS_ID:
1992 encoder->sps_id = g_value_get_uint (value);
1993 g_string_append_printf (encoder->option_string, ":sps-id=%d",
1994 encoder->sps_id);
1995 break;
1996 case ARG_AU_NALU:
1997 encoder->au_nalu = g_value_get_boolean (value);
1998 g_string_append_printf (encoder->option_string, ":aud=%d",
1999 encoder->au_nalu);
2000 break;
2001 case ARG_TRELLIS:
2002 encoder->trellis = g_value_get_boolean (value);
2003 g_string_append_printf (encoder->option_string, ":trellis=%d",
2004 encoder->trellis);
2005 break;
2006 case ARG_KEYINT_MAX:
2007 encoder->keyint_max = g_value_get_uint (value);
2008 g_string_append_printf (encoder->option_string, ":keyint=%d",
2009 encoder->keyint_max);
2010 break;
2011 case ARG_CABAC:
2012 encoder->cabac = g_value_get_boolean (value);
2013 g_string_append_printf (encoder->option_string, ":cabac=%d",
2014 encoder->cabac);
2015 break;
2016 case ARG_QP_MIN:
2017 encoder->qp_min = g_value_get_uint (value);
2018 g_string_append_printf (encoder->option_string, ":qpmin=%d",
2019 encoder->qp_min);
2020 break;
2021 case ARG_QP_MAX:
2022 encoder->qp_max = g_value_get_uint (value);
2023 g_string_append_printf (encoder->option_string, ":qpmax=%d",
2024 encoder->qp_max);
2025 break;
2026 case ARG_QP_STEP:
2027 encoder->qp_step = g_value_get_uint (value);
2028 g_string_append_printf (encoder->option_string, ":qpstep=%d",
2029 encoder->qp_step);
2030 break;
2031 case ARG_IP_FACTOR:
2032 encoder->ip_factor = g_value_get_float (value);
2033 g_string_append_printf (encoder->option_string, ":ip-factor=%f",
2034 encoder->ip_factor);
2035 break;
2036 case ARG_PB_FACTOR:
2037 encoder->pb_factor = g_value_get_float (value);
2038 g_string_append_printf (encoder->option_string, ":pb-factor=%f",
2039 encoder->pb_factor);
2040 break;
2041 case ARG_RC_MB_TREE:
2042 encoder->mb_tree = g_value_get_boolean (value);
2043 g_string_append_printf (encoder->option_string, ":mbtree=%d",
2044 encoder->mb_tree);
2045 break;
2046 case ARG_RC_LOOKAHEAD:
2047 encoder->rc_lookahead = g_value_get_int (value);
2048 g_string_append_printf (encoder->option_string, ":rc-lookahead=%d",
2049 encoder->rc_lookahead);
2050 break;
2051 case ARG_NR:
2052 encoder->noise_reduction = g_value_get_uint (value);
2053 g_string_append_printf (encoder->option_string, ":nr=%d",
2054 encoder->noise_reduction);
2055 break;
2056 case ARG_INTERLACED:
2057 encoder->interlaced = g_value_get_boolean (value);
2058 g_string_append_printf (encoder->option_string, ":interlaced=%d",
2059 encoder->interlaced);
2060 break;
2061 default:
2062 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2063 break;
2064 }
2065 GST_OBJECT_UNLOCK (encoder);
2066 return;
2068 /* ERROR */
2069 wrong_state:
2070 {
2071 GST_WARNING_OBJECT (encoder, "setting property in wrong state");
2072 GST_OBJECT_UNLOCK (encoder);
2073 }
2074 }
2076 static void
2077 gst_x264_enc_get_property (GObject * object, guint prop_id,
2078 GValue * value, GParamSpec * pspec)
2079 {
2080 GstX264Enc *encoder;
2082 encoder = GST_X264_ENC (object);
2084 GST_OBJECT_LOCK (encoder);
2085 switch (prop_id) {
2086 case ARG_THREADS:
2087 g_value_set_uint (value, encoder->threads);
2088 break;
2089 case ARG_SLICED_THREADS:
2090 g_value_set_boolean (value, encoder->sliced_threads);
2091 break;
2092 case ARG_SYNC_LOOKAHEAD:
2093 g_value_set_int (value, encoder->sync_lookahead);
2094 break;
2095 case ARG_PASS:
2096 g_value_set_enum (value, encoder->pass);
2097 break;
2098 case ARG_QUANTIZER:
2099 g_value_set_uint (value, encoder->quantizer);
2100 break;
2101 case ARG_STATS_FILE:
2102 case ARG_MULTIPASS_CACHE_FILE:
2103 g_value_set_string (value, encoder->mp_cache_file);
2104 break;
2105 case ARG_BYTE_STREAM:
2106 g_value_set_boolean (value, encoder->byte_stream);
2107 break;
2108 case ARG_BITRATE:
2109 g_value_set_uint (value, encoder->bitrate);
2110 break;
2111 case ARG_INTRA_REFRESH:
2112 g_value_set_boolean (value, encoder->intra_refresh);
2113 break;
2114 case ARG_VBV_BUF_CAPACITY:
2115 g_value_set_uint (value, encoder->vbv_buf_capacity);
2116 break;
2117 case ARG_ME:
2118 g_value_set_enum (value, encoder->me);
2119 break;
2120 case ARG_SUBME:
2121 g_value_set_uint (value, encoder->subme);
2122 break;
2123 case ARG_ANALYSE:
2124 g_value_set_flags (value, encoder->analyse);
2125 break;
2126 case ARG_DCT8x8:
2127 g_value_set_boolean (value, encoder->dct8x8);
2128 break;
2129 case ARG_REF:
2130 g_value_set_uint (value, encoder->ref);
2131 break;
2132 case ARG_BFRAMES:
2133 g_value_set_uint (value, encoder->bframes);
2134 break;
2135 case ARG_B_ADAPT:
2136 g_value_set_boolean (value, encoder->b_adapt);
2137 break;
2138 case ARG_B_PYRAMID:
2139 g_value_set_boolean (value, encoder->b_pyramid);
2140 break;
2141 case ARG_WEIGHTB:
2142 g_value_set_boolean (value, encoder->weightb);
2143 break;
2144 case ARG_SPS_ID:
2145 g_value_set_uint (value, encoder->sps_id);
2146 break;
2147 case ARG_AU_NALU:
2148 g_value_set_boolean (value, encoder->au_nalu);
2149 break;
2150 case ARG_TRELLIS:
2151 g_value_set_boolean (value, encoder->trellis);
2152 break;
2153 case ARG_KEYINT_MAX:
2154 g_value_set_uint (value, encoder->keyint_max);
2155 break;
2156 case ARG_QP_MIN:
2157 g_value_set_uint (value, encoder->qp_min);
2158 break;
2159 case ARG_QP_MAX:
2160 g_value_set_uint (value, encoder->qp_max);
2161 break;
2162 case ARG_QP_STEP:
2163 g_value_set_uint (value, encoder->qp_step);
2164 break;
2165 case ARG_CABAC:
2166 g_value_set_boolean (value, encoder->cabac);
2167 break;
2168 case ARG_IP_FACTOR:
2169 g_value_set_float (value, encoder->ip_factor);
2170 break;
2171 case ARG_PB_FACTOR:
2172 g_value_set_float (value, encoder->pb_factor);
2173 break;
2174 case ARG_RC_MB_TREE:
2175 g_value_set_boolean (value, encoder->mb_tree);
2176 break;
2177 case ARG_RC_LOOKAHEAD:
2178 g_value_set_int (value, encoder->rc_lookahead);
2179 break;
2180 case ARG_NR:
2181 g_value_set_uint (value, encoder->noise_reduction);
2182 break;
2183 case ARG_INTERLACED:
2184 g_value_set_boolean (value, encoder->interlaced);
2185 break;
2186 case ARG_SPEED_PRESET:
2187 g_value_set_enum (value, encoder->speed_preset);
2188 break;
2189 case ARG_PSY_TUNE:
2190 g_value_set_enum (value, encoder->psy_tune);
2191 break;
2192 case ARG_TUNE:
2193 g_value_set_flags (value, encoder->tune);
2194 break;
2195 case ARG_PROFILE:
2196 g_value_set_enum (value, encoder->profile);
2197 break;
2198 case ARG_OPTION_STRING:
2199 g_value_set_string (value, encoder->option_string_prop->str);
2200 break;
2201 default:
2202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2203 break;
2204 }
2205 GST_OBJECT_UNLOCK (encoder);
2206 }
2208 static gboolean
2209 plugin_init (GstPlugin * plugin)
2210 {
2211 GST_DEBUG_CATEGORY_INIT (x264_enc_debug, "x264enc", 0,
2212 "h264 encoding element");
2214 return gst_element_register (plugin, "x264enc",
2215 GST_RANK_PRIMARY, GST_TYPE_X264_ENC);
2216 }
2218 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2219 GST_VERSION_MINOR,
2220 "x264",
2221 "libx264-based H264 plugins",
2222 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)