LCOV - code coverage report
Current view: top level - libavcodec - mpegaudiodecheader.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 67 68 98.5 %
Date: 2017-12-18 13:19:42 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * MPEG Audio header decoder
       3             :  * Copyright (c) 2001, 2002 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * MPEG Audio header decoder.
      25             :  */
      26             : 
      27             : #include "libavutil/common.h"
      28             : 
      29             : #include "avcodec.h"
      30             : #include "internal.h"
      31             : #include "mpegaudio.h"
      32             : #include "mpegaudiodata.h"
      33             : #include "mpegaudiodecheader.h"
      34             : 
      35             : 
      36   114960482 : int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
      37             : {
      38             :     int sample_rate, frame_size, mpeg25, padding;
      39             :     int sample_rate_index, bitrate_index;
      40             :     int ret;
      41             : 
      42   114960482 :     ret = ff_mpa_check_header(header);
      43   114960482 :     if (ret < 0)
      44   114870036 :         return ret;
      45             : 
      46       90446 :     if (header & (1<<20)) {
      47       70687 :         s->lsf = (header & (1<<19)) ? 0 : 1;
      48       70687 :         mpeg25 = 0;
      49             :     } else {
      50       19759 :         s->lsf = 1;
      51       19759 :         mpeg25 = 1;
      52             :     }
      53             : 
      54       90446 :     s->layer = 4 - ((header >> 17) & 3);
      55             :     /* extract frequency */
      56       90446 :     sample_rate_index = (header >> 10) & 3;
      57       90446 :     if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
      58           0 :         sample_rate_index = 0;
      59       90446 :     sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
      60       90446 :     sample_rate_index += 3 * (s->lsf + mpeg25);
      61       90446 :     s->sample_rate_index = sample_rate_index;
      62       90446 :     s->error_protection = ((header >> 16) & 1) ^ 1;
      63       90446 :     s->sample_rate = sample_rate;
      64             : 
      65       90446 :     bitrate_index = (header >> 12) & 0xf;
      66       90446 :     padding = (header >> 9) & 1;
      67             :     //extension = (header >> 8) & 1;
      68       90446 :     s->mode = (header >> 6) & 3;
      69       90446 :     s->mode_ext = (header >> 4) & 3;
      70             :     //copyright = (header >> 3) & 1;
      71             :     //original = (header >> 2) & 1;
      72             :     //emphasis = header & 3;
      73             : 
      74       90446 :     if (s->mode == MPA_MONO)
      75       40838 :         s->nb_channels = 1;
      76             :     else
      77       49608 :         s->nb_channels = 2;
      78             : 
      79       90446 :     if (bitrate_index != 0) {
      80       51650 :         frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
      81       51650 :         s->bit_rate = frame_size * 1000;
      82       51650 :         switch(s->layer) {
      83       20464 :         case 1:
      84       20464 :             frame_size = (frame_size * 12000) / sample_rate;
      85       20464 :             frame_size = (frame_size + padding) * 4;
      86       20464 :             break;
      87       15652 :         case 2:
      88       15652 :             frame_size = (frame_size * 144000) / sample_rate;
      89       15652 :             frame_size += padding;
      90       15652 :             break;
      91       15534 :         default:
      92             :         case 3:
      93       15534 :             frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
      94       15534 :             frame_size += padding;
      95       15534 :             break;
      96             :         }
      97       51650 :         s->frame_size = frame_size;
      98             :     } else {
      99             :         /* if no frame size computed, signal it */
     100       38796 :         return 1;
     101             :     }
     102             : 
     103             : #if defined(DEBUG)
     104             :     ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
     105             :            s->layer, s->sample_rate, s->bit_rate);
     106             :     if (s->nb_channels == 2) {
     107             :         if (s->layer == 3) {
     108             :             if (s->mode_ext & MODE_EXT_MS_STEREO)
     109             :                 ff_dlog(NULL, "ms-");
     110             :             if (s->mode_ext & MODE_EXT_I_STEREO)
     111             :                 ff_dlog(NULL, "i-");
     112             :         }
     113             :         ff_dlog(NULL, "stereo");
     114             :     } else {
     115             :         ff_dlog(NULL, "mono");
     116             :     }
     117             :     ff_dlog(NULL, "\n");
     118             : #endif
     119       51650 :     return 0;
     120             : }
     121             : 
     122      183514 : int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id)
     123             : {
     124      183514 :     MPADecodeHeader s1, *s = &s1;
     125             : 
     126      183514 :     if (avpriv_mpegaudio_decode_header(s, head) != 0) {
     127      171194 :         return -1;
     128             :     }
     129             : 
     130       12320 :     switch(s->layer) {
     131           4 :     case 1:
     132           4 :         *codec_id = AV_CODEC_ID_MP1;
     133           4 :         *frame_size = 384;
     134           4 :         break;
     135        6173 :     case 2:
     136        6173 :         *codec_id = AV_CODEC_ID_MP2;
     137        6173 :         *frame_size = 1152;
     138        6173 :         break;
     139        6143 :     default:
     140             :     case 3:
     141        6143 :         if (*codec_id != AV_CODEC_ID_MP3ADU)
     142        6143 :             *codec_id = AV_CODEC_ID_MP3;
     143        6143 :         if (s->lsf)
     144         241 :             *frame_size = 576;
     145             :         else
     146        5902 :             *frame_size = 1152;
     147        6143 :         break;
     148             :     }
     149             : 
     150       12320 :     *sample_rate = s->sample_rate;
     151       12320 :     *channels = s->nb_channels;
     152       12320 :     *bit_rate = s->bit_rate;
     153       12320 :     return s->frame_size;
     154             : }

Generated by: LCOV version 1.13