56#include "../tools_common.h"
57#include "../video_writer.h"
59static const char *exec_name;
61void 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 if (!stats->
buf) die(
"Failed to reallocate stats buffer.");
88 memcpy((uint8_t *)stats->
buf + stats->
sz, pkt_buf, pkt_size);
89 stats->
sz += pkt_size;
99 VpxVideoWriter *writer) {
105 if (res !=
VPX_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
112 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
115 die_codec(ctx,
"Failed to write compressed frame.");
116 printf(keyframe ?
"K" :
".");
125 const VpxInterface *encoder,
132 die(
"Failed to initialize encoder");
135 while (vpx_img_read(raw, infile)) {
139 if (max_frames > 0 && frame_count >= max_frames)
break;
147 printf(
"Pass 0 complete. Processed %d frames.\n", frame_count);
153static void pass1(
vpx_image_t *raw, FILE *infile,
const char *outfile_name,
156 VpxVideoInfo info = { encoder->fourcc,
160 VpxVideoWriter *writer = NULL;
164 writer = vpx_video_writer_open(outfile_name, kContainerIVF, &info);
165 if (!writer) die(
"Failed to open %s for writing", outfile_name);
168 die(
"Failed to initialize encoder");
171 while (vpx_img_read(raw, infile)) {
175 if (max_frames > 0 && frame_count >= max_frames)
break;
186 vpx_video_writer_close(writer);
188 printf(
"Pass 1 complete. Processed %d frames.\n", frame_count);
191int main(
int argc,
char **argv) {
200 const VpxInterface *encoder = NULL;
202 const int bitrate = 200;
203 const char *
const codec_arg = argv[1];
204 const char *
const width_arg = argv[2];
205 const char *
const height_arg = argv[3];
206 const char *
const infile_arg = argv[4];
207 const char *
const outfile_arg = argv[5];
211 if (argc != 7) die(
"Invalid number of arguments.");
213 max_frames = (int)strtol(argv[6], NULL, 0);
215 encoder = get_vpx_encoder_by_name(codec_arg);
216 if (!encoder) die(
"Unsupported codec.");
218 w = (int)strtol(width_arg, NULL, 0);
219 h = (int)strtol(height_arg, NULL, 0);
221 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
222 die(
"Invalid frame size: %dx%d", w, h);
225 die(
"Failed to allocate image (%dx%d)", w, h);
231 if (res) die_codec(&codec,
"Failed to get default codec config.");
239 if (!(infile = fopen(infile_arg,
"rb")))
240 die(
"Failed to open %s for reading", infile_arg);
244 stats = pass0(&raw, infile, encoder, &cfg, max_frames);
250 pass1(&raw, infile, outfile_arg, encoder, &cfg, max_frames);
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
const void * vpx_codec_iter_t
Iterator.
Definition vpx_codec.h:190
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
@ VPX_CODEC_OK
Operation completed without error.
Definition vpx_codec.h:95
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition vpx_encoder.h:894
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:985
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
#define VPX_FRAME_IS_KEY
Definition vpx_encoder.h:119
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition vpx_encoder.h:109
long vpx_enc_frame_flags_t
Encoded Frame Flags.
Definition vpx_encoder.h:263
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
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.
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:151
@ VPX_CODEC_STATS_PKT
Definition vpx_encoder.h:152
@ VPX_RC_LAST_PASS
Definition vpx_encoder.h:231
@ VPX_RC_FIRST_PASS
Definition vpx_encoder.h:230
Codec context structure.
Definition vpx_codec.h:200
Encoder output packet.
Definition vpx_encoder.h:163
vpx_codec_frame_flags_t flags
Definition vpx_encoder.h:173
vpx_fixed_buf_t twopass_stats
Definition vpx_encoder.h:186
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:164
struct vpx_codec_cx_pkt::@1::@2 frame
size_t sz
Definition vpx_encoder.h:168
void * buf
Definition vpx_encoder.h:167
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:170
union vpx_codec_cx_pkt::@1 data
Encoder configuration structure.
Definition vpx_encoder.h:272
vpx_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition vpx_encoder.h:451
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:317
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:308
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:347
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition vpx_encoder.h:362
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:464
Generic fixed size buffer structure.
Definition vpx_encoder.h:99
size_t sz
Definition vpx_encoder.h:101
void * buf
Definition vpx_encoder.h:100
Image Descriptor.
Definition vpx_image.h:72
int den
Definition vpx_encoder.h:224
int num
Definition vpx_encoder.h:223
Describes the encoder algorithm interface to applications.
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.
@ VPX_IMG_FMT_I420
Definition vpx_image.h:42
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.