Example #1
0
        void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            IntPtr audioData = (IntPtr)0;
            IntPtr videoData = (IntPtr)0;

            if (videoFrame != null)
            {
                bool inputSignalChange = videoFrame.GetFlags().HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);
                if (inputSignalChange != m_validInputSignal)
                {
                    m_validInputSignal = inputSignalChange;
                    InputSignalChanged(m_validInputSignal);
                }
                if (inputSignalChange == false) //i.e. frame is valid
                {
                    videoFrame.GetBytes(out videoData);
                    m_mainWindow.callFrameLoop(m_number, videoData);
                }
            }
            if (audioPacket != null)
            {
                audioPacket.GetBytes(out audioData);
                m_mainWindow.handleAudio(m_number, audioData);
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(audioPacket);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
        }
Example #2
0
        /// <summary>
        /// Calback function for new frame
        /// </summary>
        /// <param name="videoFrame"></param>
        /// <param name="audioPacket"></param>
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            // The conditions under which either of these cases occur are unclear but an overstreched processor doesn't help
            if (videoFrame == null || audioPacket == null)
            {
                return;
            }

            IDeckLinkTimecode timecode;

            videoFrame.GetTimecode(config.TimecodeFormat, out timecode);

            if (audioPacket.GetSampleFrameCount() != config.SamplesPerSlot * config.SlotsPerFrame)
            {
                throw new ApplicationException("Wrong buffer size");
            }

            IntPtr buffer;

            audioPacket.GetBytes(out buffer);
            var audioFingerprints = GetAudioFingerprints(buffer, config);

            var timecodeBcd = timecode?.GetBCD() ?? 0;

            FingerprintCreated?.Invoke(this, new FingerprintEventArgs(timecodeBcd, (byte)config.SlotsPerFrame, 0, audioFingerprints));

            // The documentation suggests that neither of these are necessary
            // BM's own code does the former
            // Including these doesn't make anything go bang so, in for a penny...
            Marshal.ReleaseComObject(videoFrame);
            Marshal.ReleaseComObject(audioPacket);
        }
Example #3
0
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            IntPtr data;

            videoFrame.GetBytes(out data);
            ImageUtils.RawYUV2RGBA(data, FOutput.Data, FOutput.Image.ImageAttributes.PixelsPerFrame);
            //FOutput.Image.SetPixels(data);
            FOutput.Send();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
        }
Example #4
0
        void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            if (videoFrame != null)
            {
                bool inputSignal = videoFrame.GetFlags().HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);
                if (inputSignal != m_validInputSignal)
                {
                    m_validInputSignal = inputSignal;
                    InputSignalChanged(m_validInputSignal);
                }
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
        }
Example #5
0
 public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
 {
     this.Lock.AcquireWriterLock(5000);
     try
     {
         videoFrame.GetBytes(out FData);
         System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
         FreshData = true;
     }
     catch
     {
     }
     finally
     {
         this.Lock.ReleaseWriterLock();
     }
 }
Example #6
0
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            try
            {
                if (videoFrame != null)
                {
                    IntPtr srcPtr;
                    videoFrame.GetBytes(out srcPtr);
                    System.Runtime.InteropServices.Marshal.Copy(srcPtr, mediaFrameBuffer, 0, videoFrame.GetRowBytes() * videoFrame.GetHeight());
                    pipeServer.SendPacket(mediaFrameBuffer, 0, videoFrame.GetRowBytes() * videoFrame.GetHeight());
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
                    //unsafe
                    //{
                    //    fixed (byte* p = mediaFrameBuffer)
                    //    {
                    //        IntPtr ptr = (IntPtr)p;
                    //        Bitmap bmp = new Bitmap(960, 1080, 1920*2, System.Drawing.Imaging.PixelFormat.Format32bppRgb, ptr);
                    //        frameCount++;
                    //        //if (frameCount % 25 == 0)
                    //        {
                    //            if (InvokeRequired) this.Invoke((Action)(() =>
                    //            {
                    //                //this.frameCounterLabel.Text = "Frame count: " + frameCount++;

                    //                pictureBox.Image = bmp;
                    //            }));
                    //        }
                    //    }
                    //}
                }

                if (audioPacket != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(audioPacket);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Example #7
0
        void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            if (videoFrame != null)
            {
                bool inputSignalAbsent = videoFrame.GetFlags().HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);

                // Detect change in input signal, restart stream when valid stream detected
                if (!inputSignalAbsent && m_prevInputSignalAbsent)
                {
                    m_deckLinkInput.StopStreams();
                    m_deckLinkInput.FlushStreams();
                    m_deckLinkInput.StartStreams();
                }
                m_prevInputSignalAbsent = inputSignalAbsent;

                // Register video frame received event
                var handler = VideoFrameArrivedHandler;

                // Check whether there are any subscribers to VideoFrameArrivedHandler
                if (handler != null)
                {
                    handler(this, new DeckLinkVideoFrameArrivedEventArgs(videoFrame, inputSignalAbsent));
                }
            }

            if (audioPacket != null)
            {
                // Register audio packet received event
                var handler = AudioPacketArrivedHandler;

                // Check whether there are any subscribers to AudioPacketArrivedHandler
                if (handler != null)
                {
                    handler(this, new DeckLinkAudioPacketArrivedEventArgs(audioPacket));
                }
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
        }
Example #8
0
        // Video Input Frame Arrival
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            IntPtr data;
            int    size = videoFrame.GetRowBytes() * videoFrame.GetHeight();

            videoFrame.GetBytes(out data);

            byte[] array = new byte[size];
            Marshal.Copy(data, array, 0, size);

            frameBuffer = array;

            /*int pixel;
             * bool fullVideo = false;
             * bool fullGraphics = false;
             * bool raceTag = false;
             * bool doubleBox = false;
             * bool lowerThird = false;
             * bool racing = false;
             *
             * Console.Clear();
             *
             * // Race Tag
             * if (PixelMatchesColor(array, 64200, 142, 48, 115) || PixelMatchesColor(array, 64200, 142, 48, 116))
             * {
             *  raceTag = true;
             *  Console.WriteLine("Race Tag: TRUE");
             * }
             * else
             *  Console.WriteLine("Race Tag: FALSE");
             * PrintColor(array, 64200);
             *
             * // Full Graphics
             * if (PixelMatchesColor(array, 25620, 141, 35, 118))
             * {
             *  fullGraphics = true;
             *  Console.WriteLine("Full Graphics: TRUE");
             * }
             * else
             *  Console.WriteLine("Full Graphics: FALSE");
             * PrintColor(array, 25620);
             *
             * // Lower Third
             * if (PixelMatchesColor(array, 832030, 140, 61, 119))
             * {
             *  lowerThird = true;
             *  Console.WriteLine("Lower Third: TRUE");
             * }
             * else
             *  Console.WriteLine("Lower Third: FALSE");
             * PrintColor(array, 832030);
             *
             * // Full Video
             * if (!PixelMatchesColor(array, 256150, 131, 216, 126))
             * {
             *  fullVideo = true;
             *  Console.WriteLine("Full Video: TRUE");
             * }
             * else
             *  Console.WriteLine("Full Video: FALSE");
             * PrintColor(array, 256150);
             *
             * // Double Box
             * if (PixelMatchesColor(array, 705200, 143, 48, 115))
             * {
             *  doubleBox = true;
             *  Console.WriteLine("Double Box: TRUE");
             * }
             * else
             *  Console.WriteLine("Double Box: FALSE");
             * PrintColor(array, 705200);
             *
             * // Racing
             * if (raceTag && !lowerThird && fullVideo && !fullGraphics)
             * {
             *  racing = true;
             *  Console.WriteLine("Racing: TRUE");
             * }
             * else
             *  Console.WriteLine("Racing: FALSE"); */
        }
Example #9
0
        /// <summary>
        /// Generate a 15-bit video fingerprint from the input YUV244 buffer
        /// This is currently unused but could be used to regenerate timecode on the destination video
        /// if this has been lost in the trancode process.  There are many other potential uses for this fingerprint data.
        ///
        /// The algorithm divides the frame into 16 blocks (4x4) and applies the Hadamard masks to divide these blocks
        /// into two sets in 15 different and orthogonal ways.  The relative brightness of each pair of sets yields one
        /// bit of the fingerprint.  The entire fingerprint is comprised of 15 bits; one from each Hadamard mask.
        /// The whole algorithm is resilient to changes of resolution, aspect, brightness, contrast and gamma that might occur in the
        /// transcode process.  It would be a simple matter to automatically detect letterboxing/pillarboxing and to only analyse the
        /// active video area if this were required.
        /// Testing with transcoded broadcast video has shown the fingerprint to be robust and reliable.
        /// </summary>
        /// <param name="videoFrame"></param>
        /// <param name="sums"></param>
        /// <returns></returns>
        private unsafe int VideoFingerprint(IDeckLinkVideoInputFrame videoFrame, int[] sums)
        {
            var qheight = videoFrame.GetHeight() >> 2;
            var qwidth  = videoFrame.GetWidth() >> 2;

            IntPtr buffer;

            videoFrame.GetBytes(out buffer);

            var ptr = (byte *)buffer + 1; // Skip over first U byte

            for (var r = 0; r < qheight; r++)
            {
                for (var c = 0; c < qwidth; c++)
                {
                    sums[0] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[1] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[2] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[3] += *ptr;
                    ptr     += 2;
                }
            }

            for (var r = 0; r < qheight; r++)
            {
                for (var c = 0; c < qwidth; c++)
                {
                    sums[4] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[5] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[6] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[7] += *ptr;
                    ptr     += 2;
                }
            }

            for (var r = 0; r < qheight; r++)
            {
                for (var c = 0; c < qwidth; c++)
                {
                    sums[8] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[9] += *ptr;
                    ptr     += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[10] += *ptr;
                    ptr      += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[11] += *ptr;
                    ptr      += 2;
                }
            }

            for (var r = 0; r < qheight; r++)
            {
                for (var c = 0; c < qwidth; c++)
                {
                    sums[12] += *ptr;
                    ptr      += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[13] += *ptr;
                    ptr      += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[14] += *ptr;
                    ptr      += 2;
                }

                for (var c = 0; c < qwidth; c++)
                {
                    sums[15] += *ptr;
                    ptr      += 2;
                }
            }

            for (var i = 0; i < 16; i++)
            {
                sums[i] /= qwidth * qheight;
            }

            var videoFingerprint = 0;
            var bit = 1;

            for (var i = 0; i < 15; i++)
            {
                var sum = 0;
                for (var j = 0; j < 16; j++)
                {
                    sum += sums[j] * masks[i, j];
                }

                if (sum > 0)
                {
                    videoFingerprint |= bit;
                }

                bit <<= 1;
            }
            return(videoFingerprint);
        }
Example #10
0
 public DeckLinkVideoFrameArrivedEventArgs(IDeckLinkVideoInputFrame videoFrame, bool inputInvalid)
 {
     this.videoFrame   = videoFrame;
     this.inputInvalid = inputInvalid;
 }
Example #11
0
		public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
		{
			this.Lock.AcquireWriterLock(5000);
			try
			{
				videoFrame.GetBytes(out FData);
				System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
				FreshData = true;
			}
			catch
			{

			}
			finally
			{
				this.Lock.ReleaseWriterLock();
			}
		}
Example #12
0
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            if (videoFrame == null && audioPacket == null)
            {
                return;
            }
            try
            {
                if (!_Streaming)
                {
                    return;
                }
                if (!_VideoLock.Wait(TimeSpan.Zero))
                {
                    return;
                }
                try
                {
                    Interlocked.Increment(ref _FrameCount);
                    Run(() => frameCount.Text = _FrameCount.ToString());

                    if (videoFrame != null)
                    {
                        var rowBytes = videoFrame.GetRowBytes();
                        var height   = videoFrame.GetHeight();

                        IntPtr framePointer;
                        videoFrame.GetBytes(out framePointer);

                        var frame = new byte[rowBytes * height];
                        Marshal.Copy(framePointer, frame, 0, frame.Length);

                        if (writeRaw.Checked)
                        {
                            _VideoWriter.Write(frame);
                        }

                        if (writeEncoded.Checked)
                        {
                            _EncodeTask.Write(frame, 0, frame.Length);
                        }
                    }

                    if (audioPacket != null)
                    {
                        IntPtr audioPointer;
                        audioPacket.GetBytes(out audioPointer);

                        var frameCount = audioPacket.GetSampleFrameCount();

                        var audio = new byte[frameCount * _AudioChannels * (_AudioSampleDepth / 8)];
                        Marshal.Copy(audioPointer, audio, 0, audio.Length);

                        if (writeRaw.Checked)
                        {
                            _AudioWriter.Write(audio);
                        }
                    }
                }
                finally
                {
                    _VideoLock.Release();
                }
            }
            finally
            {
                if (videoFrame != null)
                {
                    Marshal.ReleaseComObject(videoFrame);
                }
                if (audioPacket != null)
                {
                    Marshal.ReleaseComObject(audioPacket);
                }
            }
        }
Example #13
0
		void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame iVideoInputFrame, IDeckLinkAudioInputPacket iAudioPacket)
		{
            try
            {
                if (bAVFrameArrivedAttached)
                {
                    IDeckLinkVideoFrame iVideoFrame = iVideoInputFrame;
                    IntPtr pBytesVideo = IntPtr.Zero, pBytesAudio = IntPtr.Zero;
                    int nBytesVideoQty = 0, nBytesAudioQty = 0;
                    if (null != iVideoFrame)
                    {
                        if (null != _iDLVideoConversion)
                        {
                            _iDLVideoConversion.ConvertFrame(iVideoFrame, _iVideoFrameTarget);
                            iVideoFrame = _iVideoFrameTarget;
                        }
                        _iVideoFrameLast = iVideoFrame;
                    }
                    else
                    {
                        nFramesDroppedVideo++;
                        iVideoFrame = _iVideoFrameLast;
                        (new Logger()).WriteWarning("video frame dropped");
                    }
                    if (null != iVideoFrame)
                    {
                        iVideoFrame.GetBytes(out pBytesVideo);
                        if (IntPtr.Zero != pBytesVideo)
                        {
                            nBytesVideoQty = iVideoFrame.GetRowBytes() * iVideoFrame.GetHeight();
                            nFramesVideo++;
                        }
                        else
                            (new Logger()).WriteWarning("video frame is empty");
                    }
                    if (null != iAudioPacket)
                    {
                        iAudioPacket.GetBytes(out pBytesAudio);
                        if (IntPtr.Zero != pBytesAudio)
                        {
                            nBytesAudioQty = iAudioPacket.GetSampleFrameCount() * ((int)_BMDAudioSampleType.bmdAudioSampleType16bitInteger / 8) * 2;
                            nFramesAudio++;
                        }
                        else
                            (new Logger()).WriteWarning("audio frame is empty");
                    }
                    else
                    {
                        nFramesDroppedAudio++;
                        (new Logger()).WriteWarning("audio frame dropped");
                    }
                    OnAVFrameArrived(nBytesVideoQty, pBytesVideo, nBytesAudioQty, pBytesAudio);
                }
            }
            catch (Exception ex)
            {
                (new Logger()).WriteError(ex);
            }
        }
Example #14
0
        void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            if (videoFrame != null)
            {
                try
                {
                    var frameFlags = videoFrame.GetFlags();

                    bool inputSignal = frameFlags.HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);

                    if (inputSignal != validInputSignal)
                    {
                        validInputSignal = inputSignal;
                        InputSignalChanged?.Invoke(validInputSignal);
                    }
                    else
                    {
                        int width  = videoFrame.GetWidth();
                        int height = videoFrame.GetHeight();
                        int stride = videoFrame.GetRowBytes();
                        var format = videoFrame.GetPixelFormat();

                        var bufferLength = stride * height;
                        videoFrame.GetBytes(out IntPtr pBuffer);

                        VideoDataArrived?.Invoke(pBuffer, bufferLength, 0);


                        //var f = File.Create(@"d:\testBMP2\" + DateTime.Now.ToString("HH_mm_ss_fff") + " " + width + "x" + height + "_" + format + ".raw");

                        //byte[] data = new byte[bufferLength];
                        //Marshal.Copy(pBuffer, data, 0, data.Length);
                        //f.Write(data, 0, data.Length);
                        //f.Close();
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(videoFrame);
                }
            }


            if (audioPacket != null)
            {
                try
                {
                    long packetTime = 0;
                    //audioPacket.GetPacketTime(out packetTime, 30000);

                    int sampleSize   = ((int)AudioSampleType / 8); //32bit
                    int samplesCount = audioPacket.GetSampleFrameCount();
                    int dataLength   = sampleSize * AudioChannelsCount * samplesCount;

                    if (dataLength > 0)
                    {
                        audioPacket.GetBytes(out IntPtr pBuffer);

                        if (pBuffer != IntPtr.Zero)
                        {
                            byte[] data = new byte[dataLength];
                            Marshal.Copy(pBuffer, data, 0, data.Length);

                            AudioDataArrived?.Invoke(data, packetTime);
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(audioPacket);
                }
            }
        }