[FFmpeg-devel] [PATCH 3/5] lavf: add text_file API helpers.
Nicolas George
nicolas.george at normalesup.org
Thu Aug 8 17:03:33 CEST 2013
TODO: version bump & APIChanges entry.
TODO: update srtdec and other demuxers to use this.
Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
libavformat/Makefile | 2 +
libavformat/avio.h | 4 ++
libavformat/text_file.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 123 insertions(+)
create mode 100644 libavformat/text_file.c
diff --git a/libavformat/Makefile b/libavformat/Makefile
index b8872b3..4119580 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -21,6 +21,7 @@ OBJS = allformats.o \
riff.o \
sdp.o \
seek.o \
+ text_file.o \
url.o \
utils.o \
@@ -461,6 +462,7 @@ SKIPHEADERS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpdh.h
SKIPHEADERS-$(CONFIG_NETWORK) += network.h rtsp.h
TESTPROGS = seek \
srtp \
+ text_file \
url \
TESTPROGS-$(CONFIG_NETWORK) += noproxy
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 5bdbc62..7f51ee6 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -478,4 +478,8 @@ int avio_pause(AVIOContext *h, int pause);
int64_t avio_seek_time(AVIOContext *h, int stream_index,
int64_t timestamp, int flags);
+struct AVTextFile;
+int av_text_file_read_url(struct AVTextFile *tf, const char *url);
+int av_text_file_read_pb(struct AVTextFile *tf, AVIOContext *pb);
+
#endif /* AVFORMAT_AVIO_H */
diff --git a/libavformat/text_file.c b/libavformat/text_file.c
new file mode 100644
index 0000000..da3c3df
--- /dev/null
+++ b/libavformat/text_file.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2013 Nicolas George
+ *
+ * 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 "avio.h"
+#include "libavutil/avassert.h"
+#include "libavutil/bprint.h"
+#include "libavutil/text_file.h"
+
+int av_text_file_read_pb(struct AVTextFile *tf, AVIOContext *pb)
+{
+ return av_text_file_read_callback(tf, (AVTextFileRead)avio_read, pb);
+}
+
+int av_text_file_read_url(AVTextFile *tf, const char *url)
+{
+ AVIOContext *pb = NULL;
+ int ret;
+
+ if ((ret = avio_open(&pb, url, AVIO_FLAG_READ)) < 0)
+ return ret;
+ ret = av_text_file_read_pb(tf, pb);
+ avio_closep(&pb);
+ return ret;
+}
+
+#ifdef TEST
+
+#include "avformat.h"
+
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if !HAVE_GETOPT
+#include "compat/getopt.c"
+#endif
+
+static void help(int ret, const char *self)
+{
+ fprintf(ret ? stderr : stdout,
+ "Usage: %s [-c] [-l] [-e enc1:enc2:...] file\n", self);
+ exit(ret);
+}
+
+int main(int argc, char **argv)
+{
+ int ret, opt;
+ AVTextFile tf = { AV_TEXT_FILE_DEFAULT };
+ size_t i;
+
+ while ((opt = getopt(argc, argv, "ce:lh")) != -1) {
+ switch (opt) {
+ case 'c':
+ tf.flags |= AV_TEXT_FLAG_REMOVE_CR;
+ break;
+ case 'l':
+ tf.flags |= AV_TEXT_FLAG_SPLIT_LINES;
+ break;
+ case 'e':
+ {
+ const char *encs = optarg;
+ char *enc;
+ int nb_enc = 0;
+
+ while (*encs && (enc = av_get_token(&encs, ":"))) {
+ av_dynarray_add(&tf.encodings, &nb_enc, enc);
+ encs += *encs == ':';
+ }
+ av_dynarray_add(&tf.encodings, &nb_enc, NULL);
+ }
+ break;
+ default:
+ help(opt != 'h', argv[0]);
+ }
+ }
+ if (argc - optind != 1)
+ help(1, argv[0]);
+
+ av_register_all();
+ ret = av_text_file_read_url(&tf, argv[optind]);
+ if (ret < 0) {
+ printf("av_text_file_read_url: %s\n", tf.error);
+ av_text_file_free(&tf);
+ return 1;
+ }
+ printf("Decoded %zd bytes from '%s'\n", tf.text_size, tf.encoding);
+ printf(" flag: BOM: %d\n", !!(tf.text_flags & AV_TEXT_FLAG_HAS_BOM));
+ printf(" flag: no EOL: %d\n", !!(tf.text_flags & AV_TEXT_FLAG_NO_EOL));
+ for (i = 0; i < tf.nb_lines; i++)
+ if (i < 5 || tf.nb_lines - i < 5)
+ printf("Line %3zu: [%s]\n", i, tf.lines[i]);
+ av_text_file_free(&tf);
+ if (tf.encodings) {
+ for (i = 0; tf.encodings[i]; i++)
+ av_free((char *)tf.encodings[i]);
+ av_freep(&tf.encodings);
+ }
+ return 0;
+}
+
+#endif
--
1.7.10.4
More information about the ffmpeg-devel
mailing list