Open GOP 和close GOP

Close GOP是说在当前GOP中,所有的P帧和B帧只会参考GOP内部的I帧或P帧;而Open GOP是说当前GOP中的B帧会参考邻近GOP的I帧。

如果是Close GOP,它一定是以P帧结尾;Open GOP一定是以B帧结尾。为了便于区分Close GOP和Open GOP,在一个GOP的第一帧如果是IDR帧,则该GOP为Close GOP;如果GOP的第一帧为一般的I帧,则为Open GOP。

以上可以这么理解:

  • 如果是Close GOP,如果是B结尾,那么肯定要参考后面的帧,但是它又是GOP结尾,不允许跨GOP参考,故Close GOP必须是P结尾。
  • 同理,Open GOP要以B结尾,这样它可以参考下一个GOP的I帧。
  • 为什么GOP的第一帧是IDR帧,则该GOP为Close GOP?

因为GOP第一帧为IDR帧,会清空DPB,以下图Close GOP为例,当前GOP的最后一个frame为B帧,它要参考下一个GOP的I帧和它前面的P帧,所以要现对I帧编码,如果I帧为IDR帧,会把所有参考帧都清除,包括P帧,B帧没法参考P帧了,所以GOP的第一帧为IDR帧,它只能是Close GOP。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现一个 RTSP 服务器需要考虑以下几个方面: 1. RTSP 协议的实现; 2. RTP/RTCP 协议的实现; 3. 视频编码和解码的实现; 4. 网络传输的实现。 其中,RTSP 协议和 RTP/RTCP 协议是 RTSP 服务器的核心,视频编码和解码是 RTSP 服务器的重要功能,而网络传输则是 RTSP 服务器的基础。 下面是一个使用 C++ 和 FFmpeg 实现 RTSP 服务器的示例代码: ``` #include <iostream> #include <string> #include <chrono> #include <thread> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> extern "C" { #include <libavformat/avformat.h> #include <libavdevice/avdevice.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> #include <libavutil/time.h> } using namespace std; #define WIDTH 640 #define HEIGHT 480 #define FPS 25 int main(int argc, char* argv[]) { AVFormatContext* pFormatCtx = NULL; AVOutputFormat* fmt = NULL; AVStream* video_st = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; uint8_t* picture_buf = NULL; AVFrame* picture = NULL; int size; int framecnt = 0; AVPacket pkt; int got_picture = 0; int ret = 0; int i = 0; int err = 0; AVDictionary* options = NULL; AVIOContext* pb = NULL; AVFormatContext* ofmt_ctx = NULL; AVStream* video_out = NULL; AVCodecContext* codec_out = NULL; av_register_all(); avdevice_register_all(); // 1. 打开摄像头 AVInputFormat* ifmt = av_find_input_format("v4l2"); if (avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL) != 0) { cout << "Couldn't open input stream." << endl; return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { cout << "Couldn't find stream information." << endl; return -1; } av_dump_format(pFormatCtx, 0, "/dev/video0", 0); // 2. 初始化编码器 fmt = av_guess_format("rtsp", NULL, NULL); if (!fmt) { cout << "Couldn't guess format." << endl; return -1; } pCodec = avcodec_find_encoder(fmt->video_codec); if (!pCodec) { cout << "Couldn't find encoder." << endl; return -1; } video_st = avformat_new_stream(NULL, pCodec); if (!video_st) { cout << "Couldn't create video stream." << endl; return -1; } pCodecCtx = video_st->codec; pCodecCtx->codec_id = fmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->bit_rate = 400000; pCodecCtx->width = WIDTH; pCodecCtx->height = HEIGHT; pCodecCtx->time_base.num = 1; pCodecCtx->time_base.den = FPS; pCodecCtx->gop_size = 10; pCodecCtx->max_b_frames = 1; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; if (pCodec->id == AV_CODEC_ID_H264) { av_opt_set(pCodecCtx->priv_data, "preset", "slow", 0); } if (avcodec_open2(pCodecCtx, pCodec, &options) < 0) { cout << "Couldn't open codec." << endl; return -1; } av_dump_format(ofmt_ctx, 0, "rtsp://localhost:8554/live.sdp", 1); // 3. 初始化 AVIOContext avformat_alloc_output_context2(&ofmt_ctx, NULL, "rtsp", "rtsp://localhost:8554/live.sdp"); if (!ofmt_ctx) { cout << "Couldn't create output context." << endl; return -1; } pb = avio_alloc_context(NULL, 0, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL); if (!pb) { cout << "Couldn't create AVIOContext." << endl; return -1; } ofmt_ctx->pb = pb; // 4. 添加输出流 video_out = avformat_new_stream(ofmt_ctx, NULL); if (!video_out) { cout << "Couldn't create output stream." << endl; return -1; } codec_out = video_out->codec; codec_out->codec_id = pCodec->id; codec_out->codec_type = AVMEDIA_TYPE_VIDEO; codec_out->bit_rate = 400000; codec_out->width = WIDTH; codec_out->height = HEIGHT; codec_out->time_base.den = FPS; codec_out->time_base.num = 1; codec_out->pix_fmt = AV_PIX_FMT_YUV420P; if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { codec_out->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } // 5. 打开输出流 err = avio_open(&ofmt_ctx->pb, ofmt_ctx->url, AVIO_FLAG_WRITE); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Could not open output URL '%s'\n", ofmt_ctx->url); return -1; } err = avformat_write_header(ofmt_ctx, &options); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output URL\n"); return -1; } // 6. 开始编码和发送数据 picture = av_frame_alloc(); size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); picture_buf = (uint8_t*)av_malloc(size); avpicture_fill((AVPicture*)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); av_new_packet(&pkt, size); while (true) { if (av_read_frame(pFormatCtx, &pkt) >= 0) { if (pkt.stream_index == 0) { // 解码 avcodec_decode_video2(pCodecCtx, picture, &got_picture, &pkt); if (got_picture) { // 编码 picture->pts = av_rescale_q(framecnt, pCodecCtx->time_base, video_st->time_base); ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture); if (ret >= 0 && got_picture) { pkt.stream_index = video_st->index; av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base); pkt.pos = -1; err = av_interleaved_write_frame(ofmt_ctx, &pkt); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error muxing packet\n"); break; } cout << "Write frame " << framecnt << endl; framecnt++; } av_packet_unref(&pkt); } } } else { av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_FRAME); } this_thread::sleep_for(chrono::milliseconds(40)); } av_write_trailer(ofmt_ctx); avcodec_close(pCodecCtx); av_free(pCodecCtx); av_free(picture_buf); av_free(picture); avio_close(ofmt_ctx->pb); avformat_free_context(ofmt_ctx); avformat_close_input(&pFormatCtx); return 0; } ``` 在上面的代码中,我们使用 FFmpeg 库打开摄像头,并初始化了一个 RTSP 服务器,将摄像头采集到的视频编码后发送给客户端。 在代码中,我们首先打开摄像头,然后初始化编码器。接下来,我们初始化 AVIOContext,添加输出流并打开输出流。最后,我们开始编码和发送数据。 编译代码时需要链接 FFmpeg 库: ``` g++ -o rtsp_server rtsp_server.cpp -lavdevice -lavformat -lavcodec -lavutil -lswscale -lpthread ``` 启动代码后,可以使用 VLC 等 RTSP 客户端访问 RTSP 服务器: ``` vlc rtsp://localhost:8554/live.sdp ``` 这样就可以在客户端上观看摄像头采集到的视频了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Neil_baby

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值