Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: source/libvpx/examples/vp8cx_set_ref.c

Issue 232133009: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 30 matching lines...) Expand all
41 // 41 //
42 // Observing The Effects 42 // Observing The Effects
43 // --------------------- 43 // ---------------------
44 // Use the `simple_encoder` example to encode a sample with a cut scene. 44 // Use the `simple_encoder` example to encode a sample with a cut scene.
45 // Determine the frame number of the cut scene by looking for a generated 45 // Determine the frame number of the cut scene by looking for a generated
46 // key-frame (indicated by a 'K'). Supply that frame number as an argument 46 // key-frame (indicated by a 'K'). Supply that frame number as an argument
47 // to this example, and observe that no key-frame is generated. 47 // to this example, and observe that no key-frame is generated.
48 48
49 #include <stdio.h> 49 #include <stdio.h>
50 #include <stdlib.h> 50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <string.h> 51 #include <string.h>
52
53 #define VPX_CODEC_DISABLE_COMPAT 1 53 #define VPX_CODEC_DISABLE_COMPAT 1
54 #include "vpx/vp8cx.h"
54 #include "vpx/vpx_encoder.h" 55 #include "vpx/vpx_encoder.h"
55 #include "vpx/vp8cx.h"
56 #define interface (vpx_codec_vp8_cx())
57 #define fourcc 0x30385056
58 56
59 #define IVF_FILE_HDR_SZ (32) 57 #include "./tools_common.h"
60 #define IVF_FRAME_HDR_SZ (12) 58 #include "./video_writer.h"
61 59
62 static void mem_put_le16(char *mem, unsigned int val) { 60 static const char *exec_name;
63 mem[0] = val; 61
64 mem[1] = val>>8; 62 void usage_exit() {
63 fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
64 exec_name);
65 exit(EXIT_FAILURE);
65 } 66 }
66 67
67 static void mem_put_le32(char *mem, unsigned int val) { 68 static void encode_frame(vpx_codec_ctx_t *codec,
68 mem[0] = val; 69 vpx_image_t *img,
69 mem[1] = val>>8; 70 int frame_index,
70 mem[2] = val>>16; 71 VpxVideoWriter *writer) {
71 mem[3] = val>>24; 72 vpx_codec_iter_t iter = NULL;
72 } 73 const vpx_codec_cx_pkt_t *pkt = NULL;
74 const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0,
75 VPX_DL_GOOD_QUALITY);
76 if (res != VPX_CODEC_OK)
77 die_codec(codec, "Failed to encode frame");
73 78
74 static void die(const char *fmt, ...) { 79 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
75 va_list ap; 80 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
81 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
82 if (!vpx_video_writer_write_frame(writer,
83 pkt->data.frame.buf,
84 pkt->data.frame.sz,
85 pkt->data.frame.pts)) {
86 die_codec(codec, "Failed to write compressed frame");
87 }
76 88
77 va_start(ap, fmt); 89 printf(keyframe ? "K" : ".");
78 vprintf(fmt, ap); 90 fflush(stdout);
79 if(fmt[strlen(fmt)-1] != '\n')
80 printf("\n");
81 exit(EXIT_FAILURE);
82 }
83
84 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
85 const char *detail = vpx_codec_error_detail(ctx);
86
87 printf("%s: %s\n", s, vpx_codec_error(ctx));
88 if(detail)
89 printf(" %s\n",detail);
90 exit(EXIT_FAILURE);
91 }
92
93 static int read_frame(FILE *f, vpx_image_t *img) {
94 size_t nbytes, to_read;
95 int res = 1;
96
97 to_read = img->w*img->h*3/2;
98 nbytes = fread(img->planes[0], 1, to_read, f);
99 if(nbytes != to_read) {
100 res = 0;
101 if(nbytes > 0)
102 printf("Warning: Read partial frame. Check your width & height!\n");
103 } 91 }
104 return res; 92 }
105 }
106
107 static void write_ivf_file_header(FILE *outfile,
108 const vpx_codec_enc_cfg_t *cfg,
109 int frame_cnt) {
110 char header[32];
111
112 if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
113 return;
114 header[0] = 'D';
115 header[1] = 'K';
116 header[2] = 'I';
117 header[3] = 'F';
118 mem_put_le16(header+4, 0); /* version */
119 mem_put_le16(header+6, 32); /* headersize */
120 mem_put_le32(header+8, fourcc); /* headersize */
121 mem_put_le16(header+12, cfg->g_w); /* width */
122 mem_put_le16(header+14, cfg->g_h); /* height */
123 mem_put_le32(header+16, cfg->g_timebase.den); /* rate */
124 mem_put_le32(header+20, cfg->g_timebase.num); /* scale */
125 mem_put_le32(header+24, frame_cnt); /* length */
126 mem_put_le32(header+28, 0); /* unused */
127
128 (void) fwrite(header, 1, 32, outfile);
129 }
130
131
132 static void write_ivf_frame_header(FILE *outfile,
133 const vpx_codec_cx_pkt_t *pkt)
134 {
135 char header[12];
136 vpx_codec_pts_t pts;
137
138 if(pkt->kind != VPX_CODEC_CX_FRAME_PKT)
139 return;
140
141 pts = pkt->data.frame.pts;
142 mem_put_le32(header, (unsigned int)pkt->data.frame.sz);
143 mem_put_le32(header+4, pts&0xFFFFFFFF);
144 mem_put_le32(header+8, pts >> 32);
145
146 (void) fwrite(header, 1, 12, outfile);
147 } 93 }
148 94
149 int main(int argc, char **argv) { 95 int main(int argc, char **argv) {
150 FILE *infile, *outfile; 96 FILE *infile = NULL;
151 vpx_codec_ctx_t codec; 97 vpx_codec_ctx_t codec = {0};
152 vpx_codec_enc_cfg_t cfg; 98 vpx_codec_enc_cfg_t cfg = {0};
153 int frame_cnt = 0; 99 int frame_count = 0;
154 vpx_image_t raw; 100 vpx_image_t raw;
155 vpx_codec_err_t res; 101 vpx_codec_err_t res;
156 long width; 102 VpxVideoInfo info = {0};
157 long height; 103 VpxVideoWriter *writer = NULL;
158 int frame_avail; 104 const VpxInterface *encoder = NULL;
159 int got_data; 105 int update_frame_num = 0;
160 int flags = 0; 106 const int fps = 30; // TODO(dkovalev) add command line argument
161 int update_frame_num = 0; 107 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
162 108
163 /* Open files */ 109 exec_name = argv[0];
164 if(argc!=6)
165 die("Usage: %s <width> <height> <infile> <outfile> <frame>\n",
166 argv[0]);
167 110
168 update_frame_num = atoi(argv[5]); 111 if (argc != 6)
169 if(!update_frame_num) 112 die("Invalid number of arguments");
170 die("Couldn't parse frame number '%s'\n", argv[5]);
171 113
172 width = strtol(argv[1], NULL, 0); 114 // TODO(dkovalev): add vp9 support and rename the file accordingly
173 height = strtol(argv[2], NULL, 0); 115 encoder = get_vpx_encoder_by_name("vp8");
174 if(width < 16 || width%2 || height <16 || height%2) 116 if (!encoder)
175 die("Invalid resolution: %ldx%ld", width, height); 117 die("Unsupported codec.");
176 if(!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 1))
177 die("Faile to allocate image", width, height);
178 if(!(outfile = fopen(argv[4], "wb")))
179 die("Failed to open %s for writing", argv[4]);
180 118
181 printf("Using %s\n",vpx_codec_iface_name(interface)); 119 update_frame_num = atoi(argv[5]);
120 if (!update_frame_num)
121 die("Couldn't parse frame number '%s'\n", argv[5]);
182 122
183 /* Populate encoder configuration */ 123 info.codec_fourcc = encoder->fourcc;
184 res = vpx_codec_enc_config_default(interface, &cfg, 0); 124 info.frame_width = strtol(argv[1], NULL, 0);
185 if(res) { 125 info.frame_height = strtol(argv[2], NULL, 0);
186 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res)); 126 info.time_base.numerator = 1;
187 return EXIT_FAILURE; 127 info.time_base.denominator = fps;
128
129 if (info.frame_width <= 0 ||
130 info.frame_height <= 0 ||
131 (info.frame_width % 2) != 0 ||
132 (info.frame_height % 2) != 0) {
133 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
134 }
135
136 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
137 info.frame_height, 1)) {
138 die("Failed to allocate image.");
139 }
140
141 printf("Using %s\n", vpx_codec_iface_name(encoder->interface()));
142
143 res = vpx_codec_enc_config_default(encoder->interface(), &cfg, 0);
144 if (res)
145 die_codec(&codec, "Failed to get default codec config.");
146
147 cfg.g_w = info.frame_width;
148 cfg.g_h = info.frame_height;
149 cfg.g_timebase.num = info.time_base.numerator;
150 cfg.g_timebase.den = info.time_base.denominator;
151 cfg.rc_target_bitrate = bitrate;
152
153 writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
154 if (!writer)
155 die("Failed to open %s for writing.", argv[4]);
156
157 if (!(infile = fopen(argv[3], "rb")))
158 die("Failed to open %s for reading.", argv[3]);
159
160 if (vpx_codec_enc_init(&codec, encoder->interface(), &cfg, 0))
161 die_codec(&codec, "Failed to initialize encoder");
162
163 while (vpx_img_read(&raw, infile)) {
164 if (frame_count + 1 == update_frame_num) {
165 vpx_ref_frame_t ref;
166 ref.frame_type = VP8_LAST_FRAME;
167 ref.img = raw;
168 if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
169 die_codec(&codec, "Failed to set reference frame");
188 } 170 }
189 171
190 /* Update the default configuration with our settings */ 172 encode_frame(&codec, &raw, frame_count++, writer);
191 cfg.rc_target_bitrate = width * height * cfg.rc_target_bitrate 173 }
192 / cfg.g_w / cfg.g_h; 174 encode_frame(&codec, NULL, -1, writer);
193 cfg.g_w = width;
194 cfg.g_h = height;
195 175
196 write_ivf_file_header(outfile, &cfg, 0); 176 printf("\n");
177 fclose(infile);
178 printf("Processed %d frames.\n", frame_count);
197 179
180 vpx_img_free(&raw);
181 if (vpx_codec_destroy(&codec))
182 die_codec(&codec, "Failed to destroy codec.");
198 183
199 /* Open input file for this encoding pass */ 184 vpx_video_writer_close(writer);
200 if(!(infile = fopen(argv[3], "rb")))
201 die("Failed to open %s for reading", argv[3]);
202 185
203 /* Initialize codec */ 186 return EXIT_SUCCESS;
204 if(vpx_codec_enc_init(&codec, interface, &cfg, 0))
205 die_codec(&codec, "Failed to initialize encoder");
206
207 frame_avail = 1;
208 got_data = 0;
209 while(frame_avail || got_data) {
210 vpx_codec_iter_t iter = NULL;
211 const vpx_codec_cx_pkt_t *pkt;
212
213 frame_avail = read_frame(infile, &raw);
214
215 if(frame_cnt + 1 == update_frame_num) {
216 vpx_ref_frame_t ref;
217
218 ref.frame_type = VP8_LAST_FRAME;
219 ref.img = raw;
220
221 if(vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
222 die_codec(&codec, "Failed to set reference frame");
223 }
224
225 if(vpx_codec_encode(&codec, frame_avail? &raw : NULL, frame_cnt,
226 1, flags, VPX_DL_REALTIME))
227 die_codec(&codec, "Failed to encode frame");
228 got_data = 0;
229 while( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
230 got_data = 1;
231 switch(pkt->kind) {
232 case VPX_CODEC_CX_FRAME_PKT:
233 write_ivf_frame_header(outfile, pkt);
234 (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
235 outfile);
236 break;
237 default:
238 break;
239 }
240 printf(pkt->kind == VPX_CODEC_CX_FRAME_PKT
241 && (pkt->data.frame.flags & VPX_FRAME_IS_KEY)? "K":".");
242 fflush(stdout);
243 }
244 frame_cnt++;
245 }
246 printf("\n");
247 fclose(infile);
248
249 printf("Processed %d frames.\n",frame_cnt-1);
250 vpx_img_free(&raw);
251 if(vpx_codec_destroy(&codec))
252 die_codec(&codec, "Failed to destroy codec");
253
254 /* Try to rewrite the file header with the actual frame count */
255 if(!fseek(outfile, 0, SEEK_SET))
256 write_ivf_file_header(outfile, &cfg, frame_cnt-1);
257 fclose(outfile);
258 return EXIT_SUCCESS;
259 } 187 }
OLDNEW
« no previous file with comments | « source/libvpx/examples/vp8_set_maps.c ('k') | source/libvpx/examples/vp9_spatial_scalable_encoder.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698