xref: /aosp_15_r20/external/libvpx/examples/vp9_lossless_encoder.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2014 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 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "vpx/vpx_encoder.h"
16 #include "vpx/vp8cx.h"
17 #include "vp9/common/vp9_common.h"
18 
19 #include "../tools_common.h"
20 #include "../video_writer.h"
21 
22 static const char *exec_name;
23 
usage_exit(void)24 void usage_exit(void) {
25   fprintf(stderr,
26           "vp9_lossless_encoder: Example demonstrating VP9 lossless "
27           "encoding feature. Supports raw input only.\n");
28   fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
29   exit(EXIT_FAILURE);
30 }
31 
encode_frame(vpx_codec_ctx_t * codec,vpx_image_t * img,int frame_index,int flags,VpxVideoWriter * writer)32 static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
33                         int frame_index, int flags, VpxVideoWriter *writer) {
34   int got_pkts = 0;
35   vpx_codec_iter_t iter = NULL;
36   const vpx_codec_cx_pkt_t *pkt = NULL;
37   const vpx_codec_err_t res =
38       vpx_codec_encode(codec, img, frame_index, 1, flags, VPX_DL_GOOD_QUALITY);
39   if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
40 
41   while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
42     got_pkts = 1;
43 
44     if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
45       const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
46       if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
47                                         pkt->data.frame.sz,
48                                         pkt->data.frame.pts)) {
49         die_codec(codec, "Failed to write compressed frame");
50       }
51       printf(keyframe ? "K" : ".");
52       fflush(stdout);
53     }
54   }
55 
56   return got_pkts;
57 }
58 
main(int argc,char ** argv)59 int main(int argc, char **argv) {
60   FILE *infile = NULL;
61   vpx_codec_ctx_t codec;
62   vpx_codec_enc_cfg_t cfg;
63   int frame_count = 0;
64   vpx_image_t raw;
65   vpx_codec_err_t res;
66   VpxVideoInfo info;
67   VpxVideoWriter *writer = NULL;
68   const VpxInterface *encoder = NULL;
69   const int fps = 30;
70 
71   vp9_zero(info);
72 
73   exec_name = argv[0];
74 
75   if (argc < 5) die("Invalid number of arguments");
76 
77   encoder = get_vpx_encoder_by_name("vp9");
78   if (!encoder) die("Unsupported codec.");
79 
80   info.codec_fourcc = encoder->fourcc;
81   info.frame_width = (int)strtol(argv[1], NULL, 0);
82   info.frame_height = (int)strtol(argv[2], NULL, 0);
83   info.time_base.numerator = 1;
84   info.time_base.denominator = fps;
85 
86   if (info.frame_width <= 0 || info.frame_height <= 0 ||
87       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
88     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
89   }
90 
91   if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
92                      info.frame_height, 1)) {
93     die("Failed to allocate image.");
94   }
95 
96   printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
97 
98   res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
99   if (res) die_codec(&codec, "Failed to get default codec config.");
100 
101   cfg.g_w = info.frame_width;
102   cfg.g_h = info.frame_height;
103   cfg.g_timebase.num = info.time_base.numerator;
104   cfg.g_timebase.den = info.time_base.denominator;
105 
106   writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
107   if (!writer) die("Failed to open %s for writing.", argv[4]);
108 
109   if (!(infile = fopen(argv[3], "rb")))
110     die("Failed to open %s for reading.", argv[3]);
111 
112   if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
113     die("Failed to initialize encoder");
114 
115   if (vpx_codec_control_(&codec, VP9E_SET_LOSSLESS, 1))
116     die_codec(&codec, "Failed to use lossless mode");
117 
118   // Encode frames.
119   while (vpx_img_read(&raw, infile)) {
120     encode_frame(&codec, &raw, frame_count++, 0, writer);
121   }
122 
123   // Flush encoder.
124   while (encode_frame(&codec, NULL, -1, 0, writer)) {
125   }
126 
127   printf("\n");
128   fclose(infile);
129   printf("Processed %d frames.\n", frame_count);
130 
131   vpx_img_free(&raw);
132   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
133 
134   vpx_video_writer_close(writer);
135 
136   return EXIT_SUCCESS;
137 }
138