From patchwork Fri Oct 25 09:11:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15948 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 5AC35449DC1 for ; Fri, 25 Oct 2019 12:12:30 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4043068B41C; Fri, 25 Oct 2019 12:12:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3AE6B68B40A for ; Fri, 25 Oct 2019 12:12:24 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id c22so1228340wmd.1 for ; Fri, 25 Oct 2019 02:12:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TBEso7Fwjnj48KCdwcyzNRsKZNDHJe7WWuuSSHehTRI=; b=Lfk9i7cynJZJT6ohSyMVXvj9c+8BXdDR1GEpt4Ehht2d9V1kYzyRLuPjr+39crWDl0 czsegJtF9R8TzdbHHWZhjKhwi1coIn+wi6e/TdT/GfR4K0+0XMmHQiR+233e0XtbXq9S VAo2NO1mzrx08B1WJCvH0bRMor8B4j3Go0f5Vzc4xZv8NKIipDUvEUrZ35A5OrbsfvVo 83KhEerZGU5PG1uCQVGzH+Wm2wF3X6ss/knhA5L2EWGCJZSQjcIK8KqHt2iY1zPdVf8n XK8hftCYdEUsrROIegb6hi1IZtJsKkQE+Hw1mWsye5PeKNcQBA4DOdwLSs/hCtgqtJ7k LAbA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=TBEso7Fwjnj48KCdwcyzNRsKZNDHJe7WWuuSSHehTRI=; b=ZwR3eO7G8S64qh5HasDQSXHvUB2r0JYCjO3Sy9M6HS4xe7eU3q6A4GgWQLWEgGT7Ew Mn8P8vkCkxEGsTLhmSBVoxvKpiGID8lhgJs0540tY8lPC0FhkyGyhnlfYOOn5/gRfj4w 4uro3w9BGzO96EpGZEFwTP+bWVRKtY1vgK8WhMWgko0oEK4hk4892Jr4IapmOT7Ki1LC CMwymrTf26Fkt7dB74vLsGqbbMvOuQFm3e6IEPqXZ8MBJXj7bPncNLscpXZ8m/UyWfYC cJNA6zZ2r0YS63GgX8aBvy78eFgXTKZ8bMLBdKUqw6vCHpV+uQKZADNZuf0m2eCR3+wJ Ce2A== X-Gm-Message-State: APjAAAU+ZoSZq9TI+l4ReDff4DNVub+Sp8OHFVNQlLBVEjYAd+O1Lg0p AaKn8P2JkGSzaE2qjO7bT5d/MYZp X-Google-Smtp-Source: APXvYqwxTmGlfJJHhCJ0+nwFLnxaFmVJDY2jMw6NFwrJZ4czs6Rxxg5z2mur23lZB2+cSwa99N/+TA== X-Received: by 2002:a1c:7fd8:: with SMTP id a207mr2580854wmd.10.1571994743522; Fri, 25 Oct 2019 02:12:23 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id d202sm1516724wmd.47.2019.10.25.02.12.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2019 02:12:23 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 25 Oct 2019 11:11:46 +0200 Message-Id: <20191025091147.5298-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191025091147.5298-1-andreas.rheinhardt@gmail.com> References: <20191025085222.4498-1-andreas.rheinhardt@gmail.com> <20191025091147.5298-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avformat/flvenc: Use array instead of linked list for index 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Using a linked list had very much overhead (the pointer to the next entry increased the size of the index entry struct from 16 to 24 bytes, not to mention the overhead of having separate allocations), so it is better to (re)allocate a continuous array for the index. Signed-off-by: Andreas Rheinhardt --- libavformat/flvenc.c | 58 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 0e6c66a5ff..a2bd791c59 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -74,7 +74,6 @@ typedef enum { typedef struct FLVFileposition { int64_t keyframe_position; double keyframe_timestamp; - struct FLVFileposition *next; } FLVFileposition; typedef struct FLVContext { @@ -108,9 +107,9 @@ typedef struct FLVContext { int acurframeindex; int64_t keyframes_info_offset; - int64_t filepositions_count; FLVFileposition *filepositions; - FLVFileposition *head_filepositions; + size_t filepositions_allocated; + int64_t filepositions_count; AVCodecParameters *audio_par; AVCodecParameters *video_par; @@ -549,27 +548,19 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) { - FLVFileposition *position = av_malloc(sizeof(FLVFileposition)); - - if (!position) { - av_log(s, AV_LOG_WARNING, "no mem for add keyframe index!\n"); - return AVERROR(ENOMEM); - } - - position->keyframe_timestamp = ts; - position->keyframe_position = pos; - - if (!flv->filepositions_count) { - flv->filepositions = position; - flv->head_filepositions = flv->filepositions; - position->next = NULL; - } else { - flv->filepositions->next = position; - position->next = NULL; - flv->filepositions = flv->filepositions->next; + if (flv->filepositions_count >= flv->filepositions_allocated) { + void *pos = av_realloc_array(flv->filepositions, + 2 * flv->filepositions_allocated + 1, + sizeof(*flv->filepositions)); + if (!pos) { + av_log(s, AV_LOG_WARNING, "No memory for keyframe index\n"); + return AVERROR(ENOMEM); + } + flv->filepositions = pos; + flv->filepositions_allocated = 2 * flv->filepositions_allocated + 1; } - flv->filepositions_count++; + flv->filepositions[flv->filepositions_count++] = (FLVFileposition){ pos, ts }; return 0; } @@ -784,7 +775,7 @@ static int flv_write_trailer(AVFormatContext *s) int64_t cur_pos = avio_tell(s->pb); if (build_keyframes_idx) { - FLVFileposition *newflv_posinfo, *p; + FLVFileposition *flv_posinfo = flv->filepositions; avio_seek(pb, flv->videosize_offset, SEEK_SET); put_amf_double(pb, flv->videosize); @@ -809,28 +800,17 @@ static int flv_write_trailer(AVFormatContext *s) avio_seek(pb, flv->keyframes_info_offset, SEEK_SET); put_amf_string(pb, "filepositions"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_position + flv->keyframe_index_size); + for (i = 0; i < flv->filepositions_count; i++) { + put_amf_double(pb, flv_posinfo[i].keyframe_position + flv->keyframe_index_size); } put_amf_string(pb, "times"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_timestamp); + for (i = 0; i < flv->filepositions_count; i++) { + put_amf_double(pb, flv_posinfo[i].keyframe_timestamp); } - newflv_posinfo = flv->head_filepositions; - while (newflv_posinfo) { - p = newflv_posinfo->next; - if (p) { - newflv_posinfo->next = p->next; - av_free(p); - p = NULL; - } else { - av_free(newflv_posinfo); - newflv_posinfo = NULL; - } - } + av_freep(&flv->filepositions); put_amf_string(pb, ""); avio_w8(pb, AMF_END_OF_OBJECT);