00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #include <stdio.h>
00099 #include <stdlib.h>
00100 #include <string.h>
00101
00102 #include "vpx/vpx_encoder.h"
00103
00104 #include "../tools_common.h"
00105 #include "../video_writer.h"
00106
00107 static const char *exec_name;
00108
00109 void usage_exit(void) {
00110 fprintf(stderr,
00111 "Usage: %s <codec> <width> <height> <infile> <outfile> "
00112 "<keyframe-interval> <error-resilient> <frames to encode>\n"
00113 "See comments in simple_encoder.c for more information.\n",
00114 exec_name);
00115 exit(EXIT_FAILURE);
00116 }
00117
00118 static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
00119 int frame_index, int flags, VpxVideoWriter *writer) {
00120 int got_pkts = 0;
00121 vpx_codec_iter_t iter = NULL;
00122 const vpx_codec_cx_pkt_t *pkt = NULL;
00123 const vpx_codec_err_t res =
00124 vpx_codec_encode(codec, img, frame_index, 1, flags, VPX_DL_GOOD_QUALITY);
00125 if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
00126
00127 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
00128 got_pkts = 1;
00129
00130 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
00131 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
00132 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
00133 pkt->data.frame.sz,
00134 pkt->data.frame.pts)) {
00135 die_codec(codec, "Failed to write compressed frame");
00136 }
00137 printf(keyframe ? "K" : ".");
00138 fflush(stdout);
00139 }
00140 }
00141
00142 return got_pkts;
00143 }
00144
00145
00146 int main(int argc, char **argv) {
00147 FILE *infile = NULL;
00148 vpx_codec_ctx_t codec;
00149 vpx_codec_enc_cfg_t cfg;
00150 int frame_count = 0;
00151 vpx_image_t raw;
00152 vpx_codec_err_t res;
00153 VpxVideoInfo info = { 0, 0, 0, { 0, 0 } };
00154 VpxVideoWriter *writer = NULL;
00155 const VpxInterface *encoder = NULL;
00156 const int fps = 30;
00157 const int bitrate = 200;
00158 int keyframe_interval = 0;
00159 int max_frames = 0;
00160 int frames_encoded = 0;
00161 const char *codec_arg = NULL;
00162 const char *width_arg = NULL;
00163 const char *height_arg = NULL;
00164 const char *infile_arg = NULL;
00165 const char *outfile_arg = NULL;
00166 const char *keyframe_interval_arg = NULL;
00167
00168 exec_name = argv[0];
00169
00170 if (argc != 9) die("Invalid number of arguments");
00171
00172 codec_arg = argv[1];
00173 width_arg = argv[2];
00174 height_arg = argv[3];
00175 infile_arg = argv[4];
00176 outfile_arg = argv[5];
00177 keyframe_interval_arg = argv[6];
00178 max_frames = (int)strtol(argv[8], NULL, 0);
00179
00180 encoder = get_vpx_encoder_by_name(codec_arg);
00181 if (!encoder) die("Unsupported codec.");
00182
00183 info.codec_fourcc = encoder->fourcc;
00184 info.frame_width = (int)strtol(width_arg, NULL, 0);
00185 info.frame_height = (int)strtol(height_arg, NULL, 0);
00186 info.time_base.numerator = 1;
00187 info.time_base.denominator = fps;
00188
00189 if (info.frame_width <= 0 || info.frame_height <= 0 ||
00190 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
00191 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
00192 }
00193
00194 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
00195 info.frame_height, 1)) {
00196 die("Failed to allocate image.");
00197 }
00198
00199 keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
00200 if (keyframe_interval < 0) die("Invalid keyframe interval value.");
00201
00202 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
00203
00204 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
00205 if (res) die_codec(&codec, "Failed to get default codec config.");
00206
00207 cfg.g_w = info.frame_width;
00208 cfg.g_h = info.frame_height;
00209 cfg.g_timebase.num = info.time_base.numerator;
00210 cfg.g_timebase.den = info.time_base.denominator;
00211 cfg.rc_target_bitrate = bitrate;
00212 cfg.g_error_resilient = (vpx_codec_er_flags_t)strtoul(argv[7], NULL, 0);
00213
00214 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
00215 if (!writer) die("Failed to open %s for writing.", outfile_arg);
00216
00217 if (!(infile = fopen(infile_arg, "rb")))
00218 die("Failed to open %s for reading.", infile_arg);
00219
00220 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
00221 die_codec(&codec, "Failed to initialize encoder");
00222
00223
00224 while (vpx_img_read(&raw, infile)) {
00225 int flags = 0;
00226 if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
00227 flags |= VPX_EFLAG_FORCE_KF;
00228 encode_frame(&codec, &raw, frame_count++, flags, writer);
00229 frames_encoded++;
00230 if (max_frames > 0 && frames_encoded >= max_frames) break;
00231 }
00232
00233
00234 while (encode_frame(&codec, NULL, -1, 0, writer)) {
00235 }
00236
00237 printf("\n");
00238 fclose(infile);
00239 printf("Processed %d frames.\n", frame_count);
00240
00241 vpx_img_free(&raw);
00242 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
00243
00244 vpx_video_writer_close(writer);
00245
00246 return EXIT_SUCCESS;
00247 }