Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: source/libvpx/vp9_spatial_scalable_encoder.c

Issue 181493009: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/vp9cx.mk ('k') | source/libvpx/vpx/internal/vpx_psnr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 /*
12 * This is an example demonstrating how to implement a multi-layer
13 * VP9 encoding scheme based on spatial scalability for video applications
14 * that benefit from a scalable bitstream.
15 */
16
17 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21
22 #include "./args.h"
23 #include "./tools_common.h"
24 #include "./video_writer.h"
25
26 #include "vpx/svc_context.h"
27 #include "vpx/vp8cx.h"
28 #include "vpx/vpx_encoder.h"
29
30 static const struct arg_enum_list encoding_mode_enum[] = {
31 {"i", INTER_LAYER_PREDICTION_I},
32 {"alt-ip", ALT_INTER_LAYER_PREDICTION_IP},
33 {"ip", INTER_LAYER_PREDICTION_IP},
34 {"gf", USE_GOLDEN_FRAME},
35 {NULL, 0}
36 };
37
38 static const arg_def_t encoding_mode_arg = ARG_DEF_ENUM(
39 "m", "encoding-mode", 1, "Encoding mode algorithm", encoding_mode_enum);
40 static const arg_def_t skip_frames_arg =
41 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
42 static const arg_def_t frames_arg =
43 ARG_DEF("f", "frames", 1, "number of frames to encode");
44 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
45 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
46 static const arg_def_t timebase_arg =
47 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
48 static const arg_def_t bitrate_arg = ARG_DEF(
49 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
50 static const arg_def_t layers_arg =
51 ARG_DEF("l", "layers", 1, "number of SVC layers");
52 static const arg_def_t kf_dist_arg =
53 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
54 static const arg_def_t scale_factors_arg =
55 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
56 static const arg_def_t quantizers_arg =
57 ARG_DEF("q", "quantizers", 1, "quantizers (lowest to highest layer)");
58
59 static const arg_def_t *svc_args[] = {
60 &encoding_mode_arg, &frames_arg, &width_arg, &height_arg,
61 &timebase_arg, &bitrate_arg, &skip_frames_arg, &layers_arg,
62 &kf_dist_arg, &scale_factors_arg, &quantizers_arg, NULL
63 };
64
65 static const SVC_ENCODING_MODE default_encoding_mode =
66 INTER_LAYER_PREDICTION_IP;
67 static const uint32_t default_frames_to_skip = 0;
68 static const uint32_t default_frames_to_code = 60 * 60;
69 static const uint32_t default_width = 1920;
70 static const uint32_t default_height = 1080;
71 static const uint32_t default_timebase_num = 1;
72 static const uint32_t default_timebase_den = 60;
73 static const uint32_t default_bitrate = 1000;
74 static const uint32_t default_spatial_layers = 5;
75 static const uint32_t default_kf_dist = 100;
76
77 typedef struct {
78 const char *input_filename;
79 const char *output_filename;
80 uint32_t frames_to_code;
81 uint32_t frames_to_skip;
82 } AppInput;
83
84 static const char *exec_name;
85
86 void usage_exit() {
87 fprintf(stderr, "Usage: %s <options> input_filename output_filename\n",
88 exec_name);
89 fprintf(stderr, "Options:\n");
90 arg_show_usage(stderr, svc_args);
91 exit(EXIT_FAILURE);
92 }
93
94 static void parse_command_line(int argc, const char **argv_,
95 AppInput *app_input, SvcContext *svc_ctx,
96 vpx_codec_enc_cfg_t *enc_cfg) {
97 struct arg arg = {0};
98 char **argv = NULL;
99 char **argi = NULL;
100 char **argj = NULL;
101 vpx_codec_err_t res;
102
103 // initialize SvcContext with parameters that will be passed to vpx_svc_init
104 svc_ctx->log_level = SVC_LOG_DEBUG;
105 svc_ctx->spatial_layers = default_spatial_layers;
106 svc_ctx->encoding_mode = default_encoding_mode;
107
108 // start with default encoder configuration
109 res = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), enc_cfg, 0);
110 if (res) {
111 die("Failed to get config: %s\n", vpx_codec_err_to_string(res));
112 }
113 // update enc_cfg with app default values
114 enc_cfg->g_w = default_width;
115 enc_cfg->g_h = default_height;
116 enc_cfg->g_timebase.num = default_timebase_num;
117 enc_cfg->g_timebase.den = default_timebase_den;
118 enc_cfg->rc_target_bitrate = default_bitrate;
119 enc_cfg->kf_min_dist = default_kf_dist;
120 enc_cfg->kf_max_dist = default_kf_dist;
121
122 // initialize AppInput with default values
123 app_input->frames_to_code = default_frames_to_code;
124 app_input->frames_to_skip = default_frames_to_skip;
125
126 // process command line options
127 argv = argv_dup(argc - 1, argv_ + 1);
128 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
129 arg.argv_step = 1;
130
131 if (arg_match(&arg, &encoding_mode_arg, argi)) {
132 svc_ctx->encoding_mode = arg_parse_enum_or_int(&arg);
133 } else if (arg_match(&arg, &frames_arg, argi)) {
134 app_input->frames_to_code = arg_parse_uint(&arg);
135 } else if (arg_match(&arg, &width_arg, argi)) {
136 enc_cfg->g_w = arg_parse_uint(&arg);
137 } else if (arg_match(&arg, &height_arg, argi)) {
138 enc_cfg->g_h = arg_parse_uint(&arg);
139 } else if (arg_match(&arg, &timebase_arg, argi)) {
140 enc_cfg->g_timebase = arg_parse_rational(&arg);
141 } else if (arg_match(&arg, &bitrate_arg, argi)) {
142 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
143 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
144 app_input->frames_to_skip = arg_parse_uint(&arg);
145 } else if (arg_match(&arg, &layers_arg, argi)) {
146 svc_ctx->spatial_layers = arg_parse_uint(&arg);
147 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
148 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
149 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
150 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
151 vpx_svc_set_scale_factors(svc_ctx, arg.val);
152 } else if (arg_match(&arg, &quantizers_arg, argi)) {
153 vpx_svc_set_quantizers(svc_ctx, arg.val);
154 } else {
155 ++argj;
156 }
157 }
158
159 // Check for unrecognized options
160 for (argi = argv; *argi; ++argi)
161 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
162 die("Error: Unrecognized option %s\n", *argi);
163
164 if (argv[0] == NULL || argv[1] == 0) {
165 usage_exit();
166 }
167 app_input->input_filename = argv[0];
168 app_input->output_filename = argv[1];
169 free(argv);
170
171 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
172 enc_cfg->g_h % 2)
173 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
174
175 printf(
176 "Codec %s\nframes: %d, skip: %d\n"
177 "mode: %d, layers: %d\n"
178 "width %d, height: %d,\n"
179 "num: %d, den: %d, bitrate: %d,\n"
180 "gop size: %d\n",
181 vpx_codec_iface_name(vpx_codec_vp9_cx()), app_input->frames_to_code,
182 app_input->frames_to_skip, svc_ctx->encoding_mode,
183 svc_ctx->spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
184 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
185 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
186 }
187
188 int main(int argc, const char **argv) {
189 AppInput app_input = {0};
190 VpxVideoWriter *writer = NULL;
191 VpxVideoInfo info = {0};
192 vpx_codec_ctx_t codec;
193 vpx_codec_enc_cfg_t enc_cfg;
194 SvcContext svc_ctx;
195 uint32_t i;
196 uint32_t frame_cnt = 0;
197 vpx_image_t raw;
198 vpx_codec_err_t res;
199 int pts = 0; /* PTS starts at 0 */
200 int frame_duration = 1; /* 1 timebase tick per frame */
201 FILE *infile = NULL;
202
203 memset(&svc_ctx, 0, sizeof(svc_ctx));
204 svc_ctx.log_print = 1;
205 exec_name = argv[0];
206 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
207
208 // Allocate image buffer
209 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32))
210 die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
211
212 if (!(infile = fopen(app_input.input_filename, "rb")))
213 die("Failed to open %s for reading\n", app_input.input_filename);
214
215 // Initialize codec
216 if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
217 VPX_CODEC_OK)
218 die("Failed to initialize encoder\n");
219
220 info.codec_fourcc = VP9_FOURCC;
221 info.time_base.numerator = enc_cfg.g_timebase.num;
222 info.time_base.denominator = enc_cfg.g_timebase.den;
223 if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1,
224 (unsigned int *)&info.frame_width,
225 (unsigned int *)&info.frame_height) !=
226 VPX_CODEC_OK) {
227 die("Failed to get output resolution");
228 }
229 writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF,
230 &info);
231 if (!writer)
232 die("Failed to open %s for writing\n", app_input.output_filename);
233
234 // skip initial frames
235 for (i = 0; i < app_input.frames_to_skip; ++i)
236 vpx_img_read(&raw, infile);
237
238 // Encode frames
239 while (frame_cnt < app_input.frames_to_code) {
240 if (!vpx_img_read(&raw, infile))
241 break;
242
243 res = vpx_svc_encode(&svc_ctx, &codec, &raw, pts, frame_duration,
244 VPX_DL_REALTIME);
245 printf("%s", vpx_svc_get_message(&svc_ctx));
246 if (res != VPX_CODEC_OK) {
247 die_codec(&codec, "Failed to encode frame");
248 }
249 if (vpx_svc_get_frame_size(&svc_ctx) > 0) {
250 vpx_video_writer_write_frame(writer,
251 vpx_svc_get_buffer(&svc_ctx),
252 vpx_svc_get_frame_size(&svc_ctx),
253 pts);
254 }
255 ++frame_cnt;
256 pts += frame_duration;
257 }
258
259 printf("Processed %d frames\n", frame_cnt);
260
261 fclose(infile);
262 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
263
264 vpx_video_writer_close(writer);
265
266 vpx_img_free(&raw);
267
268 // display average size, psnr
269 printf("%s", vpx_svc_dump_statistics(&svc_ctx));
270
271 vpx_svc_release(&svc_ctx);
272
273 return EXIT_SUCCESS;
274 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/vp9cx.mk ('k') | source/libvpx/vpx/internal/vpx_psnr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698