LCOV - code coverage report
Current view: top level - libavcodec - mlp_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 131 157 83.4 %
Date: 2017-12-18 06:23:41 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * MLP parser
       3             :  * Copyright (c) 2007 Ian Caulfield
       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             :  * MLP parser
      25             :  */
      26             : 
      27             : #include <stdint.h>
      28             : 
      29             : #include "libavutil/channel_layout.h"
      30             : #include "libavutil/crc.h"
      31             : #include "libavutil/internal.h"
      32             : #include "get_bits.h"
      33             : #include "parser.h"
      34             : #include "mlp_parser.h"
      35             : #include "mlp.h"
      36             : 
      37             : static const uint8_t mlp_quants[16] = {
      38             :     16, 20, 24, 0, 0, 0, 0, 0,
      39             :      0,  0,  0, 0, 0, 0, 0, 0,
      40             : };
      41             : 
      42             : static const uint8_t mlp_channels[32] = {
      43             :     1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
      44             :     5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      45             : };
      46             : 
      47             : const uint64_t ff_mlp_layout[32] = {
      48             :     AV_CH_LAYOUT_MONO,
      49             :     AV_CH_LAYOUT_STEREO,
      50             :     AV_CH_LAYOUT_2_1,
      51             :     AV_CH_LAYOUT_QUAD,
      52             :     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
      53             :     AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
      54             :     AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
      55             :     AV_CH_LAYOUT_SURROUND,
      56             :     AV_CH_LAYOUT_4POINT0,
      57             :     AV_CH_LAYOUT_5POINT0_BACK,
      58             :     AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
      59             :     AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
      60             :     AV_CH_LAYOUT_5POINT1_BACK,
      61             :     AV_CH_LAYOUT_4POINT0,
      62             :     AV_CH_LAYOUT_5POINT0_BACK,
      63             :     AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
      64             :     AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
      65             :     AV_CH_LAYOUT_5POINT1_BACK,
      66             :     AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
      67             :     AV_CH_LAYOUT_5POINT0_BACK,
      68             :     AV_CH_LAYOUT_5POINT1_BACK,
      69             :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
      70             : };
      71             : 
      72             : static const uint8_t thd_chancount[13] = {
      73             : //  LR    C   LFE  LRs LRvh  LRc LRrs  Cs   Ts  LRsd  LRw  Cvh  LFE2
      74             :      2,   1,   1,   2,   2,   2,   2,   1,   1,   2,   2,   1,   1
      75             : };
      76             : 
      77             : static const uint64_t thd_layout[13] = {
      78             :     AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT,                     // LR
      79             :     AV_CH_FRONT_CENTER,                                     // C
      80             :     AV_CH_LOW_FREQUENCY,                                    // LFE
      81             :     AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,                       // LRs
      82             :     AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT,             // LRvh
      83             :     AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRc
      84             :     AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,                       // LRrs
      85             :     AV_CH_BACK_CENTER,                                      // Cs
      86             :     AV_CH_TOP_CENTER,                                       // Ts
      87             :     AV_CH_SURROUND_DIRECT_LEFT|AV_CH_SURROUND_DIRECT_RIGHT, // LRsd
      88             :     AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT,                       // LRw
      89             :     AV_CH_TOP_FRONT_CENTER,                                 // Cvh
      90             :     AV_CH_LOW_FREQUENCY_2,                                  // LFE2
      91             : };
      92             : 
      93        5316 : static int mlp_samplerate(int in)
      94             : {
      95        5316 :     if (in == 0xF)
      96        2230 :         return 0;
      97             : 
      98        3086 :     return (in & 8 ? 44100 : 48000) << (in & 7) ;
      99             : }
     100             : 
     101        1712 : static int truehd_channels(int chanmap)
     102             : {
     103        1712 :     int channels = 0, i;
     104             : 
     105       23968 :     for (i = 0; i < 13; i++)
     106       22256 :         channels += thd_chancount[i] * ((chanmap >> i) & 1);
     107             : 
     108        1712 :     return channels;
     109             : }
     110             : 
     111        1712 : uint64_t ff_truehd_layout(int chanmap)
     112             : {
     113             :     int i;
     114        1712 :     uint64_t layout = 0;
     115             : 
     116       23968 :     for (i = 0; i < 13; i++)
     117       22256 :         layout |= thd_layout[i] * ((chanmap >> i) & 1);
     118             : 
     119        1712 :     return layout;
     120             : }
     121             : 
     122        3086 : static int mlp_get_major_sync_size(const uint8_t * buf, int bufsize)
     123             : {
     124        3086 :     int has_extension, extensions = 0;
     125        3086 :     int size = 28;
     126        3086 :     if (bufsize < 28)
     127           0 :         return -1;
     128             : 
     129        3086 :     if (AV_RB32(buf) == 0xf8726fba) {
     130         856 :         has_extension = buf[25] & 1;
     131         856 :         if (has_extension) {
     132           0 :             extensions = buf[26] >> 4;
     133           0 :             size += 2 + extensions * 2;
     134             :         }
     135             :     }
     136        3086 :     return size;
     137             : }
     138             : 
     139             : /** Read a major sync info header - contains high level information about
     140             :  *  the stream - sample rate, channel arrangement etc. Most of this
     141             :  *  information is not actually necessary for decoding, only for playback.
     142             :  *  gb must be a freshly initialized GetBitContext with no bits read.
     143             :  */
     144             : 
     145        3086 : int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
     146             : {
     147             :     int ratebits, channel_arrangement, header_size;
     148             :     uint16_t checksum;
     149             : 
     150             :     av_assert1(get_bits_count(gb) == 0);
     151             : 
     152        3086 :     header_size = mlp_get_major_sync_size(gb->buffer, gb->size_in_bits >> 3);
     153        3086 :     if (header_size < 0 || gb->size_in_bits < header_size << 3) {
     154           0 :         av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
     155           0 :         return -1;
     156             :     }
     157             : 
     158        3086 :     checksum = ff_mlp_checksum16(gb->buffer, header_size - 2);
     159        3086 :     if (checksum != AV_RL16(gb->buffer+header_size-2)) {
     160           0 :         av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
     161           0 :         return AVERROR_INVALIDDATA;
     162             :     }
     163             : 
     164        3086 :     if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
     165           0 :         return AVERROR_INVALIDDATA;
     166             : 
     167        3086 :     mh->stream_type = get_bits(gb, 8);
     168        3086 :     mh->header_size = header_size;
     169             : 
     170        3086 :     if (mh->stream_type == 0xbb) {
     171        2230 :         mh->group1_bits = mlp_quants[get_bits(gb, 4)];
     172        2230 :         mh->group2_bits = mlp_quants[get_bits(gb, 4)];
     173             : 
     174        2230 :         ratebits = get_bits(gb, 4);
     175        2230 :         mh->group1_samplerate = mlp_samplerate(ratebits);
     176        2230 :         mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
     177             : 
     178        2230 :         skip_bits(gb, 11);
     179             : 
     180        2230 :         mh->channel_arrangement=
     181        2230 :         channel_arrangement    = get_bits(gb, 5);
     182        2230 :         mh->channels_mlp       = mlp_channels[channel_arrangement];
     183        2230 :         mh->channel_layout_mlp = ff_mlp_layout[channel_arrangement];
     184         856 :     } else if (mh->stream_type == 0xba) {
     185         856 :         mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
     186         856 :         mh->group2_bits = 0;
     187             : 
     188         856 :         ratebits = get_bits(gb, 4);
     189         856 :         mh->group1_samplerate = mlp_samplerate(ratebits);
     190         856 :         mh->group2_samplerate = 0;
     191             : 
     192         856 :         skip_bits(gb, 4);
     193             : 
     194         856 :         mh->channel_modifier_thd_stream0 = get_bits(gb, 2);
     195         856 :         mh->channel_modifier_thd_stream1 = get_bits(gb, 2);
     196             : 
     197         856 :         mh->channel_arrangement=
     198         856 :         channel_arrangement            = get_bits(gb, 5);
     199         856 :         mh->channels_thd_stream1       = truehd_channels(channel_arrangement);
     200         856 :         mh->channel_layout_thd_stream1 = ff_truehd_layout(channel_arrangement);
     201             : 
     202         856 :         mh->channel_modifier_thd_stream2 = get_bits(gb, 2);
     203             : 
     204         856 :         channel_arrangement            = get_bits(gb, 13);
     205         856 :         mh->channels_thd_stream2       = truehd_channels(channel_arrangement);
     206         856 :         mh->channel_layout_thd_stream2 = ff_truehd_layout(channel_arrangement);
     207             :     } else
     208           0 :         return AVERROR_INVALIDDATA;
     209             : 
     210        3086 :     mh->access_unit_size = 40 << (ratebits & 7);
     211        3086 :     mh->access_unit_size_pow2 = 64 << (ratebits & 7);
     212             : 
     213        3086 :     skip_bits_long(gb, 48);
     214             : 
     215        3086 :     mh->is_vbr = get_bits1(gb);
     216             : 
     217        3086 :     mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
     218             : 
     219        3086 :     mh->num_substreams = get_bits(gb, 4);
     220             : 
     221        3086 :     skip_bits_long(gb, 4 + (header_size - 17) * 8);
     222             : 
     223        3086 :     return 0;
     224             : }
     225             : 
     226             : typedef struct MLPParseContext
     227             : {
     228             :     ParseContext pc;
     229             : 
     230             :     int bytes_left;
     231             : 
     232             :     int in_sync;
     233             : 
     234             :     int num_substreams;
     235             : } MLPParseContext;
     236             : 
     237           3 : static av_cold int mlp_init(AVCodecParserContext *s)
     238             : {
     239           3 :     ff_mlp_init_crc();
     240           3 :     return 0;
     241             : }
     242             : 
     243       18076 : static int mlp_parse(AVCodecParserContext *s,
     244             :                      AVCodecContext *avctx,
     245             :                      const uint8_t **poutbuf, int *poutbuf_size,
     246             :                      const uint8_t *buf, int buf_size)
     247             : {
     248       18076 :     MLPParseContext *mp = s->priv_data;
     249             :     int sync_present;
     250             :     uint8_t parity_bits;
     251             :     int next;
     252             :     int ret;
     253       18076 :     int i, p = 0;
     254             : 
     255       18076 :     *poutbuf_size = 0;
     256       18076 :     if (buf_size == 0)
     257           3 :         return 0;
     258             : 
     259       18073 :     if (!mp->in_sync) {
     260             :         // Not in sync - find a major sync header
     261             : 
     262        4744 :         for (i = 0; i < buf_size; i++) {
     263        4740 :             mp->pc.state = (mp->pc.state << 8) | buf[i];
     264        4743 :             if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
     265             :                 // ignore if we do not have the data for the start of header
     266           3 :                 mp->pc.index + i >= 7) {
     267           3 :                 mp->in_sync = 1;
     268           3 :                 mp->bytes_left = 0;
     269           3 :                 break;
     270             :             }
     271             :         }
     272             : 
     273           7 :         if (!mp->in_sync) {
     274           4 :             if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
     275           0 :                 av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
     276           4 :             return buf_size;
     277             :         }
     278             : 
     279           3 :         if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
     280           0 :             av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
     281           0 :             return ret;
     282             :         }
     283             : 
     284           3 :         return i - 7;
     285             :     }
     286             : 
     287       18066 :     if (mp->bytes_left == 0) {
     288             :         // Find length of this packet
     289             : 
     290             :         /* Copy overread bytes from last frame into buffer. */
     291       15790 :         for(; mp->pc.overread>0; mp->pc.overread--) {
     292           0 :             mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
     293             :         }
     294             : 
     295       15790 :         if (mp->pc.index + buf_size < 2) {
     296           0 :             if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
     297           0 :                 av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
     298           0 :             return buf_size;
     299             :         }
     300             : 
     301       31580 :         mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
     302       15790 :                        |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
     303       15790 :         mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
     304       15790 :         if (mp->bytes_left <= 0) { // prevent infinite loop
     305           0 :             goto lost_sync;
     306             :         }
     307       15790 :         mp->bytes_left -= mp->pc.index;
     308             :     }
     309             : 
     310       18066 :     next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
     311             : 
     312       18066 :     if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
     313        2279 :         mp->bytes_left -= buf_size;
     314        2279 :         return buf_size;
     315             :     }
     316             : 
     317       15787 :     mp->bytes_left = 0;
     318             : 
     319       15787 :     sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
     320             : 
     321       15787 :     if (!sync_present) {
     322             :         /* The first nibble of a frame is a parity check of the 4-byte
     323             :          * access unit header and all the 2- or 4-byte substream headers. */
     324             :         // Only check when this isn't a sync frame - syncs have a checksum.
     325             : 
     326       14244 :         parity_bits = 0;
     327       49124 :         for (i = -1; i < mp->num_substreams; i++) {
     328       34880 :             parity_bits ^= buf[p++];
     329       34880 :             parity_bits ^= buf[p++];
     330             : 
     331       34880 :             if (i < 0 || buf[p-2] & 0x80) {
     332       14348 :                 parity_bits ^= buf[p++];
     333       14348 :                 parity_bits ^= buf[p++];
     334             :             }
     335             :         }
     336             : 
     337       14244 :         if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
     338           0 :             av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
     339           0 :             goto lost_sync;
     340             :         }
     341             :     } else {
     342             :         GetBitContext gb;
     343             :         MLPHeaderInfo mh;
     344             : 
     345        1543 :         init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
     346        1543 :         if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
     347           0 :             goto lost_sync;
     348             : 
     349        1543 :         avctx->bits_per_raw_sample = mh.group1_bits;
     350        1543 :         if (avctx->bits_per_raw_sample > 16)
     351         428 :             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
     352             :         else
     353        1115 :             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
     354        1543 :         avctx->sample_rate = mh.group1_samplerate;
     355        1543 :         s->duration = mh.access_unit_size;
     356             : 
     357        1543 :         if(!avctx->channels || !avctx->channel_layout) {
     358           3 :         if (mh.stream_type == 0xbb) {
     359             :             /* MLP stream */
     360           1 :             avctx->channels       = mh.channels_mlp;
     361           1 :             avctx->channel_layout = mh.channel_layout_mlp;
     362             :         } else { /* mh.stream_type == 0xba */
     363             :             /* TrueHD stream */
     364           2 :             if (!mh.channels_thd_stream2) {
     365           0 :                 avctx->channels       = mh.channels_thd_stream1;
     366           0 :                 avctx->channel_layout = mh.channel_layout_thd_stream1;
     367             :             } else {
     368           2 :                 avctx->channels       = mh.channels_thd_stream2;
     369           2 :                 avctx->channel_layout = mh.channel_layout_thd_stream2;
     370             :             }
     371             :         }
     372             :         }
     373             : 
     374        1543 :         if (!mh.is_vbr) /* Stream is CBR */
     375           0 :             avctx->bit_rate = mh.peak_bitrate;
     376             : 
     377        1543 :         mp->num_substreams = mh.num_substreams;
     378             :     }
     379             : 
     380       15787 :     *poutbuf = buf;
     381       15787 :     *poutbuf_size = buf_size;
     382             : 
     383       15787 :     return next;
     384             : 
     385           0 : lost_sync:
     386           0 :     mp->in_sync = 0;
     387           0 :     return 1;
     388             : }
     389             : 
     390             : AVCodecParser ff_mlp_parser = {
     391             :     .codec_ids      = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
     392             :     .priv_data_size = sizeof(MLPParseContext),
     393             :     .parser_init    = mlp_init,
     394             :     .parser_parse   = mlp_parse,
     395             :     .parser_close   = ff_parse_close,
     396             : };

Generated by: LCOV version 1.13