summaryrefslogtreecommitdiff
path: root/code/test/vid/enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'code/test/vid/enc.c')
-rw-r--r--code/test/vid/enc.c100
1 files changed, 14 insertions, 86 deletions
diff --git a/code/test/vid/enc.c b/code/test/vid/enc.c
index a1730b4..91fe5e8 100644
--- a/code/test/vid/enc.c
+++ b/code/test/vid/enc.c
@@ -100,68 +100,7 @@
#include <string.h>
#include "enc.h"
-
-static const char *exec_name = "./enc";
-
-void usage_exit(void) {
- fprintf(stderr,
- "Usage: %s <codec> <width> <height> <infile> <outfile> "
- "<keyframe-interval> <error-resilient> <frames to encode>\n"
- "See comments in simple_encoder.c for more information.\n",
- exec_name);
- exit(EXIT_FAILURE);
-}
-
-static VpxVideoWriter *writer;
-
-static VpxVideoWriter *open_writer(const char *file_name, uint32_t fourcc, int width, int height, int fps) {
- VpxVideoInfo info = { 0, 0, 0, { 0, 0 } };
-
- info.codec_fourcc = fourcc;
- info.frame_width = width;
- info.frame_height = height;
- info.time_base.numerator = 1;
- info.time_base.denominator = fps;
-
- return vpx_video_writer_open(file_name, kContainerIVF, &info);
-}
-
-// TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
-// of vpx_image_t support
-int vpx_img_plane_width(const vpx_image_t *img, int plane) {
- if (plane > 0 && img->x_chroma_shift > 0)
- return (img->d_w + 1) >> img->x_chroma_shift;
- else
- return img->d_w;
-}
-
-int vpx_img_plane_height(const vpx_image_t *img, int plane) {
- if (plane > 0 && img->y_chroma_shift > 0)
- return (img->d_h + 1) >> img->y_chroma_shift;
- else
- return img->d_h;
-}
-
-int vpx_img_read(vpx_image_t *img, void *img_buf, int sz) {
- int plane;
-
- for (plane = 0; plane < 3; ++plane) {
- unsigned char *buf = img->planes[plane];
- const int stride = img->stride[plane];
- const int w = vpx_img_plane_width(img, plane) *
- ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
- const int h = vpx_img_plane_height(img, plane);
- int y;
-
- for (y = 0; y < h; ++y) {
- memcpy(buf, img_buf, w);
- img_buf += w;
- buf += stride;
- }
- }
-
- return 1;
-}
+#include "server.h"
int vpx_encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img, int frame_index, int flags) {
int got_pkts = 0;
@@ -176,9 +115,7 @@ int vpx_encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img, int frame_index,
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
- if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
- pkt->data.frame.sz,
- pkt->data.frame.pts)) {
+ if (send_frame(pkt->data.frame.buf, pkt->data.frame.sz, pkt->data.frame.pts) < 0) {
die_codec(codec, "Failed to write compressed frame");
}
printf(keyframe ? "K" : ".");
@@ -190,12 +127,20 @@ int vpx_encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img, int frame_index,
}
-void vpx_open(vpx_codec_ctx_t *codec, vpx_codec_iface_t *codec_interface, int width, int height, int fps, int bitrate, vpx_codec_er_flags_t err_resilient, vpx_image_t *raw) {
+void vpx_open(const char *codec_arg, int width, int height, int fps, int bitrate, vpx_codec_er_flags_t err_resilient, vpx_codec_ctx_t *codec, vpx_image_t *raw) {
vpx_codec_enc_cfg_t cfg;
vpx_codec_err_t res;
- res = vpx_codec_enc_config_default(codec_interface, &cfg, 0);
- if (res) die("Failed to get default codec config.");
+ const VpxInterface *encoder = get_vpx_encoder_by_name(codec_arg);
+ if (!encoder) die_codec(NULL, "Unsupported codec.");
+
+ printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
+
+ if (!vpx_img_alloc(raw, VPX_IMG_FMT_I420, width, height, 1))
+ die_codec(NULL, "Failed to allocate image.");
+
+ res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
+ if (res) die_codec(NULL, "Failed to get default codec config.");
cfg.g_w = width;
cfg.g_h = height;
@@ -205,15 +150,11 @@ void vpx_open(vpx_codec_ctx_t *codec, vpx_codec_iface_t *codec_interface, int wi
cfg.g_error_resilient = err_resilient;
cfg.rc_end_usage = VPX_CBR;
- if (vpx_codec_enc_init(codec, codec_interface, &cfg, 0))
+ if (vpx_codec_enc_init(codec, encoder->codec_interface(), &cfg, 0))
die_codec(codec, "Failed to initialize encoder");
if (vpx_codec_control(codec, VP8E_SET_CPUUSED, 8))
die_codec(codec, "Failed to initialize cpuused");
-
- if (!vpx_img_alloc(raw, VPX_IMG_FMT_I420, width, height, 1)) {
- die("Failed to allocate image.");
- }
}
void vpx_close(vpx_codec_ctx_t *codec, vpx_image_t *raw) {
@@ -223,17 +164,4 @@ void vpx_close(vpx_codec_ctx_t *codec, vpx_image_t *raw) {
vpx_img_free(raw);
if (vpx_codec_destroy(codec)) die_codec(codec, "Failed to destroy codec.");
-
- vpx_video_writer_close(writer);
}
-
-void vpx_init(const char *codec_arg, const char *outfile_arg, int width, int height, int fps) {
- const VpxInterface *encoder = get_vpx_encoder_by_name(codec_arg);
- if (!encoder) die("Unsupported codec.");
-
- writer = open_writer(outfile_arg, encoder->fourcc, width, height, fps);
- if (!writer) die("Failed to open %s for writing.", outfile_arg);
-
- printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
-}
-