56 #include "../tools_common.h" 57 #include "../video_writer.h" 59 static const char *exec_name;
61 void usage_exit(
void) {
63 "Usage: %s <codec> <width> <height> <infile> <outfile> " 78 if (res !=
VPX_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
86 stats->
buf = realloc(stats->
buf, stats->
sz + pkt_size);
87 memcpy((uint8_t *)stats->
buf + stats->
sz, pkt_buf, pkt_size);
88 stats->
sz += pkt_size;
98 VpxVideoWriter *writer) {
104 if (res !=
VPX_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
111 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.buf,
114 die_codec(ctx,
"Failed to write compressed frame.");
115 printf(keyframe ?
"K" :
".");
124 const VpxInterface *encoder,
131 die_codec(&codec,
"Failed to initialize encoder");
134 while (vpx_img_read(raw, infile)) {
138 if (max_frames > 0 && frame_count >= max_frames)
break;
146 printf(
"Pass 0 complete. Processed %d frames.\n", frame_count);
152 static void pass1(
vpx_image_t *raw, FILE *infile,
const char *outfile_name,
155 VpxVideoInfo info = { encoder->fourcc,
159 VpxVideoWriter *writer = NULL;
163 writer = vpx_video_writer_open(outfile_name, kContainerIVF, &info);
164 if (!writer) die(
"Failed to open %s for writing", outfile_name);
167 die_codec(&codec,
"Failed to initialize encoder");
170 while (vpx_img_read(raw, infile)) {
174 if (max_frames > 0 && frame_count >= max_frames)
break;
185 vpx_video_writer_close(writer);
187 printf(
"Pass 1 complete. Processed %d frames.\n", frame_count);
190 int main(
int argc,
char **argv) {
199 const VpxInterface *encoder = NULL;
201 const int bitrate = 200;
202 const char *
const codec_arg = argv[1];
203 const char *
const width_arg = argv[2];
204 const char *
const height_arg = argv[3];
205 const char *
const infile_arg = argv[4];
206 const char *
const outfile_arg = argv[5];
210 if (argc != 7) die(
"Invalid number of arguments.");
212 max_frames = (int)strtol(argv[6], NULL, 0);
214 encoder = get_vpx_encoder_by_name(codec_arg);
215 if (!encoder) die(
"Unsupported codec.");
217 w = (int)strtol(width_arg, NULL, 0);
218 h = (int)strtol(height_arg, NULL, 0);
220 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
221 die(
"Invalid frame size: %dx%d", w, h);
224 die(
"Failed to allocate image", w, h);
230 if (res) die_codec(&codec,
"Failed to get default codec config.");
238 if (!(infile = fopen(infile_arg,
"rb")))
239 die(
"Failed to open %s for reading", infile_arg);
243 stats = pass0(&raw, infile, encoder, &cfg, max_frames);
249 pass1(&raw, infile, outfile_arg, encoder, &cfg, max_frames);
vpx_fixed_buf_t twopass_stats
Definition: vpx_encoder.h:186
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:357
int den
Definition: vpx_encoder.h:231
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.
Definition: vpx_encoder.h:154
Encoder configuration structure.
Definition: vpx_encoder.h:279
Encoder output packet.
Definition: vpx_encoder.h:170
void * buf
Definition: vpx_encoder.h:103
Generic fixed size buffer structure.
Definition: vpx_encoder.h:102
Definition: vpx_encoder.h:237
Definition: vpx_encoder.h:238
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:55
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:318
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:327
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:171
long vpx_enc_frame_flags_t
Encoded Frame Flags.
Definition: vpx_encoder.h:270
Operation completed without error.
Definition: vpx_codec.h:92
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:477
int num
Definition: vpx_encoder.h:230
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:372
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition: vpx_encoder.h:851
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:760
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:90
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_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition: vpx_encoder.h:464
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:112
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.
size_t sz
Definition: vpx_encoder.h:104
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:122
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:187
Definition: vpx_encoder.h:153
Codec context structure.
Definition: vpx_codec.h:197