From patchwork Wed Mar 6 06:01:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andriy Gelman X-Patchwork-Id: 12213 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 BD45E448238 for ; Wed, 6 Mar 2019 08:02:34 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 95CB16807FA; Wed, 6 Mar 2019 08:02:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BBA40680626 for ; Wed, 6 Mar 2019 08:02:28 +0200 (EET) Received: by mail-qk1-f180.google.com with SMTP id p15so6169441qkl.5 for ; Tue, 05 Mar 2019 22:02:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=oMQ8sSq64nBo6w1Z+XTHrBTCWAj/pwevq1cvP/TQ0gY=; b=sust12/0exmSMKpRC58Sn98o+qw6wIHR1gSsnBqZHYGbGl8AWlO/pymsVa59XqYysC ATNBc8c9ogCisPF2X9f07/biG6ZzwyKQw7KbrahOu5hQUB8IRTSBRoo+o7DcqgJ0OKrL dFbxiBqQCZrJI7RNrWbl0fgU4xZE+pPUoZ9fOvQ+1S+qcTWw8MSq1dKuPllNzCtVlIjY AqX9aeqRuzEQGQjkGDNYudUH+Aq2H1po3XhDKqM3ELCzMzopHSJqpHedctHc34IGoC9Q 4o2FLWThIYs4hgLv9u/5Gd/mDQs4AltJASlY5eT6n0/mNptKx0jKv7c0t8lpnjgCcmhw RaQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=oMQ8sSq64nBo6w1Z+XTHrBTCWAj/pwevq1cvP/TQ0gY=; b=LXgK/+6gqQmR+Dc9+alhBZOKhv4IvwHG8izKyuhco3jjHRHaP4YX9dZTDyNYlYvnqI uddEAgjewwgDX+jYcMGmKQG3Ez+qXTlp7czpyVjdwi8+in3G8H4Xf9ravCWu+ndcJddG VY0b5zU7IuS282cELuALt4dNyOZYz/7JopuPAmGy6gPOHWXh+V0x8h8fm0eTmJUG0cT1 Vs8mgPiLYT5d8C5IlvIYn4DyIEgrXa2Wr17TnRc6F7KeZ5bZladRYNJxfva5TB9wnubJ geET767EkuhckTfMat/mNLg96cRK4SEB19EcaYnmOt8ui+xO9bksFn7BbPNeVqfdDDp+ pz7A== X-Gm-Message-State: APjAAAW1BEQvd0P+8DFTn56RZNkvCRdRT1CxQqZr4OVvQHLMMABF5p9d rShTISkj5wC2bDszhkritr8qpawq X-Google-Smtp-Source: APXvYqyBP3JB0RG8cMm23GoPHwgoZc86mpIeXs9R/EtyEPvqsv5xyfsa2KYbnsViGTLWqeChqkaqhQ== X-Received: by 2002:a37:a381:: with SMTP id m123mr4523574qke.147.1551852147092; Tue, 05 Mar 2019 22:02:27 -0800 (PST) Received: from localhost.localdomain (c-24-60-253-205.hsd1.ma.comcast.net. [24.60.253.205]) by smtp.gmail.com with ESMTPSA id s41sm443160qtc.38.2019.03.05.22.02.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Mar 2019 22:02:26 -0800 (PST) From: Andriy Gelman X-Google-Original-From: Andriy Gelman To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Mar 2019 01:01:58 -0500 Message-Id: <20190306060158.17003-1-andriy.gelman@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/mpegts: reduce buffering during initialization 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: Andriy Gelman Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Andriy Gelman Reduces buffering latency with low bitrate streams, where 8192 bytes can mean several seconds. --- libavformat/mpegts.c | 60 +++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index b04fd7b4f4..fcb5b488d8 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -53,6 +53,10 @@ (prev_dividend) = (dividend); \ } while (0) +#define PROBE_PACKET_MAX_BUF 8192 +#define PROBE_PACKET_STEP 512 +#define PROBE_PACKET_MARGIN 5 + enum MpegTSFilterType { MPEGTS_PES, MPEGTS_SECTION, @@ -591,28 +595,42 @@ static int analyze(const uint8_t *buf, int size, int packet_size, return best_score - FFMAX(stat_all - 10*best_score, 0)/10; } -/* autodetect fec presence. Must have at least 1024 bytes */ -static int get_packet_size(const uint8_t *buf, int size) +/* autodetect fec presence */ +static int get_packet_size(AVIOContext* pb) { int score, fec_score, dvhs_score; + int margin; + int ret; - if (size < (TS_FEC_PACKET_SIZE * 5 + 1)) - return AVERROR_INVALIDDATA; + /*init buffer to store stream for probing */ + uint8_t buf[PROBE_PACKET_MAX_BUF] = {0}; + int buf_size = 0; - score = analyze(buf, size, TS_PACKET_SIZE, 0); - dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, 0); - fec_score = analyze(buf, size, TS_FEC_PACKET_SIZE, 0); - av_log(NULL, AV_LOG_TRACE, "score: %d, dvhs_score: %d, fec_score: %d \n", - score, dvhs_score, fec_score); - - if (score > fec_score && score > dvhs_score) - return TS_PACKET_SIZE; - else if (dvhs_score > score && dvhs_score > fec_score) - return TS_DVHS_PACKET_SIZE; - else if (score < fec_score && dvhs_score < fec_score) - return TS_FEC_PACKET_SIZE; - else - return AVERROR_INVALIDDATA; + while (buf_size + PROBE_PACKET_STEP <= PROBE_PACKET_MAX_BUF) { + ret = avio_read(pb, buf + buf_size, PROBE_PACKET_STEP); + if (ret < 0) + break; + buf_size += ret; + + score = analyze(buf, buf_size, TS_PACKET_SIZE, 0); + dvhs_score = analyze(buf, buf_size, TS_DVHS_PACKET_SIZE, 0); + fec_score = analyze(buf, buf_size, TS_FEC_PACKET_SIZE, 0); + av_log(NULL, AV_LOG_TRACE, "Probe: %d, score: %d, dvhs_score: %d, fec_score: %d \n", + buf_size, score, dvhs_score, fec_score); + + if (buf_size < PROBE_PACKET_MAX_BUF && ret == PROBE_PACKET_STEP) + margin = PROBE_PACKET_MARGIN; /*if buffer not filled and no eof */ + else + margin = 0; + + if (score > FFMAX(fec_score, dvhs_score) + margin) + return TS_PACKET_SIZE; + else if (dvhs_score > FFMAX(score, fec_score) + margin) + return TS_DVHS_PACKET_SIZE; + else if (fec_score > FFMAX(score, dvhs_score) + margin) + return TS_FEC_PACKET_SIZE; + } + return AVERROR_INVALIDDATA; } typedef struct SectionHeader { @@ -2841,8 +2859,6 @@ static int mpegts_read_header(AVFormatContext *s) { MpegTSContext *ts = s->priv_data; AVIOContext *pb = s->pb; - uint8_t buf[8 * 1024] = {0}; - int len; int64_t pos, probesize = s->probesize; s->internal->prefer_codec_framerate = 1; @@ -2850,10 +2866,8 @@ static int mpegts_read_header(AVFormatContext *s) if (ffio_ensure_seekback(pb, probesize) < 0) av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n"); - /* read the first 8192 bytes to get packet size */ pos = avio_tell(pb); - len = avio_read(pb, buf, sizeof(buf)); - ts->raw_packet_size = get_packet_size(buf, len); + ts->raw_packet_size = get_packet_size(pb); if (ts->raw_packet_size <= 0) { av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n"); ts->raw_packet_size = TS_PACKET_SIZE;