Browse code

aacenc: use AVCodec.encode2()

Justin Ruggles authored on 2012/01/29 02:28:01
Showing 3 changed files
... ...
@@ -57,7 +57,8 @@ OBJS-$(CONFIG_AAC_DECODER)             += aacdec.o aactab.o aacsbr.o aacps.o \
57 57
 OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o    \
58 58
                                           aacpsy.o aactab.o      \
59 59
                                           psymodel.o iirfilter.o \
60
-                                          mpeg4audio.o kbdwin.o
60
+                                          mpeg4audio.o kbdwin.o  \
61
+                                          audio_frame_queue.o
61 62
 OBJS-$(CONFIG_AASC_DECODER)            += aasc.o msrledec.o
62 63
 OBJS-$(CONFIG_AC3_DECODER)             += ac3dec.o ac3dec_data.o ac3.o kbdwin.o
63 64
 OBJS-$(CONFIG_AC3_ENCODER)             += ac3enc_float.o ac3enc.o ac3tab.o \
... ...
@@ -34,6 +34,7 @@
34 34
 #include "avcodec.h"
35 35
 #include "put_bits.h"
36 36
 #include "dsputil.h"
37
+#include "internal.h"
37 38
 #include "mpeg4audio.h"
38 39
 #include "kbdwin.h"
39 40
 #include "sinewin.h"
... ...
@@ -476,8 +477,7 @@ static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s,
476 476
  * Deinterleave input samples.
477 477
  * Channels are reordered from Libav's default order to AAC order.
478 478
  */
479
-static void deinterleave_input_samples(AACEncContext *s,
480
-                                       const float *samples, int nb_samples)
479
+static void deinterleave_input_samples(AACEncContext *s, AVFrame *frame)
481 480
 {
482 481
     int ch, i;
483 482
     const int sinc = s->channels;
... ...
@@ -485,35 +485,43 @@ static void deinterleave_input_samples(AACEncContext *s,
485 485
 
486 486
     /* deinterleave and remap input samples */
487 487
     for (ch = 0; ch < sinc; ch++) {
488
-        const float *sptr = samples + channel_map[ch];
489
-
490 488
         /* copy last 1024 samples of previous frame to the start of the current frame */
491 489
         memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
492 490
 
493 491
         /* deinterleave */
494
-        for (i = 2048; i < 2048 + nb_samples; i++) {
495
-            s->planar_samples[ch][i] = *sptr;
496
-            sptr += sinc;
492
+        i = 2048;
493
+        if (frame) {
494
+            const float *sptr = ((const float *)frame->data[0]) + channel_map[ch];
495
+            for (; i < 2048 + frame->nb_samples; i++) {
496
+                s->planar_samples[ch][i] = *sptr;
497
+                sptr += sinc;
498
+            }
497 499
         }
498 500
         memset(&s->planar_samples[ch][i], 0,
499 501
                (3072 - i) * sizeof(s->planar_samples[0][0]));
500 502
     }
501 503
 }
502 504
 
503
-static int aac_encode_frame(AVCodecContext *avctx,
504
-                            uint8_t *frame, int buf_size, void *data)
505
+static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
506
+                            const AVFrame *frame, int *got_packet_ptr)
505 507
 {
506 508
     AACEncContext *s = avctx->priv_data;
507 509
     float **samples = s->planar_samples, *samples2, *la, *overlap;
508 510
     ChannelElement *cpe;
509
-    int i, ch, w, g, chans, tag, start_ch;
511
+    int i, ch, w, g, chans, tag, start_ch, ret;
510 512
     int chan_el_counter[4];
511 513
     FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
512 514
 
513 515
     if (s->last_frame == 2)
514 516
         return 0;
515 517
 
516
-    deinterleave_input_samples(s, data, data ? avctx->frame_size : 0);
518
+    /* add current frame to queue */
519
+    if (frame) {
520
+        if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
521
+            return ret;
522
+    }
523
+
524
+    deinterleave_input_samples(s, frame);
517 525
     if (s->psypp)
518 526
         ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
519 527
 
... ...
@@ -532,7 +540,7 @@ static int aac_encode_frame(AVCodecContext *avctx,
532 532
             overlap  = &samples[cur_channel][0];
533 533
             samples2 = overlap + 1024;
534 534
             la       = samples2 + (448+64);
535
-            if (!data)
535
+            if (!frame)
536 536
                 la = NULL;
537 537
             if (tag == TYPE_LFE) {
538 538
                 wi[ch].window_type[0] = ONLY_LONG_SEQUENCE;
... ...
@@ -565,7 +573,13 @@ static int aac_encode_frame(AVCodecContext *avctx,
565 565
     }
566 566
     do {
567 567
         int frame_bits;
568
-        init_put_bits(&s->pb, frame, buf_size*8);
568
+
569
+        if ((ret = ff_alloc_packet(avpkt, 768 * s->channels))) {
570
+            av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
571
+            return ret;
572
+        }
573
+        init_put_bits(&s->pb, avpkt->data, avpkt->size);
574
+
569 575
         if ((avctx->frame_number & 0xFF)==1 && !(avctx->flags & CODEC_FLAG_BITEXACT))
570 576
             put_bitstream_info(avctx, s, LIBAVCODEC_IDENT);
571 577
         start_ch = 0;
... ...
@@ -645,10 +659,15 @@ static int aac_encode_frame(AVCodecContext *avctx,
645 645
         s->lambda = FFMIN(s->lambda, 65536.f);
646 646
     }
647 647
 
648
-    if (!data)
648
+    if (!frame)
649 649
         s->last_frame++;
650 650
 
651
-    return put_bits_count(&s->pb)>>3;
651
+    ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
652
+                       &avpkt->duration);
653
+
654
+    avpkt->size = put_bits_count(&s->pb) >> 3;
655
+    *got_packet_ptr = 1;
656
+    return 0;
652 657
 }
653 658
 
654 659
 static av_cold int aac_encode_end(AVCodecContext *avctx)
... ...
@@ -662,6 +681,10 @@ static av_cold int aac_encode_end(AVCodecContext *avctx)
662 662
         ff_psy_preprocess_end(s->psypp);
663 663
     av_freep(&s->buffer.samples);
664 664
     av_freep(&s->cpe);
665
+    ff_af_queue_close(&s->afq);
666
+#if FF_API_OLD_ENCODE_AUDIO
667
+    av_freep(&avctx->coded_frame);
668
+#endif
665 669
     return 0;
666 670
 }
667 671
 
... ...
@@ -695,6 +718,11 @@ static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
695 695
     for(ch = 0; ch < s->channels; ch++)
696 696
         s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
697 697
 
698
+#if FF_API_OLD_ENCODE_AUDIO
699
+    if (!(avctx->coded_frame = avcodec_alloc_frame()))
700
+        goto alloc_fail;
701
+#endif
702
+
698 703
     return 0;
699 704
 alloc_fail:
700 705
     return AVERROR(ENOMEM);
... ...
@@ -756,6 +784,9 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
756 756
     for (i = 0; i < 428; i++)
757 757
         ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
758 758
 
759
+    avctx->delay = 1024;
760
+    ff_af_queue_init(avctx, &s->afq);
761
+
759 762
     return 0;
760 763
 fail:
761 764
     aac_encode_end(avctx);
... ...
@@ -784,7 +815,7 @@ AVCodec ff_aac_encoder = {
784 784
     .id             = CODEC_ID_AAC,
785 785
     .priv_data_size = sizeof(AACEncContext),
786 786
     .init           = aac_encode_init,
787
-    .encode         = aac_encode_frame,
787
+    .encode2        = aac_encode_frame,
788 788
     .close          = aac_encode_end,
789 789
     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
790 790
     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
... ...
@@ -27,7 +27,7 @@
27 27
 #include "dsputil.h"
28 28
 
29 29
 #include "aac.h"
30
-
30
+#include "audio_frame_queue.h"
31 31
 #include "psymodel.h"
32 32
 
33 33
 typedef struct AACEncOptions {
... ...
@@ -71,6 +71,7 @@ typedef struct AACEncContext {
71 71
     int cur_channel;
72 72
     int last_frame;
73 73
     float lambda;
74
+    AudioFrameQueue afq;
74 75
     DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
75 76
     DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
76 77