@lumen5/beamcoder 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.
Files changed (88) hide show
  1. package/.circleci/config.yml +41 -0
  2. package/.circleci/images/testbeam10-4.1/Dockerfile +12 -0
  3. package/.circleci/test_image/Dockerfile +14 -0
  4. package/.circleci/test_image/build.md +13 -0
  5. package/.eslintrc.js +27 -0
  6. package/.github/workflows/publish-npm.yml +33 -0
  7. package/LICENSE +674 -0
  8. package/README.md +1221 -0
  9. package/beamstreams.js +692 -0
  10. package/binding.gyp +103 -0
  11. package/examples/encode_h264.js +92 -0
  12. package/examples/jpeg_app.js +55 -0
  13. package/examples/jpeg_filter_app.js +101 -0
  14. package/examples/make_mp4.js +123 -0
  15. package/images/beamcoder_small.jpg +0 -0
  16. package/index.d.ts +83 -0
  17. package/index.js +44 -0
  18. package/install_ffmpeg.js +240 -0
  19. package/package.json +45 -0
  20. package/scratch/decode_aac.js +38 -0
  21. package/scratch/decode_avci.js +50 -0
  22. package/scratch/decode_hevc.js +38 -0
  23. package/scratch/decode_pcm.js +39 -0
  24. package/scratch/make_a_mux.js +68 -0
  25. package/scratch/muxer.js +74 -0
  26. package/scratch/read_wav.js +35 -0
  27. package/scratch/simple_mux.js +39 -0
  28. package/scratch/stream_avci.js +127 -0
  29. package/scratch/stream_mp4.js +78 -0
  30. package/scratch/stream_mux.js +47 -0
  31. package/scratch/stream_pcm.js +82 -0
  32. package/scratch/stream_wav.js +62 -0
  33. package/scripts/install_beamcoder_dependencies.sh +25 -0
  34. package/src/adaptor.h +202 -0
  35. package/src/beamcoder.cc +937 -0
  36. package/src/beamcoder_util.cc +1129 -0
  37. package/src/beamcoder_util.h +206 -0
  38. package/src/codec.cc +7386 -0
  39. package/src/codec.h +44 -0
  40. package/src/codec_par.cc +1818 -0
  41. package/src/codec_par.h +40 -0
  42. package/src/decode.cc +569 -0
  43. package/src/decode.h +75 -0
  44. package/src/demux.cc +584 -0
  45. package/src/demux.h +88 -0
  46. package/src/encode.cc +496 -0
  47. package/src/encode.h +72 -0
  48. package/src/filter.cc +1888 -0
  49. package/src/filter.h +30 -0
  50. package/src/format.cc +5287 -0
  51. package/src/format.h +77 -0
  52. package/src/frame.cc +2681 -0
  53. package/src/frame.h +52 -0
  54. package/src/governor.cc +286 -0
  55. package/src/governor.h +30 -0
  56. package/src/hwcontext.cc +378 -0
  57. package/src/hwcontext.h +35 -0
  58. package/src/log.cc +186 -0
  59. package/src/log.h +20 -0
  60. package/src/mux.cc +834 -0
  61. package/src/mux.h +106 -0
  62. package/src/packet.cc +762 -0
  63. package/src/packet.h +49 -0
  64. package/test/codecParamsSpec.js +148 -0
  65. package/test/decoderSpec.js +56 -0
  66. package/test/demuxerSpec.js +41 -0
  67. package/test/encoderSpec.js +69 -0
  68. package/test/filtererSpec.js +47 -0
  69. package/test/formatSpec.js +343 -0
  70. package/test/frameSpec.js +145 -0
  71. package/test/introspectionSpec.js +73 -0
  72. package/test/muxerSpec.js +34 -0
  73. package/test/packetSpec.js +122 -0
  74. package/types/Beamstreams.d.ts +98 -0
  75. package/types/Codec.d.ts +123 -0
  76. package/types/CodecContext.d.ts +555 -0
  77. package/types/CodecPar.d.ts +108 -0
  78. package/types/Decoder.d.ts +137 -0
  79. package/types/Demuxer.d.ts +113 -0
  80. package/types/Encoder.d.ts +94 -0
  81. package/types/Filter.d.ts +324 -0
  82. package/types/FormatContext.d.ts +380 -0
  83. package/types/Frame.d.ts +295 -0
  84. package/types/HWContext.d.ts +62 -0
  85. package/types/Muxer.d.ts +121 -0
  86. package/types/Packet.d.ts +82 -0
  87. package/types/PrivClass.d.ts +25 -0
  88. package/types/Stream.d.ts +165 -0
@@ -0,0 +1,1818 @@
1
+ /*
2
+ Aerostat Beam Coder - Node.js native bindings for FFmpeg.
3
+ Copyright (C) 2019 Streampunk Media Ltd.
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ https://www.streampunk.media/ mailto:furnace@streampunk.media
19
+ 14 Ormiscaig, Aultbea, Achnasheen, IV22 2JJ U.K.
20
+ */
21
+
22
+ #include "codec_par.h"
23
+
24
+ napi_value getCodecParCodecType(napi_env env, napi_callback_info info) {
25
+ napi_status status;
26
+ napi_value result;
27
+ AVCodecParameters* c;
28
+ const char* enumName;
29
+
30
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
31
+ CHECK_STATUS;
32
+
33
+ enumName = av_get_media_type_string(c->codec_type);
34
+ status = napi_create_string_utf8(env,
35
+ (enumName != nullptr) ? (char*) enumName : "data",
36
+ NAPI_AUTO_LENGTH, &result);
37
+ CHECK_STATUS;
38
+
39
+ return result;
40
+ }
41
+
42
+ napi_value setCodecParCodecType(napi_env env, napi_callback_info info) {
43
+ napi_status status;
44
+ napi_value result;
45
+ napi_valuetype type;
46
+ AVCodecParameters* c;
47
+ int mtype;
48
+ char * typeName;
49
+ size_t typeNameLen;
50
+
51
+ size_t argc = 1;
52
+ napi_value args[1];
53
+
54
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
55
+ CHECK_STATUS;
56
+ if (argc < 1) {
57
+ NAPI_THROW_ERROR("Codec parameters codec_type must be provided with a value.");
58
+ }
59
+ status = napi_typeof(env, args[0], &type);
60
+ CHECK_STATUS;
61
+ if (type != napi_string) {
62
+ NAPI_THROW_ERROR("Codec parameter codec_type must be set using a string.");
63
+ }
64
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeNameLen);
65
+ CHECK_STATUS;
66
+ typeName = (char*) malloc(sizeof(char) * (typeNameLen + 1));
67
+ status = napi_get_value_string_utf8(env, args[0], typeName, typeNameLen + 1, &typeNameLen);
68
+ CHECK_STATUS;
69
+
70
+ mtype = beam_lookup_enum(beam_avmedia_type->inverse, typeName);
71
+ c->codec_type = (mtype == BEAM_ENUM_UNKNOWN) ?
72
+ AVMEDIA_TYPE_UNKNOWN : (AVMediaType) mtype;
73
+
74
+ //SET_BODY codec_type AVMediaType CodecType
75
+ status = napi_get_undefined(env, &result);
76
+ CHECK_STATUS;
77
+ return result;
78
+ }
79
+
80
+ napi_value getCodecParCodecID(napi_env env, napi_callback_info info) {
81
+ napi_status status;
82
+ napi_value result;
83
+ AVCodecParameters* c;
84
+
85
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
86
+ CHECK_STATUS;
87
+
88
+ status = napi_create_int32(env, c->codec_id, &result);
89
+ CHECK_STATUS;
90
+
91
+ return result;
92
+ }
93
+
94
+ napi_value setCodecParCodecID(napi_env env, napi_callback_info info) {
95
+ napi_status status;
96
+ napi_value result;
97
+ napi_valuetype type;
98
+ AVCodecParameters* c;
99
+
100
+ size_t argc = 1;
101
+ napi_value args[1];
102
+
103
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
104
+ CHECK_STATUS;
105
+ if (argc < 1) {
106
+ NAPI_THROW_ERROR("Codec parameters codec_id must be provided with a value.");
107
+ }
108
+ status = napi_typeof(env, args[0], &type);
109
+ CHECK_STATUS;
110
+ if (type != napi_number) {
111
+ NAPI_THROW_ERROR("Codec parameters codec_id must be set by a number.");
112
+ }
113
+
114
+ status = napi_get_value_int32(env, args[0], (int32_t*) &c->codec_id);
115
+ CHECK_STATUS;
116
+
117
+ status = napi_get_undefined(env, &result);
118
+ CHECK_STATUS;
119
+ return result;
120
+ }
121
+
122
+ napi_value getCodecParName(napi_env env, napi_callback_info info) {
123
+ napi_status status;
124
+ napi_value result;
125
+ AVCodecParameters* c;
126
+
127
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
128
+ CHECK_STATUS;
129
+
130
+ status = napi_create_string_utf8(env, (char*) avcodec_get_name(c->codec_id),
131
+ NAPI_AUTO_LENGTH, &result);
132
+ CHECK_STATUS;
133
+
134
+ return result;
135
+ }
136
+
137
+ napi_value setCodecParName(napi_env env, napi_callback_info info) {
138
+ napi_status status;
139
+ napi_value result;
140
+ napi_valuetype type;
141
+ AVCodecParameters* c;
142
+ char* codecName;
143
+ size_t nameLen;
144
+ const AVCodecDescriptor* codecDesc;
145
+
146
+ size_t argc = 1;
147
+ napi_value args[1];
148
+
149
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
150
+ CHECK_STATUS;
151
+ if (argc < 1) {
152
+ NAPI_THROW_ERROR("Codec parameters name must be provided with a value.");
153
+ }
154
+ status = napi_typeof(env, args[0], &type);
155
+ CHECK_STATUS;
156
+ if (type != napi_string) {
157
+ NAPI_THROW_ERROR("Codec parameters name must be provided with a value.");
158
+ }
159
+
160
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &nameLen);
161
+ CHECK_STATUS;
162
+ codecName = (char*) malloc(sizeof(char) * (nameLen + 1));
163
+ status = napi_get_value_string_utf8(env, args[0], codecName, nameLen + 1, &nameLen);
164
+ CHECK_STATUS;
165
+ codecDesc = avcodec_descriptor_get_by_name((const char *) codecName);
166
+ CHECK_STATUS;
167
+ if (codecDesc == nullptr) {
168
+ c->codec_id = AV_CODEC_ID_NONE;
169
+ c->codec_type = AVMEDIA_TYPE_DATA;
170
+ } else {
171
+ c->codec_id = codecDesc->id;
172
+ c->codec_type = codecDesc->type;
173
+ }
174
+
175
+ free(codecName);
176
+
177
+ status = napi_get_undefined(env, &result);
178
+ CHECK_STATUS;
179
+ return result;
180
+ }
181
+
182
+ napi_value getCodecParCodecTag(napi_env env, napi_callback_info info) {
183
+ napi_status status;
184
+ napi_value result;
185
+ AVCodecParameters* c;
186
+ char fourcc[AV_FOURCC_MAX_STRING_SIZE];
187
+
188
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
189
+ CHECK_STATUS;
190
+
191
+ av_fourcc_make_string(fourcc, c->codec_tag);
192
+ if (strchr(fourcc, '[')) { // not a recognised tag
193
+ status = napi_create_uint32(env, c->codec_tag, &result);
194
+ CHECK_STATUS;
195
+ } else {
196
+ status = napi_create_string_utf8(env, fourcc, NAPI_AUTO_LENGTH, &result);
197
+ CHECK_STATUS;
198
+ }
199
+
200
+ return result;
201
+ }
202
+
203
+ napi_value setCodecParCodecTag(napi_env env, napi_callback_info info) {
204
+ napi_status status;
205
+ napi_value result;
206
+ napi_valuetype type;
207
+ AVCodecParameters* c;
208
+ size_t argc = 1;
209
+ napi_value args[1];
210
+
211
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
212
+ CHECK_STATUS;
213
+ if (argc < 1) {
214
+ NAPI_THROW_ERROR("Codec parameters codec_tag must be provided with a value.");
215
+ }
216
+ status = napi_typeof(env, args[0], &type);
217
+ CHECK_STATUS;
218
+
219
+ if (type == napi_string) {
220
+ char* tag;
221
+ size_t tagLen;
222
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &tagLen);
223
+ CHECK_STATUS;
224
+ tag = (char*) malloc(sizeof(char) * (tagLen + 1));
225
+ status = napi_get_value_string_utf8(env, args[0], tag, tagLen + 1, &tagLen);
226
+ CHECK_STATUS;
227
+ c->codec_tag = MKTAG(tag[0], tag[1], tag[2], tag[3]);
228
+ } else if (type == napi_number) {
229
+ status = napi_get_value_uint32(env, args[0], &c->codec_tag);
230
+ CHECK_STATUS;
231
+ } else {
232
+ printf("Setting codec parameters codec_tag expects a string or a number - attempting to set from codec_id\n");
233
+ if ((AVMEDIA_TYPE_VIDEO == c->codec_type) || (AVMEDIA_TYPE_AUDIO == c->codec_type)) {
234
+ const struct AVCodecTag *table[] = {
235
+ (AVMEDIA_TYPE_VIDEO == c->codec_type) ? avformat_get_riff_video_tags() : avformat_get_riff_audio_tags(), 0
236
+ };
237
+ if (0 == av_codec_get_tag2(table, c->codec_id, &c->codec_tag))
238
+ NAPI_THROW_ERROR("Codec parameters codec_tag could not be set.");
239
+ printf("Setting codec parameters codec_tag to 0x%08x\n", c->codec_tag);
240
+ } else
241
+ NAPI_THROW_ERROR("Codec parameters codec_tag expects a string or number value.");
242
+ }
243
+
244
+ status = napi_get_undefined(env, &result);
245
+ CHECK_STATUS;
246
+ return result;
247
+ }
248
+
249
+ napi_value getCodecParExtraData(napi_env env, napi_callback_info info) {
250
+ napi_status status;
251
+ napi_value result;
252
+ AVCodecParameters* codecPar;
253
+ void* resultData;
254
+
255
+ size_t argc = 0;
256
+ status = napi_get_cb_info(env, info, &argc, nullptr, nullptr, (void**) &codecPar);
257
+ CHECK_STATUS;
258
+ if (codecPar->extradata_size > 0) {
259
+ status = napi_create_buffer_copy(env, codecPar->extradata_size,
260
+ codecPar->extradata, &resultData, &result);
261
+ CHECK_STATUS;
262
+ } else {
263
+ status = napi_get_null(env, &result);
264
+ CHECK_STATUS;
265
+ }
266
+
267
+ return result;
268
+ }
269
+
270
+ napi_value setCodecParExtraData(napi_env env, napi_callback_info info) {
271
+ napi_status status;
272
+ napi_value result, global, jsBuffer, jsBufferFrom, arrayData;
273
+ napi_valuetype type;
274
+ bool isBuffer, isArray;
275
+ AVCodecParameters* codecPar;
276
+ uint8_t* data;
277
+ size_t dataLen;
278
+
279
+ size_t argc = 1;
280
+ napi_value args[1];
281
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &codecPar);
282
+ CHECK_STATUS;
283
+ if (argc < 1) {
284
+ NAPI_THROW_ERROR("A value is required to set the extradata property.");
285
+ }
286
+ status = napi_typeof(env, args[0], &type);
287
+ CHECK_STATUS;
288
+ if (type == napi_null) {
289
+ if (codecPar->extradata_size > 0) { // Tidy up old buffers
290
+ av_freep(&codecPar->extradata);
291
+ }
292
+ codecPar->extradata_size = 0;
293
+ goto done;
294
+ }
295
+ status = napi_is_buffer(env, args[0], &isBuffer);
296
+ CHECK_STATUS;
297
+ if (!isBuffer) {
298
+ status = napi_get_named_property(env, args[0], "data", &arrayData);
299
+ CHECK_STATUS;
300
+ // TODO more checks that this is a buffer from JSON?
301
+ status = napi_is_array(env, arrayData, &isArray);
302
+ CHECK_STATUS;
303
+ if (isArray) {
304
+ status = napi_get_global(env, &global);
305
+ CHECK_STATUS;
306
+ status = napi_get_named_property(env, global, "Buffer", &jsBuffer);
307
+ CHECK_STATUS;
308
+ status = napi_get_named_property(env, jsBuffer, "from", &jsBufferFrom);
309
+ CHECK_STATUS;
310
+ const napi_value fargs[] = { arrayData };
311
+ status = napi_call_function(env, args[0], jsBufferFrom, 1, fargs, &args[0]);
312
+ CHECK_STATUS;
313
+ } else {
314
+ NAPI_THROW_ERROR("A buffer is required to set the extradata propeprty.");
315
+ }
316
+ }
317
+
318
+ status = napi_get_buffer_info(env, args[0], (void**) &data, &dataLen);
319
+ CHECK_STATUS;
320
+ if (codecPar->extradata_size > 0) { // Tidy up old buffers
321
+ av_freep(&codecPar->extradata);
322
+ codecPar->extradata_size = 0;
323
+ }
324
+ codecPar->extradata = (uint8_t*) av_mallocz(dataLen + AV_INPUT_BUFFER_PADDING_SIZE);
325
+ codecPar->extradata_size = dataLen;
326
+ memcpy(codecPar->extradata, data, dataLen);
327
+
328
+ done:
329
+ status = napi_get_undefined(env, &result);
330
+ CHECK_STATUS;
331
+ return result;
332
+ }
333
+
334
+ napi_value getCodecParFormat(napi_env env, napi_callback_info info) {
335
+ napi_status status;
336
+ napi_value result;
337
+ AVCodecParameters* c;
338
+ const char* fmtName = nullptr;
339
+
340
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
341
+ CHECK_STATUS;
342
+
343
+ switch (c->codec_type) {
344
+ case AVMEDIA_TYPE_VIDEO:
345
+ fmtName = av_get_pix_fmt_name((AVPixelFormat) c->format);
346
+ break;
347
+ case AVMEDIA_TYPE_AUDIO:
348
+ fmtName = av_get_sample_fmt_name((AVSampleFormat) c->format);
349
+ break;
350
+ default: // Might not have media type set
351
+ fmtName = av_get_pix_fmt_name((AVPixelFormat) c->format);
352
+ if (fmtName == nullptr) {
353
+ fmtName = av_get_sample_fmt_name((AVSampleFormat) c->format);
354
+ }
355
+ break;
356
+ }
357
+ if (fmtName == nullptr) {
358
+ status = napi_get_null(env, &result);
359
+ CHECK_STATUS;
360
+ } else {
361
+ status = napi_create_string_utf8(env, (char*) fmtName, NAPI_AUTO_LENGTH, &result);
362
+ CHECK_STATUS;
363
+ }
364
+
365
+ return result;
366
+ }
367
+
368
+ napi_value setCodecParFormat(napi_env env, napi_callback_info info) {
369
+ napi_status status;
370
+ napi_value result;
371
+ napi_valuetype type;
372
+ AVCodecParameters* c;
373
+ char* formatName;
374
+ size_t nameLen;
375
+ int format;
376
+
377
+ size_t argc = 1;
378
+ napi_value args[1];
379
+
380
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
381
+ CHECK_STATUS;
382
+ if (argc < 1) {
383
+ NAPI_THROW_ERROR("Codec parameters format must be provided with a value.");
384
+ }
385
+ status = napi_typeof(env, args[0], &type);
386
+ CHECK_STATUS;
387
+ if ((type == napi_undefined) || (type == napi_null)) {
388
+ c->format = AV_PIX_FMT_NONE;
389
+ goto done;
390
+ }
391
+ if (type != napi_string) {
392
+ NAPI_THROW_ERROR("Codec parameters format must be set with a string value");
393
+ }
394
+
395
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &nameLen);
396
+ CHECK_STATUS;
397
+ formatName = (char*) malloc(sizeof(char) * (nameLen + 1));
398
+ status = napi_get_value_string_utf8(env, args[0], formatName, nameLen + 1, &nameLen);
399
+ CHECK_STATUS;
400
+
401
+ switch (c->codec_type) {
402
+ case AVMEDIA_TYPE_VIDEO:
403
+ format = (int) av_get_pix_fmt((const char *) formatName);
404
+ break;
405
+ case AVMEDIA_TYPE_AUDIO:
406
+ format = (int) av_get_sample_fmt((const char *) formatName);
407
+ break;
408
+ default: // codec_type may not have been set yet ... guess mode
409
+ format = (int) av_get_pix_fmt((const char *) formatName);
410
+ if (format == AV_PIX_FMT_NONE) {
411
+ format = (int) av_get_sample_fmt((const char *) formatName);
412
+ }
413
+ break;
414
+ }
415
+ c->format = format;
416
+
417
+ free(formatName);
418
+
419
+ done:
420
+ status = napi_get_undefined(env, &result);
421
+ CHECK_STATUS;
422
+ return result;
423
+ }
424
+
425
+ napi_value getCodecParBitRate(napi_env env, napi_callback_info info) {
426
+ napi_status status;
427
+ napi_value result;
428
+ AVCodecParameters* c;
429
+
430
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
431
+ CHECK_STATUS;
432
+
433
+ status = napi_create_int64(env, c->bit_rate, &result);
434
+ CHECK_STATUS;
435
+
436
+ return result;
437
+ }
438
+
439
+ napi_value setCodecParBitRate(napi_env env, napi_callback_info info) {
440
+ napi_status status;
441
+ napi_value result;
442
+ napi_valuetype type;
443
+ AVCodecParameters* c;
444
+
445
+ size_t argc = 1;
446
+ napi_value args[1];
447
+
448
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
449
+ CHECK_STATUS;
450
+ if (argc < 1) {
451
+ NAPI_THROW_ERROR("Codec parameters bit_rate must be provided with a value.");
452
+ }
453
+ status = napi_typeof(env, args[0], &type);
454
+ CHECK_STATUS;
455
+ if (type != napi_number) {
456
+ NAPI_THROW_ERROR("Codec parameters bit_rate must be set with a number.");
457
+ }
458
+ status = napi_get_value_int64(env, args[0], &c->bit_rate);
459
+ CHECK_STATUS;
460
+
461
+ status = napi_get_undefined(env, &result);
462
+ CHECK_STATUS;
463
+ return result;
464
+ }
465
+
466
+ napi_value getCodecParSmpAspectRt(napi_env env, napi_callback_info info) {
467
+ napi_status status;
468
+ napi_value result, element;
469
+ AVCodecParameters* c;
470
+
471
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
472
+ CHECK_STATUS;
473
+
474
+ status = napi_create_array(env, &result);
475
+ CHECK_STATUS;
476
+ status = napi_create_int32(env, c->sample_aspect_ratio.num, &element);
477
+ CHECK_STATUS;
478
+ status = napi_set_element(env, result, 0, element);
479
+ CHECK_STATUS;
480
+ status = napi_create_int32(env, c->sample_aspect_ratio.den, &element);
481
+ CHECK_STATUS;
482
+ status = napi_set_element(env, result, 1, element);
483
+ CHECK_STATUS;
484
+
485
+ return result;
486
+ }
487
+
488
+ napi_value setCodecParSmpAspectRt(napi_env env, napi_callback_info info) {
489
+ napi_status status;
490
+ napi_value result, num, den;
491
+ napi_valuetype type;
492
+ AVCodecParameters* c;
493
+ bool isArray;
494
+
495
+ size_t argc = 1;
496
+ napi_value args[1];
497
+
498
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
499
+ CHECK_STATUS;
500
+ if (argc < 1) {
501
+ NAPI_THROW_ERROR("Codec parameters sample_aspect_ratio must be provided with a value.");
502
+ }
503
+ status = napi_is_array(env, args[0], &isArray);
504
+ CHECK_STATUS;
505
+ if (!isArray) {
506
+ NAPI_THROW_ERROR("Codec parameters sample_aspect_ratio must be set with an array of numbers.");
507
+ }
508
+
509
+ status = napi_get_element(env, args[0], 0, &num);
510
+ CHECK_STATUS;
511
+ status = napi_typeof(env, num, &type);
512
+ CHECK_STATUS;
513
+ if (type != napi_number) {
514
+ NAPI_THROW_ERROR("Codec parameters sample_aspect_ratio numerator must be a number.");
515
+ }
516
+ CHECK_STATUS;
517
+
518
+ status = napi_get_element(env, args[0], 1, &den);
519
+ CHECK_STATUS;
520
+ status = napi_typeof(env, den, &type);
521
+ CHECK_STATUS;
522
+ if (type != napi_number) {
523
+ NAPI_THROW_ERROR("Codec parameters sample_aspect_ratio denominator must be a number.");
524
+ }
525
+ CHECK_STATUS;
526
+
527
+ status = napi_get_value_int32(env, num, &c->sample_aspect_ratio.num);
528
+ CHECK_STATUS;
529
+ status = napi_get_value_int32(env, den, &c->sample_aspect_ratio.den);
530
+ CHECK_STATUS;
531
+
532
+ status = napi_get_undefined(env, &result);
533
+ CHECK_STATUS;
534
+ return result;
535
+ }
536
+
537
+ napi_value getCodecParFieldOrder(napi_env env, napi_callback_info info) {
538
+ napi_status status;
539
+ napi_value result;
540
+ AVCodecParameters* c;
541
+
542
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
543
+ CHECK_STATUS;
544
+
545
+ status = napi_create_string_utf8(env,
546
+ (char*) beam_lookup_name(beam_field_order->forward, c->field_order),
547
+ NAPI_AUTO_LENGTH, &result);
548
+ CHECK_STATUS;
549
+
550
+ return result;
551
+ }
552
+
553
+ napi_value setCodecParFieldOrder(napi_env env, napi_callback_info info) {
554
+ napi_status status;
555
+ napi_value result;
556
+ napi_valuetype type;
557
+ AVCodecParameters* c;
558
+ char* enumString;
559
+ size_t strLen;
560
+ int enumValue;
561
+
562
+ size_t argc = 1;
563
+ napi_value args[1];
564
+
565
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
566
+ CHECK_STATUS;
567
+ if (argc < 1) {
568
+ NAPI_THROW_ERROR("Codec parameters field_order must be provided with a value.");
569
+ }
570
+ status = napi_typeof(env, args[0], &type);
571
+ CHECK_STATUS;
572
+ if (type != napi_string) {
573
+ NAPI_THROW_ERROR("Codec parameters field_order must be set with a string.");
574
+ }
575
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
576
+ CHECK_STATUS;
577
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
578
+ CHECK_STATUS;
579
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
580
+ CHECK_STATUS;
581
+
582
+ enumValue = beam_lookup_enum(beam_field_order->inverse, enumString);
583
+ if (enumValue == BEAM_ENUM_UNKNOWN) {
584
+ NAPI_THROW_ERROR("Codec parameters field_order value unrecognised. Did you mean e.g. 'progressive'?")
585
+ }
586
+ c->field_order = (AVFieldOrder) enumValue;
587
+
588
+ free(enumString);
589
+
590
+ status = napi_get_undefined(env, &result);
591
+ CHECK_STATUS;
592
+ return result;
593
+ }
594
+
595
+ napi_value getCodecParColorRange(napi_env env, napi_callback_info info) {
596
+ napi_status status;
597
+ napi_value result;
598
+ AVCodecParameters* c;
599
+ const char* enumName;
600
+
601
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
602
+ CHECK_STATUS;
603
+
604
+ enumName = av_color_range_name(c->color_range);
605
+ status = napi_create_string_utf8(env,
606
+ (enumName != nullptr) ? (char*) enumName : "unknown", NAPI_AUTO_LENGTH, &result);
607
+ CHECK_STATUS;
608
+
609
+ return result;
610
+ }
611
+
612
+ napi_value setCodecParColorRange(napi_env env, napi_callback_info info) {
613
+ napi_status status;
614
+ napi_value result;
615
+ napi_valuetype type;
616
+ AVCodecParameters* c;
617
+ char* enumString;
618
+ size_t strLen;
619
+ int enumValue;
620
+
621
+ size_t argc = 1;
622
+ napi_value args[1];
623
+
624
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
625
+ CHECK_STATUS;
626
+ if (argc < 1) {
627
+ NAPI_THROW_ERROR("Codec parameters color_range must be provided with a value.");
628
+ }
629
+ status = napi_typeof(env, args[0], &type);
630
+ CHECK_STATUS;
631
+ if ((type == napi_null) || (type == napi_undefined)) {
632
+ c->color_range = AVCOL_RANGE_UNSPECIFIED;
633
+ goto done;
634
+ }
635
+ if (type != napi_string) {
636
+ NAPI_THROW_ERROR("Codec parameters color_range must be set with a string value.");
637
+ }
638
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
639
+ CHECK_STATUS;
640
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
641
+ CHECK_STATUS;
642
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
643
+ CHECK_STATUS;
644
+
645
+ enumValue = av_color_range_from_name((const char *) enumString);
646
+ free(enumString);
647
+ if (enumValue < 0) {
648
+ NAPI_THROW_ERROR("Codec parameter color_range is not recognised. One of 'tv' or 'pc'?");
649
+ }
650
+ c->color_range = (AVColorRange) enumValue;
651
+
652
+
653
+ done:
654
+ status = napi_get_undefined(env, &result);
655
+ CHECK_STATUS;
656
+ return result;
657
+ }
658
+
659
+ napi_value getCodecParColorPrims(napi_env env, napi_callback_info info) {
660
+ napi_status status;
661
+ napi_value result;
662
+ AVCodecParameters* c;
663
+ const char* enumName;
664
+
665
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
666
+ CHECK_STATUS;
667
+
668
+ enumName = av_color_primaries_name(c->color_primaries);
669
+ status = napi_create_string_utf8(env,
670
+ (enumName != nullptr) ? (char*) enumName : "unknown",
671
+ NAPI_AUTO_LENGTH, &result);
672
+ CHECK_STATUS;
673
+
674
+ return result;
675
+ }
676
+
677
+ napi_value setCodecParColorPrims(napi_env env, napi_callback_info info) {
678
+ napi_status status;
679
+ napi_value result;
680
+ napi_valuetype type;
681
+ AVCodecParameters* c;
682
+ char* enumString;
683
+ size_t strLen;
684
+ int enumValue;
685
+
686
+ size_t argc = 1;
687
+ napi_value args[1];
688
+
689
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
690
+ CHECK_STATUS;
691
+ if (argc < 1) {
692
+ NAPI_THROW_ERROR("Codec parameters color_primaries must be provided with a value.");
693
+ }
694
+ status = napi_typeof(env, args[0], &type);
695
+ CHECK_STATUS;
696
+ if ((type == napi_null) || (type == napi_undefined)) {
697
+ c->color_primaries = AVCOL_PRI_UNSPECIFIED;
698
+ goto done;
699
+ }
700
+ if (type != napi_string) {
701
+ NAPI_THROW_ERROR("Codec parameters color_primaries must be set with a string value.");
702
+ }
703
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
704
+ CHECK_STATUS;
705
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
706
+ CHECK_STATUS;
707
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
708
+ CHECK_STATUS;
709
+
710
+ enumValue = av_color_primaries_from_name((const char *) enumString);
711
+ free(enumString);
712
+ if (enumValue < 0) {
713
+ NAPI_THROW_ERROR("Codec parameter color_primaries is not recognised. Did you mean e.g. 'bt709'?");
714
+ }
715
+ c->color_primaries = (AVColorPrimaries) enumValue;
716
+
717
+ done:
718
+ status = napi_get_undefined(env, &result);
719
+ CHECK_STATUS;
720
+ return result;
721
+ }
722
+
723
+ napi_value getCodecParColorTrc(napi_env env, napi_callback_info info) {
724
+ napi_status status;
725
+ napi_value result;
726
+ AVCodecParameters* c;
727
+ const char* enumName;
728
+
729
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
730
+ CHECK_STATUS;
731
+
732
+ enumName = av_color_transfer_name(c->color_trc);
733
+ status = napi_create_string_utf8(env,
734
+ (enumName != nullptr) ? (char*) enumName : "unknown",
735
+ NAPI_AUTO_LENGTH, &result);
736
+ CHECK_STATUS;
737
+
738
+ return result;
739
+ }
740
+
741
+ napi_value setCodecParColorTrc(napi_env env, napi_callback_info info) {
742
+ napi_status status;
743
+ napi_value result;
744
+ napi_valuetype type;
745
+ AVCodecParameters* c;
746
+ char* enumString;
747
+ size_t strLen;
748
+ int enumValue;
749
+
750
+ size_t argc = 1;
751
+ napi_value args[1];
752
+
753
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
754
+ CHECK_STATUS;
755
+ if (argc < 1) {
756
+ NAPI_THROW_ERROR("Codec parameters color_trc must be provided with a value.");
757
+ }
758
+ status = napi_typeof(env, args[0], &type);
759
+ CHECK_STATUS;
760
+ if ((type == napi_null) || (type == napi_undefined)) {
761
+ c->color_trc = AVCOL_TRC_UNSPECIFIED;
762
+ goto done;
763
+ }
764
+ if (type != napi_string) {
765
+ NAPI_THROW_ERROR("Codec parameters color_trc must be set with a string value.");
766
+ }
767
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
768
+ CHECK_STATUS;
769
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
770
+ CHECK_STATUS;
771
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
772
+ CHECK_STATUS;
773
+
774
+ enumValue = av_color_transfer_from_name((const char *) enumString);
775
+ free(enumString);
776
+ if (enumValue < 0) {
777
+ NAPI_THROW_ERROR("Codec parameter color_trc is not recognised. Did you mean e.g. 'bt709'?");
778
+ }
779
+ c->color_trc = (AVColorTransferCharacteristic) enumValue;
780
+
781
+
782
+ done:
783
+ status = napi_get_undefined(env, &result);
784
+ CHECK_STATUS;
785
+ return result;
786
+ }
787
+
788
+ napi_value getCodecParColorSpace(napi_env env, napi_callback_info info) {
789
+ napi_status status;
790
+ napi_value result;
791
+ AVCodecParameters* c;
792
+ const char* enumName;
793
+
794
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
795
+ CHECK_STATUS;
796
+
797
+ enumName = av_color_space_name(c->color_space);
798
+ status = napi_create_string_utf8(env,
799
+ (enumName != nullptr) ? (char*) enumName : "unknown",
800
+ NAPI_AUTO_LENGTH, &result);
801
+ CHECK_STATUS;
802
+
803
+ return result;
804
+ }
805
+
806
+ napi_value setCodecParColorSpace(napi_env env, napi_callback_info info) {
807
+ napi_status status;
808
+ napi_value result;
809
+ napi_valuetype type;
810
+ AVCodecParameters* c;
811
+ char* enumString;
812
+ size_t strLen;
813
+ int enumValue;
814
+
815
+ size_t argc = 1;
816
+ napi_value args[1];
817
+
818
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
819
+ CHECK_STATUS;
820
+ if (argc < 1) {
821
+ NAPI_THROW_ERROR("Codec parameters color_space must be provided with a value.");
822
+ }
823
+ status = napi_typeof(env, args[0], &type);
824
+ CHECK_STATUS;
825
+ if ((type == napi_null) || (type == napi_undefined)) {
826
+ c->color_space = AVCOL_SPC_UNSPECIFIED;
827
+ goto done;
828
+ }
829
+ if (type != napi_string) {
830
+ NAPI_THROW_ERROR("Codec parameters color_space must be set with a string value.");
831
+ }
832
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
833
+ CHECK_STATUS;
834
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
835
+ CHECK_STATUS;
836
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
837
+ CHECK_STATUS;
838
+
839
+ enumValue = av_color_space_from_name((const char *) enumString);
840
+ free(enumString);
841
+ if (enumValue < 0) {
842
+ NAPI_THROW_ERROR("Codec parameter color_space is not recognised. Did you mean e.g. 'bt709'?");
843
+ }
844
+ c->color_space = (AVColorSpace) enumValue;
845
+
846
+ done:
847
+ status = napi_get_undefined(env, &result);
848
+ CHECK_STATUS;
849
+ return result;
850
+ }
851
+
852
+ napi_value getCodecParChromaLoc(napi_env env, napi_callback_info info) {
853
+ napi_status status;
854
+ napi_value result;
855
+ AVCodecParameters* c;
856
+ const char* enumName;
857
+
858
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
859
+ CHECK_STATUS;
860
+
861
+ enumName = av_chroma_location_name(c->chroma_location);
862
+ status = napi_create_string_utf8(env,
863
+ (enumName != nullptr) ? (char*) enumName : "unspecified",
864
+ NAPI_AUTO_LENGTH, &result);
865
+ CHECK_STATUS;
866
+
867
+ return result;
868
+ }
869
+
870
+ napi_value setCodecParChromaLoc(napi_env env, napi_callback_info info) {
871
+ napi_status status;
872
+ napi_value result;
873
+ napi_valuetype type;
874
+ AVCodecParameters* c;
875
+ char* enumString;
876
+ size_t strLen;
877
+ int enumValue;
878
+
879
+ size_t argc = 1;
880
+ napi_value args[1];
881
+
882
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
883
+ CHECK_STATUS;
884
+ if (argc < 1) {
885
+ NAPI_THROW_ERROR("Codec parameters chroma_location must be provided with a value.");
886
+ }
887
+ status = napi_typeof(env, args[0], &type);
888
+ CHECK_STATUS;
889
+ if ((type == napi_null) || (type == napi_undefined)) {
890
+ c->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
891
+ goto done;
892
+ }
893
+ if (type != napi_string) {
894
+ NAPI_THROW_ERROR("Codec parameters chroma_location must be set with a string value.");
895
+ }
896
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
897
+ CHECK_STATUS;
898
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
899
+ CHECK_STATUS;
900
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
901
+ CHECK_STATUS;
902
+
903
+ enumValue = av_chroma_location_from_name((const char *) enumString);
904
+ free(enumString);
905
+ if (enumValue < 0) {
906
+ NAPI_THROW_ERROR("Codec parameter chroma_location is not recognised. Did you mean e.g. 'left'?");
907
+ }
908
+ c->chroma_location = (AVChromaLocation) enumValue;
909
+
910
+ done:
911
+ status = napi_get_undefined(env, &result);
912
+ CHECK_STATUS;
913
+ return result;
914
+ }
915
+
916
+ napi_value getCodecParChanLayout(napi_env env, napi_callback_info info) {
917
+ napi_status status;
918
+ napi_value result;
919
+ AVCodecParameters* c;
920
+ char enumName[64];
921
+
922
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
923
+ CHECK_STATUS;
924
+
925
+ av_get_channel_layout_string(enumName, 64, 0,
926
+ c->channel_layout ? c->channel_layout : av_get_default_channel_layout(c->channels));
927
+ status = napi_create_string_utf8(env, enumName, NAPI_AUTO_LENGTH, &result);
928
+ CHECK_STATUS;
929
+
930
+ return result;
931
+ }
932
+
933
+ napi_value setCodecParChanLayout(napi_env env, napi_callback_info info) {
934
+ napi_status status;
935
+ napi_value result;
936
+ napi_valuetype type;
937
+ AVCodecParameters* c;
938
+ char* enumString;
939
+ size_t strLen;
940
+ uint64_t chanLay;
941
+
942
+ size_t argc = 1;
943
+ napi_value args[1];
944
+
945
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
946
+ CHECK_STATUS;
947
+ if (argc < 1) { NAPI_THROW_ERROR("Codec parameters channel_layout must be provided with a value.");
948
+ }
949
+ status = napi_typeof(env, args[0], &type);
950
+ CHECK_STATUS;
951
+ if ((type == napi_null) || (type == napi_undefined)) {
952
+ c->channel_layout = 0;
953
+ goto done;
954
+ }
955
+ if (type != napi_string) {
956
+ NAPI_THROW_ERROR("Codec parameter channel_layout must be set with a string value.");
957
+ }
958
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
959
+ CHECK_STATUS;
960
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
961
+ CHECK_STATUS;
962
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
963
+ CHECK_STATUS;
964
+
965
+ chanLay = av_get_channel_layout((const char *) enumString);
966
+ free(enumString);
967
+ if (chanLay != 0) {
968
+ c->channel_layout = chanLay;
969
+ } else {
970
+ NAPI_THROW_ERROR("Channel layout name is not recognized. Set 'null' for '0 channels'.");
971
+ }
972
+
973
+ done:
974
+ status = napi_get_undefined(env, &result);
975
+ CHECK_STATUS;
976
+ return result;
977
+ }
978
+
979
+ napi_value getCodecParBitsPerCodedSmp(napi_env env, napi_callback_info info) {
980
+ napi_status status;
981
+ napi_value result;
982
+ AVCodecParameters* c;
983
+
984
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
985
+ CHECK_STATUS;
986
+
987
+ status = napi_create_int32(env, c->bits_per_coded_sample, &result);
988
+ CHECK_STATUS;
989
+ return result;
990
+ }
991
+
992
+ napi_value setCodecParBitsPerCodedSmp(napi_env env, napi_callback_info info) {
993
+ napi_status status;
994
+ napi_value result;
995
+ napi_valuetype type;
996
+ AVCodecParameters* c;
997
+
998
+ size_t argc = 1;
999
+ napi_value args[1];
1000
+
1001
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1002
+ CHECK_STATUS;
1003
+ if (argc < 1) {
1004
+ NAPI_THROW_ERROR("Codec parameter bits_per_coded_sample must be provided with a value.");
1005
+ }
1006
+ status = napi_typeof(env, args[0], &type);
1007
+ CHECK_STATUS;
1008
+ if (type != napi_number) {
1009
+ NAPI_THROW_ERROR("Codec parameter bits_per_coded_sample must be set with a number.");
1010
+ }
1011
+ status = napi_get_value_int32(env, args[0], &c->bits_per_coded_sample);
1012
+ CHECK_STATUS;
1013
+
1014
+ status = napi_get_undefined(env, &result);
1015
+ CHECK_STATUS;
1016
+ return result;
1017
+ }
1018
+
1019
+ napi_value getCodecParBitsPerRawSmp(napi_env env, napi_callback_info info) {
1020
+ napi_status status;
1021
+ napi_value result;
1022
+ AVCodecParameters* c;
1023
+
1024
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1025
+ CHECK_STATUS;
1026
+
1027
+ status = napi_create_int32(env, c->bits_per_raw_sample, &result);
1028
+ CHECK_STATUS;
1029
+ return result;
1030
+ }
1031
+
1032
+ napi_value setCodecParBitsPerRawSmp(napi_env env, napi_callback_info info) {
1033
+ napi_status status;
1034
+ napi_value result;
1035
+ napi_valuetype type;
1036
+ AVCodecParameters* c;
1037
+
1038
+ size_t argc = 1;
1039
+ napi_value args[1];
1040
+
1041
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1042
+ CHECK_STATUS;
1043
+ if (argc < 1) {
1044
+ NAPI_THROW_ERROR("Codec parameter bits_per_raw_sample must be provided with a value.");
1045
+ }
1046
+ status = napi_typeof(env, args[0], &type);
1047
+ CHECK_STATUS;
1048
+ if (type != napi_number) {
1049
+ NAPI_THROW_ERROR("Codec parameter bits_per_raw_sample must be set with a number.");
1050
+ }
1051
+ status = napi_get_value_int32(env, args[0], &c->bits_per_raw_sample);
1052
+ CHECK_STATUS;
1053
+
1054
+ status = napi_get_undefined(env, &result);
1055
+ CHECK_STATUS;
1056
+ return result;
1057
+ }
1058
+
1059
+ napi_value getCodecParLevel(napi_env env, napi_callback_info info) {
1060
+ napi_status status;
1061
+ napi_value result;
1062
+ AVCodecParameters* c;
1063
+
1064
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1065
+ CHECK_STATUS;
1066
+
1067
+ status = napi_create_int32(env, c->level, &result);
1068
+ CHECK_STATUS;
1069
+ return result;
1070
+ }
1071
+
1072
+ napi_value setCodecParLevel(napi_env env, napi_callback_info info) {
1073
+ napi_status status;
1074
+ napi_value result;
1075
+ napi_valuetype type;
1076
+ AVCodecParameters* c;
1077
+
1078
+ size_t argc = 1;
1079
+ napi_value args[1];
1080
+
1081
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1082
+ CHECK_STATUS;
1083
+ if (argc < 1) {
1084
+ NAPI_THROW_ERROR("Codec parameter level must be provided with a value.");
1085
+ }
1086
+ status = napi_typeof(env, args[0], &type);
1087
+ CHECK_STATUS;
1088
+ if ((type == napi_null) || (type == napi_undefined)) {
1089
+ c->level = FF_LEVEL_UNKNOWN;
1090
+ goto done;
1091
+ }
1092
+ if (type != napi_number) {
1093
+ NAPI_THROW_ERROR("Codec parameter level must be set with a number.");
1094
+ }
1095
+ status = napi_get_value_int32(env, args[0], &c->level);
1096
+ CHECK_STATUS;
1097
+
1098
+ done:
1099
+ status = napi_get_undefined(env, &result);
1100
+ CHECK_STATUS;
1101
+ return result;
1102
+ }
1103
+
1104
+ napi_value getCodecParWidth(napi_env env, napi_callback_info info) {
1105
+ napi_status status;
1106
+ napi_value result;
1107
+ AVCodecParameters* c;
1108
+
1109
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1110
+ CHECK_STATUS;
1111
+
1112
+ status = napi_create_int32(env, c->width, &result);
1113
+ CHECK_STATUS;
1114
+ return result;
1115
+ }
1116
+
1117
+ napi_value setCodecParWidth(napi_env env, napi_callback_info info) {
1118
+ napi_status status;
1119
+ napi_value result;
1120
+ napi_valuetype type;
1121
+ AVCodecParameters* c;
1122
+
1123
+ size_t argc = 1;
1124
+ napi_value args[1];
1125
+
1126
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1127
+ CHECK_STATUS;
1128
+ if (argc < 1) {
1129
+ NAPI_THROW_ERROR("Codec parameter width must be provided with a value.");
1130
+ }
1131
+ status = napi_typeof(env, args[0], &type);
1132
+ CHECK_STATUS;
1133
+ if (type != napi_number) {
1134
+ NAPI_THROW_ERROR("Codec parameter width must be set with a number.");
1135
+ }
1136
+ status = napi_get_value_int32(env, args[0], &c->width);
1137
+ CHECK_STATUS;
1138
+
1139
+ status = napi_get_undefined(env, &result);
1140
+ CHECK_STATUS;
1141
+ return result;
1142
+ }
1143
+
1144
+ napi_value getCodecParHeight(napi_env env, napi_callback_info info) {
1145
+ napi_status status;
1146
+ napi_value result;
1147
+ AVCodecParameters* c;
1148
+
1149
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1150
+ CHECK_STATUS;
1151
+
1152
+ status = napi_create_int32(env, c->height, &result);
1153
+ CHECK_STATUS;
1154
+ return result;
1155
+ }
1156
+
1157
+ napi_value setCodecParHeight(napi_env env, napi_callback_info info) {
1158
+ napi_status status;
1159
+ napi_value result;
1160
+ napi_valuetype type;
1161
+ AVCodecParameters* c;
1162
+
1163
+ size_t argc = 1;
1164
+ napi_value args[1];
1165
+
1166
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1167
+ CHECK_STATUS;
1168
+ if (argc < 1) {
1169
+ NAPI_THROW_ERROR("Codec parameter height must be provided with a value.");
1170
+ }
1171
+ status = napi_typeof(env, args[0], &type);
1172
+ CHECK_STATUS;
1173
+ if (type != napi_number) {
1174
+ NAPI_THROW_ERROR("Codec parameter height must be set with a number.");
1175
+ }
1176
+ status = napi_get_value_int32(env, args[0], &c->height);
1177
+ CHECK_STATUS;
1178
+
1179
+ status = napi_get_undefined(env, &result);
1180
+ CHECK_STATUS;
1181
+ return result;
1182
+ }
1183
+
1184
+ napi_value getCodecParVideoDelay(napi_env env, napi_callback_info info) {
1185
+ napi_status status;
1186
+ napi_value result;
1187
+ AVCodecParameters* c;
1188
+
1189
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1190
+ CHECK_STATUS;
1191
+
1192
+ status = napi_create_int32(env, c->video_delay, &result);
1193
+ CHECK_STATUS;
1194
+ return result;
1195
+ }
1196
+
1197
+ napi_value setCodecParVideoDelay(napi_env env, napi_callback_info info) {
1198
+ napi_status status;
1199
+ napi_value result;
1200
+ napi_valuetype type;
1201
+ AVCodecParameters* c;
1202
+
1203
+ size_t argc = 1;
1204
+ napi_value args[1];
1205
+
1206
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1207
+ CHECK_STATUS;
1208
+ if (argc < 1) {
1209
+ NAPI_THROW_ERROR("Codec parameter video_delay must be provided with a value.");
1210
+ }
1211
+ status = napi_typeof(env, args[0], &type);
1212
+ CHECK_STATUS;
1213
+ if (type != napi_number) {
1214
+ NAPI_THROW_ERROR("Codec parameter video_delay must be set with a number.");
1215
+ }
1216
+ status = napi_get_value_int32(env, args[0], &c->video_delay);
1217
+ CHECK_STATUS;
1218
+
1219
+ status = napi_get_undefined(env, &result);
1220
+ CHECK_STATUS;
1221
+ return result;
1222
+ }
1223
+
1224
+
1225
+ napi_value getCodecParChannels(napi_env env, napi_callback_info info) {
1226
+ napi_status status;
1227
+ napi_value result;
1228
+ AVCodecParameters* c;
1229
+
1230
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1231
+ CHECK_STATUS;
1232
+
1233
+ status = napi_create_int32(env, c->channels, &result);
1234
+ CHECK_STATUS;
1235
+ return result;
1236
+ }
1237
+
1238
+ napi_value setCodecParChannels(napi_env env, napi_callback_info info) {
1239
+ napi_status status;
1240
+ napi_value result;
1241
+ napi_valuetype type;
1242
+ AVCodecParameters* c;
1243
+
1244
+ size_t argc = 1;
1245
+ napi_value args[1];
1246
+
1247
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1248
+ CHECK_STATUS;
1249
+ if (argc < 1) {
1250
+ NAPI_THROW_ERROR("Codec parameter channels must be provided with a value.");
1251
+ }
1252
+ status = napi_typeof(env, args[0], &type);
1253
+ CHECK_STATUS;
1254
+ if (type != napi_number) {
1255
+ NAPI_THROW_ERROR("Codec parameter channels must be set with a number.");
1256
+ }
1257
+ status = napi_get_value_int32(env, args[0], &c->channels);
1258
+ CHECK_STATUS;
1259
+
1260
+ status = napi_get_undefined(env, &result);
1261
+ CHECK_STATUS;
1262
+ return result;
1263
+ }
1264
+
1265
+ napi_value getCodecParSampleRate(napi_env env, napi_callback_info info) {
1266
+ napi_status status;
1267
+ napi_value result;
1268
+ AVCodecParameters* c;
1269
+
1270
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1271
+ CHECK_STATUS;
1272
+
1273
+ status = napi_create_int32(env, c->sample_rate, &result);
1274
+ CHECK_STATUS;
1275
+ return result;
1276
+ }
1277
+
1278
+ napi_value setCodecParSampleRate(napi_env env, napi_callback_info info) {
1279
+ napi_status status;
1280
+ napi_value result;
1281
+ napi_valuetype type;
1282
+ AVCodecParameters* c;
1283
+
1284
+ size_t argc = 1;
1285
+ napi_value args[1];
1286
+
1287
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1288
+ CHECK_STATUS;
1289
+ if (argc < 1) {
1290
+ NAPI_THROW_ERROR("Codec parameter sample_rate must be provided with a value.");
1291
+ }
1292
+ status = napi_typeof(env, args[0], &type);
1293
+ CHECK_STATUS;
1294
+ if (type != napi_number) {
1295
+ NAPI_THROW_ERROR("Codec parameter sample_rate must be set with a number.");
1296
+ }
1297
+ status = napi_get_value_int32(env, args[0], &c->sample_rate);
1298
+ CHECK_STATUS;
1299
+
1300
+ status = napi_get_undefined(env, &result);
1301
+ CHECK_STATUS;
1302
+ return result;
1303
+ }
1304
+
1305
+ napi_value getCodecParBlockAlign(napi_env env, napi_callback_info info) {
1306
+ napi_status status;
1307
+ napi_value result;
1308
+ AVCodecParameters* c;
1309
+
1310
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1311
+ CHECK_STATUS;
1312
+
1313
+ status = napi_create_int32(env, c->block_align, &result);
1314
+ CHECK_STATUS;
1315
+ return result;
1316
+ }
1317
+
1318
+ napi_value setCodecParBlockAlign(napi_env env, napi_callback_info info) {
1319
+ napi_status status;
1320
+ napi_value result;
1321
+ napi_valuetype type;
1322
+ AVCodecParameters* c;
1323
+
1324
+ size_t argc = 1;
1325
+ napi_value args[1];
1326
+
1327
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1328
+ CHECK_STATUS;
1329
+ if (argc < 1) {
1330
+ NAPI_THROW_ERROR("Codec parameter block_align must be provided with a value.");
1331
+ }
1332
+ status = napi_typeof(env, args[0], &type);
1333
+ CHECK_STATUS;
1334
+ if (type != napi_number) {
1335
+ NAPI_THROW_ERROR("Codec parameter block_align must be set with a number.");
1336
+ }
1337
+ status = napi_get_value_int32(env, args[0], &c->block_align);
1338
+ CHECK_STATUS;
1339
+
1340
+ status = napi_get_undefined(env, &result);
1341
+ CHECK_STATUS;
1342
+ return result;
1343
+ }
1344
+
1345
+
1346
+ napi_value getCodecParFrameSize(napi_env env, napi_callback_info info) {
1347
+ napi_status status;
1348
+ napi_value result;
1349
+ AVCodecParameters* c;
1350
+
1351
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1352
+ CHECK_STATUS;
1353
+
1354
+ status = napi_create_int32(env, c->frame_size, &result);
1355
+ CHECK_STATUS;
1356
+ return result;
1357
+ }
1358
+
1359
+ napi_value setCodecParFrameSize(napi_env env, napi_callback_info info) {
1360
+ napi_status status;
1361
+ napi_value result;
1362
+ napi_valuetype type;
1363
+ AVCodecParameters* c;
1364
+
1365
+ size_t argc = 1;
1366
+ napi_value args[1];
1367
+
1368
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1369
+ CHECK_STATUS;
1370
+ if (argc < 1) {
1371
+ NAPI_THROW_ERROR("Codec parameter frame_size must be provided with a value.");
1372
+ }
1373
+ status = napi_typeof(env, args[0], &type);
1374
+ CHECK_STATUS;
1375
+ if (type != napi_number) {
1376
+ NAPI_THROW_ERROR("Codec parameter frame_size must be set with a number.");
1377
+ }
1378
+ status = napi_get_value_int32(env, args[0], &c->frame_size);
1379
+ CHECK_STATUS;
1380
+
1381
+ status = napi_get_undefined(env, &result);
1382
+ CHECK_STATUS;
1383
+ return result;
1384
+ }
1385
+
1386
+ napi_value getCodecParInitialPad(napi_env env, napi_callback_info info) {
1387
+ napi_status status;
1388
+ napi_value result;
1389
+ AVCodecParameters* c;
1390
+
1391
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1392
+ CHECK_STATUS;
1393
+
1394
+ status = napi_create_int32(env, c->initial_padding, &result);
1395
+ CHECK_STATUS;
1396
+ return result;
1397
+ }
1398
+
1399
+ napi_value setCodecParInitialPad(napi_env env, napi_callback_info info) {
1400
+ napi_status status;
1401
+ napi_value result;
1402
+ napi_valuetype type;
1403
+ AVCodecParameters* c;
1404
+
1405
+ size_t argc = 1;
1406
+ napi_value args[1];
1407
+
1408
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1409
+ CHECK_STATUS;
1410
+ if (argc < 1) {
1411
+ NAPI_THROW_ERROR("Codec parameter initial_padding must be provided with a value.");
1412
+ }
1413
+ status = napi_typeof(env, args[0], &type);
1414
+ CHECK_STATUS;
1415
+ if (type != napi_number) {
1416
+ NAPI_THROW_ERROR("Codec parameter initial_padding must be set with a number.");
1417
+ }
1418
+ status = napi_get_value_int32(env, args[0], &c->initial_padding);
1419
+ CHECK_STATUS;
1420
+
1421
+ status = napi_get_undefined(env, &result);
1422
+ CHECK_STATUS;
1423
+ return result;
1424
+ }
1425
+
1426
+ napi_value getCodecParTrailingPad(napi_env env, napi_callback_info info) {
1427
+ napi_status status;
1428
+ napi_value result;
1429
+ AVCodecParameters* c;
1430
+
1431
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1432
+ CHECK_STATUS;
1433
+
1434
+ status = napi_create_int32(env, c->trailing_padding, &result);
1435
+ CHECK_STATUS;
1436
+ return result;
1437
+ }
1438
+
1439
+ napi_value setCodecParTrailingPad(napi_env env, napi_callback_info info) {
1440
+ napi_status status;
1441
+ napi_value result;
1442
+ napi_valuetype type;
1443
+ AVCodecParameters* c;
1444
+
1445
+ size_t argc = 1;
1446
+ napi_value args[1];
1447
+
1448
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1449
+ CHECK_STATUS;
1450
+ if (argc < 1) {
1451
+ NAPI_THROW_ERROR("Codec parameter trailing_padding must be provided with a value.");
1452
+ }
1453
+ status = napi_typeof(env, args[0], &type);
1454
+ CHECK_STATUS;
1455
+ if (type != napi_number) {
1456
+ NAPI_THROW_ERROR("Codec parameter trailing_padding must be set with a number.");
1457
+ }
1458
+ status = napi_get_value_int32(env, args[0], &c->trailing_padding);
1459
+ CHECK_STATUS;
1460
+
1461
+ status = napi_get_undefined(env, &result);
1462
+ CHECK_STATUS;
1463
+ return result;
1464
+ }
1465
+
1466
+ napi_value getCodecParSeekPreroll(napi_env env, napi_callback_info info) {
1467
+ napi_status status;
1468
+ napi_value result;
1469
+ AVCodecParameters* c;
1470
+
1471
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1472
+ CHECK_STATUS;
1473
+
1474
+ status = napi_create_int32(env, c->seek_preroll, &result);
1475
+ CHECK_STATUS;
1476
+ return result;
1477
+ }
1478
+
1479
+ napi_value setCodecParSeekPreroll(napi_env env, napi_callback_info info) {
1480
+ napi_status status;
1481
+ napi_value result;
1482
+ napi_valuetype type;
1483
+ AVCodecParameters* c;
1484
+
1485
+ size_t argc = 1;
1486
+ napi_value args[1];
1487
+
1488
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1489
+ CHECK_STATUS;
1490
+ if (argc < 1) {
1491
+ NAPI_THROW_ERROR("Codec parameter seek_preroll must be provided with a value.");
1492
+ }
1493
+ status = napi_typeof(env, args[0], &type);
1494
+ CHECK_STATUS;
1495
+ if (type != napi_number) {
1496
+ NAPI_THROW_ERROR("Codec parameter seek_preroll must be set with a number.");
1497
+ }
1498
+ status = napi_get_value_int32(env, args[0], &c->seek_preroll);
1499
+ CHECK_STATUS;
1500
+
1501
+ status = napi_get_undefined(env, &result);
1502
+ CHECK_STATUS;
1503
+ return result;
1504
+ }
1505
+
1506
+ napi_value getCodecParProfile(napi_env env, napi_callback_info info) {
1507
+ napi_status status;
1508
+ napi_value result;
1509
+ AVCodecParameters* c;
1510
+ const char* profName;
1511
+
1512
+ status = napi_get_cb_info(env, info, 0, nullptr, nullptr, (void**) &c);
1513
+ CHECK_STATUS;
1514
+
1515
+ profName = avcodec_profile_name(c->codec_id, c->profile);
1516
+ if (profName != nullptr) {
1517
+ status = napi_create_string_utf8(env, (char*) profName,
1518
+ NAPI_AUTO_LENGTH, &result);
1519
+ CHECK_STATUS;
1520
+ } else {
1521
+ status = napi_create_int32(env, c->profile, &result);
1522
+ CHECK_STATUS;
1523
+ }
1524
+
1525
+ return result;
1526
+ }
1527
+
1528
+ napi_value setCodecParProfile(napi_env env, napi_callback_info info) {
1529
+ napi_status status;
1530
+ napi_value result;
1531
+ napi_valuetype type;
1532
+ AVCodecParameters* c;
1533
+ char* enumString;
1534
+ size_t strLen;
1535
+ const AVProfile* profile;
1536
+ const AVCodecDescriptor* codecDesc;
1537
+ bool foundProfile = false;
1538
+
1539
+ size_t argc = 1;
1540
+ napi_value args[1];
1541
+
1542
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, (void**) &c);
1543
+ CHECK_STATUS;
1544
+ if (argc < 1) {
1545
+ NAPI_THROW_ERROR("Codec parameter profile must be provided with a value.");
1546
+ }
1547
+ status = napi_typeof(env, args[0], &type);
1548
+ CHECK_STATUS;
1549
+ if ((type == napi_null) || (type == napi_undefined)) {
1550
+ c->profile = FF_PROFILE_UNKNOWN;
1551
+ goto done;
1552
+ }
1553
+ if (type == napi_number) {
1554
+ status = napi_get_value_int32(env, args[0], &c->profile);
1555
+ CHECK_STATUS;
1556
+ goto done;
1557
+ }
1558
+ if (type != napi_string) {
1559
+ NAPI_THROW_ERROR("Codec parameter profile must be set by integer or string.");
1560
+ }
1561
+ status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &strLen);
1562
+ CHECK_STATUS;
1563
+ enumString = (char*) malloc(sizeof(char) * (strLen + 1));
1564
+ CHECK_STATUS;
1565
+ status = napi_get_value_string_utf8(env, args[0], enumString, strLen + 1, &strLen);
1566
+ CHECK_STATUS;
1567
+
1568
+ codecDesc = avcodec_descriptor_get(c->codec_id);
1569
+ if (codecDesc == nullptr) goto done;
1570
+ profile = codecDesc->profiles;
1571
+ if (!profile) {
1572
+ printf("Failed to set codec profile \'%s\' - recognised profiles not available for codec \'%s\'.\n", enumString, codecDesc->name);
1573
+ printf("Set profile as a numeric value to work around this problem.\n");
1574
+ c->profile = FF_PROFILE_UNKNOWN;
1575
+ } else {
1576
+ while (profile->profile != FF_PROFILE_UNKNOWN) {
1577
+ if (strcmp(enumString, profile->name) == 0) {
1578
+ c->profile = profile->profile;
1579
+ foundProfile = true;
1580
+ break;
1581
+ }
1582
+ profile = profile + 1;
1583
+ }
1584
+ if (!foundProfile) {
1585
+ c->profile = FF_PROFILE_UNKNOWN;
1586
+ printf("Failed to find codec profile \'%s\' in recognised profiles.\n", enumString);
1587
+ }
1588
+ }
1589
+ free(enumString);
1590
+
1591
+ done:
1592
+ status = napi_get_undefined(env, &result);
1593
+ CHECK_STATUS;
1594
+ return result;
1595
+ }
1596
+
1597
+ napi_value getCodecParTypeName(napi_env env, napi_callback_info info) {
1598
+ napi_status status;
1599
+ napi_value result;
1600
+
1601
+ status = napi_create_string_utf8(env, "CodecParameters", NAPI_AUTO_LENGTH, &result);
1602
+ CHECK_STATUS;
1603
+
1604
+ return result;
1605
+ }
1606
+
1607
+ napi_value makeCodecParameters(napi_env env, napi_callback_info info) {
1608
+ return makeCodecParamsInternal(env, info, true);
1609
+ }
1610
+
1611
+ napi_value makeCodecParamsInternal(napi_env env, napi_callback_info info, bool ownAlloc) {
1612
+ napi_status status;
1613
+ napi_value result, global, jsObject, assign, jsJSON, jsParse;
1614
+ napi_valuetype type;
1615
+ bool isArray, deleted;
1616
+ AVCodecParameters* c = avcodec_parameters_alloc();
1617
+
1618
+ status = napi_get_global(env, &global);
1619
+ CHECK_STATUS;
1620
+
1621
+ size_t argc = 1;
1622
+ napi_value args[1];
1623
+
1624
+ status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1625
+ CHECK_STATUS;
1626
+ if (argc > 1) {
1627
+ NAPI_THROW_ERROR("Parameters may be created with zero or one options object argument.");
1628
+ }
1629
+ if (argc == 1) {
1630
+ status = napi_typeof(env, args[0], &type);
1631
+ CHECK_STATUS;
1632
+ if (type == napi_string) {
1633
+ status = napi_get_named_property(env, global, "JSON", &jsJSON);
1634
+ CHECK_STATUS;
1635
+ status = napi_get_named_property(env, jsJSON, "parse", &jsParse);
1636
+ CHECK_STATUS;
1637
+ const napi_value pargs[] = { args[0] };
1638
+ status = napi_call_function(env, args[0], jsParse, 1, pargs, &args[0]);
1639
+ CHECK_STATUS;
1640
+ status = napi_typeof(env, args[0], &type);
1641
+ CHECK_STATUS;
1642
+ if (type == napi_object) {
1643
+ status = beam_delete_named_property(env, args[0], "type", &deleted);
1644
+ CHECK_STATUS;
1645
+ status = beam_delete_named_property(env, args[0], "name", &deleted);
1646
+ CHECK_STATUS;
1647
+ }
1648
+ }
1649
+ status = napi_is_array(env, args[0], &isArray);
1650
+ CHECK_STATUS;
1651
+ if (isArray || (type != napi_object)) {
1652
+ NAPI_THROW_ERROR("Cannot create codec parameters unless argument is an object.");
1653
+ }
1654
+ }
1655
+
1656
+ status = fromAVCodecParameters(env, c, ownAlloc, &result);
1657
+ CHECK_STATUS;
1658
+
1659
+ if (argc == 1) {
1660
+ status = napi_get_named_property(env, global, "Object", &jsObject);
1661
+ CHECK_STATUS;
1662
+ status = napi_get_named_property(env, jsObject, "assign", &assign);
1663
+ CHECK_STATUS;
1664
+ const napi_value fargs[] = { result, args[0] };
1665
+ status = napi_call_function(env, result, assign, 2, fargs, &result);
1666
+ CHECK_STATUS;
1667
+ }
1668
+
1669
+ if ((c->codec_type == AVMEDIA_TYPE_UNKNOWN) && (c->codec_id != AV_CODEC_ID_NONE)) {
1670
+ c->codec_type = avcodec_get_type(c->codec_id);
1671
+ }
1672
+
1673
+ return result;
1674
+ }
1675
+
1676
+ napi_value codecParToJSON(napi_env env, napi_callback_info info) {
1677
+ napi_status status;
1678
+ napi_value result;
1679
+ AVCodecParameters* c;
1680
+ int count = 0;
1681
+
1682
+ size_t argc = 0;
1683
+ status = napi_get_cb_info(env, info, &argc, nullptr, nullptr, (void**) &c);
1684
+ CHECK_STATUS;
1685
+
1686
+ status = napi_create_object(env, &result);
1687
+ CHECK_STATUS;
1688
+
1689
+ napi_property_descriptor desc[35];
1690
+ DECLARE_GETTER3("type", true, getCodecParTypeName, c);
1691
+ DECLARE_GETTER3("codec_type", true, getCodecParCodecType, c);
1692
+ DECLARE_GETTER3("codec_id", true, getCodecParCodecID, c);
1693
+ DECLARE_GETTER3("name", true, getCodecParName, c);
1694
+ DECLARE_GETTER3("codec_tag", c->codec_tag > 0, getCodecParCodecTag, c);
1695
+ DECLARE_GETTER3("extradata", c->extradata != nullptr, getCodecParExtraData, c);
1696
+ DECLARE_GETTER3("format", c->format >= 0, getCodecParFormat, c);
1697
+ DECLARE_GETTER3("bit_rate", c->bit_rate > 0, getCodecParBitRate, c);
1698
+ DECLARE_GETTER3("bits_per_coded_sample", c->bits_per_coded_sample > 0, getCodecParBitsPerCodedSmp, c);
1699
+ // 10
1700
+ DECLARE_GETTER3("bits_per_raw_sample", c->bits_per_raw_sample > 0, getCodecParBitsPerRawSmp, c);
1701
+ DECLARE_GETTER3("profile", c->profile != FF_PROFILE_UNKNOWN, getCodecParProfile, c);
1702
+ DECLARE_GETTER3("level", c->level != FF_LEVEL_UNKNOWN, getCodecParLevel, c);
1703
+ DECLARE_GETTER3("width", c->width != 0, getCodecParWidth, c);
1704
+ DECLARE_GETTER3("height", c->height != 0, getCodecParHeight, c);
1705
+ DECLARE_GETTER3("sample_aspect_ratio",
1706
+ (c->sample_aspect_ratio.num != 0) || (c->sample_aspect_ratio.den != 1),
1707
+ getCodecParSmpAspectRt, c);
1708
+ DECLARE_GETTER3("field_order", c->field_order != AV_FIELD_UNKNOWN, getCodecParFieldOrder, c);
1709
+ DECLARE_GETTER3("color_range", c->color_range != AVCOL_RANGE_UNSPECIFIED, getCodecParColorRange, c);
1710
+ DECLARE_GETTER3("color_primaries", c->color_primaries != AVCOL_PRI_UNSPECIFIED, getCodecParColorPrims, c);
1711
+ DECLARE_GETTER3("color_trc", c->color_trc != AVCOL_TRC_UNSPECIFIED, getCodecParColorTrc, c);
1712
+ // 20
1713
+ DECLARE_GETTER3("color_space", c->color_space != AVCOL_SPC_UNSPECIFIED, getCodecParColorSpace, c);
1714
+ DECLARE_GETTER3("chroma_location", c->chroma_location != AVCHROMA_LOC_UNSPECIFIED, getCodecParChromaLoc, c);
1715
+ DECLARE_GETTER3("video_delay", c->video_delay != 0, getCodecParVideoDelay, c);
1716
+ DECLARE_GETTER3("channel_layout", c->channel_layout != 0, getCodecParChanLayout, c);
1717
+ DECLARE_GETTER3("channels", c->channels > 0, getCodecParChannels, c);
1718
+ DECLARE_GETTER3("sample_rate", c->sample_rate > 0, getCodecParSampleRate, c);
1719
+ DECLARE_GETTER3("block_align", c->block_align > 0, getCodecParBlockAlign, c);
1720
+ DECLARE_GETTER3("frame_size", c->frame_size > 0, getCodecParFrameSize, c);
1721
+ DECLARE_GETTER3("initial_padding", c->initial_padding > 0, getCodecParInitialPad, c);
1722
+ DECLARE_GETTER3("trailing_padding", c->trailing_padding > 0, getCodecParTrailingPad, c);
1723
+ // 30
1724
+ DECLARE_GETTER3("seek_preroll", c->seek_preroll > 0, getCodecParSeekPreroll, c);
1725
+
1726
+ status = napi_define_properties(env, result, count, desc);
1727
+ CHECK_STATUS;
1728
+
1729
+ return result;
1730
+ }
1731
+
1732
+ napi_status fromAVCodecParameters(napi_env env, AVCodecParameters* c, bool ownAlloc, napi_value* result) {
1733
+ napi_status status;
1734
+ napi_value jsCodecPar, extCodecPar;
1735
+
1736
+ status = napi_create_object(env, &jsCodecPar);
1737
+ PASS_STATUS;
1738
+ status = napi_create_external(env, c, ownAlloc?codecParamsFinalizer:nullptr, nullptr, &extCodecPar);
1739
+ PASS_STATUS;
1740
+
1741
+ napi_property_descriptor desc[] = {
1742
+ { "type", nullptr, nullptr, getCodecParTypeName, nop, nullptr, napi_enumerable, nullptr },
1743
+ { "codec_type", nullptr, nullptr, getCodecParCodecType, setCodecParCodecType, nullptr,
1744
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1745
+ { "codec_id", nullptr, nullptr, getCodecParCodecID, setCodecParCodecID, nullptr,
1746
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1747
+ { "name", nullptr, nullptr, getCodecParName, setCodecParName, nullptr,
1748
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1749
+ { "codec_tag", nullptr, nullptr, getCodecParCodecTag, setCodecParCodecTag, nullptr,
1750
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1751
+ { "extradata", nullptr, nullptr, getCodecParExtraData, setCodecParExtraData, nullptr,
1752
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1753
+ { "format", nullptr, nullptr, getCodecParFormat, setCodecParFormat, nullptr,
1754
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1755
+ { "bit_rate", nullptr, nullptr, getCodecParBitRate, setCodecParBitRate, nullptr,
1756
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1757
+ { "bits_per_coded_sample", nullptr, nullptr, getCodecParBitsPerCodedSmp, setCodecParBitsPerCodedSmp, nullptr,
1758
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1759
+ { "bits_per_raw_sample", nullptr, nullptr, getCodecParBitsPerRawSmp, setCodecParBitsPerRawSmp, nullptr,
1760
+ (napi_property_attributes) (napi_writable | napi_enumerable), c }, // 10
1761
+ { "profile", nullptr, nullptr, getCodecParProfile, setCodecParProfile, nullptr,
1762
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1763
+ { "level", nullptr, nullptr, getCodecParLevel, setCodecParLevel, nullptr,
1764
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1765
+ { "width", nullptr, nullptr, getCodecParWidth, setCodecParWidth, nullptr,
1766
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1767
+ { "height", nullptr, nullptr, getCodecParHeight, setCodecParHeight, nullptr,
1768
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1769
+ { "sample_aspect_ratio", nullptr, nullptr, getCodecParSmpAspectRt, setCodecParSmpAspectRt, nullptr,
1770
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1771
+ { "field_order", nullptr, nullptr, getCodecParFieldOrder, setCodecParFieldOrder, nullptr,
1772
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1773
+ { "color_range", nullptr, nullptr, getCodecParColorRange, setCodecParColorRange, nullptr,
1774
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1775
+ { "color_primaries", nullptr, nullptr, getCodecParColorPrims, setCodecParColorPrims, nullptr,
1776
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1777
+ { "color_trc", nullptr, nullptr, getCodecParColorTrc, setCodecParColorTrc, nullptr,
1778
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1779
+ { "color_space", nullptr, nullptr, getCodecParColorSpace, setCodecParColorSpace, nullptr,
1780
+ (napi_property_attributes) (napi_writable | napi_enumerable), c }, // 20
1781
+ { "chroma_location", nullptr, nullptr, getCodecParChromaLoc, setCodecParChromaLoc, nullptr,
1782
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1783
+ { "video_delay", nullptr, nullptr, getCodecParVideoDelay, setCodecParVideoDelay, nullptr,
1784
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1785
+ { "channel_layout", nullptr, nullptr, getCodecParChanLayout, setCodecParChanLayout, nullptr,
1786
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1787
+ { "channels", nullptr, nullptr, getCodecParChannels, setCodecParChannels, nullptr,
1788
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1789
+ { "sample_rate", nullptr, nullptr, getCodecParSampleRate, setCodecParSampleRate, nullptr,
1790
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1791
+ { "block_align", nullptr, nullptr, getCodecParBlockAlign, setCodecParBlockAlign, nullptr,
1792
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1793
+ { "frame_size", nullptr, nullptr, getCodecParFrameSize, setCodecParFrameSize, nullptr,
1794
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1795
+ { "initial_padding", nullptr, nullptr, getCodecParInitialPad, setCodecParInitialPad, nullptr,
1796
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1797
+ { "trailing_padding", nullptr, nullptr, getCodecParTrailingPad, setCodecParTrailingPad, nullptr,
1798
+ (napi_property_attributes) (napi_writable | napi_enumerable), c },
1799
+ { "seek_preroll", nullptr, nullptr, getCodecParSeekPreroll, setCodecParSeekPreroll, nullptr,
1800
+ (napi_property_attributes) (napi_writable | napi_enumerable), c }, // 30
1801
+ { "toJSON", nullptr, codecParToJSON, nullptr, nullptr, nullptr, napi_default, c},
1802
+ { "_codecPar", nullptr, nullptr, nullptr, nullptr, extCodecPar, napi_default, nullptr }
1803
+ };
1804
+ status = napi_define_properties(env, jsCodecPar, 32, desc);
1805
+ PASS_STATUS;
1806
+
1807
+ *result = jsCodecPar;
1808
+ return napi_ok;
1809
+ };
1810
+
1811
+ void codecParamsFinalizer(napi_env env, void* data, void* hint) {
1812
+ AVCodecParameters* c = (AVCodecParameters*) data;
1813
+ // if ((c->extradata != nullptr) && (c->extradata_size > 0)) {
1814
+ // av_freep(&c->extradata);
1815
+ // c->extradata_size = 0;
1816
+ // }
1817
+ avcodec_parameters_free(&c);
1818
+ }