The byte-buffer output path only knew about 8-bit YUV420 and NV12, so
10-bit content decoded through MediaCodec could not be returned without
losing precision.

Map COLOR_FormatYUV420P010 to AV_PIX_FMT_P010, copy the two planes in
mediacodec_sw_buffer, and request the format when the HEVC SPS signals
more than 8 bits per sample. Surface output is unaffected, since the
codec converts it itself.

Also free the MediaCodecDecContext if ff_mediacodec_dec_init() fails
instead of leaking it.

Tested on a Samsung Galaxy Tab S9 (SM-X710, Snapdragon 8 Gen 2):
MediaCodec output is bit-exact against the software decoder for 10-bit
Dolby Vision profile 5 and 8-bit HEVC content.

Signed-off-by: Clemens Wasser <[email protected]>
---
 Changelog                         |  1 +
 libavcodec/mediacodec_sw_buffer.c | 47 +++++++++++++++++++++++++++++++
 libavcodec/mediacodec_sw_buffer.h |  7 +++++
 libavcodec/mediacodecdec.c        |  5 +++-
 libavcodec/mediacodecdec_common.c | 12 ++++++++
 5 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index bce79b181f..78ee766cfe 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 - LCEVC raw demuxer and muxer
 - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder
 - PCM PAMF decoder
+- mediacodec P010 (10-bit) byte-buffer output
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/libavcodec/mediacodec_sw_buffer.c 
b/libavcodec/mediacodec_sw_buffer.c
index 92428e85f0..e2fb6524f7 100644
--- a/libavcodec/mediacodec_sw_buffer.c
+++ b/libavcodec/mediacodec_sw_buffer.c
@@ -178,6 +178,53 @@ void 
ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(AVCodecContext *avctx,
 
 
 
+void ff_mediacodec_sw_buffer_copy_yuv420_p010(AVCodecContext *avctx,
+                                              MediaCodecDecContext *s,
+                                              uint8_t *data,
+                                              size_t size,
+                                              FFAMediaCodecBufferInfo *info,
+                                              AVFrame *frame)
+{
+    int i;
+    const int stride = s->stride; /* bytes per row, 2 per sample */
+    const int chroma_h = AV_CEIL_RSHIFT(avctx->height, 1);
+    const size_t need = (size_t)s->slice_height * stride +
+                        (size_t)chroma_h * stride;
+
+    if (info->offset < 0 || (size_t)info->offset + need > size) {
+        av_log(avctx, AV_LOG_ERROR, "P010 buffer too small\n");
+        return;
+    }
+
+    for (i = 0; i < 2; i++) {
+        int height;
+        int width = avctx->width * 2;
+        uint8_t *src = data + info->offset;
+        uint8_t *dst = frame->data[i];
+
+        if (i == 0) {
+            height = avctx->height;
+            src += s->crop_top * stride + s->crop_left * 2;
+        } else {
+            height = chroma_h;
+            src += s->slice_height * stride;
+            /* chroma is subsampled: crop_top is in luma rows */
+            src += (s->crop_top / 2) * stride + s->crop_left * 2;
+        }
+
+        if (frame->linesize[i] == stride) {
+            memcpy(dst, src, (size_t)height * stride);
+            continue;
+        }
+
+        for (int j = 0; j < height; j++) {
+            memcpy(dst, src, FFMIN(width, frame->linesize[i]));
+            src += stride;
+            dst += frame->linesize[i];
+        }
+    }
+}
+
 void ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(AVCodecContext 
*avctx,
                                                             
MediaCodecDecContext *s,
                                                             uint8_t *data,
diff --git a/libavcodec/mediacodec_sw_buffer.h 
b/libavcodec/mediacodec_sw_buffer.h
index 574fb529d4..1b7fc6d20a 100644
--- a/libavcodec/mediacodec_sw_buffer.h
+++ b/libavcodec/mediacodec_sw_buffer.h
@@ -59,4 +59,11 @@ void 
ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(AVCod
                                                      FFAMediaCodecBufferInfo 
*info,
                                                      AVFrame *frame);
 
+void ff_mediacodec_sw_buffer_copy_yuv420_p010(AVCodecContext *avctx,
+                                              MediaCodecDecContext *s,
+                                              uint8_t *data,
+                                              size_t size,
+                                              FFAMediaCodecBufferInfo *info,
+                                              AVFrame *frame);
+
 #endif /* AVCODEC_MEDIACODEC_SW_BUFFER_H */
diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c
index 0551370a54..781db2c032 100644
--- a/libavcodec/mediacodecdec.c
+++ b/libavcodec/mediacodecdec.c
@@ -240,6 +240,9 @@ static int hevc_set_extradata(AVCodecContext *avctx, 
FFAMediaFormat *format)
         }
     }
 
+    if (sps && sps->bit_depth > 8)
+        avctx->bits_per_raw_sample = sps->bit_depth;
+
     if (vps && pps && sps) {
         uint8_t *data;
         int data_size;
@@ -454,7 +457,7 @@ static av_cold int mediacodec_decode_init(AVCodecContext 
*avctx)
     s->ctx->use_ndk_codec = s->use_ndk_codec;
 
     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) 
{
-        s->ctx = NULL;
+        av_freep(&s->ctx);
         goto done;
     }
 
diff --git a/libavcodec/mediacodecdec_common.c 
b/libavcodec/mediacodecdec_common.c
index 4fe89982da..ffcab8b305 100644
--- a/libavcodec/mediacodecdec_common.c
+++ b/libavcodec/mediacodecdec_common.c
@@ -190,6 +190,7 @@ static uint64_t mcdec_map_channel_mask(AVCodecContext 
*avctx,
 
 enum {
     COLOR_FormatYUV420Planar                              = 0x13,
+    COLOR_FormatYUV420P010                                = 0x36,
     COLOR_FormatYUV420SemiPlanar                          = 0x15,
     COLOR_FormatYCbYCr                                    = 0x19,
     COLOR_FormatAndroidOpaque                             = 0x7F000789,
@@ -208,6 +209,7 @@ static const struct {
 } color_formats[] = {
 
     { COLOR_FormatYUV420Planar,                              
AV_PIX_FMT_YUV420P },
+    { COLOR_FormatYUV420P010,                                AV_PIX_FMT_P010   
 },
     { COLOR_FormatYUV420SemiPlanar,                          AV_PIX_FMT_NV12   
 },
     { COLOR_QCOM_FormatYUV420SemiPlanar,                     AV_PIX_FMT_NV12   
 },
     { COLOR_QCOM_FormatYUV420SemiPlanar32m,                  AV_PIX_FMT_NV12   
 },
@@ -489,6 +491,9 @@ static int mediacodec_wrap_sw_video_buffer(AVCodecContext 
*avctx,
     case COLOR_FormatYUV420Planar:
         ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, 
frame);
         break;
+    case COLOR_FormatYUV420P010:
+        ff_mediacodec_sw_buffer_copy_yuv420_p010(avctx, s, data, size, info, 
frame);
+        break;
     case COLOR_FormatYUV420SemiPlanar:
     case COLOR_QCOM_FormatYUV420SemiPlanar:
     case COLOR_QCOM_FormatYUV420SemiPlanar32m:
@@ -861,6 +866,13 @@ int ff_mediacodec_dec_init(AVCodecContext *avctx, 
MediaCodecDecContext *s,
     if (ret < 0)
         goto fail;
 
+    /* Surface output is already converted by the codec */
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !s->surface &&
+        avctx->bits_per_raw_sample > 8) {
+        ff_AMediaFormat_setInt32(format, "color-format", 
COLOR_FormatYUV420P010);
+        av_log(avctx, AV_LOG_DEBUG, "Requesting 10-bit (P010) output\n");
+    }
+
     status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
     if (status < 0) {
         char *desc = ff_AMediaFormat_toString(format);
-- 
2.56.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to