From patchwork Wed Jun 24 13:47:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20578 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 7556E44B3C4 for ; Wed, 24 Jun 2020 16:53:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5F1AA68B601; Wed, 24 Jun 2020 16:53:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C3F1768B5BF for ; Wed, 24 Jun 2020 16:53:45 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id s10so2341765wrw.12 for ; Wed, 24 Jun 2020 06:53:45 -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=qJi+/PMXp3jn57jOjFHBdYcU0+OJIOHeeHtNj/YzNW0=; b=TTyDn9UIy/tYJ2fsfRGrrJLbZf5TG6hx5ZxnPLwVdnm6iY8pj53PXeeJgyxcSTsYke OVZ0ik6EgXVERfb/ORky1rswEFXfHbjKeRW6rQsLAmEvOiO/Da0rTA0gJ3HMN4zcutpf VnADbgTtFTNsNRHQ7uGUzx2XzFcGxJr3Dm0buJJ4m8KK2iD6X6SOSDGtj5O90VUerBOL wDxZlOm7+bWaYfFjxPqSQe54dWA0F+Ffw/IuIiWPgLcWmiiIt0PLWfJykX+5lkqpLovz NB2ofwzAlSokddvDMATmNBBKVKJtVYfC/iwcFIRBwUXWHAH5TW3L2Rcku/klf2SqT/ye LaXQ== 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=qJi+/PMXp3jn57jOjFHBdYcU0+OJIOHeeHtNj/YzNW0=; b=ImHNAEMgIG3jFUZdiRV8+xl9ImiF2IHV8e+UavmEIHuFxFcBgl6VJ9xAdik7kTD9JA LHGmFsjn0Srus6HBMzLNUI8JqlC33W54xG6zqqmSgXbUQUllZaMBx7zr1a4NWmdf8eeE KfY/gJ8/jBa2f+bq/UwSGZLCXH2Yo0bgD0qj9Ilf5DThoU5aTWrkya4Hg8QiohDzck7N +R5nEtazCGvp6nwL8pqa7bXFkcmocBZSfhIortrl8tTfuaA7inu/z4g1CcI4YB0O9hd7 7hj7WtBd4WxnjJx/NG4NnRYDWkQUnghDfdlheCaj8Jah9iEIJI2O3Q124q3Imsy7S8ih htSQ== X-Gm-Message-State: AOAM531fGw0zH7VB9XmLAioPlKLuOcV+v4IT49yPiDimQEqoa8BLsnm0 VlKWgwt0QAHawyR+FE/yZN198BCY X-Google-Smtp-Source: ABdhPJxo9mpmWAKxKClh8EfM5iJs34QlwBea6XUpbnd8hQnNicq2i+f2O7mTwqXJDB1rZuvp4jUPeg== X-Received: by 2002:a5d:404e:: with SMTP id w14mr28633037wrp.268.1593006824699; Wed, 24 Jun 2020 06:53:44 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id y25sm8172390wma.19.2020.06.24.06.53.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2020 06:53:44 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 24 Jun 2020 15:47:00 +0200 Message-Id: <20200624134705.14833-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200624134705.14833-1-andreas.rheinhardt@gmail.com> References: <20200624134705.14833-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/9] avformat/smacker: Stop caching and copying audio 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The layout of a Smacker frame is as follows: For some frames, the beginning of the frame contained a palette for the video stream; then there are potentially several audio frames, followed by the data for the video stream. The Smacker demuxer used to read the palette, then cache every audio frame into a buffer (that gets reallocated to the desired size every time a frame is read into this buffer), then read and return the video frame (together with the palette). The cached audio frames are then returned by copying the data into freshly allocated buffers; if there are none left, the next frame is read. This commit changes this: At the beginning of a frame, the palette is read and cached as now. But audio frames are no longer cached at all; they are returned immediately. This gets rid of copying and also allows to remove the code for the buffer-to-AVStream correspondence. Signed-off-by: Andreas Rheinhardt --- libavformat/smacker.c | 94 ++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/libavformat/smacker.c b/libavformat/smacker.c index c803ecbec9..4990b7d920 100644 --- a/libavformat/smacker.c +++ b/libavformat/smacker.c @@ -48,14 +48,16 @@ typedef struct SmackerContext { uint32_t *frm_size; uint8_t *frm_flags; /* internal variables */ + int64_t next_frame_pos; int cur_frame; /* current frame for demuxing */ + uint32_t frame_size; + int flags; + int next_audio_index; + int new_palette; uint8_t pal[768]; int indexes[7]; int videoindex; - uint8_t *bufs[7]; - int buf_sizes[7]; - int stream_id[7]; int curstream; int64_t aud_pts[7]; } SmackerContext; @@ -235,21 +237,19 @@ static int smacker_read_header(AVFormatContext *s) static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) { SmackerContext *smk = s->priv_data; - int64_t next_frame_pos; int flags; int ret; - int i; - int frame_size = 0; - int palchange = 0; if (avio_feof(s->pb) || smk->cur_frame >= smk->frames) return AVERROR_EOF; /* if we demuxed all streams, pass another frame */ - if (smk->curstream <= 0) { - frame_size = smk->frm_size[smk->cur_frame] & (~3); - next_frame_pos = avio_tell(s->pb) + (unsigned)frame_size; + if (!smk->next_audio_index) { + smk->frame_size = smk->frm_size[smk->cur_frame] & (~3); + smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size; + smk->curstream = 0; flags = smk->frm_flags[smk->cur_frame]; + smk->flags = flags >> 1; /* handle palette change event */ if(flags & SMACKER_PAL){ int size, sz, t, off, j, pos; @@ -258,13 +258,12 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) memcpy(oldpal, pal, 768); size = avio_r8(s->pb); - size = size * 4 - 1; - if (size + 1 > frame_size) { + size = size * 4; + if (size > smk->frame_size) { ret = AVERROR_INVALIDDATA; goto next_frame; } - frame_size -= size; - frame_size--; + smk->frame_size -= size--; sz = 0; pos = avio_tell(s->pb) + size; while(sz < 256){ @@ -298,69 +297,65 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt) } } avio_seek(s->pb, pos, 0); - palchange |= 1; + smk->new_palette = 1; } - flags >>= 1; - smk->curstream = 0; - /* if audio chunks are present, put them to stack and retrieve later */ - for(i = 0; i < 7; i++) { - if(flags & 1) { + } + + for (int i = smk->next_audio_index; i < 7; i++) { + if (smk->flags & (1 << i)) { uint32_t size; size = avio_rl32(s->pb); - if ((int)size < 8 || size > frame_size) { + if ((int)size < 8 || size > smk->frame_size) { av_log(s, AV_LOG_ERROR, "Invalid audio part size\n"); ret = AVERROR_INVALIDDATA; goto next_frame; } - frame_size -= size; + smk->frame_size -= size; size -= 4; - if ((ret = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) { - smk->buf_sizes[smk->curstream] = 0; + + if (smk->indexes[i] < 0) { + avio_skip(s->pb, size); + continue; + } + if ((ret = av_get_packet(s->pb, pkt, size)) != size) { + ret = ret < 0 ? ret : AVERROR_INVALIDDATA; goto next_frame; } - smk->buf_sizes[smk->curstream] = size; - ret = ffio_read_size(s->pb, smk->bufs[smk->curstream], size); - if (ret < 0) - goto next_frame; - smk->stream_id[smk->curstream] = smk->indexes[i]; + pkt->stream_index = smk->indexes[i]; + pkt->pts = smk->aud_pts[smk->curstream]; + smk->aud_pts[smk->curstream] += AV_RL32(pkt->data); + smk->next_audio_index = i + 1; smk->curstream++; + return 0; } - flags >>= 1; } - if (frame_size < 0 || frame_size >= INT_MAX/2) { + + if (smk->frame_size >= INT_MAX/2) { ret = AVERROR_INVALIDDATA; goto next_frame; } - if ((ret = av_new_packet(pkt, frame_size + 769)) < 0) + if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0) goto next_frame; + flags = smk->new_palette; if(smk->frm_size[smk->cur_frame] & 1) - palchange |= 2; - pkt->data[0] = palchange; + flags |= 2; + pkt->data[0] = flags; memcpy(pkt->data + 1, smk->pal, 768); - ret = ffio_read_size(s->pb, pkt->data + 769, frame_size); + ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size); if (ret < 0) goto next_frame; pkt->stream_index = smk->videoindex; pkt->pts = smk->cur_frame; pkt->size = ret + 769; + smk->next_audio_index = 0; + smk->new_palette = 0; smk->cur_frame++; - } else { - smk->curstream--; - if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream]) - return AVERROR_INVALIDDATA; - if ((ret = av_new_packet(pkt, smk->buf_sizes[smk->curstream])) < 0) - return ret; - memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]); - pkt->size = smk->buf_sizes[smk->curstream]; - pkt->stream_index = smk->stream_id[smk->curstream]; - pkt->pts = smk->aud_pts[smk->curstream]; - smk->aud_pts[smk->curstream] += AV_RL32(pkt->data); - } return 0; next_frame: - avio_seek(s->pb, next_frame_pos, SEEK_SET); + avio_seek(s->pb, smk->next_frame_pos, SEEK_SET); + smk->next_audio_index = 0; smk->cur_frame++; return ret; } @@ -368,10 +363,7 @@ next_frame: static int smacker_read_close(AVFormatContext *s) { SmackerContext *smk = s->priv_data; - int i; - for(i = 0; i < 7; i++) - av_freep(&smk->bufs[i]); av_freep(&smk->frm_size); av_freep(&smk->frm_flags);