Qt writes video surveillance system 75-calculates real-time bit rate and displays it

1. Foreword

For real-time video display of surveillance cameras, it is generally required to display real-time statistics on the channel screen. One is to test the performance of the entire software, and at the same time check whether the current stream is the main stream or the sub-stream, and whether the device is The video data is not really transmitted according to the set stream size. The video bit rate is the number of data bits transmitted per unit time during data transmission. Generally, the unit we use is kbps, that is, thousands of bits per second. The popular understanding is the sampling rate. The larger the sampling rate per unit time, the higher the accuracy, and the closer the processed file is to the original file. But the file size is directly proportional to the sampling rate, so almost all encoding formats focus on how to use the lowest bit rate to achieve the least distortion. The cbr (fixed bit rate) and vbr (variable bit rate) derived from this core Rate), are all articles in this area, but things are not absolute. For example, for an audio, the higher the bit rate, the smaller the compressed ratio, the smaller the sound quality loss, and the higher the sound quality of the audio source. near.

After ffmpeg decoding, if you need to count the real-time bit rate, you generally need to know the frame rate first. For example, if the frame rate is 25, then count the packet size of 25 frames of video. The cumulative sum is the bit rate, which may have a little deviation from the actual one. Big, after the bit rate is calculated, it is sent out through the signal, and where it is received, it can be added to the video frame with a filter in the form of a text watermark, and then pasted to the camera in the form of an OSD. In fact, the camera manufacturer may also have the OSD label setting of the real-time bit rate, which can be turned on. After it is turned on, the camera will calculate it by itself, and the transmitted video stream will carry the bit rate.

2. Effect picture

3. Experience URL

  1. Domestic site: https://gitee.com/feiyangqingyun
  2. International site: https://github.com/feiyangqingyun
  3. Personal works: https://blog.csdn.net/feiyangqingyun/article/details/97565652
  4. Experience address: https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g Extraction code: 01jf File name: bin_video_system.

4. Related code

void FFmpegThread::checkRealPacketSize(AVPacket *packet, int maxCount)
{<!-- -->
    realPacketCount++;
    realPacketSize + = packet->size;
    if (realPacketCount == maxCount) {<!-- -->
        qreal kbps = (qreal)realPacketSize / 100;
        //debug("Real-time bit rate", QString("Size: %1 kbps").arg(QString::number(kbps, 'f', 0)));
        emit receiveKbps(kbps, maxCount);
        realPacketSize = 0;
        realPacketCount = 0;
    }
}

void FFmpegThread::decodeVideo0(AVPacket *packet)
{<!-- -->
    if (countKbps) {<!-- -->
        checkRealPacketSize(packet, frameRate);
    }

    //Some start time of the first frame is not 0 but a specified time, so it needs to be subtracted
    if (videoFirstPts > 5000) {<!-- -->
        packet->pts -= videoFirstPts;
        packet->dts = packet->pts;
    }

    if (useSync) {<!-- -->
        //Add to the queue and hand it over to the decoding synchronization thread
        videoSync->append(FFmpegHelper::creatPacket(packet));
    } else {<!-- -->
        //Direct current thread decoding
        decodeVideo1(packet);
        if (decodeType != DecodeType_Fastest) {<!-- -->
            FFmpegHelper::delayTime(formatCtx, packet, startTime);
        }
    }
}

void frmDemoVideoOsd::receiveKbps(qreal kbps, int frameRate)
{<!-- -->
    OsdInfo osd;
    osd.name = "kbps";
    osd.color = "#FF0000";
    osd. fontSize = fontSize;
    osd.position = OsdPosition_RightTop;
    osd.text = QString("%1 kbps\\
%2 fps").arg((int)kbps).arg(frameRate);
    ui->videoWidget->setOsd(osd);
}

5. Features

5.1 Software modules

  1. Video monitoring module, various docking small window sub-modules, including device list, graphic alarm information, window information, PTZ control, preset position, cruise setting, device control, floating map, web browsing, etc.
  2. Video playback module, including local playback, remote playback, device playback, picture playback, video upload, etc.
  3. Electronic map module, including picture map, online map, offline map, path planning, etc.
  4. Log query module, including local logs, device logs, etc.
  5. System settings module, including system settings (basic settings, video parameters, database settings, map configuration, serial port configuration, etc.), video recorder management, camera management, polling configuration, recording plan, user management, etc.

5.2 Basic functions

  1. Support various video streams (rtsp, rtmp, http, etc.), video files (mp4, rmvb, avi, etc.), local USB camera playback.
  2. Support multi-screen switching, including 1, 4, 6, 8, 9, 13, 16, 25, 36, 64 screen switching.
  3. Support full-screen switching, a variety of switching methods include mouse right-click menu, toolbar buttons, shortcut keys (alt + enter full screen, esc to exit full screen).
  4. Support video polling, including 1, 4, 9, 16 screen polling, and can set polling group (polling plan), polling interval, code stream type, etc.
  5. Support onvif protocol, including device search, PTZ control, preset position, device control (picture parameters, proofreading time, system restart, snapshot pictures, etc.).
  6. Support permission management, different users can correspond to different module permissions, such as deleting logs, shutting down the system, etc.
  7. Various databases are supported, including sqlite, mysql, sqlserver, postgresql, oracle, Renda Jincang, etc.
  8. The local USB camera supports setting parameters such as resolution and frame rate.
  9. All docking modules automatically generate corresponding menus to control display and hide, which can be popped up by right clicking on the title bar.
  10. Support display all modules, hide all modules, reset normal layout, reset full screen layout.
  11. Double-click the device to pop up a real-time preview video, supporting image maps, online maps, offline maps, etc.
  12. Drag the camera node to the corresponding window to play the video, and support dragging local files to play directly.
  13. Deleting videos supports multiple methods such as right mouse button deletion, suspension bar closing deletion, dragging to the outside of the video monitoring panel to delete, etc.
  14. The device button on the picture map can be dragged freely, and the location information is automatically saved. On the Baidu map, you can click the mouse to obtain the latitude and longitude information, which is used to update the device location.
  15. Any channel in the video monitoring panel window supports dragging and switching, and responds instantly.
  16. It encapsulates Baidu map, view switching, movement track, equipment point, mouse press to obtain latitude and longitude, etc.
  17. Operations such as double-clicking a node, dragging a node, or dragging a window to exchange positions, etc., will automatically update and save the last playback address, which will be automatically applied next time the software is opened.
  18. The volume bar control in the lower right corner, loses focus and automatically hides, and the volume bar has a mute icon.
  19. Support video screenshot, you can specify a single or screenshot of all channels, and there is also a screenshot button in the small toolbar at the bottom.
  20. Support overtime automatically hide the mouse pointer, automatic full screen mechanism.
  21. Support onvif PTZ control, you can move the PTZ camera up, down, left, and right, including reset and focus adjustment.
  22. Support onvif preset position, you can add, delete, modify the preset position, you can call the start position.
  23. Support onvif image parameter settings, including brightness, contrast, saturation, sharpness, etc.
  24. Support other operations of onvif, including image capture, network settings, time adjustment, restart, event subscription, etc.
  25. Support any onvif camera, including but not limited to Hikvision, Dahua, Uniview, Tiandiweiye, Huawei, etc.
  26. Video can be saved, with optional timing storage or single file storage, and optional storage interval.
  27. You can set the video stream communication method tcp + udp, and you can set the video decoding speed priority, quality priority, balance, etc.
  28. You can set the software Chinese name, English name, LOGO icon, etc.
  29. Stored video files can be exported to a specified directory and uploaded to the server in batches.
  30. Perfect recording plan settings, support for each channel 7 * 24 hours every half hour to set whether to store recordings.

5.3 Features

  1. The main interface adopts the docking form mode, and various components are added in the form of small modules, and any module can be customized to add.
  2. The docking module can be dragged anywhere to embed and float, and it supports maximizing full screen and multiple screens.
  3. Dual layout file storage mechanism, normal mode and full-screen mode correspond to different layout schemes, automatically switch and save, for example, full-screen mode can highlight several modules and display them transparently in the designated position, which is more sci-fi and modern.
  4. The original onvif protocol mechanism adopts the underlying protocol analysis (udp broadcast search + http request execution command), which is lighter, easier to understand and easier to learn and expand, and does not rely on any third-party components such as gsoap.
  5. Original data import, export, and print mechanisms, cross-platform without relying on any components, and instantly export data.
  6. Multiple built-in original components, super value in the universe, including data import and export components (export to xls, pdf, printing), database components (database management thread, automatic cleaning data thread, universal paging, data request, etc.), map components , video monitoring components, multi-threaded file sending and receiving components, onvif communication components, common browser kernel components, etc.
  7. Custom information box + error box + query box + prompt box in the lower right corner (including multiple formats), etc.
  8. Exquisite skinning, up to 17 sets of skin styles can be changed at will, all styles are unified, including menus, etc.
  9. The video control floating bar can add multiple buttons by itself, and the small toolbar at the bottom of the monitoring interface can also add buttons by itself.
  10. Double-click the camera node to automatically play the video, double-click the node to automatically add videos in sequence, and automatically jump to the next one, double-click the parent node to automatically add all videos under the node. Optional main stream and sub stream.
  11. Video recorder management, camera management, can add, delete, modify, import, export and print information, and immediately apply the new device information to generate a tree list without restarting.
  12. A variety of kernels can be selected to switch freely, ffmpeg, vlc, mpv, etc., can be set in pro. It is recommended to use ffmpeg, which is the most cross-platform, and the compiled libraries on the linux and mac platforms are provided by default.
  13. Support hard decoding, you can set the hard decoding type (qsv, dxva2, d3d11va, etc.).
  14. By default, opengl is used to draw video, ultra-low CPU resource usage, supports yuyv and nv12 two formats of drawing, and the performance is explosive.
  15. Labels and graphic information support three drawing methods, drawing to the mask layer, drawing to the picture, and drawing from the source (corresponding information can be stored in a file).
  16. Highly customizable, users can easily derive their own functions on this basis, such as adding custom modules, adding operation modes, robot monitoring, drone monitoring, excavator monitoring, etc.
  17. Support xp, win7, win10, win11, linux, mac, various domestic systems (UOS, Winning Kirin, Galaxy Kirin, etc.), embedded linux and other systems.
  18. The comments are complete, the project structure is clear, the super detailed and complete user development manual is accurate to the function description of each code file, and the version is continuously iterated.