changeset 36334:c1033e9288b1

Extract audio encoding code into a separate helper function.
author reimar
date Sun, 25 Aug 2013 11:30:43 +0000
parents dc90c43a2674
children f77a74ebb95e
files av_helpers.c av_helpers.h libaf/af_lavcac3enc.c libmpcodecs/ae_lavc.c
diffstat 4 files changed, 34 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/av_helpers.c	Sun Aug 25 11:30:42 2013 +0000
+++ b/av_helpers.c	Sun Aug 25 11:30:43 2013 +0000
@@ -22,6 +22,7 @@
 #include "libavformat/avformat.h"
 #include "mp_msg.h"
 #include "av_helpers.h"
+#include "libaf/reorder_ch.h"
 
 int avcodec_initialized;
 int avformat_initialized;
@@ -113,3 +114,28 @@
         av_log_set_callback(mp_msp_av_log_callback);
     }
 }
+
+int lavc_encode_audio(AVCodecContext *ctx, void *src, int src_len, void *dst, int dst_len)
+{
+    int bps = av_get_bytes_per_sample(ctx->sample_fmt);
+    int n;
+    int got;
+    AVPacket pkt;
+    AVFrame frame;
+    if ((ctx->channels == 6 || ctx->channels == 5) &&
+        (!strcmp(ctx->codec->name,"ac3") || !strcmp(ctx->codec->name,"libfaac"))) {
+        int isac3 = !strcmp(ctx->codec->name,"ac3");
+        reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+                            isac3 ? AF_CHANNEL_LAYOUT_LAVC_DEFAULT : AF_CHANNEL_LAYOUT_AAC_DEFAULT,
+                            ctx->channels,
+                            src_len / bps, bps);
+    }
+    pkt.data = dst;
+    pkt.size = dst_len;
+    frame.data[0] = src;
+    frame.linesize[0] = src_len / ctx->channels;
+    frame.nb_samples = frame.linesize[0] / bps;
+    n = avcodec_encode_audio2(ctx, &pkt, &frame, &got);
+    if (n < 0) return n;
+    return got ? pkt.size : 0;
+}
--- a/av_helpers.h	Sun Aug 25 11:30:42 2013 +0000
+++ b/av_helpers.h	Sun Aug 25 11:30:43 2013 +0000
@@ -21,7 +21,10 @@
 #ifndef MPLAYER_AV_HELPERS_H
 #define MPLAYER_AV_HELPERS_H
 
+struct AVCodecContext;
+
 void init_avcodec(void);
 void init_avformat(void);
+int lavc_encode_audio(struct AVCodecContext *ctx, void *src, int src_len, void *dst, int dst_len);
 
 #endif /* MPLAYER_AV_HELPERS_H */
--- a/libaf/af_lavcac3enc.c	Sun Aug 25 11:30:42 2013 +0000
+++ b/libaf/af_lavcac3enc.c	Sun Aug 25 11:30:43 2013 +0000
@@ -229,15 +229,10 @@
             src += s->expect_len;
             left -= s->expect_len;
         }
-        if (c->nch >= 5)
-            reorder_channel_nch(in,
-                                AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
-                                AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
-                                c->nch,
-                                s->expect_len / 2, 2);
-        len = avcodec_encode_audio(s->lavc_actx, dest, destsize, in);
+        len = lavc_encode_audio(s->lavc_actx, in, s->expect_len, dest, destsize);
         mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
                len, s->pending_len);
+        if (len < 0) len = 0;
 
         if (s->add_iec61937_header) {
             int bsmod = dest[5] & 0x7;
--- a/libmpcodecs/ae_lavc.c	Sun Aug 25 11:30:42 2013 +0000
+++ b/libmpcodecs/ae_lavc.c	Sun Aug 25 11:30:43 2013 +0000
@@ -118,20 +118,9 @@
 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
 {
 	int n;
-	if ((encoder->params.channels == 6 || encoder->params.channels == 5) &&
-			(!strcmp(lavc_acodec->name,"ac3") ||
-			!strcmp(lavc_acodec->name,"libfaac"))) {
-		int isac3 = !strcmp(lavc_acodec->name,"ac3");
-		int bps = av_get_bytes_per_sample(lavc_actx->sample_fmt);
-		reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
-		                    isac3 ? AF_CHANNEL_LAYOUT_LAVC_DEFAULT
-		                          : AF_CHANNEL_LAYOUT_AAC_DEFAULT,
-		                    encoder->params.channels,
-		                    size / bps, bps);
-	}
-	n = avcodec_encode_audio(lavc_actx, dest, size, src);
-        compressed_frame_size = n;
-	return n;
+	n = lavc_encode_audio(lavc_actx, src, size, dest, max_size);
+	compressed_frame_size = n < 0 ? 0 : n;
+	return compressed_frame_size;
 }