On Sat, 12 Apr 2014 18:25:43 +0200, Alessandro Ghedini <[email protected]> 
wrote:
> ---
>  libavformat/avio_internal.h |   2 +
>  libavformat/aviobuf.c       |   6 +++
>  libavformat/mp3dec.c        | 107 
> +++++++++++++++++++++++++++++++++++++++-----
>  3 files changed, 105 insertions(+), 10 deletions(-)
> 
> diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
> index fdc98ec..a8bcadd 100644
> --- a/libavformat/avio_internal.h
> +++ b/libavformat/avio_internal.h
> @@ -94,6 +94,8 @@ void ffio_init_checksum(AVIOContext *s,
>  unsigned long ffio_get_checksum(AVIOContext *s);
>  unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t 
> *buf,
>                                      unsigned int len);
> +unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
> +                                unsigned int len);
>  
>  /**
>   * Open a write only packetized memory stream with a maximum packet
> diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
> index 0024f9e..cc79146 100644
> --- a/libavformat/aviobuf.c
> +++ b/libavformat/aviobuf.c
> @@ -414,6 +414,12 @@ unsigned long ff_crc04C11DB7_update(unsigned long 
> checksum, const uint8_t *buf,
>      return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
>  }
>  
> +unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
> +                                unsigned int len)
> +{
> +    return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
> +}
> +
>  unsigned long ffio_get_checksum(AVIOContext *s)
>  {
>      s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
> diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c
> index 1ab5159..a7ffea6 100644
> --- a/libavformat/mp3dec.c
> +++ b/libavformat/mp3dec.c
> @@ -21,10 +21,12 @@
>  
>  #include "libavutil/avstring.h"
>  #include "libavutil/intreadwrite.h"
> +#include "libavutil/crc.h"
>  #include "libavutil/dict.h"
>  #include "libavutil/mathematics.h"
>  #include "avformat.h"
>  #include "internal.h"
> +#include "avio_internal.h"
>  #include "id3v2.h"
>  #include "id3v1.h"
>  #include "replaygain.h"
> @@ -128,11 +130,21 @@ static void read_xing_toc(AVFormatContext *s, int64_t 
> filesize, int64_t duration
>      mp3->xing_toc = 1;
>  }
>  
> -
>  static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st,
>                                 MPADecodeHeader *c, uint32_t spf)
>  {
> +#define LAST_BITS(k, n) ((k) & ((1 << (n)) - 1))
> +#define MIDDLE_BITS(k, m, n) LAST_BITS((k) >> (m), ((n) - (m)))
> +
> +    float f;

This shouldn't be float anymore.

> +    uint16_t crc;
>      uint32_t v;
> +
> +    char version[10];
> +
> +    uint32_t peak   = 0;
> +    int32_t  r_gain = INT32_MIN, a_gain = INT32_MIN;
> +
>      MP3DecContext *mp3 = s->priv_data;
>      const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
>  
> @@ -140,17 +152,90 @@ static void mp3_parse_info_tag(AVFormatContext *s, 
> AVStream *st,
>      avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
>      v = avio_rb32(s->pb);
>      mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
> -    if (v == MKBETAG('X', 'i', 'n', 'g') || mp3->is_cbr) {
> -        v = avio_rb32(s->pb);
> -        if (v & XING_FLAG_FRAMES)
> -            mp3->frames = avio_rb32(s->pb);
> -        if (v & XING_FLAG_SIZE)
> -            mp3->size = avio_rb32(s->pb);
> -        if (v & XING_FLAG_TOC && mp3->frames)
> -            read_xing_toc(s, mp3->size,
> -                          av_rescale_q(mp3->frames,
> +    if (v != MKBETAG('X', 'i', 'n', 'g') && !mp3->is_cbr)
> +        return;
> +
> +    v = avio_rb32(s->pb);
> +    if (v & XING_FLAG_FRAMES)
> +        mp3->frames = avio_rb32(s->pb);
> +    if (v & XING_FLAG_SIZE)
> +        mp3->size = avio_rb32(s->pb);
> +    if (v & XING_FLAG_TOC && mp3->frames)
> +        read_xing_toc(s, mp3->size, av_rescale_q(mp3->frames,
>                                         (AVRational){spf, c->sample_rate},
>                                         st->time_base));
> +
> +    /* VBR quality */
> +    avio_rb32(s->pb);
> +
> +    /* Encoder short version string */
> +    avio_get_str(s->pb, 9, version, sizeof(version));

It's probably better to init version to 0 and use avio_read() here, since the
string can contain zeroes which will cause avio_get_str() to terminate early,
breaking the rest of the tag.

> +
> +    /* Info Tag revision + VBR method */
> +    avio_r8(s->pb);
> +
> +    /* Lowpass filter value */
> +    avio_r8(s->pb);
> +
> +    /* ReplayGain peak */
> +    v    = avio_rb32(s->pb);
> +    peak = av_rescale(v, 100000, 1 << 23);
> +
> +    /* Radio ReplayGain */
> +    v = avio_rb16(s->pb);
> +
> +    if (MIDDLE_BITS(v, 13, 15) == 1) {
> +        f = MIDDLE_BITS(v, 0, 8);
> +
> +        if (v & (1 << 9))
> +            r_gain = f * -10000;
> +        else
> +            r_gain = f *  10000;
> +    }
> +
> +    /* Audiophile ReplayGain */
> +    v = avio_rb16(s->pb);
> +
> +    if (MIDDLE_BITS(v, 13, 15) == 2) {
> +        f = MIDDLE_BITS(v, 0, 8);
> +
> +        if (v & (1 << 9))
> +            a_gain = f * -10000;
> +        else
> +            a_gain = f *  10000;
> +    }
> +
> +    /* Encoding flags + ATH Type */
> +    avio_r8(s->pb);
> +
> +    /* if ABR {specified bitrate} else {minimal bitrate} */
> +    avio_r8(s->pb);
> +
> +    /* Encoder delays */
> +    avio_rb24(s->pb);
> +
> +    /* Misc */
> +    avio_r8(s->pb);
> +
> +    /* MP3 gain */
> +    avio_r8(s->pb);
> +
> +    /* Preset and surround info */
> +    avio_rb16(s->pb);
> +
> +    /* Music length */
> +    avio_rb32(s->pb);
> +
> +    /* Music CRC */
> +    avio_rb16(s->pb);
> +
> +    /* Info Tag CRC */
> +    crc = ffio_get_checksum(s->pb);
> +    v   = avio_rb16(s->pb);
> +
> +    if (v == crc) {
> +        ff_replaygain_export_raw(st, r_gain, peak, a_gain, 0);
> +        av_dict_set(&st->metadata, "encoder", version, 0);
>      }
>  }
>  
> @@ -235,6 +320,8 @@ static int mp3_read_header(AVFormatContext *s)
>  
>      off = avio_tell(s->pb);
>  
> +    ffio_init_checksum(s->pb, ff_crcA001_update, 0);

Why are you putting this call here?
Seems to be it'd be safer if it was at the top of mp3_parse_vbr_tags().
Otherwise it'll probably break at least on files with id3v1 tags.

-- 
Anton Khirnov
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to