C # RTSP player

Source: Internet
Author: User

ONVIF ODM in the field of ONVIF fame is very large, is an open source NVC implementation. Its implementation uses C # C + + F #. The project is large and complex. Recently, I wrote a C # version of the RTSP player by calling its class library. Not very difficult. But to understand the principle, but also need more research and research ODM source code.

At present, the difficulty lies in the decoding process, BGR to RGB arrangement, both efficiency using unsafe pointers. for not specifically engaged in the image, still need to slowly understand.

        private void Decoderframe (Bitmap Bitmap, Videobuffer Videobuffer, playbackstatistics statistics) { try {using (var MD = Videobuffer.lock ()) {Rectangle rect                    = new Rectangle (0, 0, videobuffer.width, videobuffer.height); Lock the bitmap into memory BitmapData BitmapData = bitmap. LockBits (Rect, Imagelockmode.readwrite, bitmap.                    PixelFormat); BitmapData BitmapData = bitmap.                    LockBits (Rect, Imagelockmode.readwrite, PIXELFORMAT.FORMAT24BPPRGB);                    Gets the scan width, for example 1280*3=3840 1280 is a width of 1280 pixels, each pixel has r\g\b three, 3 bytes bitmapdata.stride = videobuffer.stride;                    byte[] buffer = new Byte[videobuffer.size];                    Copy data from MD to array buffer marshal.copy (md.value.scan0Ptr, buffer, 0, videobuffer.size); Copy data from buffer to BitmapData marshal.copy (buffer, 0, Bitmapdata.scan0, W * H * 3); Cycle processing, convert BGR to RGB//http://blog.csdn.net/lulu831110/article/details/4820377 UNSAF                        e {byte* ptr = (byte*) (bitmapdata.scan0); for (int i = 0; i < bitmapdata.height; i++) {for (int j = 0; J < Bitmapdata.width;  J + +) {//swap B and r, i.e. first and third swap byte b                                = *ptr;//b value of byte R = * (ptr + 2);//r value//Swap                                *ptr = R;                                * (PTR + 2) = B;                            PTR + = 3; }/* Indicates crossing useless areas, skipping these extra bytes, and pointing the pointer to the next line, *                     The reason for this is that the image data is stored in memory in 4-byte alignment, * This is explained as follows: Suppose there is a picture width of 6,        * assumed to be in Format24bpprgb format (3 bytes per pixel, in the following discussion, unless otherwise noted, * bitmap are considered 24-bit RGB).                             Obviously, each row needs to be 6*3=18 bytes of storage.                             * This is true for bitmap. * But for bitmapdata, though data. Width is still equal to Image.width, * but presumably for display performance considerations, * The actual number of bytes per line will become greater than or equal to the one closest to it Integer multiples of 4, * The actual number of bytes at this time is stride. In this case, 18 is not an integer multiple of 4, * and a multiple of the nearest 4 from 18 is 20, so this data.                             Stride = 20. * Obviously, when the width itself is a multiple of 4, bitmapdata.stride = image.                             Width * 3.                             * * |-------stride-------------| * |-------width----------| |                             * Scan0: * BGR BGR BGR BGR BGR BGR XX                              * BGR BGR BGR BGR bgr BGR xx * BGR BGR bgr BGR BGR BGR xx * * First Use data.Scan0 found the address of the No. 0 component of the No. 0 pixel, * This address points to a byte type, so it was defined as byte* ptr at that time.                             * When scanning, the current pointer position (may be considered as the No. 0 color component of the current pixel) * Continuous extraction of three values (3 primary colors components.) * Note that 0 1 2 represents the order of B G R. When taking the value pointed to by the pointer, * seemingly p[n] and p + = n then fetch p[0] is equivalent), and then move down 3 positions (ptr + = 3, * as referring to the next The No. 0 color component of a pixel). After Bitmap.width operation, * reached the position of Bitmap.width * 3, * should skip the byte labeled X in the graph (total                             There are stride-width * 3 bytes), * The code is PTR + = Datain.stride-datain.width * 3. * * * * * Generally, if a pixel is a byte                             (Your code makes such assumptions, in fact very bad), * bmpdata.stride should be equal to bmpwidth, but actually often unequal, to a few bytes,                        * Because Bmpdata.stride must be a multiple of 4, if not enough, then fill a few bytes, * Let Bmpdata.stride is a multiple of 4, these extra bytes will not store any color data,     * ptr + = bmpdata.stride-bmpwidth; just skip these extra bytes, * Let the pointer point to the next line * * */ptr + = Bitmapdata.stride-bitma                        Pdata.width * 3; }} bitmap.                    Unlockbits (BitmapData); Bitmap BT = (Bitmap) Bitmap.                    Clone (); Lock (This) {Bitmap_queue.                    Enqueue (BT); } if (statistics.isnosignal) {if (!panel1.                        Visible) {showlable (true);                    } Console.WriteLine ("No Signal"); } else {if (panel1.                        Visible) {showlable (false);   }                 }}}, catch {}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # RTSP player

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.