summaryrefslogtreecommitdiff
path: root/video_writer.c
diff options
context:
space:
mode:
authorDmitry Kovalev <dkovalev@google.com>2014-02-05 18:34:46 -0800
committerDmitry Kovalev <dkovalev@google.com>2014-02-05 20:34:51 -0800
commit37e6fd3d765e192f24de65c472fb0cef6a3d9a77 (patch)
tree3a09237d98b4eff48e6a41bef84479ee537a48f0 /video_writer.c
parentcebda1b65cf821b3dd7bbdd3a93c8e2bfe9b499b (diff)
downloadlibvpx-37e6fd3d765e192f24de65c472fb0cef6a3d9a77.tar.gz
Adding video reader/writer APIs.
Right now only IVF format is supported which is enough for example code. Other formats like y4m, webm, raw yuv will be supported later. Change-Id: I34c6f20731c1851947587ca5c589d7856b675164
Diffstat (limited to 'video_writer.c')
-rw-r--r--video_writer.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/video_writer.c b/video_writer.c
new file mode 100644
index 000000000..3695236bf
--- /dev/null
+++ b/video_writer.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdlib.h>
+
+#include "./ivfenc.h"
+#include "./video_writer.h"
+#include "vpx/vpx_encoder.h"
+
+struct VpxVideoWriterStruct {
+ VpxVideoInfo info;
+ FILE *file;
+ int frame_count;
+};
+
+static void write_header(FILE *file, const VpxVideoInfo *info,
+ int frame_count) {
+ struct vpx_codec_enc_cfg cfg;
+ cfg.g_w = info->frame_width;
+ cfg.g_h = info->frame_height;
+ cfg.g_timebase.num = info->time_base.numerator;
+ cfg.g_timebase.den = info->time_base.denominator;
+
+ ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
+}
+
+VpxVideoWriter *vpx_video_writer_open(const char *filename,
+ VpxContainer container,
+ const VpxVideoInfo *info) {
+ if (container == kContainerIVF) {
+ VpxVideoWriter *writer = NULL;
+ FILE *const file = fopen(filename, "wb");
+ if (!file)
+ return NULL;
+
+ writer = malloc(sizeof(*writer));
+ if (!writer)
+ return NULL;
+
+ writer->frame_count = 0;
+ writer->info = *info;
+ writer->file = file;
+
+ write_header(writer->file, info, 0);
+
+ return writer;
+ }
+
+ return NULL;
+}
+
+void vpx_video_writer_close(VpxVideoWriter *writer) {
+ if (writer) {
+ // Rewriting frame header with real frame count
+ rewind(writer->file);
+ write_header(writer->file, &writer->info, writer->frame_count);
+
+ fclose(writer->file);
+ free(writer);
+ }
+}
+
+int vpx_video_writer_write_frame(VpxVideoWriter *writer,
+ const uint8_t *buffer, size_t size,
+ int64_t pts) {
+ ivf_write_frame_header(writer->file, pts, size);
+ if (fwrite(buffer, 1, size, writer->file) != size)
+ return 0;
+
+ ++writer->frame_count;
+
+ return 1;
+}