[FFmpeg-devel] [PATCH 1/8] lavf: add internal demuxer helpers for subtitles.
Clément Bœsch
ubitux at gmail.com
Fri Jun 15 19:29:08 CEST 2012
---
libavformat/Makefile | 1 +
libavformat/subtitles.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++
libavformat/subtitles.h | 61 ++++++++++++++++++++++++++++
3 files changed, 164 insertions(+)
create mode 100644 libavformat/subtitles.c
create mode 100644 libavformat/subtitles.h
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 5e4f002..d53a3e0 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -19,6 +19,7 @@ OBJS = allformats.o \
riff.o \
sdp.o \
seek.o \
+ subtitles.o \
utils.o \
OBJS-$(CONFIG_NETWORK) += network.o
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
new file mode 100644
index 0000000..ba38842
--- /dev/null
+++ b/libavformat/subtitles.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "subtitles.h"
+
+FFDemuxSubEntry *ff_subtitles_queue_insert_event(FFDemuxSubtitlesQueue *q,
+ const uint8_t *event, int len,
+ int merge)
+{
+ FFDemuxSubEntry *subs, *sub;
+
+ if (merge && q->nsub > 0) {
+ /* merge with previous event */
+
+ uint8_t *tmp;
+ sub = &q->subs[q->nsub - 1];
+ tmp = av_realloc(sub->event, sub->len + len + 1);
+ if (!tmp)
+ return NULL;
+ sub->event = tmp;
+ memcpy(sub->event + sub->len, event, len);
+ sub->len += len;
+ } else {
+ /* new event */
+
+ subs = av_realloc_f(q->subs, q->nsub + 1, sizeof(*q->subs));
+ if (!subs)
+ return NULL;
+ q->subs = subs;
+ sub = &subs[q->nsub++];
+ sub->event = av_malloc(len + 1);
+ sub->len = len;
+ if (!sub->event)
+ return NULL;
+ memcpy(sub->event, event, len);
+ }
+ sub->event[sub->len] = 0;
+ return sub;
+}
+
+static int cmp_pkt_sub(const void *a, const void *b)
+{
+ return ((const FFDemuxSubEntry*)a)->start - ((const FFDemuxSubEntry*)b)->start;
+}
+
+void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
+{
+ int i;
+
+ for (i = 0; i < q->nsub; i++)
+ if (q->subs[i].duration == -1 && i < q->nsub - 1)
+ q->subs[i].duration = q->subs[i + 1].start - q->subs[i].start;
+ qsort(q->subs, q->nsub, sizeof(*q->subs), cmp_pkt_sub);
+}
+
+int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q,
+ AVPacket *pkt, int i)
+{
+ int res;
+ FFDemuxSubEntry *sub = q->subs + i;
+
+ if (i == q->nsub)
+ return AVERROR_EOF;
+ res = av_new_packet(pkt, sub->len + 1);
+ if (res)
+ return res;
+ memcpy(pkt->data, sub->event, sub->len);
+ pkt->data[sub->len] = 0;
+ pkt->flags |= AV_PKT_FLAG_KEY;
+ pkt->pos = sub->pos;
+ pkt->pts = pkt->dts = sub->start;
+ pkt->duration = sub->duration;
+ return 0;
+}
+
+void ff_subtitles_queue_free(FFDemuxSubtitlesQueue *q)
+{
+ int i;
+
+ for (i = 0; i < q->nsub; i++)
+ av_freep(&q->subs[i].event);
+ av_freep(&q->subs);
+ q->nsub = 0;
+}
diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h
new file mode 100644
index 0000000..993d026
--- /dev/null
+++ b/libavformat/subtitles.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFORMAT_SUBTITLES_H
+#define AVFORMAT_SUBTITLES_H
+
+typedef struct {
+ uint8_t *event; ///< allocated subtitle line, may not be zero-terminated
+ int len; ///< subtitle event length
+ int64_t pos; ///< offset position
+ int64_t start; ///< timestamp start
+ int duration; ///< event duration
+} FFDemuxSubEntry;
+
+typedef struct {
+ FFDemuxSubEntry *subs;
+ int nsub;
+} FFDemuxSubtitlesQueue;
+
+/**
+ * Insert a new subtitle event
+ */
+FFDemuxSubEntry *ff_subtitles_queue_insert_event(FFDemuxSubtitlesQueue *q,
+ const uint8_t *event, int len,
+ int merge);
+
+/**
+ * Set missing durations and sort subtitles by PTS
+ */
+void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q);
+
+/**
+ * Generic read_packet() callback for subtitles demuxers using this queue
+ * system
+ */
+int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q,
+ AVPacket *pkt, int i);
+
+/**
+ * Remove and free all the events
+ */
+void ff_subtitles_queue_free(FFDemuxSubtitlesQueue *q);
+
+#endif /* AVFORMAT_SUBTITLES_H */
--
1.7.10.4
More information about the ffmpeg-devel
mailing list