[FFmpeg-devel] [PATCH 2/2] avcodec/utils: calculate frame number of HEVC if the framerate > 30FPS

lance.lmwang at gmail.com lance.lmwang at gmail.com
Fri Aug 14 17:57:57 EEST 2020


On Fri, Jul 17, 2020 at 09:26:38PM +0800, lance.lmwang at gmail.com wrote:
> From: Limin Wang <lance.lmwang at gmail.com>
> 
> ---
>  libavcodec/internal.h |  3 ++-
>  libavcodec/nvenc.c    |  2 +-
>  libavcodec/utils.c    | 13 ++++++++++++-
>  3 files changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/internal.h b/libavcodec/internal.h
> index 0a1c0a1..4c34d7d 100644
> --- a/libavcodec/internal.h
> +++ b/libavcodec/internal.h
> @@ -382,6 +382,7 @@ int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
>   * Check AVFrame for S12M timecode side data and allocate and fill TC SEI message with timecode info
>   *
>   * @param frame      Raw frame to get S12M timecode side data from
> + * @param rate       The frame rate
>   * @param prefix_len Number of bytes to allocate before SEI message
>   * @param data       Pointer to a variable to store allocated memory
>   *                   Upon return the variable will hold NULL on error or if frame has no S12M timecode info.
> @@ -390,7 +391,7 @@ int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
>   * @param sei_size   Pointer to a variable to store generated SEI message length
>   * @return           Zero on success, negative error code on failure
>   */
> -int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
> +int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
>                       void **data, size_t *sei_size);
>  
>  /**
> diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
> index c6740c1..45a7b60 100644
> --- a/libavcodec/nvenc.c
> +++ b/libavcodec/nvenc.c
> @@ -2218,7 +2218,7 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
>              void *tc_data = NULL;
>              size_t tc_size = 0;
>  
> -            if (ff_alloc_timecode_sei(frame, 0, (void**)&tc_data, &tc_size) < 0) {
> +            if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, (void**)&tc_data, &tc_size) < 0) {
>                  av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n");
>              }
>  
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index 2ece34f..51c4067 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -2254,7 +2254,7 @@ static unsigned bcd2uint(uint8_t bcd)
>      return low + 10*high;
>  }
>  
> -int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
> +int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
>                       void **data, size_t *sei_size)
>  {
>      AVFrameSideData *sd = NULL;
> @@ -2290,6 +2290,17 @@ int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
>          unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
>          unsigned drop = tcsmpte & 1<<30 && !0;  // 1-bit drop if not arbitrary bit
>  
> +        /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
> +        if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
> +            unsigned pc;
> +            ff *= 2;
> +            if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
> +                pc = !!(tcsmpte & 1 << 7);
> +            else
> +                pc = !!(tcsmpte & 1 << 23);
> +            ff = (ff + pc) & 0x7f;
> +        }
> +
>          put_bits(&pb, 1, 1); // clock_timestamp_flag
>          put_bits(&pb, 1, 1); // units_field_based_flag
>          put_bits(&pb, 5, 0); // counting_type
> -- 
> 2.9.4

ping the patch.


> 

-- 
Thanks,
Limin Wang


More information about the ffmpeg-devel mailing list