mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-11-21 10:55:51 +02:00
avformat/subtitles: use av_packet_alloc() to allocate packets
Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
parent
fd23efb20b
commit
98a776b5e3
@ -250,7 +250,7 @@ static int jacosub_read_header(AVFormatContext *s)
|
|||||||
/* SHIFT and TIMERES affect the whole script so packet timing can only be
|
/* SHIFT and TIMERES affect the whole script so packet timing can only be
|
||||||
* done in a second pass */
|
* done in a second pass */
|
||||||
for (i = 0; i < jacosub->q.nb_subs; i++) {
|
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);
|
read_ts(jacosub, sub->data, &sub->pts, &sub->duration);
|
||||||
}
|
}
|
||||||
ff_subtitles_queue_finalize(s, &jacosub->q);
|
ff_subtitles_queue_finalize(s, &jacosub->q);
|
||||||
|
@ -934,7 +934,7 @@ static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|||||||
if (tmpq->current_sub_idx >= tmpq->nb_subs)
|
if (tmpq->current_sub_idx >= tmpq->nb_subs)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ts = tmpq->subs[tmpq->current_sub_idx].pts;
|
ts = tmpq->subs[tmpq->current_sub_idx]->pts;
|
||||||
if (ts < min_ts) {
|
if (ts < min_ts) {
|
||||||
min_ts = ts;
|
min_ts = ts;
|
||||||
sid = i;
|
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
|
/* compute maximum packet size using the next packet position. This is
|
||||||
* useful when the len in the header is non-sense */
|
* useful when the len in the header is non-sense */
|
||||||
if (q->current_sub_idx < q->nb_subs) {
|
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 {
|
} else {
|
||||||
int64_t fsize = avio_size(pb);
|
int64_t fsize = avio_size(pb);
|
||||||
psize = fsize < 0 ? 0xffff : fsize - pkt->pos;
|
psize = fsize < 0 ? 0xffff : fsize - pkt->pos;
|
||||||
|
@ -147,8 +147,8 @@ static int mpsub_read_header(AVFormatContext *s)
|
|||||||
if (common_factor > 1) {
|
if (common_factor > 1) {
|
||||||
common_factor = av_gcd(pts_info.num, common_factor);
|
common_factor = av_gcd(pts_info.num, common_factor);
|
||||||
for (i = 0; i < mpsub->q.nb_subs; i++) {
|
for (i = 0; i < mpsub->q.nb_subs; i++) {
|
||||||
mpsub->q.subs[i].pts /= common_factor;
|
mpsub->q.subs[i]->pts /= common_factor;
|
||||||
mpsub->q.subs[i].duration /= common_factor;
|
mpsub->q.subs[i]->duration /= common_factor;
|
||||||
}
|
}
|
||||||
pts_info.num /= common_factor;
|
pts_info.num /= common_factor;
|
||||||
}
|
}
|
||||||
|
@ -111,13 +111,13 @@ int ff_text_peek_r8(FFTextReader *r)
|
|||||||
AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
|
AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
|
||||||
const uint8_t *event, size_t len, int merge)
|
const uint8_t *event, size_t len, int merge)
|
||||||
{
|
{
|
||||||
AVPacket *subs, *sub;
|
AVPacket **subs, *sub;
|
||||||
|
|
||||||
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 (av_grow_packet(sub, len) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -132,10 +132,14 @@ AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
|
|||||||
if (!subs)
|
if (!subs)
|
||||||
return NULL;
|
return NULL;
|
||||||
q->subs = subs;
|
q->subs = subs;
|
||||||
sub = &subs[q->nb_subs];
|
sub = av_packet_alloc();
|
||||||
if (av_new_packet(sub, len) < 0)
|
if (!sub)
|
||||||
return NULL;
|
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->flags |= AV_PKT_FLAG_KEY;
|
||||||
sub->pts = sub->dts = 0;
|
sub->pts = sub->dts = 0;
|
||||||
memcpy(sub->data, event, len);
|
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)
|
static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
const AVPacket *s1 = a;
|
const AVPacket *s1 = *(const AVPacket **)a;
|
||||||
const AVPacket *s2 = b;
|
const AVPacket *s2 = *(const AVPacket **)b;
|
||||||
if (s1->pts == s2->pts)
|
if (s1->pts == s2->pts)
|
||||||
return FFDIFFSIGN(s1->pos, s2->pos);
|
return FFDIFFSIGN(s1->pos, s2->pos);
|
||||||
return FFDIFFSIGN(s1->pts , s2->pts);
|
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)
|
static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
const AVPacket *s1 = a;
|
const AVPacket *s1 = *(const AVPacket **)a;
|
||||||
const AVPacket *s2 = b;
|
const AVPacket *s2 = *(const AVPacket **)b;
|
||||||
if (s1->pos == s2->pos) {
|
if (s1->pos == s2->pos) {
|
||||||
if (s1->pts == s2->pts)
|
if (s1->pts == s2->pts)
|
||||||
return 0;
|
return 0;
|
||||||
@ -170,18 +174,18 @@ static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q)
|
|||||||
|
|
||||||
for (i = 1; i < q->nb_subs; i++) {
|
for (i = 1; i < q->nb_subs; i++) {
|
||||||
const int last_id = i - 1 - drop;
|
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 &&
|
if (q->subs[i]->pts == last->pts &&
|
||||||
q->subs[i].duration == last->duration &&
|
q->subs[i]->duration == last->duration &&
|
||||||
q->subs[i].stream_index == last->stream_index &&
|
q->subs[i]->stream_index == last->stream_index &&
|
||||||
!strcmp(q->subs[i].data, last->data)) {
|
!strcmp(q->subs[i]->data, last->data)) {
|
||||||
|
|
||||||
av_packet_unref(&q->subs[i]);
|
av_packet_free(&q->subs[i]);
|
||||||
drop++;
|
drop++;
|
||||||
} else if (drop) {
|
} else if (drop) {
|
||||||
q->subs[last_id + 1] = q->subs[i];
|
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
|
q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos
|
||||||
: cmp_pkt_sub_pos_ts);
|
: cmp_pkt_sub_pos_ts);
|
||||||
for (i = 0; i < q->nb_subs; i++)
|
for (i = 0; i < q->nb_subs; i++)
|
||||||
if (q->subs[i].duration < 0 && i < q->nb_subs - 1)
|
if (q->subs[i]->duration < 0 && i < q->nb_subs - 1)
|
||||||
q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
|
q->subs[i]->duration = q->subs[i + 1]->pts - q->subs[i]->pts;
|
||||||
|
|
||||||
if (!q->keep_duplicates)
|
if (!q->keep_duplicates)
|
||||||
drop_dups(log_ctx, q);
|
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)
|
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
|
||||||
{
|
{
|
||||||
AVPacket *sub = q->subs + q->current_sub_idx;
|
AVPacket *sub;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (q->current_sub_idx == q->nb_subs)
|
if (q->current_sub_idx == q->nb_subs)
|
||||||
return AVERROR_EOF;
|
return AVERROR_EOF;
|
||||||
|
sub = q->subs[q->current_sub_idx];
|
||||||
if ((ret = av_packet_ref(pkt, sub)) < 0) {
|
if ((ret = av_packet_ref(pkt, sub)) < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -238,9 +243,9 @@ static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
|
|||||||
if (s1 == s2)
|
if (s1 == s2)
|
||||||
return s1;
|
return s1;
|
||||||
if (s1 == s2 - 1)
|
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;
|
mid = (s1 + s2) / 2;
|
||||||
if (q->subs[mid].pts <= ts)
|
if (q->subs[mid]->pts <= ts)
|
||||||
s1 = mid;
|
s1 = mid;
|
||||||
else
|
else
|
||||||
s2 = mid;
|
s2 = mid;
|
||||||
@ -262,24 +267,24 @@ int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int st
|
|||||||
|
|
||||||
if (idx < 0)
|
if (idx < 0)
|
||||||
return idx;
|
return idx;
|
||||||
for (i = idx; i < q->nb_subs && q->subs[i].pts < min_ts; i++)
|
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)
|
if (stream_index == -1 || q->subs[i]->stream_index == stream_index)
|
||||||
idx = i;
|
idx = i;
|
||||||
for (i = idx; i > 0 && q->subs[i].pts > max_ts; i--)
|
for (i = idx; i > 0 && q->subs[i]->pts > max_ts; i--)
|
||||||
if (stream_index == -1 || q->subs[i].stream_index == stream_index)
|
if (stream_index == -1 || q->subs[i]->stream_index == stream_index)
|
||||||
idx = i;
|
idx = i;
|
||||||
|
|
||||||
ts_selected = q->subs[idx].pts;
|
ts_selected = q->subs[idx]->pts;
|
||||||
if (ts_selected < min_ts || ts_selected > max_ts)
|
if (ts_selected < min_ts || ts_selected > max_ts)
|
||||||
return AVERROR(ERANGE);
|
return AVERROR(ERANGE);
|
||||||
|
|
||||||
/* look back in the latest subtitles for overlapping subtitles */
|
/* look back in the latest subtitles for overlapping subtitles */
|
||||||
for (i = idx - 1; i >= 0; i--) {
|
for (i = idx - 1; i >= 0; i--) {
|
||||||
int64_t pts = q->subs[i].pts;
|
int64_t pts = q->subs[i]->pts;
|
||||||
if (q->subs[i].duration <= 0 ||
|
if (q->subs[i]->duration <= 0 ||
|
||||||
(stream_index != -1 && q->subs[i].stream_index != stream_index))
|
(stream_index != -1 && q->subs[i]->stream_index != stream_index))
|
||||||
continue;
|
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;
|
idx = i;
|
||||||
else
|
else
|
||||||
break;
|
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
|
* queue is ordered by pts and then filepos, so we can take the first
|
||||||
* entry for a given timestamp. */
|
* entry for a given timestamp. */
|
||||||
if (stream_index == -1)
|
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--;
|
idx--;
|
||||||
|
|
||||||
q->current_sub_idx = idx;
|
q->current_sub_idx = idx;
|
||||||
@ -304,7 +309,7 @@ void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < q->nb_subs; 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);
|
av_freep(&q->subs);
|
||||||
q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
|
q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ int ff_text_peek_r8(FFTextReader *r);
|
|||||||
void ff_text_read(FFTextReader *r, char *buf, size_t size);
|
void ff_text_read(FFTextReader *r, char *buf, size_t size);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
AVPacket *subs; ///< array of subtitles packets
|
AVPacket **subs; ///< array of subtitles packets
|
||||||
int nb_subs; ///< number of subtitles packets
|
int nb_subs; ///< number of subtitles packets
|
||||||
int allocated_size; ///< allocated size for subs
|
int allocated_size; ///< allocated size for subs
|
||||||
int current_sub_idx; ///< current position for the read packet callback
|
int current_sub_idx; ///< current position for the read packet callback
|
||||||
|
@ -293,9 +293,9 @@ static av_cold int tedcaptions_read_header(AVFormatContext *avf)
|
|||||||
}
|
}
|
||||||
ff_subtitles_queue_finalize(avf, &tc->subs);
|
ff_subtitles_queue_finalize(avf, &tc->subs);
|
||||||
for (i = 0; i < tc->subs.nb_subs; i++)
|
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_type = AVMEDIA_TYPE_SUBTITLE;
|
||||||
st->codecpar->codec_id = AV_CODEC_ID_TEXT;
|
st->codecpar->codec_id = AV_CODEC_ID_TEXT;
|
||||||
avpriv_set_pts_info(st, 64, 1, 1000);
|
avpriv_set_pts_info(st, 64, 1, 1000);
|
||||||
|
Loading…
Reference in New Issue
Block a user