Ticket #13211: mythtv-29-aac-6-channel-audio-backport.patch

File mythtv-29-aac-6-channel-audio-backport.patch, 19.8 KB (added by Bjoern Voigt <bjoernv@…>, 8 years ago)

Patch for MythTV 0.29 for 6 channel AAC playback (backport from FFmpeg 3.3)

  • mythtv/external/FFmpeg/libavcodec/aacdec_template.c

    Patch for bug #13211
    "MythTV needs a backport from FFmpeg 3.3/3.4 to make German DVB-T2 HD with 5.1 audio work"
    https://code.mythtv.org/trac/ticket/13211
    
    diff --git a/mythtv/external/FFmpeg/libavcodec/aacdec_template.c b/mythtv/external/FFmpeg/libavcodec/aacdec_template.c
    index 883ed527f7..f8a8dc4332 100644
    a b static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)  
    406406/**
    407407 * Save current output configuration if and only if it has been locked.
    408408 */
    409 static void push_output_configuration(AACContext *ac) {
     409static int push_output_configuration(AACContext *ac) {
     410    int pushed = 0;
     411
    410412    if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
    411413        ac->oc[0] = ac->oc[1];
     414        pushed = 1;
    412415    }
    413416    ac->oc[1].status = OC_NONE;
     417    return pushed;
    414418}
    415419
    416420/**
    static int output_configure(AACContext *ac,  
    452456        int id =           layout_map[i][1];
    453457        id_map[type][id] = type_counts[type]++;
    454458        if (id_map[type][id] >= MAX_ELEM_ID) {
    455             avpriv_request_sample(ac->avctx, "Remapped id too large\n");
     459            avpriv_request_sample(ac->avctx, "Too large remapped id");
    456460            return AVERROR_PATCHWELCOME;
    457461        }
    458462    }
    static void decode_channel_map(uint8_t layout_map[][3],  
    715719    }
    716720}
    717721
     722static inline void relative_align_get_bits(GetBitContext *gb,
     723                                           int reference_position) {
     724    int n = (reference_position - get_bits_count(gb) & 7);
     725    if (n)
     726        skip_bits(gb, n);
     727}
     728
    718729/**
    719730 * Decode program configuration element; reference: table 4.2.
    720731 *
    static void decode_channel_map(uint8_t layout_map[][3],  
    722733 */
    723734static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
    724735                      uint8_t (*layout_map)[3],
    725                       GetBitContext *gb)
     736                      GetBitContext *gb, int byte_align_ref)
    726737{
    727738    int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
    728739    int sampling_index;
    static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,  
    770781    decode_channel_map(layout_map + tags, AAC_CHANNEL_CC,    gb, num_cc);
    771782    tags += num_cc;
    772783
    773     align_get_bits(gb);
     784    relative_align_get_bits(gb, byte_align_ref);
    774785
    775786    /* comment field, first byte is length */
    776787    comment_len = get_bits(gb, 8) * 8;
    static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,  
    792803 */
    793804static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
    794805                                     GetBitContext *gb,
     806                                     int get_bit_alignment,
    795807                                     MPEG4AudioConfig *m4ac,
    796808                                     int channel_config)
    797809{
    static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,  
    815827
    816828    if (channel_config == 0) {
    817829        skip_bits(gb, 4);  // element_instance_tag
    818         tags = decode_pce(avctx, m4ac, layout_map, gb);
     830        tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
    819831        if (tags < 0)
    820832            return tags;
    821833    } else {
    static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,  
    937949 * @param   ac          pointer to AACContext, may be null
    938950 * @param   avctx       pointer to AVCCodecContext, used for logging
    939951 * @param   m4ac        pointer to MPEG4AudioConfig, used for parsing
    940  * @param   data        pointer to buffer holding an audio specific config
    941  * @param   bit_size    size of audio specific config or data in bits
     952 * @param   gb          buffer holding an audio specific config
     953 * @param   get_bit_alignment relative alignment for byte align operations
    942954 * @param   sync_extension look for an appended sync extension
    943955 *
    944956 * @return  Returns error status or number of consumed bits. <0 - error
    945957 */
    946 static int decode_audio_specific_config(AACContext *ac,
    947                                         AVCodecContext *avctx,
    948                                         MPEG4AudioConfig *m4ac,
    949                                         const uint8_t *data, int64_t bit_size,
    950                                         int sync_extension)
     958static int decode_audio_specific_config_gb(AACContext *ac,
     959                                           AVCodecContext *avctx,
     960                                           MPEG4AudioConfig *m4ac,
     961                                           GetBitContext *gb,
     962                                           int get_bit_alignment,
     963                                           int sync_extension)
    951964{
    952     GetBitContext gb;
    953965    int i, ret;
     966    GetBitContext gbc = *gb;
    954967
    955     if (bit_size < 0 || bit_size > INT_MAX) {
    956         av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
     968    /* function ff_mpeg4audio_get_config_gb is not avaible in FFmpeg 3.2 */
     969    if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension)) < 0)
    957970        return AVERROR_INVALIDDATA;
    958     }
    959 
    960     ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
    961     for (i = 0; i < bit_size >> 3; i++)
    962         ff_dlog(avctx, "%02x ", data[i]);
    963     ff_dlog(avctx, "\n");
    964971
    965     if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
    966         return ret;
    967 
    968     if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
    969                                           sync_extension)) < 0)
    970         return AVERROR_INVALIDDATA;
    971972    if (m4ac->sampling_index > 12) {
    972973        av_log(avctx, AV_LOG_ERROR,
    973974               "invalid sampling rate index %d\n",
    static int decode_audio_specific_config(AACContext *ac,  
    982983        return AVERROR_INVALIDDATA;
    983984    }
    984985
    985     skip_bits_long(&gb, i);
     986    skip_bits_long(gb, i);
    986987
    987988    switch (m4ac->object_type) {
    988989    case AOT_AAC_MAIN:
    static int decode_audio_specific_config(AACContext *ac,  
    990991    case AOT_AAC_LTP:
    991992    case AOT_ER_AAC_LC:
    992993    case AOT_ER_AAC_LD:
    993         if ((ret = decode_ga_specific_config(ac, avctx, &gb,
     994        if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
    994995                                            m4ac, m4ac->chan_config)) < 0)
    995996            return ret;
    996997        break;
    997998    case AOT_ER_AAC_ELD:
    998         if ((ret = decode_eld_specific_config(ac, avctx, &gb,
     999        if ((ret = decode_eld_specific_config(ac, avctx, gb,
    9991000                                              m4ac, m4ac->chan_config)) < 0)
    10001001            return ret;
    10011002        break;
    static int decode_audio_specific_config(AACContext *ac,  
    10131014            m4ac->sample_rate, m4ac->sbr,
    10141015            m4ac->ps);
    10151016
    1016     return get_bits_count(&gb);
     1017    return get_bits_count(gb);
     1018}
     1019
     1020static int decode_audio_specific_config(AACContext *ac,
     1021                                        AVCodecContext *avctx,
     1022                                        MPEG4AudioConfig *m4ac,
     1023                                        const uint8_t *data, int64_t bit_size,
     1024                                        int sync_extension)
     1025{
     1026    int i, ret;
     1027    GetBitContext gb;
     1028
     1029    if (bit_size < 0 || bit_size > INT_MAX) {
     1030        av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
     1031        return AVERROR_INVALIDDATA;
     1032    }
     1033
     1034    ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
     1035    for (i = 0; i < bit_size >> 3; i++)
     1036        ff_dlog(avctx, "%02x ", data[i]);
     1037    ff_dlog(avctx, "\n");
     1038
     1039    if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
     1040        return ret;
     1041
     1042    /* if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, */
     1043    /*                                      sync_extension)) < 0)  */
     1044    /*    return AVERROR_INVALIDDATA;                              */
     1045
     1046    return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
     1047                                           sync_extension);
    10171048}
    10181049
    10191050/**
    static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,  
    12551286    const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
    12561287    const int aot = m4ac->object_type;
    12571288    const int sampling_index = m4ac->sampling_index;
     1289    int ret_fail = AVERROR_INVALIDDATA;
     1290
    12581291    if (aot != AOT_ER_AAC_ELD) {
    12591292        if (get_bits1(gb)) {
    12601293            av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
    static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,  
    13051338                ics->num_swb       =    ff_aac_num_swb_512[sampling_index];
    13061339                ics->tns_max_bands =  ff_tns_max_bands_512[sampling_index];
    13071340            }
    1308             if (!ics->num_swb || !ics->swb_offset)
    1309                 return AVERROR_BUG;
     1341            if (!ics->num_swb || !ics->swb_offset) {
     1342                ret_fail = AVERROR_BUG;
     1343                goto fail;
     1344            }
    13101345        } else {
    13111346            ics->swb_offset    =    ff_swb_offset_1024[sampling_index];
    13121347            ics->num_swb       =   ff_aac_num_swb_1024[sampling_index];
    static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,  
    13301365                if (aot == AOT_ER_AAC_LD) {
    13311366                    av_log(ac->avctx, AV_LOG_ERROR,
    13321367                           "LTP in ER AAC LD not yet implemented.\n");
    1333                     return AVERROR_PATCHWELCOME;
     1368                    ret_fail = AVERROR_PATCHWELCOME;
     1369                    goto fail;
    13341370                }
    13351371                if ((ics->ltp.present = get_bits(gb, 1)))
    13361372                    decode_ltp(&ics->ltp, gb, ics->max_sfb);
    static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,  
    13491385    return 0;
    13501386fail:
    13511387    ics->max_sfb = 0;
    1352     return AVERROR_INVALIDDATA;
     1388    return ret_fail;
    13531389}
    13541390
    13551391/**
    static int decode_ics(AACContext *ac, SingleChannelElement *sce,  
    19361972    global_gain = get_bits(gb, 8);
    19371973
    19381974    if (!common_window && !scale_flag) {
    1939         if (decode_ics_info(ac, ics, gb) < 0)
    1940             return AVERROR_INVALIDDATA;
     1975        ret = decode_ics_info(ac, ics, gb);
     1976        if (ret < 0)
     1977            goto fail;
    19411978    }
    19421979
    19431980    if ((ret = decode_band_types(ac, sce->band_type,
    19441981                                 sce->band_type_run_end, gb, ics)) < 0)
    1945         return ret;
     1982        goto fail;
    19461983    if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
    19471984                                  sce->band_type, sce->band_type_run_end)) < 0)
    1948         return ret;
     1985        goto fail;
    19491986
    19501987    pulse_present = 0;
    19511988    if (!scale_flag) {
    static int decode_ics(AACContext *ac, SingleChannelElement *sce,  
    19531990            if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    19541991                av_log(ac->avctx, AV_LOG_ERROR,
    19551992                       "Pulse tool not allowed in eight short sequence.\n");
    1956                 return AVERROR_INVALIDDATA;
     1993                ret = AVERROR_INVALIDDATA;
     1994                goto fail;
    19571995            }
    19581996            if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
    19591997                av_log(ac->avctx, AV_LOG_ERROR,
    19601998                       "Pulse data corrupt or invalid.\n");
    1961                 return AVERROR_INVALIDDATA;
     1999                ret = AVERROR_INVALIDDATA;
     2000                goto fail;
    19622001            }
    19632002        }
    19642003        tns->present = get_bits1(gb);
    1965         if (tns->present && !er_syntax)
    1966             if (decode_tns(ac, tns, gb, ics) < 0)
    1967                 return AVERROR_INVALIDDATA;
     2004        if (tns->present && !er_syntax) {
     2005            ret = decode_tns(ac, tns, gb, ics);
     2006            if (ret < 0)
     2007                goto fail;
     2008        }
    19682009        if (!eld_syntax && get_bits1(gb)) {
    19692010            avpriv_request_sample(ac->avctx, "SSR");
    1970             return AVERROR_PATCHWELCOME;
     2011            ret = AVERROR_PATCHWELCOME;
     2012            goto fail;
    19712013        }
    19722014        // I see no textual basis in the spec for this occurring after SSR gain
    19732015        // control, but this is what both reference and real implmentations do
    1974         if (tns->present && er_syntax)
    1975             if (decode_tns(ac, tns, gb, ics) < 0)
    1976                 return AVERROR_INVALIDDATA;
     2016        if (tns->present && er_syntax) {
     2017            ret = decode_tns(ac, tns, gb, ics);
     2018            if (ret < 0)
     2019                goto fail;
     2020        }
    19772021    }
    19782022
    1979     if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
    1980                                     &pulse, ics, sce->band_type) < 0)
    1981         return AVERROR_INVALIDDATA;
     2023    ret = decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
     2024                                    &pulse, ics, sce->band_type);
     2025    if (ret < 0)
     2026        goto fail;
    19822027
    19832028    if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
    19842029        apply_prediction(ac, sce);
    19852030
    19862031    return 0;
     2032fail:
     2033    tns->present = 0;
     2034    return ret;
    19872035}
    19882036
    19892037/**
    static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)  
    21552203    coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
    21562204
    21572205    sign  = get_bits(gb, 1);
    2158     scale = AAC_RENAME(cce_scale)[get_bits(gb, 2)];
     2206#if USE_FIXED
     2207    scale = get_bits(gb, 2);
     2208#else
     2209    scale = cce_scale[get_bits(gb, 2)];
     2210#endif
    21592211
    21602212    if ((ret = decode_ics(ac, sce, gb, 0, 0)))
    21612213        return ret;
    static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)  
    21692221            cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
    21702222            gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
    21712223            gain_cache = GET_GAIN(scale, gain);
     2224#if USE_FIXED
     2225            if ((abs(gain_cache)-1024) >> 3 > 30)
     2226                return AVERROR(ERANGE);
     2227#endif
    21722228        }
    21732229        if (coup->coupling_point == AFTER_IMDCT) {
    21742230            coup->gain[c][0] = gain_cache;
    static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)  
    21862242                                    t >>= 1;
    21872243                                }
    21882244                                gain_cache = GET_GAIN(scale, t) * s;
     2245#if USE_FIXED
     2246                                if ((abs(gain_cache)-1024) >> 3 > 30)
     2247                                    return AVERROR(ERANGE);
     2248#endif
    21892249                            }
    21902250                        }
    21912251                        coup->gain[c][idx] = gain_cache;
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    29232983{
    29242984    AACContext *ac = avctx->priv_data;
    29252985    ChannelElement *che = NULL, *che_prev = NULL;
    2926     enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
     2986    enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
    29272987    int err, elem_id;
    29282988    int samples = 0, multiplier, audio_found = 0, pce_found = 0;
    29292989    int is_dmono, sce_count = 0;
     2990    int payload_alignment;
    29302991
    29312992    ac->frame = data;
    29322993
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    29493010    // This may lead to an undefined profile being signaled
    29503011    ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
    29513012
     3013    payload_alignment = get_bits_count(gb);
    29523014    ac->tags_mapped = 0;
    29533015    // parse
    29543016    while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    30023064        case TYPE_PCE: {
    30033065            uint8_t layout_map[MAX_ELEM_ID*4][3];
    30043066            int tags;
    3005             push_output_configuration(ac);
    3006             tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
     3067
     3068            int pushed = push_output_configuration(ac);
     3069            if (pce_found && !pushed) {
     3070                err = AVERROR_INVALIDDATA;
     3071                goto fail;
     3072            }
     3073
     3074            tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
     3075                              payload_alignment);
    30073076            if (tags < 0) {
    30083077                err = tags;
    30093078                break;
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    30113080            if (pce_found) {
    30123081                av_log(avctx, AV_LOG_ERROR,
    30133082                       "Not evaluating a further program_config_element as this construct is dubious at best.\n");
     3083                pop_output_configuration(ac);
    30143084            } else {
    30153085                err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
    30163086                if (!err)
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    30293099                    goto fail;
    30303100            }
    30313101            while (elem_id > 0)
    3032                 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
     3102                elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
    30333103            err = 0; /* FIXME */
    30343104            break;
    30353105
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,  
    30383108            break;
    30393109        }
    30403110
    3041         che_prev       = che;
    3042         elem_type_prev = elem_type;
     3111        if (elem_type < TYPE_DSE) {
     3112            che_prev      = che;
     3113            che_prev_type = elem_type;
     3114        }
    30433115
    30443116        if (err)
    30453117            goto fail;
  • mythtv/external/FFmpeg/libavcodec/mpeg4audio.c

    diff --git a/mythtv/external/FFmpeg/libavcodec/mpeg4audio.c b/mythtv/external/FFmpeg/libavcodec/mpeg4audio.c
    index 188d843eee..84f384eeb6 100644
    a b static inline int get_sample_rate(GetBitContext *gb, int *index)  
    7878        avpriv_mpeg4audio_sample_rates[*index];
    7979}
    8080
     81int ff_mpeg4audio_get_config_gb(MPEG4AudioConfig *c, GetBitContext *gb,
     82                                int sync_extension)
     83{
     84    int specific_config_bitindex, ret;
     85    int start_bit_index = get_bits_count(gb);
     86    c->object_type = get_object_type(gb);
     87    c->sample_rate = get_sample_rate(gb, &c->sampling_index);
     88    c->chan_config = get_bits(gb, 4);
     89    if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels))
     90        c->channels = ff_mpeg4audio_channels[c->chan_config];
     91    c->sbr = -1;
     92    c->ps  = -1;
     93    if (c->object_type == AOT_SBR || (c->object_type == AOT_PS &&
     94        // check for W6132 Annex YYYY draft MP3onMP4
     95        !(show_bits(gb, 3) & 0x03 && !(show_bits(gb, 9) & 0x3F)))) {
     96        if (c->object_type == AOT_PS)
     97            c->ps = 1;
     98        c->ext_object_type = AOT_SBR;
     99        c->sbr = 1;
     100        c->ext_sample_rate = get_sample_rate(gb, &c->ext_sampling_index);
     101        c->object_type = get_object_type(gb);
     102        if (c->object_type == AOT_ER_BSAC)
     103            c->ext_chan_config = get_bits(gb, 4);
     104    } else {
     105        c->ext_object_type = AOT_NULL;
     106        c->ext_sample_rate = 0;
     107    }
     108    specific_config_bitindex = get_bits_count(gb);
     109
     110    if (c->object_type == AOT_ALS) {
     111        skip_bits(gb, 5);
     112        if (show_bits_long(gb, 24) != MKBETAG('\0','A','L','S'))
     113            skip_bits_long(gb, 24);
     114
     115        specific_config_bitindex = get_bits_count(gb);
     116
     117        ret = parse_config_ALS(gb, c);
     118        if (ret < 0)
     119            return ret;
     120    }
     121
     122    if (c->ext_object_type != AOT_SBR && sync_extension) {
     123        while (get_bits_left(gb) > 15) {
     124            if (show_bits(gb, 11) == 0x2b7) { // sync extension
     125                get_bits(gb, 11);
     126                c->ext_object_type = get_object_type(gb);
     127                if (c->ext_object_type == AOT_SBR && (c->sbr = get_bits1(gb)) == 1) {
     128                    c->ext_sample_rate = get_sample_rate(gb, &c->ext_sampling_index);
     129                    if (c->ext_sample_rate == c->sample_rate)
     130                        c->sbr = -1;
     131                }
     132                if (get_bits_left(gb) > 11 && get_bits(gb, 11) == 0x548)
     133                    c->ps = get_bits1(gb);
     134                break;
     135            } else
     136                get_bits1(gb); // skip 1 bit
     137        }
     138    }
     139
     140    //PS requires SBR
     141    if (!c->sbr)
     142        c->ps = 0;
     143    //Limit implicit PS to the HE-AACv2 Profile
     144    if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01)
     145        c->ps = 0;
     146
     147    return specific_config_bitindex - start_bit_index;
     148}
     149
    81150int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf,
    82151                                 int bit_size, int sync_extension)
    83152{