00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014
00015 #include "vpx/vpx_encoder.h"
00016 #include "vpx/vp8cx.h"
00017 #include "vp9/common/vp9_common.h"
00018
00019 #include "../tools_common.h"
00020 #include "../video_writer.h"
00021
00022 static const char *exec_name;
00023
00024 void usage_exit(void) {
00025 fprintf(stderr,
00026 "vp9_lossless_encoder: Example demonstrating VP9 lossless "
00027 "encoding feature. Supports raw input only.\n");
00028 fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
00029 exit(EXIT_FAILURE);
00030 }
00031
00032 static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
00033 int frame_index, int flags, VpxVideoWriter *writer) {
00034 int got_pkts = 0;
00035 vpx_codec_iter_t iter = NULL;
00036 const vpx_codec_cx_pkt_t *pkt = NULL;
00037 const vpx_codec_err_t res =
00038 vpx_codec_encode(codec, img, frame_index, 1, flags, VPX_DL_GOOD_QUALITY);
00039 if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
00040
00041 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
00042 got_pkts = 1;
00043
00044 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
00045 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
00046 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
00047 pkt->data.frame.sz,
00048 pkt->data.frame.pts)) {
00049 die_codec(codec, "Failed to write compressed frame");
00050 }
00051 printf(keyframe ? "K" : ".");
00052 fflush(stdout);
00053 }
00054 }
00055
00056 return got_pkts;
00057 }
00058
00059 int main(int argc, char **argv) {
00060 FILE *infile = NULL;
00061 vpx_codec_ctx_t codec;
00062 vpx_codec_enc_cfg_t cfg;
00063 int frame_count = 0;
00064 vpx_image_t raw;
00065 vpx_codec_err_t res;
00066 VpxVideoInfo info;
00067 VpxVideoWriter *writer = NULL;
00068 const VpxInterface *encoder = NULL;
00069 const int fps = 30;
00070
00071 vp9_zero(info);
00072
00073 exec_name = argv[0];
00074
00075 if (argc < 5) die("Invalid number of arguments");
00076
00077 encoder = get_vpx_encoder_by_name("vp9");
00078 if (!encoder) die("Unsupported codec.");
00079
00080 info.codec_fourcc = encoder->fourcc;
00081 info.frame_width = (int)strtol(argv[1], NULL, 0);
00082 info.frame_height = (int)strtol(argv[2], NULL, 0);
00083 info.time_base.numerator = 1;
00084 info.time_base.denominator = fps;
00085
00086 if (info.frame_width <= 0 || info.frame_height <= 0 ||
00087 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
00088 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
00089 }
00090
00091 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
00092 info.frame_height, 1)) {
00093 die("Failed to allocate image.");
00094 }
00095
00096 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
00097
00098 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
00099 if (res) die_codec(&codec, "Failed to get default codec config.");
00100
00101 cfg.g_w = info.frame_width;
00102 cfg.g_h = info.frame_height;
00103 cfg.g_timebase.num = info.time_base.numerator;
00104 cfg.g_timebase.den = info.time_base.denominator;
00105
00106 writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
00107 if (!writer) die("Failed to open %s for writing.", argv[4]);
00108
00109 if (!(infile = fopen(argv[3], "rb")))
00110 die("Failed to open %s for reading.", argv[3]);
00111
00112 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
00113 die_codec(&codec, "Failed to initialize encoder");
00114
00115 if (vpx_codec_control_(&codec, VP9E_SET_LOSSLESS, 1))
00116 die_codec(&codec, "Failed to use lossless mode");
00117
00118
00119 while (vpx_img_read(&raw, infile)) {
00120 encode_frame(&codec, &raw, frame_count++, 0, writer);
00121 }
00122
00123
00124 while (encode_frame(&codec, NULL, -1, 0, writer)) {
00125 }
00126
00127 printf("\n");
00128 fclose(infile);
00129 printf("Processed %d frames.\n", frame_count);
00130
00131 vpx_img_free(&raw);
00132 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
00133
00134 vpx_video_writer_close(writer);
00135
00136 return EXIT_SUCCESS;
00137 }