OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 static const arg_def_t bitrate_arg = ARG_DEF( | 39 static const arg_def_t bitrate_arg = ARG_DEF( |
40 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second"); | 40 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second"); |
41 static const arg_def_t spatial_layers_arg = | 41 static const arg_def_t spatial_layers_arg = |
42 ARG_DEF("sl", "spatial-layers", 1, "number of spatial SVC layers"); | 42 ARG_DEF("sl", "spatial-layers", 1, "number of spatial SVC layers"); |
43 static const arg_def_t temporal_layers_arg = | 43 static const arg_def_t temporal_layers_arg = |
44 ARG_DEF("tl", "temporal-layers", 1, "number of temporal SVC layers"); | 44 ARG_DEF("tl", "temporal-layers", 1, "number of temporal SVC layers"); |
45 static const arg_def_t kf_dist_arg = | 45 static const arg_def_t kf_dist_arg = |
46 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes"); | 46 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes"); |
47 static const arg_def_t scale_factors_arg = | 47 static const arg_def_t scale_factors_arg = |
48 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)"); | 48 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)"); |
49 static const arg_def_t quantizers_arg = | |
50 ARG_DEF("q", "quantizers", 1, "quantizers for non key frames, also will " | |
51 "be applied to key frames if -qn is not specified (lowest to " | |
52 "highest layer)"); | |
53 static const arg_def_t passes_arg = | 49 static const arg_def_t passes_arg = |
54 ARG_DEF("p", "passes", 1, "Number of passes (1/2)"); | 50 ARG_DEF("p", "passes", 1, "Number of passes (1/2)"); |
55 static const arg_def_t pass_arg = | 51 static const arg_def_t pass_arg = |
56 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)"); | 52 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)"); |
57 static const arg_def_t fpf_name_arg = | 53 static const arg_def_t fpf_name_arg = |
58 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name"); | 54 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name"); |
59 static const arg_def_t min_q_arg = | 55 static const arg_def_t min_q_arg = |
60 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer"); | 56 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer"); |
61 static const arg_def_t max_q_arg = | 57 static const arg_def_t max_q_arg = |
62 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer"); | 58 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer"); |
63 static const arg_def_t min_bitrate_arg = | 59 static const arg_def_t min_bitrate_arg = |
64 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate"); | 60 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate"); |
65 static const arg_def_t max_bitrate_arg = | 61 static const arg_def_t max_bitrate_arg = |
66 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate"); | 62 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate"); |
67 | 63 |
68 static const arg_def_t *svc_args[] = { | 64 static const arg_def_t *svc_args[] = { |
69 &frames_arg, &width_arg, &height_arg, | 65 &frames_arg, &width_arg, &height_arg, |
70 &timebase_arg, &bitrate_arg, &skip_frames_arg, &spatial_layers_arg, | 66 &timebase_arg, &bitrate_arg, &skip_frames_arg, &spatial_layers_arg, |
71 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, &passes_arg, | 67 &kf_dist_arg, &scale_factors_arg, &passes_arg, &pass_arg, |
72 &pass_arg, &fpf_name_arg, &min_q_arg, &max_q_arg, | 68 &fpf_name_arg, &min_q_arg, &max_q_arg, &min_bitrate_arg, |
73 &min_bitrate_arg, &max_bitrate_arg, &temporal_layers_arg, | 69 &max_bitrate_arg, &temporal_layers_arg, NULL |
74 NULL | |
75 }; | 70 }; |
76 | 71 |
77 static const uint32_t default_frames_to_skip = 0; | 72 static const uint32_t default_frames_to_skip = 0; |
78 static const uint32_t default_frames_to_code = 60 * 60; | 73 static const uint32_t default_frames_to_code = 60 * 60; |
79 static const uint32_t default_width = 1920; | 74 static const uint32_t default_width = 1920; |
80 static const uint32_t default_height = 1080; | 75 static const uint32_t default_height = 1080; |
81 static const uint32_t default_timebase_num = 1; | 76 static const uint32_t default_timebase_num = 1; |
82 static const uint32_t default_timebase_den = 60; | 77 static const uint32_t default_timebase_den = 60; |
83 static const uint32_t default_bitrate = 1000; | 78 static const uint32_t default_bitrate = 1000; |
84 static const uint32_t default_spatial_layers = 5; | 79 static const uint32_t default_spatial_layers = 5; |
(...skipping 27 matching lines...) Expand all Loading... |
112 struct arg arg = {0}; | 107 struct arg arg = {0}; |
113 char **argv = NULL; | 108 char **argv = NULL; |
114 char **argi = NULL; | 109 char **argi = NULL; |
115 char **argj = NULL; | 110 char **argj = NULL; |
116 vpx_codec_err_t res; | 111 vpx_codec_err_t res; |
117 int passes = 0; | 112 int passes = 0; |
118 int pass = 0; | 113 int pass = 0; |
119 const char *fpf_file_name = NULL; | 114 const char *fpf_file_name = NULL; |
120 unsigned int min_bitrate = 0; | 115 unsigned int min_bitrate = 0; |
121 unsigned int max_bitrate = 0; | 116 unsigned int max_bitrate = 0; |
| 117 char string_options[1024] = {0}; |
122 | 118 |
123 // initialize SvcContext with parameters that will be passed to vpx_svc_init | 119 // initialize SvcContext with parameters that will be passed to vpx_svc_init |
124 svc_ctx->log_level = SVC_LOG_DEBUG; | 120 svc_ctx->log_level = SVC_LOG_DEBUG; |
125 svc_ctx->spatial_layers = default_spatial_layers; | 121 svc_ctx->spatial_layers = default_spatial_layers; |
126 svc_ctx->temporal_layers = default_temporal_layers; | 122 svc_ctx->temporal_layers = default_temporal_layers; |
127 | 123 |
128 // start with default encoder configuration | 124 // start with default encoder configuration |
129 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0); | 125 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0); |
130 if (res) { | 126 if (res) { |
131 die("Failed to get config: %s\n", vpx_codec_err_to_string(res)); | 127 die("Failed to get config: %s\n", vpx_codec_err_to_string(res)); |
(...skipping 30 matching lines...) Expand all Loading... |
162 } else if (arg_match(&arg, &skip_frames_arg, argi)) { | 158 } else if (arg_match(&arg, &skip_frames_arg, argi)) { |
163 app_input->frames_to_skip = arg_parse_uint(&arg); | 159 app_input->frames_to_skip = arg_parse_uint(&arg); |
164 } else if (arg_match(&arg, &spatial_layers_arg, argi)) { | 160 } else if (arg_match(&arg, &spatial_layers_arg, argi)) { |
165 svc_ctx->spatial_layers = arg_parse_uint(&arg); | 161 svc_ctx->spatial_layers = arg_parse_uint(&arg); |
166 } else if (arg_match(&arg, &temporal_layers_arg, argi)) { | 162 } else if (arg_match(&arg, &temporal_layers_arg, argi)) { |
167 svc_ctx->temporal_layers = arg_parse_uint(&arg); | 163 svc_ctx->temporal_layers = arg_parse_uint(&arg); |
168 } else if (arg_match(&arg, &kf_dist_arg, argi)) { | 164 } else if (arg_match(&arg, &kf_dist_arg, argi)) { |
169 enc_cfg->kf_min_dist = arg_parse_uint(&arg); | 165 enc_cfg->kf_min_dist = arg_parse_uint(&arg); |
170 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist; | 166 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist; |
171 } else if (arg_match(&arg, &scale_factors_arg, argi)) { | 167 } else if (arg_match(&arg, &scale_factors_arg, argi)) { |
172 vpx_svc_set_scale_factors(svc_ctx, arg.val); | 168 snprintf(string_options, 1024, "%s scale-factors=%s", |
173 } else if (arg_match(&arg, &quantizers_arg, argi)) { | 169 string_options, arg.val); |
174 vpx_svc_set_quantizers(svc_ctx, arg.val); | |
175 } else if (arg_match(&arg, &passes_arg, argi)) { | 170 } else if (arg_match(&arg, &passes_arg, argi)) { |
176 passes = arg_parse_uint(&arg); | 171 passes = arg_parse_uint(&arg); |
177 if (passes < 1 || passes > 2) { | 172 if (passes < 1 || passes > 2) { |
178 die("Error: Invalid number of passes (%d)\n", passes); | 173 die("Error: Invalid number of passes (%d)\n", passes); |
179 } | 174 } |
180 } else if (arg_match(&arg, &pass_arg, argi)) { | 175 } else if (arg_match(&arg, &pass_arg, argi)) { |
181 pass = arg_parse_uint(&arg); | 176 pass = arg_parse_uint(&arg); |
182 if (pass < 1 || pass > 2) { | 177 if (pass < 1 || pass > 2) { |
183 die("Error: Invalid pass selected (%d)\n", pass); | 178 die("Error: Invalid pass selected (%d)\n", pass); |
184 } | 179 } |
185 } else if (arg_match(&arg, &fpf_name_arg, argi)) { | 180 } else if (arg_match(&arg, &fpf_name_arg, argi)) { |
186 fpf_file_name = arg.val; | 181 fpf_file_name = arg.val; |
187 } else if (arg_match(&arg, &min_q_arg, argi)) { | 182 } else if (arg_match(&arg, &min_q_arg, argi)) { |
188 enc_cfg->rc_min_quantizer = arg_parse_uint(&arg); | 183 snprintf(string_options, 1024, "%s min-quantizers=%s", |
| 184 string_options, arg.val); |
189 } else if (arg_match(&arg, &max_q_arg, argi)) { | 185 } else if (arg_match(&arg, &max_q_arg, argi)) { |
190 enc_cfg->rc_max_quantizer = arg_parse_uint(&arg); | 186 snprintf(string_options, 1024, "%s max-quantizers=%s", |
| 187 string_options, arg.val); |
191 } else if (arg_match(&arg, &min_bitrate_arg, argi)) { | 188 } else if (arg_match(&arg, &min_bitrate_arg, argi)) { |
192 min_bitrate = arg_parse_uint(&arg); | 189 min_bitrate = arg_parse_uint(&arg); |
193 } else if (arg_match(&arg, &max_bitrate_arg, argi)) { | 190 } else if (arg_match(&arg, &max_bitrate_arg, argi)) { |
194 max_bitrate = arg_parse_uint(&arg); | 191 max_bitrate = arg_parse_uint(&arg); |
195 } else { | 192 } else { |
196 ++argj; | 193 ++argj; |
197 } | 194 } |
198 } | 195 } |
199 | 196 |
| 197 // There will be a space in front of the string options |
| 198 if (strlen(string_options) > 0) |
| 199 vpx_svc_set_options(svc_ctx, string_options + 1); |
| 200 |
200 if (passes == 0 || passes == 1) { | 201 if (passes == 0 || passes == 1) { |
201 if (pass) { | 202 if (pass) { |
202 fprintf(stderr, "pass is ignored since there's only one pass\n"); | 203 fprintf(stderr, "pass is ignored since there's only one pass\n"); |
203 } | 204 } |
204 enc_cfg->g_pass = VPX_RC_ONE_PASS; | 205 enc_cfg->g_pass = VPX_RC_ONE_PASS; |
205 } else { | 206 } else { |
206 if (pass == 0) { | 207 if (pass == 0) { |
207 die("pass must be specified when passes is 2\n"); | 208 die("pass must be specified when passes is 2\n"); |
208 } | 209 } |
209 | 210 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 vpx_codec_enc_cfg_t enc_cfg; | 276 vpx_codec_enc_cfg_t enc_cfg; |
276 SvcContext svc_ctx; | 277 SvcContext svc_ctx; |
277 uint32_t i; | 278 uint32_t i; |
278 uint32_t frame_cnt = 0; | 279 uint32_t frame_cnt = 0; |
279 vpx_image_t raw; | 280 vpx_image_t raw; |
280 vpx_codec_err_t res; | 281 vpx_codec_err_t res; |
281 int pts = 0; /* PTS starts at 0 */ | 282 int pts = 0; /* PTS starts at 0 */ |
282 int frame_duration = 1; /* 1 timebase tick per frame */ | 283 int frame_duration = 1; /* 1 timebase tick per frame */ |
283 FILE *infile = NULL; | 284 FILE *infile = NULL; |
284 int end_of_stream = 0; | 285 int end_of_stream = 0; |
285 int frame_size; | 286 int frames_received = 0; |
286 | 287 |
287 memset(&svc_ctx, 0, sizeof(svc_ctx)); | 288 memset(&svc_ctx, 0, sizeof(svc_ctx)); |
288 svc_ctx.log_print = 1; | 289 svc_ctx.log_print = 1; |
289 exec_name = argv[0]; | 290 exec_name = argv[0]; |
290 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg); | 291 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg); |
291 | 292 |
292 // Allocate image buffer | 293 // Allocate image buffer |
293 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32)) | 294 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32)) |
294 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h); | 295 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h); |
295 | 296 |
296 if (!(infile = fopen(app_input.input_filename, "rb"))) | 297 if (!(infile = fopen(app_input.input_filename, "rb"))) |
297 die("Failed to open %s for reading\n", app_input.input_filename); | 298 die("Failed to open %s for reading\n", app_input.input_filename); |
298 | 299 |
299 // Initialize codec | 300 // Initialize codec |
300 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) != | 301 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) != |
301 VPX_CODEC_OK) | 302 VPX_CODEC_OK) |
302 die("Failed to initialize encoder\n"); | 303 die("Failed to initialize encoder\n"); |
303 | 304 |
304 info.codec_fourcc = VP9_FOURCC; | 305 info.codec_fourcc = VP9_FOURCC; |
305 info.time_base.numerator = enc_cfg.g_timebase.num; | 306 info.time_base.numerator = enc_cfg.g_timebase.num; |
306 info.time_base.denominator = enc_cfg.g_timebase.den; | 307 info.time_base.denominator = enc_cfg.g_timebase.den; |
307 if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1, | |
308 (unsigned int *)&info.frame_width, | |
309 (unsigned int *)&info.frame_height) != | |
310 VPX_CODEC_OK) { | |
311 die("Failed to get output resolution"); | |
312 } | |
313 | 308 |
314 if (!(app_input.passes == 2 && app_input.pass == 1)) { | 309 if (!(app_input.passes == 2 && app_input.pass == 1)) { |
315 // We don't save the bitstream for the 1st pass on two pass rate control | 310 // We don't save the bitstream for the 1st pass on two pass rate control |
316 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF, | 311 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF, |
317 &info); | 312 &info); |
318 if (!writer) | 313 if (!writer) |
319 die("Failed to open %s for writing\n", app_input.output_filename); | 314 die("Failed to open %s for writing\n", app_input.output_filename); |
320 } | 315 } |
321 | 316 |
322 // skip initial frames | 317 // skip initial frames |
323 for (i = 0; i < app_input.frames_to_skip; ++i) | 318 for (i = 0; i < app_input.frames_to_skip; ++i) |
324 vpx_img_read(&raw, infile); | 319 vpx_img_read(&raw, infile); |
325 | 320 |
326 // Encode frames | 321 // Encode frames |
327 while (!end_of_stream) { | 322 while (!end_of_stream) { |
| 323 vpx_codec_iter_t iter = NULL; |
| 324 const vpx_codec_cx_pkt_t *cx_pkt; |
328 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) { | 325 if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) { |
329 // We need one extra vpx_svc_encode call at end of stream to flush | 326 // We need one extra vpx_svc_encode call at end of stream to flush |
330 // encoder and get remaining data | 327 // encoder and get remaining data |
331 end_of_stream = 1; | 328 end_of_stream = 1; |
332 } | 329 } |
333 | 330 |
334 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw), | 331 res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw), |
335 pts, frame_duration, VPX_DL_GOOD_QUALITY); | 332 pts, frame_duration, VPX_DL_GOOD_QUALITY); |
336 printf("%s", vpx_svc_get_message(&svc_ctx)); | 333 printf("%s", vpx_svc_get_message(&svc_ctx)); |
337 if (res != VPX_CODEC_OK) { | 334 if (res != VPX_CODEC_OK) { |
338 die_codec(&codec, "Failed to encode frame"); | 335 die_codec(&codec, "Failed to encode frame"); |
339 } | 336 } |
340 if (!(app_input.passes == 2 && app_input.pass == 1)) { | 337 |
341 while ((frame_size = vpx_svc_get_frame_size(&svc_ctx)) > 0) { | 338 while ((cx_pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) { |
342 vpx_video_writer_write_frame(writer, | 339 switch (cx_pkt->kind) { |
343 vpx_svc_get_buffer(&svc_ctx), | 340 case VPX_CODEC_CX_FRAME_PKT: { |
344 frame_size, pts); | 341 if (cx_pkt->data.frame.sz > 0) |
| 342 vpx_video_writer_write_frame(writer, |
| 343 cx_pkt->data.frame.buf, |
| 344 cx_pkt->data.frame.sz, |
| 345 cx_pkt->data.frame.pts); |
| 346 |
| 347 printf("SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received, |
| 348 !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY), |
| 349 (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts); |
| 350 ++frames_received; |
| 351 break; |
| 352 } |
| 353 case VPX_CODEC_STATS_PKT: { |
| 354 stats_write(&app_input.rc_stats, |
| 355 cx_pkt->data.twopass_stats.buf, |
| 356 cx_pkt->data.twopass_stats.sz); |
| 357 break; |
| 358 } |
| 359 default: { |
| 360 break; |
| 361 } |
345 } | 362 } |
346 } | 363 } |
347 if (vpx_svc_get_rc_stats_buffer_size(&svc_ctx) > 0) { | 364 |
348 stats_write(&app_input.rc_stats, | |
349 vpx_svc_get_rc_stats_buffer(&svc_ctx), | |
350 vpx_svc_get_rc_stats_buffer_size(&svc_ctx)); | |
351 } | |
352 if (!end_of_stream) { | 365 if (!end_of_stream) { |
353 ++frame_cnt; | 366 ++frame_cnt; |
354 pts += frame_duration; | 367 pts += frame_duration; |
355 } | 368 } |
356 } | 369 } |
357 | 370 |
358 printf("Processed %d frames\n", frame_cnt); | 371 printf("Processed %d frames\n", frame_cnt); |
359 | 372 |
360 fclose(infile); | 373 fclose(infile); |
361 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); | 374 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); |
362 | 375 |
363 if (app_input.passes == 2) | 376 if (app_input.passes == 2) |
364 stats_close(&app_input.rc_stats, 1); | 377 stats_close(&app_input.rc_stats, 1); |
365 | 378 |
366 if (writer) { | 379 if (writer) { |
367 vpx_video_writer_close(writer); | 380 vpx_video_writer_close(writer); |
368 } | 381 } |
369 | 382 |
370 vpx_img_free(&raw); | 383 vpx_img_free(&raw); |
371 | 384 |
372 // display average size, psnr | 385 // display average size, psnr |
373 printf("%s", vpx_svc_dump_statistics(&svc_ctx)); | 386 printf("%s", vpx_svc_dump_statistics(&svc_ctx)); |
374 | 387 |
375 vpx_svc_release(&svc_ctx); | 388 vpx_svc_release(&svc_ctx); |
376 | 389 |
377 return EXIT_SUCCESS; | 390 return EXIT_SUCCESS; |
378 } | 391 } |
OLD | NEW |