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
45
46
47
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51
54#include "vp8/common/common.h"
55
56#include "../tools_common.h"
57#include "../video_writer.h"
58
59static const char *exec_name;
60
61void usage_exit(void) {
62 fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
63 exec_name);
64 exit(EXIT_FAILURE);
65}
66
68 int frame_index, VpxVideoWriter *writer) {
69 int got_pkts = 0;
74 if (res !=
VPX_CODEC_OK) die_codec(codec,
"Failed to encode frame");
75
77 got_pkts = 1;
78
81 if (!vpx_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
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
95int main(int argc, char **argv) {
96 FILE *infile = NULL;
99 int frame_count = 0;
102 VpxVideoInfo info;
103 VpxVideoWriter *writer = NULL;
104 const VpxInterface *encoder = NULL;
105 int update_frame_num = 0;
106 const int fps = 30;
107 const int bitrate = 200;
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
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
136 info.frame_height, 1)) {
137 die("Failed to allocate image.");
138 }
139
141
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;
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
158 die("Failed to initialize encoder");
159
160
161 while (vpx_img_read(&raw, infile)) {
162 if (frame_count + 1 == update_frame_num) {
167 die_codec(&codec, "Failed to set reference frame");
168 }
169
170 encode_frame(&codec, &raw, frame_count++, writer);
171 }
172
173
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
183
184 vpx_video_writer_close(writer);
185
186 return EXIT_SUCCESS;
187}
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:894
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:994
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:119
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, unsigned long deadline)
Encode a frame.
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:151
struct vpx_ref_frame vpx_ref_frame_t
reference frame data struct
@ VP8_SET_REFERENCE
pass in an external frame into decoder to be used as reference frame
Definition vp8.h:47
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:173
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:164
size_t sz
Definition vpx_encoder.h:168
void * buf
Definition vpx_encoder.h:167
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:170
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:317
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:308
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:347
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:464
int den
Definition vpx_encoder.h:224
int num
Definition vpx_encoder.h:223
vpx_ref_frame_type_t frame_type
Definition vp8.h:102
vpx_image_t img
Definition vp8.h:103
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.