当前位置: 首页 > news >正文

Android MediaCodec设置H264 Profile到High

     H264 High Profile压缩率高,能降低码率,这里记录下MediaCodec Profile设置到High遇到的一些问题。

     Android 4.1 就引入了MediaCodecInfo.CodecProfileLevel类,下面截取H264(AVC)的Profile和Level定义:

/** Copyright (C) 2012 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.media;public final class MediaCodecInfo {/*** Encapsulates the profiles available for a codec component.*/public static final class CodecProfileLevel {// These constants were originally in-line with OMX values, but this// correspondence is no longer maintained.// Profiles and levels for AVC Codec, corresponding to the definitions in// "SERIES H: AUDIOVISUAL AND MULTIMEDIA SYSTEMS,// Infrastructure of audiovisual services – Coding of moving video// Advanced video coding for generic audiovisual services"// found at// https://www.itu.int/rec/T-REC-H.264-201704-I/*** AVC Baseline profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileBaseline = 0x01;/*** AVC Main profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileMain     = 0x02;/*** AVC Extended profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileExtended = 0x04;/*** AVC High profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileHigh     = 0x08;/*** AVC High 10 profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileHigh10   = 0x10;/*** AVC High 4:2:2 profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileHigh422  = 0x20;/*** AVC High 4:4:4 profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileHigh444  = 0x40;/*** AVC Constrained Baseline profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileConstrainedBaseline = 0x10000;/*** AVC Constrained High profile.* See definition in* <a href="https://www.itu.int/rec/T-REC-H.264-201704-I">H.264 recommendation</a>,* Annex A.*/public static final int AVCProfileConstrainedHigh     = 0x80000;public static final int AVCLevel1       = 0x01;public static final int AVCLevel1b      = 0x02;public static final int AVCLevel11      = 0x04;public static final int AVCLevel12      = 0x08;public static final int AVCLevel13      = 0x10;public static final int AVCLevel2       = 0x20;public static final int AVCLevel21      = 0x40;public static final int AVCLevel22      = 0x80;public static final int AVCLevel3       = 0x100;public static final int AVCLevel31      = 0x200;public static final int AVCLevel32      = 0x400;public static final int AVCLevel4       = 0x800;public static final int AVCLevel41      = 0x1000;public static final int AVCLevel42      = 0x2000;public static final int AVCLevel5       = 0x4000;public static final int AVCLevel51      = 0x8000;public static final int AVCLevel52      = 0x10000;public static final int AVCLevel6       = 0x20000;public static final int AVCLevel61      = 0x40000;public static final int AVCLevel62      = 0x80000;}
}

     其中AVCProfileConstrainedBaseline 和 AVCProfileConstrainedHigh 是安卓8.1 引入的; AVCLevel52 是安卓5.0 引入的; AVCLevel6, AVCLevel61, AVCLevel62 是安卓10 引入的;

     AVC High profile 和 AVC Constrained High profile的区别: 1. 从名字就可以看出Constrained High是High的子集;  2. Constrained High 不支持B slice(B帧); 3.  Constrained High 只支持帧编码,不支持场编码和帧场自适应编码; 

    另外Level也要选择合适的,  GB28181-2016要求本地录像帧率不低于25fps, 网络传输时重要图像帧率宜25fps. GB28181-2022要求一类视频监控点、二类视频监控点的本地录像和网络传输的最大视频帧率应不低于25fps. 这里以视频30fps为例, 安卓摄像头常用的几个分辨率最小支持的Level如下:

分辨率最小支持的Level
640*4803
1280*720(720p)3.1
1920*1280(1080p)

4

3840*2160(4k)5.1

   虽然安卓5.0已引入android.media.MediaFormat.KEY_PROFILE, 安卓6.0 引入android.media.MediaFormat.KEY_LEVEL. 但安卓7.0及以上AVC Profile设置才有效, 并且要求Profile和Level必须一起设置.

   我的代码也提供了Mediacode编码参数设置接口, Java 和 NDK 两种MediaCodec接口方式都支持:

    // Copyright (C) 1130758427@qq.com/*** 设置视频硬编码是否使用Native Media NDK, 默认是不使用, 安卓5.0以下设备不支持* @param handle* @param is_native: 0表示不使用, 1表示使用, sdk默认是0.* @return {0} if successful*/public native int SetNativeMediaNDK(long handle, int is_native);/** 设置视频硬编码码率控制模式* @param hw_bitrate_mode: -1表示使用默认值,不设置也会使用默认值, 0:CQ, 1:VBR, 2:CBR, 3:CBR_FD, 请参考:android.media.MediaCodecInfo.EncoderCapabilities* 注意硬编码和手机硬件有关,多数手机只支持部分码率模式, 另外硬编码设备差异很大,不同设备同一码率控制模式效果可能不一样* @return {0} if successful*/public native int SetVideoHWEncoderBitrateMode(long handle, int hw_bitrate_mode);/** 设置视频硬编码质量, 安卓9及以上支持, 仅当硬编码器码率控制模式(BitrateMode)是CQ(constant-quality mode)时才有效* @param hw_quality: -1表示不设置, 请参考:android.media.MediaFormat.KEY_QUALITY* 注意硬编码和手机硬件有关,部分手机可能不支持此设置* @return {0} if successful*/public native int SetVideoHWEncoderQuality(long handle, int hw_quality);/** 设置H.264硬编码Profile, 安卓7及以上支持* @param hw_avc_profile: 0表示使用默认值, 0x01: Baseline, 0x02: Main, 0x08: High, 0x10000: ConstrainedBaseline, 0x80000: ConstrainedHigh;* 注意: ConstrainedBaseline 和 ConstrainedHigh 可能多数设备不支持,* H.264推荐使用 High 或者 ConstrainedHigh, 如果您使用的手机硬解码解不了,那还是设置Baseline* 如果设置的Profile硬编码器不支持,应编码器会使用默认值* 具体参考:android.media.MediaCodecInfo.CodecProfileLevel* @return {0} if successful*/public native int SetAVCHWEncoderProfile(long handle, int hw_avc_profile);/** 设置H.264硬编码Level, 这个只有在设置了Profile的情况下才有效, 安卓7及以上支持* @param hw_avc_level: 0表示使用默认值, 0x100: Level3, 0x200: Level3.1, 0x400: Level3.2,* 0x800: Level4, 0x1000: Level4.1, 0x2000: Level4.2,* 0x4000: Level5, 0x8000: Level5.1,  0x10000: Level5.2,* 0x20000: Level6, 0x40000: Level6.1,  0x80000: Level6.2,* 如果设置的level太高硬编码器不支持,SDK内部会做相应调整* 注意: 640*480@25fps最小支持的是Level3, 720p最小支持的是Level3.1, 1080p最小支持的是Level4* 具体参考:android.media.MediaCodecInfo.CodecProfileLevel* @return {0} if successful*/public native int SetAVCHWEncoderLevel(long handle, int hw_avc_level);/** 设置视频硬编码最大码率, 安卓没有相关文档说明, 所以不建议设置,* @param hw_max_bitrate: 每秒最大码率, 单位bps* @return {0} if successful*/public native int SetVideoHWEncoderMaxBitrate(long handle, long hw_max_bitrate);

  经测试MTK、 海思(鸿蒙系统)、 三星、全志、晶晨、瑞芯微等编码器AVC Profile设置都可以成功。

相关文章:

Android MediaCodec设置H264 Profile到High

H264 High Profile压缩率高&#xff0c;能降低码率&#xff0c;这里记录下MediaCodec Profile设置到High遇到的一些问题。 Android 4.1 就引入了MediaCodecInfo.CodecProfileLevel类&#xff0c;下面截取H264(AVC)的Profile和Level定义: /** Copyright (C) 2012 The Android O…...

QT之QSysInfo(查看电脑信息)

文章目录前言一、API使用总结前言 QSysInfo是Qt中用于获取有关运行应用程序的系统信息的类。 我们可以获取以下信息&#xff1a; 返回系统产品类型&#xff0c;如ios&#xff0c;windows&#xff0c;Linux等 返回当前系统的产品版本。 返回当前系统的内核类型。 返回当前系统的…...

中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年

中国塑料编织袋产业竞争状况及投资前景预测报告2023-2029年 KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK 《报告编号》: BG451639 《出版时间》: 2023年4月 《出版机构》: 中智正业研究院 免费售后 服务一年&#xff0c;具体内容及订购流程欢迎咨询客服人员 内容简介&…...

从头用脚分析FFmpeg源码 - av_read_frame

av_read_frame作用 /*** Return the next frame of a stream.* This function returns what is stored in the file, and does not validate* that what is there are valid frames for the decoder. It will split what is* stored in the file into frames and return one f…...

第17章_触发器

第17章_触发器 &#x1f3e0;个人主页&#xff1a;shark-Gao &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是shark-Gao&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f389;目前状况&#xff1a;23届毕业生&#xff0c;目前在某公…...

3956. 截断数组

3956. 截断数组 - AcWing题库 3956. 截断数组 【题目描述】 给定一个长度为 nn 的数组 a1,a2,…,ana1,a2,…,an。 现在&#xff0c;要将该数组从中间截断&#xff0c;得到三个非空子数组。 要求&#xff0c;三个子数组内各元素之和都相等。 请问&#xff0c;共有多少种不同…...

React Labs: 我们最近在做什么——2023 年 3 月

原文&#xff1a;https://react.dev/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023 React Server Components React Server Components(下文简称 RSC) 是由 React 团队设计的新应用程序架构。 我们首先在一个介绍性演讲和一个RFC中分享了我们对 RSC 的…...

文件系统设计详解

抽象的文件系统以目录的形式来组织文件&#xff0c;我们可以利用该文件系统来读取某个文件的内容&#xff0c;也可以对目录或者文件实施监控并及时获取变化的通知。 IChangeToken IChangeToken对象就是一个与某组监控数据相关联的“令牌”&#xff08;Token&#xff09;&#x…...

好看~立马启动python实现美女通通下

人生苦短&#xff0c;我用python一、环境版本使用二、代码实现思路三、代码展示&#xff1a;导入模块伪装(请求头)四、部分好看截图&#xff0c;更多的就自己去采集噜~吃饭放松的时候哇一不小心看见了很多好看的东西 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 独乐乐不如众乐乐&#xf…...

Git 安装设置

1、安装 安装以下三个软件&#xff1a; Git-2.13.3-64-bit.exe TortoiseGit-2.4.0.2-64bit.msi TortoiseGit-LanguagePack-2.4.0.0-64bit-zh_CN.msi 安装过程中不用填写、不用选择&#xff0c;全部点"下一步"&#xff0c;完成后需要重启机器。 2、基本设…...

Python-闭包

介绍 Python的闭包是一种高级的编程技巧&#xff0c;它可以在函数内部定义另一个函数&#xff0c;并返回该函数的引用。这个内部函数可以访问外部函数的变量和参数&#xff0c;即使外部函数已经执行完毕 好处 1&#xff09;闭包可以避免全局变量的污染&#xff0c;使得代码更…...

Gitlab中Pipeline语法四

Gitlab中Pipeline语法 cache cache:paths 在job build中定义缓存,将会缓存target目录下的所有*.jar文件当全局定义了cache:paths会被job中覆盖.以下实例将缓存target目录 buld:script: buildcache:paths:- target/*.jar#设置key可以解决cache被覆盖 cache:paths:- my/files…...

Go语言精修(尚硅谷笔记)第五章

五、程序流程控制 5.1 单分支控制 package main import ("fmt" )func main() {//请大家看个案例[ifDemo.go]://编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,则输出 "你年龄大//于18,要对自己的行为负责!"//分析 //1.年龄 > var age int…...

三、MySQL 高级(DML 增删改)

三、MySQL 高级&#xff08;DML 增删改&#xff09; 3.1 DML 数据操纵语言 DML&#xff08;Data Manipulation Language&#xff09;DML对数据库中表记录的执行操作 插入&#xff08;INSERT&#xff09; 插入单行数据 插入多行数据 将查询结果插入到新表 更新&#xff08…...

面向AI编程的本质是什么?

面向AI编程的本质是什么&#xff1f; 面向AI编程的本质是编程的第五代编程语言&#xff0c;与自然语言非常相似&#xff0c;但是是有区别的。 因此出现了针对与AI通话的提示工程。 简单地回顾一下编程语言的发展史&#xff0c; 第一代编程语言是机器语言&#xff0c;它直接使…...

深入浅出——深度学习训练中的warmup

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️&#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…...

你知道如何用C语言将格式化数据和字符串相互转换吗?

今天重点介绍2个函数&#xff0c;分别是sprintf和sscanf&#xff0c;用来将格式化数据和字符串相互转换。它们的作用分别是&#xff1a; sprintf函数用于将格式化数据转换成字符串。sscanf函数用于将字符串转换成格式化数据。 接下来是第一个大问题&#xff1a;我怎么记忆呢&…...

免费一键生成原创文章-原创文章批量生成

免费使用一键生成原创文章&#xff0c;轻松解决写作难题&#xff01; 您是否因为写作枯竭、文章档次不高&#xff0c;而感到烦恼&#xff1f;现在&#xff0c;我们有一个免费的文章创作工具&#xff0c;帮助您无需付出太多的努力就能高效地创造原创文章。 一键生成&#xff1…...

【数据库管理】④重做日志Redo Log

1. Redo log(重做日志)的功能 重做日志&#xff08;Redo log&#xff09;是数据库管理系统中的一种机制&#xff0c;主要作用包括&#xff1a; 提供事务的持久性支持&#xff1a;重做日志记录了每个事务对数据库所做的修改操作&#xff0c;以便在系统故障或崩溃时&#xff0c;通…...

5-python文件操作

文章目录1.打开文件2.文件读取3.文件关闭4.文件写入/追加1.打开文件 当传参顺序不一致时&#xff0c;不能使用位置传参&#xff0c;应使用关键字传参 open(file, mode‘r’, buffering-1, encodingNone, errorsNone, newlineNone, closefdTrue, openerNone) 通常使用&#xf…...

网络编程(Modbus进阶)

思维导图 Modbus RTU&#xff08;先学一点理论&#xff09; 概念 Modbus RTU 是工业自动化领域 最广泛应用的串行通信协议&#xff0c;由 Modicon 公司&#xff08;现施耐德电气&#xff09;于 1979 年推出。它以 高效率、强健性、易实现的特点成为工业控制系统的通信标准。 包…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

ServerTrust 并非唯一

NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

[Java恶补day16] 238.除自身以外数组的乘积

给你一个整数数组 nums&#xff0c;返回 数组 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法&#xff0c;且在 O(n) 时间复杂度…...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

CRMEB 中 PHP 短信扩展开发:涵盖一号通、阿里云、腾讯云、创蓝

目前已有一号通短信、阿里云短信、腾讯云短信扩展 扩展入口文件 文件目录 crmeb\services\sms\Sms.php 默认驱动类型为&#xff1a;一号通 namespace crmeb\services\sms;use crmeb\basic\BaseManager; use crmeb\services\AccessTokenServeService; use crmeb\services\sms\…...

Python Einops库:深度学习中的张量操作革命

Einops&#xff08;爱因斯坦操作库&#xff09;就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库&#xff0c;用类似自然语言的表达式替代了晦涩的API调用&#xff0c;彻底改变了深度学习工程…...

Linux安全加固:从攻防视角构建系统免疫

Linux安全加固:从攻防视角构建系统免疫 构建坚不可摧的数字堡垒 引言:攻防对抗的新纪元 在日益复杂的网络威胁环境中,Linux系统安全已从被动防御转向主动免疫。2023年全球网络安全报告显示,高级持续性威胁(APT)攻击同比增长65%,平均入侵停留时间缩短至48小时。本章将从…...