blob: f8de3d24c284cc5f846dd5ef5e4d76728e6d0d76 [file] [log] [blame]
Sami Kalliomäkie2410e92017-06-02 14:46:12 +02001/*
2 * Copyright 2017 The WebRTC 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
11package org.webrtc;
12
Niels Möller6fd67f02019-06-13 14:37:24 +020013import android.support.annotation.Nullable;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020014import java.nio.ByteBuffer;
sakale172d892017-08-31 02:37:28 -070015import java.util.concurrent.TimeUnit;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020016
17/**
18 * An encoded frame from a video stream. Used as an input for decoders and as an output for
19 * encoders.
20 */
21public class EncodedImage {
sakal07a3bd72017-09-04 03:57:21 -070022 // Must be kept in sync with common_types.h FrameType.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020023 public enum FrameType {
sakal07a3bd72017-09-04 03:57:21 -070024 EmptyFrame(0),
25 VideoFrameKey(3),
26 VideoFrameDelta(4);
27
28 private final int nativeIndex;
29
30 private FrameType(int nativeIndex) {
31 this.nativeIndex = nativeIndex;
32 }
33
34 public int getNative() {
35 return nativeIndex;
36 }
Magnus Jedvert4eb01882017-11-20 22:33:40 +010037
Magnus Jedvert4eb01882017-11-20 22:33:40 +010038 @CalledByNative("FrameType")
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010039 static FrameType fromNativeIndex(int nativeIndex) {
Magnus Jedvert4eb01882017-11-20 22:33:40 +010040 for (FrameType type : FrameType.values()) {
41 if (type.getNative() == nativeIndex) {
42 return type;
43 }
44 }
45 throw new IllegalArgumentException("Unknown native frame type: " + nativeIndex);
46 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020047 }
48
49 public final ByteBuffer buffer;
50 public final int encodedWidth;
51 public final int encodedHeight;
sakale172d892017-08-31 02:37:28 -070052 public final long captureTimeMs; // Deprecated
53 public final long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020054 public final FrameType frameType;
55 public final int rotation;
56 public final boolean completeFrame;
Niels Möller6fd67f02019-06-13 14:37:24 +020057 public final @Nullable Integer qp;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020058
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010059 @CalledByNative
sakale172d892017-08-31 02:37:28 -070060 private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs,
Niels Möller6fd67f02019-06-13 14:37:24 +020061 FrameType frameType, int rotation, boolean completeFrame, @Nullable Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020062 this.buffer = buffer;
63 this.encodedWidth = encodedWidth;
64 this.encodedHeight = encodedHeight;
sakale172d892017-08-31 02:37:28 -070065 this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs);
66 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020067 this.frameType = frameType;
68 this.rotation = rotation;
69 this.completeFrame = completeFrame;
70 this.qp = qp;
71 }
72
Niels Möller6fd67f02019-06-13 14:37:24 +020073 @CalledByNative
74 private ByteBuffer getBuffer() {
75 return buffer;
76 }
77
78 @CalledByNative
79 private int getEncodedWidth() {
80 return encodedWidth;
81 }
82
83 @CalledByNative
84 private int getEncodedHeight() {
85 return encodedHeight;
86 }
87
88 @CalledByNative
89 private long getCaptureTimeNs() {
90 return captureTimeNs;
91 }
92
93 @CalledByNative
94 private int getFrameType() {
95 return frameType.getNative();
96 }
97
98 @CalledByNative
99 private int getRotation() {
100 return rotation;
101 }
102
103 @CalledByNative
104 private boolean getCompleteFrame() {
105 return completeFrame;
106 }
107
108 @CalledByNative
109 private @Nullable Integer getQp() {
110 return qp;
111 }
112
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700113 public static Builder builder() {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200114 return new Builder();
115 }
116
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700117 public static class Builder {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200118 private ByteBuffer buffer;
119 private int encodedWidth;
120 private int encodedHeight;
sakale172d892017-08-31 02:37:28 -0700121 private long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200122 private EncodedImage.FrameType frameType;
123 private int rotation;
124 private boolean completeFrame;
Niels Möller6fd67f02019-06-13 14:37:24 +0200125 private @Nullable Integer qp;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200126
127 private Builder() {}
128
129 public Builder setBuffer(ByteBuffer buffer) {
130 this.buffer = buffer;
131 return this;
132 }
133
134 public Builder setEncodedWidth(int encodedWidth) {
135 this.encodedWidth = encodedWidth;
136 return this;
137 }
138
139 public Builder setEncodedHeight(int encodedHeight) {
140 this.encodedHeight = encodedHeight;
141 return this;
142 }
143
sakale172d892017-08-31 02:37:28 -0700144 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200145 public Builder setCaptureTimeMs(long captureTimeMs) {
sakale172d892017-08-31 02:37:28 -0700146 this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs);
147 return this;
148 }
149
150 public Builder setCaptureTimeNs(long captureTimeNs) {
151 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200152 return this;
153 }
154
155 public Builder setFrameType(EncodedImage.FrameType frameType) {
156 this.frameType = frameType;
157 return this;
158 }
159
160 public Builder setRotation(int rotation) {
161 this.rotation = rotation;
162 return this;
163 }
164
165 public Builder setCompleteFrame(boolean completeFrame) {
166 this.completeFrame = completeFrame;
167 return this;
168 }
169
Niels Möller6fd67f02019-06-13 14:37:24 +0200170 public Builder setQp(@Nullable Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200171 this.qp = qp;
172 return this;
173 }
174
175 public EncodedImage createEncodedImage() {
sakale172d892017-08-31 02:37:28 -0700176 return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +0200177 rotation, completeFrame, qp);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200178 }
179 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200180}