@@ -50,7 +50,8 @@ public static String findDecoderByFormat(MediaFormat mediaFormat, boolean onlySo
5050 }
5151 String mimeType = mediaFormat .getString (MediaFormat .KEY_MIME );
5252 try {
53- MediaCodecInfo .CodecCapabilities capabilities = codecInfo .getCapabilitiesForType (mimeType );
53+ MediaCodecInfo .CodecCapabilities capabilities =
54+ codecInfo .getCapabilitiesForType (mimeType );
5455 if (capabilities .isFormatSupported (mediaFormat )) {
5556 return codecInfo .getName ();
5657 }
@@ -82,7 +83,8 @@ public static String findEncoderByFormat(MediaFormat mediaFormat, boolean onlySo
8283 }
8384 String mimeType = mediaFormat .getString (MediaFormat .KEY_MIME );
8485 try {
85- MediaCodecInfo .CodecCapabilities capabilities = codecInfo .getCapabilitiesForType (mimeType );
86+ MediaCodecInfo .CodecCapabilities capabilities =
87+ codecInfo .getCapabilitiesForType (mimeType );
8688 if (capabilities .isFormatSupported (mediaFormat )) {
8789 return codecInfo .getName ();
8890 }
@@ -142,14 +144,17 @@ public enum EGLColorSpace {
142144 }
143145
144146 @ NonNull
145- public static MediaFormat createOutputFormat (@ NonNull Context ctx , @ NonNull Uri srcUri , @ NonNull MediaFormat mOriVideoFormat , @ NonNull TranscodeConfig config , @ NonNull VideoOutputConfig outputConfig ) {
147+ public static MediaFormat createOutputFormat (@ NonNull Context ctx , @ NonNull Uri srcUri ,
148+ @ NonNull MediaFormat mOriVideoFormat ,
149+ @ NonNull TranscodeConfig config ,
150+ @ NonNull VideoOutputConfig outputConfig ) {
146151 MediaFormat outputFormat ;
147152 String inMimeType = mOriVideoFormat .getString (MediaFormat .KEY_MIME );
148153 boolean isH265 = false ;
149154 String mime ;
150155 if (config .h265 ) {
151156 isH265 = true ;
152- if (MediaFormat .MIMETYPE_VIDEO_DOLBY_VISION .equals (inMimeType ) && Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
157+ if (outputConfig . outputLevel != OutputLevel . NO_HDR && MediaFormat .MIMETYPE_VIDEO_DOLBY_VISION .equals (inMimeType ) && Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
153158 //如果是杜比视界,那么需要检查能否使用杜比视界的mimeType
154159 mime = MediaFormat .MIMETYPE_VIDEO_DOLBY_VISION ;
155160 outputConfig .isDolby = true ;
@@ -165,7 +170,8 @@ public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri
165170 if (codecName == null ) {
166171 //说明没有杜比视界的编码器,降级到Hevc去
167172 mime = MediaFormat .MIMETYPE_VIDEO_HEVC ;
168- outputFormat = MediaFormat .createVideoFormat (mime , config .outWidth , config .outHeight );
173+ outputFormat = MediaFormat .createVideoFormat (mime , config .outWidth ,
174+ config .outHeight );
169175 outputConfig .isDolby = false ;
170176 }
171177 }
@@ -190,51 +196,56 @@ public static MediaFormat createOutputFormat(@NonNull Context ctx, @NonNull Uri
190196 //设置Color相关参数,使其尽量保证HDR视频转码后仍然是HDR视频
191197 int colorTransfer = 0 ;
192198 int colorStandard = mOriVideoFormat .getInteger (MediaFormat .KEY_COLOR_STANDARD );
193- outputConfig .isHDR = colorStandard == MediaFormat .COLOR_STANDARD_BT2020 ;
199+ outputConfig .isHDR =
200+ outputConfig .outputLevel != OutputLevel .NO_HDR && colorStandard == MediaFormat .COLOR_STANDARD_BT2020 ;
194201 if (outputConfig .isHDR ) {
195202 outputConfig .isHDRVivid = MediaCodecUtils .isHDRVivid (ctx , null , srcUri , null );
196203 }
197- if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_STANDARD )) {
198- outputFormat .setInteger (MediaFormat .KEY_COLOR_STANDARD , colorStandard );
199- }
200- if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_TRANSFER )) {
201- colorTransfer = mOriVideoFormat .getInteger (MediaFormat .KEY_COLOR_TRANSFER );
202- outputFormat .setInteger (MediaFormat .KEY_COLOR_TRANSFER , colorTransfer );
203- }
204- if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_RANGE )) {
205- outputFormat .setInteger (MediaFormat .KEY_COLOR_RANGE ,
206- mOriVideoFormat .getInteger (MediaFormat .KEY_COLOR_RANGE ));
207- }
208- if (mOriVideoFormat .containsKey (MediaFormat .KEY_HDR_STATIC_INFO )) {
209- outputFormat .setByteBuffer (MediaFormat .KEY_HDR_STATIC_INFO ,
210- mOriVideoFormat .getByteBuffer (MediaFormat .KEY_HDR_STATIC_INFO ));
211- }
212- if (outputConfig .isDolby ) {
213- //如果是杜比
214- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O_MR1 ) {
215- outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
216- MediaCodecInfo .CodecProfileLevel .DolbyVisionProfileDvheSt );
217- } else {
218- outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
219- MediaCodecInfo .CodecProfileLevel .DolbyVisionProfileDvheStn );
204+ if (outputConfig .isHDR ) {
205+ if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_STANDARD )) {
206+ outputFormat .setInteger (MediaFormat .KEY_COLOR_STANDARD , colorStandard );
220207 }
221- } else {
222- outputFormat .setFeatureEnabled ("hdr-editing" , true );
223- switch (colorTransfer ) {
224- case MediaFormat .COLOR_TRANSFER_HLG :
225- //HLG(HGL10)
226- outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
227- MediaCodecInfo .CodecProfileLevel .HEVCProfileMain10 );
228- break ;
229- case MediaFormat .COLOR_TRANSFER_ST2084 :
230- //PQ(HDR10和HDR10+)
231- //TODO 怎么区分HDR10和HDR10+ HEVCProfileMain10HDR10Plus
232- outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
233- MediaCodecInfo .CodecProfileLevel .HEVCProfileMain10HDR10 );
234- //TODO 可能还是要降级成HEVCProfileMain10
235- break ;
236- default :
237- break ;
208+ if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_TRANSFER )) {
209+ colorTransfer = mOriVideoFormat .getInteger (MediaFormat .KEY_COLOR_TRANSFER );
210+ outputFormat .setInteger (MediaFormat .KEY_COLOR_TRANSFER , colorTransfer );
211+ }
212+ if (mOriVideoFormat .containsKey (MediaFormat .KEY_COLOR_RANGE )) {
213+ outputFormat .setInteger (MediaFormat .KEY_COLOR_RANGE ,
214+ mOriVideoFormat .getInteger (MediaFormat .KEY_COLOR_RANGE ));
215+ }
216+ if (mOriVideoFormat .containsKey (MediaFormat .KEY_HDR_STATIC_INFO )) {
217+ outputFormat .setByteBuffer (MediaFormat .KEY_HDR_STATIC_INFO ,
218+ mOriVideoFormat .getByteBuffer (MediaFormat .KEY_HDR_STATIC_INFO ));
219+ }
220+ if (outputConfig .outputLevel != OutputLevel .NO_PROFILE ) {
221+ if (outputConfig .isDolby ) {
222+ //如果是杜比
223+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O_MR1 ) {
224+ outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
225+ MediaCodecInfo .CodecProfileLevel .DolbyVisionProfileDvheSt );
226+ } else {
227+ outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
228+ MediaCodecInfo .CodecProfileLevel .DolbyVisionProfileDvheStn );
229+ }
230+ } else {
231+ outputFormat .setFeatureEnabled ("hdr-editing" , true );
232+ switch (colorTransfer ) {
233+ case MediaFormat .COLOR_TRANSFER_HLG :
234+ //HLG(HGL10)
235+ outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
236+ MediaCodecInfo .CodecProfileLevel .HEVCProfileMain10 );
237+ break ;
238+ case MediaFormat .COLOR_TRANSFER_ST2084 :
239+ //PQ(HDR10和HDR10+)
240+ //TODO 怎么区分HDR10和HDR10+ HEVCProfileMain10HDR10Plus
241+ outputFormat .setInteger (MediaFormat .KEY_PROFILE ,
242+ MediaCodecInfo .CodecProfileLevel .HEVCProfileMain10HDR10 );
243+ //TODO 可能还是要降级成HEVCProfileMain10
244+ break ;
245+ default :
246+ break ;
247+ }
248+ }
238249 }
239250 }
240251 }
0 commit comments