OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // | 76 // |
77 // Error Handling | 77 // Error Handling |
78 // -------------- | 78 // -------------- |
79 // This example does not special case any error return codes. If there was | 79 // This example does not special case any error return codes. If there was |
80 // an error, a descriptive message is printed and the program exits. With | 80 // an error, a descriptive message is printed and the program exits. With |
81 // few exeptions, vpx_codec functions return an enumerated error status, | 81 // few exeptions, vpx_codec functions return an enumerated error status, |
82 // with the value `0` indicating success. | 82 // with the value `0` indicating success. |
83 | 83 |
84 #include <stdio.h> | 84 #include <stdio.h> |
85 #include <stdlib.h> | 85 #include <stdlib.h> |
86 #include <stdarg.h> | |
87 #include <string.h> | 86 #include <string.h> |
| 87 |
88 #define VPX_CODEC_DISABLE_COMPAT 1 | 88 #define VPX_CODEC_DISABLE_COMPAT 1 |
89 #include "vpx/vpx_encoder.h" | 89 #include "vpx/vpx_encoder.h" |
90 #include "vpx/vp8cx.h" | |
91 #define interface (vpx_codec_vp8_cx()) | |
92 #define fourcc 0x30385056 | |
93 | 90 |
94 #define IVF_FILE_HDR_SZ (32) | 91 #include "./tools_common.h" |
95 #define IVF_FRAME_HDR_SZ (12) | 92 #include "./video_writer.h" |
96 | 93 |
97 static void mem_put_le16(char *mem, unsigned int val) { | 94 static const char *exec_name; |
98 mem[0] = val; | 95 |
99 mem[1] = val>>8; | 96 void usage_exit() { |
| 97 fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n", |
| 98 exec_name); |
| 99 exit(EXIT_FAILURE); |
100 } | 100 } |
101 | 101 |
102 static void mem_put_le32(char *mem, unsigned int val) { | 102 static void encode_frame(vpx_codec_ctx_t *codec, |
103 mem[0] = val; | 103 vpx_image_t *img, |
104 mem[1] = val>>8; | 104 int frame_index, |
105 mem[2] = val>>16; | 105 VpxVideoWriter *writer) { |
106 mem[3] = val>>24; | 106 vpx_codec_iter_t iter = NULL; |
107 } | 107 const vpx_codec_cx_pkt_t *pkt = NULL; |
| 108 const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0, |
| 109 VPX_DL_GOOD_QUALITY); |
| 110 if (res != VPX_CODEC_OK) |
| 111 die_codec(codec, "Failed to encode frame"); |
108 | 112 |
109 static void die(const char *fmt, ...) { | 113 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { |
110 va_list ap; | 114 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { |
| 115 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; |
| 116 if (!vpx_video_writer_write_frame(writer, |
| 117 pkt->data.frame.buf, |
| 118 pkt->data.frame.sz, |
| 119 pkt->data.frame.pts)) { |
| 120 die_codec(codec, "Failed to write compressed frame"); |
| 121 } |
111 | 122 |
112 va_start(ap, fmt); | 123 printf(keyframe ? "K" : "."); |
113 vprintf(fmt, ap); | 124 fflush(stdout); |
114 if(fmt[strlen(fmt)-1] != '\n') | |
115 printf("\n"); | |
116 exit(EXIT_FAILURE); | |
117 } | |
118 | |
119 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { | |
120 const char *detail = vpx_codec_error_detail(ctx); | |
121 | |
122 printf("%s: %s\n", s, vpx_codec_error(ctx)); | |
123 if(detail) | |
124 printf(" %s\n",detail); | |
125 exit(EXIT_FAILURE); | |
126 } | |
127 | |
128 static int read_frame(FILE *f, vpx_image_t *img) { | |
129 size_t nbytes, to_read; | |
130 int res = 1; | |
131 | |
132 to_read = img->w*img->h*3/2; | |
133 nbytes = fread(img->planes[0], 1, to_read, f); | |
134 if(nbytes != to_read) { | |
135 res = 0; | |
136 if(nbytes > 0) | |
137 printf("Warning: Read partial frame. Check your width & height!\n"); | |
138 } | 125 } |
139 return res; | 126 } |
140 } | |
141 | |
142 static void write_ivf_file_header(FILE *outfile, | |
143 const vpx_codec_enc_cfg_t *cfg, | |
144 int frame_cnt) { | |
145 char header[32]; | |
146 | |
147 if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS) | |
148 return; | |
149 header[0] = 'D'; | |
150 header[1] = 'K'; | |
151 header[2] = 'I'; | |
152 header[3] = 'F'; | |
153 mem_put_le16(header+4, 0); /* version */ | |
154 mem_put_le16(header+6, 32); /* headersize */ | |
155 mem_put_le32(header+8, fourcc); /* headersize */ | |
156 mem_put_le16(header+12, cfg->g_w); /* width */ | |
157 mem_put_le16(header+14, cfg->g_h); /* height */ | |
158 mem_put_le32(header+16, cfg->g_timebase.den); /* rate */ | |
159 mem_put_le32(header+20, cfg->g_timebase.num); /* scale */ | |
160 mem_put_le32(header+24, frame_cnt); /* length */ | |
161 mem_put_le32(header+28, 0); /* unused */ | |
162 | |
163 (void) fwrite(header, 1, 32, outfile); | |
164 } | |
165 | |
166 | |
167 static void write_ivf_frame_header(FILE *outfile, | |
168 const vpx_codec_cx_pkt_t *pkt) | |
169 { | |
170 char header[12]; | |
171 vpx_codec_pts_t pts; | |
172 | |
173 if(pkt->kind != VPX_CODEC_CX_FRAME_PKT) | |
174 return; | |
175 | |
176 pts = pkt->data.frame.pts; | |
177 mem_put_le32(header, pkt->data.frame.sz); | |
178 mem_put_le32(header+4, pts&0xFFFFFFFF); | |
179 mem_put_le32(header+8, pts >> 32); | |
180 | |
181 (void) fwrite(header, 1, 12, outfile); | |
182 } | 127 } |
183 | 128 |
184 int main(int argc, char **argv) { | 129 int main(int argc, char **argv) { |
185 FILE *infile, *outfile; | 130 FILE *infile = NULL; |
186 vpx_codec_ctx_t codec; | 131 vpx_codec_ctx_t codec; |
187 vpx_codec_enc_cfg_t cfg; | 132 vpx_codec_enc_cfg_t cfg; |
188 int frame_cnt = 0; | 133 int frame_count = 0; |
189 vpx_image_t raw; | 134 vpx_image_t raw; |
190 vpx_codec_err_t res; | 135 vpx_codec_err_t res; |
191 long width; | 136 VpxVideoInfo info = {0}; |
192 long height; | 137 VpxVideoWriter *writer = NULL; |
193 int frame_avail; | 138 const VpxInterface *encoder = NULL; |
194 int got_data; | 139 const int fps = 30; // TODO(dkovalev) add command line argument |
195 int flags = 0; | 140 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument |
| 141 const char *const codec_arg = argv[1]; |
| 142 const char *const width_arg = argv[2]; |
| 143 const char *const height_arg = argv[3]; |
| 144 const char *const infile_arg = argv[4]; |
| 145 const char *const outfile_arg = argv[5]; |
196 | 146 |
197 /* Open files */ | 147 exec_name = argv[0]; |
198 if(argc!=5) | |
199 die("Usage: %s <width> <height> <infile> <outfile>\n", argv[0]); | |
200 width = strtol(argv[1], NULL, 0); | |
201 height = strtol(argv[2], NULL, 0); | |
202 if(width < 16 || width%2 || height <16 || height%2) | |
203 die("Invalid resolution: %ldx%ld", width, height); | |
204 if(!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 1)) | |
205 die("Faile to allocate image", width, height); | |
206 if(!(outfile = fopen(argv[4], "wb"))) | |
207 die("Failed to open %s for writing", argv[4]); | |
208 | 148 |
209 printf("Using %s\n",vpx_codec_iface_name(interface)); | 149 if (argc != 6) |
| 150 die("Invalid number of arguments"); |
210 | 151 |
211 /* Populate encoder configuration */ | 152 encoder = get_vpx_encoder_by_name(codec_arg); |
212 res = vpx_codec_enc_config_default(interface, &cfg, 0); | 153 if (!encoder) |
213 if(res) { | 154 die("Unsupported codec."); |
214 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res)); | |
215 return EXIT_FAILURE; | |
216 } | |
217 | 155 |
218 /* Update the default configuration with our settings */ | 156 info.codec_fourcc = encoder->fourcc; |
219 cfg.rc_target_bitrate = width * height * cfg.rc_target_bitrate | 157 info.frame_width = strtol(width_arg, NULL, 0); |
220 / cfg.g_w / cfg.g_h; | 158 info.frame_height = strtol(height_arg, NULL, 0); |
221 cfg.g_w = width; | 159 info.time_base.numerator = 1; |
222 cfg.g_h = height; | 160 info.time_base.denominator = fps; |
223 | 161 |
224 write_ivf_file_header(outfile, &cfg, 0); | 162 if (info.frame_width <= 0 || |
| 163 info.frame_height <= 0 || |
| 164 (info.frame_width % 2) != 0 || |
| 165 (info.frame_height % 2) != 0) { |
| 166 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); |
| 167 } |
225 | 168 |
| 169 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width, |
| 170 info.frame_height, 1)) { |
| 171 die("Failed to allocate image."); |
| 172 } |
226 | 173 |
227 /* Open input file for this encoding pass */ | 174 printf("Using %s\n", vpx_codec_iface_name(encoder->interface())); |
228 if(!(infile = fopen(argv[3], "rb"))) | |
229 die("Failed to open %s for reading", argv[3]); | |
230 | 175 |
231 /* Initialize codec */ | 176 res = vpx_codec_enc_config_default(encoder->interface(), &cfg, 0); |
232 if(vpx_codec_enc_init(&codec, interface, &cfg, 0)) | 177 if (res) |
233 die_codec(&codec, "Failed to initialize encoder"); | 178 die_codec(&codec, "Failed to get default codec config."); |
234 | 179 |
235 frame_avail = 1; | 180 cfg.g_w = info.frame_width; |
236 got_data = 0; | 181 cfg.g_h = info.frame_height; |
237 while(frame_avail || got_data) { | 182 cfg.g_timebase.num = info.time_base.numerator; |
238 vpx_codec_iter_t iter = NULL; | 183 cfg.g_timebase.den = info.time_base.denominator; |
239 const vpx_codec_cx_pkt_t *pkt; | 184 cfg.rc_target_bitrate = bitrate; |
240 | 185 |
241 frame_avail = read_frame(infile, &raw); | 186 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info); |
242 if(vpx_codec_encode(&codec, frame_avail? &raw : NULL, frame_cnt, | 187 if (!writer) |
243 1, flags, VPX_DL_REALTIME)) | 188 die("Failed to open %s for writing.", outfile_arg); |
244 die_codec(&codec, "Failed to encode frame"); | |
245 got_data = 0; | |
246 while( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) { | |
247 got_data = 1; | |
248 switch(pkt->kind) { | |
249 case VPX_CODEC_CX_FRAME_PKT: | |
250 write_ivf_frame_header(outfile, pkt); | |
251 (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, | |
252 outfile); | |
253 break; | |
254 default: | |
255 break; | |
256 } | |
257 printf(pkt->kind == VPX_CODEC_CX_FRAME_PKT | |
258 && (pkt->data.frame.flags & VPX_FRAME_IS_KEY)? "K":"."); | |
259 fflush(stdout); | |
260 } | |
261 frame_cnt++; | |
262 } | |
263 printf("\n"); | |
264 fclose(infile); | |
265 | 189 |
266 printf("Processed %d frames.\n",frame_cnt-1); | 190 if (!(infile = fopen(infile_arg, "rb"))) |
267 vpx_img_free(&raw); | 191 die("Failed to open %s for reading.", infile_arg); |
268 if(vpx_codec_destroy(&codec)) | |
269 die_codec(&codec, "Failed to destroy codec"); | |
270 | 192 |
271 /* Try to rewrite the file header with the actual frame count */ | 193 if (vpx_codec_enc_init(&codec, encoder->interface(), &cfg, 0)) |
272 if(!fseek(outfile, 0, SEEK_SET)) | 194 die_codec(&codec, "Failed to initialize encoder"); |
273 write_ivf_file_header(outfile, &cfg, frame_cnt-1); | 195 |
274 fclose(outfile); | 196 while (vpx_img_read(&raw, infile)) |
275 return EXIT_SUCCESS; | 197 encode_frame(&codec, &raw, frame_count++, writer); |
| 198 encode_frame(&codec, NULL, -1, writer); // flush the encoder |
| 199 |
| 200 printf("\n"); |
| 201 fclose(infile); |
| 202 printf("Processed %d frames.\n", frame_count); |
| 203 |
| 204 vpx_img_free(&raw); |
| 205 if (vpx_codec_destroy(&codec)) |
| 206 die_codec(&codec, "Failed to destroy codec."); |
| 207 |
| 208 vpx_video_writer_close(writer); |
| 209 |
| 210 return EXIT_SUCCESS; |
276 } | 211 } |
OLD | NEW |