node-av 1.0.3 → 1.2.0

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.
Files changed (162) hide show
  1. package/README.md +56 -41
  2. package/dist/api/bitstream-filter.d.ts +180 -123
  3. package/dist/api/bitstream-filter.js +182 -126
  4. package/dist/api/bitstream-filter.js.map +1 -1
  5. package/dist/api/decoder.d.ts +286 -130
  6. package/dist/api/decoder.js +321 -159
  7. package/dist/api/decoder.js.map +1 -1
  8. package/dist/api/encoder.d.ts +254 -158
  9. package/dist/api/encoder.js +326 -298
  10. package/dist/api/encoder.js.map +1 -1
  11. package/dist/api/filter-presets.d.ts +912 -0
  12. package/dist/api/filter-presets.js +1407 -0
  13. package/dist/api/filter-presets.js.map +1 -0
  14. package/dist/api/filter.d.ts +280 -284
  15. package/dist/api/filter.js +435 -509
  16. package/dist/api/filter.js.map +1 -1
  17. package/dist/api/hardware.d.ts +226 -159
  18. package/dist/api/hardware.js +405 -287
  19. package/dist/api/hardware.js.map +1 -1
  20. package/dist/api/index.d.ts +3 -2
  21. package/dist/api/index.js +1 -0
  22. package/dist/api/index.js.map +1 -1
  23. package/dist/api/io-stream.d.ts +65 -61
  24. package/dist/api/io-stream.js +45 -47
  25. package/dist/api/io-stream.js.map +1 -1
  26. package/dist/api/media-input.d.ts +244 -141
  27. package/dist/api/media-input.js +207 -104
  28. package/dist/api/media-input.js.map +1 -1
  29. package/dist/api/media-output.d.ts +206 -128
  30. package/dist/api/media-output.js +212 -129
  31. package/dist/api/media-output.js.map +1 -1
  32. package/dist/api/pipeline.d.ts +168 -38
  33. package/dist/api/pipeline.js +238 -14
  34. package/dist/api/pipeline.js.map +1 -1
  35. package/dist/api/types.d.ts +22 -182
  36. package/dist/api/utilities/audio-sample.d.ts +1 -1
  37. package/dist/api/utilities/image.d.ts +1 -1
  38. package/dist/api/utilities/media-type.d.ts +1 -1
  39. package/dist/api/utilities/pixel-format.d.ts +1 -1
  40. package/dist/api/utilities/sample-format.d.ts +1 -1
  41. package/dist/api/utilities/timestamp.d.ts +1 -1
  42. package/dist/api/utils.d.ts +1 -2
  43. package/dist/api/utils.js +9 -0
  44. package/dist/api/utils.js.map +1 -1
  45. package/dist/{lib → constants}/channel-layouts.d.ts +1 -1
  46. package/dist/constants/channel-layouts.js.map +1 -0
  47. package/dist/{lib → constants}/constants.d.ts +19 -4
  48. package/dist/{lib → constants}/constants.js +15 -1
  49. package/dist/constants/constants.js.map +1 -0
  50. package/dist/constants/decoders.d.ts +609 -0
  51. package/dist/constants/decoders.js +617 -0
  52. package/dist/constants/decoders.js.map +1 -0
  53. package/dist/constants/encoders.d.ts +285 -0
  54. package/dist/constants/encoders.js +298 -0
  55. package/dist/constants/encoders.js.map +1 -0
  56. package/dist/constants/index.d.ts +4 -0
  57. package/dist/constants/index.js +5 -0
  58. package/dist/constants/index.js.map +1 -0
  59. package/dist/index.d.ts +1 -0
  60. package/dist/index.js +2 -0
  61. package/dist/index.js.map +1 -1
  62. package/dist/lib/audio-fifo.d.ts +128 -171
  63. package/dist/lib/audio-fifo.js +130 -173
  64. package/dist/lib/audio-fifo.js.map +1 -1
  65. package/dist/lib/binding.d.ts +7 -5
  66. package/dist/lib/binding.js +5 -0
  67. package/dist/lib/binding.js.map +1 -1
  68. package/dist/lib/bitstream-filter-context.d.ts +139 -184
  69. package/dist/lib/bitstream-filter-context.js +139 -188
  70. package/dist/lib/bitstream-filter-context.js.map +1 -1
  71. package/dist/lib/bitstream-filter.d.ts +69 -55
  72. package/dist/lib/bitstream-filter.js +68 -54
  73. package/dist/lib/bitstream-filter.js.map +1 -1
  74. package/dist/lib/codec-context.d.ts +317 -381
  75. package/dist/lib/codec-context.js +316 -381
  76. package/dist/lib/codec-context.js.map +1 -1
  77. package/dist/lib/codec-parameters.d.ts +161 -171
  78. package/dist/lib/codec-parameters.js +162 -172
  79. package/dist/lib/codec-parameters.js.map +1 -1
  80. package/dist/lib/codec-parser.d.ts +92 -105
  81. package/dist/lib/codec-parser.js +92 -103
  82. package/dist/lib/codec-parser.js.map +1 -1
  83. package/dist/lib/codec.d.ts +328 -217
  84. package/dist/lib/codec.js +392 -218
  85. package/dist/lib/codec.js.map +1 -1
  86. package/dist/lib/dictionary.d.ts +150 -204
  87. package/dist/lib/dictionary.js +159 -213
  88. package/dist/lib/dictionary.js.map +1 -1
  89. package/dist/lib/error.d.ts +97 -131
  90. package/dist/lib/error.js +98 -128
  91. package/dist/lib/error.js.map +1 -1
  92. package/dist/lib/filter-context.d.ts +317 -194
  93. package/dist/lib/filter-context.js +335 -200
  94. package/dist/lib/filter-context.js.map +1 -1
  95. package/dist/lib/filter-graph.d.ts +252 -293
  96. package/dist/lib/filter-graph.js +253 -294
  97. package/dist/lib/filter-graph.js.map +1 -1
  98. package/dist/lib/filter-inout.d.ts +87 -95
  99. package/dist/lib/filter-inout.js +87 -95
  100. package/dist/lib/filter-inout.js.map +1 -1
  101. package/dist/lib/filter.d.ts +93 -111
  102. package/dist/lib/filter.js +94 -112
  103. package/dist/lib/filter.js.map +1 -1
  104. package/dist/lib/format-context.d.ts +321 -429
  105. package/dist/lib/format-context.js +314 -386
  106. package/dist/lib/format-context.js.map +1 -1
  107. package/dist/lib/frame.d.ts +263 -406
  108. package/dist/lib/frame.js +263 -408
  109. package/dist/lib/frame.js.map +1 -1
  110. package/dist/lib/hardware-device-context.d.ts +150 -204
  111. package/dist/lib/hardware-device-context.js +149 -203
  112. package/dist/lib/hardware-device-context.js.map +1 -1
  113. package/dist/lib/hardware-frames-context.d.ts +171 -181
  114. package/dist/lib/hardware-frames-context.js +171 -181
  115. package/dist/lib/hardware-frames-context.js.map +1 -1
  116. package/dist/lib/index.d.ts +2 -3
  117. package/dist/lib/index.js +2 -5
  118. package/dist/lib/index.js.map +1 -1
  119. package/dist/lib/input-format.d.ts +90 -118
  120. package/dist/lib/input-format.js +89 -117
  121. package/dist/lib/input-format.js.map +1 -1
  122. package/dist/lib/io-context.d.ts +210 -242
  123. package/dist/lib/io-context.js +221 -253
  124. package/dist/lib/io-context.js.map +1 -1
  125. package/dist/lib/log.d.ts +86 -120
  126. package/dist/lib/log.js +85 -122
  127. package/dist/lib/log.js.map +1 -1
  128. package/dist/lib/native-types.d.ts +127 -112
  129. package/dist/lib/native-types.js +9 -0
  130. package/dist/lib/native-types.js.map +1 -1
  131. package/dist/lib/option.d.ts +285 -242
  132. package/dist/lib/option.js +310 -250
  133. package/dist/lib/option.js.map +1 -1
  134. package/dist/lib/output-format.d.ts +78 -102
  135. package/dist/lib/output-format.js +77 -101
  136. package/dist/lib/output-format.js.map +1 -1
  137. package/dist/lib/packet.d.ts +173 -241
  138. package/dist/lib/packet.js +172 -241
  139. package/dist/lib/packet.js.map +1 -1
  140. package/dist/lib/rational.d.ts +0 -2
  141. package/dist/lib/rational.js +0 -2
  142. package/dist/lib/rational.js.map +1 -1
  143. package/dist/lib/software-resample-context.d.ts +242 -326
  144. package/dist/lib/software-resample-context.js +242 -326
  145. package/dist/lib/software-resample-context.js.map +1 -1
  146. package/dist/lib/software-scale-context.d.ts +130 -174
  147. package/dist/lib/software-scale-context.js +132 -176
  148. package/dist/lib/software-scale-context.js.map +1 -1
  149. package/dist/lib/stream.d.ts +88 -198
  150. package/dist/lib/stream.js +87 -197
  151. package/dist/lib/stream.js.map +1 -1
  152. package/dist/lib/types.d.ts +1 -1
  153. package/dist/lib/utilities.d.ts +372 -181
  154. package/dist/lib/utilities.js +373 -182
  155. package/dist/lib/utilities.js.map +1 -1
  156. package/install/check.js +0 -1
  157. package/package.json +32 -24
  158. package/release_notes.md +43 -13
  159. package/CHANGELOG.md +0 -8
  160. package/dist/lib/channel-layouts.js.map +0 -1
  161. package/dist/lib/constants.js.map +0 -1
  162. /package/dist/{lib → constants}/channel-layouts.js +0 -0
@@ -1,185 +1,172 @@
1
+ import type { AVCodecID } from '../constants/constants.js';
1
2
  import type { CodecContext } from './codec-context.js';
2
- import type { AVCodecID } from './constants.js';
3
3
  import type { NativeCodecParser, NativeWrapper } from './native-types.js';
4
4
  import type { Packet } from './packet.js';
5
5
  /**
6
- * Codec parser for splitting elementary streams into frames.
6
+ * Parser for extracting codec frames from raw bitstream data.
7
7
  *
8
- * Parsers split raw byte streams (currently only video) into coded frames.
9
- * Many decoders require that coded video frames are preceded by a start code,
10
- * and this parser splits the stream at these boundaries. Parsers are essential
11
- * for handling raw elementary streams from sources like network streams or
12
- * raw video files without container format.
8
+ * Analyzes and splits raw bitstream data into individual codec frames.
9
+ * Essential for processing elementary streams, extracting NAL units,
10
+ * and handling frame boundaries in raw codec data. Commonly used for
11
+ * parsing H.264/H.265 streams, AAC ADTS streams, and other raw formats.
13
12
  *
14
- * Direct wrapper around AVCodecParserContext.
13
+ * Direct mapping to FFmpeg's AVCodecParserContext.
15
14
  *
16
15
  * @example
17
16
  * ```typescript
18
17
  * import { CodecParser, CodecContext, Packet, FFmpegError } from 'node-av';
19
- * import { AV_CODEC_ID_H264, AV_NOPTS_VALUE } from 'node-av/constants';
18
+ * import { AV_CODEC_ID_H264 } from 'node-av/constants';
20
19
  *
20
+ * // Create and initialize parser
21
21
  * const parser = new CodecParser();
22
22
  * parser.init(AV_CODEC_ID_H264);
23
23
  *
24
- * // Parse raw H.264 stream
24
+ * // Parse raw data into packets
25
25
  * const packet = new Packet();
26
26
  * packet.alloc();
27
27
  *
28
- * const bytesConsumed = parser.parse2(
28
+ * const rawData = Buffer.from([...]); // Raw H.264 stream
29
+ * const ret = parser.parse2(
29
30
  * codecContext,
30
31
  * packet,
31
- * inputBuffer,
32
- * AV_NOPTS_VALUE,
33
- * AV_NOPTS_VALUE,
34
- * 0
32
+ * rawData,
33
+ * 0n, // pts
34
+ * 0n, // dts
35
+ * 0 // position
35
36
  * );
36
37
  *
37
- * if (packet.size > 0) {
38
- * // We have a complete frame in packet
39
- * const ret = await codecContext.sendPacket(packet);
40
- * FFmpegError.throwIfError(ret, 'sendPacket');
38
+ * if (ret > 0) {
39
+ * // Got complete packet, ret is consumed bytes
40
+ * console.log(`Parsed ${ret} bytes into packet`);
41
41
  * }
42
42
  *
43
+ * // Cleanup
43
44
  * parser.close();
44
45
  * ```
45
46
  *
47
+ * @see [AVCodecParserContext](https://ffmpeg.org/doxygen/trunk/structAVCodecParserContext.html) - FFmpeg Doxygen
46
48
  * @see {@link CodecContext} For decoding parsed packets
47
- * @see {@link Packet} For storing parsed frame data
48
49
  */
49
- export declare class CodecParser implements NativeWrapper<NativeCodecParser> {
50
+ export declare class CodecParser implements Disposable, NativeWrapper<NativeCodecParser> {
50
51
  private native;
51
- /**
52
- * Create a new CodecParser instance.
53
- *
54
- * The parser is uninitialized - you must call init() before use.
55
- * No FFmpeg resources are allocated until init() is called.
56
- *
57
- * Direct wrapper around AVCodecParserContext allocation.
58
- *
59
- * @example
60
- * ```typescript
61
- * import { CodecParser } from 'node-av';
62
- * import { AV_CODEC_ID_MPEG1VIDEO } from 'node-av/constants';
63
- *
64
- * const parser = new CodecParser();
65
- * parser.init(AV_CODEC_ID_MPEG1VIDEO);
66
- * // parser is now ready for use
67
- * ```
68
- */
69
52
  constructor();
70
53
  /**
71
- * Initialize the parser with a specific codec ID.
54
+ * Initialize parser for specific codec.
72
55
  *
73
- * Allocates and initializes the AVCodecParserContext for the specified codec.
74
- * Must be called before parse2() can be used.
56
+ * Sets up the parser to handle a specific codec format.
57
+ * Must be called before parsing data.
75
58
  *
76
- * Direct mapping to av_parser_init()
59
+ * Direct mapping to av_parser_init().
77
60
  *
78
- * @param codecId - AVCodecID of the codec to parse
61
+ * @param codecId - Codec ID to parse
79
62
  *
80
- * @throws {Error} Parser for codec ID not found
63
+ * @throws {Error} If codec parser not available
81
64
  *
82
65
  * @example
83
66
  * ```typescript
84
- * import { CodecParser } from 'node-av';
85
- * import { AV_CODEC_ID_H264, AV_CODEC_ID_HEVC } from 'node-av/constants';
67
+ * import { AV_CODEC_ID_AAC } from 'node-av/constants';
86
68
  *
87
69
  * const parser = new CodecParser();
88
- *
89
- * // Initialize for H.264
90
- * parser.init(AV_CODEC_ID_H264);
91
- * // Parser is now ready to parse H.264 streams
92
- *
93
- * // For HEVC/H.265
94
- * const hevcParser = new CodecParser();
95
- * hevcParser.init(AV_CODEC_ID_HEVC);
70
+ * parser.init(AV_CODEC_ID_AAC);
71
+ * // Parser ready for AAC ADTS streams
96
72
  * ```
97
73
  *
98
- * @see {@link parse2} For parsing data after initialization
74
+ * @see {@link parse2} To parse data
75
+ * @see {@link close} To cleanup
99
76
  */
100
77
  init(codecId: AVCodecID): void;
101
78
  /**
102
- * Parse a buffer and extract packets from elementary stream.
79
+ * Parse bitstream data into packets.
103
80
  *
104
- * Parses the input buffer and extracts complete coded frames. The parser
105
- * maintains internal state to handle partial frames across multiple calls.
106
- * The parser may combine multiple frames into one packet or split one frame
107
- * into multiple packets, depending on the codec and stream format.
81
+ * Analyzes raw bitstream data and extracts complete codec frames.
82
+ * May require multiple calls to accumulate enough data for a complete frame.
83
+ * Returns the number of bytes consumed from the input buffer.
108
84
  *
109
- * Direct mapping to av_parser_parse2()
85
+ * Direct mapping to av_parser_parse2().
110
86
  *
111
- * @param codecContext - Codec context (used for stream parameters)
112
- * @param packet - Packet to fill with parsed data
113
- * @param data - Input buffer containing elementary stream data
114
- * @param pts - Presentation timestamp of the first byte in data
115
- * @param dts - Decoding timestamp of the first byte in data
116
- * @param pos - Byte position of the first byte in data in the stream
117
- *
118
- * @returns Number of bytes consumed from the input buffer:
119
- * - >0: Number of bytes consumed from input
120
- * - 0: More data needed to complete a frame
87
+ * @param codecContext - Codec context for parser state
88
+ * @param packet - Packet to receive parsed frame
89
+ * @param data - Raw bitstream data to parse
90
+ * @param pts - Presentation timestamp for data
91
+ * @param dts - Decoding timestamp for data
92
+ * @param pos - Byte position in stream
93
+ * @returns Number of bytes consumed from data, negative on error:
94
+ * - AVERROR_EINVAL: Invalid parameters
95
+ * - AVERROR_ENOMEM: Memory allocation failure
121
96
  *
122
97
  * @example
123
98
  * ```typescript
124
- * import { CodecParser, CodecContext, Packet, FFmpegError } from 'node-av';
125
- * import { AV_NOPTS_VALUE } from 'node-av/constants';
126
- * import * as fs from 'fs';
127
- *
128
- * const inbuf = Buffer.alloc(4096);
129
- * const bytesRead = fs.readSync(fd, inbuf, 0, 4096, null);
99
+ * import { FFmpegError } from 'node-av';
130
100
  *
131
101
  * let offset = 0;
132
- * while (offset < bytesRead) {
133
- * const consumed = parser.parse2(
102
+ * while (offset < rawData.length) {
103
+ * const remaining = rawData.subarray(offset);
104
+ * const ret = parser.parse2(
134
105
  * codecContext,
135
106
  * packet,
136
- * inbuf.subarray(offset),
137
- * AV_NOPTS_VALUE,
138
- * AV_NOPTS_VALUE,
139
- * 0
107
+ * remaining,
108
+ * pts,
109
+ * dts,
110
+ * offset
140
111
  * );
141
112
  *
142
- * offset += consumed;
113
+ * if (ret < 0) {
114
+ * FFmpegError.throwIfError(ret, 'parse2');
115
+ * }
116
+ *
117
+ * offset += ret;
143
118
  *
144
119
  * if (packet.size > 0) {
145
- * // Complete packet ready for decoding
146
- * const ret = await codecContext.sendPacket(packet);
147
- * FFmpegError.throwIfError(ret, 'sendPacket');
120
+ * // Got complete packet
121
+ * await processPacket(packet);
122
+ * packet.unref();
148
123
  * }
149
124
  * }
150
125
  * ```
151
126
  *
152
- * @see {@link init} Must be called before parse2
153
- * @see {@link close} Should be called when done parsing
127
+ * @see {@link init} To initialize parser
154
128
  */
155
129
  parse2(codecContext: CodecContext, packet: Packet, data: Buffer, pts: bigint, dts: bigint, pos: number): number;
156
130
  /**
157
- * Close the parser and free all resources.
131
+ * Close the codec parser.
158
132
  *
159
- * Releases all resources associated with the parser context.
160
- * After calling close(), the parser instance should not be used anymore.
133
+ * Releases all resources associated with the parser.
134
+ * The parser becomes invalid after calling this.
161
135
  *
162
- * Direct mapping to av_parser_close()
136
+ * Direct mapping to av_parser_close().
163
137
  *
164
138
  * @example
165
139
  * ```typescript
166
- * import { CodecParser } from 'node-av';
167
- *
168
- * const parser = new CodecParser();
169
- * // ... use parser ...
170
- *
171
140
  * parser.close();
172
- * // Parser resources are now freed
173
- * // Do not use parser after this point
141
+ * // Parser is now invalid
174
142
  * ```
143
+ *
144
+ * @see {@link init} To initialize
145
+ * @see {@link Symbol.dispose} For automatic cleanup
175
146
  */
176
147
  close(): void;
177
148
  /**
178
- * @internal
179
- * Get the underlying native codec parser object.
180
- * This method is for internal use by other FFmpeg classes.
149
+ * Get the underlying native CodecParser object.
181
150
  *
182
- * @returns The underlying native codec parser object
151
+ * @returns The native CodecParser binding object
152
+ *
153
+ * @internal
183
154
  */
184
155
  getNative(): NativeCodecParser;
156
+ /**
157
+ * Dispose of the codec parser.
158
+ *
159
+ * Implements the Disposable interface for automatic cleanup.
160
+ * Equivalent to calling close().
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * {
165
+ * using parser = new CodecParser();
166
+ * parser.init(AV_CODEC_ID_H264);
167
+ * // Use parser...
168
+ * } // Automatically closed when leaving scope
169
+ * ```
170
+ */
171
+ [Symbol.dispose](): void;
185
172
  }
@@ -1,193 +1,182 @@
1
1
  import { bindings } from './binding.js';
2
2
  /**
3
- * Codec parser for splitting elementary streams into frames.
3
+ * Parser for extracting codec frames from raw bitstream data.
4
4
  *
5
- * Parsers split raw byte streams (currently only video) into coded frames.
6
- * Many decoders require that coded video frames are preceded by a start code,
7
- * and this parser splits the stream at these boundaries. Parsers are essential
8
- * for handling raw elementary streams from sources like network streams or
9
- * raw video files without container format.
5
+ * Analyzes and splits raw bitstream data into individual codec frames.
6
+ * Essential for processing elementary streams, extracting NAL units,
7
+ * and handling frame boundaries in raw codec data. Commonly used for
8
+ * parsing H.264/H.265 streams, AAC ADTS streams, and other raw formats.
10
9
  *
11
- * Direct wrapper around AVCodecParserContext.
10
+ * Direct mapping to FFmpeg's AVCodecParserContext.
12
11
  *
13
12
  * @example
14
13
  * ```typescript
15
14
  * import { CodecParser, CodecContext, Packet, FFmpegError } from 'node-av';
16
- * import { AV_CODEC_ID_H264, AV_NOPTS_VALUE } from 'node-av/constants';
15
+ * import { AV_CODEC_ID_H264 } from 'node-av/constants';
17
16
  *
17
+ * // Create and initialize parser
18
18
  * const parser = new CodecParser();
19
19
  * parser.init(AV_CODEC_ID_H264);
20
20
  *
21
- * // Parse raw H.264 stream
21
+ * // Parse raw data into packets
22
22
  * const packet = new Packet();
23
23
  * packet.alloc();
24
24
  *
25
- * const bytesConsumed = parser.parse2(
25
+ * const rawData = Buffer.from([...]); // Raw H.264 stream
26
+ * const ret = parser.parse2(
26
27
  * codecContext,
27
28
  * packet,
28
- * inputBuffer,
29
- * AV_NOPTS_VALUE,
30
- * AV_NOPTS_VALUE,
31
- * 0
29
+ * rawData,
30
+ * 0n, // pts
31
+ * 0n, // dts
32
+ * 0 // position
32
33
  * );
33
34
  *
34
- * if (packet.size > 0) {
35
- * // We have a complete frame in packet
36
- * const ret = await codecContext.sendPacket(packet);
37
- * FFmpegError.throwIfError(ret, 'sendPacket');
35
+ * if (ret > 0) {
36
+ * // Got complete packet, ret is consumed bytes
37
+ * console.log(`Parsed ${ret} bytes into packet`);
38
38
  * }
39
39
  *
40
+ * // Cleanup
40
41
  * parser.close();
41
42
  * ```
42
43
  *
44
+ * @see [AVCodecParserContext](https://ffmpeg.org/doxygen/trunk/structAVCodecParserContext.html) - FFmpeg Doxygen
43
45
  * @see {@link CodecContext} For decoding parsed packets
44
- * @see {@link Packet} For storing parsed frame data
45
46
  */
46
47
  export class CodecParser {
47
48
  native;
48
- /**
49
- * Create a new CodecParser instance.
50
- *
51
- * The parser is uninitialized - you must call init() before use.
52
- * No FFmpeg resources are allocated until init() is called.
53
- *
54
- * Direct wrapper around AVCodecParserContext allocation.
55
- *
56
- * @example
57
- * ```typescript
58
- * import { CodecParser } from 'node-av';
59
- * import { AV_CODEC_ID_MPEG1VIDEO } from 'node-av/constants';
60
- *
61
- * const parser = new CodecParser();
62
- * parser.init(AV_CODEC_ID_MPEG1VIDEO);
63
- * // parser is now ready for use
64
- * ```
65
- */
66
49
  constructor() {
67
50
  this.native = new bindings.CodecParser();
68
51
  }
69
52
  /**
70
- * Initialize the parser with a specific codec ID.
53
+ * Initialize parser for specific codec.
71
54
  *
72
- * Allocates and initializes the AVCodecParserContext for the specified codec.
73
- * Must be called before parse2() can be used.
55
+ * Sets up the parser to handle a specific codec format.
56
+ * Must be called before parsing data.
74
57
  *
75
- * Direct mapping to av_parser_init()
58
+ * Direct mapping to av_parser_init().
76
59
  *
77
- * @param codecId - AVCodecID of the codec to parse
60
+ * @param codecId - Codec ID to parse
78
61
  *
79
- * @throws {Error} Parser for codec ID not found
62
+ * @throws {Error} If codec parser not available
80
63
  *
81
64
  * @example
82
65
  * ```typescript
83
- * import { CodecParser } from 'node-av';
84
- * import { AV_CODEC_ID_H264, AV_CODEC_ID_HEVC } from 'node-av/constants';
66
+ * import { AV_CODEC_ID_AAC } from 'node-av/constants';
85
67
  *
86
68
  * const parser = new CodecParser();
87
- *
88
- * // Initialize for H.264
89
- * parser.init(AV_CODEC_ID_H264);
90
- * // Parser is now ready to parse H.264 streams
91
- *
92
- * // For HEVC/H.265
93
- * const hevcParser = new CodecParser();
94
- * hevcParser.init(AV_CODEC_ID_HEVC);
69
+ * parser.init(AV_CODEC_ID_AAC);
70
+ * // Parser ready for AAC ADTS streams
95
71
  * ```
96
72
  *
97
- * @see {@link parse2} For parsing data after initialization
73
+ * @see {@link parse2} To parse data
74
+ * @see {@link close} To cleanup
98
75
  */
99
76
  init(codecId) {
100
77
  this.native.init(codecId);
101
78
  }
102
79
  /**
103
- * Parse a buffer and extract packets from elementary stream.
80
+ * Parse bitstream data into packets.
104
81
  *
105
- * Parses the input buffer and extracts complete coded frames. The parser
106
- * maintains internal state to handle partial frames across multiple calls.
107
- * The parser may combine multiple frames into one packet or split one frame
108
- * into multiple packets, depending on the codec and stream format.
82
+ * Analyzes raw bitstream data and extracts complete codec frames.
83
+ * May require multiple calls to accumulate enough data for a complete frame.
84
+ * Returns the number of bytes consumed from the input buffer.
109
85
  *
110
- * Direct mapping to av_parser_parse2()
86
+ * Direct mapping to av_parser_parse2().
111
87
  *
112
- * @param codecContext - Codec context (used for stream parameters)
113
- * @param packet - Packet to fill with parsed data
114
- * @param data - Input buffer containing elementary stream data
115
- * @param pts - Presentation timestamp of the first byte in data
116
- * @param dts - Decoding timestamp of the first byte in data
117
- * @param pos - Byte position of the first byte in data in the stream
118
- *
119
- * @returns Number of bytes consumed from the input buffer:
120
- * - >0: Number of bytes consumed from input
121
- * - 0: More data needed to complete a frame
88
+ * @param codecContext - Codec context for parser state
89
+ * @param packet - Packet to receive parsed frame
90
+ * @param data - Raw bitstream data to parse
91
+ * @param pts - Presentation timestamp for data
92
+ * @param dts - Decoding timestamp for data
93
+ * @param pos - Byte position in stream
94
+ * @returns Number of bytes consumed from data, negative on error:
95
+ * - AVERROR_EINVAL: Invalid parameters
96
+ * - AVERROR_ENOMEM: Memory allocation failure
122
97
  *
123
98
  * @example
124
99
  * ```typescript
125
- * import { CodecParser, CodecContext, Packet, FFmpegError } from 'node-av';
126
- * import { AV_NOPTS_VALUE } from 'node-av/constants';
127
- * import * as fs from 'fs';
128
- *
129
- * const inbuf = Buffer.alloc(4096);
130
- * const bytesRead = fs.readSync(fd, inbuf, 0, 4096, null);
100
+ * import { FFmpegError } from 'node-av';
131
101
  *
132
102
  * let offset = 0;
133
- * while (offset < bytesRead) {
134
- * const consumed = parser.parse2(
103
+ * while (offset < rawData.length) {
104
+ * const remaining = rawData.subarray(offset);
105
+ * const ret = parser.parse2(
135
106
  * codecContext,
136
107
  * packet,
137
- * inbuf.subarray(offset),
138
- * AV_NOPTS_VALUE,
139
- * AV_NOPTS_VALUE,
140
- * 0
108
+ * remaining,
109
+ * pts,
110
+ * dts,
111
+ * offset
141
112
  * );
142
113
  *
143
- * offset += consumed;
114
+ * if (ret < 0) {
115
+ * FFmpegError.throwIfError(ret, 'parse2');
116
+ * }
117
+ *
118
+ * offset += ret;
144
119
  *
145
120
  * if (packet.size > 0) {
146
- * // Complete packet ready for decoding
147
- * const ret = await codecContext.sendPacket(packet);
148
- * FFmpegError.throwIfError(ret, 'sendPacket');
121
+ * // Got complete packet
122
+ * await processPacket(packet);
123
+ * packet.unref();
149
124
  * }
150
125
  * }
151
126
  * ```
152
127
  *
153
- * @see {@link init} Must be called before parse2
154
- * @see {@link close} Should be called when done parsing
128
+ * @see {@link init} To initialize parser
155
129
  */
156
130
  parse2(codecContext, packet, data, pts, dts, pos) {
157
131
  return this.native.parse2(codecContext.getNative(), packet.getNative(), data, pts, dts, pos);
158
132
  }
159
133
  /**
160
- * Close the parser and free all resources.
134
+ * Close the codec parser.
161
135
  *
162
- * Releases all resources associated with the parser context.
163
- * After calling close(), the parser instance should not be used anymore.
136
+ * Releases all resources associated with the parser.
137
+ * The parser becomes invalid after calling this.
164
138
  *
165
- * Direct mapping to av_parser_close()
139
+ * Direct mapping to av_parser_close().
166
140
  *
167
141
  * @example
168
142
  * ```typescript
169
- * import { CodecParser } from 'node-av';
170
- *
171
- * const parser = new CodecParser();
172
- * // ... use parser ...
173
- *
174
143
  * parser.close();
175
- * // Parser resources are now freed
176
- * // Do not use parser after this point
144
+ * // Parser is now invalid
177
145
  * ```
146
+ *
147
+ * @see {@link init} To initialize
148
+ * @see {@link Symbol.dispose} For automatic cleanup
178
149
  */
179
150
  close() {
180
151
  this.native.close();
181
152
  }
182
153
  /**
183
- * @internal
184
- * Get the underlying native codec parser object.
185
- * This method is for internal use by other FFmpeg classes.
154
+ * Get the underlying native CodecParser object.
155
+ *
156
+ * @returns The native CodecParser binding object
186
157
  *
187
- * @returns The underlying native codec parser object
158
+ * @internal
188
159
  */
189
160
  getNative() {
190
161
  return this.native;
191
162
  }
163
+ /**
164
+ * Dispose of the codec parser.
165
+ *
166
+ * Implements the Disposable interface for automatic cleanup.
167
+ * Equivalent to calling close().
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * {
172
+ * using parser = new CodecParser();
173
+ * parser.init(AV_CODEC_ID_H264);
174
+ * // Use parser...
175
+ * } // Automatically closed when leaving scope
176
+ * ```
177
+ */
178
+ [Symbol.dispose]() {
179
+ this.close();
180
+ }
192
181
  }
193
182
  //# sourceMappingURL=codec-parser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"codec-parser.js","sourceRoot":"","sources":["../../src/lib/codec-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAOxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,MAAM,OAAO,WAAW;IACd,MAAM,CAAoB;IAElC;;;;;;;;;;;;;;;;;OAiBG;IACH;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC,WAAW,EAAE,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACH,IAAI,CAAC,OAAkB;QACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqDG;IACH,MAAM,CAAC,YAA0B,EAAE,MAAc,EAAE,IAAY,EAAE,GAAW,EAAE,GAAW,EAAE,GAAW;QACpG,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAC/F,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;;;;OAMG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF"}
1
+ {"version":3,"file":"codec-parser.js","sourceRoot":"","sources":["../../src/lib/codec-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAOxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,OAAO,WAAW;IACd,MAAM,CAAoB;IAElC;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC,WAAW,EAAE,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,CAAC,OAAkB;QACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkDG;IACH,MAAM,CAAC,YAA0B,EAAE,MAAc,EAAE,IAAY,EAAE,GAAW,EAAE,GAAW,EAAE,GAAW;QACpG,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAC/F,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;;;;OAMG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC;QACd,IAAI,CAAC,KAAK,EAAE,CAAC;IACf,CAAC;CACF"}