LCOV - code coverage report
Current view: top level - libavcodec - ac3_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 107 109 98.2 %
Date: 2017-12-18 06:23:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * AC-3 parser
       3             :  * Copyright (c) 2003 Fabrice Bellard
       4             :  * Copyright (c) 2003 Michael Niedermayer
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #include "config.h"
      24             : 
      25             : #include "libavutil/channel_layout.h"
      26             : #include "parser.h"
      27             : #include "ac3_parser.h"
      28             : #include "ac3_parser_internal.h"
      29             : #include "aac_ac3_parser.h"
      30             : #include "get_bits.h"
      31             : 
      32             : 
      33             : #define AC3_HEADER_SIZE 7
      34             : 
      35             : #if CONFIG_AC3_PARSER
      36             : 
      37             : static const uint8_t eac3_blocks[4] = {
      38             :     1, 2, 3, 6
      39             : };
      40             : 
      41             : /**
      42             :  * Table for center mix levels
      43             :  * reference: Section 5.4.2.4 cmixlev
      44             :  */
      45             : static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
      46             : 
      47             : /**
      48             :  * Table for surround mix levels
      49             :  * reference: Section 5.4.2.5 surmixlev
      50             :  */
      51             : static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
      52             : 
      53             : 
      54       50135 : int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
      55             : {
      56             :     int frame_size_code;
      57             : 
      58       50135 :     memset(hdr, 0, sizeof(*hdr));
      59             : 
      60       50135 :     hdr->sync_word = get_bits(gbc, 16);
      61       50135 :     if(hdr->sync_word != 0x0B77)
      62       33577 :         return AAC_AC3_PARSE_ERROR_SYNC;
      63             : 
      64             :     /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
      65       16558 :     hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
      66       16558 :     if(hdr->bitstream_id > 16)
      67        2602 :         return AAC_AC3_PARSE_ERROR_BSID;
      68             : 
      69       13956 :     hdr->num_blocks = 6;
      70             : 
      71             :     /* set default mix levels */
      72       13956 :     hdr->center_mix_level   = 5;  // -4.5dB
      73       13956 :     hdr->surround_mix_level = 6;  // -6.0dB
      74             : 
      75             :     /* set default dolby surround mode */
      76       13956 :     hdr->dolby_surround_mode = AC3_DSURMOD_NOTINDICATED;
      77             : 
      78       13956 :     if(hdr->bitstream_id <= 10) {
      79             :         /* Normal AC-3 */
      80        6619 :         hdr->crc1 = get_bits(gbc, 16);
      81        6619 :         hdr->sr_code = get_bits(gbc, 2);
      82        6619 :         if(hdr->sr_code == 3)
      83         172 :             return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
      84             : 
      85        6447 :         frame_size_code = get_bits(gbc, 6);
      86        6447 :         if(frame_size_code > 37)
      87         182 :             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
      88             : 
      89        6265 :         skip_bits(gbc, 5); // skip bsid, already got it
      90             : 
      91        6265 :         hdr->bitstream_mode = get_bits(gbc, 3);
      92        6265 :         hdr->channel_mode = get_bits(gbc, 3);
      93             : 
      94        6265 :         if(hdr->channel_mode == AC3_CHMODE_STEREO) {
      95        1651 :             hdr->dolby_surround_mode = get_bits(gbc, 2);
      96             :         } else {
      97        4614 :             if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
      98        3179 :                 hdr->  center_mix_level =   center_levels[get_bits(gbc, 2)];
      99        4614 :             if(hdr->channel_mode & 4)
     100        3209 :                 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
     101             :         }
     102        6265 :         hdr->lfe_on = get_bits1(gbc);
     103             : 
     104        6265 :         hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
     105        6265 :         hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
     106        6265 :         hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
     107        6265 :         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
     108        6265 :         hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
     109        6265 :         hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
     110        6265 :         hdr->substreamid = 0;
     111             :     } else {
     112             :         /* Enhanced AC-3 */
     113        7337 :         hdr->crc1 = 0;
     114        7337 :         hdr->frame_type = get_bits(gbc, 2);
     115        7337 :         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
     116          74 :             return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     117             : 
     118        7263 :         hdr->substreamid = get_bits(gbc, 3);
     119             : 
     120        7263 :         hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
     121        7263 :         if(hdr->frame_size < AC3_HEADER_SIZE)
     122           2 :             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
     123             : 
     124        7261 :         hdr->sr_code = get_bits(gbc, 2);
     125        7261 :         if (hdr->sr_code == 3) {
     126          78 :             int sr_code2 = get_bits(gbc, 2);
     127          78 :             if(sr_code2 == 3)
     128          24 :                 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
     129          54 :             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
     130          54 :             hdr->sr_shift = 1;
     131             :         } else {
     132        7183 :             hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
     133        7183 :             hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
     134        7183 :             hdr->sr_shift = 0;
     135             :         }
     136             : 
     137        7237 :         hdr->channel_mode = get_bits(gbc, 3);
     138        7237 :         hdr->lfe_on = get_bits1(gbc);
     139             : 
     140       14474 :         hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate /
     141        7237 :                         (hdr->num_blocks * 256);
     142        7237 :         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
     143             :     }
     144       13502 :     hdr->channel_layout = avpriv_ac3_channel_layout_tab[hdr->channel_mode];
     145       13502 :     if (hdr->lfe_on)
     146        4269 :         hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
     147             : 
     148       13502 :     return 0;
     149             : }
     150             : 
     151             : // TODO: Better way to pass AC3HeaderInfo fields to mov muxer.
     152         652 : int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf,
     153             :                             size_t size)
     154             : {
     155             :     GetBitContext gb;
     156             :     AC3HeaderInfo *hdr;
     157             :     int err;
     158             : 
     159         652 :     if (!*phdr)
     160         652 :         *phdr = av_mallocz(sizeof(AC3HeaderInfo));
     161         652 :     if (!*phdr)
     162           0 :         return AVERROR(ENOMEM);
     163         652 :     hdr = *phdr;
     164             : 
     165         652 :     init_get_bits8(&gb, buf, size);
     166         652 :     err = ff_ac3_parse_header(&gb, hdr);
     167         652 :     if (err < 0)
     168           1 :         return AVERROR_INVALIDDATA;
     169             : 
     170         651 :     return get_bits_count(&gb);
     171             : }
     172             : 
     173       22426 : int av_ac3_parse_header(const uint8_t *buf, size_t size,
     174             :                         uint8_t *bitstream_id, uint16_t *frame_size)
     175             : {
     176             :     GetBitContext gb;
     177             :     AC3HeaderInfo hdr;
     178             :     int err;
     179             : 
     180       22426 :     init_get_bits8(&gb, buf, size);
     181       22426 :     err = ff_ac3_parse_header(&gb, &hdr);
     182       22426 :     if (err < 0)
     183       15196 :         return AVERROR_INVALIDDATA;
     184             : 
     185        7230 :     *bitstream_id = hdr.bitstream_id;
     186        7230 :     *frame_size   = hdr.frame_size;
     187             : 
     188        7230 :     return 0;
     189             : }
     190             : 
     191       24950 : static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
     192             :         int *need_next_header, int *new_frame_start)
     193             : {
     194             :     int err;
     195             :     union {
     196             :         uint64_t u64;
     197             :         uint8_t  u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
     198       24950 :     } tmp = { av_be2ne64(state) };
     199             :     AC3HeaderInfo hdr;
     200             :     GetBitContext gbc;
     201             : 
     202       24950 :     init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
     203       24950 :     err = ff_ac3_parse_header(&gbc, &hdr);
     204             : 
     205       24950 :     if(err < 0)
     206       21436 :         return 0;
     207             : 
     208        3514 :     hdr_info->sample_rate = hdr.sample_rate;
     209        3514 :     hdr_info->bit_rate = hdr.bit_rate;
     210        3514 :     hdr_info->channels = hdr.channels;
     211        3514 :     hdr_info->channel_layout = hdr.channel_layout;
     212        3514 :     hdr_info->samples = hdr.num_blocks * 256;
     213        3514 :     hdr_info->service_type = hdr.bitstream_mode;
     214        3514 :     if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
     215           0 :         hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
     216        3514 :     if(hdr.bitstream_id>10)
     217        2337 :         hdr_info->codec_id = AV_CODEC_ID_EAC3;
     218        1177 :     else if (hdr_info->codec_id == AV_CODEC_ID_NONE)
     219          26 :         hdr_info->codec_id = AV_CODEC_ID_AC3;
     220             : 
     221        3514 :     *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
     222        3514 :     *new_frame_start  = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
     223        3514 :     return hdr.frame_size;
     224             : }
     225             : 
     226          37 : static av_cold int ac3_parse_init(AVCodecParserContext *s1)
     227             : {
     228          37 :     AACAC3ParseContext *s = s1->priv_data;
     229          37 :     s->header_size = AC3_HEADER_SIZE;
     230          37 :     s->sync = ac3_sync;
     231          37 :     return 0;
     232             : }
     233             : 
     234             : 
     235             : AVCodecParser ff_ac3_parser = {
     236             :     .codec_ids      = { AV_CODEC_ID_AC3, AV_CODEC_ID_EAC3 },
     237             :     .priv_data_size = sizeof(AACAC3ParseContext),
     238             :     .parser_init    = ac3_parse_init,
     239             :     .parser_parse   = ff_aac_ac3_parse,
     240             :     .parser_close   = ff_parse_close,
     241             : };
     242             : 
     243             : #else
     244             : 
     245             : int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf,
     246             :                             size_t size)
     247             : {
     248             :     return AVERROR(ENOSYS);
     249             : }
     250             : 
     251             : int av_ac3_parse_header(const uint8_t *buf, size_t size,
     252             :                         uint8_t *bitstream_id, uint16_t *frame_size)
     253             : {
     254             :     return AVERROR(ENOSYS);
     255             : }
     256             : #endif

Generated by: LCOV version 1.13