xref: /aosp_15_r20/external/libvpx/examples/twopass_encoder.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 // Two Pass Encoder
12 // ================
13 //
14 // This is an example of a two pass encoder loop. It takes an input file in
15 // YV12 format, passes it through the encoder twice, and writes the compressed
16 // frames to disk in IVF format. It builds upon the simple_encoder example.
17 //
18 // Twopass Variables
19 // -----------------
20 // Twopass mode needs to track the current pass number and the buffer of
21 // statistics packets.
22 //
23 // Updating The Configuration
24 // ---------------------------------
25 // In two pass mode, the configuration has to be updated on each pass. The
26 // statistics buffer is passed on the last pass.
27 //
28 // Encoding A Frame
29 // ----------------
30 // Encoding a frame in two pass mode is identical to the simple encoder
31 // example. To increase the quality while sacrificing encoding speed,
32 // VPX_DL_BEST_QUALITY can be used in place of VPX_DL_GOOD_QUALITY.
33 //
34 // Processing Statistics Packets
35 // -----------------------------
36 // Each packet of type `VPX_CODEC_CX_FRAME_PKT` contains the encoded data
37 // for this frame. We write a IVF frame header, followed by the raw data.
38 //
39 //
40 // Pass Progress Reporting
41 // -----------------------------
42 // It's sometimes helpful to see when each pass completes.
43 //
44 //
45 // Clean-up
46 // -----------------------------
47 // Destruction of the encoder instance must be done on each pass. The
48 // raw image should be destroyed at the end as usual.
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include "vpx/vpx_encoder.h"
55 
56 #include "../tools_common.h"
57 #include "../video_writer.h"
58 
59 static const char *exec_name;
60 
usage_exit(void)61 void usage_exit(void) {
62   fprintf(stderr,
63           "Usage: %s <codec> <width> <height> <infile> <outfile> "
64           "<frame limit>\n",
65           exec_name);
66   exit(EXIT_FAILURE);
67 }
68 
get_frame_stats(vpx_codec_ctx_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned int duration,vpx_enc_frame_flags_t flags,unsigned int deadline,vpx_fixed_buf_t * stats)69 static int get_frame_stats(vpx_codec_ctx_t *ctx, const vpx_image_t *img,
70                            vpx_codec_pts_t pts, unsigned int duration,
71                            vpx_enc_frame_flags_t flags, unsigned int deadline,
72                            vpx_fixed_buf_t *stats) {
73   int got_pkts = 0;
74   vpx_codec_iter_t iter = NULL;
75   const vpx_codec_cx_pkt_t *pkt = NULL;
76   const vpx_codec_err_t res =
77       vpx_codec_encode(ctx, img, pts, duration, flags, deadline);
78   if (res != VPX_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
79 
80   while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
81     got_pkts = 1;
82 
83     if (pkt->kind == VPX_CODEC_STATS_PKT) {
84       const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
85       const size_t pkt_size = pkt->data.twopass_stats.sz;
86       stats->buf = realloc(stats->buf, stats->sz + pkt_size);
87       if (!stats->buf) die("Failed to reallocate stats buffer.");
88       memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
89       stats->sz += pkt_size;
90     }
91   }
92 
93   return got_pkts;
94 }
95 
encode_frame(vpx_codec_ctx_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned int duration,vpx_enc_frame_flags_t flags,unsigned int deadline,VpxVideoWriter * writer)96 static int encode_frame(vpx_codec_ctx_t *ctx, const vpx_image_t *img,
97                         vpx_codec_pts_t pts, unsigned int duration,
98                         vpx_enc_frame_flags_t flags, unsigned int deadline,
99                         VpxVideoWriter *writer) {
100   int got_pkts = 0;
101   vpx_codec_iter_t iter = NULL;
102   const vpx_codec_cx_pkt_t *pkt = NULL;
103   const vpx_codec_err_t res =
104       vpx_codec_encode(ctx, img, pts, duration, flags, deadline);
105   if (res != VPX_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
106 
107   while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
108     got_pkts = 1;
109     if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
110       const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
111 
112       if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
113                                         pkt->data.frame.sz,
114                                         pkt->data.frame.pts))
115         die_codec(ctx, "Failed to write compressed frame.");
116       printf(keyframe ? "K" : ".");
117       fflush(stdout);
118     }
119   }
120 
121   return got_pkts;
122 }
123 
pass0(vpx_image_t * raw,FILE * infile,const VpxInterface * encoder,const vpx_codec_enc_cfg_t * cfg,int max_frames)124 static vpx_fixed_buf_t pass0(vpx_image_t *raw, FILE *infile,
125                              const VpxInterface *encoder,
126                              const vpx_codec_enc_cfg_t *cfg, int max_frames) {
127   vpx_codec_ctx_t codec;
128   int frame_count = 0;
129   vpx_fixed_buf_t stats = { NULL, 0 };
130 
131   if (vpx_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
132     die("Failed to initialize encoder");
133 
134   // Calculate frame statistics.
135   while (vpx_img_read(raw, infile)) {
136     ++frame_count;
137     get_frame_stats(&codec, raw, frame_count, 1, 0, VPX_DL_GOOD_QUALITY,
138                     &stats);
139     if (max_frames > 0 && frame_count >= max_frames) break;
140   }
141 
142   // Flush encoder.
143   while (get_frame_stats(&codec, NULL, frame_count, 1, 0, VPX_DL_GOOD_QUALITY,
144                          &stats)) {
145   }
146 
147   printf("Pass 0 complete. Processed %d frames.\n", frame_count);
148   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
149 
150   return stats;
151 }
152 
pass1(vpx_image_t * raw,FILE * infile,const char * outfile_name,const VpxInterface * encoder,const vpx_codec_enc_cfg_t * cfg,int max_frames)153 static void pass1(vpx_image_t *raw, FILE *infile, const char *outfile_name,
154                   const VpxInterface *encoder, const vpx_codec_enc_cfg_t *cfg,
155                   int max_frames) {
156   VpxVideoInfo info = { encoder->fourcc,
157                         cfg->g_w,
158                         cfg->g_h,
159                         { cfg->g_timebase.num, cfg->g_timebase.den } };
160   VpxVideoWriter *writer = NULL;
161   vpx_codec_ctx_t codec;
162   int frame_count = 0;
163 
164   writer = vpx_video_writer_open(outfile_name, kContainerIVF, &info);
165   if (!writer) die("Failed to open %s for writing", outfile_name);
166 
167   if (vpx_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
168     die("Failed to initialize encoder");
169 
170   // Encode frames.
171   while (vpx_img_read(raw, infile)) {
172     ++frame_count;
173     encode_frame(&codec, raw, frame_count, 1, 0, VPX_DL_GOOD_QUALITY, writer);
174 
175     if (max_frames > 0 && frame_count >= max_frames) break;
176   }
177 
178   // Flush encoder.
179   while (encode_frame(&codec, NULL, -1, 1, 0, VPX_DL_GOOD_QUALITY, writer)) {
180   }
181 
182   printf("\n");
183 
184   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
185 
186   vpx_video_writer_close(writer);
187 
188   printf("Pass 1 complete. Processed %d frames.\n", frame_count);
189 }
190 
main(int argc,char ** argv)191 int main(int argc, char **argv) {
192   FILE *infile = NULL;
193   int w, h;
194   vpx_codec_ctx_t codec;
195   vpx_codec_enc_cfg_t cfg;
196   vpx_image_t raw;
197   vpx_codec_err_t res;
198   vpx_fixed_buf_t stats;
199 
200   const VpxInterface *encoder = NULL;
201   const int fps = 30;       // TODO(dkovalev) add command line argument
202   const int bitrate = 200;  // kbit/s TODO(dkovalev) add command line argument
203   const char *const codec_arg = argv[1];
204   const char *const width_arg = argv[2];
205   const char *const height_arg = argv[3];
206   const char *const infile_arg = argv[4];
207   const char *const outfile_arg = argv[5];
208   int max_frames = 0;
209   exec_name = argv[0];
210 
211   if (argc != 7) die("Invalid number of arguments.");
212 
213   max_frames = (int)strtol(argv[6], NULL, 0);
214 
215   encoder = get_vpx_encoder_by_name(codec_arg);
216   if (!encoder) die("Unsupported codec.");
217 
218   w = (int)strtol(width_arg, NULL, 0);
219   h = (int)strtol(height_arg, NULL, 0);
220 
221   if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
222     die("Invalid frame size: %dx%d", w, h);
223 
224   if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, w, h, 1))
225     die("Failed to allocate image (%dx%d)", w, h);
226 
227   printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
228 
229   // Configuration
230   res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
231   if (res) die_codec(&codec, "Failed to get default codec config.");
232 
233   cfg.g_w = w;
234   cfg.g_h = h;
235   cfg.g_timebase.num = 1;
236   cfg.g_timebase.den = fps;
237   cfg.rc_target_bitrate = bitrate;
238 
239   if (!(infile = fopen(infile_arg, "rb")))
240     die("Failed to open %s for reading", infile_arg);
241 
242   // Pass 0
243   cfg.g_pass = VPX_RC_FIRST_PASS;
244   stats = pass0(&raw, infile, encoder, &cfg, max_frames);
245 
246   // Pass 1
247   rewind(infile);
248   cfg.g_pass = VPX_RC_LAST_PASS;
249   cfg.rc_twopass_stats_in = stats;
250   pass1(&raw, infile, outfile_arg, encoder, &cfg, max_frames);
251   free(stats.buf);
252 
253   vpx_img_free(&raw);
254   fclose(infile);
255 
256   return EXIT_SUCCESS;
257 }
258