PTS,DTS,CTS和I,P,B帧的关系

本文详细解析了I帧、P帧和B帧的工作原理及其依赖关系,同时介绍了DTS和PTS的区别及应用场景,帮助读者深入理解音视频编解码的基础知识。

今天终于弄懂以上几个关系。

I frame 的解码不依赖于任何的其它的帧.

p frame的解码则依赖于其前面的I frame或者P frame.

B frame的解码则依赖于其前的最近的一个I frame或者P frame 及其后的最近的一个P frame.

解码顺序:

I P B B B P B B B ...
显示顺序
I B B B P B B B P ... 

DTS和PTS的不同:

DTS主要用于视频的解码,在解码阶段使用.

PTS主要用于视频的同步和输出.在display的时候使用.

在没有B frame的情况下.DTS和PTS的输出顺序是一样的.(这点很重要)

compositionTime = (PTS - DTS) / 90.0 

AVCVIDEOPACKET

CompositionTime SI24 if AVCPacketType == 1
Composition time offset    

    else

0

由于我做的是直播流,为了减少延时,没有使用B帧。故CompositionTime为00 00 00.

特此记录

而x264.h里面
x264_picture_t两个成员

    int64_t i_pts;
    /* Out: frame dts. When the pts of the first frame is close to zero,
     *      initial frames may have a negative dts which must be dealt with by any muxer */
    int64_t i_dts;
    /* In: custom encoding parameters to be set from this frame forwards
           (in coded order, not display order). If NULL, continue using
           parameters from the previous frame.  Some parameters, such as
           aspect ratio, can only be changed per-GOP due to the limitations
           of H.264 itself; in this case, the caller must force an IDR frame
           if it needs the changed parameter to apply immediately. */

解码顺序:

I P B B B P B B B ...
显示顺序
I B B B P B B B P ... 

### 正确配置视频编码器中的DTSPTS 在处理视频流时,正确设置解码时间戳(DTS)显示时间戳(PTS)对于确保媒体文件能够被准确无误地回放至关重要。H264编码标准定义了两种不同类型的时间戳:一种用于指示何时应该对特定进行解码操作;另一种则决定了该应在屏幕上呈现的具体时刻。 #### DTSPTS关系 当不存在B(双向预测)时,由于每桢图像仅依赖于前一的数据来进行压缩,因此其解码顺序同展示顺序一致,此时DTS等于PTS[^3]。然而,在引入了B之后情况变得复杂起来——因为这些不仅基于前面已经看到的画面也考虑到了未来即将出现的内容来做优化,这就导致了解码次序不同于实际观看过程中各之间应有的先后关系。为了适应这种变化: - **I** (关键): 总是具有最早的DTS值,并且它的PTS通常紧随其后的某个位置; - **P** (前向预测): 它们的DTS位于所参考的关键之后但在任何后续的B之前; - **B**: 需要在两个相邻的关键或P间完成解码工作,尽管它们可能较早出现在比特流中,但直到所有必要的参照数据都可用以后才会真正展现给观众。 #### 设置方法 针对不同的开发环境技术栈,具体实现方式会有所差异。下面提供了一个通用指导原则以及一段Python伪代码示例来说明如何手动调整这两者之间的关联性。 ##### Python伪代码实例 ```python from datetime import timedelta class Frame: def __init__(self, type_, duration_ms=0): self.type_ = type_ self.duration_ms = duration_ms def set_timestamps(frames_list): current_time = 0 for i, frame in enumerate(frames_list): if frame.type_.upper() == 'I': # I frames have equal DTS and PTS. dts = pts = current_time elif frame.type_.upper() == 'P': # P frames' DTS is after the previous reference frame's PTS, # but before its own PTS which follows immediately afterwards. prev_frame_pts = get_previous_reference_frame(i).pts dts = max(prev_frame_pts + 1, current_time) pts = dts else: # Assuming it’s a B-frame here. next_ref_frames = find_next_two_reference_frames(i) earliest_possible_dts = min([f.dts for f in next_ref_frames]) latest_allowed_pts = max([f.pts for f in next_ref_frames]) - 1 dts = earliest_possible_dts pts = latest_allowed_pts yield {'index': i, 'type': frame.type_, 'dts': dts, 'pts': pts} # Update time based on this frame's duration. current_time += frame.duration_ms # Helper functions to simulate finding references. def get_previous_reference_frame(index): pass # Implement logic to retrieve last key or predicted frame. def find_next_two_reference_frames(index): pass # Implement logic to locate upcoming two refs. ``` 此段代码展示了怎样遍历一系列由不同类型的组成的列表,并为其分配合理的DTS/PTS组合。需要注意的是这只是一个简化版本的实际应用场景可能会更加复杂尤其是涉及到多线程处理或是硬件加速等情况下的同步机制设计等问题上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值