WebM Codec SDK: vp9_lossless_encoder

WebM Codec SDK

vp9_lossless_encoder
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
22static const char *exec_name;
23
24void 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
32static 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
59int 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}
vpx_codec_destroy
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
vpx_codec_iter_t
const void * vpx_codec_iter_t
Iterator.
Definition vpx_codec.h:190
vpx_codec_iface_name
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
vpx_codec_err_t
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
vpx_codec_control_
vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
VPX_CODEC_OK
@ VPX_CODEC_OK
Operation completed without error.
Definition vpx_codec.h:95
vpx_codec_enc_init
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition vpx_encoder.h:900
VPX_DL_GOOD_QUALITY
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:1006
vpx_codec_get_cx_data
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
VPX_FRAME_IS_KEY
#define VPX_FRAME_IS_KEY
Definition vpx_encoder.h:123
vpx_codec_enc_config_default
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
vpx_codec_encode
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, vpx_enc_deadline_t deadline)
Encode a frame.
VPX_CODEC_CX_FRAME_PKT
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:155
VP9E_SET_LOSSLESS
@ VP9E_SET_LOSSLESS
Codec control function to set lossless encoding mode.
Definition vp8cx.h:345
vpx_codec_ctx
Codec context structure.
Definition vpx_codec.h:200
vpx_codec_cx_pkt
Encoder output packet.
Definition vpx_encoder.h:167
vpx_codec_cx_pkt::flags
vpx_codec_frame_flags_t flags
Definition vpx_encoder.h:177
vpx_codec_cx_pkt::kind
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:168
vpx_codec_cx_pkt::frame
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_codec_cx_pkt::sz
size_t sz
Definition vpx_encoder.h:172
vpx_codec_cx_pkt::buf
void * buf
Definition vpx_encoder.h:171
vpx_codec_cx_pkt::pts
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:174
vpx_codec_cx_pkt::data
union vpx_codec_cx_pkt::@1 data
vpx_codec_enc_cfg
Encoder configuration structure.
Definition vpx_encoder.h:276
vpx_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:321
vpx_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:312
vpx_codec_enc_cfg::g_timebase
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:351
vpx_image
Image Descriptor.
Definition vpx_image.h:76
vpx_rational::den
int den
Definition vpx_encoder.h:228
vpx_rational::num
int num
Definition vpx_encoder.h:227
vp8cx.h
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
vpx_encoder.h
Describes the encoder algorithm interface to applications.
vpx_img_alloc
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
VPX_IMG_FMT_I420
@ VPX_IMG_FMT_I420
Definition vpx_image.h:42
vpx_img_free
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.