diff av_helpers.c @ 36334:c1033e9288b1

Extract audio encoding code into a separate helper function.
author reimar
date Sun, 25 Aug 2013 11:30:43 +0000
parents d443d66a746c
children f77a74ebb95e
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;
+}