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#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
56#include "vp9/common/vp9_common.h"
57
58#include "./tools_common.h"
59#include "./video_writer.h"
60
61static const char *exec_name;
62
63void usage_exit() {
64 fprintf(stderr,
65 "Usage: %s <width> <height> <infile> <outfile> "
66 "<frame> <limit(optional)>\n",
67 exec_name);
68 exit(EXIT_FAILURE);
69}
70
72 unsigned int frame_out, int *mismatch_seen) {
75
76 if (*mismatch_seen) return;
77
79 ref_dec.idx = 0;
81 die_codec(encoder, "Failed to get encoder reference frame");
82 enc_img = ref_enc.img;
84 die_codec(decoder, "Failed to get decoder reference frame");
85 dec_img = ref_dec.img;
86
87 if (!compare_img(&enc_img, &dec_img)) {
88 int y[4], u[4], v[4];
89
90 *mismatch_seen = 1;
91
92 find_mismatch(&enc_img, &dec_img, y, u, v);
93 printf(
94 "Encode/decode mismatch on frame %d at"
95 " Y[%d, %d] {%d/%d},"
96 " U[%d, %d] {%d/%d},"
97 " V[%d, %d] {%d/%d}",
98 frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
99 v[2], v[3]);
100 }
101
104}
105
107 unsigned int frame_in, VpxVideoWriter *writer,
109 unsigned int *frame_out, int *mismatch_seen) {
110 int got_pkts = 0;
113 int got_data;
116 if (res !=
VPX_CODEC_OK) die_codec(ecodec,
"Failed to encode frame");
117
118 got_data = 0;
119
121 got_pkts = 1;
122
125
127 *frame_out += 1;
128 }
129
130 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
133 die_codec(ecodec, "Failed to write compressed frame");
134 }
135 printf(keyframe ? "K" : ".");
136 fflush(stdout);
137 got_data = 1;
138
139
140 if (test_decode) {
143 die_codec(dcodec, "Failed to decode frame.");
144 }
145 }
146 }
147
148
149 if (got_data && test_decode) {
150 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
151 }
152
153 return got_pkts;
154}
155
156int main(int argc, char **argv) {
157 FILE *infile = NULL;
158
161 unsigned int frame_in = 0;
164 VpxVideoInfo info;
165 VpxVideoWriter *writer = NULL;
166 const VpxInterface *encoder = NULL;
167
168
169 int test_decode = 1;
170
172 unsigned int frame_out = 0;
173
174
175 unsigned int update_frame_num = 0;
176 int mismatch_seen = 0;
177
178 const int fps = 30;
179 const int bitrate = 500;
180
181 const char *width_arg = NULL;
182 const char *height_arg = NULL;
183 const char *infile_arg = NULL;
184 const char *outfile_arg = NULL;
185 const char *update_frame_num_arg = NULL;
186 unsigned int limit = 0;
187
188 vp9_zero(ecodec);
189 vp9_zero(cfg);
190 vp9_zero(info);
191
192 exec_name = argv[0];
193
194 if (argc < 6) die("Invalid number of arguments");
195
196 width_arg = argv[1];
197 height_arg = argv[2];
198 infile_arg = argv[3];
199 outfile_arg = argv[4];
200 update_frame_num_arg = argv[5];
201
202 encoder = get_vpx_encoder_by_name("vp9");
203 if (!encoder) die("Unsupported codec.");
204
205 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
206
207
208
209 if (update_frame_num <= 1) {
210 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
211 }
212
213 if (argc > 6) {
214 limit = (unsigned int)strtoul(argv[6], NULL, 0);
215 if (update_frame_num > limit)
216 die("Update frame number couldn't larger than limit\n");
217 }
218
219 info.codec_fourcc = encoder->fourcc;
220 info.frame_width = (int)strtol(width_arg, NULL, 0);
221 info.frame_height = (int)strtol(height_arg, NULL, 0);
222 info.time_base.numerator = 1;
223 info.time_base.denominator = fps;
224
225 if (info.frame_width <= 0 || info.frame_height <= 0 ||
226 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
227 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
228 }
229
231 info.frame_height, 1)) {
232 die("Failed to allocate image.");
233 }
234
236
238 if (res) die_codec(&ecodec, "Failed to get default codec config.");
239
240 cfg.
g_w = info.frame_width;
241 cfg.
g_h = info.frame_height;
246
247 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
248 if (!writer) die("Failed to open %s for writing.", outfile_arg);
249
250 if (!(infile = fopen(infile_arg, "rb")))
251 die("Failed to open %s for reading.", infile_arg);
252
254 die("Failed to initialize encoder");
255
256
258 die_codec(&ecodec, "Failed to set enable auto alt ref");
259
260 if (test_decode) {
261 const VpxInterface *decoder = get_vpx_decoder_by_name("vp9");
263 die_codec(&dcodec, "Failed to initialize decoder.");
264 }
265
266
267 while (vpx_img_read(&raw, infile)) {
268 if (limit && frame_in >= limit) break;
269 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
273
275 die_codec(&ecodec, "Failed to set reference frame");
276 printf(" <SET_REF>");
277
278
279
280 if (test_decode) {
282 die_codec(&dcodec, "Failed to set reference frame");
283 }
284 }
285
286 encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
287 &frame_out, &mismatch_seen);
288 frame_in++;
289 if (mismatch_seen) break;
290 }
291
292
293 if (!mismatch_seen)
294 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
295 &frame_out, &mismatch_seen)) {
296 }
297
298 printf("\n");
299 fclose(infile);
300 printf("Processed %d frames.\n", frame_out);
301
302 if (test_decode) {
303 if (!mismatch_seen)
304 printf("Encoder/decoder results are matching.\n");
305 else
306 printf("Encoder/decoder results are NOT matching.\n");
307 }
308
309 if (test_decode)
311 die_codec(&dcodec, "Failed to destroy decoder");
312
315 die_codec(&ecodec, "Failed to destroy encoder.");
316
317 vpx_video_writer_close(writer);
318
319 return EXIT_SUCCESS;
320}
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.
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition vpx_codec.h:408
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
@ VPX_CODEC_OK
Operation completed without error.
Definition vpx_codec.h:95
vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data, unsigned int data_sz, void *user_priv, long deadline)
Decode data.
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition vpx_decoder.h:143
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
#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
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.
#define VPX_FRAME_IS_FRAGMENT
this is a fragment of the encoded frame
Definition vpx_encoder.h:126
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:151
@ VP8E_SET_ENABLEAUTOALTREF
Codec control function to enable automatic use of arf frames.
Definition vp8cx.h:182
struct vpx_ref_frame vpx_ref_frame_t
reference frame data struct
@ VP8_SET_REFERENCE
pass in an external frame into decoder to be used as reference frame
Definition vp8.h:47
@ VP9_GET_REFERENCE
Definition vp8.h:55
VP9 specific reference frame data struct.
Definition vp8.h:110
int idx
Definition vp8.h:111
vpx_image_t img
Definition vp8.h:112
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
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
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
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition vpx_encoder.h:376
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:464
int den
Definition vpx_encoder.h:224
int num
Definition vpx_encoder.h:223
vpx_ref_frame_type_t frame_type
Definition vp8.h:102
vpx_image_t img
Definition vp8.h:103
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
Describes the decoder algorithm interface to applications.
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.