Changing webmdec to use libwebm

Changing webmdec to use libwebm for WebM file parsing.

Change-Id: I2a57a7b44dbed05eaa04409e1e75e6fc03b30fbc
parent 91013632
......@@ -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
......
......@@ -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
......
/*
* 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);
}
}
/*
* 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);
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment