public void Close() { logger.Debug("MfH264Dxva2Decoder::Close()"); if (InputMediaType != null) { InputMediaType.Dispose(); InputMediaType = null; } if (OutputMediaType != null) { OutputMediaType.Dispose(); OutputMediaType = null; } if (decoder != null) { decoder.Dispose(); decoder = null; } }
public bool ProcessSample(Sample inputSample, Action <Sample> OnSampleDecoded, Action <MediaType> OnMediaTypeChanged = null) { bool Result = false; if (inputSample == null) { return(false); } decoder.ProcessInput(0, inputSample, 0); //if (decoder.OutputStatus == (int)MftOutputStatusFlags.MftOutputStatusSampleReady) { decoder.GetOutputStreamInfo(0, out TOutputStreamInformation streamInfo); MftOutputStreamInformationFlags flags = (MftOutputStreamInformationFlags)streamInfo.DwFlags; bool createSample = !flags.HasFlag(MftOutputStreamInformationFlags.MftOutputStreamProvidesSamples); do { Sample pSample = null; // Create output sample if (createSample) { pSample = MediaFactory.CreateSample(); pSample.SampleTime = inputSample.SampleTime; pSample.SampleDuration = inputSample.SampleDuration; pSample.SampleFlags = inputSample.SampleFlags; //logger.Debug("CreateSample: " + inputSample.SampleTime); using (var mediaBuffer = MediaFactory.CreateMemoryBuffer(streamInfo.CbSize)) { pSample.AddBuffer(mediaBuffer); } } TOutputDataBuffer[] outputBuffers = new TOutputDataBuffer[1]; var outputBuffer = new TOutputDataBuffer { DwStatus = 0, DwStreamID = 0, PSample = pSample, PEvents = null, }; outputBuffers[0] = outputBuffer; var res = decoder.TryProcessOutput(TransformProcessOutputFlags.None, outputBuffers, out TransformProcessOutputStatus status); //logger.Info("TryProcessOutput(...) " + res + " " + outputDataBuffer[0].DwStatus); if (res == SharpDX.Result.Ok) { var buf = outputBuffers[0]; var outputSample = buf.PSample; var pEvents = buf.PEvents; if (pEvents != null) { var eventsCount = pEvents.ElementCount; Debug.Assert(eventsCount == 0, "eventsCount == 0"); if (eventsCount > 0) { for (int i = 0; i < eventsCount; i++) { var e = pEvents.GetElement(i); if (e != null) { e.Dispose(); e = null; } } } } Debug.Assert(outputSample != null, "res.Success && outputSample != null"); OnSampleDecoded?.Invoke(outputSample); Result = true; //continue; } else if (res == SharpDX.MediaFoundation.ResultCode.TransformNeedMoreInput) { //logger.Info("-------------------------"); if (pSample != null) { pSample.Dispose(); pSample = null; } Result = true; break; } else if (res == SharpDX.MediaFoundation.ResultCode.TransformStreamChange) { logger.Warn(res.ToString() + " TransformStreamChange"); MediaType newOutputType = null; try { decoder.TryGetOutputAvailableType(outputStreamId, 0, out newOutputType); decoder.SetOutputType(outputStreamId, newOutputType, 0); if (OutputMediaType != null) { OutputMediaType.Dispose(); OutputMediaType = null; } OutputMediaType = newOutputType; logger.Info("============== NEW OUTPUT TYPE=================="); logger.Info(MfTool.LogMediaType(OutputMediaType)); OnMediaTypeChanged?.Invoke(OutputMediaType); } finally { //newOutputType?.Dispose(); //newOutputType = null; } } else { res.CheckError(); Result = false; break; } }while (true); } return(Result); }