LCOV - code coverage report
Current view: top level - libavcodec - vorbis_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 103 154 66.9 %
Date: 2017-12-12 11:08:38 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Justin Ruggles
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * Vorbis audio parser
      24             :  *
      25             :  * Determines the duration for each packet.
      26             :  */
      27             : 
      28             : #include "libavutil/log.h"
      29             : 
      30             : #include "get_bits.h"
      31             : #include "parser.h"
      32             : #include "xiph.h"
      33             : #include "vorbis_parser_internal.h"
      34             : 
      35             : static const AVClass vorbis_parser_class = {
      36             :     .class_name = "Vorbis parser",
      37             :     .item_name  = av_default_item_name,
      38             :     .version    = LIBAVUTIL_VERSION_INT,
      39             : };
      40             : 
      41          33 : static int parse_id_header(AVVorbisParseContext *s,
      42             :                            const uint8_t *buf, int buf_size)
      43             : {
      44             :     /* Id header should be 30 bytes */
      45          33 :     if (buf_size < 30) {
      46           0 :         av_log(s, AV_LOG_ERROR, "Id header is too short\n");
      47           0 :         return AVERROR_INVALIDDATA;
      48             :     }
      49             : 
      50             :     /* make sure this is the Id header */
      51          33 :     if (buf[0] != 1) {
      52           0 :         av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
      53           0 :         return AVERROR_INVALIDDATA;
      54             :     }
      55             : 
      56             :     /* check for header signature */
      57          33 :     if (memcmp(&buf[1], "vorbis", 6)) {
      58           0 :         av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
      59           0 :         return AVERROR_INVALIDDATA;
      60             :     }
      61             : 
      62          33 :     if (!(buf[29] & 0x1)) {
      63           0 :         av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
      64           0 :         return AVERROR_INVALIDDATA;
      65             :     }
      66             : 
      67          33 :     s->blocksize[0] = 1 << (buf[28] & 0xF);
      68          33 :     s->blocksize[1] = 1 << (buf[28] >>  4);
      69             : 
      70          33 :     return 0;
      71             : }
      72             : 
      73          33 : static int parse_setup_header(AVVorbisParseContext *s,
      74             :                               const uint8_t *buf, int buf_size)
      75             : {
      76             :     GetBitContext gb, gb0;
      77             :     uint8_t *rev_buf;
      78          33 :     int i, ret = 0;
      79          33 :     int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
      80             : 
      81             :     /* avoid overread */
      82          33 :     if (buf_size < 7) {
      83           0 :         av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
      84           0 :         return AVERROR_INVALIDDATA;
      85             :     }
      86             : 
      87             :     /* make sure this is the Setup header */
      88          33 :     if (buf[0] != 5) {
      89           0 :         av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
      90           0 :         return AVERROR_INVALIDDATA;
      91             :     }
      92             : 
      93             :     /* check for header signature */
      94          33 :     if (memcmp(&buf[1], "vorbis", 6)) {
      95           0 :         av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
      96           0 :         return AVERROR_INVALIDDATA;
      97             :     }
      98             : 
      99             :     /* reverse bytes so we can easily read backwards with get_bits() */
     100          33 :     if (!(rev_buf = av_malloc(buf_size))) {
     101           0 :         av_log(s, AV_LOG_ERROR, "Out of memory\n");
     102           0 :         return AVERROR(ENOMEM);
     103             :     }
     104      179525 :     for (i = 0; i < buf_size; i++)
     105      179492 :         rev_buf[i] = buf[buf_size - 1 - i];
     106          33 :     init_get_bits(&gb, rev_buf, buf_size * 8);
     107             : 
     108          33 :     got_framing_bit = 0;
     109         195 :     while (get_bits_left(&gb) > 97) {
     110         162 :         if (get_bits1(&gb)) {
     111          33 :             got_framing_bit = get_bits_count(&gb);
     112          33 :             break;
     113             :         }
     114             :     }
     115          33 :     if (!got_framing_bit) {
     116           0 :         av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
     117           0 :         ret = AVERROR_INVALIDDATA;
     118           0 :         goto bad_header;
     119             :     }
     120             : 
     121             :     /* Now we search backwards to find possible valid mode counts. This is not
     122             :      * fool-proof because we could have false positive matches and read too
     123             :      * far, but there isn't really any way to be sure without parsing through
     124             :      * all the many variable-sized fields before the modes. This approach seems
     125             :      * to work well in testing, and it is similar to how it is handled in
     126             :      * liboggz. */
     127          33 :     mode_count = 0;
     128          33 :     got_mode_header = 0;
     129         132 :     while (get_bits_left(&gb) >= 97) {
     130          99 :         if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
     131             :             break;
     132          66 :         skip_bits(&gb, 1);
     133          66 :         mode_count++;
     134          66 :         if (mode_count > 64)
     135           0 :             break;
     136          66 :         gb0 = gb;
     137          66 :         if (get_bits(&gb0, 6) + 1 == mode_count) {
     138          66 :             got_mode_header = 1;
     139          66 :             last_mode_count = mode_count;
     140             :         }
     141             :     }
     142          33 :     if (!got_mode_header) {
     143           0 :         av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
     144           0 :         ret = AVERROR_INVALIDDATA;
     145           0 :         goto bad_header;
     146             :     }
     147             :     /* All samples I've seen use <= 2 modes, so ask for a sample if we find
     148             :      * more than that, as it is most likely a false positive. If we get any
     149             :      * we may need to approach this the long way and parse the whole Setup
     150             :      * header, but I hope very much that it never comes to that. */
     151          33 :     if (last_mode_count > 2) {
     152           0 :         avpriv_request_sample(s,
     153             :                               "%d modes (either a false positive or a "
     154             :                               "sample from an unknown encoder)",
     155             :                               last_mode_count);
     156             :     }
     157             :     /* We're limiting the mode count to 63 so that we know that the previous
     158             :      * block flag will be in the first packet byte. */
     159          33 :     if (last_mode_count > 63) {
     160           0 :         av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
     161             :                last_mode_count);
     162           0 :         ret = AVERROR_INVALIDDATA;
     163           0 :         goto bad_header;
     164             :     }
     165          33 :     s->mode_count = mode_count = last_mode_count;
     166             :     /* Determine the number of bits required to code the mode and turn that
     167             :      * into a bitmask to directly access the mode from the first frame byte. */
     168          33 :     s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
     169             :     /* The previous window flag is the next bit after the mode */
     170          33 :     s->prev_mask = (s->mode_mask | 0x1) + 1;
     171             : 
     172          33 :     init_get_bits(&gb, rev_buf, buf_size * 8);
     173          33 :     skip_bits_long(&gb, got_framing_bit);
     174          99 :     for (i = mode_count - 1; i >= 0; i--) {
     175          66 :         skip_bits_long(&gb, 40);
     176          66 :         s->mode_blocksize[i] = get_bits1(&gb);
     177             :     }
     178             : 
     179          33 : bad_header:
     180          33 :     av_free(rev_buf);
     181          33 :     return ret;
     182             : }
     183             : 
     184          33 : static int vorbis_parse_init(AVVorbisParseContext *s,
     185             :                              const uint8_t *extradata, int extradata_size)
     186             : {
     187             :     const uint8_t *header_start[3];
     188             :     int header_len[3];
     189             :     int ret;
     190             : 
     191          33 :     s->class = &vorbis_parser_class;
     192          33 :     s->extradata_parsed = 1;
     193             : 
     194          33 :     if ((ret = avpriv_split_xiph_headers(extradata,
     195             :                                          extradata_size, 30,
     196             :                                          header_start, header_len)) < 0) {
     197           0 :         av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
     198           0 :         return ret;
     199             :     }
     200             : 
     201          33 :     if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
     202           0 :         return ret;
     203             : 
     204          33 :     if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
     205           0 :         return ret;
     206             : 
     207          33 :     s->valid_extradata = 1;
     208          33 :     s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
     209             : 
     210          33 :     return 0;
     211             : }
     212             : 
     213      141981 : int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
     214             :                                 int buf_size, int *flags)
     215             : {
     216      141981 :     int duration = 0;
     217             : 
     218      141981 :     if (s->valid_extradata && buf_size > 0) {
     219             :         int mode, current_blocksize;
     220      141973 :         int previous_blocksize = s->previous_blocksize;
     221             : 
     222      141973 :         if (buf[0] & 1) {
     223             :             /* If the user doesn't care about special packets, it's a bad one. */
     224           0 :             if (!flags)
     225           0 :                 goto bad_packet;
     226             : 
     227             :             /* Set the flag for which kind of special packet it is. */
     228           0 :             if (buf[0] == 1)
     229           0 :                 *flags |= VORBIS_FLAG_HEADER;
     230           0 :             else if (buf[0] == 3)
     231           0 :                 *flags |= VORBIS_FLAG_COMMENT;
     232           0 :             else if (buf[0] == 5)
     233           0 :                 *flags |= VORBIS_FLAG_SETUP;
     234             :             else
     235           0 :                 goto bad_packet;
     236             : 
     237             :             /* Special packets have no duration. */
     238           0 :             return 0;
     239             : 
     240           0 : bad_packet:
     241           0 :             av_log(s, AV_LOG_ERROR, "Invalid packet\n");
     242           0 :             return AVERROR_INVALIDDATA;
     243             :         }
     244      141973 :         if (s->mode_count == 1)
     245           0 :             mode = 0;
     246             :         else
     247      141973 :             mode = (buf[0] & s->mode_mask) >> 1;
     248      141973 :         if (mode >= s->mode_count) {
     249           0 :             av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
     250           0 :             return AVERROR_INVALIDDATA;
     251             :         }
     252      141973 :         if(s->mode_blocksize[mode]){
     253       79721 :             int flag = !!(buf[0] & s->prev_mask);
     254       79721 :             previous_blocksize = s->blocksize[flag];
     255             :         }
     256      141973 :         current_blocksize     = s->blocksize[s->mode_blocksize[mode]];
     257      141973 :         duration              = (previous_blocksize + current_blocksize) >> 2;
     258      141973 :         s->previous_blocksize = current_blocksize;
     259             :     }
     260             : 
     261      141981 :     return duration;
     262             : }
     263             : 
     264         463 : int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
     265             :                           int buf_size)
     266             : {
     267         463 :     return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
     268             : }
     269             : 
     270       17868 : void av_vorbis_parse_reset(AVVorbisParseContext *s)
     271             : {
     272       17868 :     if (s->valid_extradata)
     273       17868 :         s->previous_blocksize = s->blocksize[0];
     274       17868 : }
     275             : 
     276          68 : void av_vorbis_parse_free(AVVorbisParseContext **s)
     277             : {
     278          68 :     av_freep(s);
     279          68 : }
     280             : 
     281          33 : AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
     282             :                                            int extradata_size)
     283             : {
     284          33 :     AVVorbisParseContext *s = av_mallocz(sizeof(*s));
     285             :     int ret;
     286             : 
     287          33 :     if (!s)
     288           0 :         return NULL;
     289             : 
     290          33 :     ret = vorbis_parse_init(s, extradata, extradata_size);
     291          33 :     if (ret < 0) {
     292           0 :         av_vorbis_parse_free(&s);
     293           0 :         return NULL;
     294             :     }
     295             : 
     296          33 :     return s;
     297             : }
     298             : 
     299             : #if CONFIG_VORBIS_PARSER
     300             : 
     301             : typedef struct VorbisParseContext {
     302             :     AVVorbisParseContext *vp;
     303             : } VorbisParseContext;
     304             : 
     305         463 : static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
     306             :                         const uint8_t **poutbuf, int *poutbuf_size,
     307             :                         const uint8_t *buf, int buf_size)
     308             : {
     309         463 :     VorbisParseContext *s = s1->priv_data;
     310             :     int duration;
     311             : 
     312         463 :     if (!s->vp && avctx->extradata && avctx->extradata_size) {
     313           8 :         s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
     314             :     }
     315         463 :     if (!s->vp)
     316           0 :         goto end;
     317             : 
     318         463 :     if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
     319         463 :         s1->duration = duration;
     320             : 
     321         463 : end:
     322             :     /* always return the full packet. this parser isn't doing any splitting or
     323             :        combining, only packet analysis */
     324         463 :     *poutbuf      = buf;
     325         463 :     *poutbuf_size = buf_size;
     326         463 :     return buf_size;
     327             : }
     328             : 
     329          43 : static void vorbis_parser_close(AVCodecParserContext *ctx)
     330             : {
     331          43 :     VorbisParseContext *s = ctx->priv_data;
     332          43 :     av_vorbis_parse_free(&s->vp);
     333          43 : }
     334             : 
     335             : AVCodecParser ff_vorbis_parser = {
     336             :     .codec_ids      = { AV_CODEC_ID_VORBIS },
     337             :     .priv_data_size = sizeof(VorbisParseContext),
     338             :     .parser_parse   = vorbis_parse,
     339             :     .parser_close   = vorbis_parser_close,
     340             : };
     341             : #endif /* CONFIG_VORBIS_PARSER */

Generated by: LCOV version 1.13