1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <assert.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48
51
52#include "../tools_common.h"
53#include "../video_writer.h"
54
55static const char *exec_name;
56
57void usage_exit(void) {
58 fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
59 exec_name);
60 exit(EXIT_FAILURE);
61}
62
65 unsigned int i;
67 memset(&roi, 0, sizeof(roi));
68
69 roi.
rows = (cfg->
g_h + 15) / 16;
70 roi.
cols = (cfg->
g_w + 15) / 16;
71
76
81
86
89
91 die_codec(codec, "Failed to set ROI map");
92
94}
95
98 unsigned int i;
100
101 map.
rows = (cfg->
g_h + 15) / 16;
102 map.
cols = (cfg->
g_w + 15) / 16;
103
106
108 die_codec(codec, "Failed to set active map");
109
111}
112
116
117 map.
rows = (cfg->
g_h + 15) / 16;
118 map.
cols = (cfg->
g_w + 15) / 16;
120
122 die_codec(codec, "Failed to set active map");
123}
124
126 int frame_index, VpxVideoWriter *writer) {
127 int got_pkts = 0;
132 if (res !=
VPX_CODEC_OK) die_codec(codec,
"Failed to encode frame");
133
135 got_pkts = 1;
136
139 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
142 die_codec(codec, "Failed to write compressed frame");
143 }
144
145 printf(keyframe ? "K" : ".");
146 fflush(stdout);
147 }
148 }
149
150 return got_pkts;
151}
152
153int main(int argc, char **argv) {
154 FILE *infile = NULL;
157 int frame_count = 0;
160 VpxVideoInfo info;
161 VpxVideoWriter *writer = NULL;
162 const VpxInterface *encoder = NULL;
163 const int fps = 2;
164 const double bits_per_pixel_per_frame = 0.067;
165
166 exec_name = argv[0];
167 if (argc != 6) die("Invalid number of arguments");
168
169 memset(&info, 0, sizeof(info));
170
171 encoder = get_vpx_encoder_by_name(argv[1]);
172 if (encoder == NULL) {
173 die("Unsupported codec.");
174 }
175 assert(encoder != NULL);
176 info.codec_fourcc = encoder->fourcc;
177 info.frame_width = (int)strtol(argv[2], NULL, 0);
178 info.frame_height = (int)strtol(argv[3], NULL, 0);
179 info.time_base.numerator = 1;
180 info.time_base.denominator = fps;
181
182 if (info.frame_width <= 0 || info.frame_height <= 0 ||
183 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
184 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
185 }
186
188 info.frame_height, 1)) {
189 die("Failed to allocate image.");
190 }
191
193
195 if (res) die_codec(&codec, "Failed to get default codec config.");
196
197 cfg.
g_w = info.frame_width;
198 cfg.
g_h = info.frame_height;
202 (
unsigned int)(bits_per_pixel_per_frame * cfg.
g_w * cfg.
g_h * fps / 1000);
204
205 writer = vpx_video_writer_open(argv[5], kContainerIVF, &info);
206 if (!writer) die("Failed to open %s for writing.", argv[5]);
207
208 if (!(infile = fopen(argv[4], "rb")))
209 die("Failed to open %s for reading.", argv[4]);
210
212 die("Failed to initialize encoder");
213
214
215 while (vpx_img_read(&raw, infile)) {
216 ++frame_count;
217
218 if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) {
219 set_roi_map(&cfg, &codec);
220 } else if (frame_count == 33) {
221 set_active_map(&cfg, &codec);
222 } else if (frame_count == 44) {
223 unset_active_map(&cfg, &codec);
224 }
225
226 encode_frame(&codec, &raw, frame_count, writer);
227 }
228
229
230 while (encode_frame(&codec, NULL, -1, writer)) {
231 }
232
233 printf("\n");
234 fclose(infile);
235 printf("Processed %d frames.\n", frame_count);
236
239
240 vpx_video_writer_close(writer);
241
242 return EXIT_SUCCESS;
243}
struct vpx_codec_ctx vpx_codec_ctx_t
Codec context structure.
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
const void * vpx_codec_iter_t
Iterator.
Definition vpx_codec.h:190
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition vpx_codec.h:408
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
@ VPX_CODEC_OK
Operation completed without error.
Definition vpx_codec.h:95
struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t
Encoder configuration structure.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition vpx_encoder.h:900
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:1006
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
#define VPX_FRAME_IS_KEY
Definition vpx_encoder.h:123
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
struct vpx_codec_cx_pkt vpx_codec_cx_pkt_t
Encoder output packet.
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, vpx_enc_deadline_t deadline)
Encode a frame.
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:155
struct vpx_active_map vpx_active_map_t
vpx active region map
struct vpx_roi_map vpx_roi_map_t
vpx region of interest map
@ VP8E_SET_ROI_MAP
Codec control function to pass an ROI map to encoder.
Definition vp8cx.h:147
@ VP8E_SET_ACTIVEMAP
Codec control function to pass an Active map to encoder.
Definition vp8cx.h:153
unsigned int rows
Definition vp8cx.h:852
unsigned int cols
Definition vp8cx.h:853
unsigned char * active_map
specify an on (1) or off (0) each 16x16 region within a frame
Definition vp8cx.h:851
union vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240 data
struct vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240::@337301343345304110063267327113124066016321050157 frame
vpx_codec_frame_flags_t flags
Definition vpx_encoder.h:177
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:168
size_t sz
Definition vpx_encoder.h:172
void * buf
Definition vpx_encoder.h:171
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:174
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:321
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:312
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:351
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition vpx_encoder.h:380
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:470
int den
Definition vpx_encoder.h:228
int num
Definition vpx_encoder.h:227
unsigned int static_threshold[4]
Definition vp8cx.h:840
unsigned int rows
Definition vp8cx.h:831
unsigned int cols
Definition vp8cx.h:832
int delta_q[8]
Definition vp8cx.h:834
unsigned char * roi_map
Definition vp8cx.h:830
int delta_lf[8]
Definition vp8cx.h:835
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
Describes the encoder algorithm interface to applications.
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ VPX_IMG_FMT_I420
Definition vpx_image.h:42
struct vpx_image vpx_image_t
Image Descriptor.
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.