Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | // VP8 Set Reference Frame |
| 13 | // ======================= |
| 14 | // |
| 15 | // This is an example demonstrating how to overwrite the VP8 encoder's |
| 16 | // internal reference frame. In the sample we set the last frame to the |
| 17 | // current frame. If this is done at a cut scene it will avoid a keyframe. |
| 18 | // This technique could be used to bounce between two cameras. |
| 19 | // |
| 20 | // Note that the decoder would also have to set the reference frame to the |
| 21 | // same value on the same frame, or the video will become corrupt. |
| 22 | // |
| 23 | // Usage |
| 24 | // ----- |
| 25 | // This example adds a single argument to the `simple_encoder` example, |
| 26 | // which specifies the frame number to update the reference frame on. |
| 27 | // The parameter is parsed as follows: |
| 28 | // |
| 29 | // |
| 30 | // Extra Variables |
| 31 | // --------------- |
| 32 | // This example maintains the frame number passed on the command line |
| 33 | // in the `update_frame_num` variable. |
| 34 | // |
| 35 | // |
| 36 | // Configuration |
| 37 | // ------------- |
| 38 | // |
| 39 | // The reference frame is updated on the frame specified on the command |
| 40 | // line. |
| 41 | // |
| 42 | // Observing The Effects |
| 43 | // --------------------- |
| 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 |
| 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. |
| 48 | |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 51 | #include <string.h> |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 52 | |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 53 | #include "vpx/vp8cx.h" |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 54 | #include "vpx/vpx_encoder.h" |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 55 | |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 56 | #include "../tools_common.h" |
| 57 | #include "../video_writer.h" |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 58 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 59 | static const char *exec_name; |
| 60 | |
James Zern | 59e7a47 | 2015-05-09 10:33:26 -0700 | [diff] [blame] | 61 | void usage_exit(void) { |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 62 | fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n", |
| 63 | exec_name); |
| 64 | exit(EXIT_FAILURE); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Dmitry Kovalev | 965af79 | 2014-08-14 10:28:00 -0700 | [diff] [blame] | 67 | static int encode_frame(vpx_codec_ctx_t *codec, |
| 68 | vpx_image_t *img, |
| 69 | int frame_index, |
| 70 | VpxVideoWriter *writer) { |
| 71 | int got_pkts = 0; |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 72 | vpx_codec_iter_t iter = NULL; |
| 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"); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 78 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 79 | while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { |
Dmitry Kovalev | 965af79 | 2014-08-14 10:28:00 -0700 | [diff] [blame] | 80 | got_pkts = 1; |
| 81 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 82 | if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { |
| 83 | const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; |
| 84 | if (!vpx_video_writer_write_frame(writer, |
| 85 | pkt->data.frame.buf, |
| 86 | pkt->data.frame.sz, |
| 87 | pkt->data.frame.pts)) { |
| 88 | die_codec(codec, "Failed to write compressed frame"); |
| 89 | } |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 90 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 91 | printf(keyframe ? "K" : "."); |
| 92 | fflush(stdout); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 93 | } |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 94 | } |
Dmitry Kovalev | 965af79 | 2014-08-14 10:28:00 -0700 | [diff] [blame] | 95 | |
| 96 | return got_pkts; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int main(int argc, char **argv) { |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 100 | FILE *infile = NULL; |
| 101 | vpx_codec_ctx_t codec = {0}; |
| 102 | vpx_codec_enc_cfg_t cfg = {0}; |
| 103 | int frame_count = 0; |
| 104 | vpx_image_t raw; |
| 105 | vpx_codec_err_t res; |
| 106 | VpxVideoInfo info = {0}; |
| 107 | VpxVideoWriter *writer = NULL; |
| 108 | const VpxInterface *encoder = NULL; |
| 109 | int update_frame_num = 0; |
| 110 | const int fps = 30; // TODO(dkovalev) add command line argument |
| 111 | const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 112 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 113 | exec_name = argv[0]; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 114 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 115 | if (argc != 6) |
| 116 | die("Invalid number of arguments"); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 117 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 118 | // TODO(dkovalev): add vp9 support and rename the file accordingly |
| 119 | encoder = get_vpx_encoder_by_name("vp8"); |
| 120 | if (!encoder) |
| 121 | die("Unsupported codec."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 122 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 123 | update_frame_num = atoi(argv[5]); |
| 124 | if (!update_frame_num) |
| 125 | die("Couldn't parse frame number '%s'\n", argv[5]); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 126 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 127 | info.codec_fourcc = encoder->fourcc; |
| 128 | info.frame_width = strtol(argv[1], NULL, 0); |
| 129 | info.frame_height = strtol(argv[2], NULL, 0); |
| 130 | info.time_base.numerator = 1; |
| 131 | info.time_base.denominator = fps; |
| 132 | |
| 133 | if (info.frame_width <= 0 || |
| 134 | info.frame_height <= 0 || |
| 135 | (info.frame_width % 2) != 0 || |
| 136 | (info.frame_height % 2) != 0) { |
| 137 | die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); |
| 138 | } |
| 139 | |
| 140 | if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width, |
| 141 | info.frame_height, 1)) { |
| 142 | die("Failed to allocate image."); |
| 143 | } |
| 144 | |
Deb Mukherjee | f332c63 | 2014-07-15 16:55:12 -0700 | [diff] [blame] | 145 | printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface())); |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 146 | |
Deb Mukherjee | f332c63 | 2014-07-15 16:55:12 -0700 | [diff] [blame] | 147 | res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 148 | if (res) |
| 149 | die_codec(&codec, "Failed to get default codec config."); |
| 150 | |
| 151 | cfg.g_w = info.frame_width; |
| 152 | cfg.g_h = info.frame_height; |
| 153 | cfg.g_timebase.num = info.time_base.numerator; |
| 154 | cfg.g_timebase.den = info.time_base.denominator; |
| 155 | cfg.rc_target_bitrate = bitrate; |
| 156 | |
| 157 | writer = vpx_video_writer_open(argv[4], kContainerIVF, &info); |
| 158 | if (!writer) |
| 159 | die("Failed to open %s for writing.", argv[4]); |
| 160 | |
| 161 | if (!(infile = fopen(argv[3], "rb"))) |
| 162 | die("Failed to open %s for reading.", argv[3]); |
| 163 | |
Deb Mukherjee | f332c63 | 2014-07-15 16:55:12 -0700 | [diff] [blame] | 164 | if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0)) |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 165 | die_codec(&codec, "Failed to initialize encoder"); |
| 166 | |
Dmitry Kovalev | 965af79 | 2014-08-14 10:28:00 -0700 | [diff] [blame] | 167 | // Encode frames. |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 168 | while (vpx_img_read(&raw, infile)) { |
| 169 | if (frame_count + 1 == update_frame_num) { |
James Zern | ee77a1a | 2014-03-06 21:12:47 -0800 | [diff] [blame] | 170 | vpx_ref_frame_t ref; |
| 171 | ref.frame_type = VP8_LAST_FRAME; |
| 172 | ref.img = raw; |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 173 | if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref)) |
| 174 | die_codec(&codec, "Failed to set reference frame"); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 177 | encode_frame(&codec, &raw, frame_count++, writer); |
| 178 | } |
Dmitry Kovalev | 965af79 | 2014-08-14 10:28:00 -0700 | [diff] [blame] | 179 | |
| 180 | // Flush encoder. |
Deb Mukherjee | a349ee3 | 2014-10-13 14:27:53 -0700 | [diff] [blame] | 181 | while (encode_frame(&codec, NULL, -1, writer)) {} |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 182 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 183 | printf("\n"); |
| 184 | fclose(infile); |
| 185 | printf("Processed %d frames.\n", frame_count); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 186 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 187 | vpx_img_free(&raw); |
| 188 | if (vpx_codec_destroy(&codec)) |
| 189 | die_codec(&codec, "Failed to destroy codec."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 190 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 191 | vpx_video_writer_close(writer); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 192 | |
Dmitry Kovalev | cb4eb79 | 2014-03-03 17:41:51 -0800 | [diff] [blame] | 193 | return EXIT_SUCCESS; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 194 | } |