[FFmpeg-devel] [PATCH 03/11] Implement av_get_codec_tag_string() and use it in ffprobe.

Michael Niedermayer michaelni
Sun May 23 17:01:21 CEST 2010


On Sun, May 23, 2010 at 02:07:16PM +0200, Stefano Sabatini wrote:
> On date Sunday 2010-05-23 11:19:31 +0200, Michael Niedermayer encoded:
> > On Sat, May 22, 2010 at 05:58:07PM +0200, Stefano Sabatini wrote:
> > > ---
> > [...]
> > > --- a/libavcodec/avcodec.h
> > > +++ b/libavcodec/avcodec.h
> > > @@ -3088,6 +3088,15 @@ attribute_deprecated enum PixelFormat avcodec_get_pix_fmt(const char* name);
> > >   */
> > >  unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat pix_fmt);
> > >  
> > > +/**
> > > + * Returns a string representing a codec tag, NULL if the codec_tag is
> > > + * unknown.
> > > + *
> > 
> > > + * @param buf buffer with size buf_size where the function puts the
> > > + * codec tag string
> > 
> > doesnt match code
> 
> I changed the signature, now it is inspired upon that of snprintf().
> 
> > > + */
> > > +const char *av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag);
> > > +
> > >  #define FF_LOSS_RESOLUTION  0x0001 /**< loss due to resolution change */
> > >  #define FF_LOSS_DEPTH       0x0002 /**< loss due to color depth change */
> > >  #define FF_LOSS_COLORSPACE  0x0004 /**< loss due to color space conversion */
> > > diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> > > index 56d4dbd..8d8826a 100644
> > > --- a/libavcodec/utils.c
> > > +++ b/libavcodec/utils.c
> > > @@ -798,6 +798,22 @@ static int get_bit_rate(AVCodecContext *ctx)
> > >      return bit_rate;
> > >  }
> > >  
> > > +const char *av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
> > 
> > const?
> 
> Fixed.
>   
> > > +{
> > > +    int i, fourcc[4];
> > > +    fourcc[0] = codec_tag     & 0xff;
> > > +    fourcc[1] = codec_tag>>8  & 0xff;
> > > +    fourcc[2] = codec_tag>>16 & 0xff;
> > > +    fourcc[3] = codec_tag>>24 & 0xff;
> > > +
> > > +    buf[0] = 0;
> > > +    for (i = 0; i < 4; i++) {
> > > +        if (isprint(fourcc[i])) av_strlcatf(buf, buf_size,  "%c" , fourcc[i]);
> > > +        else                    av_strlcatf(buf, buf_size, "[%d]", fourcc[i]);
> > 
> > snprintf()
> > 
> > codec_tag&0xFF
> > codec_tag>>=8;
> 
> Yes.
> 
> Please check again the updated patch, regards.
> -- 
> FFmpeg = Fantastic and Fabulous Martial Power Elegant Game

>  ffprobe.c            |   14 +++-----------
>  libavcodec/avcodec.h |    9 +++++++++
>  libavcodec/utils.c   |   15 +++++++++++++++
>  3 files changed, 27 insertions(+), 11 deletions(-)
> 2c048b1456afa23054677398fedab3014697e274  0001-Implement-av_get_codec_tag_string-and-use-it-in-ffpr.patch
> >From 41821d40ace61e7bdecd67c0cc1836324643df36 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
> Date: Sat, 22 May 2010 16:27:31 +0200
> Subject: [PATCH 1/9] Implement av_get_codec_tag_string() and use it in ffprobe.
> 
> ---
>  ffprobe.c            |   14 +++-----------
>  libavcodec/avcodec.h |    9 +++++++++
>  libavcodec/utils.c   |   15 +++++++++++++++
>  3 files changed, 27 insertions(+), 11 deletions(-)
> 
> diff --git a/ffprobe.c b/ffprobe.c
> index 7cb6167..4be5d0f 100644
> --- a/ffprobe.c
> +++ b/ffprobe.c
> @@ -120,7 +120,6 @@ static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
>      AVCodec *dec;
>      char val_str[128];
>      AVMetadataTag *tag = NULL;
> -    char a, b, c, d;
>      AVRational display_aspect_ratio;
>  
>      printf("[STREAM]\n");
> @@ -139,16 +138,9 @@ static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
>          printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
>  
>          /* print AVI/FourCC tag */
> -        a = dec_ctx->codec_tag     & 0xff;
> -        b = dec_ctx->codec_tag>>8  & 0xff;
> -        c = dec_ctx->codec_tag>>16 & 0xff;
> -        d = dec_ctx->codec_tag>>24 & 0xff;
> -        printf("codec_tag_string=");
> -        if (isprint(a)) printf("%c", a); else printf("[%d]", a);
> -        if (isprint(b)) printf("%c", b); else printf("[%d]", b);
> -        if (isprint(c)) printf("%c", c); else printf("[%d]", c);
> -        if (isprint(d)) printf("%c", d); else printf("[%d]", d);
> -        printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
> +        av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
> +        printf("codec_tag_string=%s\n", val_str);
> +        printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
>  
>          switch (dec_ctx->codec_type) {
>          case AVMEDIA_TYPE_VIDEO:
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 8781c0a..2cae900 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -3088,6 +3088,15 @@ attribute_deprecated enum PixelFormat avcodec_get_pix_fmt(const char* name);
>   */
>  unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat pix_fmt);
>  
> +/**
> + * Puts a string representing the codec tag codec_tag in buf.
> + *
> + * @param buf_size size in bytes of buf
> + * @return the number of characters which would be generated for the
> + * given input, excluding the trailing null
> + */
> +int av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag);
> +
>  #define FF_LOSS_RESOLUTION  0x0001 /**< loss due to resolution change */
>  #define FF_LOSS_DEPTH       0x0002 /**< loss due to color depth change */
>  #define FF_LOSS_COLORSPACE  0x0004 /**< loss due to color space conversion */
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index 56d4dbd..0daf331 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -798,6 +798,21 @@ static int get_bit_rate(AVCodecContext *ctx)
>      return bit_rate;
>  }
>  
> +int av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
> +{
> +    int i, len, ret = 0;
> +
> +    for (i = 0; i < 4; i++) {
> +        const char *tmpl = isprint(codec_tag&0xFF) ? "%c" : "[%d]";
> +        len = snprintf(buf, buf_size, tmpl, codec_tag&0xFF);
> +        buf      += len;
> +        buf_size  = len >= buf_size ? 0 : buf_size - len;
> +        ret      += len;
> +        codec_tag>>=8;
> +    }

hmm, i see now av_strlcatf() would have been simpler, it wasnt my intent to
complicate this

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Its not that you shouldnt use gotos but rather that you should write
readable code and code with gotos often but not always is less readable
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20100523/cefa76b5/attachment.pgp>



More information about the ffmpeg-devel mailing list