Blame view

src/decoder/interface/VideoTools.cpp 4.11 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  #include "VideoTools.h"
  #include "logger.hpp"
  
  extern "C" {
     	#include <libavcodec/avcodec.h> 
  	#include <libavdevice/avdevice.h> 
  	#include <libavformat/avformat.h> 
  	#include <libavfilter/avfilter.h> 
  	#include <libavutil/avutil.h> 
      #include <libavutil/pixdesc.h> 
  	#include <libswscale/swscale.h>
      #include <libavutil/imgutils.h>
  }
  
  namespace VideoTools {
  
  FFImgInfo* snapshot(const string& uri){
      if (uri.empty()){
          return nullptr;
      }
   
      AVFormatContext* ifmt_ctx = nullptr;
  	AVCodecContext* codec_ctx = nullptr;
  	AVCodec* codec = nullptr;
  	AVPacket* pkt = nullptr;
  	AVFrame *frame = nullptr;
  	AVFrame *pFrameRGB = nullptr;	
  	int video_index = -1;
  	AVStream* st = nullptr;
  	SwsContext *img_convert_ctx = nullptr;
  	uint8_t *buffer = nullptr;
      int numBytes = 0;
  	int index = 0;
  
  	FFImgInfo* imgInfo = nullptr;
   
   	//av_register_all();
  	avformat_network_init();
  
  	// 参数设置
  	AVDictionary *options = nullptr;
  	av_dict_set( &options, "bufsize", "655360", 0 );
  	av_dict_set( &options, "rtsp_transport", "tcp", 0 );
  	av_dict_set( &options, "stimeout", "30000000", 0 ); // 单位为 百万分之一秒
  	
  	///打开输入的流
  	int ret = avformat_open_input(&ifmt_ctx, uri.c_str(), nullptr, &options);
  	if (ret != 0){
  		printf("Couldn't open input stream.\n");
  		goto end_flag ;
  	}
   
  	//查找流信息
  	if (avformat_find_stream_info(ifmt_ctx, nullptr) < 0){
  		printf("Couldn't find stream information.\n");
  		goto end_flag ;
  	}
   
  	//找到视频流索引
      video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
   
      st = ifmt_ctx->streams[video_index];
      
      //找到解码器
      codec = avcodec_find_decoder(st->codecpar->codec_id);
      if (!codec){
          fprintf(stderr, "Codec not found\n");
          goto end_flag ;
      }
   
      //申请AVCodecContext
      codec_ctx = avcodec_alloc_context3(codec);
      if (!codec_ctx){
          goto end_flag ;
      }
   
  	avcodec_parameters_to_context(codec_ctx, ifmt_ctx->streams[video_index]->codecpar);
   
      //打开解码器
      if ((ret = avcodec_open2(codec_ctx, codec, nullptr) < 0)){
          goto end_flag ;
      }
  	
      // 计算解码后原始数据所需缓冲区大小,并分配内存空间 Determine required buffer size and allocate buffer
      numBytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24, codec_ctx->width, codec_ctx->height, 1);
      buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
      
  	pFrameRGB = av_frame_alloc();
      av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_BGR24, codec_ctx->width, codec_ctx->height, 1);
  
  	img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height,codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_BGR24, 
  		   SWS_BICUBIC, nullptr, nullptr, nullptr);
   
  	pkt = av_packet_alloc();
  	frame = av_frame_alloc();
  	while (av_read_frame(ifmt_ctx, pkt) >= 0){
  		if (pkt->stream_index == video_index){
  			int ret = avcodec_send_packet(codec_ctx, pkt);
  			if (ret >= 0){
  				ret = avcodec_receive_frame(codec_ctx, frame);
  				if ((ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) || ret < 0){
  					LOG_ERROR("Failed to receive frame: {}",ret);
  					continue;
  				}
  
  				index ++ ;
  
  				if (index >= 5){
  					// 取解码出来的第三帧,应该可以一定程度优化花屏问题
  					sws_scale(img_convert_ctx, (const unsigned char* const*)frame->data, frame->linesize, 0, codec_ctx->height, pFrameRGB->data, pFrameRGB->linesize);
  
  					imgInfo = new FFImgInfo();
  					imgInfo->pData = buffer;
  					imgInfo->height = codec_ctx->height;
  					imgInfo->width = codec_ctx->width;
  
  					break;
  				}
  			}
  		}
  		av_packet_unref(pkt);
  	}
  
  end_flag:
  	if (codec_ctx != nullptr){
  		avcodec_close(codec_ctx);
  		avcodec_free_context(&codec_ctx);
  	}
  	
  	if (ifmt_ctx != nullptr){
  		avformat_close_input(&ifmt_ctx);
  	}
  	
  	if (frame != nullptr){
  		av_frame_free(&frame);
  	}
  
  	if (pFrameRGB != nullptr){
  		av_frame_free(&pFrameRGB);
  	}
  
  	if (pkt != nullptr){
  		av_packet_free(&pkt);
  	}
  
  	return imgInfo;
  }
  
  void releaseFFImgInfo(FFImgInfo* info) {
  	if(nullptr != info){
  		if(info->pData != nullptr){
  			av_free(info->pData);
  			info->pData = nullptr;
  		}
  		delete info;
  		info = nullptr;
  	}
  }
  
  }  // namespace