Re: [FFmpeg-devel] [PATCH 1/3] avcodec: Refactor common nvdec hwaccel logic

2017-11-20 Thread Timo Rothenpieler

this version is good as well



smime.p7s
Description: S/MIME Cryptographic Signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 1/3] avcodec: Refactor common nvdec hwaccel logic

2017-11-19 Thread Philip Langdale
The 'simple' hwaccels (not h.264 and hevc) all use the same bitstream
management and reference lookup logic so let's refactor all that into
common functions.

I verified that casting a signed int -1 to unsigned char produces 255
according to the C language specification.

Signed-off-by: Philip Langdale 
---
 libavcodec/nvdec.c| 46 +++
 libavcodec/nvdec.h|  4 
 libavcodec/nvdec_mpeg12.c | 53 -
 libavcodec/nvdec_vc1.c| 55 ++-
 libavcodec/nvdec_vp9.c| 53 +
 5 files changed, 65 insertions(+), 146 deletions(-)

diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index 3d62840e9f..97ff605f0f 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -475,6 +475,36 @@ finish:
 return ret;
 }
 
+int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
+{
+NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
+int ret = ff_nvdec_end_frame(avctx);
+ctx->bitstream = NULL;
+return ret;
+}
+
+int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
+ uint32_t size)
+{
+NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
+void *tmp;
+
+tmp = av_fast_realloc(ctx->slice_offsets, >slice_offsets_allocated,
+  (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
+if (!tmp)
+return AVERROR(ENOMEM);
+ctx->slice_offsets = tmp;
+
+if (!ctx->bitstream)
+ctx->bitstream = (uint8_t*)buffer;
+
+ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
+ctx->bitstream_len += size;
+ctx->nb_slices++;
+
+return 0;
+}
+
 int ff_nvdec_frame_params(AVCodecContext *avctx,
   AVBufferRef *hw_frames_ctx,
   int dpb_size)
@@ -520,3 +550,19 @@ int ff_nvdec_frame_params(AVCodecContext *avctx,
 
 return 0;
 }
+
+int ff_nvdec_get_ref_idx(AVFrame *frame)
+{
+FrameDecodeData *fdd;
+NVDECFrame *cf;
+
+if (!frame || !frame->private_ref)
+return -1;
+
+fdd = (FrameDecodeData*)frame->private_ref->data;
+cf  = (NVDECFrame*)fdd->hwaccel_priv;
+if (!cf)
+return -1;
+
+return cf->idx;
+}
diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h
index 14d29ee94b..90578d5a1c 100644
--- a/libavcodec/nvdec.h
+++ b/libavcodec/nvdec.h
@@ -58,8 +58,12 @@ int ff_nvdec_decode_init(AVCodecContext *avctx);
 int ff_nvdec_decode_uninit(AVCodecContext *avctx);
 int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame);
 int ff_nvdec_end_frame(AVCodecContext *avctx);
+int ff_nvdec_simple_end_frame(AVCodecContext *avctx);
+int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
+ uint32_t size);
 int ff_nvdec_frame_params(AVCodecContext *avctx,
   AVBufferRef *hw_frames_ctx,
   int dpb_size);
+int ff_nvdec_get_ref_idx(AVFrame *frame);
 
 #endif /* AVCODEC_NVDEC_H */
diff --git a/libavcodec/nvdec_mpeg12.c b/libavcodec/nvdec_mpeg12.c
index 127e843d85..db9cebeddd 100644
--- a/libavcodec/nvdec_mpeg12.c
+++ b/libavcodec/nvdec_mpeg12.c
@@ -25,22 +25,6 @@
 #include "nvdec.h"
 #include "decode.h"
 
-static int get_ref_idx(AVFrame *frame)
-{
-FrameDecodeData *fdd;
-NVDECFrame *cf;
-
-if (!frame || !frame->private_ref)
-return -1;
-
-fdd = (FrameDecodeData*)frame->private_ref->data;
-cf  = (NVDECFrame*)fdd->hwaccel_priv;
-if (!cf)
-return -1;
-
-return cf->idx;
-}
-
 static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t 
*buffer, uint32_t size)
 {
 MpegEncContext *s = avctx->priv_data;
@@ -71,8 +55,8 @@ static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, 
const uint8_t *buffer
  s->pict_type == AV_PICTURE_TYPE_P,
 
 .CodecSpecific.mpeg2 = {
-.ForwardRefIdx = get_ref_idx(s->last_picture.f),
-.BackwardRefIdx= get_ref_idx(s->next_picture.f),
+.ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
+.BackwardRefIdx= ff_nvdec_get_ref_idx(s->next_picture.f),
 
 .picture_coding_type= s->pict_type,
 .full_pel_forward_vector= s->full_pel[0],
@@ -99,35 +83,6 @@ static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, 
const uint8_t *buffer
 return 0;
 }
 
-static int nvdec_mpeg12_end_frame(AVCodecContext *avctx)
-{
-NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
-int ret = ff_nvdec_end_frame(avctx);
-ctx->bitstream = NULL;
-return ret;
-}
-
-static int nvdec_mpeg12_decode_slice(AVCodecContext *avctx, const uint8_t 
*buffer, uint32_t size)
-{
-NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
-void *tmp;
-
-tmp = av_fast_realloc(ctx->slice_offsets,