node-av 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/LICENSE.md +22 -0
- package/README.md +377 -0
- package/binding.gyp +78 -0
- package/dist/api/bitstream-filter.d.ts +246 -0
- package/dist/api/bitstream-filter.js +369 -0
- package/dist/api/bitstream-filter.js.map +1 -0
- package/dist/api/decoder.d.ts +257 -0
- package/dist/api/decoder.js +424 -0
- package/dist/api/decoder.js.map +1 -0
- package/dist/api/encoder.d.ts +298 -0
- package/dist/api/encoder.js +574 -0
- package/dist/api/encoder.js.map +1 -0
- package/dist/api/filter.d.ts +457 -0
- package/dist/api/filter.js +876 -0
- package/dist/api/filter.js.map +1 -0
- package/dist/api/hardware.d.ts +318 -0
- package/dist/api/hardware.js +558 -0
- package/dist/api/hardware.js.map +1 -0
- package/dist/api/index.d.ts +12 -0
- package/dist/api/index.js +20 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/io-stream.d.ts +109 -0
- package/dist/api/io-stream.js +124 -0
- package/dist/api/io-stream.js.map +1 -0
- package/dist/api/media-input.d.ts +295 -0
- package/dist/api/media-input.js +456 -0
- package/dist/api/media-input.js.map +1 -0
- package/dist/api/media-output.d.ts +274 -0
- package/dist/api/media-output.js +486 -0
- package/dist/api/media-output.js.map +1 -0
- package/dist/api/pipeline.d.ts +117 -0
- package/dist/api/pipeline.js +836 -0
- package/dist/api/pipeline.js.map +1 -0
- package/dist/api/types.d.ts +440 -0
- package/dist/api/types.js +2 -0
- package/dist/api/types.js.map +1 -0
- package/dist/api/utilities/audio-sample.d.ts +115 -0
- package/dist/api/utilities/audio-sample.js +110 -0
- package/dist/api/utilities/audio-sample.js.map +1 -0
- package/dist/api/utilities/channel-layout.d.ts +83 -0
- package/dist/api/utilities/channel-layout.js +87 -0
- package/dist/api/utilities/channel-layout.js.map +1 -0
- package/dist/api/utilities/image.d.ts +177 -0
- package/dist/api/utilities/image.js +183 -0
- package/dist/api/utilities/image.js.map +1 -0
- package/dist/api/utilities/index.d.ts +8 -0
- package/dist/api/utilities/index.js +17 -0
- package/dist/api/utilities/index.js.map +1 -0
- package/dist/api/utilities/media-type.d.ts +56 -0
- package/dist/api/utilities/media-type.js +60 -0
- package/dist/api/utilities/media-type.js.map +1 -0
- package/dist/api/utilities/pixel-format.d.ts +94 -0
- package/dist/api/utilities/pixel-format.js +102 -0
- package/dist/api/utilities/pixel-format.js.map +1 -0
- package/dist/api/utilities/sample-format.d.ts +132 -0
- package/dist/api/utilities/sample-format.js +144 -0
- package/dist/api/utilities/sample-format.js.map +1 -0
- package/dist/api/utilities/streaming.d.ts +104 -0
- package/dist/api/utilities/streaming.js +137 -0
- package/dist/api/utilities/streaming.js.map +1 -0
- package/dist/api/utilities/timestamp.d.ts +187 -0
- package/dist/api/utilities/timestamp.js +200 -0
- package/dist/api/utilities/timestamp.js.map +1 -0
- package/dist/api/utils.d.ts +61 -0
- package/dist/api/utils.js +330 -0
- package/dist/api/utils.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/audio-fifo.d.ts +339 -0
- package/dist/lib/audio-fifo.js +365 -0
- package/dist/lib/audio-fifo.js.map +1 -0
- package/dist/lib/binding.d.ts +192 -0
- package/dist/lib/binding.js +70 -0
- package/dist/lib/binding.js.map +1 -0
- package/dist/lib/bitstream-filter-context.d.ts +345 -0
- package/dist/lib/bitstream-filter-context.js +407 -0
- package/dist/lib/bitstream-filter-context.js.map +1 -0
- package/dist/lib/bitstream-filter.d.ts +124 -0
- package/dist/lib/bitstream-filter.js +138 -0
- package/dist/lib/bitstream-filter.js.map +1 -0
- package/dist/lib/channel-layouts.d.ts +51 -0
- package/dist/lib/channel-layouts.js +55 -0
- package/dist/lib/channel-layouts.js.map +1 -0
- package/dist/lib/codec-context.d.ts +763 -0
- package/dist/lib/codec-context.js +974 -0
- package/dist/lib/codec-context.js.map +1 -0
- package/dist/lib/codec-parameters.d.ts +362 -0
- package/dist/lib/codec-parameters.js +460 -0
- package/dist/lib/codec-parameters.js.map +1 -0
- package/dist/lib/codec-parser.d.ts +185 -0
- package/dist/lib/codec-parser.js +193 -0
- package/dist/lib/codec-parser.js.map +1 -0
- package/dist/lib/codec.d.ts +432 -0
- package/dist/lib/codec.js +492 -0
- package/dist/lib/codec.js.map +1 -0
- package/dist/lib/constants.d.ts +2037 -0
- package/dist/lib/constants.js +1659 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/dictionary.d.ts +371 -0
- package/dist/lib/dictionary.js +406 -0
- package/dist/lib/dictionary.js.map +1 -0
- package/dist/lib/error.d.ts +216 -0
- package/dist/lib/error.js +254 -0
- package/dist/lib/error.js.map +1 -0
- package/dist/lib/filter-context.d.ts +445 -0
- package/dist/lib/filter-context.js +505 -0
- package/dist/lib/filter-context.js.map +1 -0
- package/dist/lib/filter-graph.d.ts +556 -0
- package/dist/lib/filter-graph.js +608 -0
- package/dist/lib/filter-graph.js.map +1 -0
- package/dist/lib/filter-inout.d.ts +205 -0
- package/dist/lib/filter-inout.js +264 -0
- package/dist/lib/filter-inout.js.map +1 -0
- package/dist/lib/filter.d.ts +231 -0
- package/dist/lib/filter.js +260 -0
- package/dist/lib/filter.js.map +1 -0
- package/dist/lib/format-context.d.ts +798 -0
- package/dist/lib/format-context.js +845 -0
- package/dist/lib/format-context.js.map +1 -0
- package/dist/lib/frame.d.ts +784 -0
- package/dist/lib/frame.js +933 -0
- package/dist/lib/frame.js.map +1 -0
- package/dist/lib/hardware-device-context.d.ts +407 -0
- package/dist/lib/hardware-device-context.js +429 -0
- package/dist/lib/hardware-device-context.js.map +1 -0
- package/dist/lib/hardware-frames-context.d.ts +374 -0
- package/dist/lib/hardware-frames-context.js +430 -0
- package/dist/lib/hardware-frames-context.js.map +1 -0
- package/dist/lib/index.d.ts +31 -0
- package/dist/lib/index.js +54 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/input-format.d.ts +216 -0
- package/dist/lib/input-format.js +246 -0
- package/dist/lib/input-format.js.map +1 -0
- package/dist/lib/io-context.d.ts +495 -0
- package/dist/lib/io-context.js +550 -0
- package/dist/lib/io-context.js.map +1 -0
- package/dist/lib/log.d.ts +201 -0
- package/dist/lib/log.js +219 -0
- package/dist/lib/log.js.map +1 -0
- package/dist/lib/native-types.d.ts +719 -0
- package/dist/lib/native-types.js +2 -0
- package/dist/lib/native-types.js.map +1 -0
- package/dist/lib/option.d.ts +589 -0
- package/dist/lib/option.js +853 -0
- package/dist/lib/option.js.map +1 -0
- package/dist/lib/output-format.d.ts +179 -0
- package/dist/lib/output-format.js +205 -0
- package/dist/lib/output-format.js.map +1 -0
- package/dist/lib/packet.d.ts +487 -0
- package/dist/lib/packet.js +558 -0
- package/dist/lib/packet.js.map +1 -0
- package/dist/lib/rational.d.ts +210 -0
- package/dist/lib/rational.js +233 -0
- package/dist/lib/rational.js.map +1 -0
- package/dist/lib/software-resample-context.d.ts +572 -0
- package/dist/lib/software-resample-context.js +610 -0
- package/dist/lib/software-resample-context.js.map +1 -0
- package/dist/lib/software-scale-context.d.ts +290 -0
- package/dist/lib/software-scale-context.js +308 -0
- package/dist/lib/software-scale-context.js.map +1 -0
- package/dist/lib/stream.d.ts +322 -0
- package/dist/lib/stream.js +408 -0
- package/dist/lib/stream.js.map +1 -0
- package/dist/lib/types.d.ts +59 -0
- package/dist/lib/types.js +8 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utilities.d.ts +346 -0
- package/dist/lib/utilities.js +424 -0
- package/dist/lib/utilities.js.map +1 -0
- package/install/check.js +113 -0
- package/install/ffmpeg.js +163 -0
- package/package.json +107 -0
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
import { bindings } from './binding.js';
|
|
2
|
+
import { HardwareDeviceContext } from './hardware-device-context.js';
|
|
3
|
+
import { HardwareFramesContext } from './hardware-frames-context.js';
|
|
4
|
+
import { OptionMember } from './option.js';
|
|
5
|
+
import { Rational } from './rational.js';
|
|
6
|
+
/**
|
|
7
|
+
* Codec context for encoding and decoding media.
|
|
8
|
+
*
|
|
9
|
+
* Central structure for media encoding and decoding operations.
|
|
10
|
+
* Manages codec state, parameters, and threading.
|
|
11
|
+
* Supports both software and hardware acceleration.
|
|
12
|
+
*
|
|
13
|
+
* Direct mapping to FFmpeg's AVCodecContext.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { CodecContext, Codec, FFmpegError } from 'node-av';
|
|
18
|
+
* import { AV_CODEC_ID_H264, AV_PIX_FMT_YUV420P } from 'node-av/constants';
|
|
19
|
+
*
|
|
20
|
+
* // Create and configure decoder context
|
|
21
|
+
* const codec = Codec.findDecoder(AV_CODEC_ID_H264);
|
|
22
|
+
* const ctx = new CodecContext();
|
|
23
|
+
* ctx.allocContext3(codec);
|
|
24
|
+
*
|
|
25
|
+
* // Configure parameters
|
|
26
|
+
* ctx.width = 1920;
|
|
27
|
+
* ctx.height = 1080;
|
|
28
|
+
* ctx.pixelFormat = AV_PIX_FMT_YUV420P;
|
|
29
|
+
*
|
|
30
|
+
* // Open codec
|
|
31
|
+
* const ret = await ctx.open2(codec, null);
|
|
32
|
+
* FFmpegError.throwIfError(ret, 'open2');
|
|
33
|
+
*
|
|
34
|
+
* // Decode packets
|
|
35
|
+
* const sendRet = await ctx.sendPacket(packet);
|
|
36
|
+
* FFmpegError.throwIfError(sendRet, 'sendPacket');
|
|
37
|
+
*
|
|
38
|
+
* while (true) {
|
|
39
|
+
* const ret = await ctx.receiveFrame(frame);
|
|
40
|
+
* if (ret === AVERROR_EOF || ret === AVERROR(EAGAIN)) break;
|
|
41
|
+
* FFmpegError.throwIfError(ret, 'receiveFrame');
|
|
42
|
+
* // Process frame
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* // Cleanup
|
|
46
|
+
* ctx.freeContext();
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export class CodecContext extends OptionMember {
|
|
50
|
+
_hwDeviceCtx; // Cache for hardware device context wrapper
|
|
51
|
+
_hwFramesCtx; // Cache for hardware frames context wrapper
|
|
52
|
+
/**
|
|
53
|
+
* Create a new codec context.
|
|
54
|
+
*
|
|
55
|
+
* The context is uninitialized - you must call allocContext3() before use.
|
|
56
|
+
* No FFmpeg resources are allocated until initialization.
|
|
57
|
+
*
|
|
58
|
+
* Direct wrapper around AVCodecContext.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* import { CodecContext, Codec } from 'node-av';
|
|
63
|
+
*
|
|
64
|
+
* const ctx = new CodecContext();
|
|
65
|
+
* ctx.allocContext3(codec);
|
|
66
|
+
* // Context is now ready for configuration
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
constructor() {
|
|
70
|
+
super(new bindings.CodecContext());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Codec type.
|
|
74
|
+
*
|
|
75
|
+
* Identifies whether this is a video, audio, subtitle, or data codec.
|
|
76
|
+
*
|
|
77
|
+
* Direct mapping to AVCodecContext->codec_type
|
|
78
|
+
*/
|
|
79
|
+
get codecType() {
|
|
80
|
+
return this.native.codecType;
|
|
81
|
+
}
|
|
82
|
+
set codecType(value) {
|
|
83
|
+
this.native.codecType = value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Codec ID.
|
|
87
|
+
*
|
|
88
|
+
* Identifies the specific codec (e.g., AV_CODEC_ID_H264, AV_CODEC_ID_AAC).
|
|
89
|
+
*
|
|
90
|
+
* Direct mapping to AVCodecContext->codec_id
|
|
91
|
+
*/
|
|
92
|
+
get codecId() {
|
|
93
|
+
return this.native.codecId;
|
|
94
|
+
}
|
|
95
|
+
set codecId(value) {
|
|
96
|
+
this.native.codecId = value;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The average bitrate.
|
|
100
|
+
*
|
|
101
|
+
* Direct mapping to AVCodecContext->bit_rate
|
|
102
|
+
*
|
|
103
|
+
* - encoding: Set by user, unused for constant quantizer encoding.
|
|
104
|
+
* - decoding: Set by user, may be overwritten by libavcodec if this info is available in the stream.
|
|
105
|
+
*/
|
|
106
|
+
get bitRate() {
|
|
107
|
+
return this.native.bitRate;
|
|
108
|
+
}
|
|
109
|
+
set bitRate(value) {
|
|
110
|
+
this.native.bitRate = value;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Time base for timestamps.
|
|
114
|
+
*
|
|
115
|
+
* The fundamental unit of time (in seconds) for frame timestamps.
|
|
116
|
+
*
|
|
117
|
+
* Direct mapping to AVCodecContext->time_base
|
|
118
|
+
*
|
|
119
|
+
* - encoding: MUST be set by user.
|
|
120
|
+
* - decoding: the use of this field for decoding is deprecated. Use framerate instead.
|
|
121
|
+
*/
|
|
122
|
+
get timeBase() {
|
|
123
|
+
const tb = this.native.timeBase;
|
|
124
|
+
return new Rational(tb.num, tb.den);
|
|
125
|
+
}
|
|
126
|
+
set timeBase(value) {
|
|
127
|
+
this.native.timeBase = { num: value.num, den: value.den };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
|
|
131
|
+
* This is the fundamental unit of time (in seconds) in terms
|
|
132
|
+
* of which frame timestamps are represented.
|
|
133
|
+
*/
|
|
134
|
+
get pktTimebase() {
|
|
135
|
+
const tb = this.native.pktTimebase;
|
|
136
|
+
return new Rational(tb.num, tb.den);
|
|
137
|
+
}
|
|
138
|
+
set pktTimebase(value) {
|
|
139
|
+
this.native.pktTimebase = { num: value.num, den: value.den };
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Codec delay.
|
|
143
|
+
* - encoding: Number of frames delay there will be from the encoder input to
|
|
144
|
+
* the decoder output. (we assume the decoder matches the spec)
|
|
145
|
+
* - decoding: Number of frames delay in addition to what a standard decoder
|
|
146
|
+
* as specified in the spec would produce.
|
|
147
|
+
* @readonly
|
|
148
|
+
*/
|
|
149
|
+
get delay() {
|
|
150
|
+
return this.native.delay;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* AV_CODEC_FLAG_* flags.
|
|
154
|
+
* - encoding: Set by user.
|
|
155
|
+
* - decoding: Set by user.
|
|
156
|
+
*/
|
|
157
|
+
get flags() {
|
|
158
|
+
return this.native.flags;
|
|
159
|
+
}
|
|
160
|
+
set flags(value) {
|
|
161
|
+
this.native.flags = value;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* AV_CODEC_FLAG2_* flags.
|
|
165
|
+
* - encoding: Set by user.
|
|
166
|
+
* - decoding: Set by user.
|
|
167
|
+
*/
|
|
168
|
+
get flags2() {
|
|
169
|
+
return this.native.flags2;
|
|
170
|
+
}
|
|
171
|
+
set flags2(value) {
|
|
172
|
+
this.native.flags2 = value;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Some codecs need / can use extradata like Huffman tables.
|
|
176
|
+
* MJPEG: Huffman tables
|
|
177
|
+
* rv10: additional flags
|
|
178
|
+
* MPEG-4: global headers (they can be in the bitstream or here)
|
|
179
|
+
* The allocated memory should be AV_INPUT_BUFFER_PADDING_SIZE bytes larger
|
|
180
|
+
* than extradata_size to avoid problems if it is read with the bitstream reader.
|
|
181
|
+
* The bytewise contents of extradata must not depend on the architecture or CPU endianness.
|
|
182
|
+
* Must be allocated with the av_malloc() family of functions.
|
|
183
|
+
* - encoding: Set/allocated/freed by libavcodec.
|
|
184
|
+
* - decoding: Set/allocated/freed by user.
|
|
185
|
+
*/
|
|
186
|
+
get extraData() {
|
|
187
|
+
return this.native.extraData;
|
|
188
|
+
}
|
|
189
|
+
set extraData(value) {
|
|
190
|
+
this.native.extraData = value;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Profile (FF_PROFILE_H264_BASELINE, FF_PROFILE_H264_MAIN, etc.)
|
|
194
|
+
* - encoding: Set by user.
|
|
195
|
+
* - decoding: Set by libavcodec.
|
|
196
|
+
*/
|
|
197
|
+
get profile() {
|
|
198
|
+
return this.native.profile;
|
|
199
|
+
}
|
|
200
|
+
set profile(value) {
|
|
201
|
+
this.native.profile = value;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Level (FF_LEVEL_UNKNOWN, or codec-specific values)
|
|
205
|
+
* - encoding: Set by user.
|
|
206
|
+
* - decoding: Set by libavcodec.
|
|
207
|
+
*/
|
|
208
|
+
get level() {
|
|
209
|
+
return this.native.level;
|
|
210
|
+
}
|
|
211
|
+
set level(value) {
|
|
212
|
+
this.native.level = value;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Thread count.
|
|
216
|
+
* Is used to decide how many independent tasks should be passed to execute().
|
|
217
|
+
* - encoding: Set by user.
|
|
218
|
+
* - decoding: Set by user.
|
|
219
|
+
*/
|
|
220
|
+
get threadCount() {
|
|
221
|
+
return this.native.threadCount;
|
|
222
|
+
}
|
|
223
|
+
set threadCount(value) {
|
|
224
|
+
this.native.threadCount = value;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Picture width.
|
|
228
|
+
* - encoding: MUST be set by user.
|
|
229
|
+
* - decoding: May be set by the user before opening the decoder if known e.g.
|
|
230
|
+
* from the container. Some decoders will require the dimensions
|
|
231
|
+
* to be set by the caller. During decoding, the decoder may
|
|
232
|
+
* overwrite those values as required while parsing the data.
|
|
233
|
+
*/
|
|
234
|
+
get width() {
|
|
235
|
+
return this.native.width;
|
|
236
|
+
}
|
|
237
|
+
set width(value) {
|
|
238
|
+
this.native.width = value;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Picture height.
|
|
242
|
+
* - encoding: MUST be set by user.
|
|
243
|
+
* - decoding: May be set by the user before opening the decoder if known e.g.
|
|
244
|
+
* from the container. Some decoders will require the dimensions
|
|
245
|
+
* to be set by the caller. During decoding, the decoder may
|
|
246
|
+
* overwrite those values as required while parsing the data.
|
|
247
|
+
*/
|
|
248
|
+
get height() {
|
|
249
|
+
return this.native.height;
|
|
250
|
+
}
|
|
251
|
+
set height(value) {
|
|
252
|
+
this.native.height = value;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The number of pictures in a group of pictures, or 0 for intra_only.
|
|
256
|
+
* - encoding: Set by user.
|
|
257
|
+
* - decoding: unused
|
|
258
|
+
*/
|
|
259
|
+
get gopSize() {
|
|
260
|
+
return this.native.gopSize;
|
|
261
|
+
}
|
|
262
|
+
set gopSize(value) {
|
|
263
|
+
this.native.gopSize = value;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Pixel format.
|
|
267
|
+
* - encoding: Set by user.
|
|
268
|
+
* - decoding: Set by user if known, overridden by libavcodec while
|
|
269
|
+
* parsing the data.
|
|
270
|
+
*/
|
|
271
|
+
get pixelFormat() {
|
|
272
|
+
return this.native.pixelFormat;
|
|
273
|
+
}
|
|
274
|
+
set pixelFormat(value) {
|
|
275
|
+
this.native.pixelFormat = value;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Maximum number of B-frames between non-B-frames.
|
|
279
|
+
* Note: The output will be delayed by max_b_frames+1 relative to the input.
|
|
280
|
+
* - encoding: Set by user.
|
|
281
|
+
* - decoding: unused
|
|
282
|
+
*/
|
|
283
|
+
get maxBFrames() {
|
|
284
|
+
return this.native.maxBFrames;
|
|
285
|
+
}
|
|
286
|
+
set maxBFrames(value) {
|
|
287
|
+
this.native.maxBFrames = value;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Macroblock decision mode.
|
|
291
|
+
*
|
|
292
|
+
* Direct mapping to AVCodecContext->mb_decision
|
|
293
|
+
*
|
|
294
|
+
* - encoding: Set by user.
|
|
295
|
+
* - decoding: unused
|
|
296
|
+
*
|
|
297
|
+
* Values:
|
|
298
|
+
* - 0 (FF_MB_DECISION_SIMPLE): uses mb_cmp
|
|
299
|
+
* - 1 (FF_MB_DECISION_BITS): chooses the one which needs the fewest bits
|
|
300
|
+
* - 2 (FF_MB_DECISION_RD): rate distortion
|
|
301
|
+
*/
|
|
302
|
+
get mbDecision() {
|
|
303
|
+
return this.native.mbDecision;
|
|
304
|
+
}
|
|
305
|
+
set mbDecision(value) {
|
|
306
|
+
this.native.mbDecision = value;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Size of the frame reordering buffer in the decoder.
|
|
310
|
+
* For MPEG-2 it is 1 IPB or 0 low delay IP.
|
|
311
|
+
* - encoding: Set by libavcodec.
|
|
312
|
+
* - decoding: Set by libavcodec.
|
|
313
|
+
* @readonly
|
|
314
|
+
*/
|
|
315
|
+
get hasBFrames() {
|
|
316
|
+
return this.native.hasBFrames;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Sample aspect ratio (0 if unknown).
|
|
320
|
+
* That is the width of a pixel divided by the height of the pixel.
|
|
321
|
+
* Numerator and denominator must be relatively prime and smaller than 256 for some video standards.
|
|
322
|
+
* - encoding: Set by user.
|
|
323
|
+
* - decoding: Set by libavcodec.
|
|
324
|
+
*/
|
|
325
|
+
get sampleAspectRatio() {
|
|
326
|
+
const sar = this.native.sampleAspectRatio;
|
|
327
|
+
return new Rational(sar.num || 0, sar.den || 1);
|
|
328
|
+
}
|
|
329
|
+
set sampleAspectRatio(value) {
|
|
330
|
+
this.native.sampleAspectRatio = { num: value.num, den: value.den };
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Framerate.
|
|
334
|
+
* - encoding: May be used to signal the framerate of CFR content to an encoder.
|
|
335
|
+
* - decoding: For codecs that store a framerate value in the compressed
|
|
336
|
+
* bitstream, the decoder may export it here. { 0, 1} when
|
|
337
|
+
* unknown.
|
|
338
|
+
*/
|
|
339
|
+
get framerate() {
|
|
340
|
+
const fr = this.native.framerate;
|
|
341
|
+
return new Rational(fr.num, fr.den);
|
|
342
|
+
}
|
|
343
|
+
set framerate(value) {
|
|
344
|
+
this.native.framerate = { num: value.num, den: value.den };
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Color range (AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, etc.)
|
|
348
|
+
* - encoding: Set by user
|
|
349
|
+
* - decoding: Set by libavcodec
|
|
350
|
+
*/
|
|
351
|
+
get colorRange() {
|
|
352
|
+
return this.native.colorRange;
|
|
353
|
+
}
|
|
354
|
+
set colorRange(value) {
|
|
355
|
+
this.native.colorRange = value;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Chromaticity coordinates of the source primaries.
|
|
359
|
+
* - encoding: Set by user
|
|
360
|
+
* - decoding: Set by libavcodec
|
|
361
|
+
*/
|
|
362
|
+
get colorPrimaries() {
|
|
363
|
+
return this.native.colorPrimaries;
|
|
364
|
+
}
|
|
365
|
+
set colorPrimaries(value) {
|
|
366
|
+
this.native.colorPrimaries = value;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Color Transfer Characteristic.
|
|
370
|
+
* - encoding: Set by user
|
|
371
|
+
* - decoding: Set by libavcodec
|
|
372
|
+
*/
|
|
373
|
+
get colorTrc() {
|
|
374
|
+
return this.native.colorTrc;
|
|
375
|
+
}
|
|
376
|
+
set colorTrc(value) {
|
|
377
|
+
this.native.colorTrc = value;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* YUV colorspace type.
|
|
381
|
+
* - encoding: Set by user
|
|
382
|
+
* - decoding: Set by libavcodec
|
|
383
|
+
*/
|
|
384
|
+
get colorSpace() {
|
|
385
|
+
return this.native.colorSpace;
|
|
386
|
+
}
|
|
387
|
+
set colorSpace(value) {
|
|
388
|
+
this.native.colorSpace = value;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Location of chroma samples.
|
|
392
|
+
* - encoding: Set by user
|
|
393
|
+
* - decoding: Set by libavcodec
|
|
394
|
+
*/
|
|
395
|
+
get chromaLocation() {
|
|
396
|
+
return this.native.chromaLocation;
|
|
397
|
+
}
|
|
398
|
+
set chromaLocation(value) {
|
|
399
|
+
this.native.chromaLocation = value;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Sample rate of the audio data.
|
|
403
|
+
* - encoding: MUST be set by user.
|
|
404
|
+
* - decoding: May be set by the user before opening the decoder if known e.g.
|
|
405
|
+
* from the container. The decoder can change this value.
|
|
406
|
+
*/
|
|
407
|
+
get sampleRate() {
|
|
408
|
+
return this.native.sampleRate;
|
|
409
|
+
}
|
|
410
|
+
set sampleRate(value) {
|
|
411
|
+
this.native.sampleRate = value;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Number of audio channels.
|
|
415
|
+
* @deprecated use ch_layout.nb_channels
|
|
416
|
+
*/
|
|
417
|
+
get channels() {
|
|
418
|
+
return this.native.channels;
|
|
419
|
+
}
|
|
420
|
+
set channels(value) {
|
|
421
|
+
this.native.channels = value;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Audio sample format.
|
|
425
|
+
* - encoding: Set by user.
|
|
426
|
+
* - decoding: Set by libavcodec.
|
|
427
|
+
*/
|
|
428
|
+
get sampleFormat() {
|
|
429
|
+
return this.native.sampleFormat;
|
|
430
|
+
}
|
|
431
|
+
set sampleFormat(value) {
|
|
432
|
+
this.native.sampleFormat = value;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Number of samples per channel in an audio frame.
|
|
436
|
+
* - encoding: Set by libavcodec in avcodec_open2(). Each submitted frame
|
|
437
|
+
* except the last must contain exactly frame_size samples per channel.
|
|
438
|
+
* May be 0 when the codec has AV_CODEC_CAP_VARIABLE_FRAME_SIZE set, then the
|
|
439
|
+
* frame size is not restricted.
|
|
440
|
+
* - decoding: May be set by some decoders to indicate constant frame size.
|
|
441
|
+
*/
|
|
442
|
+
get frameSize() {
|
|
443
|
+
return this.native.frameSize;
|
|
444
|
+
}
|
|
445
|
+
set frameSize(value) {
|
|
446
|
+
this.native.frameSize = value;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Frame counter, set by libavcodec.
|
|
450
|
+
* - decoding: Total number of frames returned from the decoder so far.
|
|
451
|
+
* - encoding: Total number of frames passed to the encoder so far.
|
|
452
|
+
* @readonly
|
|
453
|
+
*/
|
|
454
|
+
get frameNumber() {
|
|
455
|
+
return this.native.frameNumber;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Audio channel layout.
|
|
459
|
+
* - encoding: Set by user.
|
|
460
|
+
* - decoding: Set by user, may be overwritten by libavcodec.
|
|
461
|
+
* @deprecated use ch_layout
|
|
462
|
+
*/
|
|
463
|
+
get channelLayout() {
|
|
464
|
+
return this.native.channelLayout;
|
|
465
|
+
}
|
|
466
|
+
set channelLayout(value) {
|
|
467
|
+
this.native.channelLayout = value;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Minimum quantizer.
|
|
471
|
+
* - encoding: Set by user.
|
|
472
|
+
* - decoding: unused
|
|
473
|
+
*/
|
|
474
|
+
get qMin() {
|
|
475
|
+
return this.native.qMin;
|
|
476
|
+
}
|
|
477
|
+
set qMin(value) {
|
|
478
|
+
this.native.qMin = value;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Maximum quantizer.
|
|
482
|
+
* - encoding: Set by user.
|
|
483
|
+
* - decoding: unused
|
|
484
|
+
*/
|
|
485
|
+
get qMax() {
|
|
486
|
+
return this.native.qMax;
|
|
487
|
+
}
|
|
488
|
+
set qMax(value) {
|
|
489
|
+
this.native.qMax = value;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Decoder bitstream buffer size.
|
|
493
|
+
* - encoding: Set by user.
|
|
494
|
+
* - decoding: unused
|
|
495
|
+
*/
|
|
496
|
+
get rcBufferSize() {
|
|
497
|
+
return this.native.rcBufferSize;
|
|
498
|
+
}
|
|
499
|
+
set rcBufferSize(value) {
|
|
500
|
+
this.native.rcBufferSize = value;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Maximum bitrate.
|
|
504
|
+
* - encoding: Set by user.
|
|
505
|
+
* - decoding: Set by user, may be overwritten by libavcodec.
|
|
506
|
+
*/
|
|
507
|
+
get rcMaxRate() {
|
|
508
|
+
return this.native.rcMaxRate;
|
|
509
|
+
}
|
|
510
|
+
set rcMaxRate(value) {
|
|
511
|
+
this.native.rcMaxRate = value;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Minimum bitrate.
|
|
515
|
+
* - encoding: Set by user.
|
|
516
|
+
* - decoding: unused
|
|
517
|
+
*/
|
|
518
|
+
get rcMinRate() {
|
|
519
|
+
return this.native.rcMinRate;
|
|
520
|
+
}
|
|
521
|
+
set rcMinRate(value) {
|
|
522
|
+
this.native.rcMinRate = value;
|
|
523
|
+
}
|
|
524
|
+
// Hardware Acceleration
|
|
525
|
+
/**
|
|
526
|
+
* Hardware device context for hardware acceleration.
|
|
527
|
+
*
|
|
528
|
+
* Direct mapping to AVCodecContext->hw_device_ctx
|
|
529
|
+
*
|
|
530
|
+
* If the codec supports hardware acceleration, this should be set
|
|
531
|
+
* to the hardware device context before opening the codec.
|
|
532
|
+
*/
|
|
533
|
+
get hwDeviceCtx() {
|
|
534
|
+
const native = this.native.hwDeviceCtx;
|
|
535
|
+
if (!native) {
|
|
536
|
+
// Clear cache if native is null
|
|
537
|
+
this._hwDeviceCtx = undefined;
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
// Return cached wrapper if available and still valid
|
|
541
|
+
if (this._hwDeviceCtx && this._hwDeviceCtx.native === native) {
|
|
542
|
+
return this._hwDeviceCtx;
|
|
543
|
+
}
|
|
544
|
+
// Create and cache new wrapper
|
|
545
|
+
const device = Object.create(HardwareDeviceContext.prototype);
|
|
546
|
+
device.native = native;
|
|
547
|
+
this._hwDeviceCtx = device;
|
|
548
|
+
return device;
|
|
549
|
+
}
|
|
550
|
+
set hwDeviceCtx(value) {
|
|
551
|
+
this.native.hwDeviceCtx = value?.getNative() ?? null;
|
|
552
|
+
// Clear cache when setting new value
|
|
553
|
+
this._hwDeviceCtx = undefined;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Hardware frames context for hardware acceleration.
|
|
557
|
+
*
|
|
558
|
+
* Direct mapping to AVCodecContext->hw_frames_ctx
|
|
559
|
+
*
|
|
560
|
+
* For decoders, this is an optional field that the decoder can set
|
|
561
|
+
* to provide the caller with hardware frames. For encoders, this
|
|
562
|
+
* must be set by the caller before opening the encoder.
|
|
563
|
+
*/
|
|
564
|
+
get hwFramesCtx() {
|
|
565
|
+
const native = this.native.hwFramesCtx;
|
|
566
|
+
if (!native) {
|
|
567
|
+
// Clear cache if native is null
|
|
568
|
+
this._hwFramesCtx = undefined;
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
// Return cached wrapper if available and still valid
|
|
572
|
+
if (this._hwFramesCtx && this._hwFramesCtx.native === native) {
|
|
573
|
+
return this._hwFramesCtx;
|
|
574
|
+
}
|
|
575
|
+
// Create and cache new wrapper
|
|
576
|
+
const frames = Object.create(HardwareFramesContext.prototype);
|
|
577
|
+
frames.native = native;
|
|
578
|
+
this._hwFramesCtx = frames;
|
|
579
|
+
return frames;
|
|
580
|
+
}
|
|
581
|
+
set hwFramesCtx(value) {
|
|
582
|
+
this.native.hwFramesCtx = value?.getNative() ?? null;
|
|
583
|
+
// Clear cache when setting new value
|
|
584
|
+
this._hwFramesCtx = undefined;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Check if the codec context is open.
|
|
588
|
+
*
|
|
589
|
+
* Direct mapping to avcodec_is_open()
|
|
590
|
+
*
|
|
591
|
+
* @returns true if the codec is open and ready for encoding/decoding
|
|
592
|
+
*/
|
|
593
|
+
get isOpen() {
|
|
594
|
+
return this.native.isOpen;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Allocate an AVCodecContext and set its fields to default values.
|
|
598
|
+
*
|
|
599
|
+
* Allocates the codec context and initializes with codec-specific defaults.
|
|
600
|
+
* Must be called before using the context.
|
|
601
|
+
*
|
|
602
|
+
* Direct mapping to avcodec_alloc_context3()
|
|
603
|
+
*
|
|
604
|
+
* @param codec - If non-NULL, allocate private data and initialize defaults
|
|
605
|
+
* for the given codec. It is illegal to then call open2()
|
|
606
|
+
* with a different codec.
|
|
607
|
+
*
|
|
608
|
+
* @throws {Error} Memory allocation failure (ENOMEM)
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```typescript
|
|
612
|
+
* import { CodecContext, Codec } from 'node-av';
|
|
613
|
+
* import { AV_CODEC_ID_H264 } from 'node-av/constants';
|
|
614
|
+
*
|
|
615
|
+
* const codec = Codec.findDecoder(AV_CODEC_ID_H264);
|
|
616
|
+
* const ctx = new CodecContext();
|
|
617
|
+
* ctx.allocContext3(codec);
|
|
618
|
+
* // Context is now allocated with H264 defaults
|
|
619
|
+
* ```
|
|
620
|
+
*
|
|
621
|
+
* @see {@link open2} To open the codec
|
|
622
|
+
* @see {@link freeContext} To free the context
|
|
623
|
+
*/
|
|
624
|
+
allocContext3(codec = null) {
|
|
625
|
+
this.native.allocContext3(codec?.getNative() ?? null);
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Free the codec context and everything associated with it.
|
|
629
|
+
*
|
|
630
|
+
* Releases all resources associated with the codec context.
|
|
631
|
+
* The context becomes invalid after this call.
|
|
632
|
+
*
|
|
633
|
+
* Direct mapping to avcodec_free_context()
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```typescript
|
|
637
|
+
* ctx.freeContext();
|
|
638
|
+
* // ctx is now invalid and should not be used
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
freeContext() {
|
|
642
|
+
this.native.freeContext();
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Initialize the AVCodecContext to use the given AVCodec.
|
|
646
|
+
*
|
|
647
|
+
* Opens the codec and prepares it for encoding or decoding.
|
|
648
|
+
* Prior to using this function the context has to be allocated with allocContext3().
|
|
649
|
+
*
|
|
650
|
+
* Direct mapping to avcodec_open2()
|
|
651
|
+
*
|
|
652
|
+
* @param codec - The codec to open this context for. If a non-NULL codec has been
|
|
653
|
+
* previously passed to allocContext3() for this context, then this
|
|
654
|
+
* parameter MUST be either NULL or equal to the previously passed codec.
|
|
655
|
+
* @param options - A dictionary filled with AVCodecContext and codec-private options.
|
|
656
|
+
*
|
|
657
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
658
|
+
* - 0: Success
|
|
659
|
+
* - AVERROR(EINVAL): Invalid parameters or codec not found
|
|
660
|
+
* - AVERROR(ENOMEM): Memory allocation failure
|
|
661
|
+
* - <0: Other codec-specific errors
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* import { FFmpegError } from 'node-av';
|
|
666
|
+
*
|
|
667
|
+
* const ret = await ctx.open2(codec, null);
|
|
668
|
+
* FFmpegError.throwIfError(ret, 'open2');
|
|
669
|
+
* ```
|
|
670
|
+
*
|
|
671
|
+
* @see {@link close} To close the codec context
|
|
672
|
+
* @see {@link allocContext3} Must be called before open2()
|
|
673
|
+
*/
|
|
674
|
+
async open2(codec = null, options = null) {
|
|
675
|
+
return await this.native.open2(codec?.getNative() ?? null, options?.getNative() ?? null);
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Fill the codec context based on the values from the supplied codec parameters.
|
|
679
|
+
*
|
|
680
|
+
* Direct mapping to avcodec_parameters_to_context()
|
|
681
|
+
*
|
|
682
|
+
* @param params - Codec parameters to copy from
|
|
683
|
+
*
|
|
684
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
685
|
+
* - 0: Success
|
|
686
|
+
* - AVERROR(EINVAL): Invalid parameters
|
|
687
|
+
* - <0: Other errors
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```typescript
|
|
691
|
+
* // Copy parameters from stream to codec context
|
|
692
|
+
* const ret = ctx.parametersToContext(stream.codecpar);
|
|
693
|
+
* if (ret < 0) {
|
|
694
|
+
* throw new FFmpegError(ret);
|
|
695
|
+
* }
|
|
696
|
+
* ```
|
|
697
|
+
*
|
|
698
|
+
* @see parametersFromContext() - To copy in the opposite direction
|
|
699
|
+
*/
|
|
700
|
+
parametersToContext(params) {
|
|
701
|
+
return this.native.parametersToContext(params.getNative());
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Fill the parameters struct based on the values from the supplied codec context.
|
|
705
|
+
*
|
|
706
|
+
* Direct mapping to avcodec_parameters_from_context()
|
|
707
|
+
*
|
|
708
|
+
* @param params - Codec parameters to fill
|
|
709
|
+
*
|
|
710
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
711
|
+
* - 0: Success
|
|
712
|
+
* - AVERROR(EINVAL): Invalid parameters
|
|
713
|
+
* - <0: Other errors
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```typescript
|
|
717
|
+
* // Copy parameters from codec context to stream
|
|
718
|
+
* const ret = ctx.parametersFromContext(outputStream.codecpar);
|
|
719
|
+
* if (ret < 0) {
|
|
720
|
+
* throw new FFmpegError(ret);
|
|
721
|
+
* }
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
* @see parametersToContext() - To copy in the opposite direction
|
|
725
|
+
*/
|
|
726
|
+
parametersFromContext(params) {
|
|
727
|
+
return this.native.parametersFromContext(params.getNative());
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Reset the internal codec state / flush internal buffers.
|
|
731
|
+
* Should be called when seeking or switching to a different stream.
|
|
732
|
+
*
|
|
733
|
+
* Direct mapping to avcodec_flush_buffers()
|
|
734
|
+
*
|
|
735
|
+
* @example
|
|
736
|
+
* ```typescript
|
|
737
|
+
* // Flush buffers when seeking
|
|
738
|
+
* formatContext.seekFrame(streamIndex, timestamp, flags);
|
|
739
|
+
* codecContext.flushBuffers();
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
flushBuffers() {
|
|
743
|
+
this.native.flushBuffers();
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Supply raw packet data as input to a decoder.
|
|
747
|
+
*
|
|
748
|
+
* Sends compressed data to the decoder for processing.
|
|
749
|
+
* The decoder may buffer the packet internally.
|
|
750
|
+
*
|
|
751
|
+
* Direct mapping to avcodec_send_packet()
|
|
752
|
+
*
|
|
753
|
+
* @param packet - The input packet. May be NULL to signal end of stream (flush).
|
|
754
|
+
*
|
|
755
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
756
|
+
* - 0: Success
|
|
757
|
+
* - AVERROR(EAGAIN): Input not accepted - must read output with receiveFrame() first
|
|
758
|
+
* - AVERROR_EOF: Decoder has been flushed, no new packets can be sent
|
|
759
|
+
* - AVERROR(EINVAL): Codec not opened, is an encoder, or requires flush
|
|
760
|
+
* - AVERROR(ENOMEM): Failed to add packet to internal queue
|
|
761
|
+
* - <0: Other legitimate decoding errors
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```typescript
|
|
765
|
+
* import { FFmpegError, Frame } from 'node-av';
|
|
766
|
+
* import { AVERROR_EAGAIN } from 'node-av/constants';
|
|
767
|
+
*
|
|
768
|
+
* // Decode packet
|
|
769
|
+
* const ret = await decoder.sendPacket(packet);
|
|
770
|
+
* if (ret === AVERROR_EAGAIN) {
|
|
771
|
+
* // Need to read output first
|
|
772
|
+
* const frame = new Frame();
|
|
773
|
+
* frame.alloc();
|
|
774
|
+
* const recvRet = await decoder.receiveFrame(frame);
|
|
775
|
+
* FFmpegError.throwIfError(recvRet, 'receiveFrame');
|
|
776
|
+
* } else {
|
|
777
|
+
* FFmpegError.throwIfError(ret, 'sendPacket');
|
|
778
|
+
* }
|
|
779
|
+
* ```
|
|
780
|
+
*
|
|
781
|
+
* @see {@link receiveFrame} To retrieve decoded frames
|
|
782
|
+
*/
|
|
783
|
+
async sendPacket(packet) {
|
|
784
|
+
return await this.native.sendPacket(packet?.getNative() ?? null);
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Return decoded output data from a decoder.
|
|
788
|
+
*
|
|
789
|
+
* Retrieves decoded frames from the decoder.
|
|
790
|
+
* The frame must be allocated before calling this function.
|
|
791
|
+
*
|
|
792
|
+
* Direct mapping to avcodec_receive_frame()
|
|
793
|
+
*
|
|
794
|
+
* @param frame - Frame to receive decoded data. Must be allocated.
|
|
795
|
+
*
|
|
796
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
797
|
+
* - 0: Success, a frame was returned
|
|
798
|
+
* - AVERROR(EAGAIN): Output not available, must send new input
|
|
799
|
+
* - AVERROR_EOF: Decoder fully flushed, no more frames
|
|
800
|
+
* - AVERROR(EINVAL): Codec not opened or is an encoder
|
|
801
|
+
* - <0: Other legitimate decoding errors
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* ```typescript
|
|
805
|
+
* import { Frame, FFmpegError } from 'node-av';
|
|
806
|
+
* import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av/constants';
|
|
807
|
+
*
|
|
808
|
+
* // Receive all frames from decoder
|
|
809
|
+
* const frame = new Frame();
|
|
810
|
+
* frame.alloc();
|
|
811
|
+
*
|
|
812
|
+
* while (true) {
|
|
813
|
+
* const ret = await decoder.receiveFrame(frame);
|
|
814
|
+
* if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
|
|
815
|
+
* break;
|
|
816
|
+
* }
|
|
817
|
+
* FFmpegError.throwIfError(ret, 'receiveFrame');
|
|
818
|
+
*
|
|
819
|
+
* // Process frame
|
|
820
|
+
* processFrame(frame);
|
|
821
|
+
* frame.unref();
|
|
822
|
+
* }
|
|
823
|
+
* ```
|
|
824
|
+
*
|
|
825
|
+
* @see {@link sendPacket} To send input packets
|
|
826
|
+
*/
|
|
827
|
+
async receiveFrame(frame) {
|
|
828
|
+
return await this.native.receiveFrame(frame.getNative());
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Supply a raw video or audio frame to the encoder.
|
|
832
|
+
*
|
|
833
|
+
* Sends uncompressed frame data to the encoder.
|
|
834
|
+
* Use receivePacket() to retrieve buffered output packets.
|
|
835
|
+
*
|
|
836
|
+
* Direct mapping to avcodec_send_frame()
|
|
837
|
+
*
|
|
838
|
+
* @param frame - AVFrame containing the raw audio or video frame to be encoded.
|
|
839
|
+
* Can be NULL for flush packet (signals end of stream).
|
|
840
|
+
*
|
|
841
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
842
|
+
* - 0: Success
|
|
843
|
+
* - AVERROR(EAGAIN): Input not accepted - must read output with receivePacket()
|
|
844
|
+
* - AVERROR_EOF: Encoder has been flushed, no new frames can be sent
|
|
845
|
+
* - AVERROR(EINVAL): Codec not opened, is a decoder, or requires flush
|
|
846
|
+
* - AVERROR(ENOMEM): Failed to add packet to internal queue
|
|
847
|
+
* - <0: Other legitimate encoding errors
|
|
848
|
+
*
|
|
849
|
+
* @example
|
|
850
|
+
* ```typescript
|
|
851
|
+
* import { Packet, FFmpegError } from 'node-av';
|
|
852
|
+
* import { AVERROR_EAGAIN } from 'node-av/constants';
|
|
853
|
+
*
|
|
854
|
+
* // Send frame to encoder
|
|
855
|
+
* const ret = await encoder.sendFrame(frame);
|
|
856
|
+
* if (ret === AVERROR_EAGAIN) {
|
|
857
|
+
* // Need to read output first
|
|
858
|
+
* const packet = new Packet();
|
|
859
|
+
* packet.alloc();
|
|
860
|
+
* const recvRet = await encoder.receivePacket(packet);
|
|
861
|
+
* FFmpegError.throwIfError(recvRet, 'receivePacket');
|
|
862
|
+
* } else {
|
|
863
|
+
* FFmpegError.throwIfError(ret, 'sendFrame');
|
|
864
|
+
* }
|
|
865
|
+
* ```
|
|
866
|
+
*
|
|
867
|
+
* @see {@link receivePacket} To retrieve encoded packets
|
|
868
|
+
*
|
|
869
|
+
* @note For audio: If AV_CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame
|
|
870
|
+
* can have any number of samples. If not set, frame.nbSamples must equal
|
|
871
|
+
* avctx.frameSize for all frames except the last.
|
|
872
|
+
*/
|
|
873
|
+
async sendFrame(frame) {
|
|
874
|
+
return await this.native.sendFrame(frame?.getNative() ?? null);
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Read encoded data from the encoder.
|
|
878
|
+
*
|
|
879
|
+
* Retrieves compressed packets from the encoder.
|
|
880
|
+
* The packet must be allocated before calling.
|
|
881
|
+
*
|
|
882
|
+
* Direct mapping to avcodec_receive_packet()
|
|
883
|
+
*
|
|
884
|
+
* @param packet - Packet to receive encoded data. Must be allocated.
|
|
885
|
+
*
|
|
886
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
887
|
+
* - 0: Success, a packet was returned
|
|
888
|
+
* - AVERROR(EAGAIN): Output not available, must send new input
|
|
889
|
+
* - AVERROR_EOF: Encoder fully flushed, no more packets
|
|
890
|
+
* - AVERROR(EINVAL): Codec not opened or is a decoder
|
|
891
|
+
* - <0: Other legitimate encoding errors
|
|
892
|
+
*
|
|
893
|
+
* @example
|
|
894
|
+
* ```typescript
|
|
895
|
+
* import { Packet, FFmpegError } from 'node-av';
|
|
896
|
+
* import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av/constants';
|
|
897
|
+
*
|
|
898
|
+
* // Receive all packets from encoder
|
|
899
|
+
* const packet = new Packet();
|
|
900
|
+
* packet.alloc();
|
|
901
|
+
*
|
|
902
|
+
* while (true) {
|
|
903
|
+
* const ret = await encoder.receivePacket(packet);
|
|
904
|
+
* if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
|
|
905
|
+
* break;
|
|
906
|
+
* }
|
|
907
|
+
* if (ret < 0) {
|
|
908
|
+
* throw new FFmpegError(ret);
|
|
909
|
+
* }
|
|
910
|
+
* // Write packet to output
|
|
911
|
+
* await formatContext.writeFrame(packet);
|
|
912
|
+
* packet.unref();
|
|
913
|
+
* }
|
|
914
|
+
* ```
|
|
915
|
+
*
|
|
916
|
+
* @see sendFrame() - To send input frames
|
|
917
|
+
*/
|
|
918
|
+
async receivePacket(packet) {
|
|
919
|
+
return await this.native.receivePacket(packet.getNative());
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* Set the hardware pixel format for hardware acceleration.
|
|
923
|
+
*
|
|
924
|
+
* This configures the codec context to prefer a specific hardware pixel format
|
|
925
|
+
* when negotiating formats with FFmpeg. Optionally, a software fallback format
|
|
926
|
+
* can be specified.
|
|
927
|
+
*
|
|
928
|
+
* @param hwFormat - The preferred hardware pixel format (e.g., AV_PIX_FMT_VIDEOTOOLBOX)
|
|
929
|
+
* @param swFormat - Optional software fallback format if hardware format is not available
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* ```typescript
|
|
933
|
+
* // For VideoToolbox hardware decoding on macOS
|
|
934
|
+
* codecContext.setHardwarePixelFormat(AV_PIX_FMT_VIDEOTOOLBOX);
|
|
935
|
+
*
|
|
936
|
+
* // With software fallback
|
|
937
|
+
* codecContext.setHardwarePixelFormat(
|
|
938
|
+
* AV_PIX_FMT_VIDEOTOOLBOX,
|
|
939
|
+
* AV_PIX_FMT_YUV420P // Fallback to YUV420P if hardware not available
|
|
940
|
+
* );
|
|
941
|
+
* ```
|
|
942
|
+
*/
|
|
943
|
+
setHardwarePixelFormat(hwFormat, swFormat) {
|
|
944
|
+
this.native.setHardwarePixelFormat(hwFormat, swFormat);
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* Get the native FFmpeg AVCodecContext pointer.
|
|
948
|
+
*
|
|
949
|
+
* @internal For use by other wrapper classes
|
|
950
|
+
* @returns The underlying native codec context object
|
|
951
|
+
*/
|
|
952
|
+
getNative() {
|
|
953
|
+
return this.native;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Dispose of the codec context.
|
|
957
|
+
*
|
|
958
|
+
* Implements the Disposable interface for automatic cleanup.
|
|
959
|
+
* Equivalent to calling freeContext().
|
|
960
|
+
*
|
|
961
|
+
* @example
|
|
962
|
+
* ```typescript
|
|
963
|
+
* {
|
|
964
|
+
* using ctx = new CodecContext();
|
|
965
|
+
* ctx.allocContext3(codec);
|
|
966
|
+
* // ... use context
|
|
967
|
+
* } // Automatically freed when leaving scope
|
|
968
|
+
* ```
|
|
969
|
+
*/
|
|
970
|
+
[Symbol.dispose]() {
|
|
971
|
+
this.native[Symbol.dispose]();
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
//# sourceMappingURL=codec-context.js.map
|