2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
14 // This is an example demonstrating how to control placement of keyframes
15 // on a frame-by-frame basis.
19 // Keyframes can be forced by setting the VPX_EFLAG_FORCE_KF bit of the
20 // flags passed to `vpx_codec_control()`. In this example, we force a
21 // keyframe every 8 frames.
23 // Observing The Effects
24 // ---------------------
25 // The output of the encoder examples shows a 'K' rather than a dot '.'
26 // when the encoder generates a keyframe. Note that every 8 frames a 'K'
33 #define VPX_CODEC_DISABLE_COMPAT 1
34 #include "vpx/vpx_encoder.h"
35 #include "vpx/vp8cx.h"
36 #define interface (vpx_codec_vp8_cx())
37 #define fourcc 0x30385056
39 #define IVF_FILE_HDR_SZ (32)
40 #define IVF_FRAME_HDR_SZ (12)
42 static void mem_put_le16(char *mem, unsigned int val) {
47 static void mem_put_le32(char *mem, unsigned int val) {
54 static void die(const char *fmt, ...) {
59 if(fmt[strlen(fmt)-1] != '\n')
64 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
65 const char *detail = vpx_codec_error_detail(ctx);
67 printf("%s: %s\n", s, vpx_codec_error(ctx));
69 printf(" %s\n",detail);
73 static int read_frame(FILE *f, vpx_image_t *img) {
74 size_t nbytes, to_read;
77 to_read = img->w*img->h*3/2;
78 nbytes = fread(img->planes[0], 1, to_read, f);
79 if(nbytes != to_read) {
82 printf("Warning: Read partial frame. Check your width & height!\n");
87 static void write_ivf_file_header(FILE *outfile,
88 const vpx_codec_enc_cfg_t *cfg,
92 if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
98 mem_put_le16(header+4, 0); /* version */
99 mem_put_le16(header+6, 32); /* headersize */
100 mem_put_le32(header+8, fourcc); /* headersize */
101 mem_put_le16(header+12, cfg->g_w); /* width */
102 mem_put_le16(header+14, cfg->g_h); /* height */
103 mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
104 mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
105 mem_put_le32(header+24, frame_cnt); /* length */
106 mem_put_le32(header+28, 0); /* unused */
108 (void) fwrite(header, 1, 32, outfile);
112 static void write_ivf_frame_header(FILE *outfile,
113 const vpx_codec_cx_pkt_t *pkt)
118 if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
121 pts = pkt->data.frame.pts;
122 mem_put_le32(header, (unsigned int)pkt->data.frame.sz);
123 mem_put_le32(header+4, pts&0xFFFFFFFF);
124 mem_put_le32(header+8, pts >> 32);
126 (void) fwrite(header, 1, 12, outfile);
129 int main(int argc, char **argv) {
130 FILE *infile, *outfile;
131 vpx_codec_ctx_t codec;
132 vpx_codec_enc_cfg_t cfg;
144 die("Usage: %s <width> <height> <infile> <outfile>\n", argv[0]);
145 width = strtol(argv[1], NULL, 0);
146 height = strtol(argv[2], NULL, 0);
147 if(width < 16 || width%2 || height <16 || height%2)
148 die("Invalid resolution: %ldx%ld", width, height);
149 if(!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 1))
150 die("Faile to allocate image", width, height);
151 if(!(outfile = fopen(argv[4], "wb")))
152 die("Failed to open %s for writing", argv[4]);
154 printf("Using %s\n",vpx_codec_iface_name(interface));
156 /* Populate encoder configuration */
157 res = vpx_codec_enc_config_default(interface, &cfg, 0);
159 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
163 /* Update the default configuration with our settings */
164 cfg.rc_target_bitrate = width * height * cfg.rc_target_bitrate
169 write_ivf_file_header(outfile, &cfg, 0);
172 /* Open input file for this encoding pass */
173 if(!(infile = fopen(argv[3], "rb")))
174 die("Failed to open %s for reading", argv[3]);
176 /* Initialize codec */
177 if(vpx_codec_enc_init(&codec, interface, &cfg, 0))
178 die_codec(&codec, "Failed to initialize encoder");
182 while(frame_avail || got_data) {
183 vpx_codec_iter_t iter = NULL;
184 const vpx_codec_cx_pkt_t *pkt;
187 flags |= VPX_EFLAG_FORCE_KF;
189 flags &= ~VPX_EFLAG_FORCE_KF;
190 frame_avail = read_frame(infile, &raw);
191 if(vpx_codec_encode(&codec, frame_avail? &raw : NULL, frame_cnt,
192 1, flags, VPX_DL_REALTIME))
193 die_codec(&codec, "Failed to encode frame");
195 while( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
198 case VPX_CODEC_CX_FRAME_PKT:
199 write_ivf_frame_header(outfile, pkt);
200 (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
206 printf(pkt->kind == VPX_CODEC_CX_FRAME_PKT
207 && (pkt->data.frame.flags & VPX_FRAME_IS_KEY)? "K":".");
215 printf("Processed %d frames.\n",frame_cnt-1);
217 if(vpx_codec_destroy(&codec))
218 die_codec(&codec, "Failed to destroy codec");
220 /* Try to rewrite the file header with the actual frame count */
221 if(!fseek(outfile, 0, SEEK_SET))
222 write_ivf_file_header(outfile, &cfg, frame_cnt-1);