Commit 00a35aab authored by Tom Finegan's avatar Tom Finegan

vpx[dec|enc]: Extract IVF support from the apps.

- Move IVF reading support into ivfdec.c and ivfdec.h
- Move IVF writing support into ivfenc.c and ivfenc.h
- Removed IVF writing code from the SVC example in favor of ivfenc.

Change-Id: I70adf6240d0320fdd232d8546ed573f0f68dd793
parent 58f75437
......@@ -23,6 +23,7 @@ vpxdec.SRCS += md5_utils.c md5_utils.h
vpxdec.SRCS += vpx_ports/vpx_timer.h
vpxdec.SRCS += vpx/vpx_integer.h
vpxdec.SRCS += args.c args.h
vpxdec.SRCS += ivfdec.c ivfdec.h
vpxdec.SRCS += tools_common.c tools_common.h
vpxdec.SRCS += nestegg/halloc/halloc.h
vpxdec.SRCS += nestegg/halloc/src/align.h
......@@ -36,6 +37,8 @@ vpxdec.GUID = BA5FE66F-38DD-E034-F542-B1578C5FB950
vpxdec.DESCRIPTION = Full featured decoder
UTILS-$(CONFIG_ENCODERS) += vpxenc.c
vpxenc.SRCS += args.c args.h y4minput.c y4minput.h
vpxenc.SRCS += ivfdec.c ivfdec.h
vpxenc.SRCS += ivfenc.c ivfenc.h
vpxenc.SRCS += tools_common.c tools_common.h
vpxenc.SRCS += webmenc.c webmenc.h
vpxenc.SRCS += vpx_ports/mem_ops.h
......@@ -53,18 +56,11 @@ vp8_scalable_patterns.GUID = 0D6A210B-F482-4D6F-8570-4A9C01ACC88C
vp8_scalable_patterns.DESCRIPTION = Temporal Scalability Encoder
UTILS-$(CONFIG_VP9_ENCODER) += vp9_spatial_scalable_encoder.c
vp9_spatial_scalable_encoder.SRCS += args.c args.h
vp9_spatial_scalable_encoder.SRCS += ivfenc.c ivfenc.h
vp9_spatial_scalable_encoder.SRCS += tools_common.c tools_common.h
vp9_spatial_scalable_encoder.GUID = 4A38598D-627D-4505-9C7B-D4020C84100D
vp9_spatial_scalable_encoder.DESCRIPTION = Spatial Scalable Encoder
# Clean up old ivfenc, ivfdec binaries.
ifeq ($(CONFIG_MSVS),yes)
CLEAN-OBJS += $(foreach p,$(VS_PLATFORMS),$(p)/Release/ivfenc.exe)
CLEAN-OBJS += $(foreach p,$(VS_PLATFORMS),$(p)/Release/ivfdec.exe)
else
CLEAN-OBJS += ivfenc{.c.o,.c.d,.dox,.exe,}
CLEAN-OBJS += ivfdec{.c.o,.c.d,.dox,.exe,}
endif
# XMA example disabled for now, not used in VP8
#UTILS-$(CONFIG_DECODERS) += example_xma.c
#example_xma.GUID = A955FC4A-73F1-44F7-135E-30D84D32F022
......
/*
* 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 "./ivfdec.h"
#include <stdio.h>
#include <stdlib.h>
int file_is_ivf(struct VpxInputContext *input_ctx) {
char raw_hdr[32];
int is_ivf = 0;
// TODO(tomfinegan): This can eventually go away, but for now it's required
// because the means by which file types are detected differ in vpxdec and
// vpxenc.
rewind(input_ctx->file);
if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K' &&
raw_hdr[2] == 'I' && raw_hdr[3] == 'F') {
is_ivf = 1;
if (mem_get_le16(raw_hdr + 4) != 0) {
fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
" decode properly.");
}
input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
input_ctx->width = mem_get_le16(raw_hdr + 12);
input_ctx->height = mem_get_le16(raw_hdr + 14);
input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
/* Some versions of vpxenc used 1/(2*fps) for the timebase, so
* we can guess the framerate using only the timebase in this
* case. Other files would require reading ahead to guess the
* timebase, like we do for webm.
*/
if (input_ctx->framerate.numerator < 1000) {
/* Correct for the factor of 2 applied to the timebase in the
* encoder.
*/
if (input_ctx->framerate.numerator & 1)
input_ctx->framerate.denominator <<= 1;
else
input_ctx->framerate.numerator >>= 1;
} else {
/* Don't know FPS for sure, and don't have readahead code
* (yet?), so just default to 30fps.
*/
input_ctx->framerate.numerator = 30;
input_ctx->framerate.denominator = 1;
}
}
}
if (!is_ivf)
rewind(input_ctx->file);
else
input_ctx->detect.position = 4;
return is_ivf;
}
int ivf_read_frame(struct VpxInputContext *input_ctx,
uint8_t **buffer,
size_t *bytes_read,
size_t *buffer_size) {
char raw_header[IVF_FRAME_HDR_SZ] = {0};
size_t frame_size = 0;
FILE *infile = input_ctx->file;
if (input_ctx->file_type != FILE_TYPE_IVF)
return 0;
if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
if (!feof(infile))
warn("Failed to read frame size\n");
} else {
frame_size = mem_get_le32(raw_header);
if (frame_size > 256 * 1024 * 1024) {
warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
frame_size = 0;
}
if (frame_size > *buffer_size) {
uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
if (new_buffer) {
*buffer = new_buffer;
*buffer_size = 2 * frame_size;
} else {
warn("Failed to allocate compressed data buffer\n");
frame_size = 0;
}
}
}
if (!feof(infile)) {
if (fread(*buffer, 1, frame_size, infile) != frame_size) {
warn("Failed to read full frame\n");
return 1;
}
*bytes_read = frame_size;
return 0;
}
return 1;
}
/*
* 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.
*/
#ifndef IVFDEC_H_
#define IVFDEC_H_
#include "./tools_common.h"
#ifdef __cplusplus
extern "C" {
#endif
int file_is_ivf(struct VpxInputContext *input);
int ivf_read_frame(struct VpxInputContext *input,
uint8_t **buffer,
size_t *bytes_read,
size_t *buffer_size);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* IVFDEC_H_ */
/*
* 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 "./ivfenc.h"
#include "./tools_common.h"
#include "vpx/vpx_encoder.h"
#include "vpx_ports/mem_ops.h"
void ivf_write_file_header(FILE *outfile,
const struct vpx_codec_enc_cfg *cfg,
unsigned int fourcc,
int frame_cnt) {
char header[32];
if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
return;
header[0] = 'D';
header[1] = 'K';
header[2] = 'I';
header[3] = 'F';
mem_put_le16(header + 4, 0); /* version */
mem_put_le16(header + 6, 32); /* headersize */
mem_put_le32(header + 8, fourcc); /* four CC */
mem_put_le16(header + 12, cfg->g_w); /* width */
mem_put_le16(header + 14, cfg->g_h); /* height */
mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
mem_put_le32(header + 24, frame_cnt); /* length */
mem_put_le32(header + 28, 0); /* unused */
(void) fwrite(header, 1, 32, outfile);
}
void ivf_write_frame_header(FILE *outfile, const struct vpx_codec_cx_pkt *pkt) {
char header[12];
vpx_codec_pts_t pts;
if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
return;
pts = pkt->data.frame.pts;
mem_put_le32(header, (int)pkt->data.frame.sz);
mem_put_le32(header + 4, pts & 0xFFFFFFFF);
mem_put_le32(header + 8, pts >> 32);
(void) fwrite(header, 1, 12, outfile);
}
void ivf_write_frame_size(FILE *outfile, size_t size) {
char header[4];
mem_put_le32(header, (int)size);
(void) fwrite(header, 1, 4, outfile);
}
/*
* 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.
*/
#ifndef IVFENC_H_
#define IVFENC_H_
#include "./tools_common.h"
struct vpx_codec_enc_cfg;
struct vpx_codec_cx_pkt;
#ifdef __cplusplus
extern "C" {
#endif
void ivf_write_file_header(FILE *outfile,
const struct vpx_codec_enc_cfg *cfg,
uint32_t fourcc,
int frame_cnt);
void ivf_write_frame_header(FILE *outfile, const struct vpx_codec_cx_pkt *pkt);
void ivf_write_frame_size(FILE *outfile, size_t size);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* IVFENC_H_ */
......@@ -7,10 +7,13 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "tools_common.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) || defined(__OS2__)
#include <io.h>
......@@ -56,3 +59,74 @@ void fatal(const char *fmt, ...) {
void warn(const char *fmt, ...) {
LOG_ERROR("Warning");
}
uint16_t mem_get_le16(const void *data) {
uint16_t val;
const uint8_t *mem = (const uint8_t*)data;
val = mem[1] << 8;
val |= mem[0];
return val;
}
uint32_t mem_get_le32(const void *data) {
uint32_t val;
const uint8_t *mem = (const uint8_t*)data;
val = mem[3] << 24;
val |= mem[2] << 16;
val |= mem[1] << 8;
val |= mem[0];
return val;
}
int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) {
FILE *f = input_ctx->file;
struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
int plane = 0;
int shortread = 0;
for (plane = 0; plane < 3; ++plane) {
uint8_t *ptr;
const int w = (plane ? (1 + yuv_frame->d_w) / 2 : yuv_frame->d_w);
const int h = (plane ? (1 + yuv_frame->d_h) / 2 : yuv_frame->d_h);
int r;
/* Determine the correct plane based on the image format. The for-loop
* always counts in Y,U,V order, but this may not match the order of
* the data on disk.
*/
switch (plane) {
case 1:
ptr = yuv_frame->planes[
yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
break;
case 2:
ptr = yuv_frame->planes[
yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
break;
default:
ptr = yuv_frame->planes[plane];
}
for (r = 0; r < h; ++r) {
size_t needed = w;
size_t buf_position = 0;
const size_t left = detect->buf_read - detect->position;
if (left > 0) {
const size_t more = (left < needed) ? left : needed;
memcpy(ptr, detect->buf + detect->position, more);
buf_position = more;
needed -= more;
detect->position += more;
}
if (needed > 0) {
shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
}
ptr += yuv_frame->stride[plane];
}
}
return shortread;
}
......@@ -13,6 +13,12 @@
#include <stdio.h>
#include "./vpx_config.h"
#include "vpx/vpx_image.h"
#include "vpx/vpx_integer.h"
#if CONFIG_ENCODERS
#include "./y4minput.h"
#endif
#if defined(_MSC_VER)
/* MSVS doesn't define off_t, and uses _f{seek,tell}i64. */
......@@ -52,11 +58,55 @@ typedef long off_t; /* NOLINT */
#define PATH_MAX 512
#endif
#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
#define IVF_FILE_HDR_SZ 32
#define RAW_FRAME_HDR_SZ sizeof(uint32_t)
#define VP8_FOURCC (0x30385056)
#define VP9_FOURCC (0x30395056)
#define VP8_FOURCC_MASK (0x00385056)
#define VP9_FOURCC_MASK (0x00395056)
enum VideoFileType {
FILE_TYPE_RAW,
FILE_TYPE_IVF,
FILE_TYPE_Y4M,
FILE_TYPE_WEBM
};
struct FileTypeDetectionBuffer {
char buf[4];
size_t buf_read;
size_t position;
};
struct VpxRational {
int numerator;
int denominator;
};
struct VpxInputContext {
const char *filename;
FILE *file;
off_t length;
struct FileTypeDetectionBuffer detect;
enum VideoFileType file_type;
unsigned int width;
unsigned int height;
int use_i420;
int only_i420;
unsigned int fourcc;
struct VpxRational framerate;
#if CONFIG_ENCODERS
y4m_input y4m;
#endif
};
#ifdef __cplusplus
extern "C" {
#endif
/* Sets a stdio stream into binary mode */
FILE *set_binary_mode(FILE *stream);
......@@ -67,4 +117,13 @@ void warn(const char *fmt, ...);
/* The tool including this file must define usage_exit() */
void usage_exit();
uint16_t mem_get_le16(const void *data);
uint32_t mem_get_le32(const void *data);
int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif // TOOLS_COMMON_H_
......@@ -19,12 +19,12 @@
#include <string.h>
#include <time.h>
#include "./args.h"
#include "./ivfenc.h"
#include "./tools_common.h"
#include "vpx/svc_context.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"
#define VP90_FOURCC 0x30395056
static const struct arg_enum_list encoding_mode_enum[] = {
{"i", INTER_LAYER_PREDICTION_I},
{"alt-ip", ALT_INTER_LAYER_PREDICTION_IP},
......@@ -77,25 +77,13 @@ static const uint32_t default_kf_dist = 100;
static const int default_use_dummy_frame = 1;
typedef struct {
char *input_filename;
char *output_filename;
uint32_t frames_to_code;
uint32_t frames_to_skip;
struct VpxInputContext input_ctx;
} AppInput;
static void mem_put_le16(char *mem, uint32_t val) {
mem[0] = val;
mem[1] = val >> 8;
}
static void mem_put_le32(char *mem, uint32_t val) {
mem[0] = val;
mem[1] = val >> 8;
mem[2] = val >> 16;
mem[3] = val >> 24;
}
static void usage(const char *exec_name) {
void usage_exit(const char *exec_name) {
fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
exec_name);
fprintf(stderr, "Options:\n");
......@@ -103,15 +91,6 @@ static void usage(const char *exec_name) {
exit(EXIT_FAILURE);
}
void die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
if (fmt[strlen(fmt) - 1] != '\n') printf("\n");
exit(EXIT_FAILURE);
}
static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
const char *detail = vpx_codec_error_detail(ctx);
......@@ -120,83 +99,12 @@ static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
exit(EXIT_FAILURE);
}
static int read_frame(FILE *f, vpx_image_t *img) {
size_t nbytes;
int res = 1;
int plane;
for (plane = 0; plane < 3; ++plane) {
uint8_t *ptr;
const int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
const int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
int r;
switch (plane) {
case 1:
ptr = img->planes[VPX_PLANE_U];
break;
case 2:
ptr = img->planes[VPX_PLANE_V];
break;
default:
ptr = img->planes[plane];
}
for (r = 0; r < h; ++r) {
const int to_read = w;
nbytes = fread(ptr, 1, to_read, f);
if (nbytes != to_read) {
res = 0;
if (nbytes > 0)
printf("Warning: Read partial frame. Check your width & height!\n");
break;
}
ptr += img->stride[plane];
}
if (!res) break;
}
return res;
}
static int create_dummy_frame(vpx_image_t *img) {
const size_t buf_size = img->w * img->h * 3 / 2;
memset(img->planes[0], 129, buf_size);
return 1;
}
static void write_ivf_file_header(FILE *outfile,
uint32_t width, uint32_t height,
int timebase_num, int timebase_den,
int frame_cnt) {
char header[32];
header[0] = 'D';
header[1] = 'K';
header[2] = 'I';
header[3] = 'F';
mem_put_le16(header + 4, 0); /* version */
mem_put_le16(header + 6, 32); /* headersize */
mem_put_le32(header + 8, VP90_FOURCC); /* fourcc */
mem_put_le16(header + 12, width); /* width */
mem_put_le16(header + 14, height); /* height */
mem_put_le32(header + 16, timebase_den); /* rate */
mem_put_le32(header + 20, timebase_num); /* scale */
mem_put_le32(header + 24, frame_cnt); /* length */
mem_put_le32(header + 28, 0); /* unused */
(void)fwrite(header, 1, 32, outfile);
}
static void write_ivf_frame_header(FILE *outfile, vpx_codec_pts_t pts,
size_t sz) {
char header[12];
mem_put_le32(header, (uint32_t)sz);
mem_put_le32(header + 4, pts & 0xFFFFFFFF);
mem_put_le32(header + 8, pts >> 32);
(void)fwrite(header, 1, 12, outfile);
}
static void parse_command_line(int argc, const char **argv_,
AppInput *app_input, SvcContext *svc_ctx,
vpx_codec_enc_cfg_t *enc_cfg) {
......@@ -272,9 +180,9 @@ static void parse_command_line(int argc, const char **argv_,
die("Error: Unrecognized option %s\n", *argi);
if (argv[0] == NULL || argv[1] == 0) {
usage(argv_[0]);
usage_exit(argv_[0]);
}
app_input->input_filename = argv[0];
app_input->input_ctx.filename = argv[0];
app_input->output_filename = argv[1];
free(argv);
......@@ -298,7 +206,7 @@ static void parse_command_line(int argc, const char **argv_,
int main(int argc, const char **argv) {
AppInput app_input = {0};
FILE *infile, *outfile;
FILE *outfile;
vpx_codec_ctx_t codec;
vpx_codec_enc_cfg_t enc_cfg;
SvcContext svc_ctx;
......@@ -308,6 +216,8 @@ int main(int argc, const char **argv) {
vpx_codec_err_t res;
int pts = 0; /* PTS starts at 0 */
int frame_duration = 1; /* 1 timebase tick per frame */
vpx_codec_cx_pkt_t packet = {0};
packet.kind = VPX_CODEC_CX_FRAME_PKT;
memset(&svc_ctx, 0, sizeof(svc_ctx));
svc_ctx.log_print = 1;
......@@ -317,8 +227,8 @@ int main(int argc, const char **argv) {
if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
if (!(infile = fopen(app_input.input_filename, "rb")))
die("Failed to open %s for reading\n", app_input.input_filename);
if (!(app_input.input_ctx.file = fopen(app_input.input_ctx.filename, "rb")))
die("Failed to open %s for reading\n", app_input.input_ctx.filename);
if (!(outfile = fopen(app_input.output_filename, "wb")))
die("Failed to open %s for writing\n", app_input.output_filename);
......@@ -328,12 +238,11 @@ int main(int argc, const char **argv) {
VPX_CODEC_OK)
die("Failed to initialize encoder\n");
write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
enc_cfg.g_timebase.num, enc_cfg.g_timebase.den, 0);
ivf_write_file_header(outfile, &enc_cfg, VP9_FOURCC, 0);
// skip initial frames
for (i = 0; i < app_input.frames_to_skip; ++i) {
read_frame(infile, &raw);
read_yuv_frame(&app_input.input_ctx, &raw);
}
// Encode frames
......@@ -341,7 +250,7 @@ int main(int argc, const char **argv) {
if (frame_cnt == 0 && svc_ctx.first_frame_full_size) {
create_dummy_frame(&raw);
} else {
if (!read_frame(infile, &raw)) break;
if (!read_yuv_frame(&app_input.input_ctx, &raw)) break;
}
res = vpx_svc_encode(&svc_ctx, &codec, &raw, pts, frame_duration,
VPX_DL_REALTIME);
......@@ -350,7 +259,9 @@ int main(int argc, const char **argv) {
die_codec(&codec, "Failed to encode frame");
}
if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
write_ivf_frame_header(outfile, pts, vpx_svc_get_frame_size(&svc_ctx));
packet.data.frame.pts = pts;
packet.data.frame.sz = vpx_svc_get_frame_size(&svc_ctx);
ivf_write_frame_header(outfile, &packet);
(void)fwrite(vpx_svc_get_buffer(&svc_ctx), 1,
vpx_svc_get_frame_size(&svc_ctx), outfile);
}
......@@ -360,14 +271,12 @@ int main(int argc, const char **argv) {
printf("Processed %d frames\n", frame_cnt - svc_ctx.first_frame_full_size);
fclose(infile);
fclose(app_input.input_ctx.file);
if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
// rewrite the output file headers with the actual frame count
if (!fseek(outfile, 0, SEEK_SET)) {
write_ivf_file_header(outfile, enc_cfg.g_w, enc_cfg.g_h,
enc_cfg.g_timebase.num, enc_cfg.g_timebase.den,
frame_cnt);
ivf_write_file_header(outfile, &enc_cfg, VP9_FOURCC, frame_cnt);
}
fclose(outfile);
vpx_img_free(&raw);
......
This diff is collapsed.
This diff is collapsed.
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