]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/aacenc.c
avcodec/g2meet: Clear pointers after deallocation
[ffmpeg] / libavcodec / aacenc.c
index 897c3a10df225620a236844d9f94ec6b6991c6f8..f05f51b571661e9cb9669ab1c18c47986a83bc5a 100644 (file)
@@ -312,19 +312,26 @@ static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
 static void adjust_frame_information(ChannelElement *cpe, int chans)
 {
     int i, w, w2, g, ch;
-    int start, maxsfb, cmaxsfb;
+    int maxsfb, cmaxsfb;
+    IndividualChannelStream *ics;
 
-    for (ch = 0; ch < chans; ch++) {
-        IndividualChannelStream *ics = &cpe->ch[ch].ics;
-        start = 0;
-        maxsfb = 0;
-        cpe->ch[ch].pulse.num_pulse = 0;
+    if (cpe->common_window) {
+        ics = &cpe->ch[0].ics;
         for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
-            for (w2 = 0; w2 < ics->group_len[w]; w2++) {
-                start = (w+w2) * 128;
+            for (w2 =  0; w2 < ics->group_len[w]; w2++) {
+                int start = (w+w2) * 128;
                 for (g = 0; g < ics->num_swb; g++) {
-                    //apply M/S
-                    if (cpe->common_window && !ch && cpe->ms_mask[w*16 + g]) {
+                    //apply Intensity stereo coeffs transformation
+                    if (cpe->is_mask[w*16 + g]) {
+                        int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
+                        float scale = cpe->ch[0].is_ener[w*16+g];
+                        for (i = 0; i < ics->swb_sizes[g]; i++) {
+                            cpe->ch[0].coeffs[start+i] = (cpe->ch[0].pcoeffs[start+i] + p*cpe->ch[1].pcoeffs[start+i]) * scale;
+                            cpe->ch[1].coeffs[start+i] = 0.0f;
+                        }
+                    } else if (cpe->ms_mask[w*16 + g] &&
+                               cpe->ch[0].band_type[w*16 + g] < NOISE_BT &&
+                               cpe->ch[1].band_type[w*16 + g] < NOISE_BT) {
                         for (i = 0; i < ics->swb_sizes[g]; i++) {
                             cpe->ch[0].coeffs[start+i] = (cpe->ch[0].pcoeffs[start+i] + cpe->ch[1].pcoeffs[start+i]) * 0.5f;
                             cpe->ch[1].coeffs[start+i] = cpe->ch[0].coeffs[start+i] - cpe->ch[1].pcoeffs[start+i];
@@ -332,6 +339,16 @@ static void adjust_frame_information(ChannelElement *cpe, int chans)
                     }
                     start += ics->swb_sizes[g];
                 }
+            }
+        }
+    }
+
+    for (ch = 0; ch < chans; ch++) {
+        IndividualChannelStream *ics = &cpe->ch[ch].ics;
+        maxsfb = 0;
+        cpe->ch[ch].pulse.num_pulse = 0;
+        for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
+            for (w2 =  0; w2 < ics->group_len[w]; w2++) {
                 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
                     ;
                 maxsfb = FFMAX(maxsfb, cmaxsfb);
@@ -389,7 +406,7 @@ static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
                                  SingleChannelElement *sce)
 {
     int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
-    int noise_flag = 1;
+    int off_is = 0, noise_flag = 1;
     int i, w;
 
     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
@@ -402,6 +419,10 @@ static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
                         put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);
                         continue;
                     }
+                } else if (sce->band_type[w*16 + i] == INTENSITY_BT  ||
+                           sce->band_type[w*16 + i] == INTENSITY_BT2) {
+                    diff = sce->sf_idx[w*16 + i] - off_is;
+                    off_is = sce->sf_idx[w*16 + i];
                 } else {
                     diff = sce->sf_idx[w*16 + i] - off_sf;
                     off_sf = sce->sf_idx[w*16 + i];
@@ -529,7 +550,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     AACEncContext *s = avctx->priv_data;
     float **samples = s->planar_samples, *samples2, *la, *overlap;
     ChannelElement *cpe;
-    int i, ch, w, g, chans, tag, start_ch, ret, ms_mode = 0;
+    int i, ch, w, g, chans, tag, start_ch, ret, ms_mode = 0, is_mode = 0;
     int chan_el_counter[4];
     FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
 
@@ -613,6 +634,8 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
             tag      = s->chan_map[i+1];
             chans    = tag == TYPE_CPE ? 2 : 1;
             cpe      = &s->cpe[i];
+            memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
+            memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
             put_bits(&s->pb, 3, tag);
             put_bits(&s->pb, 4, chan_el_counter[tag]++);
             for (ch = 0; ch < chans; ch++)
@@ -635,6 +658,12 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                     }
                 }
             }
+            if (s->options.pns && s->coder->search_for_pns) {
+                for (ch = 0; ch < chans; ch++) {
+                    s->cur_channel = start_ch + ch;
+                    s->coder->search_for_pns(s, avctx, &cpe->ch[ch], s->lambda);
+                }
+            }
             s->cur_channel = start_ch;
             if (s->options.stereo_mode && cpe->common_window) {
                 if (s->options.stereo_mode > 0) {
@@ -646,6 +675,13 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                     s->coder->search_for_ms(s, cpe, s->lambda);
                 }
             }
+            if (chans > 1 && s->options.intensity_stereo && s->coder->search_for_is) {
+                s->coder->search_for_is(s, avctx, cpe, s->lambda);
+                if (cpe->is_mode) is_mode = 1;
+            }
+            if (s->coder->set_special_band_scalefactors)
+                for (ch = 0; ch < chans; ch++)
+                    s->coder->set_special_band_scalefactors(s, &cpe->ch[ch]);
             adjust_frame_information(cpe, chans);
             if (chans == 2) {
                 put_bits(&s->pb, 1, cpe->common_window);
@@ -667,7 +703,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
             s->psy.bitres.bits = frame_bits / s->channels;
             break;
         }
-        if (ms_mode) {
+        if (is_mode || ms_mode) {
             for (i = 0; i < s->chan_map[0]; i++) {
                 // Must restore coeffs
                 chans = tag == TYPE_CPE ? 2 : 1;
@@ -843,8 +879,11 @@ static const AVOption aacenc_options[] = {
         {"twoloop",  "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
         {"fast",     "Constant quantizer",        0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST},    INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
     {"aac_pns", "Perceptual Noise Substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_pns"},
-        {"disable",  "Disable PNS", 0, AV_OPT_TYPE_CONST, {.i64 =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
-        {"enable",   "Enable PNS (Proof of concept)",  0, AV_OPT_TYPE_CONST, {.i64 =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
+        {"disable",  "Disable perceptual noise substitution", 0, AV_OPT_TYPE_CONST, {.i64 =  0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
+        {"enable",   "Enable perceptual noise substitution",  0, AV_OPT_TYPE_CONST, {.i64 =  1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
+    {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "intensity_stereo"},
+        {"disable",  "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
+        {"enable",   "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
     {NULL}
 };