gpt4 book ai didi

c - DTS 和 PTS 是不同的,尽管没有 B 帧 (ffmpeg)

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:24 25 4
gpt4 key购买 nike

关于使用 ffmpeg 对视频文件进行视频编码/复用的简单问题。基本上,我的复用器正在运行,我正在尝试让我的数据包输出正确的 PTS/DTS。

这是我编码我的 AVFrame 的代码的一部分,将它混合到输出文件中:

int ret;
int got_packet = 0;

AVPacket pkt = { 0 };
av_init_packet(&pkt);

pkt.data = NULL;
pkt.size = 0;

/* encode the image */
ret = avcodec_encode_video2(cc, &pkt, frame, &got_packet);
if (ret < 0)
{
fprintf(stderr, "error encoding video frame: %s\n", av_err2str(ret));
exit(EXIT_FAILURE);
}

if (got_packet)
{
av_packet_rescale_ts(&pkt, cc->time_base, st->time_base);

fprintf(stderr, "\npkt.pts = %ld\n", pkt.pts);
fprintf(stderr, "pkt.dts = %ld\n", pkt.dts);
fprintf(stderr, "writing frame\n");

ret = av_interleaved_write_frame(fmt_ctx, &pkt);
av_packet_unref(&pkt);
}
else
{
ret = 0;
}

...

然后我得到以下输出:

pkt.pts     = 0
pkt.dts = 0
writing frame

pkt.pts = 1502
pkt.dts = 0
writing frame

pkt.pts = 3003
pkt.dts = 1502
writing frame

pkt.pts = 4505
pkt.dts = 3003
writing frame

...

我的目标是让我的 PST 和 DST 都具有以下模式:1502、3003、4505、6006、7508,...

但似乎第一个 DTS 值重复了一次,因此与其对应的 PTS 值不同步。还值得一提的是,编解码器上下文配置为没有 b 帧,因此此处仅存在 i 帧和 p 帧。

有没有经验丰富的人对此有一些见解?

添加:

我在终端中运行了以下命令来检查我的 DTS 和 PTS 值是否与我的打印语句一致:

sudo ./ffprobe -show_packets -print_format json mux_test.ts | less

我得到了以下信息:

{
"packets": [
{
"codec_type": "video",
"stream_index": 0,
"pts": 0,
"pts_time": "0.000000",
"dts": -1501,
"dts_time": "-0.016678",
"duration": 1501,
"duration_time": "0.016678",
"convergence_duration": "N/A",
"convergence_duration_time": "N/A",
"size": "55409",
"pos": "564",
"flags": "K"
},
{
"codec_type": "video",
"stream_index": 0,
"pts": 1502,
"pts_time": "0.016689",
"dts": 0,
"dts_time": "0.000000",
"duration": 1501,
"duration_time": "0.016678",
"convergence_duration": "N/A",
"convergence_duration_time": "N/A",
"size": "46574",
"pos": "60160",
"flags": "_"
},
{
"codec_type": "video",
"stream_index": 0,
"pts": 3003,
"pts_time": "0.033367",
"dts": 1502,
"dts_time": "0.016689",
"duration": 1501,
"duration_time": "0.016678",
"convergence_duration": "N/A",
"convergence_duration_time": "N/A",
"size": "2544",
"pos": "110356",
"flags": "_"
},

...

它不会重复显示我的第一个 DTS 值,但会在我的 PTS 之后继续显示我的 DTS 一个周期。

最佳答案

调试API后,我终于得出结论。

要使 DTS 值有效,它们必须以单调、一致的速率增加(假设时基和帧速率在混合过程中没有以某种方式改变),因此值本身并不那么重要。

此代码块直接来自 ffmpeg 库。它位于 mpegvideo_enc.c2074-2080 行(为清楚起见重新格式化):

...

pkt->pts = s->current_picture.f->pts;
if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B)
{
if (!s->current_picture.f->coded_picture_number)
{
pkt->dts = pkt->pts - s->dts_delta;
}
else
{
pkt->dts = s->reordered_pts;
}
s->reordered_pts = pkt->pts;

...

如您所见,只有第一帧会进入 if (!s->current_picture.f->coded_picture_number) 语句,coded_picture_number 值为 0 . 后续每一帧都会进入else语句,设置当前DTS等于之前的PTS值。

因此,对于使用 MPEG-2 编码器的混合情况,这种行为似乎是正常的。 DTS 应该落后“1-cycle”。

关于c - DTS 和 PTS 是不同的,尽管没有 B 帧 (ffmpeg),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47477483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com