From patchwork Thu Feb 4 19:09:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 25412 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 E391F4480C6 for ; Thu, 4 Feb 2021 21:11:36 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDC7968A9C8; Thu, 4 Feb 2021 21:11:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0A79D689AB3 for ; Thu, 4 Feb 2021 21:11:34 +0200 (EET) Received: by mail-qt1-f175.google.com with SMTP id e11so3224604qtg.6 for ; Thu, 04 Feb 2021 11:11:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Hk3DiNja2vyA2nWGuVCjhzaO6j80YakNzOUbHRsPdWQ=; b=jxIF3Cu7MpDVVe4XOopwjr4wOEUgUZFlX7I2tuVvMuEfDCZpagflqAQ2xB0ybB1QL5 VfItXNCJ91zf1gJkZ+sw1xwkLjphMYeDRa4Zhj2U0o9BXLx2OGQnNy6H5k1zRfSQQGj0 ByOtN5tV5+PEzqhCZt2hvjZIdjGQfwtcUVhtT3wPshgJyFHIwS/D9EesldVD8DViicmn wxIlqTOg16pv7VG8QTixdRuqRNNtKWmrAd9MJICwdhQUnIsKcA/6R+7EMIfAWtMCtDLu 0tEw99GMpF0E7UskBIndnsWvqUob5rEvwhwCU/R3RHlgu+hYNY31sbHf7QFvCjwtQ2rj 5unw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Hk3DiNja2vyA2nWGuVCjhzaO6j80YakNzOUbHRsPdWQ=; b=A49M3/mCEXSGVDYP8ENY+eSTuRLcMKDq0yXQPAoMmp3IBONRFLFdGPbpyrJ8qSeOfA kITTlN/9GArkaq12auZO4PRE3rUJvVNyPbxJYbZcjcwEoH+R7uM5vlWuQek67LYD0Xpw EkjTs7laEpmgsJfnVkaqrMyROenfoHhqS73iEXUVD7G4k/hAxf/ovp6BWMu89dguVfs1 cqmJGM8JfICo0lELBj8vOpCszK79D+DsjB+wG2sGyEpmUjD4y0YWlPdDTlctuOHiuG2f V7M1buSZ/AD0IzvqjCyH+sfbZNwhtRhcfYJzddAkizso7lRK4ptgrVEFPmOVBzvkqHiY SE+g== X-Gm-Message-State: AOAM53287aiBwMKaUiAzZw70ujDQ5mAFO6Jpu9D8Ouq0FmLS8tQlsKKT fwlXg3MkLFjYs9fbJOgjoiMSUe4rcTQ= X-Google-Smtp-Source: ABdhPJyeBSqyDY7XiAFmvKnHjPsvVIqqnamkUFrPUCRQD/z9t1bDpaenEI0RCz+HvYPFWcujj/Ml8w== X-Received: by 2002:ac8:5a4b:: with SMTP id o11mr1072652qta.202.1612465892427; Thu, 04 Feb 2021 11:11:32 -0800 (PST) Received: from localhost.localdomain ([181.23.64.183]) by smtp.gmail.com with ESMTPSA id t14sm5889291qkt.50.2021.02.04.11.11.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 11:11:31 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Feb 2021 16:09:45 -0300 Message-Id: <20210204191005.48190-31-jamrial@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210204191005.48190-1-jamrial@gmail.com> References: <20210204191005.48190-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 30/50] avformat/subtitles: use av_packet_alloc() to allocate packets 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- libavformat/jacosubdec.c | 2 +- libavformat/mpeg.c | 4 +-- libavformat/mpsubdec.c | 4 +-- libavformat/subtitles.c | 64 +++++++++++++++++++----------------- libavformat/subtitles.h | 2 +- libavformat/tedcaptionsdec.c | 4 +-- 6 files changed, 42 insertions(+), 38 deletions(-) diff --git a/libavformat/jacosubdec.c b/libavformat/jacosubdec.c index 14221b166c..b44e3b7783 100644 --- a/libavformat/jacosubdec.c +++ b/libavformat/jacosubdec.c @@ -250,7 +250,7 @@ static int jacosub_read_header(AVFormatContext *s) /* SHIFT and TIMERES affect the whole script so packet timing can only be * done in a second pass */ for (i = 0; i < jacosub->q.nb_subs; i++) { - AVPacket *sub = &jacosub->q.subs[i]; + AVPacket *sub = jacosub->q.subs[i]; read_ts(jacosub, sub->data, &sub->pts, &sub->duration); } ff_subtitles_queue_finalize(s, &jacosub->q); diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index 20d1e10168..79610ec600 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -934,7 +934,7 @@ static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt) if (tmpq->current_sub_idx >= tmpq->nb_subs) continue; - ts = tmpq->subs[tmpq->current_sub_idx].pts; + ts = tmpq->subs[tmpq->current_sub_idx]->pts; if (ts < min_ts) { min_ts = ts; sid = i; @@ -950,7 +950,7 @@ static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt) /* compute maximum packet size using the next packet position. This is * useful when the len in the header is non-sense */ if (q->current_sub_idx < q->nb_subs) { - psize = q->subs[q->current_sub_idx].pos - pkt->pos; + psize = q->subs[q->current_sub_idx]->pos - pkt->pos; } else { int64_t fsize = avio_size(pb); psize = fsize < 0 ? 0xffff : fsize - pkt->pos; diff --git a/libavformat/mpsubdec.c b/libavformat/mpsubdec.c index 2e6dc883eb..c113be5eba 100644 --- a/libavformat/mpsubdec.c +++ b/libavformat/mpsubdec.c @@ -147,8 +147,8 @@ static int mpsub_read_header(AVFormatContext *s) if (common_factor > 1) { common_factor = av_gcd(pts_info.num, common_factor); for (i = 0; i < mpsub->q.nb_subs; i++) { - mpsub->q.subs[i].pts /= common_factor; - mpsub->q.subs[i].duration /= common_factor; + mpsub->q.subs[i]->pts /= common_factor; + mpsub->q.subs[i]->duration /= common_factor; } pts_info.num /= common_factor; } diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c index ad7f68938e..ec10b99822 100644 --- a/libavformat/subtitles.c +++ b/libavformat/subtitles.c @@ -111,13 +111,13 @@ int ff_text_peek_r8(FFTextReader *r) AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge) { - AVPacket *subs, *sub; + AVPacket **subs, *sub; if (merge && q->nb_subs > 0) { /* merge with previous event */ int old_len; - sub = &q->subs[q->nb_subs - 1]; + sub = q->subs[q->nb_subs - 1]; old_len = sub->size; if (av_grow_packet(sub, len) < 0) return NULL; @@ -132,7 +132,10 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, if (!subs) return NULL; q->subs = subs; - sub = &subs[q->nb_subs]; + subs[q->nb_subs] = av_packet_alloc(); + if (!subs[q->nb_subs]) + return NULL; + sub = subs[q->nb_subs]; if (av_new_packet(sub, len) < 0) return NULL; q->nb_subs++; @@ -145,8 +148,8 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, static int cmp_pkt_sub_ts_pos(const void *a, const void *b) { - const AVPacket *s1 = a; - const AVPacket *s2 = b; + const AVPacket *s1 = *(const AVPacket **)a; + const AVPacket *s2 = *(const AVPacket **)b; if (s1->pts == s2->pts) return FFDIFFSIGN(s1->pos, s2->pos); return FFDIFFSIGN(s1->pts , s2->pts); @@ -154,8 +157,8 @@ static int cmp_pkt_sub_ts_pos(const void *a, const void *b) static int cmp_pkt_sub_pos_ts(const void *a, const void *b) { - const AVPacket *s1 = a; - const AVPacket *s2 = b; + const AVPacket *s1 = *(const AVPacket **)a; + const AVPacket *s2 = *(const AVPacket **)b; if (s1->pos == s2->pos) { if (s1->pts == s2->pts) return 0; @@ -170,18 +173,18 @@ static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q) for (i = 1; i < q->nb_subs; i++) { const int last_id = i - 1 - drop; - const AVPacket *last = &q->subs[last_id]; + const AVPacket *last = q->subs[last_id]; - if (q->subs[i].pts == last->pts && - q->subs[i].duration == last->duration && - q->subs[i].stream_index == last->stream_index && - !strcmp(q->subs[i].data, last->data)) { + if (q->subs[i]->pts == last->pts && + q->subs[i]->duration == last->duration && + q->subs[i]->stream_index == last->stream_index && + !strcmp(q->subs[i]->data, last->data)) { - av_packet_unref(&q->subs[i]); + av_packet_free(&q->subs[i]); drop++; } else if (drop) { q->subs[last_id + 1] = q->subs[i]; - memset(&q->subs[i], 0, sizeof(q->subs[i])); // for safety + q->subs[i] = NULL; } } @@ -202,8 +205,8 @@ void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q) q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos : cmp_pkt_sub_pos_ts); for (i = 0; i < q->nb_subs; i++) - if (q->subs[i].duration < 0 && i < q->nb_subs - 1) - q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts; + if (q->subs[i]->duration < 0 && i < q->nb_subs - 1) + q->subs[i]->duration = q->subs[i + 1]->pts - q->subs[i]->pts; if (!q->keep_duplicates) drop_dups(log_ctx, q); @@ -211,11 +214,12 @@ void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q) int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt) { - AVPacket *sub = q->subs + q->current_sub_idx; + AVPacket *sub; int ret; if (q->current_sub_idx == q->nb_subs) return AVERROR_EOF; + sub = q->subs[q->current_sub_idx]; if ((ret = av_packet_ref(pkt, sub)) < 0) { return ret; } @@ -238,9 +242,9 @@ static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts) if (s1 == s2) return s1; if (s1 == s2 - 1) - return q->subs[s1].pts <= q->subs[s2].pts ? s1 : s2; + return q->subs[s1]->pts <= q->subs[s2]->pts ? s1 : s2; mid = (s1 + s2) / 2; - if (q->subs[mid].pts <= ts) + if (q->subs[mid]->pts <= ts) s1 = mid; else s2 = mid; @@ -262,24 +266,24 @@ int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int st if (idx < 0) return idx; - for (i = idx; i < q->nb_subs && q->subs[i].pts < min_ts; i++) - if (stream_index == -1 || q->subs[i].stream_index == stream_index) + for (i = idx; i < q->nb_subs && q->subs[i]->pts < min_ts; i++) + if (stream_index == -1 || q->subs[i]->stream_index == stream_index) idx = i; - for (i = idx; i > 0 && q->subs[i].pts > max_ts; i--) - if (stream_index == -1 || q->subs[i].stream_index == stream_index) + for (i = idx; i > 0 && q->subs[i]->pts > max_ts; i--) + if (stream_index == -1 || q->subs[i]->stream_index == stream_index) idx = i; - ts_selected = q->subs[idx].pts; + ts_selected = q->subs[idx]->pts; if (ts_selected < min_ts || ts_selected > max_ts) return AVERROR(ERANGE); /* look back in the latest subtitles for overlapping subtitles */ for (i = idx - 1; i >= 0; i--) { - int64_t pts = q->subs[i].pts; - if (q->subs[i].duration <= 0 || - (stream_index != -1 && q->subs[i].stream_index != stream_index)) + int64_t pts = q->subs[i]->pts; + if (q->subs[i]->duration <= 0 || + (stream_index != -1 && q->subs[i]->stream_index != stream_index)) continue; - if (pts >= min_ts && pts > ts_selected - q->subs[i].duration) + if (pts >= min_ts && pts > ts_selected - q->subs[i]->duration) idx = i; else break; @@ -291,7 +295,7 @@ int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int st * queue is ordered by pts and then filepos, so we can take the first * entry for a given timestamp. */ if (stream_index == -1) - while (idx > 0 && q->subs[idx - 1].pts == q->subs[idx].pts) + while (idx > 0 && q->subs[idx - 1]->pts == q->subs[idx]->pts) idx--; q->current_sub_idx = idx; @@ -304,7 +308,7 @@ void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q) int i; for (i = 0; i < q->nb_subs; i++) - av_packet_unref(&q->subs[i]); + av_packet_free(&q->subs[i]); av_freep(&q->subs); q->nb_subs = q->allocated_size = q->current_sub_idx = 0; } diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h index 6b418e3621..ca769639be 100644 --- a/libavformat/subtitles.h +++ b/libavformat/subtitles.h @@ -100,7 +100,7 @@ int ff_text_peek_r8(FFTextReader *r); void ff_text_read(FFTextReader *r, char *buf, size_t size); typedef struct { - AVPacket *subs; ///< array of subtitles packets + AVPacket **subs; ///< array of subtitles packets int nb_subs; ///< number of subtitles packets int allocated_size; ///< allocated size for subs int current_sub_idx; ///< current position for the read packet callback diff --git a/libavformat/tedcaptionsdec.c b/libavformat/tedcaptionsdec.c index c15aeea06c..9a882c4d79 100644 --- a/libavformat/tedcaptionsdec.c +++ b/libavformat/tedcaptionsdec.c @@ -293,9 +293,9 @@ static av_cold int tedcaptions_read_header(AVFormatContext *avf) } ff_subtitles_queue_finalize(avf, &tc->subs); for (i = 0; i < tc->subs.nb_subs; i++) - tc->subs.subs[i].pts += tc->start_time; + tc->subs.subs[i]->pts += tc->start_time; - last = &tc->subs.subs[tc->subs.nb_subs - 1]; + last = tc->subs.subs[tc->subs.nb_subs - 1]; st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codecpar->codec_id = AV_CODEC_ID_TEXT; avpriv_set_pts_info(st, 64, 1, 1000);