53 #include "../tools_common.h"
54 #include "../video_writer.h"
56 static const char *exec_name;
58 void usage_exit(
void) {
59 fprintf(stderr,
"Usage: %s <codec> <width> <height> <infile> <outfile>\n",
68 memset(&roi, 0,
sizeof(roi));
70 roi.
rows = (cfg->
g_h + 15) / 16;
71 roi.
cols = (cfg->
g_w + 15) / 16;
89 for (i = 0; i < roi.
rows * roi.
cols; ++i)
93 die_codec(codec,
"Failed to set ROI map");
103 map.
rows = (cfg->
g_h + 15) / 16;
104 map.
cols = (cfg->
g_w + 15) / 16;
107 for (i = 0; i < map.
rows * map.
cols; ++i)
111 die_codec(codec,
"Failed to set active map");
120 map.
rows = (cfg->
g_h + 15) / 16;
121 map.
cols = (cfg->
g_w + 15) / 16;
125 die_codec(codec,
"Failed to set active map");
131 VpxVideoWriter *writer) {
138 die_codec(codec,
"Failed to encode frame");
145 if (!vpx_video_writer_write_frame(writer,
149 die_codec(codec,
"Failed to write compressed frame");
152 printf(keyframe ?
"K" :
".");
160 int main(
int argc,
char **argv) {
168 VpxVideoWriter *writer = NULL;
169 const VpxInterface *encoder = NULL;
171 const double bits_per_pixel_per_frame = 0.067;
175 die(
"Invalid number of arguments");
177 memset(&info, 0,
sizeof(info));
179 encoder = get_vpx_encoder_by_name(argv[1]);
180 if (encoder == NULL) {
181 die(
"Unsupported codec.");
183 assert(encoder != NULL);
184 info.codec_fourcc = encoder->fourcc;
185 info.frame_width = strtol(argv[2], NULL, 0);
186 info.frame_height = strtol(argv[3], NULL, 0);
187 info.time_base.numerator = 1;
188 info.time_base.denominator = fps;
190 if (info.frame_width <= 0 ||
191 info.frame_height <= 0 ||
192 (info.frame_width % 2) != 0 ||
193 (info.frame_height % 2) != 0) {
194 die(
"Invalid frame size: %dx%d", info.frame_width, info.frame_height);
198 info.frame_height, 1)) {
199 die(
"Failed to allocate image.");
206 die_codec(&codec,
"Failed to get default codec config.");
208 cfg.
g_w = info.frame_width;
209 cfg.
g_h = info.frame_height;
213 cfg.
g_h * fps / 1000);
216 writer = vpx_video_writer_open(argv[5], kContainerIVF, &info);
218 die(
"Failed to open %s for writing.", argv[5]);
220 if (!(infile = fopen(argv[4],
"rb")))
221 die(
"Failed to open %s for reading.", argv[4]);
224 die_codec(&codec,
"Failed to initialize encoder");
227 while (vpx_img_read(&raw, infile)) {
230 if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) {
231 set_roi_map(&cfg, &codec);
232 }
else if (frame_count == 33) {
233 set_active_map(&cfg, &codec);
234 }
else if (frame_count == 44) {
235 unset_active_map(&cfg, &codec);
238 encode_frame(&codec, &raw, frame_count, writer);
242 while (encode_frame(&codec, NULL, -1, writer)) {}
246 printf(
"Processed %d frames.\n", frame_count);
250 die_codec(&codec,
"Failed to destroy codec.");
252 vpx_video_writer_close(writer);
unsigned char * roi_map
Definition: vp8cx.h:611
Image Descriptor.
Definition: vpx_image.h:88
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
unsigned int cols
Definition: vp8cx.h:633
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:397
unsigned int cols
Definition: vp8cx.h:613
int den
Definition: vpx_encoder.h:261
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
Codec control function to pass an ROI map to encoder.
Definition: vp8cx.h:139
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:429
Encoder configuration structure.
Definition: vpx_encoder.h:314
Encoder output packet.
Definition: vpx_encoder.h:195
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:357
unsigned int static_threshold[4]
Definition: vp8cx.h:620
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:367
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:196
Operation completed without error.
Definition: vpx_codec.h:91
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:525
int num
Definition: vpx_encoder.h:260
int delta_lf[4]
Definition: vp8cx.h:618
#define VPX_DL_GOOD_QUALITY
Definition: vpx_encoder.h:914
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
unsigned char * active_map
Definition: vp8cx.h:631
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:813
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
unsigned int rows
Definition: vp8cx.h:632
union vpx_codec_cx_pkt::@1 data
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
vpx active region map
Definition: vp8cx.h:630
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:407
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
unsigned int rows
Definition: vp8cx.h:612
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:130
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
vpx region of interest map
Definition: vp8cx.h:609
Definition: vpx_encoder.h:176
int delta_q[4]
Definition: vp8cx.h:617
Codec control function to pass an Active map to encoder.
Definition: vp8cx.h:145
Codec context structure.
Definition: vpx_codec.h:199