summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples.mk15
-rwxr-xr-xtest/decode_with_drops.sh75
-rwxr-xr-xtest/examples.sh28
-rwxr-xr-xtest/simple_encoder.sh58
-rw-r--r--test/test.mk14
-rwxr-xr-xtest/tools_common.sh7
-rw-r--r--webmdec.c202
-rw-r--r--webmdec.cc219
-rw-r--r--webmdec.h43
9 files changed, 431 insertions, 230 deletions
diff --git a/examples.mk b/examples.mk
index 91b980168..f6e7c0062 100644
--- a/examples.mk
+++ b/examples.mk
@@ -25,6 +25,11 @@ LIBWEBM_MUXER_SRCS += third_party/libwebm/mkvmuxer.cpp \
third_party/libwebm/mkvwriter.hpp \
third_party/libwebm/webmids.hpp
+LIBWEBM_PARSER_SRCS = third_party/libwebm/mkvparser.cpp \
+ third_party/libwebm/mkvreader.cpp \
+ third_party/libwebm/mkvparser.hpp \
+ third_party/libwebm/mkvreader.hpp
+
# List of examples to build. UTILS are tools meant for distribution
# while EXAMPLES demonstrate specific portions of the API.
UTILS-$(CONFIG_DECODERS) += vpxdec.c
@@ -39,14 +44,8 @@ vpxdec.SRCS += tools_common.c tools_common.h
vpxdec.SRCS += y4menc.c y4menc.h
vpxdec.SRCS += $(LIBYUV_SRCS)
ifeq ($(CONFIG_WEBM_IO),yes)
- vpxdec.SRCS += third_party/nestegg/halloc/halloc.h
- vpxdec.SRCS += third_party/nestegg/halloc/src/align.h
- vpxdec.SRCS += third_party/nestegg/halloc/src/halloc.c
- vpxdec.SRCS += third_party/nestegg/halloc/src/hlist.h
- vpxdec.SRCS += third_party/nestegg/halloc/src/macros.h
- vpxdec.SRCS += third_party/nestegg/include/nestegg/nestegg.h
- vpxdec.SRCS += third_party/nestegg/src/nestegg.c
- vpxdec.SRCS += webmdec.c webmdec.h
+ vpxdec.SRCS += $(LIBWEBM_PARSER_SRCS)
+ vpxdec.SRCS += webmdec.cc webmdec.h
endif
vpxdec.GUID = BA5FE66F-38DD-E034-F542-B1578C5FB950
vpxdec.DESCRIPTION = Full featured decoder
diff --git a/test/decode_with_drops.sh b/test/decode_with_drops.sh
new file mode 100755
index 000000000..d0321bfb2
--- /dev/null
+++ b/test/decode_with_drops.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+##
+## 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.
+##
+## This file tests the libvpx decode_with_drops example. To add new tests to
+## this file, do the following:
+## 1. Write a shell function (this is your test).
+## 2. Add the function to decode_with_drops_tests (on a new line).
+##
+. $(dirname $0)/tools_common.sh
+
+# Environment check: Make sure input is available:
+# $VP8_IVF_FILE and $VP9_IVF_FILE are required.
+decode_with_drops_verify_environment() {
+ if [ ! -e "${VP8_IVF_FILE}" ] || [ ! -e "${VP9_IVF_FILE}" ]; then
+ echo "Libvpx test data must exist in LIBVPX_TEST_DATA_PATH."
+ return 1
+ fi
+}
+
+# Runs decode_with_drops on $1, $2 is interpreted as codec name and used solely
+# to name the output file. $3 is the drop mode, and is passed directly to
+# decode_with_drops.
+decode_with_drops() {
+ local decoder="${LIBVPX_BIN_PATH}/decode_with_drops${VPX_TEST_EXE_SUFFIX}"
+ local input_file="$1"
+ local codec="$2"
+ local output_file="${VPX_TEST_OUTPUT_DIR}/decode_with_drops_${codec}"
+ local drop_mode="$3"
+
+ [ -x "${decoder}" ] || return 1
+
+ "${decoder}" "${input_file}" "${output_file}" "${drop_mode}" > /dev/null 2>&1
+
+ [ -e "${output_file}" ] || return 1
+}
+
+# Decodes $VP8_IVF_FILE while dropping frames, twice: once in sequence mode,
+# and once in pattern mode.
+# Note: This test assumes that $VP8_IVF_FILE has exactly 29 frames, and could
+# break if the file is modified.
+decode_with_drops_vp8() {
+ if [ "$(vp8_decode_available)" = "yes" ]; then
+ # Test sequence mode: Drop frames 2-28.
+ decode_with_drops "${VP8_IVF_FILE}" "vp8" "2-28"
+
+ # Test pattern mode: Drop 3 of every 4 frames.
+ decode_with_drops "${VP8_IVF_FILE}" "vp8" "3/4"
+ fi
+}
+
+# Decodes $VP9_IVF_FILE while dropping frames, twice: once in sequence mode,
+# and once in pattern mode.
+# Note: This test assumes that $VP9_IVF_FILE has exactly 20 frames, and could
+# break if the file is modified.
+decode_with_drops_vp9() {
+ if [ "$(vp9_decode_available)" = "yes" ]; then
+ # Test sequence mode: Drop frames 2-28.
+ decode_with_drops "${VP9_IVF_FILE}" "vp9" "2-19"
+
+ # Test pattern mode: Drop 3 of every 4 frames.
+ decode_with_drops "${VP9_IVF_FILE}" "vp9" "3/4"
+ fi
+}
+
+decode_with_drops_tests="decode_with_drops_vp8
+ decode_with_drops_vp9"
+
+run_tests decode_with_drops_verify_environment "${decode_with_drops_tests}"
diff --git a/test/examples.sh b/test/examples.sh
new file mode 100755
index 000000000..ac2a18c03
--- /dev/null
+++ b/test/examples.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+##
+## 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.
+##
+## This file runs all of the tests for the libvpx examples.
+##
+. $(dirname $0)/tools_common.sh
+
+example_tests=$(ls $(dirname $0)/*.sh)
+
+# List of script names to exclude.
+exclude_list="examples vpxdec vpxenc tools_common"
+
+# Filter out the scripts in $exclude_list.
+for word in ${exclude_list}; do
+ example_tests=$(filter_strings "${example_tests}" "${word}" exclude)
+done
+
+for test in ${example_tests}; do
+ # Source each test script so that exporting variables can be avoided.
+ . "${test}"
+done
diff --git a/test/simple_encoder.sh b/test/simple_encoder.sh
new file mode 100755
index 000000000..13f5e298b
--- /dev/null
+++ b/test/simple_encoder.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+##
+## 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.
+##
+## This file tests the libvpx simple_encoder example. To add new tests to this
+## file, do the following:
+## 1. Write a shell function (this is your test).
+## 2. Add the function to simple_encoder_tests (on a new line).
+##
+. $(dirname $0)/tools_common.sh
+
+# Environment check: $YUV_RAW_INPUT is required.
+simple_encoder_verify_environment() {
+ if [ ! -e "${YUV_RAW_INPUT}" ]; then
+ echo "Libvpx test data must exist in LIBVPX_TEST_DATA_PATH."
+ return 1
+ fi
+}
+
+# Runs simple_encoder using the codec specified by $1.
+simple_encoder() {
+ local encoder="${LIBVPX_BIN_PATH}/simple_encoder${VPX_TEST_EXE_SUFFIX}"
+ local codec="$1"
+ local output_file="${VPX_TEST_OUTPUT_DIR}/simple_encoder_${codec}.ivf"
+
+ [ -x "${encoder}" ] || return 1
+
+ "${encoder}" "${codec}" "${YUV_RAW_INPUT_WIDTH}" "${YUV_RAW_INPUT_HEIGHT}" \
+ "${YUV_RAW_INPUT}" "${output_file}" 9999 > /dev/null 2>&1
+
+ [ -e "${output_file}" ] || return 1
+}
+
+simple_encoder_vp8() {
+ if [ "$(vp8_encode_available)" = "yes" ]; then
+ simple_encoder vp8 || return 1
+ fi
+}
+
+# TODO(tomfinegan): Add a frame limit param to simple_encoder and enable this
+# test. VP9 is just too slow right now: This test takes 4m30s+ on a fast
+# machine.
+DISABLED_simple_encoder_vp9() {
+ if [ "$(vp9_encode_available)" = "yes" ]; then
+ simple_encoder vp9 || return 1
+ fi
+}
+
+simple_encoder_tests="simple_encoder_vp8
+ DISABLED_simple_encoder_vp9"
+
+run_tests simple_encoder_verify_environment "${simple_encoder_tests}"
diff --git a/test/test.mk b/test/test.mk
index da56b00ec..0dcb6c86e 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -43,15 +43,13 @@ LIBVPX_TEST_SRCS-yes += encode_test_driver.h
## WebM Parsing
ifeq ($(CONFIG_WEBM_IO), yes)
-NESTEGG_SRCS += ../third_party/nestegg/halloc/halloc.h
-NESTEGG_SRCS += ../third_party/nestegg/halloc/src/align.h
-NESTEGG_SRCS += ../third_party/nestegg/halloc/src/halloc.c
-NESTEGG_SRCS += ../third_party/nestegg/halloc/src/hlist.h
-NESTEGG_SRCS += ../third_party/nestegg/include/nestegg/nestegg.h
-NESTEGG_SRCS += ../third_party/nestegg/src/nestegg.c
-LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += $(NESTEGG_SRCS)
+LIBWEBM_PARSER_SRCS += ../third_party/libwebm/mkvparser.cpp
+LIBWEBM_PARSER_SRCS += ../third_party/libwebm/mkvreader.cpp
+LIBWEBM_PARSER_SRCS += ../third_party/libwebm/mkvparser.hpp
+LIBWEBM_PARSER_SRCS += ../third_party/libwebm/mkvreader.hpp
+LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += $(LIBWEBM_PARSER_SRCS)
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ../tools_common.h
-LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ../webmdec.c
+LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ../webmdec.cc
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += ../webmdec.h
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += webm_video_source.h
endif
diff --git a/test/tools_common.sh b/test/tools_common.sh
index 45b777178..0f5cbb9e0 100755
--- a/test/tools_common.sh
+++ b/test/tools_common.sh
@@ -9,6 +9,11 @@
## be found in the AUTHORS file in the root of the source tree.
##
## This file contains shell code shared by test scripts for libvpx tools.
+
+# Use $VPX_TEST_TOOLS_COMMON_SH as a pseudo include guard.
+if [ -z "${VPX_TEST_TOOLS_COMMON_SH}" ]; then
+VPX_TEST_TOOLS_COMMON_SH=included
+
set -e
# Sets $VPX_TOOL_TEST to the name specified by positional parameter one.
@@ -441,3 +446,5 @@ $(basename "${0%.*}") test configuration:
VPX_TEST_RUN_DISABLED_TESTS=${VPX_TEST_RUN_DISABLED_TESTS}
EOF
fi
+
+fi # End $VPX_TEST_TOOLS_COMMON_SH pseudo include guard.
diff --git a/webmdec.c b/webmdec.c
deleted file mode 100644
index 93a8d9fb5..000000000
--- a/webmdec.c
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * Copyright (c) 2013 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 "./webmdec.h"
-
-#include <stdarg.h>
-
-#include "third_party/nestegg/include/nestegg/nestegg.h"
-
-static int nestegg_read_cb(void *buffer, size_t length, void *userdata) {
- FILE *f = userdata;
-
- if (fread(buffer, 1, length, f) < length) {
- if (ferror(f))
- return -1;
- if (feof(f))
- return 0;
- }
- return 1;
-}
-
-static int nestegg_seek_cb(int64_t offset, int whence, void *userdata) {
- switch (whence) {
- case NESTEGG_SEEK_SET:
- whence = SEEK_SET;
- break;
- case NESTEGG_SEEK_CUR:
- whence = SEEK_CUR;
- break;
- case NESTEGG_SEEK_END:
- whence = SEEK_END;
- break;
- };
- return fseek(userdata, (int32_t)offset, whence) ? -1 : 0;
-}
-
-static int64_t nestegg_tell_cb(void *userdata) {
- return ftell(userdata);
-}
-
-static void nestegg_log_cb(nestegg *context,
- unsigned int severity,
- char const *format, ...) {
- va_list ap;
- va_start(ap, format);
- vfprintf(stderr, format, ap);
- fprintf(stderr, "\n");
- va_end(ap);
-}
-
-int file_is_webm(struct WebmInputContext *webm_ctx,
- struct VpxInputContext *vpx_ctx) {
- uint32_t i, n;
- int track_type = -1;
- int codec_id;
-
- nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb, 0};
- nestegg_video_params params;
-
- io.userdata = vpx_ctx->file;
- if (nestegg_init(&webm_ctx->nestegg_ctx, io, NULL, -1))
- goto fail;
-
- if (nestegg_track_count(webm_ctx->nestegg_ctx, &n))
- goto fail;
-
- for (i = 0; i < n; i++) {
- track_type = nestegg_track_type(webm_ctx->nestegg_ctx, i);
-
- if (track_type == NESTEGG_TRACK_VIDEO)
- break;
- else if (track_type < 0)
- goto fail;
- }
-
- codec_id = nestegg_track_codec_id(webm_ctx->nestegg_ctx, i);
- if (codec_id == NESTEGG_CODEC_VP8) {
- vpx_ctx->fourcc = VP8_FOURCC;
- } else if (codec_id == NESTEGG_CODEC_VP9) {
- vpx_ctx->fourcc = VP9_FOURCC;
- } else {
- fprintf(stderr, "Not VPx video, quitting.\n");
- goto fail;
- }
-
- webm_ctx->video_track = i;
-
- if (nestegg_track_video_params(webm_ctx->nestegg_ctx, i, &params))
- goto fail;
-
- vpx_ctx->framerate.denominator = 0;
- vpx_ctx->framerate.numerator = 0;
- vpx_ctx->width = params.width;
- vpx_ctx->height = params.height;
-
- return 1;
-
- fail:
- webm_ctx->nestegg_ctx = NULL;
- rewind(vpx_ctx->file);
-
- return 0;
-}
-
-int webm_read_frame(struct WebmInputContext *webm_ctx,
- uint8_t **buffer,
- size_t *bytes_in_buffer,
- size_t *buffer_size) {
- if (webm_ctx->chunk >= webm_ctx->chunks) {
- uint32_t track;
- int status;
-
- do {
- /* End of this packet, get another. */
- if (webm_ctx->pkt) {
- nestegg_free_packet(webm_ctx->pkt);
- webm_ctx->pkt = NULL;
- }
-
- status = nestegg_read_packet(webm_ctx->nestegg_ctx, &webm_ctx->pkt);
- if (status <= 0)
- return status ? status : 1;
-
- if (nestegg_packet_track(webm_ctx->pkt, &track))
- return -1;
- } while (track != webm_ctx->video_track);
-
- if (nestegg_packet_count(webm_ctx->pkt, &webm_ctx->chunks))
- return -1;
-
- webm_ctx->chunk = 0;
- }
-
- if (nestegg_packet_data(webm_ctx->pkt, webm_ctx->chunk,
- buffer, bytes_in_buffer)) {
- return -1;
- }
-
- webm_ctx->chunk++;
- return 0;
-}
-
-int webm_guess_framerate(struct WebmInputContext *webm_ctx,
- struct VpxInputContext *vpx_ctx) {
- uint32_t i;
- uint64_t tstamp = 0;
-
- /* Check to see if we can seek before we parse any data. */
- if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0)) {
- fprintf(stderr, "Failed to guess framerate (no Cues), set to 30fps.\n");
- vpx_ctx->framerate.numerator = 30;
- vpx_ctx->framerate.denominator = 1;
- return 0;
- }
-
- /* Guess the framerate. Read up to 1 second, or 50 video packets,
- * whichever comes first.
- */
- for (i = 0; tstamp < 1000000000 && i < 50;) {
- nestegg_packet *pkt;
- uint32_t track;
-
- if (nestegg_read_packet(webm_ctx->nestegg_ctx, &pkt) <= 0)
- break;
-
- nestegg_packet_track(pkt, &track);
- if (track == webm_ctx->video_track) {
- nestegg_packet_tstamp(pkt, &tstamp);
- ++i;
- }
-
- nestegg_free_packet(pkt);
- }
-
- if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0))
- goto fail;
-
- vpx_ctx->framerate.numerator = (i - 1) * 1000000;
- vpx_ctx->framerate.denominator = (int)(tstamp / 1000);
- return 0;
-
- fail:
- nestegg_destroy(webm_ctx->nestegg_ctx);
- webm_ctx->nestegg_ctx = NULL;
- rewind(vpx_ctx->file);
- return 1;
-}
-
-void webm_free(struct WebmInputContext *webm_ctx) {
- if (webm_ctx && webm_ctx->nestegg_ctx) {
- if (webm_ctx->pkt)
- nestegg_free_packet(webm_ctx->pkt);
- nestegg_destroy(webm_ctx->nestegg_ctx);
- }
-}
diff --git a/webmdec.cc b/webmdec.cc
new file mode 100644
index 000000000..eb89befd8
--- /dev/null
+++ b/webmdec.cc
@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2013 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 "./webmdec.h"
+
+#include <cstring>
+#include <cstdio>
+
+#include "third_party/libwebm/mkvparser.hpp"
+#include "third_party/libwebm/mkvreader.hpp"
+
+namespace {
+
+void reset(struct WebmInputContext *const webm_ctx) {
+ if (webm_ctx->reader != NULL) {
+ mkvparser::MkvReader *const reader =
+ reinterpret_cast<mkvparser::MkvReader*>(webm_ctx->reader);
+ delete reader;
+ }
+ if (webm_ctx->segment != NULL) {
+ mkvparser::Segment *const segment =
+ reinterpret_cast<mkvparser::Segment*>(webm_ctx->segment);
+ delete segment;
+ }
+ if (webm_ctx->buffer != NULL) {
+ delete[] webm_ctx->buffer;
+ }
+ webm_ctx->reader = NULL;
+ webm_ctx->segment = NULL;
+ webm_ctx->buffer = NULL;
+ webm_ctx->cluster = NULL;
+ webm_ctx->block_entry = NULL;
+ webm_ctx->block = NULL;
+ webm_ctx->block_frame_index = 0;
+ webm_ctx->video_track_index = 0;
+ webm_ctx->timestamp_ns = 0;
+}
+
+void get_first_cluster(struct WebmInputContext *const webm_ctx) {
+ mkvparser::Segment *const segment =
+ reinterpret_cast<mkvparser::Segment*>(webm_ctx->segment);
+ const mkvparser::Cluster *const cluster = segment->GetFirst();
+ webm_ctx->cluster = cluster;
+}
+
+void rewind_and_reset(struct WebmInputContext *const webm_ctx,
+ struct VpxInputContext *const vpx_ctx) {
+ rewind(vpx_ctx->file);
+ reset(webm_ctx);
+}
+
+} // namespace
+
+int file_is_webm(struct WebmInputContext *webm_ctx,
+ struct VpxInputContext *vpx_ctx) {
+ mkvparser::MkvReader *const reader = new mkvparser::MkvReader(vpx_ctx->file);
+ webm_ctx->reader = reader;
+
+ mkvparser::EBMLHeader header;
+ long long pos = 0;
+ if (header.Parse(reader, pos) < 0) {
+ rewind_and_reset(webm_ctx, vpx_ctx);
+ return 0;
+ }
+
+ mkvparser::Segment* segment;
+ if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
+ rewind_and_reset(webm_ctx, vpx_ctx);
+ return 0;
+ }
+ webm_ctx->segment = segment;
+ if (segment->Load() < 0) {
+ rewind_and_reset(webm_ctx, vpx_ctx);
+ return 0;
+ }
+
+ const mkvparser::Tracks *const tracks = segment->GetTracks();
+ const mkvparser::VideoTrack* video_track = NULL;
+ for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
+ const mkvparser::Track* const track = tracks->GetTrackByIndex(i);
+ if (track->GetType() == mkvparser::Track::kVideo) {
+ video_track = static_cast<const mkvparser::VideoTrack*>(track);
+ webm_ctx->video_track_index = track->GetNumber();
+ break;
+ }
+ }
+
+ if (video_track == NULL) {
+ rewind_and_reset(webm_ctx, vpx_ctx);
+ return 0;
+ }
+
+ if (!strncmp(video_track->GetCodecId(), "V_VP8", 5)) {
+ vpx_ctx->fourcc = VP8_FOURCC;
+ } else if (!strncmp(video_track->GetCodecId(), "V_VP9", 5)) {
+ vpx_ctx->fourcc = VP9_FOURCC;
+ } else {
+ rewind_and_reset(webm_ctx, vpx_ctx);
+ return 0;
+ }
+
+ vpx_ctx->framerate.denominator = 0;
+ vpx_ctx->framerate.numerator = 0;
+ vpx_ctx->width = video_track->GetWidth();
+ vpx_ctx->height = video_track->GetHeight();
+
+ get_first_cluster(webm_ctx);
+
+ return 1;
+}
+
+int webm_read_frame(struct WebmInputContext *webm_ctx,
+ uint8_t **buffer,
+ size_t *bytes_in_buffer,
+ size_t *buffer_size) {
+ mkvparser::Segment *const segment =
+ reinterpret_cast<mkvparser::Segment*>(webm_ctx->segment);
+ const mkvparser::Cluster* cluster =
+ reinterpret_cast<const mkvparser::Cluster*>(webm_ctx->cluster);
+ const mkvparser::Block *block =
+ reinterpret_cast<const mkvparser::Block*>(webm_ctx->block);
+ const mkvparser::BlockEntry *block_entry =
+ reinterpret_cast<const mkvparser::BlockEntry*>(webm_ctx->block_entry);
+ bool block_entry_eos = false;
+ do {
+ long status = 0;
+ bool get_new_block = false;
+ if (block_entry == NULL && !block_entry_eos) {
+ status = cluster->GetFirst(block_entry);
+ get_new_block = true;
+ } else if (block_entry_eos || block_entry->EOS()) {
+ cluster = segment->GetNext(cluster);
+ if (cluster == NULL || cluster->EOS()) {
+ *bytes_in_buffer = 0;
+ return 1;
+ }
+ status = cluster->GetFirst(block_entry);
+ block_entry_eos = false;
+ get_new_block = true;
+ } else if (block == NULL ||
+ webm_ctx->block_frame_index == block->GetFrameCount() ||
+ block->GetTrackNumber() != webm_ctx->video_track_index) {
+ status = cluster->GetNext(block_entry, block_entry);
+ if (block_entry == NULL || block_entry->EOS()) {
+ block_entry_eos = true;
+ continue;
+ }
+ get_new_block = true;
+ }
+ if (status) {
+ return -1;
+ }
+ if (get_new_block) {
+ block = block_entry->GetBlock();
+ webm_ctx->block_frame_index = 0;
+ }
+ } while (block->GetTrackNumber() != webm_ctx->video_track_index ||
+ block_entry_eos);
+
+ webm_ctx->cluster = cluster;
+ webm_ctx->block_entry = block_entry;
+ webm_ctx->block = block;
+
+ const mkvparser::Block::Frame& frame =
+ block->GetFrame(webm_ctx->block_frame_index);
+ ++webm_ctx->block_frame_index;
+ if (frame.len > static_cast<long>(*buffer_size)) {
+ delete[] *buffer;
+ *buffer = new uint8_t[frame.len];
+ if (*buffer == NULL) {
+ return -1;
+ }
+ *buffer_size = frame.len;
+ webm_ctx->buffer = *buffer;
+ }
+ *bytes_in_buffer = frame.len;
+ webm_ctx->timestamp_ns = block->GetTime(cluster);
+
+ mkvparser::MkvReader *const reader =
+ reinterpret_cast<mkvparser::MkvReader*>(webm_ctx->reader);
+ return frame.Read(reader, *buffer) ? -1 : 0;
+}
+
+int webm_guess_framerate(struct WebmInputContext *webm_ctx,
+ struct VpxInputContext *vpx_ctx) {
+ uint32_t i = 0;
+ uint8_t *buffer = NULL;
+ size_t bytes_in_buffer = 0;
+ size_t buffer_size = 0;
+ while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
+ if (webm_read_frame(webm_ctx, &buffer, &bytes_in_buffer, &buffer_size)) {
+ break;
+ }
+ ++i;
+ }
+ vpx_ctx->framerate.numerator = (i - 1) * 1000000;
+ vpx_ctx->framerate.denominator =
+ static_cast<int>(webm_ctx->timestamp_ns / 1000);
+ delete[] buffer;
+
+ get_first_cluster(webm_ctx);
+ webm_ctx->block = NULL;
+ webm_ctx->block_entry = NULL;
+ webm_ctx->block_frame_index = 0;
+ webm_ctx->timestamp_ns = 0;
+
+ return 0;
+}
+
+void webm_free(struct WebmInputContext *webm_ctx) {
+ reset(webm_ctx);
+}
diff --git a/webmdec.h b/webmdec.h
index 108c6ade9..29b815da1 100644
--- a/webmdec.h
+++ b/webmdec.h
@@ -16,34 +16,53 @@
extern "C" {
#endif
-struct nestegg;
-struct nestegg_packet;
struct VpxInputContext;
struct WebmInputContext {
- uint32_t chunk;
- uint32_t chunks;
- uint32_t video_track;
- struct nestegg *nestegg_ctx;
- struct nestegg_packet *pkt;
+ void *reader;
+ void *segment;
+ uint8_t *buffer;
+ const void *cluster;
+ const void *block_entry;
+ const void *block;
+ int block_frame_index;
+ int video_track_index;
+ uint64_t timestamp_ns;
};
+// Checks if the input is a WebM file. If so, initializes WebMInputContext so
+// that webm_read_frame can be called to retrieve a video frame.
+// Returns 1 on success and 0 on failure or input is not WebM file.
+// TODO(vigneshv): Refactor this function into two smaller functions specific
+// to their task.
int file_is_webm(struct WebmInputContext *webm_ctx,
struct VpxInputContext *vpx_ctx);
-/* Reads a WebM video frame. Return values:
- * 0 - Success
- * 1 - End of File
- * -1 - Error
- */
+// Reads a WebM Video Frame. Memory for the buffer is created, owned and managed
+// by this function. For the first call, |buffer| should be NULL and
+// |*bytes_in_buffer| should be 0. Once all the frames are read and used,
+// webm_free() should be called, otherwise there will be a leak.
+// Parameters:
+// webm_ctx - WebmInputContext object
+// buffer - pointer where the frame data will be filled.
+// bytes_in_buffer - pointer to buffer size.
+// buffer_size - unused TODO(vigneshv): remove this
+// Return values:
+// 0 - Success
+// 1 - End of Stream
+// -1 - Error
+// TODO(vigneshv): Make the return values consistent across all functions in
+// this file.
int webm_read_frame(struct WebmInputContext *webm_ctx,
uint8_t **buffer,
size_t *bytes_in_buffer,
size_t *buffer_size);
+// Guesses the frame rate of the input file based on the container timestamps.
int webm_guess_framerate(struct WebmInputContext *webm_ctx,
struct VpxInputContext *vpx_ctx);
+// Resets the WebMInputContext.
void webm_free(struct WebmInputContext *webm_ctx);
#ifdef __cplusplus