Debian Codegraph @

ext/x264/gstx264enc.c

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