Commit c761bd78 authored by Dmitry Kovalev's avatar Dmitry Kovalev Committed by Gerrit Code Review

Merge "Cleaning up {simple, twopass}_encoder examples."

parents 52c1550e 592936b4
......@@ -107,6 +107,10 @@ simple_encoder.SRCS += video_writer.h video_writer.c
simple_encoder.GUID = 4607D299-8A71-4D2C-9B1D-071899B6FBFD
simple_encoder.DESCRIPTION = Simplified encoder loop
EXAMPLES-$(CONFIG_VP8_ENCODER) += twopass_encoder.c
twopass_encoder.SRCS += ivfenc.h ivfenc.c
twopass_encoder.SRCS += tools_common.h tools_common.c
twopass_encoder.SRCS += video_common.h
twopass_encoder.SRCS += video_writer.h video_writer.c
twopass_encoder.GUID = 73494FA6-4AF9-4763-8FBB-265C92402FD8
twopass_encoder.DESCRIPTION = Two-pass encoder loop
EXAMPLES-$(CONFIG_VP8_ENCODER) += force_keyframe.c
......
......@@ -86,7 +86,6 @@
#include <string.h>
#define VPX_CODEC_DISABLE_COMPAT 1
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"
......@@ -102,33 +101,17 @@ void usage_exit() {
exit(EXIT_FAILURE);
}
static int read_frame(FILE *f, vpx_image_t *img) {
int res = 1;
size_t to_read = img->w * img->h * 3 / 2;
size_t nbytes = fread(img->planes[0], 1, to_read, f);
if (nbytes != to_read) {
res = 0;
if (nbytes > 0)
printf("Warning: Read partial frame. Check your width & height!\n");
}
return res;
}
static int is_valid_dimension(int value) {
return value >= 16 && (value % 2 == 0);
}
int main(int argc, char **argv) {
FILE *infile;
FILE *infile = NULL;
vpx_codec_ctx_t codec;
vpx_codec_enc_cfg_t cfg;
int frame_count = 0;
vpx_image_t raw;
vpx_codec_err_t res;
VpxVideoInfo info;
VpxVideoWriter *writer;
const int fps = 30; // TODO(dkovalev) add command line argument
const int bitrate = 100; // kbit/s TODO(dkovalev) add command line argument
VpxVideoInfo info = {0};
VpxVideoWriter *writer = NULL;
const int fps = 30; // TODO(dkovalev) add command line argument
const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
exec_name = argv[0];
......@@ -141,21 +124,23 @@ int main(int argc, char **argv) {
info.time_base.numerator = 1;
info.time_base.denominator = fps;
if (!is_valid_dimension(info.frame_width) ||
!is_valid_dimension(info.frame_height))
die("Invalid resolution: %dx%d", info.frame_width, info.frame_height);
if (info.frame_width <= 0 ||
info.frame_height <= 0 ||
(info.frame_width % 2) != 0 ||
(info.frame_height % 2) != 0) {
die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
}
if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
info.frame_height, 1))
die("Failed to allocate image");
info.frame_height, 1)) {
die("Failed to allocate image.");
}
printf("Using %s\n", vpx_codec_iface_name(interface));
res = vpx_codec_enc_config_default(interface, &cfg, 0);
if (res) {
printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
return EXIT_FAILURE;
}
if (res)
die_codec(&codec, "Failed to get default codec config.");
cfg.g_w = info.frame_width;
cfg.g_h = info.frame_height;
......@@ -167,25 +152,23 @@ int main(int argc, char **argv) {
if (!writer)
die("Failed to open %s for writing.", argv[4]);
// Open input file for this encoding pass
if (!(infile = fopen(argv[3], "rb")))
die("Failed to open %s for reading.", argv[3]);
// Initialize codec
if (vpx_codec_enc_init(&codec, interface, &cfg, 0))
die_codec(&codec, "Failed to initialize encoder");
while (read_frame(infile, &raw)) {
while (vpx_img_read(&raw, infile)) {
vpx_codec_iter_t iter = NULL;
const vpx_codec_cx_pkt_t *pkt = NULL;
++frame_count;
res = vpx_codec_encode(&codec, &raw, frame_count, 1, 0,
VPX_DL_GOOD_QUALITY);
if (res != VPX_CODEC_OK)
die_codec(&codec, "Failed to encode frame");
++frame_count;
while ((pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) {
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
......@@ -195,14 +178,14 @@ int main(int argc, char **argv) {
pkt->data.frame.pts))
die_codec(&codec, "Failed to write compressed frame.");
printf(keyframe ? "K" : ".");
fflush(stdout);
}
}
}
printf("\n");
fclose(infile);
printf("Processed %d frames.\n", frame_count);
vpx_img_free(&raw);
if (vpx_codec_destroy(&codec))
die_codec(&codec, "Failed to destroy codec.");
......
This diff is collapsed.
......@@ -168,9 +168,31 @@ void vpx_img_write(const vpx_image_t *img, FILE *file) {
const int stride = img->stride[plane];
const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
for (y = 0; y < h; ++y) {
fwrite(buf, 1, w, file);
buf += stride;
}
}
}
int vpx_img_read(vpx_image_t *img, FILE *file) {
int plane;
for (plane = 0; plane < 3; ++plane) {
unsigned char *buf = img->planes[plane];
const int stride = img->stride[plane];
const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
int y;
for (y = 0; y < h; ++y) {
if (fread(buf, 1, w, file) != w)
return 0;
buf += stride;
}
}
return 1;
}
......@@ -129,6 +129,10 @@ vpx_codec_iface_t *get_codec_interface(unsigned int fourcc);
// of vpx_image_t support
void vpx_img_write(const vpx_image_t *img, FILE *file);
// TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
// of vpx_image_t support
int vpx_img_read(vpx_image_t *img, FILE *file);
#ifdef __cplusplus
} /* extern "C" */
#endif
......
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