IPB帧及PTS、D'T'S

本文详细解释了视频编码中的I帧、P帧和B帧的概念及其作用,并介绍了PTS和DTS的区别,通过具体例子展示了不同帧之间的依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I frame :帧内编码帧 又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象。I帧可以看成是一个图像经过压缩后的产物。

P frame: 前向预测编码帧 又称predictive-frame,通过充分将低于图像序列中前面已编码帧的时间冗余信息来压缩传输数据量的编码图像,也叫预测帧;

B frame: 双向预测内插编码帧 又称bi-directional interpolated prediction frame,既考虑与源图像序列前面已编码帧,也顾及源图像序列后面已编码帧之间的时间冗余信息来压缩传输数据量的编码图像,也叫双向预测帧;

PTS:Presentation Time Stamp。PTS主要用于度量解码后的视频帧什么时候被显示出来

DTS:Decode Time Stamp。DTS主要是标识读入内存中的bit流在什么时候开始送入解码器中进行解码。

在没有B帧存在的情况下DTS的顺序和PTS的顺序应该是一样的。

IPB帧的不同:

I frame:自身可以通过视频解压算法解压成一张单独的完整的图片。

P frame:需要参考其前面的一个I frame 或者B frame来生成一张完整的图片。

B frame:则要参考其前一个I或者P帧及其后面的一个P帧来生成一张完整的图片。

两个I frame之间形成一个GOP,在x264中同时可以通过参数来设定bf的大小,即:I 和p或者两个P之间B的数量。

通过上述基本可以说明如果有B frame 存在的情况下一个GOP的最后一个frame一定是P.

DTS和PTS的不同:

DTS主要用于视频的解码,在解码阶段使用.PTS主要用于视频的同步和输出.在display的时候使用.在没有B frame的情况下.DTS和PTS的输出顺序是一样的.

例子:

下面给出一个GOP为15的例子,其解码的参照frame及其解码的顺序都在里面:

ibpdtspts

如上图:I frame 的解码不依赖于任何的其它的帧.而p frame的解码则依赖于其前面的I frame或者P frame.B frame的解码则依赖于其前的最近的一个I frame或者P frame 及其后的最近的一个P frame.

/* * SPDX-License-Identifier: MIT * * Copyright (C) 2013-2024 OpenMV, LLC. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * Template matching with NCC (Normalized Cross Correlation) using exhaustive and diamond search. * * References: * Briechle, Kai, and Uwe D. Hanebeck. "Template matching using fast normalized cross correlation." Aerospace * Lewis, J. P. "Fast normalized cross-correlation." * Zhu, Shan, and Kai-Kuang Ma. "A new diamond search algorithm for fast block-matching motion estimation." */ #include <stdio.h> #include <float.h> #include <limits.h> #include "imlib.h" static void set_dsp(int cx, int cy, point_t *pts, bool sdsp, int step) { if (sdsp) { // Small DSP // 4 // 3 0 1 // 2 pts[0].x = cx; pts[0].y = cy; pts[1].x = cx + step / 2; pts[1].y = cy; pts[2].x = cx; pts[2].y = cy + step / 2; pts[3].x = cx - step / 2; pts[3].y = cy; pts[4].x = cx; pts[4].y = cy - step / 2; } else { // Large DSP // 7 // 6 8 // 5 0 1 // 4 2 // 3 pts[0].x = cx; pts[0].y = cy; pts[1].x = cx + step; pts[1].y = cy; pts[2].x = cx + step / 2; pts[2].y = cy + step / 2; pts[3].x = cx; pts[3].y = cy + step; pts[4].x = cx - step / 2; pts[4].y = cy + step / 2; pts[5].x = cx - step; pts[5].y = cy; pts[6].x = cx - step / 2; pts[6].y = cy - step / 2; pts[7].x = cx; pts[7].y = cy - step; pts[8].x = cx + step / 2; pts[8].y = cy - step / 2; } } static float find_block_ncc(image_t *f, image_t *t, i_image_t *sum, int t_mean, uint32_t t_sumsq, int u, int v) { int w = t->w; int h = t->h; int num = 0; uint32_t f_sumsq = 0; if (u < 0) { u = 0; } if (v < 0) { v = 0; } if (u + w >= f->w) { w = f->w - u; } if (v + h >= f->h) { h = f->h - v; } // Find the mean of the current patch uint32_t f_sum = imlib_integral_lookup(sum, u, v, w, h); uint32_t f_mean = f_sum / (w * h); // Find the normalized sum of squares of the image for (int y = v; y < v + h; y++) { for (int x = u; x < u + w; x++) { int a = (int) f->data[y * f->w + x] - f_mean; int b = (int) t->data[(y - v) * t->w + (x - u)] - t_mean; num += a * b; f_sumsq += a * a; } } // Find the normalized cross-correlation return (num / (fast_sqrtf(f_sumsq) * fast_sqrtf(t_sumsq))); } float imlib_template_match_ds(image_t *f, image_t *t, rectangle_t *r) { point_t pts[9]; // Integral images i_image_t sum; imlib_integral_image_alloc(&sum, f->w, f->h); imlib_integral_image(f, &sum); // Normalized sum of squares of the template int t_mean = 0; uint32_t t_sumsq = 0; imlib_image_mean(t, &t_mean, &t_mean, &t_mean); for (int i = 0; i < (t->w * t->h); i++) { int c = (int) t->data[i] - t_mean; t_sumsq += c * c; } int px = 0; int py = 0; // Initial center point int cx = f->w / 2 - t->w / 2; int cy = f->h / 2 - t->h / 2; // Max cross-correlation float max_xc = -FLT_MAX; // Start with the Large Diamond Search Pattern (LDSP) 9 points. bool sdsp = false; // Step size == template width int step = t->w; while (step > 0) { // Set the Diamond Search Pattern (DSP). set_dsp(cx, cy, pts, sdsp, step); // Set the number of search blocks (5 or 9 for SDSP and LDSP respectively). int num_pts = (sdsp == true)? 5: 9; // Find the block with the highest NCC for (int i = 0; i < num_pts; i++) { if (pts[i].x >= f->w || pts[i].y >= f->h) { continue; } float blk_xc = find_block_ncc(f, t, &sum, t_mean, t_sumsq, pts[i].x, pts[i].y); if (blk_xc > max_xc) { px = pts[i].x; py = pts[i].y; max_xc = blk_xc; } } // If the highest correlation is found at the center block and search is using // LDSP then the highest correlation is found, if not then switch search to SDSP. if (px == cx && py == cy) { // Note instead of switching to the smaller pattern, the step size can be reduced // each time the highest correlation is found at the center, and break on step == 0. // This makes DS much more accurate, but slower. step--; } // Set the new search center to the block with highest correlation cx = px; cy = py; } r->x = cx; r->y = cy; r->w = t->w; r->h = t->h; if (cx < 0) { r->x = 0; } if (cy < 0) { r->y = 0; } if (cx + t->w > f->w) { r->w = f->w - cx; } if (cy + t->h > f->h) { r->h = f->h - cy; } imlib_integral_image_free(&sum); //printf("max xc: %f\n", (double) max_xc); return max_xc; } /* The NCC can be optimized using integral images and rectangular basis functions. * See Kai Briechle's paper "Template Matching using Fast Normalized Cross Correlation". * * NOTE: only the denominator is optimized. * */ float imlib_template_match_ex(image_t *f, image_t *t, rectangle_t *roi, int step, rectangle_t *r) { int den_b = 0; float corr = 0.0f; // Integral images i_image_t sum; i_image_t sumsq; imlib_integral_image_alloc(&sum, f->w, f->h); imlib_integral_image_alloc(&sumsq, f->w, f->h); imlib_integral_image(f, &sum); imlib_integral_image_sq(f, &sumsq); // Normalized sum of squares of the template int t_mean = 0; imlib_image_mean(t, &t_mean, &t_mean, &t_mean); for (int i = 0; i < (t->w * t->h); i++) { int c = (int) t->data[i] - t_mean; den_b += c * c; } for (int v = roi->y; v <= (roi->y + roi->h - t->h); v += step) { for (int u = roi->x; u <= (roi->x + roi->w - t->w); u += step) { int num = 0; // The mean of the current patch uint32_t f_sum = imlib_integral_lookup(&sum, u, v, t->w, t->h); uint32_t f_sumsq = imlib_integral_lookup(&sumsq, u, v, t->w, t->h); uint32_t f_mean = f_sum / (float) (t->w * t->h); // Normalized sum of squares of the image for (int y = v; y < (v + t->h); y++) { for (int x = u; x < (u + t->w); x++) { int a = (int) f->data[y * f->w + x] - f_mean; int b = (int) t->data[(y - v) * t->w + (x - u)] - t_mean; num += a * b; } } uint32_t den_a = f_sumsq - f_sum * (f_sum / (float) (t->w * t->h)); // Find normalized cross-correlation float c = num / (fast_sqrtf(den_a) * fast_sqrtf(den_b)); if (c > corr) { corr = c; r->x = u; r->y = v; r->w = t->w; r->h = t->h; } } } imlib_integral_image_free(&sum); imlib_integral_image_free(&sumsq); return corr; }帮我找到我想要的函数
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值