104 #include "../tools_common.h"
105 #include "../video_writer.h"
107 static const char *exec_name;
109 void usage_exit(
void) {
111 "Usage: %s <codec> <width> <height> <infile> <outfile> "
112 "<keyframe-interval> [<error-resilient>]\nSee comments in "
113 "simple_encoder.c for more information.\n",
122 VpxVideoWriter *writer) {
129 die_codec(codec,
"Failed to encode frame");
136 if (!vpx_video_writer_write_frame(writer,
140 die_codec(codec,
"Failed to write compressed frame");
142 printf(keyframe ?
"K" :
".");
150 int main(
int argc,
char **argv) {
157 VpxVideoInfo info = {0};
158 VpxVideoWriter *writer = NULL;
159 const VpxInterface *encoder = NULL;
161 const int bitrate = 200;
162 int keyframe_interval = 0;
166 const char *codec_arg = NULL;
167 const char *width_arg = NULL;
168 const char *height_arg = NULL;
169 const char *infile_arg = NULL;
170 const char *outfile_arg = NULL;
171 const char *keyframe_interval_arg = NULL;
176 die(
"Invalid number of arguments");
180 height_arg = argv[3];
181 infile_arg = argv[4];
182 outfile_arg = argv[5];
183 keyframe_interval_arg = argv[6];
185 encoder = get_vpx_encoder_by_name(codec_arg);
187 die(
"Unsupported codec.");
189 info.codec_fourcc = encoder->fourcc;
190 info.frame_width = strtol(width_arg, NULL, 0);
191 info.frame_height = strtol(height_arg, NULL, 0);
192 info.time_base.numerator = 1;
193 info.time_base.denominator = fps;
195 if (info.frame_width <= 0 ||
196 info.frame_height <= 0 ||
197 (info.frame_width % 2) != 0 ||
198 (info.frame_height % 2) != 0) {
199 die(
"Invalid frame size: %dx%d", info.frame_width, info.frame_height);
203 info.frame_height, 1)) {
204 die(
"Failed to allocate image.");
207 keyframe_interval = strtol(keyframe_interval_arg, NULL, 0);
208 if (keyframe_interval < 0)
209 die(
"Invalid keyframe interval value.");
215 die_codec(&codec,
"Failed to get default codec config.");
217 cfg.
g_w = info.frame_width;
218 cfg.
g_h = info.frame_height;
224 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
226 die(
"Failed to open %s for writing.", outfile_arg);
228 if (!(infile = fopen(infile_arg,
"rb")))
229 die(
"Failed to open %s for reading.", infile_arg);
232 die_codec(&codec,
"Failed to initialize encoder");
235 while (vpx_img_read(&raw, infile)) {
237 if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
239 encode_frame(&codec, &raw, frame_count++, flags, writer);
243 while (encode_frame(&codec, NULL, -1, 0, writer)) {};
247 printf(
"Processed %d frames.\n", frame_count);
251 die_codec(&codec,
"Failed to destroy codec.");
253 vpx_video_writer_close(writer);
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.
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:397
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.
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 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
#define VPX_DL_GOOD_QUALITY
Definition: vpx_encoder.h:914
#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.
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_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:130
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:305
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:176
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:406
Codec context structure.
Definition: vpx_codec.h:199