1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
55
56#include "../tools_common.h"
57#include "../video_writer.h"
58
59static const char *exec_name;
60
61void usage_exit(void) {
62 fprintf(stderr,
63 "Usage: %s <codec> <width> <height> <infile> <outfile> "
64 "<frame limit>\n",
65 exec_name);
66 exit(EXIT_FAILURE);
67}
68
73 int got_pkts = 0;
78 if (res !=
VPX_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
79
81 got_pkts = 1;
82
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;
90 }
91 }
92
93 return got_pkts;
94}
95
99 VpxVideoWriter *writer) {
100 int got_pkts = 0;
105 if (res !=
VPX_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
106
108 got_pkts = 1;
111
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" : ".");
117 fflush(stdout);
118 }
119 }
120
121 return got_pkts;
122}
123
125 const VpxInterface *encoder,
128 int frame_count = 0;
130
132 die("Failed to initialize encoder");
133
134
135 while (vpx_img_read(raw, infile)) {
136 ++frame_count;
138 &stats);
139 if (max_frames > 0 && frame_count >= max_frames) break;
140 }
141
142
144 &stats)) {
145 }
146
147 printf("Pass 0 complete. Processed %d frames.\n", frame_count);
149
150 return stats;
151}
152
153static void pass1(
vpx_image_t *raw, FILE *infile,
const char *outfile_name,
155 int max_frames) {
156 VpxVideoInfo info = { encoder->fourcc,
160 VpxVideoWriter *writer = NULL;
162 int frame_count = 0;
163
164 writer = vpx_video_writer_open(outfile_name, kContainerIVF, &info);
165 if (!writer) die("Failed to open %s for writing", outfile_name);
166
168 die("Failed to initialize encoder");
169
170
171 while (vpx_img_read(raw, infile)) {
172 ++frame_count;
174
175 if (max_frames > 0 && frame_count >= max_frames) break;
176 }
177
178
180 }
181
182 printf("\n");
183
185
186 vpx_video_writer_close(writer);
187
188 printf("Pass 1 complete. Processed %d frames.\n", frame_count);
189}
190
191int main(int argc, char **argv) {
192 FILE *infile = NULL;
193 int w, h;
199
200 const VpxInterface *encoder = NULL;
201 const int fps = 30;
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];
208 int max_frames = 0;
209 exec_name = argv[0];
210
211 if (argc != 7) die("Invalid number of arguments.");
212
213 max_frames = (int)strtol(argv[6], NULL, 0);
214
215 encoder = get_vpx_encoder_by_name(codec_arg);
216 if (!encoder) die("Unsupported codec.");
217
218 w = (int)strtol(width_arg, NULL, 0);
219 h = (int)strtol(height_arg, NULL, 0);
220
221 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
222 die("Invalid frame size: %dx%d", w, h);
223
225 die("Failed to allocate image (%dx%d)", w, h);
226
228
229
231 if (res) die_codec(&codec, "Failed to get default codec config.");
232
238
239 if (!(infile = fopen(infile_arg, "rb")))
240 die("Failed to open %s for reading", infile_arg);
241
242
244 stats = pass0(&raw, infile, encoder, &cfg, max_frames);
245
246
247 rewind(infile);
250 pass1(&raw, infile, outfile_arg, encoder, &cfg, max_frames);
252
254 fclose(infile);
255
256 return EXIT_SUCCESS;
257}
struct vpx_codec_ctx vpx_codec_ctx_t
Codec context structure.
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
struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t
Encoder configuration structure.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition vpx_encoder.h:894
struct vpx_fixed_buf vpx_fixed_buf_t
Generic fixed size buffer structure.
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:994
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.
struct vpx_codec_cx_pkt vpx_codec_cx_pkt_t
Encoder output packet.
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
union vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240 data
struct vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240::@337301343345304110063267327113124066016321050157 frame
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
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
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
size_t sz
Definition vpx_encoder.h:101
void * buf
Definition vpx_encoder.h:100
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
struct vpx_image vpx_image_t
Image Descriptor.
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.