forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpeg.cpp
More file actions
executable file
·2388 lines (2114 loc) · 86 KB
/
Copy pathffmpeg.cpp
File metadata and controls
executable file
·2388 lines (2114 loc) · 86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//FFMPEG
//Requires FFMPEG/5.1.x or better
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/mathematics.h>
#include <libavutil/timestamp.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
#include <chrono>
//returned by Decoder.read()
#define END_FRAME -1
#define NULL_FRAME 0 //could be metadata frame
#define AUDIO_FRAME 1
#define VIDEO_FRAME 2
static jboolean ffmpeg_loaded = JNI_FALSE;
static jboolean ff_debug_log = JNI_FALSE;
static jboolean ff_debug_trace = JNI_FALSE;
static jboolean ff_debug_buffer = JNI_FALSE;
static jboolean ff_debug_box = JNI_FALSE;
static jboolean ff_debug_time_base = JNI_FALSE;
JF_LIB_HANDLE codec = NULL;
JF_LIB_HANDLE device = NULL;
JF_LIB_HANDLE ffilter = NULL;
JF_LIB_HANDLE format = NULL;
JF_LIB_HANDLE util = NULL;
JF_LIB_HANDLE resample = NULL;
JF_LIB_HANDLE postproc = NULL;
JF_LIB_HANDLE scale = NULL;
jboolean shownCopyWarning = JNI_FALSE;
static AVChannelLayout channel_layout_1;
static AVChannelLayout channel_layout_2;
static AVChannelLayout channel_layout_4;
static void copyWarning() {
printf("Warning : JNI::Get*ArrayElements returned a copy : Performance will be degraded!\n");
shownCopyWarning = JNI_TRUE;
}
//avcodec functions
AVCodec* (*_avcodec_find_decoder)(int codec_id);
int (*_avcodec_open2)(AVCodecContext *avctx,AVCodec *codec,void* options);
AVCodecContext* (*_avcodec_alloc_context3)(AVCodec *codec);
void (*_avcodec_free_context)(AVCodecContext **ctx);
void (*_av_init_packet)(AVPacket *pkt);
void (*_av_packet_free)(AVPacket **pkt);
void (*_av_packet_free_side_data)(AVPacket *pkt);
//encoding
AVCodec* (*_avcodec_find_encoder)(int codec_id);
//int (*_avpicture_alloc)(AVPicture *pic, int pix_fmt, int width, int height);
//int (*_avpicture_free)(AVPicture *pic);
int (*_avcodec_fill_audio_frame)(AVFrame *frame, int nb_channels, int fmt, void* buf, int bufsize, int align);
//int (*_avcodec_close)(AVCodecContext *cc);
const char* (*_avcodec_get_name)(AVCodecID id);
void (*_av_packet_rescale_ts)(AVPacket *pkt, AVRational src, AVRational dst);
int (*_avcodec_parameters_to_context)(AVCodecContext *ctx, const AVCodecParameters *par);
int (*_avcodec_parameters_from_context)(AVCodecParameters *par, const AVCodecContext *ctx);
int (*_avcodec_version)();
//encoding
int (*_avcodec_send_frame)(AVCodecContext *ctx, const AVFrame *frame);
int (*_avcodec_receive_packet)(AVCodecContext *ctx, const AVPacket *pkt);
//decoding
int (*_avcodec_send_packet)(AVCodecContext *ctx, const AVPacket *pkt);
int (*_avcodec_receive_frame)(AVCodecContext *ctx, const AVFrame *frame);
//avdevice functions
//avfilter functions
//avformat functions
AVOutputFormat* (*_av_guess_format)(const char* shortName, const char* fileName, const char* mimeType);
int (*_av_find_best_stream)(AVFormatContext *ic,int type,int wanted_stream_nb,int related_stream
,void** decoder_ret, int flags);
AVIOContext* (*_avio_alloc_context)(void* buffer,int buffer_size,int write_flag,void* opaque
,void* read,void* write,void* seek);
AVFormatContext* (*_avformat_alloc_context)();
int (*_avformat_alloc_output_context2)(AVFormatContext** fmt_ctx, AVOutputFormat* ofmt, const char* name, const char* filename);
int (*_avio_close)(void* ctx);
void (*_avformat_free_context)(AVFormatContext *s);
int (*_avformat_open_input)(void** ps,const char* filename,void* fmt,void* options);
int (*_avformat_find_stream_info)(AVFormatContext *ic,void** options);
int (*_av_read_frame)(AVFormatContext *s,AVPacket *pkt);
void* (*_av_find_input_format)(const char* name);
int (*_avformat_seek_file)(AVFormatContext *ctx, int stream_idx, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
int (*_av_seek_frame)(AVFormatContext *ctx, int stream_idx, int64_t ts, int flags);
//encoding
AVStream* (*_avformat_new_stream)(AVFormatContext *fc, AVCodec *codec);
int (*_avformat_write_header)(AVFormatContext *fc, void* opts);
int (*_av_interleaved_write_frame)(AVFormatContext *fc, AVPacket *pkt);
int (*_av_write_frame)(AVFormatContext *fc, AVPacket *pkt);
int (*_avio_flush)(void* io_ctx);
void (*_av_dump_format)(AVFormatContext *fmt_ctx, int index, const char* url, int is_output);
int (*_av_write_trailer)(AVFormatContext *fc);
int (*_avformat_version)();
//avutil functions
void (*_av_image_copy)(uint8_t* dst_data[],int dst_linesizes[]
, uint8_t* src_data[],int src_linesizes[],int pix_fmt,int width,int height);
int (*_av_get_bytes_per_sample)(int sample_fmt);
void* (*_av_malloc)(int size);
void* (*_av_mallocz)(int size);
void (*_av_free)(void* ptr);
void (*_av_freep)(void** ptr);
int (*_av_image_alloc)(uint8_t* ptrs[],int linesizes[],int w,int h,int pix_fmt,int align);
int (*_av_opt_set)(void* obj,const char* name,const char* val,int search_flags);
int (*_av_opt_set_int)(void* obj,const char* name,int64_t val,int search_flags);
int (*_av_opt_set_sample_fmt)(void* obj,const char* name,int val,int search_flags);
int (*_av_opt_get)(void* obj,const char* name,int search_flags,void* val[]);
int (*_av_opt_get_int)(void* obj,const char* name,int search_flags,int64_t val[]);
// int (*_av_opt_get_pixel_fmt)(void* obj,const char* name,int search_flags,int val[]);
void* (*_av_opt_find)(void* obj, const char* name, const char* unit, int opt_flags, int search_flags);
void* (*_av_opt_next)(void* obj, void* prev);
void* (*_av_opt_child_next)(void* obj, void* prev);
void (*_av_opt_set_defaults)(void* obj);
int64_t (*_av_rescale_rnd)(int64_t a,int64_t b,int64_t c,AVRounding r);
int (*_av_samples_alloc)(uint8_t* audio_data[],int linesize[],int nb_channels,int nb_samples,int sample_fmt,int align);
int64_t (*_av_rescale_q)(int64_t a, AVRational bq, AVRational cq);
int (*_av_samples_get_buffer_size)(void* linesize, int chs, int samples, int sample_fmt, int align);
int (*_av_log_set_level)(int lvl);
void* (*_av_dict_get)(AVDictionary* dict, const char* key, void* prev, int flags);
int (*_av_dict_set)(AVDictionary** dictref, const char* key, const char* value, int flags);
int (*_av_frame_make_writable)(AVFrame *frame);
int (*_av_compare_ts)(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b);
int (*_av_frame_get_buffer)(AVFrame *frame, int align);
AVFrame* (*_av_frame_alloc)();
void (*_av_frame_free)(void** frame);
void (*_av_buffer_unref)(AVBufferRef **buf);
int (*_av_channel_layout_copy) (AVChannelLayout* dst, const AVChannelLayout* src);
int (*_avutil_version)();
//swresample functions (ffmpeg.org)
void* (*_swr_alloc)();
int (*_swr_alloc_set_opts2)(void**, AVChannelLayout* out_ch_layout, int out_sample_fmt, int out_sample_rate, AVChannelLayout* in_ch_layout, int in_sample_fmt, int in_sample_rate, int log_offset, void*log_ctx);
int (*_swr_init)(void* ctx);
int64_t (*_swr_get_delay)(void* ctx,int64_t base);
int (*_swr_convert)(void* ctx,uint8_t* out_arg[],int out_count,uint8_t* in_arg[],int in_count);
void (*_swr_free)(void** ctx);
//swscale functions
void* (*_sws_getContext)(int srcW,int srcH,int srcFormat,int dstW,int dstH,int dstFormat
,int flags,void* srcFilter,void* dstFilter, void* param);
int (*_sws_scale)(void* c, uint8_t* srcSlice[],int srcStride[],int srcSliceY,int srcSliceH
,uint8_t* dst[],int dstStride[]);
void (*_sws_freeContext)(void* ctx);
static AVPacket *AVPacket_New() {
AVPacket *pkt = (AVPacket*)(*_av_mallocz)(sizeof(AVPacket));
return pkt;
}
static AVOutputFormat *AVOutputFormat_New() {
AVOutputFormat *ofmt = (AVOutputFormat*)(*_av_mallocz)(sizeof(AVOutputFormat));
return ofmt;
}
static AVOutputFormat *vpx = NULL;
//av_free_packet is deprecated : easy to implement
static void _av_free_packet(AVPacket *pkt) {
if (pkt) {
if (pkt->buf) (*_av_buffer_unref)(&pkt->buf);
pkt->data = NULL;
pkt->size = 0;
(*_av_packet_free_side_data)(pkt);
}
}
static void log_packet(const char* type, const AVFormatContext *fmt_ctx, const AVPacket *pkt)
{
AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
char pts[AV_TS_MAX_STRING_SIZE];
char pts_tb[AV_TS_MAX_STRING_SIZE];
char dts[AV_TS_MAX_STRING_SIZE];
char dts_tb[AV_TS_MAX_STRING_SIZE];
char dur[AV_TS_MAX_STRING_SIZE];
char dur_tb[AV_TS_MAX_STRING_SIZE];
printf("%s:pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s\n", type,
av_ts_make_string(pts, pkt->pts), av_ts_make_time_string(pts_tb, pkt->pts, time_base),
av_ts_make_string(dts, pkt->dts), av_ts_make_time_string(dts_tb, pkt->dts, time_base),
av_ts_make_string(dur, pkt->duration), av_ts_make_time_string(dur_tb, pkt->duration, time_base)
);
}
#ifndef _WIN32
int GetLastError() {
return errno;
}
#endif
static int64_t currentTimeMillis() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
static jboolean ffmpeg_init(const char* codecFile, const char* deviceFile, const char* filterFile, const char* formatFile
, const char* utilFile, const char* scaleFile, const char* postFile, const char* resampleFile)
{
//load libraries (order is important)
printf("ffmpeg init...");
util = loadLibrary(utilFile);
if (util == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), utilFile);
return JNI_FALSE;
}
resample = loadLibrary(resampleFile);
if (resample == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), resampleFile);
return JNI_FALSE;
}
scale = loadLibrary(scaleFile);
if (scale == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), scaleFile);
return JNI_FALSE;
}
postproc = loadLibrary(postFile);
if (postproc == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), postFile);
return JNI_FALSE;
}
codec = loadLibrary(codecFile);
if (codec == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), codecFile);
return JNI_FALSE;
}
format = loadLibrary(formatFile);
if (format == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), formatFile);
return JNI_FALSE;
}
ffilter = loadLibrary(filterFile);
if (ffilter == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), filterFile);
return JNI_FALSE;
}
device = loadLibrary(deviceFile);
if (device == NULL) {
printf("Could not find(0x%x):%s\n", GetLastError(), deviceFile);
return JNI_FALSE;
}
//get functions
getFunction(codec, (void**)&_avcodec_find_decoder, "avcodec_find_decoder");
getFunction(codec, (void**)&_avcodec_open2, "avcodec_open2");
getFunction(codec, (void**)&_avcodec_alloc_context3, "avcodec_alloc_context3");
getFunction(codec, (void**)&_avcodec_free_context, "avcodec_free_context");
getFunction(codec, (void**)&_av_init_packet, "av_init_packet");
getFunction(codec, (void**)&_av_packet_free, "av_packet_free");
getFunction(codec, (void**)&_av_packet_free_side_data, "av_packet_free_side_data");
getFunction(codec, (void**)&_avcodec_find_encoder, "avcodec_find_encoder");
// getFunction(codec, (void**)&_avpicture_alloc, "avpicture_alloc");
// getFunction(codec, (void**)&_avpicture_free, "avpicture_free");
getFunction(codec, (void**)&_avcodec_fill_audio_frame, "avcodec_fill_audio_frame");
// getFunction(codec, (void**)&_avcodec_close, "avcodec_close");
getFunction(codec, (void**)&_avcodec_get_name, "avcodec_get_name"); //for debug output only
getFunction(codec, (void**)&_av_packet_rescale_ts, "av_packet_rescale_ts");
getFunction(codec, (void**)&_avcodec_parameters_to_context, "avcodec_parameters_to_context");
getFunction(codec, (void**)&_avcodec_parameters_from_context, "avcodec_parameters_from_context");
getFunction(codec, (void**)&_avcodec_version, "avcodec_version");
getFunction(codec, (void**)&_avcodec_send_frame, "avcodec_send_frame");
getFunction(codec, (void**)&_avcodec_receive_packet, "avcodec_receive_packet");
getFunction(codec, (void**)&_avcodec_send_packet, "avcodec_send_packet");
getFunction(codec, (void**)&_avcodec_receive_frame, "avcodec_receive_frame");
getFunction(format, (void**)&_av_guess_format, "av_guess_format");
getFunction(format, (void**)&_av_find_best_stream, "av_find_best_stream");
getFunction(format, (void**)&_avio_alloc_context, "avio_alloc_context");
getFunction(format, (void**)&_avformat_alloc_context, "avformat_alloc_context");
getFunction(format, (void**)&_avformat_alloc_output_context2, "avformat_alloc_output_context2");
getFunction(format, (void**)&_avio_close, "avio_close");
getFunction(format, (void**)&_avformat_free_context, "avformat_free_context");
getFunction(format, (void**)&_avformat_open_input, "avformat_open_input");
getFunction(format, (void**)&_avformat_find_stream_info, "avformat_find_stream_info");
getFunction(format, (void**)&_av_read_frame, "av_read_frame");
getFunction(format, (void**)&_av_find_input_format, "av_find_input_format");
getFunction(format, (void**)&_avformat_seek_file, "avformat_seek_file");
getFunction(format, (void**)&_av_seek_frame, "av_seek_frame");
getFunction(format, (void**)&_avformat_new_stream, "avformat_new_stream");
getFunction(format, (void**)&_avformat_write_header, "avformat_write_header");
getFunction(format, (void**)&_av_interleaved_write_frame, "av_interleaved_write_frame");
getFunction(format, (void**)&_av_write_frame, "av_write_frame");
getFunction(format, (void**)&_avio_flush, "avio_flush");
getFunction(format, (void**)&_av_dump_format, "av_dump_format");
getFunction(format, (void**)&_av_write_trailer, "av_write_trailer");
getFunction(format, (void**)&_avformat_version, "avformat_version");
getFunction(util, (void**)&_av_image_copy, "av_image_copy");
getFunction(util, (void**)&_av_get_bytes_per_sample, "av_get_bytes_per_sample");
getFunction(util, (void**)&_av_malloc, "av_malloc");
getFunction(util, (void**)&_av_mallocz, "av_mallocz");
getFunction(util, (void**)&_av_free, "av_free");
getFunction(util, (void**)&_av_freep, "av_freep");
getFunction(util, (void**)&_av_image_alloc, "av_image_alloc");
getFunction(util, (void**)&_av_opt_set, "av_opt_set");
getFunction(util, (void**)&_av_opt_set_int, "av_opt_set_int");
getFunction(util, (void**)&_av_opt_set_sample_fmt, "av_opt_set_sample_fmt");
getFunction(util, (void**)&_av_opt_get, "av_opt_get");
getFunction(util, (void**)&_av_opt_get_int, "av_opt_get_int");
getFunction(util, (void**)&_av_opt_find, "av_opt_find");
getFunction(util, (void**)&_av_opt_next, "av_opt_next");
getFunction(util, (void**)&_av_opt_child_next, "av_opt_child_next");
getFunction(util, (void**)&_av_opt_set_defaults, "av_opt_set_defaults");
getFunction(util, (void**)&_av_rescale_rnd, "av_rescale_rnd");
getFunction(util, (void**)&_av_samples_alloc, "av_samples_alloc");
getFunction(util, (void**)&_av_rescale_q, "av_rescale_q");
getFunction(util, (void**)&_av_samples_get_buffer_size, "av_samples_get_buffer_size");
getFunction(util, (void**)&_av_log_set_level, "av_log_set_level");
getFunction(util, (void**)&_av_dict_get, "av_dict_get");
getFunction(util, (void**)&_av_dict_set, "av_dict_set");
getFunction(util, (void**)&_av_frame_make_writable, "av_frame_make_writable");
getFunction(util, (void**)&_av_compare_ts, "av_compare_ts");
getFunction(util, (void**)&_av_frame_get_buffer, "av_frame_get_buffer");
getFunction(util, (void**)&_av_frame_alloc, "av_frame_alloc");
getFunction(util, (void**)&_av_frame_free, "av_frame_free");
getFunction(util, (void**)&_av_buffer_unref, "av_buffer_unref");
getFunction(util, (void**)&_av_channel_layout_copy, "av_channel_layout_copy");
getFunction(util, (void**)&_avutil_version, "avutil_version");
getFunction(scale, (void**)&_sws_getContext, "sws_getContext");
getFunction(scale, (void**)&_sws_scale, "sws_scale");
getFunction(scale, (void**)&_sws_freeContext, "sws_freeContext");
getFunction(resample, (void**)&_swr_alloc, "swr_alloc");
getFunction(resample, (void**)&_swr_alloc_set_opts2, "swr_alloc_set_opts2");
getFunction(resample, (void**)&_swr_init, "swr_init");
getFunction(resample, (void**)&_swr_get_delay, "swr_get_delay");
getFunction(resample, (void**)&_swr_convert, "swr_convert");
getFunction(resample, (void**)&_swr_free, "swr_free");
//print version info
union {
int v32;
struct {
unsigned char micro, minor, major;
} v8;
} version;
//enable debugging
if (ff_debug_log) {
(*_av_log_set_level)(AV_LOG_TRACE);
}
//setup AVChannelLayout's to avoid using designated initializers
channel_layout_1.order = AV_CHANNEL_ORDER_NATIVE;
channel_layout_1.nb_channels = 1;
channel_layout_1.u.mask = AV_CH_LAYOUT_MONO;
channel_layout_1.opaque = NULL;
channel_layout_2.order = AV_CHANNEL_ORDER_NATIVE;
channel_layout_2.nb_channels = 2;
channel_layout_2.u.mask = AV_CH_LAYOUT_STEREO;
channel_layout_2.opaque = NULL;
channel_layout_4.order = AV_CHANNEL_ORDER_NATIVE;
channel_layout_4.nb_channels = 4;
channel_layout_4.u.mask = AV_CH_LAYOUT_QUAD;
channel_layout_4.opaque = NULL;
printf("ok\r\n");
#ifdef _JF_DEBUG
version.v32 = (*_avutil_version)();
printf("avutil_version=%d.%d.%d\n", version.v8.major, version.v8.minor, version.v8.micro);
version.v32 = (*_avcodec_version)();
printf("avcodec_version=%d.%d.%d\n", version.v8.major, version.v8.minor, version.v8.micro);
version.v32 = (*_avformat_version)();
printf("avformat_version=%d.%d.%d\n", version.v8.major, version.v8.minor, version.v8.micro);
#endif
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_javaforce_media_MediaCoder_ninit
(JNIEnv *e, jclass c, jstring jcodec, jstring jdevice, jstring jfilter, jstring jformat, jstring jutil, jstring jresample, jstring jpostproc, jstring jscale)
{
if (ffmpeg_loaded) return ffmpeg_loaded;
const char *codecFile = e->GetStringUTFChars(jcodec, NULL);
const char *deviceFile = e->GetStringUTFChars(jdevice, NULL);
const char *filterFile = e->GetStringUTFChars(jfilter, NULL);
const char *formatFile = e->GetStringUTFChars(jformat, NULL);
const char *utilFile = e->GetStringUTFChars(jutil, NULL);
const char *resampleFile = e->GetStringUTFChars(jresample, NULL);
const char *postFile = e->GetStringUTFChars(jpostproc, NULL);
const char *scaleFile = e->GetStringUTFChars(jscale, NULL);
jboolean ret = ffmpeg_init(codecFile, deviceFile, filterFile, formatFile, utilFile, resampleFile, postFile, scaleFile);
e->ReleaseStringUTFChars(jcodec, codecFile);
e->ReleaseStringUTFChars(jdevice, deviceFile);
e->ReleaseStringUTFChars(jfilter, filterFile);
e->ReleaseStringUTFChars(jformat, formatFile);
e->ReleaseStringUTFChars(jutil, utilFile);
e->ReleaseStringUTFChars(jresample, resampleFile);
e->ReleaseStringUTFChars(jpostproc, postFile);
e->ReleaseStringUTFChars(jscale, scaleFile);
if (!ret) return JNI_FALSE;
//get JNI IDs
ffmpeg_loaded = JNI_TRUE;
return JNI_TRUE;
}
JNIEXPORT void JNICALL Java_javaforce_media_MediaCoder_setLogging
(JNIEnv *e, jclass c, jboolean state)
{
(*_av_log_set_level)(state ? AV_LOG_ERROR : AV_LOG_QUIET);
}
static int ff_min(int a, int b) {
if (a < b) return a; else return b;
}
struct FFContext {
JNIEnv *e; //only valid during native function
jobject c; //only valid during native function
//MediaIO (these can be cached since the MediaIO object should not be GCed)
jclass cls_mio;
jmethodID mid_ff_read;
jmethodID mid_ff_write;
jmethodID mid_ff_seek;
//decoder fields
jobject mio;
AVFormatContext *fmt_ctx;
AVIOContext *io_ctx;
void* input_fmt;
AVCodecContext *codec_ctx; //returned by open_codec_context()
int video_stream_idx;
AVStream *video_stream;
AVCodecContext *video_codec_ctx;
void* sws_ctx;
uint8_t* decode_buffer;
int decode_buffer_size;
//compressed video
int video_dst_bufsize;
uint8_t* video_dst_data[4];
int video_dst_linesize[4];
//rgb video
int rgb_video_dst_bufsize;
uint8_t* rgb_video_dst_data[4];
int rgb_video_dst_linesize[4];
int audio_stream_idx;
AVStream *audio_stream;
AVCodecContext *audio_codec_ctx;
void* swr_ctx;
//compressed audio
int src_rate;
//user audio
int dst_rate;
int dst_nb_channels;
int dst_sample_fmt;
uint8_t* audio_dst_data[4];
int audio_dst_linesize[4];
AVPacket *pkt; //decoders only
jboolean pkt_key_frame;
AVFrame *frame;
jintArray jvideo;
jshortArray jvideo16;
int jvideo_length;
jshortArray jaudio;
int jaudio_length;
//additional raw video decoder fields
AVCodec *video_codec;
//additional encoder fields
AVCodec *audio_codec;
AVOutputFormat *out_fmt;
int width, height, fps;
int chs, freq;
jboolean scaleVideo;
int org_width, org_height;
AVFrame *audio_frame, *video_frame;
AVFrame *src_pic;
jboolean audio_frame_size_variable;
jboolean video_delay;
int audio_frame_size;
short *audio_buffer;
int audio_buffer_size;
int64_t audio_pts;
int64_t video_pts;
int64_t last_dts;
int64_t last_pts;
uint8_t* audio_src_data[4];
jboolean is_dash;
jboolean is_mp4;
/** Set to make fps = fps * 1000 / 1001. */
jboolean config_fps_1000_1001;
/** Number of frames per group of pictures (GOP).
* Determines how often key frame is generated.
* Default = 12
*/
int config_gop_size;
/** Video bit rate.
* Default = 400000
*/
int config_video_bit_rate;
/** Audio bit rate.
* Default = 128000
*/
int config_audio_bit_rate;
int config_compressionLevel;
/** ProfileLevel (1=baseline 2=main 3=high) */
int config_profileLevel;
void GetMediaIO() {
cls_mio = e->GetObjectClass(mio);
mid_ff_read = e->GetMethodID(cls_mio, "read", "(Ljavaforce/media/MediaCoder;[B)I");
mid_ff_write = e->GetMethodID(cls_mio, "write", "(Ljavaforce/media/MediaCoder;[B)I");
mid_ff_seek = e->GetMethodID(cls_mio, "seek", "(Ljavaforce/media/MediaCoder;JI)J");
}
};
#define ffiobufsiz (64 * 1024)
FFContext* createFFContext(JNIEnv *e, jobject c) {
FFContext *ctx;
jclass cls_coder = e->FindClass("javaforce/media/MediaCoder");
jfieldID fid_ff_ctx = e->GetFieldID(cls_coder, "ctx", "J");
ctx = (FFContext*)e->GetLongField(c, fid_ff_ctx);
if (ctx != NULL) {
printf("MediaCoder used twice\n");
return NULL;
}
ctx = (FFContext*)(*_av_mallocz)(sizeof(FFContext));
e->SetLongField(c,fid_ff_ctx,(jlong)ctx);
ctx->e = e;
ctx->c = c;
return ctx;
}
FFContext* getFFContext(JNIEnv *e, jobject c) {
FFContext *ctx;
jclass cls_coder = e->FindClass("javaforce/media/MediaCoder");
jfieldID fid_ff_ctx = e->GetFieldID(cls_coder, "ctx", "J");
ctx = (FFContext*)e->GetLongField(c, fid_ff_ctx);
if (ctx == NULL) return NULL;
ctx->e = e;
ctx->c = c;
return ctx;
}
void deleteFFContext(JNIEnv *e, jobject c, FFContext *ctx) {
if (ctx == NULL) return;
if (ctx->mio != NULL) {
e->DeleteGlobalRef(ctx->mio);
ctx->mio = NULL;
}
(*_av_free)(ctx);
jclass cls_coder = e->FindClass("javaforce/media/MediaCoder");
jfieldID fid_ff_ctx = e->GetFieldID(cls_coder, "ctx", "J");
e->SetLongField(c,fid_ff_ctx,0);
}
static int read_packet(FFContext *ctx, void*buf, int size) {
jbyteArray ba = ctx->e->NewByteArray(size);
jint read = ctx->e->CallIntMethod(ctx->mio, ctx->mid_ff_read, ctx->c, ba);
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
if (read > 0) {
ctx->e->GetByteArrayRegion(ba, 0, read, (jbyte*)buf);
}
ctx->e->DeleteLocalRef(ba);
return read;
}
typedef struct mp4_box {
int size;
char type[4];
} mp4_box;
static int swap_order(int val) {
union {
int i32;
char i8[4];
} be, le;
be.i32 = val;
le.i8[0] = be.i8[3];
le.i8[1] = be.i8[2];
le.i8[2] = be.i8[1];
le.i8[3] = be.i8[0];
return le.i32;
}
static int write_packet(FFContext *ctx, void*buf, int size) {
jbyteArray ba = ctx->e->NewByteArray(size);
ctx->e->SetByteArrayRegion(ba, 0, size, (jbyte*)buf);
int write = ctx->e->CallIntMethod(ctx->mio, ctx->mid_ff_write, ctx->c, ba); //obj, methodID, args[]
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
ctx->e->DeleteLocalRef(ba);
if (ctx->is_mp4 && ff_debug_box) {
int pkt_len = size;
char* buf8 = (char*)buf;
while (pkt_len > 0) {
mp4_box* box = (mp4_box*)buf8;
int box_size = swap_order(box->size);
if (box_size <= 0) break; //mid packet?
printf("box:%c%c%c%c:%d\n", box->type[0], box->type[1], box->type[2], box->type[3], box_size);
buf8 += box_size;
pkt_len -= box_size;
}
}
if (ctx->is_mp4 && ff_debug_buffer) {
char* chbuf = (char*)buf;
int len = size;
if (len > 1024) len = 1024;
printf("buf = [");
for(int a=0;a<len;a++) {
char ch = chbuf[a];
if (ch < 32 || ch > 127) {
printf("{%02x}", ch & 0xff);
} else {
printf("%c", ch);
}
}
if (size > 1024) {
printf("{...}");
}
printf("]\n");
}
return write;
}
static jlong zero = 0;
static jlong seek_packet(FFContext *ctx, jlong offset, int how) {
if (how == AVSEEK_SIZE) { //return size of file
jlong curpos = ctx->e->CallLongMethod(ctx->mio, ctx->mid_ff_seek, ctx->c, zero, SEEK_CUR);
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
jlong filesize = ctx->e->CallLongMethod(ctx->mio, ctx->mid_ff_seek, ctx->c, zero, SEEK_END);
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
ctx->e->CallLongMethod(ctx->mio, ctx->mid_ff_seek, ctx->c, curpos, SEEK_SET);
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
return filesize;
}
jlong ret = ctx->e->CallLongMethod(ctx->mio, ctx->mid_ff_seek, ctx->c, offset, how);
if (ctx->e->ExceptionCheck()) ctx->e->ExceptionClear();
return ret;
}
//decoder codebase
//returns stream idx >= 0
static int decoder_open_codec_context(FFContext *ctx, AVFormatContext *fmt_ctx, int type)
{
int ret;
int stream_idx;
AVStream *stream;
AVCodec *codec;
stream_idx = (*_av_find_best_stream)(ctx->fmt_ctx, type, -1, -1, NULL, 0);
if (stream_idx >= 0) {
stream = (AVStream*)ctx->fmt_ctx->streams[stream_idx];
codec = (*_avcodec_find_decoder)(stream->codecpar->codec_id);
if (codec == NULL) {
return -1;
}
ctx->codec_ctx = (*_avcodec_alloc_context3)(codec);
(*_avcodec_parameters_to_context)(ctx->codec_ctx, stream->codecpar);
// ctx->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
if ((ret = (*_avcodec_open2)(ctx->codec_ctx, codec, NULL)) < 0) {
return ret;
}
}
return stream_idx;
}
static jboolean decoder_open_codecs(FFContext *ctx, int new_width, int new_height, int new_chs, int new_freq) {
AVCodecContext *codec_ctx;
if ((ctx->video_stream_idx = decoder_open_codec_context(ctx, ctx->fmt_ctx, AVMEDIA_TYPE_VIDEO)) >= 0) {
ctx->video_stream = (AVStream*)ctx->fmt_ctx->streams[ctx->video_stream_idx];
ctx->video_codec_ctx = ctx->codec_ctx;
if (new_width == -1) new_width = ctx->video_codec_ctx->width;
if (new_height == -1) new_height = ctx->video_codec_ctx->height;
if ((ctx->video_dst_bufsize = (*_av_image_alloc)(ctx->video_dst_data, ctx->video_dst_linesize
, ctx->video_codec_ctx->width, ctx->video_codec_ctx->height
, ctx->video_codec_ctx->pix_fmt, 1)) < 0)
{
return JNI_FALSE;
}
if ((ctx->rgb_video_dst_bufsize = (*_av_image_alloc)(ctx->rgb_video_dst_data, ctx->rgb_video_dst_linesize
, new_width, new_height
, AV_PIX_FMT_BGRA, 1)) < 0)
{
return JNI_FALSE;
}
ctx->jvideo_length = ctx->rgb_video_dst_bufsize/4;
ctx->jvideo = (jintArray)ctx->e->NewGlobalRef(ctx->e->NewIntArray(ctx->jvideo_length));
//create video conversion context
ctx->sws_ctx = (*_sws_getContext)(ctx->video_codec_ctx->width, ctx->video_codec_ctx->height, ctx->video_codec_ctx->pix_fmt
, new_width, new_height, AV_PIX_FMT_BGRA
, SWS_BILINEAR, NULL, NULL, NULL);
}
if ((ctx->audio_stream_idx = decoder_open_codec_context(ctx, ctx->fmt_ctx, AVMEDIA_TYPE_AUDIO)) >= 0) {
ctx->audio_stream = (AVStream*)ctx->fmt_ctx->streams[ctx->audio_stream_idx];
ctx->audio_codec_ctx = ctx->codec_ctx;
//create audio conversion context
ctx->swr_ctx = (*_swr_alloc)();
if (new_chs == -1) new_chs = ctx->audio_codec_ctx->ch_layout.nb_channels;
AVChannelLayout new_layout;
switch (new_chs) {
case 1: (*_av_channel_layout_copy)(&new_layout, &channel_layout_1); ctx->dst_nb_channels = 1; break;
case 2: (*_av_channel_layout_copy)(&new_layout, &channel_layout_2); ctx->dst_nb_channels = 2; break;
case 4: (*_av_channel_layout_copy)(&new_layout, &channel_layout_4); ctx->dst_nb_channels = 4; break;
default: return JNI_FALSE;
}
AVChannelLayout src_layout;
(*_av_channel_layout_copy)(&src_layout, &ctx->audio_codec_ctx->ch_layout);
ctx->dst_sample_fmt = AV_SAMPLE_FMT_S16;
ctx->src_rate = ctx->audio_codec_ctx->sample_rate;
if (new_freq == -1) new_freq = ctx->src_rate;
(*_swr_alloc_set_opts2)(&ctx->swr_ctx,
&new_layout, ctx->dst_sample_fmt, new_freq,
&src_layout, ctx->audio_codec_ctx->sample_fmt, ctx->src_rate,
0, NULL);
int ret;
ret = (*_swr_init)(ctx->swr_ctx);
if (ret < 0) {
printf("resample init failed:%d\n", ret);
}
ctx->dst_rate = new_freq;
}
if ((ctx->frame = (*_av_frame_alloc)()) == NULL) return JNI_FALSE;
ctx->pkt = AVPacket_New();
(*_av_init_packet)(ctx->pkt);
ctx->pkt->data = NULL;
ctx->pkt->size = 0;
return JNI_TRUE;
}
/**
* Starts demuxing/decoding a stream.
* @param new_width - scale video to new width (-1 = use stream width)
* @param new_height - scale video to new height (-1 = use stream height)
* @param new_chs - # of channels to mix to (-1 = use stream channels)
* @param new_freq - output sampling rate (-1 = use stream rate)
* @param seekable - can you seek input? (true=file JNI_FALSE=stream)
* NOTE : Audio output is always 16bit
*/
JNIEXPORT jboolean JNICALL Java_javaforce_media_MediaDecoder_start
(JNIEnv *e, jobject c, jobject mio, jint new_width, jint new_height, jint new_chs, jint new_freq, jboolean seekable)
{
FFContext *ctx = createFFContext(e,c);
if (ctx == NULL) return JNI_FALSE;
ctx->mio = e->NewGlobalRef(mio);
ctx->GetMediaIO();
void *ff_buffer = (*_av_mallocz)(ffiobufsiz);
ctx->io_ctx = (*_avio_alloc_context)(ff_buffer, ffiobufsiz, 0, (void*)ctx, (void*)&read_packet, (void*)&write_packet, seekable ? (void*)&seek_packet : NULL);
if (ctx->io_ctx == NULL) return JNI_FALSE;
// ctx->io_ctx->direct = 1;
ctx->fmt_ctx = (*_avformat_alloc_context)();
ctx->fmt_ctx->pb = ctx->io_ctx;
int res;
if ((res = (*_avformat_open_input)((void**)&ctx->fmt_ctx, "stream", NULL, NULL)) != 0) {
printf("avformat_open_input() failed : %d\n", res);
return JNI_FALSE;
}
if ((res = (*_avformat_find_stream_info)(ctx->fmt_ctx, NULL)) < 0) {
printf("avformat_find_stream_info() failed : %d\n", res);
return JNI_FALSE;
}
(*_av_dump_format)(ctx->fmt_ctx, 0, "memory_io", 0);
return decoder_open_codecs(ctx, new_width, new_height, new_chs, new_freq);
}
/**
* Alternative start that works with files.
*
* Example: start("/dev/video0", "v4l2", ...);
*
* NOTE:input_format may be NULL
*/
JNIEXPORT jboolean JNICALL Java_javaforce_media_MediaDecoder_startFile
(JNIEnv *e, jobject c, jstring file, jstring input_format, jint new_width, jint new_height, jint new_chs, jint new_freq)
{
FFContext *ctx = createFFContext(e,c);
if (ctx == NULL) return JNI_FALSE;
int res;
ctx->fmt_ctx = (*_avformat_alloc_context)();
const char *cinput_format = e->GetStringUTFChars(input_format, NULL);
if (cinput_format != NULL) {
ctx->input_fmt = (*_av_find_input_format)(cinput_format);
if (ctx->input_fmt == NULL) {
printf("FFMPEG:av_find_input_format failed:%s\n", cinput_format);
e->ReleaseStringUTFChars(input_format, cinput_format);
return JNI_FALSE;
}
}
e->ReleaseStringUTFChars(input_format, cinput_format);
const char *cfile = e->GetStringUTFChars(file, NULL);
if ((res = (*_avformat_open_input)((void**)&ctx->fmt_ctx, cfile, ctx->input_fmt, NULL)) != 0) {
e->ReleaseStringUTFChars(file, cfile);
printf("avformat_open_input() failed : %d\n", res);
return JNI_FALSE;
}
e->ReleaseStringUTFChars(file, cfile);
(*_av_dump_format)(ctx->fmt_ctx, 0, "memory_io", 0);
if ((res = (*_avformat_find_stream_info)(ctx->fmt_ctx, NULL)) < 0) {
printf("avformat_find_stream_info() failed : %d\n", res);
return JNI_FALSE;
}
return decoder_open_codecs(ctx, new_width, new_height, new_chs, new_freq);
}
JNIEXPORT void JNICALL Java_javaforce_media_MediaDecoder_stop
(JNIEnv *e, jobject c)
{
FFContext *ctx = getFFContext(e,c);
if (ctx == NULL) return;
if (ctx->io_ctx != NULL) {
(*_avio_flush)(ctx->io_ctx);
(*_av_free)(ctx->io_ctx->buffer);
(*_av_free)(ctx->io_ctx);
ctx->io_ctx = NULL;
}
if (ctx->fmt_ctx != NULL) {
(*_avformat_free_context)(ctx->fmt_ctx);
ctx->fmt_ctx = NULL;
}
if (ctx->video_codec_ctx != NULL) {
(*_avcodec_free_context)(&ctx->video_codec_ctx);
ctx->video_codec_ctx = NULL;
}
if (ctx->audio_codec_ctx != NULL) {
(*_avcodec_free_context)(&ctx->audio_codec_ctx);
ctx->audio_codec_ctx = NULL;
}
if (ctx->frame != NULL) {
(*_av_frame_free)((void**)&ctx->frame);
ctx->frame = NULL;
}
if (ctx->video_dst_data[0] != NULL) {
(*_av_free)(ctx->video_dst_data[0]);
ctx->video_dst_data[0] = NULL;
}
if (ctx->rgb_video_dst_data[0] != NULL) {
(*_av_free)(ctx->rgb_video_dst_data[0]);
ctx->rgb_video_dst_data[0] = NULL;
}
if (ctx->sws_ctx != NULL) {
(*_sws_freeContext)(ctx->sws_ctx);
ctx->sws_ctx = NULL;
}
if (ctx->swr_ctx != NULL) {
(*_swr_free)(&ctx->swr_ctx);
ctx->swr_ctx = NULL;
}
if (ctx->jaudio != NULL) {
e->DeleteGlobalRef(ctx->jaudio);
ctx->jaudio = NULL;
}
if (ctx->jvideo != NULL) {
e->DeleteGlobalRef(ctx->jvideo);
ctx->jvideo = NULL;
}
if (ctx->pkt != NULL) {
(*_av_packet_free)(&ctx->pkt);
ctx->pkt = NULL;
}
deleteFFContext(e,c,ctx);
}
/** Reads next frame in stream and returns what type it was : AUDIO_FRAME, VIDEO_FRAME, NULL_FRAME or END_FRAME */
JNIEXPORT jint JNICALL Java_javaforce_media_MediaDecoder_read
(JNIEnv *e, jobject c)
{
FFContext *ctx = getFFContext(e,c);
if (ctx == NULL) return 0;
if (ctx->pkt == NULL) {
printf("MediaDecoder.read():pkt==NULL\n");
return END_FRAME;
}
//read another frame
if ((*_av_read_frame)(ctx->fmt_ctx, ctx->pkt) >= 0) {
ctx->pkt_key_frame = ((ctx->pkt->flags & 0x0001) == 0x0001);
} else {
return END_FRAME;
}
//try to decode another frame
if (ctx->pkt->stream_index == ctx->video_stream_idx) {
//extract a video frame
int ret = (*_avcodec_send_packet)(ctx->video_codec_ctx, ctx->pkt);
if (ret < 0) {
printf("Error:%d\n", ret);
return NULL_FRAME;
}
_av_free_packet(ctx->pkt);
while (1) {
ret = (*_avcodec_receive_frame)(ctx->video_codec_ctx, ctx->frame);
if (ret < 0) break;
(*_av_image_copy)(ctx->video_dst_data, ctx->video_dst_linesize
, ctx->frame->data, ctx->frame->linesize
, ctx->video_codec_ctx->pix_fmt, ctx->video_codec_ctx->width, ctx->video_codec_ctx->height);
//convert image to RGBA format
(*_sws_scale)(ctx->sws_ctx, ctx->video_dst_data, ctx->video_dst_linesize, 0, ctx->video_codec_ctx->height
, ctx->rgb_video_dst_data, ctx->rgb_video_dst_linesize);
}
return VIDEO_FRAME;
}
if (ctx->pkt->stream_index == ctx->audio_stream_idx) {
//extract an audio frame
int ret = (*_avcodec_send_packet)(ctx->audio_codec_ctx, ctx->pkt);
if (ret < 0) {
printf("Error:%d\n", ret);
return NULL_FRAME;
}
_av_free_packet(ctx->pkt);
while (1) {
ret = (*_avcodec_receive_frame)(ctx->audio_codec_ctx, ctx->frame);
if (ret < 0) break;
// int unpadded_linesize = frame.nb_samples * avutil.av_get_bytes_per_sample(audio_codec_ctx.sample_fmt);
//convert to new format
int dst_nb_samples;
dst_nb_samples = (int)(*_av_rescale_rnd)((*_swr_get_delay)(ctx->swr_ctx, ctx->src_rate)
+ ctx->frame->nb_samples, ctx->dst_rate, ctx->src_rate, AV_ROUND_UP);
if (((*_av_samples_alloc)(ctx->audio_dst_data, ctx->audio_dst_linesize, ctx->dst_nb_channels
, dst_nb_samples, ctx->dst_sample_fmt, 1)) < 0) return NULL_FRAME;
int converted_nb_samples = 0;
converted_nb_samples = (*_swr_convert)(ctx->swr_ctx, ctx->audio_dst_data, dst_nb_samples
, ctx->frame->extended_data, ctx->frame->nb_samples);
if (converted_nb_samples < 0) {
printf("FFMPEG:Resample failed!\n");
return NULL_FRAME;
}
int count = converted_nb_samples * ctx->dst_nb_channels;
if (ctx->jaudio == NULL || ctx->jaudio_length != count) {
if (ctx->jaudio != NULL) {
//free old audio array
e->DeleteGlobalRef(ctx->jaudio);
}
ctx->jaudio_length = count;
ctx->jaudio = (jshortArray)e->NewGlobalRef(e->NewShortArray(count));
}
e->SetShortArrayRegion(ctx->jaudio, 0, ctx->jaudio_length, (const jshort*)ctx->audio_dst_data[0]);
//free audio_dst_data
if (ctx->audio_dst_data[0] != NULL) {
(*_av_free)(ctx->audio_dst_data[0]);
ctx->audio_dst_data[0] = NULL;
}
}
return AUDIO_FRAME;
}