Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get webcam data Decoding by FFmpeg

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how FFmpeg obtains webcam data decoding, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Real-time coding of USB cameras has been discussed earlier. This time change the way of thinking, try to intercept the H264 stream of the webcam, decode it and play it.

The test code here is based on the Haikang camera.

The general process of decoding is the same as before, but some functions have been added.

There are three steps for FFmpeg to open a media file and view the information of the media file:

Avformat_open_input

Avformat_find_stream_info

Av_dump_format

After calling three functions in turn, we can know all kinds of information of the code stream very clearly.

The complete code:

# include # include "queue.h" extern "C" {# include # include # include} # pragma comment (lib, "avcodec.lib") # pragma comment (lib, "avformat.lib") # pragma comment (lib, "avutil.lib") # pragma comment (lib, "swscale.lib") using namespace std;using namespace cv; DWORD WINAPI opencv_imshow (LPVOID lparam) {result_link_type* result_link = (result_link_type*) lparam Struct result_node_datatype * result_node2 = NULL; while (1) {result_node2 = result_pop (result_link); if (result_node2 = = NULL) {Sleep (1); continue;} imshow ("frame", result_node2- > result); waitKey (1);} int main (int argc, const char * argv []) {HANDLE thread1; result_link_type * result_link = new result_link_type; result_link- > head = result_link- > end = NULL; result_link- > result_num = 0 Thread1 = CreateThread (NULL, 0, opencv_imshow, (LPVOID) result_link, 0, NULL); int i; int videoStream; int frameFinished; int numBytes; int ret; int got_picture; long prepts = 0; bool first_time = true; AVCodec * pCodec; AVFrame * pFrame; AVFrame * pFrameRGB; AVPacket packet; AVCodecContext * pCodecCtx; AVFormatContext * pFormatCtx = NULL;// structure AVFormatContext: contains more code stream parameters static struct SwsContext * img_convert_ctx; uint8_t * buffer; Mat pCvMat Char filepath [] = "rtsp://admin:jdh223456@10.170.6.187/axis-media/media.amp?camera=2"; / / get the path to the stream av_register_all (); / / register the codec avformat_network_init (); / / load the socket library and the network encryption protocol-related library if (avformat_open_input (& pFormatCtx, filepath, NULL, NULL)! = 0) / / Open the multimedia data and get the information {return-1 } if (avformat_find_stream_info (pFormatCtx, NULL)

< 0)//读取视音频数据并且获得信息 { return -1; } av_dump_format(pFormatCtx, 0, argv[1], false);//手工调试函数,看到pFormatCtx->

Content of streams videoStream =-1; for (I = 0; I

< pFormatCtx->

Nb_streams; istreams +) {if (pFormatCtx- > streams [I]-> codec- > codec_type = = AVMEDIA_TYPE_VIDEO) {videoStream = I; break;}} if (videoStream = =-1) {return-1;} pCodecCtx = pFormatCtx- > streams [videostream]-> codec; pCodec = avcodec_find_decoder (pCodecCtx- > codec_id); / / find decoder if (pCodec = NULL) {return-1;} if (avcodec_open2 (pCodecCtx, pCodec, 0)

< 0)//初始化AVCodecContext { return -1; } if (pCodecCtx->

Time_base.num > 1000 & & pCodecCtx- > time_base.den = 1) {pCodecCtx- > time_base.den = 1000;} pFrame = av_frame_alloc (); / / allocate memory pFrameRGB = av_frame_alloc (); I = 0 While (1) {if (av_read_frame (pFormatCtx, & packet) > = 0) / read several frames of audio or video in the bitstream {if (packet.stream_index = = videoStream) {ret = avcodec_decode_video2 (pCodecCtx, pFrame, & got_picture, & packet); / / start decoding if (ret

< 0) { printf("Decode Error.(解码错误)\n"); return ret; } if (got_picture)//解码成功,got_picture返回任意非零值 { if (first_time) { //初始化SwsContext img_convert_ctx = sws_getContext(pCodecCtx->

Width, pCodecCtx- > height, pCodecCtx- > pix_fmt, pCodecCtx- > width, pCodecCtx- > height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); if (img_convert_ctx = = NULL) {fprintf (stderr, "Cannot initialize the conversion context!\ n"); exit (1);} numBytes = avpicture_get_size (AV_PIX_FMT_BGR24, pCodecCtx- > width, pCodecCtx- > height); buffer = (uint8_t *) av_malloc (numBytes) Avpicture_fill ((AVPicture *) pFrameRGB, buffer, AV_PIX_FMT_BGR24, pCodecCtx- > width, pCodecCtx- > height); / / allocator memory for BGR buffer pCvMat.create (cv::Size (pCodecCtx- > width, pCodecCtx- > height), CV_8UC3); first_time = false;} / / processing image data sws_scale (img_convert_ctx, pFrame- > data, pFrame- > linesize, 0, pCodecCtx- > height, pFrameRGB- > data, pFrameRGB- > linesize); memcpy (pCvMat.data, buffer, numBytes) Struct result_node_datatype * result_node = new struct result_node_datatype; result_node- > result = pCvMat; result_push (result_link, result_node);}} av_free_packet (& packet);}} / / free (buffer); av_free (buffer); av_free (pFrameRGB); av_free (pFrame); avcodec_close (pCodecCtx); avformat_close_input (& pFormatCtx); system ("Pause"); return 0;}

Queue function:

# include "queue.h" # include using namespace std; void result_push (result_link_type* result_link, result_node_datatype * result_node) / / queue operation {if (result_link- > head = = NULL) {result_link- > head = result_node; result_link- > end = result_link- > head; result_link- > result_num++;} else {result_link- > end- > next = result_node; result_link- > end = result_node; result_link- > result_num++ }} struct result_node_datatype* result_pop (result_link_type* result_link) / / dequeue operation {struct result_node_datatype* tmp_node; if (result_link- > head = = NULL) return NULL; else if (result_link- > head = = result_link- > end) {return NULL;} else {tmp_node = result_link- > head; result_link- > head = result_link- > head- > next; result_link- > result_num--; return tmp_node;}}

Header file of the queue function:

# ifndef QUEUE_H#define QUEUE_H#include # include using namespace cv; typedef struct result_link_datatype {struct result_node_datatype* head; struct result_node_datatype* end; int result_num;} result_link_type; struct result_node_datatype {Mat result; struct result_node_datatype* next;}; void result_push (result_link_type* result_link, result_node_datatype* result_node) / / queue operation struct result_node_datatype* result_pop (result_link_type* result_link); / / team operation # endif

The decoded data is entered into the queue, then taken out of the queue, and displayed by opencv (opencv display is another thread function).

Admin:jdh223456@10.170.6.187, here is the name and IP address of the camera.

Thank you for reading this article carefully. I hope the article "how to get FFmpeg data Decoding from webcam" shared by Xiaobian will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report