]> granicus.if.org Git - libvpx/blob - examples/vp8cx_set_ref.c
Merge "Add early termination in transform size search"
[libvpx] / examples / vp8cx_set_ref.c
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>
51 #include <string.h>
52
53 #define VPX_CODEC_DISABLE_COMPAT 1
54 #include "vpx/vp8cx.h"
55 #include "vpx/vpx_encoder.h"
56
57 #include "./tools_common.h"
58 #include "./video_writer.h"
59
60 static const char *exec_name;
61
62 void usage_exit() {
63   fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
64           exec_name);
65   exit(EXIT_FAILURE);
66 }
67
68 static int encode_frame(vpx_codec_ctx_t *codec,
69                         vpx_image_t *img,
70                         int frame_index,
71                         VpxVideoWriter *writer) {
72   int got_pkts = 0;
73   vpx_codec_iter_t iter = NULL;
74   const vpx_codec_cx_pkt_t *pkt = NULL;
75   const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0,
76                                                VPX_DL_GOOD_QUALITY);
77   if (res != VPX_CODEC_OK)
78     die_codec(codec, "Failed to encode frame");
79
80   while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
81     got_pkts = 1;
82
83     if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
84       const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
85       if (!vpx_video_writer_write_frame(writer,
86                                         pkt->data.frame.buf,
87                                         pkt->data.frame.sz,
88                                         pkt->data.frame.pts)) {
89         die_codec(codec, "Failed to write compressed frame");
90       }
91
92       printf(keyframe ? "K" : ".");
93       fflush(stdout);
94     }
95   }
96
97   return got_pkts;
98 }
99
100 int main(int argc, char **argv) {
101   FILE *infile = NULL;
102   vpx_codec_ctx_t codec = {0};
103   vpx_codec_enc_cfg_t cfg = {0};
104   int frame_count = 0;
105   vpx_image_t raw;
106   vpx_codec_err_t res;
107   VpxVideoInfo info = {0};
108   VpxVideoWriter *writer = NULL;
109   const VpxInterface *encoder = NULL;
110   int update_frame_num = 0;
111   const int fps = 30;        // TODO(dkovalev) add command line argument
112   const int bitrate = 200;   // kbit/s TODO(dkovalev) add command line argument
113
114   exec_name = argv[0];
115
116   if (argc != 6)
117     die("Invalid number of arguments");
118
119   // TODO(dkovalev): add vp9 support and rename the file accordingly
120   encoder = get_vpx_encoder_by_name("vp8");
121   if (!encoder)
122     die("Unsupported codec.");
123
124   update_frame_num = atoi(argv[5]);
125   if (!update_frame_num)
126     die("Couldn't parse frame number '%s'\n", argv[5]);
127
128   info.codec_fourcc = encoder->fourcc;
129   info.frame_width = strtol(argv[1], NULL, 0);
130   info.frame_height = strtol(argv[2], NULL, 0);
131   info.time_base.numerator = 1;
132   info.time_base.denominator = fps;
133
134   if (info.frame_width <= 0 ||
135       info.frame_height <= 0 ||
136       (info.frame_width % 2) != 0 ||
137       (info.frame_height % 2) != 0) {
138     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
139   }
140
141   if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
142                                              info.frame_height, 1)) {
143     die("Failed to allocate image.");
144   }
145
146   printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
147
148   res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
149   if (res)
150     die_codec(&codec, "Failed to get default codec config.");
151
152   cfg.g_w = info.frame_width;
153   cfg.g_h = info.frame_height;
154   cfg.g_timebase.num = info.time_base.numerator;
155   cfg.g_timebase.den = info.time_base.denominator;
156   cfg.rc_target_bitrate = bitrate;
157
158   writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
159   if (!writer)
160     die("Failed to open %s for writing.", argv[4]);
161
162   if (!(infile = fopen(argv[3], "rb")))
163     die("Failed to open %s for reading.", argv[3]);
164
165   if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
166     die_codec(&codec, "Failed to initialize encoder");
167
168   // Encode frames.
169   while (vpx_img_read(&raw, infile)) {
170     if (frame_count + 1 == update_frame_num) {
171       vpx_ref_frame_t ref;
172       ref.frame_type = VP8_LAST_FRAME;
173       ref.img = raw;
174       if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
175         die_codec(&codec, "Failed to set reference frame");
176     }
177
178     encode_frame(&codec, &raw, frame_count++, writer);
179   }
180
181   // Flush encoder.
182   while (encode_frame(&codec, NULL, -1, writer)) {};
183
184   printf("\n");
185   fclose(infile);
186   printf("Processed %d frames.\n", frame_count);
187
188   vpx_img_free(&raw);
189   if (vpx_codec_destroy(&codec))
190     die_codec(&codec, "Failed to destroy codec.");
191
192   vpx_video_writer_close(writer);
193
194   return EXIT_SUCCESS;
195 }