| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 vpx_active_map_t map = {0, 0, 0}; | 118 vpx_active_map_t map = {0, 0, 0}; |
| 119 | 119 |
| 120 map.rows = (cfg->g_h + 15) / 16; | 120 map.rows = (cfg->g_h + 15) / 16; |
| 121 map.cols = (cfg->g_w + 15) / 16; | 121 map.cols = (cfg->g_w + 15) / 16; |
| 122 map.active_map = NULL; | 122 map.active_map = NULL; |
| 123 | 123 |
| 124 if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map)) | 124 if (vpx_codec_control(codec, VP8E_SET_ACTIVEMAP, &map)) |
| 125 die_codec(codec, "Failed to set active map"); | 125 die_codec(codec, "Failed to set active map"); |
| 126 } | 126 } |
| 127 | 127 |
| 128 static void encode_frame(vpx_codec_ctx_t *codec, | 128 static int encode_frame(vpx_codec_ctx_t *codec, |
| 129 vpx_image_t *img, | 129 vpx_image_t *img, |
| 130 int frame_index, | 130 int frame_index, |
| 131 VpxVideoWriter *writer) { | 131 VpxVideoWriter *writer) { |
| 132 int got_pkts = 0; |
| 132 vpx_codec_iter_t iter = NULL; | 133 vpx_codec_iter_t iter = NULL; |
| 133 const vpx_codec_cx_pkt_t *pkt = NULL; | 134 const vpx_codec_cx_pkt_t *pkt = NULL; |
| 134 const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0, | 135 const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0, |
| 135 VPX_DL_GOOD_QUALITY); | 136 VPX_DL_GOOD_QUALITY); |
| 136 if (res != VPX_CODEC_OK) | 137 if (res != VPX_CODEC_OK) |
| 137 die_codec(codec, "Failed to encode frame"); | 138 die_codec(codec, "Failed to encode frame"); |
| 138 | 139 |
| 139 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { | 140 while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { |
| 141 got_pkts = 1; |
| 142 |
| 140 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { | 143 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { |
| 141 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; | 144 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; |
| 142 if (!vpx_video_writer_write_frame(writer, | 145 if (!vpx_video_writer_write_frame(writer, |
| 143 pkt->data.frame.buf, | 146 pkt->data.frame.buf, |
| 144 pkt->data.frame.sz, | 147 pkt->data.frame.sz, |
| 145 pkt->data.frame.pts)) { | 148 pkt->data.frame.pts)) { |
| 146 die_codec(codec, "Failed to write compressed frame"); | 149 die_codec(codec, "Failed to write compressed frame"); |
| 147 } | 150 } |
| 148 | 151 |
| 149 printf(keyframe ? "K" : "."); | 152 printf(keyframe ? "K" : "."); |
| 150 fflush(stdout); | 153 fflush(stdout); |
| 151 } | 154 } |
| 152 } | 155 } |
| 156 |
| 157 return got_pkts; |
| 153 } | 158 } |
| 154 | 159 |
| 155 int main(int argc, char **argv) { | 160 int main(int argc, char **argv) { |
| 156 FILE *infile = NULL; | 161 FILE *infile = NULL; |
| 157 vpx_codec_ctx_t codec; | 162 vpx_codec_ctx_t codec; |
| 158 vpx_codec_enc_cfg_t cfg; | 163 vpx_codec_enc_cfg_t cfg; |
| 159 int frame_count = 0; | 164 int frame_count = 0; |
| 160 vpx_image_t raw; | 165 vpx_image_t raw; |
| 161 vpx_codec_err_t res; | 166 vpx_codec_err_t res; |
| 162 VpxVideoInfo info; | 167 VpxVideoInfo info; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 writer = vpx_video_writer_open(argv[5], kContainerIVF, &info); | 215 writer = vpx_video_writer_open(argv[5], kContainerIVF, &info); |
| 211 if (!writer) | 216 if (!writer) |
| 212 die("Failed to open %s for writing.", argv[5]); | 217 die("Failed to open %s for writing.", argv[5]); |
| 213 | 218 |
| 214 if (!(infile = fopen(argv[4], "rb"))) | 219 if (!(infile = fopen(argv[4], "rb"))) |
| 215 die("Failed to open %s for reading.", argv[4]); | 220 die("Failed to open %s for reading.", argv[4]); |
| 216 | 221 |
| 217 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0)) | 222 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0)) |
| 218 die_codec(&codec, "Failed to initialize encoder"); | 223 die_codec(&codec, "Failed to initialize encoder"); |
| 219 | 224 |
| 225 // Encode frames. |
| 220 while (vpx_img_read(&raw, infile)) { | 226 while (vpx_img_read(&raw, infile)) { |
| 221 ++frame_count; | 227 ++frame_count; |
| 222 | 228 |
| 223 if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) { | 229 if (frame_count == 22 && encoder->fourcc == VP8_FOURCC) { |
| 224 set_roi_map(&cfg, &codec); | 230 set_roi_map(&cfg, &codec); |
| 225 } else if (frame_count == 33) { | 231 } else if (frame_count == 33) { |
| 226 set_active_map(&cfg, &codec); | 232 set_active_map(&cfg, &codec); |
| 227 } else if (frame_count == 44) { | 233 } else if (frame_count == 44) { |
| 228 unset_active_map(&cfg, &codec); | 234 unset_active_map(&cfg, &codec); |
| 229 } | 235 } |
| 230 | 236 |
| 231 encode_frame(&codec, &raw, frame_count, writer); | 237 encode_frame(&codec, &raw, frame_count, writer); |
| 232 } | 238 } |
| 233 encode_frame(&codec, NULL, -1, writer); | 239 |
| 240 // Flush encoder. |
| 241 while (encode_frame(&codec, NULL, -1, writer)) {} |
| 242 |
| 234 printf("\n"); | 243 printf("\n"); |
| 235 fclose(infile); | 244 fclose(infile); |
| 236 printf("Processed %d frames.\n", frame_count); | 245 printf("Processed %d frames.\n", frame_count); |
| 237 | 246 |
| 238 vpx_img_free(&raw); | 247 vpx_img_free(&raw); |
| 239 if (vpx_codec_destroy(&codec)) | 248 if (vpx_codec_destroy(&codec)) |
| 240 die_codec(&codec, "Failed to destroy codec."); | 249 die_codec(&codec, "Failed to destroy codec."); |
| 241 | 250 |
| 242 vpx_video_writer_close(writer); | 251 vpx_video_writer_close(writer); |
| 243 | 252 |
| 244 return EXIT_SUCCESS; | 253 return EXIT_SUCCESS; |
| 245 } | 254 } |
| OLD | NEW |