From patchwork Sun Feb 21 16:46:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 25861 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 10F0344A660 for ; Sun, 21 Feb 2021 18:48:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E9CA368A846; Sun, 21 Feb 2021 18:48:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E2893687F3C for ; Sun, 21 Feb 2021 18:48:34 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id CBD9EE502C; Sun, 21 Feb 2021 17:48:34 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yoGOkVOtygE4; Sun, 21 Feb 2021 17:48:05 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 3540EE502B; Sun, 21 Feb 2021 17:47:16 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 21 Feb 2021 17:46:59 +0100 Message-Id: <20210221164659.19180-5-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210221164659.19180-1-cus@passwd.hu> References: <20210221164659.19180-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] avdevice/pulse_audio_dec: do not read undersized frames X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Keep on reading fragments until we got fragment_size amount of data, otherwise we might get frames with 1-2 samples only if pa_stream_peek is called slightly less frequently than sample rate. Note that fragments might contain a lot less data than fragment_size, so reading multiple fragments to get fragment_size amount of data is intentional. Signed-off-by: Marton Balint --- libavdevice/pulse_audio_dec.c | 71 ++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/libavdevice/pulse_audio_dec.c b/libavdevice/pulse_audio_dec.c index 0454a643dd..3777396ef6 100644 --- a/libavdevice/pulse_audio_dec.c +++ b/libavdevice/pulse_audio_dec.c @@ -48,6 +48,7 @@ typedef struct PulseData { pa_threaded_mainloop *mainloop; pa_context *context; pa_stream *stream; + size_t pa_frame_size; TimeFilter *timefilter; int last_period; @@ -250,6 +251,7 @@ static av_cold int pulse_read_header(AVFormatContext *s) goto unlock_and_fail; } pd->fragment_size = queried_attr->fragsize; + pd->pa_frame_size = pa_frame_size(&ss); pa_threaded_mainloop_unlock(pd->mainloop); @@ -261,7 +263,7 @@ static av_cold int pulse_read_header(AVFormatContext *s) avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate, - 1000, 1.5E-6); + pd->fragment_size / pd->pa_frame_size, 1.5E-6); if (!pd->timefilter) { pulse_close(s); @@ -286,12 +288,13 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt) int64_t dts; pa_usec_t latency; int negative; + ptrdiff_t pos = 0; pa_threaded_mainloop_lock(pd->mainloop); CHECK_DEAD_GOTO(pd, ret, unlock_and_fail); - while (!read_data) { + while (pos < pd->fragment_size) { int r; r = pa_stream_peek(pd->stream, &read_data, &read_length); @@ -305,43 +308,51 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt) * silence, but that wouldn't work for compressed streams. */ r = pa_stream_drop(pd->stream); CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail); + } else { + if (!pos) { + if (av_new_packet(pkt, pd->fragment_size) < 0) { + ret = AVERROR(ENOMEM); + goto unlock_and_fail; + } + + dts = av_gettime(); + pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL)); + + if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) { + if (negative) { + dts += latency; + } else + dts -= latency; + } else { + av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n"); + } + } + if (pkt->size - pos < read_length) { + if (pos) + break; + pa_stream_drop(pd->stream); + /* Oversized fragment??? */ + ret = AVERROR_EXTERNAL; + goto unlock_and_fail; + } + memcpy(pkt->data + pos, read_data, read_length); + pos += read_length; + pa_stream_drop(pd->stream); } } - if (av_new_packet(pkt, read_length) < 0) { - ret = AVERROR(ENOMEM); - goto unlock_and_fail; - } - - dts = av_gettime(); - pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL)); - - if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) { - enum AVCodecID codec_id = - s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id; - int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels); - int frame_duration = read_length / frame_size; - - - if (negative) { - dts += latency; - } else - dts -= latency; - if (pd->wallclock) - pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period); + pa_threaded_mainloop_unlock(pd->mainloop); - pd->last_period = frame_duration; - } else { - av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n"); - } + av_shrink_packet(pkt, pos); - memcpy(pkt->data, read_data, read_length); - pa_stream_drop(pd->stream); + if (pd->wallclock) + pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period); + pd->last_period = pkt->size / pd->pa_frame_size; - pa_threaded_mainloop_unlock(pd->mainloop); return 0; unlock_and_fail: + av_packet_unref(pkt); pa_threaded_mainloop_unlock(pd->mainloop); return ret; }