[FFmpeg-devel] [PATCH 30/50] avformat/subtitles: use av_packet_alloc() to allocate packets
Andreas Rheinhardt
andreas.rheinhardt at gmail.com
Mon Feb 8 17:34:00 EET 2021
James Almer:
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
> 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];
q->nb_subs++. And remove the q->nb_subs++ below. Otherwise the packet
leaks if av_new_packet fails below.
> 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);
>
More information about the ffmpeg-devel
mailing list