Skip to content

Commit

Permalink
avformat/subtitles: extend ff_subtitles_queue_insert() to support not…
Browse files Browse the repository at this point in the history
… yet available events

If ff_subtitles_queue_insert() were to given a NULL buffer
with 0 length, it would still attempt to grow the packet
or memcpy depending on if merge option is enabled.

In this commit, allow passing a NULL buffer with 0 length.
This way, if a subtitle demuxer happens to pass an empty
cue or wants to use av_get_packet() to read bytes, there
are no unnecessary operations on the packet after it is
allocated.

Signed-off-by: Marth64 <marth64@proxyid.net>
  • Loading branch information
Marth64x committed Mar 23, 2024
1 parent 5d7f234 commit 5938d87
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions libavformat/subtitles.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "avformat.h"
#include "subtitles.h"
#include "avio_internal.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"

void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Expand Down Expand Up @@ -111,12 +112,17 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
{
AVPacket **subs, *sub;

if (!event)
av_assert1(len == 0);

if (merge && q->nb_subs > 0) {
/* merge with previous event */

int old_len;
sub = q->subs[q->nb_subs - 1];
old_len = sub->size;
if (!event)
return sub;
if (av_grow_packet(sub, len) < 0)
return NULL;
memcpy(sub->data + old_len, event, len);
Expand All @@ -133,14 +139,15 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
sub = av_packet_alloc();
if (!sub)
return NULL;
if (av_new_packet(sub, len) < 0) {
if (event && 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);
if (event)
memcpy(sub->data, event, len);
}
return sub;
}
Expand Down
2 changes: 1 addition & 1 deletion libavformat/subtitles.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ typedef struct {
/**
* Insert a new subtitle event.
*
* @param event the subtitle line, may not be zero terminated
* @param event the subtitle line (not zero terminated) or NULL on not yet available event
* @param len the length of the event (in strlen() sense, so without '\0')
* @param merge set to 1 if the current event should be concatenated with the
* previous one instead of adding a new entry, 0 otherwise
Expand Down

0 comments on commit 5938d87

Please sign in to comment.