avaudenc: Use non-deprecated avcodec_encode_audio2() API
authorSebastian Dröge <sebastian.droege@collabora.co.uk>
Tue, 20 Nov 2012 09:53:01 +0000 (10:53 +0100)
committerSebastian Dröge <sebastian.droege@collabora.co.uk>
Tue, 20 Nov 2012 09:53:01 +0000 (10:53 +0100)
This also allows us to always get an output buffer of the required size
instead of risking that it is too small.

ext/libav/gstavaudenc.c

index cecd165ffe4bd1a061b06150fd33459051bd9b23..2955c5c051880df2d3b9fdc5309daa61df72e4e8 100644 (file)
@@ -346,44 +346,49 @@ static GstFlowReturn
 gst_ffmpegaudenc_encode_audio (GstFFMpegAudEnc * ffmpegaudenc,
     guint8 * audio_in, guint in_size, guint max_size)
 {
-  GstBuffer *outbuf;
   AVCodecContext *ctx;
-  GstMapInfo map;
   gint res;
   GstFlowReturn ret;
+  GstAudioInfo *info;
+  AVPacket pkt;
+  AVFrame frame;
+  gint got_output;
 
   ctx = ffmpegaudenc->context;
 
   /* We need to provide at least ffmpegs minimal buffer size */
-  outbuf =
-      gst_audio_encoder_allocate_output_buffer (GST_AUDIO_ENCODER
-      (ffmpegaudenc), max_size + FF_MIN_BUFFER_SIZE);
-  gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
-
   GST_LOG_OBJECT (ffmpegaudenc, "encoding buffer of max size %d", max_size);
   if (ffmpegaudenc->buffer_size != max_size)
     ffmpegaudenc->buffer_size = max_size;
 
-  res = avcodec_encode_audio (ctx, map.data, max_size, (short *) audio_in);
+  memset (&pkt, 0, sizeof (pkt));
+  memset (&frame, 0, sizeof (frame));
+
+  info = gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (ffmpegaudenc));
+  frame.data[0] = audio_in;
+  frame.linesize[0] = in_size;
+  frame.nb_samples = in_size / info->bpf;
 
+  res = avcodec_encode_audio2 (ctx, &pkt, &frame, &got_output);
   if (res < 0) {
-    gst_buffer_unmap (outbuf, &map);
     GST_ERROR_OBJECT (ffmpegaudenc, "Failed to encode buffer: %d", res);
-    gst_buffer_unref (outbuf);
     return GST_FLOW_OK;
   }
   GST_LOG_OBJECT (ffmpegaudenc, "got output size %d", res);
-  gst_buffer_unmap (outbuf, &map);
-  gst_buffer_resize (outbuf, 0, res);
 
-  if (res > 0) {
-    GST_LOG_OBJECT (ffmpegaudenc, "pushing size %d", res);
+  if (got_output) {
+    GstBuffer *outbuf;
+
+    GST_LOG_OBJECT (ffmpegaudenc, "pushing size %d", pkt.size);
+
+    outbuf =
+        gst_buffer_new_wrapped_full (0, pkt.data, pkt.size, 0, pkt.size,
+        pkt.data, av_free);
     ret =
         gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (ffmpegaudenc),
         outbuf, 1);
   } else {
     GST_LOG_OBJECT (ffmpegaudenc, "no output produced");
-    gst_buffer_unref (outbuf);
     ret = GST_FLOW_OK;
   }