Commit 1c353336 authored by Marth64's avatar Marth64 Committed by Stefano Sabatini

avformat/subtitles: extend ff_subtitles_queue_insert() to support not yet available events

If ff_subtitles_queue_insert() were 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
without attempting to do such operations. 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: 's avatarMarth64 <marth64@proxyid.net>
parent ed936305
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "avformat.h" #include "avformat.h"
#include "subtitles.h" #include "subtitles.h"
#include "avio_internal.h" #include "avio_internal.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h" #include "libavutil/avstring.h"
#include "libavutil/mem.h" #include "libavutil/mem.h"
...@@ -112,15 +113,19 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, ...@@ -112,15 +113,19 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
{ {
AVPacket **subs, *sub; AVPacket **subs, *sub;
av_assert1(event || len == 0);
if (merge && q->nb_subs > 0) { if (merge && q->nb_subs > 0) {
/* merge with previous event */ /* merge with previous event */
int old_len; int old_len;
sub = q->subs[q->nb_subs - 1]; sub = q->subs[q->nb_subs - 1];
old_len = sub->size; old_len = sub->size;
if (av_grow_packet(sub, len) < 0) if (event) {
return NULL; if (av_grow_packet(sub, len) < 0)
memcpy(sub->data + old_len, event, len); return NULL;
memcpy(sub->data + old_len, event, len);
}
} else { } else {
/* new event */ /* new event */
...@@ -134,14 +139,16 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, ...@@ -134,14 +139,16 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
sub = av_packet_alloc(); sub = av_packet_alloc();
if (!sub) if (!sub)
return NULL; return NULL;
if (av_new_packet(sub, len) < 0) { if (event) {
av_packet_free(&sub); if (av_new_packet(sub, len) < 0) {
return NULL; av_packet_free(&sub);
return NULL;
}
memcpy(sub->data, event, len);
} }
subs[q->nb_subs++] = sub;
sub->flags |= AV_PKT_FLAG_KEY; sub->flags |= AV_PKT_FLAG_KEY;
sub->pts = sub->dts = 0; sub->pts = sub->dts = 0;
memcpy(sub->data, event, len); subs[q->nb_subs++] = sub;
} }
return sub; return sub;
} }
......
...@@ -112,7 +112,7 @@ typedef struct { ...@@ -112,7 +112,7 @@ typedef struct {
/** /**
* Insert a new subtitle event. * 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 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 * @param merge set to 1 if the current event should be concatenated with the
* previous one instead of adding a new entry, 0 otherwise * previous one instead of adding a new entry, 0 otherwise
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment