]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/aacdec.c
Merge commit 'fb1473080223a634b8ac2cca48a632d037a0a69d'
[ffmpeg] / libavcodec / aacdec.c
index 48cf637b796e67863115ab69334ce29f0d4cd647..622cc5c087a2799f5061c50d5e2ab8993833564d 100644 (file)
@@ -303,6 +303,11 @@ static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
     if (num_back_channels < 0)
         return 0;
 
+    if (num_side_channels == 0 && num_back_channels >= 4) {
+        num_side_channels = 2;
+        num_back_channels -= 2;
+    }
+
     i = 0;
     if (num_front_channels & 1) {
         e2c_vec[i] = (struct elem_to_channel) {
@@ -457,12 +462,18 @@ static int output_configure(AACContext *ac,
     AVCodecContext *avctx = ac->avctx;
     int i, channels = 0, ret;
     uint64_t layout = 0;
+    uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
+    uint8_t type_counts[TYPE_END] = { 0 };
 
     if (ac->oc[1].layout_map != layout_map) {
         memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
         ac->oc[1].layout_map_tags = tags;
     }
-
+    for (i = 0; i < tags; i++) {
+        int type =         layout_map[i][0];
+        int id =           layout_map[i][1];
+        id_map[type][id] = type_counts[type]++;
+    }
     // Try to sniff a reasonable channel order, otherwise output the
     // channels in the order the PCE declared them.
     if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
@@ -470,12 +481,14 @@ static int output_configure(AACContext *ac,
     for (i = 0; i < tags; i++) {
         int type =     layout_map[i][0];
         int id =       layout_map[i][1];
+        int iid =      id_map[type][id];
         int position = layout_map[i][2];
         // Allocate or free elements depending on if they are in the
         // current program configuration.
-        ret = che_configure(ac, position, type, id, &channels);
+        ret = che_configure(ac, position, type, iid, &channels);
         if (ret < 0)
             return ret;
+        ac->tag_che_map[type][id] = ac->che[type][iid];
     }
     if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
         if (layout == AV_CH_FRONT_CENTER) {
@@ -485,7 +498,6 @@ static int output_configure(AACContext *ac,
         }
     }
 
-    memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
     if (layout) avctx->channel_layout = layout;
                             ac->oc[1].channel_layout = layout;
     avctx->channels       = ac->oc[1].channels       = channels;
@@ -527,7 +539,8 @@ static int set_default_channel_config(AVCodecContext *avctx,
                                       int *tags,
                                       int channel_config)
 {
-    if (channel_config < 1 || channel_config > 7) {
+    if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
+        channel_config > 12) {
         av_log(avctx, AV_LOG_ERROR,
                "invalid default channel configuration (%d)\n",
                channel_config);
@@ -607,11 +620,19 @@ static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
     /* For indexed channel configurations map the channels solely based
      * on position. */
     switch (ac->oc[1].m4ac.chan_config) {
+    case 12:
     case 7:
         if (ac->tags_mapped == 3 && type == TYPE_CPE) {
             ac->tags_mapped++;
             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
         }
+    case 11:
+        if (ac->tags_mapped == 2 &&
+            ac->oc[1].m4ac.chan_config == 11 &&
+            type == TYPE_SCE) {
+            ac->tags_mapped++;
+            return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
+        }
     case 6:
         /* Some streams incorrectly code 5.1 audio as
          * SCE[0] CPE[0] CPE[1] SCE[1]
@@ -2885,7 +2906,7 @@ static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
 
     ac->tags_mapped = 0;
 
-    if (chan_config < 0 || chan_config >= 8) {
+    if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
         avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
                               chan_config);
         return AVERROR_INVALIDDATA;
@@ -2966,6 +2987,11 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
         if (avctx->debug & FF_DEBUG_STARTCODE)
             av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
 
+        if (!avctx->channels && elem_type != TYPE_PCE) {
+            err = AVERROR_INVALIDDATA;
+            goto fail;
+        }
+
         if (elem_type < TYPE_DSE) {
             if (!(che=get_che(ac, elem_type, elem_id))) {
                 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
@@ -3055,6 +3081,11 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
         }
     }
 
+    if (!avctx->channels) {
+        *got_frame_ptr = 0;
+        return 0;
+    }
+
     spectral_to_sample(ac);
 
     multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;