codec: subsdec: fix variable shadowing
[vlc.git] / modules / codec / qsv.c
bloba9b23330f7fc04b709d72df8d77294b49b99c1d2
1 /*****************************************************************************
2 * qsv.c: mpeg4-part10/mpeg2 video encoder using Intel Media SDK
3 *****************************************************************************
4 * Copyright (C) 2013 VideoLabs
5 * $Id$
7 * Authors: Julien 'Lta' BALLET <contact@lta.io>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_picture.h>
34 #include <vlc_codec.h>
35 #include <vlc_picture_pool.h>
37 #include <vlc_fifo_helper.h>
39 #include <mfx/mfxvideo.h>
40 #include "../demux/mpeg/timestamps.h"
42 #define SOUT_CFG_PREFIX "sout-qsv-"
44 #define QSV_HAVE_CO2 (MFX_VERSION_MAJOR > 1 || (MFX_VERSION_MAJOR == 1 && MFX_VERSION_MINOR >= 6))
46 /* Default wait on libavcodec */
47 #define QSV_SYNCPOINT_WAIT (1000)
48 /* Encoder input synchronization busy wait loop time */
49 #define QSV_BUSYWAIT_TIME VLC_HARD_MIN_SLEEP
50 /* The SDK doesn't have a default bitrate, so here's one. */
51 #define QSV_BITRATE_DEFAULT (842)
53 /* Makes x a multiple of 'align'. 'align' must be a power of 2 */
54 #define QSV_ALIGN(align, x) (((x)+(align)-1)&~((align)-1))
56 /*****************************************************************************
57 * Modules descriptor
58 *****************************************************************************/
59 static int Open(vlc_object_t *);
60 static void Close(vlc_object_t *);
62 #define SW_IMPL_TEXT N_("Enable software mode")
63 #define SW_IMPL_LONGTEXT N_("Allow the use of the Intel Media SDK software " \
64 "implementation of the codecs if no QuickSync Video hardware " \
65 "acceleration is present on the system.")
67 #define PROFILE_TEXT N_("Codec Profile")
68 #define PROFILE_LONGTEXT N_( \
69 "Specify the codec profile explicitly. If you don't, the codec will " \
70 "determine the correct profile from other sources, such as resolution " \
71 "and bitrate. E.g. 'high'")
73 #define LEVEL_TEXT N_("Codec Level")
74 #define LEVEL_LONGTEXT N_( \
75 "Specify the codec level explicitly. If you don't, the codec will " \
76 "determine the correct profile from other sources, such as resolution " \
77 "and bitrate. E.g. '4.2' for mpeg4-part10 or 'low' for mpeg2")
79 #define GOP_SIZE_TEXT N_("Group of Picture size")
80 #define GOP_SIZE_LONGTEXT N_( \
81 "Number of pictures within the current GOP (Group of Pictures); if " \
82 "GopPicSize=0, then the GOP size is unspecified. If GopPicSize=1, " \
83 "only I-frames are used.")
85 #define GOP_REF_DIST_TEXT N_("Group of Picture Reference Distance")
86 #define GOP_REF_DIST_LONGTEXT N_( \
87 "Distance between I- or P- key frames; if it is zero, the GOP " \
88 "structure is unspecified. Note: If GopRefDist = 1, there are no B-" \
89 "frames used.")
91 #define TARGET_USAGE_TEXT N_("Target Usage")
92 #define TARGET_USAGE_LONGTEXT N_("The target usage allow to choose between " \
93 "different trade-offs between quality and speed. Allowed values are: " \
94 "'speed', 'balanced' and 'quality'.")
96 #define IDR_INTERVAL_TEXT N_("IDR interval")
97 #define IDR_INTERVAL_LONGTEXT N_( \
98 "For H.264, IdrInterval specifies IDR-frame interval in terms of I-" \
99 "frames; if IdrInterval=0, then every I-frame is an IDR-frame. If " \
100 "IdrInterval=1, then every other I-frame is an IDR-frame, etc. " \
101 "For MPEG2, IdrInterval defines sequence header interval in terms " \
102 "of I-frames. If IdrInterval=N, SDK inserts the sequence header " \
103 "before every Nth I-frame. If IdrInterval=0 (default), SDK inserts " \
104 "the sequence header once at the beginning of the stream.")
106 #define RATE_CONTROL_TEXT N_("Rate Control Method")
107 #define RATE_CONTROL_LONGTEXT N_( \
108 "The rate control method to use when encoding. Can be one of " \
109 "'cbr', 'vbr', 'qp', 'avbr'. 'qp' mode isn't supported for mpeg2.")
111 #define QP_TEXT N_("Quantization parameter")
112 #define QP_LONGTEXT N_("Quantization parameter for all types of frames. " \
113 "This parameters sets qpi, qpp and qpb. It has less precedence than " \
114 "the forementionned parameters. Used only if rc_method is 'qp'.")
116 #define QPI_TEXT N_("Quantization parameter for I-frames")
117 #define QPI_LONGTEXT N_("Quantization parameter for I-frames. This parameter " \
118 "overrides any qp set globally. Used only if rc_method is 'qp'.")
120 #define QPP_TEXT N_("Quantization parameter for P-frames")
121 #define QPP_LONGTEXT N_("Quantization parameter for P-frames. This parameter " \
122 "overrides any qp set globally. Used only if rc_method is 'qp'.")
124 #define QPB_TEXT N_("Quantization parameter for B-frames")
125 #define QPB_LONGTEXT N_("Quantization parameter for B-frames. This parameter " \
126 "overrides any qp set globally. Used only if rc_method is 'qp'.")
128 #define MAX_BITRATE_TEXT N_("Maximum Bitrate")
129 #define MAX_BITRATE_LONGTEXT N_("Defines the maximum bitrate in kbps " \
130 "(1000 bits/s) for VBR rate control method. If not set, this parameter" \
131 " is computed from other sources such as bitrate, profile, level, etc.")
133 #define ACCURACY_TEXT N_("Accuracy of RateControl")
134 #define ACCURACY_LONGTEXT N_("Tolerance in percentage of the 'avbr' " \
135 " (Average Variable BitRate) method. (e.g. 10 with a bitrate of 800 " \
136 " kbps means the encoder tries not to go above 880 kbps and under " \
137 " 730 kbps. The targeted accuracy is only reached after a certained " \
138 " convergence period. See the convergence parameter")
140 #define CONVERGENCE_TEXT N_("Convergence time of 'avbr' RateControl")
141 #define CONVERGENCE_LONGTEXT N_("Number of 100 frames before the " \
142 "'avbr' rate control method reaches the requested bitrate with " \
143 "the requested accuracy. See the accuracy parameter.")
145 #define NUM_SLICE_TEXT N_("Number of slices per frame")
146 #define NUM_SLICE_LONGTEXT N_("Number of slices in each video frame; "\
147 "each slice contains one or more macro-block rows. If numslice is " \
148 "not set, the encoder may choose any slice partitioning allowed " \
149 "by the codec standard.")
151 #define NUM_REF_FRAME_TEXT N_("Number of reference frames")
152 #define NUM_REF_FRAME_LONGTEXT N_("Number of reference frames")
154 #define ASYNC_DEPTH_TEXT N_("Number of parallel operations")
155 #define ASYNC_DEPTH_LONGTEXT N_("Defines the number of parallel " \
156 "encoding operations before we synchronise the result. Higher " \
157 "numbers may result on better throughput depending on hardware. " \
158 "MPEG2 needs at least 1 here.")
160 static const int profile_h264_list[] =
161 { MFX_PROFILE_UNKNOWN, MFX_PROFILE_AVC_CONSTRAINED_BASELINE, MFX_PROFILE_AVC_MAIN,
162 MFX_PROFILE_AVC_EXTENDED, MFX_PROFILE_AVC_HIGH };
163 static const char *const profile_h264_text[] =
164 { "decide", "baseline", "main", "extended", "high" };
166 static const int profile_mpeg2_list[] =
167 { MFX_PROFILE_UNKNOWN, MFX_PROFILE_MPEG2_SIMPLE, MFX_PROFILE_MPEG2_MAIN,
168 MFX_PROFILE_MPEG2_HIGH };
169 static const char *const profile_mpeg2_text[] =
170 { "decide", "simple", "main", "high" };
172 static const int level_h264_list[] =
173 { MFX_LEVEL_UNKNOWN, MFX_LEVEL_AVC_1, MFX_LEVEL_AVC_1b, MFX_LEVEL_AVC_12,
174 MFX_LEVEL_AVC_13, MFX_LEVEL_AVC_2, MFX_LEVEL_AVC_21, MFX_LEVEL_AVC_22,
175 MFX_LEVEL_AVC_3, MFX_LEVEL_AVC_31, MFX_LEVEL_AVC_32, MFX_LEVEL_AVC_4,
176 MFX_LEVEL_AVC_41, MFX_LEVEL_AVC_42, MFX_LEVEL_AVC_5, MFX_LEVEL_AVC_51,
177 MFX_LEVEL_AVC_52};
178 static const char *const level_h264_text[] =
179 { "decide", "1", "1.1b", "1.2", "1.3", "2", "2.1", "2.2", "3", "3.1",
180 "3.2", "4", "4.1", "4.2", "5", "5.1", "5.2" };
182 static const int level_mpeg2_list[] =
183 { MFX_LEVEL_UNKNOWN, MFX_LEVEL_MPEG2_LOW, MFX_LEVEL_MPEG2_MAIN,
184 MFX_LEVEL_MPEG2_HIGH, MFX_LEVEL_MPEG2_HIGH1440 };
185 static const char *const level_mpeg2_text[] =
186 { "decide", "low", "main", "high", "high1440" };
188 static const int target_usage_list[] =
189 { MFX_TARGETUSAGE_UNKNOWN, MFX_TARGETUSAGE_BEST_QUALITY, MFX_TARGETUSAGE_BALANCED,
190 MFX_TARGETUSAGE_BEST_SPEED };
191 static const char *const target_usage_text[] =
192 { "decide", "quality", "balanced", "speed" };
194 static const int rc_method_list[] =
195 { MFX_RATECONTROL_CBR, MFX_RATECONTROL_VBR,
196 MFX_RATECONTROL_CQP, MFX_RATECONTROL_AVBR};
197 static const char *const rc_method_text[] =
198 { "cbr", "vbr", "qp", "avbr" };
200 vlc_module_begin ()
201 set_category(CAT_INPUT)
202 set_subcategory(SUBCAT_INPUT_VCODEC)
203 set_description(N_("Intel QuickSync Video encoder for MPEG4-Part10/MPEG2 (aka H.264/H.262)"))
204 set_shortname("qsv")
205 set_capability("encoder", 0)
206 set_callbacks(Open, Close)
208 add_bool(SOUT_CFG_PREFIX "software", false, SW_IMPL_TEXT, SW_IMPL_LONGTEXT, true)
210 add_string(SOUT_CFG_PREFIX "h264-profile", "unspecified" , PROFILE_TEXT, PROFILE_LONGTEXT, false)
211 change_string_list(profile_h264_text, profile_h264_text)
213 add_string(SOUT_CFG_PREFIX "h264-level", "unspecified", LEVEL_TEXT, LEVEL_LONGTEXT, false)
214 change_string_list(level_h264_text, level_h264_text)
216 add_string(SOUT_CFG_PREFIX "mpeg2-profile", "unspecified", PROFILE_TEXT, PROFILE_LONGTEXT, false)
217 change_string_list(profile_mpeg2_text, profile_mpeg2_text)
219 add_string(SOUT_CFG_PREFIX "mpeg2-level", "unspecified", LEVEL_TEXT, LEVEL_LONGTEXT, false)
220 change_string_list(level_mpeg2_text, level_mpeg2_text)
222 add_integer(SOUT_CFG_PREFIX "gop-size", 32, GOP_SIZE_TEXT, GOP_SIZE_LONGTEXT, true)
223 add_integer(SOUT_CFG_PREFIX "gop-refdist", 4, GOP_REF_DIST_TEXT, GOP_REF_DIST_LONGTEXT, true)
224 add_integer(SOUT_CFG_PREFIX "idr-interval", 0, IDR_INTERVAL_TEXT, IDR_INTERVAL_LONGTEXT, true)
226 add_string(SOUT_CFG_PREFIX "target-usage", "quality", TARGET_USAGE_TEXT, TARGET_USAGE_LONGTEXT, false)
227 change_string_list(target_usage_text, target_usage_text)
229 add_string(SOUT_CFG_PREFIX "rc-method", "vbr", RATE_CONTROL_TEXT, RATE_CONTROL_LONGTEXT, true)
230 change_string_list(rc_method_text, rc_method_text)
232 add_integer(SOUT_CFG_PREFIX "qp", 0, QP_TEXT, QP_LONGTEXT, true)
233 change_integer_range(0, 51)
234 add_integer(SOUT_CFG_PREFIX "qpi", 0, QPI_TEXT, QPI_LONGTEXT, true)
235 change_integer_range(0, 51)
236 add_integer(SOUT_CFG_PREFIX "qpp", 0, QPP_TEXT, QPP_LONGTEXT, true)
237 change_integer_range(0, 51)
238 add_integer(SOUT_CFG_PREFIX "qpb", 0, QPB_TEXT, QPB_LONGTEXT, true)
239 change_integer_range(0, 51)
241 add_integer(SOUT_CFG_PREFIX "bitrate-max", 0, MAX_BITRATE_TEXT, MAX_BITRATE_LONGTEXT, true)
243 add_integer(SOUT_CFG_PREFIX "accuracy", 0, ACCURACY_TEXT, ACCURACY_LONGTEXT, true)
244 change_integer_range(0, 100)
246 add_integer(SOUT_CFG_PREFIX "convergence", 0, CONVERGENCE_TEXT, CONVERGENCE_LONGTEXT, true)
248 add_integer(SOUT_CFG_PREFIX "num-slice", 0, NUM_SLICE_TEXT, NUM_SLICE_LONGTEXT, true)
249 add_integer(SOUT_CFG_PREFIX "num-ref-frame", 0, NUM_REF_FRAME_TEXT, NUM_REF_FRAME_LONGTEXT, true)
251 add_integer(SOUT_CFG_PREFIX "async-depth", 4, ASYNC_DEPTH_TEXT, ASYNC_DEPTH_LONGTEXT, true)
252 change_integer_range(1, 32)
254 vlc_module_end ()
256 /*****************************************************************************
257 * Local prototypes
258 *****************************************************************************/
259 static const char *const sout_options[] = {
260 "software", "h264-profile", "h264-level", "mpeg2-profile", "mpeg2-level",
261 "gop-size", "gop-refdist", "target-usage", "rc-method", "qp", "qpi", "qpp",
262 "qpb", "bitrate-max", "accuracy", "convergence", "num-slice",
263 "num-ref-frame", "async-depth",
264 NULL
267 // Frame pool for QuickSync video encoder with Intel Media SDK's format frames.
268 typedef struct _QSVFrame
270 struct _QSVFrame *next;
271 picture_t *pic;
272 mfxFrameSurface1 surface;
273 mfxEncodeCtrl enc_ctrl;
274 int used;
275 } QSVFrame;
277 typedef struct async_task_t
279 fifo_item_t fifo;
280 mfxBitstream bs; // Intel's bitstream structure.
281 mfxSyncPoint *syncp; // Async Task Sync Point.
282 block_t *block; // VLC's block structure to be returned by Encode.
283 } async_task_t;
285 TYPED_FIFO(async_task_t, async_task_t)
287 typedef struct
289 mfxSession session; // Intel Media SDK Session.
290 mfxVideoParam params; // Encoding parameters.
291 QSVFrame *work_frames; // IntelMediaSDK's frame pool.
292 uint64_t dts_warn_counter; // DTS warning counter for rate-limiting of msg;
293 uint64_t busy_warn_counter; // Device Busy warning counter for rate-limiting of msg;
294 uint64_t async_depth; // Number of parallel encoding operations.
295 fifo_t packets; // FIFO of queued packets
296 vlc_tick_t offset_pts; // The pts of the first frame, to avoid conversion overflow.
297 vlc_tick_t last_dts; // The dts of the last frame, to interpolate over buggy dts
299 picture_pool_t *input_pool; // pool of pictures to feed the decoder
300 // as it doesn't like constantly changing buffers
301 } encoder_sys_t;
303 static block_t *Encode(encoder_t *, picture_t *);
306 static void clear_unused_frames(encoder_sys_t *sys)
308 QSVFrame *cur = sys->work_frames;
309 while (cur) {
310 if (cur->used && !cur->surface.Data.Locked) {
311 picture_Release(cur->pic);
312 cur->used = 0;
314 cur = cur->next;
319 * Finds an unlocked frame, releases the associated picture if
320 * necessary associates the new picture with it and return the frame.
321 * Returns 0 if there's an error.
323 static int get_free_frame(encoder_sys_t *sys, QSVFrame **out)
325 QSVFrame *frame, **last;
327 clear_unused_frames(sys);
329 frame = sys->work_frames;
330 last = &sys->work_frames;
331 while (frame) {
332 if (!frame->used) {
333 *out = frame;
334 frame->used = 1;
335 return VLC_SUCCESS;
338 last = &frame->next;
339 frame = frame->next;
342 frame = calloc(1,sizeof(QSVFrame));
343 if (unlikely(frame==NULL))
344 return VLC_ENOMEM;
345 *last = frame;
347 *out = frame;
348 frame->used = 1;
350 return VLC_SUCCESS;
353 static uint64_t qsv_params_get_value(const char *const *text,
354 const int *list,
355 size_t size, char *sel)
357 size_t result = 0;
359 if (unlikely(!sel))
360 return list[0];
362 size /= sizeof(list[0]);
363 for (size_t i = 0; i < size; i++)
364 if (!strcmp(sel, text[i])) {
365 result = i;
366 break;
369 // sel comes from var_InheritString and must be freed.
370 free(sel);
371 // Returns the found item, or the default/first one if not found.
372 return list[result];
375 static int Open(vlc_object_t *this)
377 encoder_t *enc = (encoder_t *)this;
378 encoder_sys_t *sys = NULL;
380 mfxStatus sts = MFX_ERR_NONE;
381 mfxFrameAllocRequest alloc_request = { 0 };
382 uint8_t sps_buf[128];
383 uint8_t pps_buf[128];
384 mfxExtCodingOptionSPSPPS headers;
385 mfxExtCodingOption co = {
386 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION,
387 .Header.BufferSz = sizeof(co),
388 .PicTimingSEI = MFX_CODINGOPTION_ON,
390 #if QSV_HAVE_CO2
391 mfxExtCodingOption2 co2 = {
392 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION2,
393 .Header.BufferSz = sizeof(co2),
395 #endif
396 mfxExtBuffer *init_params[] =
398 (mfxExtBuffer*)&co,
399 #if QSV_HAVE_CO2
400 (mfxExtBuffer*)&co2,
401 #endif
403 mfxExtBuffer *extended_params[] = {
404 (mfxExtBuffer*)&headers,
405 (mfxExtBuffer*)&co,
406 #if QSV_HAVE_CO2
407 (mfxExtBuffer*)&co2,
408 #endif
410 mfxVersion ver = { { 1, 1 } };
411 mfxIMPL impl;
412 mfxVideoParam param_out = { 0 };
414 uint8_t *p_extra;
415 size_t i_extra;
417 if (enc->fmt_out.i_codec != VLC_CODEC_H264 &&
418 enc->fmt_out.i_codec != VLC_CODEC_MPGV && !enc->obj.force)
419 return VLC_EGENERIC;
421 if (!enc->fmt_in.video.i_visible_height || !enc->fmt_in.video.i_visible_width ||
422 !enc->fmt_in.video.i_frame_rate || !enc->fmt_in.video.i_frame_rate_base) {
423 msg_Err(enc, "Framerate and picture dimensions must be non-zero");
424 return VLC_EGENERIC;
427 /* Allocate the memory needed to store the decoder's structure */
428 sys = calloc(1, sizeof(encoder_sys_t));
429 if (unlikely(!sys))
430 return VLC_ENOMEM;
432 /* Initialize dispatcher, it will loads the actual SW/HW Implementation */
433 sts = MFXInit(MFX_IMPL_AUTO_ANY, &ver, &sys->session);
435 if (sts != MFX_ERR_NONE) {
436 if (sts == MFX_ERR_UNSUPPORTED)
437 msg_Err(enc, "Intel Media SDK implementation not supported, is your card plugged?");
438 else
439 msg_Err(enc, "Unable to find an Intel Media SDK implementation (%d).", sts);
440 free(sys);
441 return VLC_EGENERIC;
444 enc->p_sys = sys;
446 config_ChainParse(enc, SOUT_CFG_PREFIX, sout_options, enc->p_cfg);
448 /* Checking if we are on software and are allowing it */
449 MFXQueryIMPL(sys->session, &impl);
450 if (!var_InheritBool(enc, SOUT_CFG_PREFIX "software") && (impl & MFX_IMPL_SOFTWARE)) {
451 msg_Err(enc, "No hardware implementation found and software mode disabled");
452 goto error;
455 msg_Dbg(enc, "Using Intel QuickSync Video %s implementation",
456 impl & MFX_IMPL_HARDWARE ? "hardware" : "software");
458 /* Input picture format description */
459 sys->params.mfx.FrameInfo.FrameRateExtN = enc->fmt_in.video.i_frame_rate;
460 sys->params.mfx.FrameInfo.FrameRateExtD = enc->fmt_in.video.i_frame_rate_base;
461 sys->params.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
462 sys->params.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
463 sys->params.mfx.FrameInfo.Width = QSV_ALIGN(16, enc->fmt_in.video.i_width);
464 sys->params.mfx.FrameInfo.Height = QSV_ALIGN(32, enc->fmt_in.video.i_height);
465 sys->params.mfx.FrameInfo.CropW = enc->fmt_in.video.i_visible_width;
466 sys->params.mfx.FrameInfo.CropH = enc->fmt_in.video.i_visible_height;
467 sys->params.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
468 sys->params.mfx.FrameInfo.AspectRatioH = enc->fmt_in.video.i_sar_num;
469 sys->params.mfx.FrameInfo.AspectRatioW = enc->fmt_in.video.i_sar_den;
470 sys->params.mfx.FrameInfo.BitDepthChroma = 8; /* for VLC_CODEC_NV12 */
471 sys->params.mfx.FrameInfo.BitDepthLuma = 8; /* for VLC_CODEC_NV12 */
473 /* Parsing options common to all RC methods and codecs */
474 sys->params.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
475 sys->params.AsyncDepth = var_InheritInteger(enc, SOUT_CFG_PREFIX "async-depth");
476 sys->params.mfx.GopOptFlag = 1; /* TODO */
477 sys->params.mfx.GopPicSize = var_InheritInteger(enc, SOUT_CFG_PREFIX "gop-size");
478 sys->params.mfx.GopRefDist = var_InheritInteger(enc, SOUT_CFG_PREFIX "gop-refdist");
479 sys->params.mfx.IdrInterval = var_InheritInteger(enc, SOUT_CFG_PREFIX "idr-interval");
480 sys->params.mfx.NumSlice = var_InheritInteger(enc, SOUT_CFG_PREFIX "num-slice");
481 sys->params.mfx.NumRefFrame = var_InheritInteger(enc, SOUT_CFG_PREFIX "num-ref-frame");
482 sys->params.mfx.TargetUsage = qsv_params_get_value(target_usage_text,
483 target_usage_list, sizeof(target_usage_list),
484 var_InheritString(enc, SOUT_CFG_PREFIX "target-usage"));
486 if (enc->fmt_out.i_codec == VLC_CODEC_H264) {
487 sys->params.mfx.CodecId = MFX_CODEC_AVC;
488 sys->params.mfx.CodecProfile = qsv_params_get_value(profile_h264_text,
489 profile_h264_list, sizeof(profile_h264_list),
490 var_InheritString(enc, SOUT_CFG_PREFIX "h264-profile"));
491 sys->params.mfx.CodecLevel = qsv_params_get_value(level_h264_text,
492 level_h264_list, sizeof(level_h264_list),
493 var_InheritString(enc, SOUT_CFG_PREFIX "h264-level"));
494 msg_Dbg(enc, "Encoder in H264 mode, with profile %d and level %d",
495 sys->params.mfx.CodecProfile, sys->params.mfx.CodecLevel);
497 } else {
498 sys->params.mfx.CodecId = MFX_CODEC_MPEG2;
499 sys->params.mfx.CodecProfile = qsv_params_get_value(profile_mpeg2_text,
500 profile_mpeg2_list, sizeof(profile_mpeg2_list),
501 var_InheritString(enc, SOUT_CFG_PREFIX "mpeg2-profile"));
502 sys->params.mfx.CodecLevel = qsv_params_get_value(level_mpeg2_text,
503 level_mpeg2_list, sizeof(level_mpeg2_list),
504 var_InheritString(enc, SOUT_CFG_PREFIX "mpeg2-level"));
505 msg_Dbg(enc, "Encoder in MPEG2 mode, with profile %d and level %d",
506 sys->params.mfx.CodecProfile, sys->params.mfx.CodecLevel);
508 param_out.mfx.CodecId = sys->params.mfx.CodecId;
510 char *psz_rc = var_InheritString(enc, SOUT_CFG_PREFIX "rc-method");
511 msg_Dbg(enc, "Encoder using '%s' Rate Control method", psz_rc );
512 sys->params.mfx.RateControlMethod = qsv_params_get_value(rc_method_text,
513 rc_method_list, sizeof(rc_method_list), psz_rc );
515 if (sys->params.mfx.RateControlMethod == MFX_RATECONTROL_CQP) {
516 sys->params.mfx.QPI = sys->params.mfx.QPB = sys->params.mfx.QPP =
517 var_InheritInteger(enc, SOUT_CFG_PREFIX "qp");
518 sys->params.mfx.QPI = var_InheritInteger(enc, SOUT_CFG_PREFIX "qpi");
519 sys->params.mfx.QPB = var_InheritInteger(enc, SOUT_CFG_PREFIX "qpb");
520 sys->params.mfx.QPP = var_InheritInteger(enc, SOUT_CFG_PREFIX "qpp");
521 } else {
522 if (!enc->fmt_out.i_bitrate) {
523 msg_Warn(enc, "No bitrate specified, using default %d",
524 QSV_BITRATE_DEFAULT);
525 sys->params.mfx.TargetKbps = QSV_BITRATE_DEFAULT;
526 } else
527 sys->params.mfx.TargetKbps = enc->fmt_out.i_bitrate / 1000;
529 if (sys->params.mfx.RateControlMethod == MFX_RATECONTROL_AVBR) {
530 sys->params.mfx.Accuracy = var_InheritInteger(enc, SOUT_CFG_PREFIX "accuracy");
531 sys->params.mfx.Convergence = var_InheritInteger(enc, SOUT_CFG_PREFIX "convergence");
532 } else if (sys->params.mfx.RateControlMethod == MFX_RATECONTROL_VBR)
533 sys->params.mfx.MaxKbps = var_InheritInteger(enc, SOUT_CFG_PREFIX "bitrate-max");
536 sts = MFXVideoENCODE_Query(sys->session, &sys->params, &param_out);
537 if ( sts < MFX_ERR_NONE )
539 msg_Err(enc, "Unsupported encoding parameters (%d)", sts);
540 goto error;
543 if ( sys->params.mfx.RateControlMethod != param_out.mfx.RateControlMethod )
545 msg_Err(enc, "Unsupported control method %d got %d", sys->params.mfx.RateControlMethod, param_out.mfx.RateControlMethod);
546 goto error;
549 if (MFXVideoENCODE_Query(sys->session, &sys->params, &sys->params) < 0)
551 msg_Err(enc, "Error querying encoder params");
552 goto error;
555 /* Request number of surface needed and creating frame pool */
556 if (MFXVideoENCODE_QueryIOSurf(sys->session, &sys->params, &alloc_request)!= MFX_ERR_NONE)
558 msg_Err(enc, "Failed to query for allocation");
559 goto error;
562 enc->fmt_in.video.i_chroma = VLC_CODEC_NV12;
563 video_format_t pool_fmt = enc->fmt_in.video;
564 pool_fmt.i_width = sys->params.mfx.FrameInfo.Width;
565 pool_fmt.i_height = sys->params.mfx.FrameInfo.Height;
566 sys->input_pool = picture_pool_NewFromFormat( &pool_fmt, 18 );
567 if (sys->input_pool == NULL)
569 msg_Err(enc, "Failed to create the internal pool");
570 goto error;
573 sys->params.ExtParam = (mfxExtBuffer**)&init_params;
574 sys->params.NumExtParam =
575 #if QSV_HAVE_CO2
577 #else
579 #endif
581 /* Initializing MFX_Encoder */
582 sts = MFXVideoENCODE_Init(sys->session, &sys->params);
583 if (sts == MFX_ERR_NONE)
584 msg_Dbg(enc, "Successfuly initialized video encoder");
585 else if (sts < MFX_ERR_NONE) {
586 msg_Err(enc, "Unable to initialize video encoder error (%d). " \
587 " Most likely because of provided encoding parameters", sts);
588 goto error;
589 } else
590 msg_Warn(enc, "Video encoder initialization : %d. The stream might be corrupted/invalid", sts);
592 /* Querying PPS/SPS Headers, BufferSizeInKB, ... */
593 memset(&headers, 0, sizeof(headers));
594 headers.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
595 headers.Header.BufferSz = sizeof(headers);
596 headers.PPSBufSize = sizeof(pps_buf);
597 headers.SPSBufSize = sizeof(sps_buf);
598 headers.SPSBuffer = sps_buf;
599 headers.PPSBuffer = pps_buf;
600 sys->params.ExtParam = (mfxExtBuffer **)&extended_params;
601 sys->params.NumExtParam =
602 #if QSV_HAVE_CO2
604 #else
606 #endif
608 MFXVideoENCODE_GetVideoParam(sys->session, &sys->params);
610 i_extra = headers.SPSBufSize + headers.PPSBufSize;
611 p_extra = malloc(i_extra);
613 if (unlikely(!p_extra))
614 goto nomem;
616 memcpy(p_extra, headers.SPSBuffer, headers.SPSBufSize);
617 memcpy(p_extra + headers.SPSBufSize, headers.PPSBuffer, headers.PPSBufSize);
618 enc->fmt_out.p_extra = p_extra;
619 enc->fmt_out.i_extra = i_extra;
621 sys->async_depth = sys->params.AsyncDepth;
622 async_task_t_fifo_Init(&sys->packets);
624 /* Vlc module configuration */
625 enc->fmt_in.i_codec = VLC_CODEC_NV12; // Intel Media SDK requirement
626 enc->fmt_in.video.i_bits_per_pixel = 12;
627 enc->fmt_in.video.i_width = sys->params.mfx.FrameInfo.Width;
628 enc->fmt_in.video.i_height = sys->params.mfx.FrameInfo.Height;
630 enc->pf_encode_video = Encode;
632 return VLC_SUCCESS;
634 error:
635 Close(this);
636 enc->p_sys = NULL;
637 return VLC_EGENERIC;
638 nomem:
639 Close(this);
640 enc->p_sys = NULL;
641 return VLC_ENOMEM;
644 static void Close(vlc_object_t *this)
646 encoder_t *enc = (encoder_t *)this;
647 encoder_sys_t *sys = enc->p_sys;
649 MFXVideoENCODE_Close(sys->session);
650 MFXClose(sys->session);
651 /* if (enc->fmt_out.p_extra) */
652 /* free(enc->fmt_out.p_extra); */
653 async_task_t_fifo_Release(&sys->packets);
654 if (sys->input_pool)
655 picture_pool_Release(sys->input_pool);
656 free(sys);
660 * The behavior in the next function comes from x264.c
662 static void qsv_set_block_flags(block_t *block, uint16_t frame_type)
664 if (frame_type & MFX_FRAMETYPE_IDR)
665 block->i_flags = BLOCK_FLAG_TYPE_I;
666 else if ((frame_type & MFX_FRAMETYPE_P) || (frame_type & MFX_FRAMETYPE_I))
667 block->i_flags = BLOCK_FLAG_TYPE_P;
668 else if (frame_type & MFX_FRAMETYPE_B)
669 block->i_flags = BLOCK_FLAG_TYPE_B;
670 else
671 block->i_flags = BLOCK_FLAG_TYPE_PB;
675 * Convert the Intel Media SDK's timestamps into VLC's format.
677 static void qsv_set_block_ts(encoder_t *enc, encoder_sys_t *sys, block_t *block, mfxBitstream *bs)
679 block->i_pts = FROM_SCALE_NZ(bs->TimeStamp) + sys->offset_pts;
680 block->i_dts = FROM_SCALE_NZ(bs->DecodeTimeStamp) + sys->offset_pts;
682 /* HW encoder (with old driver versions) and some parameters
683 combinations doesn't set the DecodeTimeStamp field so we warn
684 the user about it */
685 if (!bs->DecodeTimeStamp || bs->DecodeTimeStamp > (int64_t)bs->TimeStamp)
686 if (sys->dts_warn_counter++ % 16 == 0) // Rate limiting this message.
687 msg_Warn(enc, "Encode returning empty DTS or DTS > PTS. Your stream will be invalid. "
688 " Please double-check they weren't any warning at encoder initialization "
689 " and that you have the last version of Intel's drivers installed.");
692 static block_t *qsv_synchronize_block(encoder_t *enc, async_task_t *task)
694 encoder_sys_t *sys = enc->p_sys;
695 mfxStatus sts;
697 /* Synchronize and fill block_t. If the SyncOperation fails we leak :-/ (or we can segfault, ur choice) */
698 do {
699 sts = MFXVideoCORE_SyncOperation(sys->session, *task->syncp, QSV_SYNCPOINT_WAIT);
700 } while (sts == MFX_WRN_IN_EXECUTION);
701 if (sts != MFX_ERR_NONE) {
702 msg_Err(enc, "SyncOperation failed (%d), outputting garbage data. "
703 "Updating your drivers and/or changing the encoding settings might resolve this", sts);
704 return NULL;
706 if (task->bs.DataLength == 0)
708 msg_Dbg(enc, "Empty encoded block");
709 return NULL;
711 block_t *block = task->block;
712 block->i_buffer = task->bs.DataLength;
713 block->p_buffer += task->bs.DataOffset;
715 qsv_set_block_ts(enc, sys, block, &task->bs);
716 qsv_set_block_flags(block, task->bs.FrameType);
718 /* msg_Dbg(enc, "block->i_pts = %lld, block->i_dts = %lld", block->i_pts, block->i_dts); */
719 /* msg_Dbg(enc, "FrameType = %#.4x, TimeStamp = %lld (pts %lld), DecodeTimeStamp = %lld syncp=0x%x",*/
720 /* task->bs.FrameType, task->bs.TimeStamp, block->i_pts, task->bs.DecodeTimeStamp, *task->syncp); */
722 /* Copied from x264.c: This isn't really valid for streams with B-frames */
723 block->i_length = vlc_tick_from_samples( enc->fmt_in.video.i_frame_rate_base,
724 enc->fmt_in.video.i_frame_rate );
726 // Buggy DTS (value comes from experiments)
727 if (task->bs.DecodeTimeStamp < -10000)
728 block->i_dts = sys->last_dts + block->i_length;
729 sys->last_dts = block->i_dts;
731 task->bs.DataLength = task->bs.DataOffset = 0;
732 return block;
735 static int submit_frame(encoder_t *enc, picture_t *pic, QSVFrame **new_frame)
737 encoder_sys_t *sys = enc->p_sys;
738 QSVFrame *qf = NULL;
739 int ret = get_free_frame(sys, &qf);
740 if (ret != VLC_SUCCESS) {
741 msg_Warn(enc, "Unable to find an unlocked surface in the pool");
742 return ret;
745 qf->pic = picture_pool_Get( sys->input_pool );
746 if (unlikely(!qf->pic))
748 msg_Warn(enc, "Unable to find an unlocked surface in the pool");
749 qf->used = 0;
750 return ret;
752 picture_Copy( qf->pic, pic );
754 assert(qf->pic->p[0].p_pixels + (qf->pic->p[0].i_pitch * qf->pic->p[0].i_lines) == qf->pic->p[1].p_pixels);
756 qf->surface.Info = sys->params.mfx.FrameInfo;
758 // Specify picture structure at runtime.
759 if (pic->b_progressive)
760 qf->surface.Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
761 else if (pic->b_top_field_first)
762 qf->surface.Info.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
763 else
764 qf->surface.Info.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
766 //qf->surface.Data.Pitch = QSV_ALIGN(16, qf->surface.Info.Width);
768 qf->surface.Data.PitchLow = qf->pic->p[0].i_pitch;
769 qf->surface.Data.Y = qf->pic->p[0].p_pixels;
770 qf->surface.Data.UV = qf->pic->p[1].p_pixels;
772 qf->surface.Data.TimeStamp = TO_SCALE_NZ(pic->date - sys->offset_pts);
774 *new_frame = qf;
776 return VLC_SUCCESS;
779 static async_task_t *encode_frame(encoder_t *enc, picture_t *pic)
781 encoder_sys_t *sys = enc->p_sys;
782 mfxStatus sts = MFX_ERR_MEMORY_ALLOC;
783 QSVFrame *qsv_frame = NULL;
784 mfxFrameSurface1 *surf = NULL;
785 async_task_t *task = calloc(1, sizeof(*task));
786 if (unlikely(task == NULL))
787 goto done;
789 if (pic) {
790 /* To avoid qsv -> vlc timestamp conversion overflow, we use timestamp relative
791 to the first picture received. That way, vlc will overflow before us.
792 (Thanks to funman for the idea) */
793 if (!sys->offset_pts) // First frame
794 sys->offset_pts = pic->date;
796 if (submit_frame(enc, pic, &qsv_frame) != VLC_SUCCESS)
798 msg_Warn(enc, "Unable to find an unlocked surface in the pool");
799 goto done;
803 if (!(task->syncp = calloc(1, sizeof(*task->syncp)))) {
804 msg_Err(enc, "Unable to allocate syncpoint for encoder output");
805 goto done;
808 /* Allocate block_t and prepare mfxBitstream for encoder */
809 if (!(task->block = block_Alloc(sys->params.mfx.BufferSizeInKB * 1000))) {
810 msg_Err(enc, "Unable to allocate block for encoder output");
811 goto done;
813 memset(&task->bs, 0, sizeof(task->bs));
814 task->bs.MaxLength = task->block->i_buffer;
815 task->bs.Data = task->block->p_buffer;
817 if (qsv_frame) {
818 surf = &qsv_frame->surface;
821 for (;;) {
822 sts = MFXVideoENCODE_EncodeFrameAsync(sys->session, 0, surf, &task->bs, task->syncp);
823 if (sts != MFX_WRN_DEVICE_BUSY && sts != MFX_WRN_IN_EXECUTION)
824 break;
825 if (sys->busy_warn_counter++ % 16 == 0)
826 msg_Dbg(enc, "Device is busy, let's wait and retry %d", sts);
827 if (sts == MFX_WRN_DEVICE_BUSY)
828 vlc_tick_sleep(QSV_BUSYWAIT_TIME);
831 // msg_Dbg(enc, "Encode async status: %d, Syncpoint = %tx", sts, (ptrdiff_t)task->syncp);
833 if (sts == MFX_ERR_MORE_DATA)
834 if (pic)
835 msg_Dbg(enc, "Encoder feeding phase, more data is needed.");
836 else
837 msg_Dbg(enc, "Encoder is empty");
838 else if (sts < MFX_ERR_NONE) {
839 msg_Err(enc, "Encoder not ready or error (%d), trying a reset...", sts);
840 MFXVideoENCODE_Reset(sys->session, &sys->params);
843 done:
844 if (sts < MFX_ERR_NONE || (task != NULL && !task->syncp)) {
845 if (task->block != NULL)
846 block_Release(task->block);
847 free(task);
848 task = NULL;
850 return task;
854 * The Encode function has 3 encoding phases :
855 * - Feed : We feed the encoder until it stops to return MFX_MORE_DATA_NEEDED
856 * and the async_tasks are all in use (honouring the AsyncDepth)
857 * - Main encoding phase : synchronizing the oldest task each call.
858 * - Empty : pic = 0, we empty the decoder. Synchronizing the remaining tasks.
860 static block_t *Encode(encoder_t *this, picture_t *pic)
862 encoder_t *enc = (encoder_t *)this;
863 encoder_sys_t *sys = enc->p_sys;
864 async_task_t *task;
865 block_t *block = NULL;
867 if (likely(pic != NULL))
869 task = encode_frame( enc, pic );
870 if (likely(task != NULL))
871 async_task_t_fifo_Put(&sys->packets, task);
874 if ( async_task_t_fifo_GetCount(&sys->packets) == (sys->async_depth + 1) ||
875 (!pic && async_task_t_fifo_GetCount(&sys->packets)))
877 assert(async_task_t_fifo_Show(&sys->packets)->syncp != 0);
878 async_task_t *task = async_task_t_fifo_Get(&sys->packets);
879 block = qsv_synchronize_block( enc, task );
880 free(task->syncp);
881 free(task);
884 return block;