Replacing SoundStreamHeads with MP3

This commit is contained in:
Jindra Petřík
2022-12-26 23:00:29 +01:00
parent cc71ba8cef
commit a940ea1c6f
9 changed files with 333 additions and 17 deletions

View File

@@ -163,12 +163,13 @@ public abstract class SoundStreamHeadTypeTag extends Tag implements CharacterIdT
newSoundSize = true;
newSoundType = fr.isStereo();
int len = snd.sampleCount();
/*int len = snd.sampleCount();
if (fr.isStereo()) {
len = len / 2;
}
}*/
newSoundSampleCount = len;
newSoundSampleCount = (int) Math.ceil(soundRateHz / swf.frameRate);
//newSoundSampleCount = len;
}
mp3Frames = snd.frames;
@@ -237,7 +238,7 @@ public abstract class SoundStreamHeadTypeTag extends Tag implements CharacterIdT
}
if (mp3Frames != null) {
/*int frame = 0;
int frame = 0;
int mp3FrameNum = 0;
long lastNumSamplesLong = 0;
@@ -252,21 +253,28 @@ public abstract class SoundStreamHeadTypeTag extends Tag implements CharacterIdT
List<MP3FRAME> blockMp3Frames = new ArrayList<>();
int blockSamples = 0;
while(lastNumSamplesLong < numSamplesAfterFrame) {
while(lastNumSamplesLong < numSamplesAfterFrame && mp3FrameNum < mp3Frames.size()) {
MP3FRAME mp3Frame = mp3Frames.get(mp3FrameNum);
lastNumSamplesLong += mp3Frame.getSampleCount();
blockSamples += mp3Frame.getSampleCount();
blockMp3Frames.add(mp3Frame);
mp3FrameNum++;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SWFOutputStream sos = new SWFOutputStream(baos, SWF.DEFAULT_VERSION, null);
try {
sos.writeUI16(blockSamples);
sos.writeSI16(seekSamples);
for (MP3FRAME mp3Frame:blockMp3Frames) {
sos.write(mp3Frame.getBytes());
}
} catch (IOException ex) {
Logger.getLogger(SoundStreamHeadTypeTag.class.getName()).log(Level.SEVERE, null, ex);
}
} */
block.streamSoundData = new ByteArrayRange(baos.toByteArray());
blocks.add(block);
frame++;
}
}
ReadOnlyTagList tags = timelined.getTags();

View File

@@ -35,11 +35,22 @@ public class MP3FRAME {
private Header h;
private SampleBuffer samples;
private byte[] fullData;
private MP3FRAME() {
}
public void setFullData(byte[] fullData) {
this.fullData = fullData;
}
public byte[] getBytes() {
return fullData;
}
public static MP3FRAME readFrame(Bitstream bitstream, Decoder decoder) throws IOException {
MP3FRAME ret = new MP3FRAME();
try {
@@ -60,24 +71,24 @@ public class MP3FRAME {
}
public int getSampleCount() {
if (h.version() == 3) {
if (h.version() == Header.MPEG1) {
switch(h.layer()) {
case 1:
return 1152;
return 384;
case 2:
return 1152;
case 3:
return 384;
return 1152;
}
}
if (h.version() == 2 || h.version() == 0) {
if (h.version() == Header.MPEG2_LSF || h.version() == Header.MPEG25_LSF) {
switch(h.layer()) {
case 1:
return 576;
return 384;
case 2:
return 1152;
case 3:
return 384;
return 576;
}
}
return 0;

View File

@@ -20,9 +20,12 @@ import com.jpexs.decompiler.flash.SWFInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.MarkingBufferedInputStream;
import javazoom.jl.decoder.MarkingPushbackInputStream;
/**
*
@@ -41,10 +44,23 @@ public class MP3SOUNDDATA {
frames = new ArrayList<>();
MP3FRAME f;
Decoder decoder = new Decoder();
Bitstream bitstream = new Bitstream(new ByteArrayInputStream(sis.readBytesEx(sis.available(), "soundStream")));
while ((f = MP3FRAME.readFrame(bitstream, decoder)) != null) {
frames.add(f);
}
byte data[] = sis.readBytesEx(sis.available(), "soundStream");
MarkingBufferedInputStream mis = new MarkingBufferedInputStream(new ByteArrayInputStream(data));
Bitstream bitstream = new Bitstream(mis); //new ByteArrayInputStream(data)
long initLen = mis.getPosition();
MarkingPushbackInputStream mpis = bitstream.getSource();
while (true) {
//System.err.println("initLen = "+initLen);
long posBefore = initLen+mpis.getPosition();
MP3FRAME frame = MP3FRAME.readFrame(bitstream, decoder);
if (frame == null) {
break;
}
long posAfter = initLen+mpis.getPosition();
frame.setFullData(Arrays.copyOfRange(data, (int)posBefore, (int)posAfter));
frames.add(frame);
}
}
public int sampleCount() {

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2010-2022 JPEXS, 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 3.0 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.types.sound;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author JPEXS
*/
public class MarkingInputStream extends InputStream {
private InputStream is;
private long pos = 0;
public MarkingInputStream(InputStream is) {
this.is = is;
}
@Override
public int read() throws IOException {
pos++;
return is.read();
}
public long getPos() {
return pos;
}
}