[Asterisk-code-review] res_format_attr_h264: Allow level to be downgraded. (asterisk[18])

Alexander Traud asteriskteam at digium.com
Tue Feb 9 05:30:54 CST 2021


Alexander Traud has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15421 )


Change subject: res_format_attr_h264: Allow level to be downgraded.
......................................................................

res_format_attr_h264: Allow level to be downgraded.

ASTERISK-29285

Change-Id: I29a4e1474b0ae1d56ebaa2fe4c9e603fd39c31c6
---
M res/res_format_attr_h264.c
1 file changed, 45 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/15421/1

diff --git a/res/res_format_attr_h264.c b/res/res_format_attr_h264.c
index c1b538e..209be32 100644
--- a/res/res_format_attr_h264.c
+++ b/res/res_format_attr_h264.c
@@ -35,6 +35,8 @@
 
 #include "asterisk/module.h"
 #include "asterisk/format.h"
+#include "asterisk/strings.h"           /* for ast_str_append, etc */
+#include "asterisk/utils.h"             /* for MAX, ast_calloc, ast_free, etc */
 
 /*! \brief Value that indicates an attribute is actually unset */
 #define H264_ATTR_KEY_UNSET UINT8_MAX
@@ -48,6 +50,10 @@
  *   H264_MAX_SPS_PPS_SIZE */
 #define H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "15"
 
+#define H264_DEFAULT_PROFILE_IDC 66  /* 42 */
+#define H264_DEFAULT_PROFILE_IOP 160 /* a0 */
+#define H264_MIN_LEVEL 10            /* 0a */
+
 struct h264_attr {
 	unsigned int PROFILE_IDC;
 	unsigned int PROFILE_IOP;
@@ -129,7 +135,6 @@
 
 	DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IDC);
 	DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IOP);
-	DETERMINE_JOINT(attr, attr1, attr2, LEVEL);
 	DETERMINE_JOINT(attr, attr1, attr2, MAX_MBPS);
 	DETERMINE_JOINT(attr, attr1, attr2, MAX_FS);
 	DETERMINE_JOINT(attr, attr1, attr2, MAX_CPB);
@@ -148,6 +153,35 @@
 	DETERMINE_JOINT(attr, attr1, attr2, LEVEL_ASYMMETRY_ALLOWED);
 	DETERMINE_JOINT(attr, attr1, attr2, PACKETIZATION_MODE);
 
+	/* https://mailarchive.ietf.org/arch/msg/avt/o-q0iDkaZXZRg1IQUeD67pd4kAo/ */
+	if (!attr->LEVEL_ASYMMETRY_ALLOWED) {
+		if (attr1 && attr2) {
+			/* Level 1b is 0x09 sometimes, see RFC 6184 section 8.2.2:
+			 * Level 1b is one level higher than Level 1.0 */
+			if (9 == attr1->LEVEL ||  9 == attr2->LEVEL) {
+				if (!attr1->LEVEL || 10 == attr1->LEVEL ||
+				    !attr2->LEVEL || 10 == attr2->LEVEL) {
+					attr->LEVEL = 10; /* minimum is Level 1.0 */
+				} else {
+					attr->LEVEL =  9; /* minimum is Level 1b */
+				}
+			/* otherwise minimum level is 1.0; this is forced here again
+			 * because cached formats do not have that as default but 0 */
+			} else {
+				attr->LEVEL = MIN(MAX(attr1->LEVEL,H264_MIN_LEVEL),
+					          MAX(attr2->LEVEL,H264_MIN_LEVEL));
+			}
+			attr1->LEVEL = attr->LEVEL;
+			attr2->LEVEL = attr->LEVEL;
+		} else if (attr1) {
+			attr->LEVEL = attr1->LEVEL;
+		} else if (attr2) {
+			attr->LEVEL = attr2->LEVEL;
+		} else {
+			attr->LEVEL = H264_MIN_LEVEL;
+		}
+	}
+
 	if (attr1 && !ast_strlen_zero(attr1->SPS)) {
 		ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
 	} else if (attr2 && !ast_strlen_zero(attr2->SPS)) {
@@ -178,7 +212,10 @@
 	attr->REDUNDANT_PIC_CAP = H264_ATTR_KEY_UNSET;
 	attr->PARAMETER_ADD = H264_ATTR_KEY_UNSET;
 	attr->PACKETIZATION_MODE = H264_ATTR_KEY_UNSET;
-	attr->LEVEL_ASYMMETRY_ALLOWED = H264_ATTR_KEY_UNSET;
+
+	attr->PROFILE_IDC = H264_DEFAULT_PROFILE_IDC;
+	attr->PROFILE_IOP = H264_DEFAULT_PROFILE_IOP;
+	attr->LEVEL = H264_MIN_LEVEL;
 
 	while ((attrib = strsep(&attribs, ";"))) {
 		unsigned int val;
@@ -280,9 +317,13 @@
 	APPEND_IF_NOT_H264_UNSET(attr->REDUNDANT_PIC_CAP, str, "redundant-pic-cap");
 	APPEND_IF_NOT_H264_UNSET(attr->PARAMETER_ADD, str, "parameter-add");
 	APPEND_IF_NOT_H264_UNSET(attr->PACKETIZATION_MODE, str, "packetization-mode");
-	APPEND_IF_NOT_H264_UNSET(attr->LEVEL_ASYMMETRY_ALLOWED, str, "level-asymmetry-allowed");
 
-	if (attr->PROFILE_IDC && attr->LEVEL) {
+	APPEND_IF_NONZERO(attr->LEVEL_ASYMMETRY_ALLOWED, str, "level-asymmetry-allowed");
+
+	/* cached formats have not the defaults but 0 */
+	if ((attr->PROFILE_IDC && H264_DEFAULT_PROFILE_IDC != attr->PROFILE_IDC) ||
+	    (attr->PROFILE_IOP && H264_DEFAULT_PROFILE_IOP != attr->PROFILE_IOP) ||
+	    (attr->LEVEL && H264_MIN_LEVEL != attr->LEVEL)) { /* could be 0x09 for Level 1b */
 		if (added) {
 			ast_str_append(str, 0, ";");
 		} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15421
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 18
Gerrit-Change-Id: I29a4e1474b0ae1d56ebaa2fe4c9e603fd39c31c6
Gerrit-Change-Number: 15421
Gerrit-PatchSet: 1
Gerrit-Owner: Alexander Traud <pabstraud at compuserve.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210209/40fe6bae/attachment-0001.html>


More information about the asterisk-code-review mailing list