forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LiferayConverter.java
899 lines (667 loc) · 22.5 KB
/
LiferayConverter.java
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
/**
* Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portlet.documentlibrary.util;
import com.liferay.portal.image.ImageToolImpl;
import com.liferay.portal.kernel.image.ImageTool;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.xuggle.ferry.RefCounted;
import com.xuggle.xuggler.Global;
import com.xuggle.xuggler.IAudioResampler;
import com.xuggle.xuggler.IAudioSamples.Format;
import com.xuggle.xuggler.IAudioSamples;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IContainerFormat;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IRational;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;
import com.xuggle.xuggler.video.ConverterFactory;
import com.xuggle.xuggler.video.IConverter;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Properties;
import javax.imageio.ImageIO;
/**
* @author Juan González
* @author Sergio González
* @author Brian Wing Shun Chan
* @author Alexander Chow
*/
public abstract class LiferayConverter {
public abstract void convert() throws Exception;
protected void cleanUp(IPacket inputIPacket, IPacket outputIPacket) {
if (inputIPacket != null) {
inputIPacket.delete();
}
if (outputIPacket != null) {
outputIPacket.delete();
}
}
protected void cleanUp(
IStreamCoder[] inputIStreamCoders, IStreamCoder[] outputIStreamCoders) {
if (inputIStreamCoders != null) {
for (IStreamCoder iStreamCoder : inputIStreamCoders) {
if (iStreamCoder != null) {
iStreamCoder.close();
}
}
}
if (outputIStreamCoders != null) {
for (IStreamCoder iStreamCoder : outputIStreamCoders) {
if (iStreamCoder != null) {
iStreamCoder.close();
}
}
}
}
protected void cleanUp(
RefCounted[] inputRefCountedArray, RefCounted[] outputRefCountedArray) {
if (inputRefCountedArray != null) {
for (RefCounted refCounted : inputRefCountedArray) {
if (refCounted != null) {
refCounted.delete();
}
}
}
if (outputRefCountedArray != null) {
for (RefCounted refCounted : outputRefCountedArray) {
if (refCounted != null) {
refCounted.delete();
}
}
}
}
protected int countNonKeyAfterKey(
IPacket inputIPacket, Boolean keyPacketFound, int nonKeyAfterKeyCount) {
if (inputIPacket.isKey()) {
nonKeyAfterKeyCount = 0;
}
else if (keyPacketFound) {
nonKeyAfterKeyCount++;
}
return nonKeyAfterKeyCount;
}
protected IAudioResampler createIAudioResampler(
IStreamCoder inputIStreamCoder, IStreamCoder outputIStreamCoder)
throws Exception {
IAudioResampler iAudioResampler = null;
Format inputSampleFormat = inputIStreamCoder.getSampleFormat();
Format outputSampleFormat = outputIStreamCoder.getSampleFormat();
if ((inputIStreamCoder.getChannels() ==
outputIStreamCoder.getChannels()) &&
(inputIStreamCoder.getSampleRate() ==
outputIStreamCoder.getSampleRate()) &&
inputSampleFormat.equals(outputSampleFormat)) {
return iAudioResampler;
}
iAudioResampler = IAudioResampler.make(
outputIStreamCoder.getChannels(), inputIStreamCoder.getChannels(),
outputIStreamCoder.getSampleRate(),
inputIStreamCoder.getSampleRate(),
outputIStreamCoder.getSampleFormat(),
inputIStreamCoder.getSampleFormat());
if (iAudioResampler == null) {
throw new RuntimeException("Audio resampling is not supported");
}
return iAudioResampler;
}
protected IVideoResampler createIVideoResampler(
IStreamCoder inputIStreamCoder, IStreamCoder outputIStreamCoder,
int height, int width)
throws Exception {
IVideoResampler iVideoResampler = null;
IPixelFormat.Type inputIPixelFormatType =
inputIStreamCoder.getPixelType();
IPixelFormat.Type outputIPixelFormatType =
outputIStreamCoder.getPixelType();
if ((height == inputIStreamCoder.getHeight()) &&
(width == inputIStreamCoder.getWidth()) &&
inputIPixelFormatType.equals(outputIPixelFormatType)) {
return iVideoResampler;
}
iVideoResampler = IVideoResampler.make(
width, height, outputIStreamCoder.getPixelType(),
inputIStreamCoder.getWidth(), inputIStreamCoder.getHeight(),
inputIStreamCoder.getPixelType());
if (iVideoResampler == null) {
throw new RuntimeException("Video resampling is not supported");
}
return iVideoResampler;
}
protected void decodeAudio(
IAudioResampler iAudioResampler, IAudioSamples inputIAudioSample,
IAudioSamples resampledIAudioSample, IPacket inputIPacket,
IPacket outputIPacket, IStreamCoder inputIStreamCoder,
IStreamCoder outputIStreamCoder, IContainer outputIContainer,
int currentPacketSize, int previousPacketSize, int streamIndex,
long timeStampOffset)
throws Exception {
int offset = 0;
while (offset < inputIPacket.getSize()) {
boolean stopDecoding = false;
int value = inputIStreamCoder.decodeAudio(
inputIAudioSample, inputIPacket, offset);
if (value <= 0) {
if ((previousPacketSize == currentPacketSize) &&
(previousPacketSize != -1)) {
throw new RuntimeException(
"Unable to decode audio stream " + streamIndex);
}
else {
stopDecoding = true;
}
}
updateAudioTimeStamp(inputIAudioSample, timeStampOffset);
offset += value;
IAudioSamples outputIAudioSample = resampleAudio(
iAudioResampler, inputIAudioSample, resampledIAudioSample);
encodeAudio(
outputIStreamCoder, outputIPacket, outputIAudioSample,
outputIContainer);
if (stopDecoding) {
if (_log.isDebugEnabled()) {
_log.debug("Stop decoding audio stream " + streamIndex);
}
break;
}
}
}
protected int decodeVideo(
IVideoResampler iVideoResampler, IVideoPicture inputIVideoPicture,
IVideoPicture resampledIVideoPicture, IPacket inputIPacket,
IPacket outputIPacket, IStreamCoder inputIStreamCoder,
IStreamCoder outputIStreamCoder, IContainer outputIContainer,
File thumbnailFile, String thumbnailExtension, int thumbnailHeight,
int thumbnailWidth, long timeStampOffset)
throws Exception {
int offset = 0;
boolean stopDecoding = false;
while (offset < inputIPacket.getSize()) {
int value = inputIStreamCoder.decodeVideo(
inputIVideoPicture, inputIPacket, offset);
if (value <= 0) {
return value;
}
updateVideoTimeStamp(inputIVideoPicture, timeStampOffset);
offset += value;
// Workaround for FFmpeg bug. See
// http://comments.gmane.org/gmane.comp.video.ffmpeg.devel/135657
ICodec.ID iCodecID = inputIStreamCoder.getCodecID();
if (iCodecID.equals(ICodec.ID.CODEC_ID_MJPEG)) {
stopDecoding = true;
}
if (!inputIVideoPicture.isComplete()) {
if (stopDecoding) {
return 1;
}
else {
continue;
}
}
if (thumbnailFile != null) {
BufferedImage bufferedImage = null;
if (_converterFactoryType == null) {
_converterFactoryType =
ConverterFactory.findRegisteredConverter(
ConverterFactory.XUGGLER_BGR_24);
}
if (_converterFactoryType == null) {
throw new UnsupportedOperationException(
"No converter found for " +
ConverterFactory.XUGGLER_BGR_24);
}
if (_videoIConverter == null) {
_videoIConverter = ConverterFactory.createConverter(
_converterFactoryType.getDescriptor(),
inputIVideoPicture);
}
bufferedImage = _videoIConverter.toImage(inputIVideoPicture);
thumbnailFile.createNewFile();
ImageTool imageTool = ImageToolImpl.getInstance();
RenderedImage renderedImage = imageTool.scale(
bufferedImage, thumbnailHeight, thumbnailWidth);
ImageIO.write(
renderedImage, thumbnailExtension,
new FileOutputStream(thumbnailFile));
return DECODE_VIDEO_THUMBNAIL;
}
if ((outputIStreamCoder != null) && (outputIContainer != null)) {
IVideoPicture outputIVideoPicture = resampleVideo(
iVideoResampler, inputIVideoPicture,
resampledIVideoPicture);
outputIVideoPicture.setQuality(0);
encodeVideo(
outputIStreamCoder, outputIVideoPicture, outputIPacket,
outputIContainer);
}
if (stopDecoding) {
break;
}
}
return 1;
}
protected void encodeAudio(
IStreamCoder outputIStreamCoder, IPacket outputIPacket,
IAudioSamples outputIAudioSample, IContainer outputIContainer)
throws Exception {
int consumedSamplesCount = 0;
while (consumedSamplesCount < outputIAudioSample.getNumSamples()) {
int value = outputIStreamCoder.encodeAudio(
outputIPacket, outputIAudioSample, consumedSamplesCount);
if (value <= 0) {
throw new RuntimeException("Unable to encode audio");
}
consumedSamplesCount += value;
if (outputIPacket.isComplete()) {
value = outputIContainer.writePacket(outputIPacket, true);
if (value < 0) {
throw new RuntimeException("Unable to write audio packet");
}
}
}
}
protected void encodeVideo(
IStreamCoder outputIStreamCoder, IVideoPicture outputIVideoPicture,
IPacket outputIPacket, IContainer outputIContainer)
throws Exception {
int value = outputIStreamCoder.encodeVideo(
outputIPacket, outputIVideoPicture, 0);
if (value < 0) {
throw new RuntimeException("Unable to encode video");
}
if (outputIPacket.isComplete()) {
value = outputIContainer.writePacket(outputIPacket, true);
if (value < 0) {
throw new RuntimeException("Unable to write video packet");
}
}
}
protected void flush(
IStreamCoder outputIStreamCoder, IContainer outputIContainer,
IPacket iPacket) {
if (outputIStreamCoder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
outputIStreamCoder.encodeAudio(iPacket, null, 0);
}
else {
outputIStreamCoder.encodeVideo(iPacket, null, 0);
}
if (iPacket.isComplete()) {
outputIContainer.writePacket(iPacket, true);
}
}
protected void flush(
IStreamCoder[] outputIStreamCoders, IContainer outputIContainer) {
for (IStreamCoder outputIStreamCoder : outputIStreamCoders) {
if (outputIStreamCoder == null) {
continue;
}
IPacket iPacket = IPacket.make();
flush(outputIStreamCoder, outputIContainer, iPacket);
while (iPacket.isComplete()) {
flush(outputIStreamCoder, outputIContainer, iPacket);
}
}
}
protected int getAudioBitRate(ICodec outputICodec, int originalBitRate) {
if ((originalBitRate == 0) || (originalBitRate > AUDIO_BIT_RATE_MAX)) {
originalBitRate = AUDIO_BIT_RATE_DEFAULT;
}
if (outputICodec.getID().equals(ICodec.ID.CODEC_ID_VORBIS)){
if (originalBitRate < 64000){
originalBitRate = 64000;
}
}
return originalBitRate;
}
protected int getAudioEncodingChannels(
IContainer outputIContainer, int channels) {
IContainerFormat iContainerFormat =
outputIContainer.getContainerFormat();
String outputFormat = iContainerFormat.getOutputFormatShortName();
if (outputFormat.equals("ogg")) {
return 2;
}
if ((channels == 0) || (channels > 2)) {
channels = 2;
}
return channels;
}
protected ICodec getAudioEncodingICodec(IContainer outputIContainer) {
IContainerFormat iContainerFormat =
outputIContainer.getContainerFormat();
String outputFormat = iContainerFormat.getOutputFormatShortName();
if (outputFormat.equals("ogg")) {
return ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_VORBIS);
}
return null;
}
protected Format getAudioEncodingSampleFormat(ICodec outputCodec,
IStreamCoder inputStreamCoder) {
Format returnFormat = null;
Format preferredFormat = inputStreamCoder.getSampleFormat();
System.out.println("Preferido:"+preferredFormat);
List<Format> formats = outputCodec.getSupportedAudioSampleFormats();
for (Format format : formats) {
System.out.println("A comprobar:"+format.toString());
returnFormat = format;
if (format == preferredFormat) {
System.out.println("Coinciden!!");
break;
}
}
return returnFormat;
}
protected int getAudioSampleRate() {
return AUDIO_SAMPLE_RATE_DEFAULT;
}
protected abstract IContainer getInputIContainer();
protected int getProperty(
int originalValue, int defaultValue, int maxValue) {
if (originalValue <= 0) {
originalValue = defaultValue;
}
else if (originalValue > maxValue) {
originalValue = maxValue;
}
return originalValue;
}
protected int getProperty(
Properties properties, String propertyName, String prettyPropertyName,
String container, int defaultValue, int maxValue) {
int property = GetterUtil.getInteger(
properties.getProperty(propertyName + "[" + container + "]"),
defaultValue);
if (property > maxValue) {
property = maxValue;
}
if (_log.isInfoEnabled()) {
_log.info(
"Default " + prettyPropertyName + " for " + container +
" configured to " + property);
}
return property;
}
protected long getSeekTimeStamp(int percentage) throws Exception {
IContainer inputIContainer = getInputIContainer();
long seekTimeStamp = -1;
long videoSeconds = inputIContainer.getDuration() / 1000000L;
long seekSeconds = ((videoSeconds * percentage) / 100L);
for (int i = 0; i < inputIContainer.getNumStreams(); i++) {
IStream inputIStream = inputIContainer.getStream(i);
IStreamCoder inputIStreamCoder = inputIStream.getStreamCoder();
if (inputIStreamCoder.getCodecType() !=
ICodec.Type.CODEC_TYPE_VIDEO) {
continue;
}
IRational iRational = inputIStream.getTimeBase();
long timeStampOffset =
iRational.getDenominator() / iRational.getNumerator() *
seekSeconds;
seekTimeStamp = inputIContainer.getStartTime() + timeStampOffset;
break;
}
return seekTimeStamp;
}
protected long getStreamTimeStampOffset(IStream iStream) {
long timeStampOffset = 0;
if ((iStream.getStartTime() != Global.NO_PTS) &&
(iStream.getStartTime() > 0) && (iStream.getTimeBase() != null)) {
IRational iRational = IRational.make(
1, (int)Global.DEFAULT_PTS_PER_SECOND);
timeStampOffset = iRational.rescale(
iStream.getStartTime(), iStream.getTimeBase());
}
return timeStampOffset;
}
protected boolean isKeyPacketFound(
IPacket inputIPacket, boolean keyPacketFound) {
if (inputIPacket.isKey() && !keyPacketFound) {
return true;
}
return keyPacketFound;
}
protected boolean isStartDecoding(
IPacket inputIPacket, IStreamCoder inputIStreamCoder,
boolean keyPacketFound, int nonKeyAfterKeyCount,
boolean onlyDecodeKeyPackets) {
if (onlyDecodeKeyPackets && !inputIPacket.isKey()) {
return false;
}
ICodec.ID iCodecID = inputIStreamCoder.getCodecID();
if (iCodecID.equals(ICodec.ID.CODEC_ID_MJPEG)) {
return true;
}
else if (iCodecID.equals(ICodec.ID.CODEC_ID_MPEG2VIDEO) ||
iCodecID.equals(ICodec.ID.CODEC_ID_THEORA)) {
if (nonKeyAfterKeyCount != 1) {
return true;
}
return false;
}
return keyPacketFound;
}
protected void openContainer(
IContainer iContainer, String url, boolean writeContainer)
throws Exception {
int value = 0;
if (writeContainer) {
value = iContainer.open(url, IContainer.Type.WRITE, null);
}
else {
value = iContainer.open(url, IContainer.Type.READ, null);
}
if (value < 0) {
if (writeContainer) {
throw new RuntimeException("Unable to open output URL");
}
else {
throw new RuntimeException("Unable to open input URL");
}
}
}
protected void openStreamCoder(IStreamCoder iStreamCoder) throws Exception {
if ((iStreamCoder != null) &&
(iStreamCoder.getCodecType() != ICodec.Type.CODEC_TYPE_UNKNOWN)) {
// some codecs require experimental mode to be set, and so we set it
// here.
if (iStreamCoder
.setStandardsCompliance(IStreamCoder.CodecStandardsCompliance.COMPLIANCE_EXPERIMENTAL) < 0) {
throw new RuntimeException(
"could not set compliance mode to experimental");
}
if (iStreamCoder.open(null, null) < 0) {
throw new RuntimeException("Unable to open coder");
}
}
}
protected void prepareAudio(
IAudioResampler[] iAudioResamplers,
IAudioSamples[] inputIAudioSamples,
IAudioSamples[] outputIAudioSamples, IStreamCoder inputIStreamCoder,
IStreamCoder[] outputIStreamCoders, IContainer outputIContainer,
IStream[] outputIStreams, ICodec.Type inputICodecType,
String outputURL, int index)
throws Exception {
ICodec iCodec = getAudioEncodingICodec(outputIContainer);
if (iCodec == null) {
iCodec = ICodec.guessEncodingCodec(
null, null, outputURL, null, inputICodecType);
}
if (iCodec == null) {
throw new RuntimeException(
"Unable to determine " + inputICodecType + " encoder for " +
outputURL);
}
IStream outputIStream = outputIContainer.addNewStream(iCodec);
outputIStreams[index] = outputIStream;
IStreamCoder outputIStreamCoder = outputIStream.getStreamCoder();
outputIStreamCoders[index] = outputIStreamCoder;
Format sampleFormat = getAudioEncodingSampleFormat(iCodec, inputIStreamCoder);
System.out.println("Establecemos formato a "+sampleFormat.toString());
outputIStreamCoder.setSampleFormat(sampleFormat);
/*Format preferredFormat = inputIStreamCoder.getSampleFormat();
System.out.println("Formato preferido:"+preferredFormat.toString());
List<Format> formats = iCodec.getSupportedAudioSampleFormats();
for (Format format : formats) {
System.out.println("Formato a comprobar:"+format.toString());
outputIStreamCoder.setSampleFormat(format);
if (format == preferredFormat) {
System.out.println("Formato "+format.toString()+" es igual que el preferido");
break;
}
}*/
int bitRate = inputIStreamCoder.getBitRate();
if (_log.isInfoEnabled()) {
_log.info("Original audio bitrate " + bitRate);
}
bitRate = getAudioBitRate(iCodec, bitRate);
if (_log.isInfoEnabled()) {
_log.info("Modified audio bitrate " + bitRate);
}
outputIStreamCoder.setBitRate(bitRate);
int channels = getAudioEncodingChannels(
outputIContainer, inputIStreamCoder.getChannels());
outputIStreamCoder.setChannels(channels);
outputIStreamCoder.setGlobalQuality(0);
outputIStreamCoder.setSampleRate(getAudioSampleRate());
iAudioResamplers[index] = createIAudioResampler(
inputIStreamCoder, outputIStreamCoder);
inputIAudioSamples[index] = IAudioSamples.make(1024,
inputIStreamCoder.getChannels(),
inputIStreamCoder.getSampleFormat());
outputIAudioSamples[index] = IAudioSamples.make(1024,
outputIStreamCoder.getChannels(),
outputIStreamCoder.getSampleFormat());
}
protected IAudioSamples resampleAudio(
IAudioResampler iAudioResampler, IAudioSamples inputIAudioSample,
IAudioSamples resampledIAudioSample)
throws Exception {
if ((iAudioResampler == null) ||
(inputIAudioSample.getNumSamples() <= 0)) {
return inputIAudioSample;
}
iAudioResampler.resample(
resampledIAudioSample, inputIAudioSample,
inputIAudioSample.getNumSamples());
return resampledIAudioSample;
}
protected IVideoPicture resampleVideo(
IVideoResampler iVideoResampler, IVideoPicture inputIVideoPicture,
IVideoPicture resampledIVideoPicture)
throws Exception {
if (iVideoResampler == null) {
return inputIVideoPicture;
}
if (iVideoResampler.resample(
resampledIVideoPicture, inputIVideoPicture) < 0) {
throw new RuntimeException("Unable to resample video");
}
return resampledIVideoPicture;
}
protected void rewind() throws Exception {
IContainer inputIContainer = getInputIContainer();
if (inputIContainer == null) {
return;
}
int value = 0;
for (int i = 0; i < inputIContainer.getNumStreams(); i++) {
IStream inputIStream = inputIContainer.getStream(i);
IStreamCoder inputIStreamCoder = inputIStream.getStreamCoder();
if (inputIStreamCoder.getCodecType() !=
ICodec.Type.CODEC_TYPE_VIDEO) {
continue;
}
value = rewind(i);
if (value < 0) {
throw new RuntimeException("Error while seeking file");
}
break;
}
}
protected int rewind(int index) throws Exception {
IContainer inputIContainer = getInputIContainer();
if (inputIContainer == null) {
return -1;
}
int value = inputIContainer.seekKeyFrame(index, -1, 0);
if (value < 0) {
throw new RuntimeException("Error while seeking file");
}
return value;
}
protected int seek(int index, long timeStamp) throws Exception {
IContainer inputIContainer = getInputIContainer();
if (inputIContainer == null) {
return -1;
}
int value = inputIContainer.seekKeyFrame(index, timeStamp, 0);
if (value < 0) {
throw new RuntimeException("Error while seeking file");
}
return value;
}
protected long seek(long timeStamp) throws Exception {
IContainer inputIContainer = getInputIContainer();
if (inputIContainer == null) {
return -1;
}
int value = 0;
for (int i = 0; i < inputIContainer.getNumStreams(); i++) {
IStream inputIStream = inputIContainer.getStream(i);
IStreamCoder inputIStreamCoder = inputIStream.getStreamCoder();
if (inputIStreamCoder.getCodecType() !=
ICodec.Type.CODEC_TYPE_VIDEO) {
continue;
}
value = seek(i, timeStamp);
if (value < 0) {
throw new RuntimeException("Error while seeking file");
}
break;
}
return value;
}
protected void updateAudioTimeStamp(
IAudioSamples inputAudioSample, long timeStampOffset) {
if (inputAudioSample.getTimeStamp() != Global.NO_PTS) {
inputAudioSample.setTimeStamp(
inputAudioSample.getTimeStamp() - timeStampOffset);
}
}
protected void updateVideoTimeStamp(
IVideoPicture inputIVideoPicture, long timeStampOffset) {
if (inputIVideoPicture.getTimeStamp() != Global.NO_PTS) {
inputIVideoPicture.setTimeStamp(
inputIVideoPicture.getTimeStamp() - timeStampOffset);
}
}
protected static final int AUDIO_BIT_RATE_DEFAULT = 64000;
protected static final int AUDIO_BIT_RATE_MAX = 500000;
protected static final int AUDIO_SAMPLE_RATE_DEFAULT = 44100;
protected static final int AUDIO_SAMPLE_RATE_MAX = 192000;
protected static final int DECODE_VIDEO_THUMBNAIL = 2;
private static Log _log = LogFactoryUtil.getLog(LiferayConverter.class);
private ConverterFactory.Type _converterFactoryType;
private IConverter _videoIConverter;
}