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

Side by Side Diff: source/libvpx/examples/twopass_encoder.c

Issue 554673004: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 #include "./video_writer.h" 59 #include "./video_writer.h"
60 60
61 static const char *exec_name; 61 static const char *exec_name;
62 62
63 void usage_exit() { 63 void usage_exit() {
64 fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n", 64 fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
65 exec_name); 65 exec_name);
66 exit(EXIT_FAILURE); 66 exit(EXIT_FAILURE);
67 } 67 }
68 68
69 static void get_frame_stats(vpx_codec_ctx_t *ctx, 69 static int get_frame_stats(vpx_codec_ctx_t *ctx,
70 const vpx_image_t *img, 70 const vpx_image_t *img,
71 vpx_codec_pts_t pts, 71 vpx_codec_pts_t pts,
72 unsigned int duration, 72 unsigned int duration,
73 vpx_enc_frame_flags_t flags, 73 vpx_enc_frame_flags_t flags,
74 unsigned int deadline, 74 unsigned int deadline,
75 vpx_fixed_buf_t *stats) { 75 vpx_fixed_buf_t *stats) {
76 int got_pkts = 0;
76 vpx_codec_iter_t iter = NULL; 77 vpx_codec_iter_t iter = NULL;
77 const vpx_codec_cx_pkt_t *pkt = NULL; 78 const vpx_codec_cx_pkt_t *pkt = NULL;
78 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags, 79 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags,
79 deadline); 80 deadline);
80 if (res != VPX_CODEC_OK) 81 if (res != VPX_CODEC_OK)
81 die_codec(ctx, "Failed to get frame stats."); 82 die_codec(ctx, "Failed to get frame stats.");
82 83
83 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) { 84 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
85 got_pkts = 1;
86
84 if (pkt->kind == VPX_CODEC_STATS_PKT) { 87 if (pkt->kind == VPX_CODEC_STATS_PKT) {
85 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf; 88 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
86 const size_t pkt_size = pkt->data.twopass_stats.sz; 89 const size_t pkt_size = pkt->data.twopass_stats.sz;
87 stats->buf = realloc(stats->buf, stats->sz + pkt_size); 90 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
88 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size); 91 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
89 stats->sz += pkt_size; 92 stats->sz += pkt_size;
90 } 93 }
91 } 94 }
95
96 return got_pkts;
92 } 97 }
93 98
94 static void encode_frame(vpx_codec_ctx_t *ctx, 99 static int encode_frame(vpx_codec_ctx_t *ctx,
95 const vpx_image_t *img, 100 const vpx_image_t *img,
96 vpx_codec_pts_t pts, 101 vpx_codec_pts_t pts,
97 unsigned int duration, 102 unsigned int duration,
98 vpx_enc_frame_flags_t flags, 103 vpx_enc_frame_flags_t flags,
99 unsigned int deadline, 104 unsigned int deadline,
100 VpxVideoWriter *writer) { 105 VpxVideoWriter *writer) {
106 int got_pkts = 0;
101 vpx_codec_iter_t iter = NULL; 107 vpx_codec_iter_t iter = NULL;
102 const vpx_codec_cx_pkt_t *pkt = NULL; 108 const vpx_codec_cx_pkt_t *pkt = NULL;
103 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags, 109 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags,
104 deadline); 110 deadline);
105 if (res != VPX_CODEC_OK) 111 if (res != VPX_CODEC_OK)
106 die_codec(ctx, "Failed to encode frame."); 112 die_codec(ctx, "Failed to encode frame.");
107 113
108 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) { 114 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
115 got_pkts = 1;
109 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { 116 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
110 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; 117 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
111 118
112 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf, 119 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
113 pkt->data.frame.sz, 120 pkt->data.frame.sz,
114 pkt->data.frame.pts)) 121 pkt->data.frame.pts))
115 die_codec(ctx, "Failed to write compressed frame."); 122 die_codec(ctx, "Failed to write compressed frame.");
116 printf(keyframe ? "K" : "."); 123 printf(keyframe ? "K" : ".");
117 fflush(stdout); 124 fflush(stdout);
118 } 125 }
119 } 126 }
127
128 return got_pkts;
129 }
130
131 static vpx_fixed_buf_t pass0(vpx_image_t *raw,
132 FILE *infile,
133 const VpxInterface *encoder,
134 const vpx_codec_enc_cfg_t *cfg) {
135 vpx_codec_ctx_t codec;
136 int frame_count = 0;
137 vpx_fixed_buf_t stats = {NULL, 0};
138
139 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
140 die_codec(&codec, "Failed to initialize encoder");
141
142 // Calculate frame statistics.
143 while (vpx_img_read(raw, infile)) {
144 ++frame_count;
145 get_frame_stats(&codec, raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
146 &stats);
147 }
148
149 // Flush encoder.
150 while (get_frame_stats(&codec, NULL, frame_count, 1, 0,
151 VPX_DL_BEST_QUALITY, &stats)) {}
152
153 printf("Pass 0 complete. Processed %d frames.\n", frame_count);
154 if (vpx_codec_destroy(&codec))
155 die_codec(&codec, "Failed to destroy codec.");
156
157 return stats;
158 }
159
160 static void pass1(vpx_image_t *raw,
161 FILE *infile,
162 const char *outfile_name,
163 const VpxInterface *encoder,
164 const vpx_codec_enc_cfg_t *cfg) {
165 VpxVideoInfo info = {
166 encoder->fourcc,
167 cfg->g_w,
168 cfg->g_h,
169 {cfg->g_timebase.num, cfg->g_timebase.den}
170 };
171 VpxVideoWriter *writer = NULL;
172 vpx_codec_ctx_t codec;
173 int frame_count = 0;
174
175 writer = vpx_video_writer_open(outfile_name, kContainerIVF, &info);
176 if (!writer)
177 die("Failed to open %s for writing", outfile_name);
178
179 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
180 die_codec(&codec, "Failed to initialize encoder");
181
182 // Encode frames.
183 while (vpx_img_read(raw, infile)) {
184 ++frame_count;
185 encode_frame(&codec, raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY, writer);
186 }
187
188 // Flush encoder.
189 while (encode_frame(&codec, NULL, -1, 1, 0, VPX_DL_BEST_QUALITY, writer)) {}
190
191 printf("\n");
192
193 if (vpx_codec_destroy(&codec))
194 die_codec(&codec, "Failed to destroy codec.");
195
196 vpx_video_writer_close(writer);
197
198 printf("Pass 1 complete. Processed %d frames.\n", frame_count);
120 } 199 }
121 200
122 int main(int argc, char **argv) { 201 int main(int argc, char **argv) {
123 FILE *infile = NULL; 202 FILE *infile = NULL;
124 VpxVideoWriter *writer = NULL; 203 int w, h;
125 vpx_codec_ctx_t codec; 204 vpx_codec_ctx_t codec;
126 vpx_codec_enc_cfg_t cfg; 205 vpx_codec_enc_cfg_t cfg;
127 vpx_image_t raw; 206 vpx_image_t raw;
128 vpx_codec_err_t res; 207 vpx_codec_err_t res;
129 vpx_fixed_buf_t stats = {0}; 208 vpx_fixed_buf_t stats;
130 VpxVideoInfo info = {0}; 209
131 const VpxInterface *encoder = NULL; 210 const VpxInterface *encoder = NULL;
132 int pass;
133 const int fps = 30; // TODO(dkovalev) add command line argument 211 const int fps = 30; // TODO(dkovalev) add command line argument
134 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument 212 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
135 const char *const codec_arg = argv[1]; 213 const char *const codec_arg = argv[1];
136 const char *const width_arg = argv[2]; 214 const char *const width_arg = argv[2];
137 const char *const height_arg = argv[3]; 215 const char *const height_arg = argv[3];
138 const char *const infile_arg = argv[4]; 216 const char *const infile_arg = argv[4];
139 const char *const outfile_arg = argv[5]; 217 const char *const outfile_arg = argv[5];
140 exec_name = argv[0]; 218 exec_name = argv[0];
141 219
142 if (argc != 6) 220 if (argc != 6)
143 die("Invalid number of arguments."); 221 die("Invalid number of arguments.");
144 222
145 encoder = get_vpx_encoder_by_name(codec_arg); 223 encoder = get_vpx_encoder_by_name(codec_arg);
146 if (!encoder) 224 if (!encoder)
147 die("Unsupported codec."); 225 die("Unsupported codec.");
148 226
149 info.codec_fourcc = encoder->fourcc; 227 w = strtol(width_arg, NULL, 0);
150 info.time_base.numerator = 1; 228 h = strtol(height_arg, NULL, 0);
151 info.time_base.denominator = fps;
152 info.frame_width = strtol(width_arg, NULL, 0);
153 info.frame_height = strtol(height_arg, NULL, 0);
154 229
155 if (info.frame_width <= 0 || 230 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
156 info.frame_height <= 0 || 231 die("Invalid frame size: %dx%d", w, h);
157 (info.frame_width % 2) != 0 ||
158 (info.frame_height % 2) != 0) {
159 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
160 }
161 232
162 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width, 233 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, w, h, 1))
163 info.frame_height, 1)) { 234 die("Failed to allocate image", w, h);
164 die("Failed to allocate image", info.frame_width, info.frame_height);
165 }
166
167 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
168 if (!writer)
169 die("Failed to open %s for writing", outfile_arg);
170 235
171 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface())); 236 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
172 237
238 // Configuration
173 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); 239 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
174 if (res) 240 if (res)
175 die_codec(&codec, "Failed to get default codec config."); 241 die_codec(&codec, "Failed to get default codec config.");
176 242
177 cfg.g_w = info.frame_width; 243 cfg.g_w = w;
178 cfg.g_h = info.frame_height; 244 cfg.g_h = h;
179 cfg.g_timebase.num = info.time_base.numerator; 245 cfg.g_timebase.num = 1;
180 cfg.g_timebase.den = info.time_base.denominator; 246 cfg.g_timebase.den = fps;
181 cfg.rc_target_bitrate = bitrate; 247 cfg.rc_target_bitrate = bitrate;
182 248
183 for (pass = 0; pass < 2; ++pass) { 249 if (!(infile = fopen(infile_arg, "rb")))
184 int frame_count = 0; 250 die("Failed to open %s for reading", infile_arg);
185 251
186 if (pass == 0) { 252 // Pass 0
187 cfg.g_pass = VPX_RC_FIRST_PASS; 253 cfg.g_pass = VPX_RC_FIRST_PASS;
188 } else { 254 stats = pass0(&raw, infile, encoder, &cfg);
189 cfg.g_pass = VPX_RC_LAST_PASS;
190 cfg.rc_twopass_stats_in = stats;
191 }
192 255
193 if (!(infile = fopen(infile_arg, "rb"))) 256 // Pass 1
194 die("Failed to open %s for reading", infile_arg); 257 rewind(infile);
195 258 cfg.g_pass = VPX_RC_LAST_PASS;
196 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0)) 259 cfg.rc_twopass_stats_in = stats;
197 die_codec(&codec, "Failed to initialize encoder"); 260 pass1(&raw, infile, outfile_arg, encoder, &cfg);
198 261 free(stats.buf);
199 while (vpx_img_read(&raw, infile)) {
200 ++frame_count;
201
202 if (pass == 0) {
203 get_frame_stats(&codec, &raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
204 &stats);
205 } else {
206 encode_frame(&codec, &raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
207 writer);
208 }
209 }
210
211 if (pass == 0) {
212 get_frame_stats(&codec, NULL, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
213 &stats);
214 } else {
215 printf("\n");
216 }
217
218 fclose(infile);
219 printf("Pass %d complete. Processed %d frames.\n", pass + 1, frame_count);
220 if (vpx_codec_destroy(&codec))
221 die_codec(&codec, "Failed to destroy codec.");
222 }
223 262
224 vpx_img_free(&raw); 263 vpx_img_free(&raw);
225 free(stats.buf); 264 fclose(infile);
226
227 vpx_video_writer_close(writer);
228 265
229 return EXIT_SUCCESS; 266 return EXIT_SUCCESS;
230 } 267 }
OLDNEW
« no previous file with comments | « source/libvpx/examples/set_maps.c ('k') | source/libvpx/examples/vp8_multi_resolution_encoder.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698