[FFmpeg-cvslog] avformat/subtitles: use av_packet_alloc() to allocate packets

James Almer git at videolan.org
Wed Mar 17 21:17:05 EET 2021


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Wed Feb  3 20:55:27 2021 -0300| [98a776b5e32c3c7b535d34f9645757b242114d63] | committer: James Almer

avformat/subtitles: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=98a776b5e32c3c7b535d34f9645757b242114d63
---

 libavformat/jacosubdec.c     |  2 +-
 libavformat/mpeg.c           |  4 +--
 libavformat/mpsubdec.c       |  4 +--
 libavformat/subtitles.c      | 69 ++++++++++++++++++++++++--------------------
 libavformat/subtitles.h      |  2 +-
 libavformat/tedcaptionsdec.c |  4 +--
 6 files changed, 45 insertions(+), 40 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..05c07cd852 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,10 +132,14 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
         if (!subs)
             return NULL;
         q->subs = subs;
-        sub = &subs[q->nb_subs];
-        if (av_new_packet(sub, len) < 0)
+        sub = av_packet_alloc();
+        if (!sub)
             return NULL;
-        q->nb_subs++;
+        if (av_new_packet(sub, len) < 0) {
+            av_packet_free(&sub);
+            return NULL;
+        }
+        subs[q->nb_subs++] = sub;
         sub->flags |= AV_PKT_FLAG_KEY;
         sub->pts = sub->dts = 0;
         memcpy(sub->data, event, len);
@@ -145,8 +149,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 +158,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 +174,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 +206,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 +215,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 +243,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 +267,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 +296,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 +309,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-cvslog mailing list