1 /*
2 * Copyright (c) 2010 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 // VP8 Set Reference Frame
12 // =======================
13 //
14 // This is an example demonstrating how to overwrite the VP8 encoder's
15 // internal reference frame. In the sample we set the last frame to the
16 // current frame. If this is done at a cut scene it will avoid a keyframe.
17 // This technique could be used to bounce between two cameras.
18 //
19 // Note that the decoder would also have to set the reference frame to the
20 // same value on the same frame, or the video will become corrupt.
21 //
22 // Usage
23 // -----
24 // This example adds a single argument to the `simple_encoder` example,
25 // which specifies the frame number to update the reference frame on.
26 // The parameter is parsed as follows:
27 //
28 //
29 // Extra Variables
30 // ---------------
31 // This example maintains the frame number passed on the command line
32 // in the `update_frame_num` variable.
33 //
34 //
35 // Configuration
36 // -------------
37 //
38 // The reference frame is updated on the frame specified on the command
39 // line.
40 //
41 // Observing The Effects
42 // ---------------------
43 // Use the `simple_encoder` example to encode a sample with a cut scene.
44 // Determine the frame number of the cut scene by looking for a generated
45 // key-frame (indicated by a 'K'). Supply that frame number as an argument
46 // to this example, and observe that no key-frame is generated.
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "vpx/vp8cx.h"
53 #include "vpx/vpx_encoder.h"
54 #include "vp8/common/common.h"
55
56 #include "../tools_common.h"
57 #include "../video_writer.h"
58
59 static const char *exec_name;
60
usage_exit(void)61 void usage_exit(void) {
62 fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
63 exec_name);
64 exit(EXIT_FAILURE);
65 }
66
encode_frame(vpx_codec_ctx_t * codec,vpx_image_t * img,int frame_index,VpxVideoWriter * writer)67 static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
68 int frame_index, VpxVideoWriter *writer) {
69 int got_pkts = 0;
70 vpx_codec_iter_t iter = NULL;
71 const vpx_codec_cx_pkt_t *pkt = NULL;
72 const vpx_codec_err_t res =
73 vpx_codec_encode(codec, img, frame_index, 1, 0, VPX_DL_GOOD_QUALITY);
74 if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
75
76 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
77 got_pkts = 1;
78
79 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
80 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
81 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
82 pkt->data.frame.sz,
83 pkt->data.frame.pts)) {
84 die_codec(codec, "Failed to write compressed frame");
85 }
86
87 printf(keyframe ? "K" : ".");
88 fflush(stdout);
89 }
90 }
91
92 return got_pkts;
93 }
94
main(int argc,char ** argv)95 int main(int argc, char **argv) {
96 FILE *infile = NULL;
97 vpx_codec_ctx_t codec;
98 vpx_codec_enc_cfg_t cfg;
99 int frame_count = 0;
100 vpx_image_t raw;
101 vpx_codec_err_t res;
102 VpxVideoInfo info;
103 VpxVideoWriter *writer = NULL;
104 const VpxInterface *encoder = NULL;
105 int update_frame_num = 0;
106 const int fps = 30; // TODO(dkovalev) add command line argument
107 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
108
109 vp8_zero(codec);
110 vp8_zero(cfg);
111 vp8_zero(info);
112
113 exec_name = argv[0];
114
115 if (argc != 6) die("Invalid number of arguments");
116
117 // TODO(dkovalev): add vp9 support and rename the file accordingly
118 encoder = get_vpx_encoder_by_name("vp8");
119 if (!encoder) die("Unsupported codec.");
120
121 update_frame_num = atoi(argv[5]);
122 if (!update_frame_num) die("Couldn't parse frame number '%s'\n", argv[5]);
123
124 info.codec_fourcc = encoder->fourcc;
125 info.frame_width = (int)strtol(argv[1], NULL, 0);
126 info.frame_height = (int)strtol(argv[2], NULL, 0);
127 info.time_base.numerator = 1;
128 info.time_base.denominator = fps;
129
130 if (info.frame_width <= 0 || info.frame_height <= 0 ||
131 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
132 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
133 }
134
135 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
136 info.frame_height, 1)) {
137 die("Failed to allocate image.");
138 }
139
140 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
141
142 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
143 if (res) die_codec(&codec, "Failed to get default codec config.");
144
145 cfg.g_w = info.frame_width;
146 cfg.g_h = info.frame_height;
147 cfg.g_timebase.num = info.time_base.numerator;
148 cfg.g_timebase.den = info.time_base.denominator;
149 cfg.rc_target_bitrate = bitrate;
150
151 writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
152 if (!writer) die("Failed to open %s for writing.", argv[4]);
153
154 if (!(infile = fopen(argv[3], "rb")))
155 die("Failed to open %s for reading.", argv[3]);
156
157 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
158 die("Failed to initialize encoder");
159
160 // Encode frames.
161 while (vpx_img_read(&raw, infile)) {
162 if (frame_count + 1 == update_frame_num) {
163 vpx_ref_frame_t ref;
164 ref.frame_type = VP8_LAST_FRAME;
165 ref.img = raw;
166 if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
167 die_codec(&codec, "Failed to set reference frame");
168 }
169
170 encode_frame(&codec, &raw, frame_count++, writer);
171 }
172
173 // Flush encoder.
174 while (encode_frame(&codec, NULL, -1, writer)) {
175 }
176
177 printf("\n");
178 fclose(infile);
179 printf("Processed %d frames.\n", frame_count);
180
181 vpx_img_free(&raw);
182 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
183
184 vpx_video_writer_close(writer);
185
186 return EXIT_SUCCESS;
187 }
188