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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98#include <stdio.h>
99#include <stdlib.h>
100#include <string.h>
101
103
104#include "../tools_common.h"
105#include "../video_writer.h"
106
107static const char *exec_name;
108
109void usage_exit(void) {
110 fprintf(stderr,
111 "Usage: %s <codec> <width> <height> <infile> <outfile> "
112 "<keyframe-interval> <error-resilient> <frames to encode>\n"
113 "See comments in simple_encoder.c for more information.\n",
114 exec_name);
115 exit(EXIT_FAILURE);
116}
117
119 int frame_index, int flags, VpxVideoWriter *writer) {
120 int got_pkts = 0;
125 if (res !=
VPX_CODEC_OK) die_codec(codec,
"Failed to encode frame");
126
128 got_pkts = 1;
129
132 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
135 die_codec(codec, "Failed to write compressed frame");
136 }
137 printf(keyframe ? "K" : ".");
138 fflush(stdout);
139 }
140 }
141
142 return got_pkts;
143}
144
145
146int main(int argc, char **argv) {
147 FILE *infile = NULL;
150 int frame_count = 0;
153 VpxVideoInfo info = { 0, 0, 0, { 0, 0 } };
154 VpxVideoWriter *writer = NULL;
155 const VpxInterface *encoder = NULL;
156 const int fps = 30;
157 const int bitrate = 200;
158 int keyframe_interval = 0;
159 int max_frames = 0;
160 int frames_encoded = 0;
161 const char *codec_arg = NULL;
162 const char *width_arg = NULL;
163 const char *height_arg = NULL;
164 const char *infile_arg = NULL;
165 const char *outfile_arg = NULL;
166 const char *keyframe_interval_arg = NULL;
167
168 exec_name = argv[0];
169
170 if (argc != 9) die("Invalid number of arguments");
171
172 codec_arg = argv[1];
173 width_arg = argv[2];
174 height_arg = argv[3];
175 infile_arg = argv[4];
176 outfile_arg = argv[5];
177 keyframe_interval_arg = argv[6];
178 max_frames = (int)strtol(argv[8], NULL, 0);
179
180 encoder = get_vpx_encoder_by_name(codec_arg);
181 if (!encoder) die("Unsupported codec.");
182
183 info.codec_fourcc = encoder->fourcc;
184 info.frame_width = (int)strtol(width_arg, NULL, 0);
185 info.frame_height = (int)strtol(height_arg, NULL, 0);
186 info.time_base.numerator = 1;
187 info.time_base.denominator = fps;
188
189 if (info.frame_width <= 0 || info.frame_height <= 0 ||
190 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
191 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
192 }
193
195 info.frame_height, 1)) {
196 die("Failed to allocate image.");
197 }
198
199 keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
200 if (keyframe_interval < 0) die("Invalid keyframe interval value.");
201
203
205 if (res) die_codec(&codec, "Failed to get default codec config.");
206
207 cfg.
g_w = info.frame_width;
208 cfg.
g_h = info.frame_height;
213
214 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
215 if (!writer) die("Failed to open %s for writing.", outfile_arg);
216
217 if (!(infile = fopen(infile_arg, "rb")))
218 die("Failed to open %s for reading.", infile_arg);
219
221 die("Failed to initialize encoder");
222
223
224 while (vpx_img_read(&raw, infile)) {
225 int flags = 0;
226 if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
228 encode_frame(&codec, &raw, frame_count++, flags, writer);
229 frames_encoded++;
230 if (max_frames > 0 && frames_encoded >= max_frames) break;
231 }
232
233
234 while (encode_frame(&codec, NULL, -1, 0, writer)) {
235 }
236
237 printf("\n");
238 fclose(infile);
239 printf("Processed %d frames.\n", frame_count);
240
243
244 vpx_video_writer_close(writer);
245
246 return EXIT_SUCCESS;
247}
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:900
#define VPX_EFLAG_FORCE_KF
Definition vpx_encoder.h:268
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:1006
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
uint32_t vpx_codec_er_flags_t
Error Resilient flags.
Definition vpx_encoder.h:138
#define VPX_FRAME_IS_KEY
Definition vpx_encoder.h:123
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, vpx_enc_deadline_t deadline)
Encode a frame.
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:155
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:177
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:168
size_t sz
Definition vpx_encoder.h:172
void * buf
Definition vpx_encoder.h:171
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:174
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:321
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition vpx_encoder.h:359
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:312
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:351
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:470
int den
Definition vpx_encoder.h:228
int num
Definition vpx_encoder.h:227
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.