From patchwork Wed Jan 1 13:27:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17115 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 F040D4490FA for ; Wed, 1 Jan 2020 15:28:25 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CB75268AD5A; Wed, 1 Jan 2020 15:28:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2C92068A5BE for ; Wed, 1 Jan 2020 15:28:19 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id z7so36915937wrl.13 for ; Wed, 01 Jan 2020 05:28:19 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=+rdPkdITsiakV8ycRHFi/vjXKZJYMnjPyOGikqUf2Os=; b=olOfrBXlls3M+FWOlBEqV+gAX6ytXQDbgAJ5fNW/UDFLUq5tv6WTQFAXNGjmgtnQMP +DRsyeJTOXo2vdmFSVcyJf0kPxwZ8ss8SRx6ejU7iXAgo7BuqlkZ7G6GFC4+vKThPsb8 AoZIOpAilTbSSwNwSEYHh46U7XIqbvxs7EPMcGdoNEmTLljBOoU68N7S/3OGJmoFp7oo 0IQk9FleZsi6WNnrwg2gk2FfWhqXEKj/PtQEIaT69F+qS3L4ydqIgd+5RLLtVcCJDcyo oIfUR1SglMZX4t4vv0XkwrcwJRqvZj7mFOAFr4pPLQhD4a9LsUdW7h3gSt529GszhFLZ z9Gw== 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=+rdPkdITsiakV8ycRHFi/vjXKZJYMnjPyOGikqUf2Os=; b=ntVMkfY5Nf6J6p/WeuLiitEBz80Nfg1b9hn2PPDCLUZ8eKNQkpUQoOo3mUGTWoyNrL L/gj/vvZxkAYo2RC1ZG7gHZn1e1GOJHm/uLC9GxKr2uBxdB34x/n/zmpvwIMaikWbPpx CnpFhf9f/ssHaeTk6rioGKRrCGP0OUIC+REgJgIlcFIa3SC7JpJJZFIB02snttX0Ft6Y rds1PtZrWw37eLluZYpnUgiltEqKWbZDaKv9U7oAOVEfJXf/cF3ewI297CU46jTtqUcl 2B2BMRYNVtaq4fJEBdcK/+HWWxFjX4bCOuTL4A46VkCP1K03HVLYR6hDKJ8K2xLaY9Rh 5PIg== X-Gm-Message-State: APjAAAUM453tCHM5zObFYidL6QVRzFJQ9+pAus4G10EaK6etR+rsWCsK ngknBxKvCfAFOfea2H4ZMxFRjVzS X-Google-Smtp-Source: APXvYqxBtuidZf9ox6RutxbfdbUBKc4BpRM79hGhL6QZZYj1zSMAzOKUuP8ojYbZGK5hqN8rlNmPhA== X-Received: by 2002:adf:b60f:: with SMTP id f15mr73293725wre.372.1577885298513; Wed, 01 Jan 2020 05:28:18 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id f17sm5520462wmc.8.2020.01.01.05.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2020 05:28:18 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 1 Jan 2020 14:27:58 +0100 Message-Id: <20200101132758.4452-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200101132758.4452-1-andreas.rheinhardt@gmail.com> References: <20200101132758.4452-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 12/17] 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. av_fast_realloc_array() is used for this purpose, in order not to reallocate the array for each entry. Signed-off-by: Andreas Rheinhardt --- libavformat/flvenc.c | 58 ++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 1aaf0333ca..74f4e499f6 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; + unsigned filepositions_count; + unsigned filepositions_allocated; AVCodecParameters *audio_par; AVCodecParameters *video_par; @@ -549,27 +548,17 @@ 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; + int ret = av_fast_realloc_array(&flv->filepositions, + &flv->filepositions_allocated, + flv->filepositions_count + 1, + UINT_MAX - 1, + sizeof(*flv->filepositions)); + if (ret < 0) { + av_log(s, AV_LOG_WARNING, "Adding entry to keyframe index failed.\n"); + return ret; } - flv->filepositions_count++; + flv->filepositions[flv->filepositions_count++] = (FLVFileposition){ pos, ts }; return 0; } @@ -586,7 +575,7 @@ static int shift_data(AVFormatContext *s) int read_size[2]; AVIOContext *read_pb; - metadata_size = flv->filepositions_count * 9 * 2 + 10; /* filepositions and times value */ + metadata_size = flv->filepositions_count * 9LL * 2 + 10; /* filepositions and times value */ metadata_size += 2 + 13; /* filepositions String */ metadata_size += 2 + 5; /* times String */ metadata_size += 3; /* Object end */ @@ -784,7 +773,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 +798,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 (unsigned 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 (unsigned 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);