mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-10-06 10:42:22 +00:00
avaudenc: Use non-deprecated avcodec_encode_audio2() API
This also allows us to always get an output buffer of the required size instead of risking that it is too small.
This commit is contained in:
parent
7a29cffc50
commit
3ffbb6ff94
1 changed files with 20 additions and 15 deletions
|
@ -346,44 +346,49 @@ static GstFlowReturn
|
||||||
gst_ffmpegaudenc_encode_audio (GstFFMpegAudEnc * ffmpegaudenc,
|
gst_ffmpegaudenc_encode_audio (GstFFMpegAudEnc * ffmpegaudenc,
|
||||||
guint8 * audio_in, guint in_size, guint max_size)
|
guint8 * audio_in, guint in_size, guint max_size)
|
||||||
{
|
{
|
||||||
GstBuffer *outbuf;
|
|
||||||
AVCodecContext *ctx;
|
AVCodecContext *ctx;
|
||||||
GstMapInfo map;
|
|
||||||
gint res;
|
gint res;
|
||||||
GstFlowReturn ret;
|
GstFlowReturn ret;
|
||||||
|
GstAudioInfo *info;
|
||||||
|
AVPacket pkt;
|
||||||
|
AVFrame frame;
|
||||||
|
gint got_output;
|
||||||
|
|
||||||
ctx = ffmpegaudenc->context;
|
ctx = ffmpegaudenc->context;
|
||||||
|
|
||||||
/* We need to provide at least ffmpegs minimal buffer size */
|
/* 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);
|
GST_LOG_OBJECT (ffmpegaudenc, "encoding buffer of max size %d", max_size);
|
||||||
if (ffmpegaudenc->buffer_size != max_size)
|
if (ffmpegaudenc->buffer_size != max_size)
|
||||||
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) {
|
if (res < 0) {
|
||||||
gst_buffer_unmap (outbuf, &map);
|
|
||||||
GST_ERROR_OBJECT (ffmpegaudenc, "Failed to encode buffer: %d", res);
|
GST_ERROR_OBJECT (ffmpegaudenc, "Failed to encode buffer: %d", res);
|
||||||
gst_buffer_unref (outbuf);
|
|
||||||
return GST_FLOW_OK;
|
return GST_FLOW_OK;
|
||||||
}
|
}
|
||||||
GST_LOG_OBJECT (ffmpegaudenc, "got output size %d", res);
|
GST_LOG_OBJECT (ffmpegaudenc, "got output size %d", res);
|
||||||
gst_buffer_unmap (outbuf, &map);
|
|
||||||
gst_buffer_resize (outbuf, 0, res);
|
|
||||||
|
|
||||||
if (res > 0) {
|
if (got_output) {
|
||||||
GST_LOG_OBJECT (ffmpegaudenc, "pushing size %d", res);
|
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 =
|
ret =
|
||||||
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (ffmpegaudenc),
|
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (ffmpegaudenc),
|
||||||
outbuf, 1);
|
outbuf, 1);
|
||||||
} else {
|
} else {
|
||||||
GST_LOG_OBJECT (ffmpegaudenc, "no output produced");
|
GST_LOG_OBJECT (ffmpegaudenc, "no output produced");
|
||||||
gst_buffer_unref (outbuf);
|
|
||||||
ret = GST_FLOW_OK;
|
ret = GST_FLOW_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue