[FFmpeg-devel] [PATCH v2 1/3] avutil/timecode: add ff_timecode_set_smpte

Timo Rothenpieler timo at rothenpieler.org
Sun Mar 2 14:32:17 EET 2025


On 28.02.2025 23:46, Timo Rothenpieler wrote:
> ---
>   libavutil/timecode.c          | 27 +++----------------
>   libavutil/timecode_internal.c | 51 +++++++++++++++++++++++++++++++++++
>   libavutil/timecode_internal.h | 51 +++++++++++++++++++++++++++++++++++
>   3 files changed, 105 insertions(+), 24 deletions(-)
>   create mode 100644 libavutil/timecode_internal.c
>   create mode 100644 libavutil/timecode_internal.h
> 
> diff --git a/libavutil/timecode.c b/libavutil/timecode.c
> index f454466f97..bca16b6ac2 100644
> --- a/libavutil/timecode.c
> +++ b/libavutil/timecode.c
> @@ -29,6 +29,7 @@
>   #include <stdio.h>
>   #include "common.h"
>   #include "timecode.h"
> +#include "timecode_internal.h"
>   #include "log.h"
>   #include "error.h"
>   
> @@ -127,32 +128,10 @@ char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum_arg)
>       return buf;
>   }
>   
> -static unsigned bcd2uint(uint8_t bcd)
> -{
> -   unsigned low  = bcd & 0xf;
> -   unsigned high = bcd >> 4;
> -   if (low > 9 || high > 9)
> -       return 0;
> -   return low + 10*high;
> -}
> -
>   char *av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
>   {
> -    unsigned hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
> -    unsigned mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
> -    unsigned ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
> -    unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
> -    unsigned drop = tcsmpte & 1<<30 && !prevent_df;  // 1-bit drop if not arbitrary bit
> -
> -    if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
> -        ff <<= 1;
> -        if (!skip_field) {
> -            if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
> -                ff += !!(tcsmpte & 1 << 7);
> -            else
> -                ff += !!(tcsmpte & 1 << 23);
> -        }
> -    }
> +    unsigned hh, mm, ss, ff, drop;
> +    ff_timecode_set_smpte(&drop, &hh, &mm, &ss, &ff, rate, tcsmpte, prevent_df, skip_field);
>   
>       snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
>                hh, mm, ss, drop ? ';' : ':', ff);
> diff --git a/libavutil/timecode_internal.c b/libavutil/timecode_internal.c
> new file mode 100644
> index 0000000000..259ebf1664
> --- /dev/null
> +++ b/libavutil/timecode_internal.c
> @@ -0,0 +1,51 @@
> +/*
> + * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier at gmail.com>
> + * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch at smartjog.com>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "timecode_internal.h"
> +
> +static unsigned bcd2uint(uint8_t bcd)
> +{
> +   unsigned low  = bcd & 0xf;
> +   unsigned high = bcd >> 4;
> +   if (low > 9 || high > 9)
> +       return 0;
> +   return low + 10*high;
> +}
> +
> +void ff_timecode_set_smpte(unsigned *drop, unsigned *hh, unsigned *mm, unsigned *ss, unsigned *ff,
> +                           AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
> +{
> +    *hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
> +    *mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
> +    *ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
> +    *ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
> +    *drop = tcsmpte & 1<<30 && !prevent_df;  // 1-bit drop if not arbitrary bit
> +
> +    if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
> +        *ff <<= 1;
> +        if (!skip_field) {
> +            if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
> +                *ff += !!(tcsmpte & 1 << 7);
> +            else
> +                *ff += !!(tcsmpte & 1 << 23);
> +        }
> +    }
> +}
> diff --git a/libavutil/timecode_internal.h b/libavutil/timecode_internal.h
> new file mode 100644
> index 0000000000..8ef43d1f98
> --- /dev/null
> +++ b/libavutil/timecode_internal.h
> @@ -0,0 +1,51 @@
> +/*
> + * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier at gmail.com>
> + * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch at smartjog.com>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @file
> + * Timecode helpers header
> + */
> +
> +#ifndef AVUTIL_TIMECODE_INTERNAL_H
> +#define AVUTIL_TIMECODE_INTERNAL_H
> +
> +#include <stdint.h>
> +#include "rational.h"
> +
> +/**
> + * Convert SMPTE 12M binary representation to sei info.
> + *
> + * @param drop       drop flag output
> + * @param hh         hour output
> + * @param mm         minute output
> + * @param ss         second output
> + * @param ff         frame number output
> + * @param rate       frame rate of the timecode
> + * @param tcsmpte    the 32-bit SMPTE timecode
> + * @param prevent_df prevent the use of a drop flag when it is known the DF bit
> + *                   is arbitrary
> + * @param skip_field prevent the use of a field flag when it is known the field
> + *                   bit is arbitrary (e.g. because it is used as PC flag)
> + */
> +void ff_timecode_set_smpte(unsigned *drop, unsigned *hh, unsigned *mm, unsigned *ss, unsigned *ff,
> +                           AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field);
> +
> +#endif /* AVUTIL_TIMECODE_INTERNAL_H */

will apply the series soon if nobody objects


More information about the ffmpeg-devel mailing list