Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * DCA ExSS extension
  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. #include "libavutil/common.h"
  22. #include "libavutil/log.h"
  23.  
  24. #include "dca.h"
  25. #include "dca_syncwords.h"
  26. #include "get_bits.h"
  27.  
  28. /* extensions that reside in core substream */
  29. #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
  30.  
  31. /* these are unconfirmed but should be mostly correct */
  32. enum DCAExSSSpeakerMask {
  33.     DCA_EXSS_FRONT_CENTER          = 0x0001,
  34.     DCA_EXSS_FRONT_LEFT_RIGHT      = 0x0002,
  35.     DCA_EXSS_SIDE_REAR_LEFT_RIGHT  = 0x0004,
  36.     DCA_EXSS_LFE                   = 0x0008,
  37.     DCA_EXSS_REAR_CENTER           = 0x0010,
  38.     DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
  39.     DCA_EXSS_REAR_LEFT_RIGHT       = 0x0040,
  40.     DCA_EXSS_FRONT_HIGH_CENTER     = 0x0080,
  41.     DCA_EXSS_OVERHEAD              = 0x0100,
  42.     DCA_EXSS_CENTER_LEFT_RIGHT     = 0x0200,
  43.     DCA_EXSS_WIDE_LEFT_RIGHT       = 0x0400,
  44.     DCA_EXSS_SIDE_LEFT_RIGHT       = 0x0800,
  45.     DCA_EXSS_LFE2                  = 0x1000,
  46.     DCA_EXSS_SIDE_HIGH_LEFT_RIGHT  = 0x2000,
  47.     DCA_EXSS_REAR_HIGH_CENTER      = 0x4000,
  48.     DCA_EXSS_REAR_HIGH_LEFT_RIGHT  = 0x8000,
  49. };
  50.  
  51. /**
  52.  * Return the number of channels in an ExSS speaker mask (HD)
  53.  */
  54. static int dca_exss_mask2count(int mask)
  55. {
  56.     /* count bits that mean speaker pairs twice */
  57.     return av_popcount(mask) +
  58.            av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT      |
  59.                                DCA_EXSS_FRONT_LEFT_RIGHT       |
  60.                                DCA_EXSS_FRONT_HIGH_LEFT_RIGHT  |
  61.                                DCA_EXSS_WIDE_LEFT_RIGHT        |
  62.                                DCA_EXSS_SIDE_LEFT_RIGHT        |
  63.                                DCA_EXSS_SIDE_HIGH_LEFT_RIGHT   |
  64.                                DCA_EXSS_SIDE_REAR_LEFT_RIGHT   |
  65.                                DCA_EXSS_REAR_LEFT_RIGHT        |
  66.                                DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
  67. }
  68.  
  69. /**
  70.  * Skip mixing coefficients of a single mix out configuration (HD)
  71.  */
  72. static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
  73. {
  74.     int i;
  75.  
  76.     for (i = 0; i < channels; i++) {
  77.         int mix_map_mask = get_bits(gb, out_ch);
  78.         int num_coeffs = av_popcount(mix_map_mask);
  79.         skip_bits_long(gb, num_coeffs * 6);
  80.     }
  81. }
  82.  
  83. /**
  84.  * Parse extension substream asset header (HD)
  85.  */
  86. static int dca_exss_parse_asset_header(DCAContext *s)
  87. {
  88.     int header_pos = get_bits_count(&s->gb);
  89.     int header_size;
  90.     int channels = 0;
  91.     int embedded_stereo = 0;
  92.     int embedded_6ch    = 0;
  93.     int drc_code_present;
  94.     int extensions_mask = 0;
  95.     int i, j;
  96.  
  97.     if (get_bits_left(&s->gb) < 16)
  98.         return AVERROR_INVALIDDATA;
  99.  
  100.     /* We will parse just enough to get to the extensions bitmask with which
  101.      * we can set the profile value. */
  102.  
  103.     header_size = get_bits(&s->gb, 9) + 1;
  104.     skip_bits(&s->gb, 3); // asset index
  105.  
  106.     if (s->static_fields) {
  107.         if (get_bits1(&s->gb))
  108.             skip_bits(&s->gb, 4); // asset type descriptor
  109.         if (get_bits1(&s->gb))
  110.             skip_bits_long(&s->gb, 24); // language descriptor
  111.  
  112.         if (get_bits1(&s->gb)) {
  113.             /* How can one fit 1024 bytes of text here if the maximum value
  114.              * for the asset header size field above was 512 bytes? */
  115.             int text_length = get_bits(&s->gb, 10) + 1;
  116.             if (get_bits_left(&s->gb) < text_length * 8)
  117.                 return AVERROR_INVALIDDATA;
  118.             skip_bits_long(&s->gb, text_length * 8); // info text
  119.         }
  120.  
  121.         skip_bits(&s->gb, 5); // bit resolution - 1
  122.         skip_bits(&s->gb, 4); // max sample rate code
  123.         channels = get_bits(&s->gb, 8) + 1;
  124.  
  125.         s->one2one_map_chtospkr = get_bits1(&s->gb);
  126.         if (s->one2one_map_chtospkr) {
  127.             int spkr_remap_sets;
  128.             int spkr_mask_size = 16;
  129.             int num_spkrs[7];
  130.  
  131.             if (channels > 2)
  132.                 embedded_stereo = get_bits1(&s->gb);
  133.             if (channels > 6)
  134.                 embedded_6ch = get_bits1(&s->gb);
  135.  
  136.             if (get_bits1(&s->gb)) {
  137.                 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  138.                 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
  139.             }
  140.  
  141.             spkr_remap_sets = get_bits(&s->gb, 3);
  142.  
  143.             for (i = 0; i < spkr_remap_sets; i++) {
  144.                 /* std layout mask for each remap set */
  145.                 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
  146.             }
  147.  
  148.             for (i = 0; i < spkr_remap_sets; i++) {
  149.                 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
  150.                 if (get_bits_left(&s->gb) < 0)
  151.                     return AVERROR_INVALIDDATA;
  152.  
  153.                 for (j = 0; j < num_spkrs[i]; j++) {
  154.                     int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
  155.                     int num_dec_ch = av_popcount(remap_dec_ch_mask);
  156.                     skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
  157.                 }
  158.             }
  159.         } else {
  160.             skip_bits(&s->gb, 3); // representation type
  161.         }
  162.     }
  163.  
  164.     drc_code_present = get_bits1(&s->gb);
  165.     if (drc_code_present)
  166.         get_bits(&s->gb, 8); // drc code
  167.  
  168.     if (get_bits1(&s->gb))
  169.         skip_bits(&s->gb, 5); // dialog normalization code
  170.  
  171.     if (drc_code_present && embedded_stereo)
  172.         get_bits(&s->gb, 8); // drc stereo code
  173.  
  174.     if (s->mix_metadata && get_bits1(&s->gb)) {
  175.         skip_bits(&s->gb, 1); // external mix
  176.         skip_bits(&s->gb, 6); // post mix gain code
  177.  
  178.         if (get_bits(&s->gb, 2) != 3) // mixer drc code
  179.             skip_bits(&s->gb, 3); // drc limit
  180.         else
  181.             skip_bits(&s->gb, 8); // custom drc code
  182.  
  183.         if (get_bits1(&s->gb)) // channel specific scaling
  184.             for (i = 0; i < s->num_mix_configs; i++)
  185.                 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
  186.         else
  187.             skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
  188.  
  189.         for (i = 0; i < s->num_mix_configs; i++) {
  190.             if (get_bits_left(&s->gb) < 0)
  191.                 return AVERROR_INVALIDDATA;
  192.             dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
  193.             if (embedded_6ch)
  194.                 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
  195.             if (embedded_stereo)
  196.                 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
  197.         }
  198.     }
  199.  
  200.     switch (get_bits(&s->gb, 2)) {
  201.     case 0:
  202.         extensions_mask = get_bits(&s->gb, 12);
  203.         break;
  204.     case 1:
  205.         extensions_mask = DCA_EXT_EXSS_XLL;
  206.         break;
  207.     case 2:
  208.         extensions_mask = DCA_EXT_EXSS_LBR;
  209.         break;
  210.     case 3:
  211.         extensions_mask = 0; /* aux coding */
  212.         break;
  213.     }
  214.  
  215.     /* not parsed further, we were only interested in the extensions mask */
  216.  
  217.     if (get_bits_left(&s->gb) < 0)
  218.         return AVERROR_INVALIDDATA;
  219.  
  220.     if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
  221.         av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
  222.         return AVERROR_INVALIDDATA;
  223.     }
  224.     skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
  225.  
  226.     if (extensions_mask & DCA_EXT_EXSS_XLL)
  227.         s->profile = FF_PROFILE_DTS_HD_MA;
  228.     else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
  229.                                 DCA_EXT_EXSS_XXCH))
  230.         s->profile = FF_PROFILE_DTS_HD_HRA;
  231.  
  232.     if (!(extensions_mask & DCA_EXT_CORE))
  233.         av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
  234.     if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
  235.         av_log(s->avctx, AV_LOG_WARNING,
  236.                "DTS extensions detection mismatch (%d, %d)\n",
  237.                extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
  238.  
  239.     return 0;
  240. }
  241.  
  242. /**
  243.  * Parse extension substream header (HD)
  244.  */
  245. void ff_dca_exss_parse_header(DCAContext *s)
  246. {
  247.     int asset_size[8];
  248.     int ss_index;
  249.     int blownup;
  250.     int num_audiop = 1;
  251.     int num_assets = 1;
  252.     int active_ss_mask[8];
  253.     int i, j;
  254.     int start_pos;
  255.     int hdrsize;
  256.     uint32_t mkr;
  257.  
  258.     if (get_bits_left(&s->gb) < 52)
  259.         return;
  260.  
  261.     start_pos = get_bits_count(&s->gb) - 32;
  262.  
  263.     skip_bits(&s->gb, 8); // user data
  264.     ss_index = get_bits(&s->gb, 2);
  265.  
  266.     blownup = get_bits1(&s->gb);
  267.     hdrsize = get_bits(&s->gb,  8 + 4 * blownup) + 1; // header_size
  268.     skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
  269.  
  270.     s->static_fields = get_bits1(&s->gb);
  271.     if (s->static_fields) {
  272.         skip_bits(&s->gb, 2); // reference clock code
  273.         skip_bits(&s->gb, 3); // frame duration code
  274.  
  275.         if (get_bits1(&s->gb))
  276.             skip_bits_long(&s->gb, 36); // timestamp
  277.  
  278.         /* a single stream can contain multiple audio assets that can be
  279.          * combined to form multiple audio presentations */
  280.  
  281.         num_audiop = get_bits(&s->gb, 3) + 1;
  282.         if (num_audiop > 1) {
  283.             avpriv_request_sample(s->avctx,
  284.                                   "Multiple DTS-HD audio presentations");
  285.             /* ignore such streams for now */
  286.             return;
  287.         }
  288.  
  289.         num_assets = get_bits(&s->gb, 3) + 1;
  290.         if (num_assets > 1) {
  291.             avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
  292.             /* ignore such streams for now */
  293.             return;
  294.         }
  295.  
  296.         for (i = 0; i < num_audiop; i++)
  297.             active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
  298.  
  299.         for (i = 0; i < num_audiop; i++)
  300.             for (j = 0; j <= ss_index; j++)
  301.                 if (active_ss_mask[i] & (1 << j))
  302.                     skip_bits(&s->gb, 8); // active asset mask
  303.  
  304.         s->mix_metadata = get_bits1(&s->gb);
  305.         if (s->mix_metadata) {
  306.             int mix_out_mask_size;
  307.  
  308.             skip_bits(&s->gb, 2); // adjustment level
  309.             mix_out_mask_size  = (get_bits(&s->gb, 2) + 1) << 2;
  310.             s->num_mix_configs =  get_bits(&s->gb, 2) + 1;
  311.  
  312.             for (i = 0; i < s->num_mix_configs; i++) {
  313.                 int mix_out_mask        = get_bits(&s->gb, mix_out_mask_size);
  314.                 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
  315.             }
  316.         }
  317.     }
  318.  
  319.     av_assert0(num_assets > 0); // silence a warning
  320.  
  321.     for (i = 0; i < num_assets; i++)
  322.         asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup) + 1;
  323.  
  324.     for (i = 0; i < num_assets; i++) {
  325.         if (dca_exss_parse_asset_header(s))
  326.             return;
  327.     }
  328.  
  329.         j = get_bits_count(&s->gb);
  330.         if (start_pos + hdrsize * 8 > j)
  331.             skip_bits_long(&s->gb, start_pos + hdrsize * 8 - j);
  332.  
  333.         for (i = 0; i < num_assets; i++) {
  334.             int end_pos;
  335.             start_pos = get_bits_count(&s->gb);
  336.             end_pos   = start_pos + asset_size[i] * 8;
  337.             mkr       = get_bits_long(&s->gb, 32);
  338.  
  339.             /* parse extensions that we know about */
  340.             switch (mkr) {
  341.             case DCA_SYNCWORD_XBR:
  342.                 ff_dca_xbr_parse_frame(s);
  343.                 break;
  344.             case DCA_SYNCWORD_XXCH:
  345.                 ff_dca_xxch_decode_frame(s);
  346.                 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
  347.                 break;
  348.             case DCA_SYNCWORD_XLL:
  349.                 if (s->xll_disable) {
  350.                     av_log(s->avctx, AV_LOG_DEBUG,
  351.                            "DTS-XLL: ignoring XLL extension\n");
  352.                     break;
  353.                 }
  354.                 av_log(s->avctx, AV_LOG_DEBUG,
  355.                        "DTS-XLL: decoding XLL extension\n");
  356.                 if (ff_dca_xll_decode_header(s)        == 0 &&
  357.                     ff_dca_xll_decode_navi(s, end_pos) == 0)
  358.                     s->exss_ext_mask |= DCA_EXT_EXSS_XLL;
  359.                 break;
  360.             default:
  361.                 av_log(s->avctx, AV_LOG_DEBUG,
  362.                        "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
  363.             }
  364.  
  365.             /* skip to end of block */
  366.             j = get_bits_count(&s->gb);
  367.             if (j > end_pos)
  368.                 av_log(s->avctx, AV_LOG_ERROR,
  369.                        "DTS-ExSS: Processed asset too long.\n");
  370.             if (j < end_pos)
  371.                 skip_bits_long(&s->gb, end_pos - j);
  372.         }
  373. }
  374.