
Recherche avancée
Autres articles (36)
-
Les vidéos
21 avril 2011, parComme les documents de type "audio", Mediaspip affiche dans la mesure du possible les vidéos grâce à la balise html5 .
Un des inconvénients de cette balise est qu’elle n’est pas reconnue correctement par certains navigateurs (Internet Explorer pour ne pas le nommer) et que chaque navigateur ne gère en natif que certains formats de vidéos.
Son avantage principal quant à lui est de bénéficier de la prise en charge native de vidéos dans les navigateur et donc de se passer de l’utilisation de Flash et (...) -
MediaSPIP v0.2
21 juin 2013, parMediaSPIP 0.2 est la première version de MediaSPIP stable.
Sa date de sortie officielle est le 21 juin 2013 et est annoncée ici.
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Comme pour la version précédente, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...) -
MediaSPIP version 0.1 Beta
16 avril 2011, parMediaSPIP 0.1 beta est la première version de MediaSPIP décrétée comme "utilisable".
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Pour avoir une installation fonctionnelle, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...)
Sur d’autres sites (4967)
-
Linphone OSx msx264 encryption VGA takes 97% CPU, why ?
11 septembre 2013, par Maxim ShoustinI have problem and today don't know how to fix it or even from where to start.
I have Linphone application that uses msx264 plugin.
All stuff I run on OSx and my ffmpeg version installed from
port
, I didn't using selfupdate for portbash-3.2# port installed ffmpeg-devel
The following ports are currently installed:
ffmpeg-devel @20130205_0+gpl2
ffmpeg-devel @20130328_0 (active)
ffmpeg-devel @20130328_0+gpl2So I compiled and build msx264, no errors.
Now I try to send video over CIP resolution VGA (640x480) and get huge delay 8-9 seconds, even self-view I see in big delay.
when I configure CIF (352x288), all seems fine.
It's really strange that self-view camera has delay 4-5 sec.
So from logs during the session I found that msx264 plugin takes 97% CPU
On PC (windows 7) the same code runs fine, even HD I don't see any problems.
What is the problem should be ?
warning: Video MSTicker: We are late of 32146 miliseconds.
message: Filter MSRtpRecv is not scheduled; nothing to do.
message: ===========================================================
message: AUDIO SESSION'S RTP STATISTICS
message: -----------------------------------------------------------
message: sent 2344 packets
message: 403168 bytes
message: received 2038 packets
message: 350536 bytes
message: incoming delivered to the app 325080 bytes
message: lost 0 packets
message: received too late 123 packets
message: bad formatted 0 packets
message: discarded (queue overflow) 17 packets
message: ===========================================================
message: ms_filter_unlink: MSAuRead:0x7fb5a34955b0,0-->MSResample:0x7fb5aa917820,0
message: ms_filter_unlink: MSResample:0x7fb5aa917820,0-->MSSpeexEC:0x7fb5a34f6d20,1
message: ms_filter_unlink: MSSpeexEC:0x7fb5a34f6d20,1-->MSVolume:0x7fb5a3493450,0
message: ms_filter_unlink: MSVolume:0x7fb5a3493450,0-->MSTee:0x7fb5a3498e40,0
message: ms_filter_unlink: MSTee:0x7fb5a3498e40,0-->MSUlawEnc:0x7fb5a3499410,0
message: ms_filter_unlink: MSUlawEnc:0x7fb5a3499410,0-->MSRtpSend:0x7fb5aa910ba0,0
message: ms_filter_unlink: MSRtpRecv:0x7fb5a3400170,0-->MSUlawDec:0x7fb5a34933c0,0
message: ms_filter_unlink: MSUlawDec:0x7fb5a34933c0,0-->MSGenericPLC:0x7fb5aa91b040,0
message: ms_filter_unlink: MSGenericPLC:0x7fb5aa91b040,0-->MSDtmfGen:0x7fb5a6585f00,0
message: ms_filter_unlink: MSDtmfGen:0x7fb5a6585f00,0-->MSVolume:0x7fb5aa917790,0
message: ms_filter_unlink: MSVolume:0x7fb5aa917790,0-->MSTee:0x7fb5aa914fc0,0
message: ms_filter_unlink: MSTee:0x7fb5aa914fc0,0-->MSEqualizer:0x7fb5a3498f50,0
message: ms_filter_unlink: MSEqualizer:0x7fb5a3498f50,0-->MSSpeexEC:0x7fb5a34f6d20,0
message: ms_filter_unlink: MSSpeexEC:0x7fb5a34f6d20,0-->MSResample:0x7fb5aa9178b0,0
message: ms_filter_unlink: MSResample:0x7fb5aa9178b0,0-->MSAuWrite:0x7fb5a3499380,0
message: ms_filter_unlink: MSTee:0x7fb5a3498e40,1-->MSAudioMixer:0x7fb5aa914df0,0
message: ms_filter_unlink: MSTee:0x7fb5aa914fc0,1-->MSAudioMixer:0x7fb5aa914df0,1
message: ms_filter_unlink: MSAudioMixer:0x7fb5aa914df0,0-->MSFileRec:0x7fb5aa911020,0
message: Audio MSTicker thread exiting
message: ===========================================================
message: FILTER USAGE STATISTICS
message: Name Count Time/tick (ms) CPU Usage
message: -----------------------------------------------------------
message: MSX264Enc 321 138.147 97.1677
message: MSResample 8076 0.0550274 0.97085
message: MSSpeexEC 4302 0.0873765 0.821276
message: MSH264Dec 291 0.880267 0.561463
message: MSRtpSend 6174 0.012353 0.166623
message: MSRtpRecv 6174 0.0115132 0.155295
message: MSOSXGLDisplay 375 0.0376117 0.0308912
message: MSAudioMixer 4695 0.00249638 0.0256072
message: MSV4m 1480 0.00740446 0.0239537
message: MSUlawEnc 4038 0.0019542 0.0172411
message: MSTee 6540 0.000698976 0.00998688
message: MSAuRead 4695 0.00095017 0.0097466
message: MSUlawDec 1890 0.00205553 0.00849059
message: MSVolume 5928 0.000633159 0.00820007
message: MSFileRec 4695 0.000722743 0.00741371
message: MSDtmfGen 4695 0.0005 0.00512887
message: MSGenericPLC 4695 0.000429514 0.00440585
message: MSAuWrite 4038 0.000364199 0.00321319
message: MSEqualizer 1890 0.000250661 0.00103538
message: MSSizeConv 322 0.00104334 0.000736128
message: MSJpegWriter 290 0.000694158 0.00044124
message: MSPixConv 322 0.000405573 0.000286151
message: MSFilePlayer 0 0 0
message: MSVoidSink 0 0 0
message: ===========================================================
warning: Video MSTicker: We are late of 32256 miliseconds.
message: v4m video device closed.
message: Filter MSRtpRecv is not scheduled; nothing to do.
message: ===========================================================
message: VIDEO SESSION'S RTP STATISTICS
message: -----------------------------------------------------------
message: sent 1311 packets
message: 1517528 bytes
message: received 1783 packets
message: 1049010 bytes
message: incoming delivered to the app 986868 bytes
message: lost 0 packets
message: received too late 0 packets
message: bad formatted 0 packets
message: discarded (queue overflow) 0 packets
message: ===========================================================In addition the application shows me delay status, from logs :
message:: Dialog [0x7fb5a7634940]: now updated by transaction [0x7fb5aa9685d0].
warning: Video MSTicker: We are late of 20415 miliseconds.
warning: Video MSTicker: We are late of 20564 miliseconds.
message:: A SPS is being sent.
message:: A PPS is being sent.
warning: Video MSTicker: We are late of 20609 miliseconds.
warning: Video MSTicker: We are late of 20636 miliseconds.
warning: Video MSTicker: We are late of 20694 miliseconds.
warning: Video MSTicker: We are late of 20784 miliseconds.
warning: Video MSTicker: We are late of 20894 miliseconds.
warning: Video MSTicker: We are late of 21016 miliseconds.
warning: echo canceller: we are accumulating too much reference signal, need to throw out 1216 samples
message:: audio_stream_iterate(): local statistics available
Local's current jitter buffer size:77.440002 ms
message:: bandwidth usage: audio=[d=80.1,u=80.1] video=[d=305.3,u=441.8] kbit/sec
message:: Thread processing load: audio=2.135499 video=1268.186768
warning: Video MSTicker: We are late of 21134 miliseconds.
warning: Video MSTicker: We are late of 21256 miliseconds.
warning: Video MSTicker: We are late of 21382 miliseconds.
warning: Video MSTicker: We are late of 21506 miliseconds.
warning: Video MSTicker: We are late of 21638 miliseconds.
warning: Video MSTicker: We are late of 21781 miliseconds.
warning: Video MSTicker: We are late of 21921 miliseconds.
message:: bandwidth usage: audio=[d=81.6,u=80.0] video=[d=271.9,u=185.5] kbit/sec
message:: Thread processing load: audio=1.971647 video=1342.125000
warning: Video MSTicker: We are late of 22068 miliseconds.
message:: audio_stream_iterate(): remote statistics available
remote's interarrival jitter=68
remote's lost packets percentage since last report=0.390625
round trip time=0.258850 seconds
warning: Video MSTicker: We are late of 22216 miliseconds.Please, help me to find the problem.
Thanks,
this is a msx264 git repository :
git clone git://git.linphone.org/msx264.git
-
Linphone OSx msx264 encryption VGA takes 97% CPU, why ?
11 septembre 2013, par Maxim ShoustinI have problem and today don't know how to fix it or even from where to start.
I have Linphone application that uses msx264 plugin.
All stuff I run on OSx and my ffmpeg version installed from
port
, I didn't using selfupdate for portbash-3.2# port installed ffmpeg-devel
The following ports are currently installed:
ffmpeg-devel @20130205_0+gpl2
ffmpeg-devel @20130328_0 (active)
ffmpeg-devel @20130328_0+gpl2So I compiled and build msx264, no errors.
Now I try to send video over CIP resolution VGA (640x480) and get huge delay 8-9 seconds, even self-view I see in big delay.
when I configure CIF (352x288), all seems fine.
It's really strange that self-view camera has delay 4-5 sec.
So from logs during the session I found that msx264 plugin takes 97% CPU
On PC (windows 7) the same code runs fine, even HD I don't see any problems.
What is the problem should be ?
warning: Video MSTicker: We are late of 32146 miliseconds.
message: Filter MSRtpRecv is not scheduled; nothing to do.
message: ===========================================================
message: AUDIO SESSION'S RTP STATISTICS
message: -----------------------------------------------------------
message: sent 2344 packets
message: 403168 bytes
message: received 2038 packets
message: 350536 bytes
message: incoming delivered to the app 325080 bytes
message: lost 0 packets
message: received too late 123 packets
message: bad formatted 0 packets
message: discarded (queue overflow) 17 packets
message: ===========================================================
message: ms_filter_unlink: MSAuRead:0x7fb5a34955b0,0-->MSResample:0x7fb5aa917820,0
message: ms_filter_unlink: MSResample:0x7fb5aa917820,0-->MSSpeexEC:0x7fb5a34f6d20,1
message: ms_filter_unlink: MSSpeexEC:0x7fb5a34f6d20,1-->MSVolume:0x7fb5a3493450,0
message: ms_filter_unlink: MSVolume:0x7fb5a3493450,0-->MSTee:0x7fb5a3498e40,0
message: ms_filter_unlink: MSTee:0x7fb5a3498e40,0-->MSUlawEnc:0x7fb5a3499410,0
message: ms_filter_unlink: MSUlawEnc:0x7fb5a3499410,0-->MSRtpSend:0x7fb5aa910ba0,0
message: ms_filter_unlink: MSRtpRecv:0x7fb5a3400170,0-->MSUlawDec:0x7fb5a34933c0,0
message: ms_filter_unlink: MSUlawDec:0x7fb5a34933c0,0-->MSGenericPLC:0x7fb5aa91b040,0
message: ms_filter_unlink: MSGenericPLC:0x7fb5aa91b040,0-->MSDtmfGen:0x7fb5a6585f00,0
message: ms_filter_unlink: MSDtmfGen:0x7fb5a6585f00,0-->MSVolume:0x7fb5aa917790,0
message: ms_filter_unlink: MSVolume:0x7fb5aa917790,0-->MSTee:0x7fb5aa914fc0,0
message: ms_filter_unlink: MSTee:0x7fb5aa914fc0,0-->MSEqualizer:0x7fb5a3498f50,0
message: ms_filter_unlink: MSEqualizer:0x7fb5a3498f50,0-->MSSpeexEC:0x7fb5a34f6d20,0
message: ms_filter_unlink: MSSpeexEC:0x7fb5a34f6d20,0-->MSResample:0x7fb5aa9178b0,0
message: ms_filter_unlink: MSResample:0x7fb5aa9178b0,0-->MSAuWrite:0x7fb5a3499380,0
message: ms_filter_unlink: MSTee:0x7fb5a3498e40,1-->MSAudioMixer:0x7fb5aa914df0,0
message: ms_filter_unlink: MSTee:0x7fb5aa914fc0,1-->MSAudioMixer:0x7fb5aa914df0,1
message: ms_filter_unlink: MSAudioMixer:0x7fb5aa914df0,0-->MSFileRec:0x7fb5aa911020,0
message: Audio MSTicker thread exiting
message: ===========================================================
message: FILTER USAGE STATISTICS
message: Name Count Time/tick (ms) CPU Usage
message: -----------------------------------------------------------
message: MSX264Enc 321 138.147 97.1677
message: MSResample 8076 0.0550274 0.97085
message: MSSpeexEC 4302 0.0873765 0.821276
message: MSH264Dec 291 0.880267 0.561463
message: MSRtpSend 6174 0.012353 0.166623
message: MSRtpRecv 6174 0.0115132 0.155295
message: MSOSXGLDisplay 375 0.0376117 0.0308912
message: MSAudioMixer 4695 0.00249638 0.0256072
message: MSV4m 1480 0.00740446 0.0239537
message: MSUlawEnc 4038 0.0019542 0.0172411
message: MSTee 6540 0.000698976 0.00998688
message: MSAuRead 4695 0.00095017 0.0097466
message: MSUlawDec 1890 0.00205553 0.00849059
message: MSVolume 5928 0.000633159 0.00820007
message: MSFileRec 4695 0.000722743 0.00741371
message: MSDtmfGen 4695 0.0005 0.00512887
message: MSGenericPLC 4695 0.000429514 0.00440585
message: MSAuWrite 4038 0.000364199 0.00321319
message: MSEqualizer 1890 0.000250661 0.00103538
message: MSSizeConv 322 0.00104334 0.000736128
message: MSJpegWriter 290 0.000694158 0.00044124
message: MSPixConv 322 0.000405573 0.000286151
message: MSFilePlayer 0 0 0
message: MSVoidSink 0 0 0
message: ===========================================================
warning: Video MSTicker: We are late of 32256 miliseconds.
message: v4m video device closed.
message: Filter MSRtpRecv is not scheduled; nothing to do.
message: ===========================================================
message: VIDEO SESSION'S RTP STATISTICS
message: -----------------------------------------------------------
message: sent 1311 packets
message: 1517528 bytes
message: received 1783 packets
message: 1049010 bytes
message: incoming delivered to the app 986868 bytes
message: lost 0 packets
message: received too late 0 packets
message: bad formatted 0 packets
message: discarded (queue overflow) 0 packets
message: ===========================================================In addition the application shows me delay status, from logs :
message:: Dialog [0x7fb5a7634940]: now updated by transaction [0x7fb5aa9685d0].
warning: Video MSTicker: We are late of 20415 miliseconds.
warning: Video MSTicker: We are late of 20564 miliseconds.
message:: A SPS is being sent.
message:: A PPS is being sent.
warning: Video MSTicker: We are late of 20609 miliseconds.
warning: Video MSTicker: We are late of 20636 miliseconds.
warning: Video MSTicker: We are late of 20694 miliseconds.
warning: Video MSTicker: We are late of 20784 miliseconds.
warning: Video MSTicker: We are late of 20894 miliseconds.
warning: Video MSTicker: We are late of 21016 miliseconds.
warning: echo canceller: we are accumulating too much reference signal, need to throw out 1216 samples
message:: audio_stream_iterate(): local statistics available
Local's current jitter buffer size:77.440002 ms
message:: bandwidth usage: audio=[d=80.1,u=80.1] video=[d=305.3,u=441.8] kbit/sec
message:: Thread processing load: audio=2.135499 video=1268.186768
warning: Video MSTicker: We are late of 21134 miliseconds.
warning: Video MSTicker: We are late of 21256 miliseconds.
warning: Video MSTicker: We are late of 21382 miliseconds.
warning: Video MSTicker: We are late of 21506 miliseconds.
warning: Video MSTicker: We are late of 21638 miliseconds.
warning: Video MSTicker: We are late of 21781 miliseconds.
warning: Video MSTicker: We are late of 21921 miliseconds.
message:: bandwidth usage: audio=[d=81.6,u=80.0] video=[d=271.9,u=185.5] kbit/sec
message:: Thread processing load: audio=1.971647 video=1342.125000
warning: Video MSTicker: We are late of 22068 miliseconds.
message:: audio_stream_iterate(): remote statistics available
remote's interarrival jitter=68
remote's lost packets percentage since last report=0.390625
round trip time=0.258850 seconds
warning: Video MSTicker: We are late of 22216 miliseconds.Please, help me to find the problem.
Thanks,
this is a msx264 git repository :
git clone git://git.linphone.org/msx264.git
-
Why am I getting blips when encoding a sound file using Java JNA ?
21 mars 2014, par yonranI have implemented a hello world libavcodec using JNA to generate a wav file containing a pure 440Hz sine wave. But when I actually run the program the wav file contains annoying clicks and blips (compare to pure sin wav created from the C program). How am I calling
avcodec_encode_audio2
wrong ?Here is my Java code. All the sources are also at github in case you want to try to compile it.
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Objects;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class Sin {
/**
* Abstract class that allows you to put the initialization and cleanup
* code at the same place instead of separated by the big try block.
*/
public static abstract class SharedPtr<t> implements AutoCloseable {
public T ptr;
public SharedPtr(T ptr) {
this.ptr = ptr;
}
/**
* Abstract override forces method to throw no checked exceptions.
* Subclasses will call a C function that throws no exceptions.
*/
@Override public abstract void close();
}
/**
* @param args
* @throws IOException
* @throws LineUnavailableException
*/
public static void main(String[] args) throws IOException, LineUnavailableException {
final AvcodecLibrary avcodec = AvcodecLibrary.INSTANCE;
final AvformatLibrary avformat = AvformatLibrary.INSTANCE;
final AvutilLibrary avutil = AvutilLibrary.INSTANCE;
avcodec.avcodec_register_all();
avformat.av_register_all();
AVOutputFormat.ByReference format = null;
String format_name = "wav", file_url = "file:sinjava.wav";
for (AVOutputFormat.ByReference formatIter = avformat.av_oformat_next(null); formatIter != null; formatIter = avformat.av_oformat_next(formatIter)) {
formatIter.setAutoWrite(false);
String iterName = formatIter.name;
if (format_name.equals(iterName)) {
format = formatIter;
break;
}
}
Objects.requireNonNull(format);
System.out.format("Found format %s%n", format_name);
AVCodec codec = avcodec.avcodec_find_encoder(format.audio_codec); // one of AvcodecLibrary.CodecID
Objects.requireNonNull(codec);
codec.setAutoWrite(false);
try (
SharedPtr<avformatcontext> fmtCtxPtr = new SharedPtr<avformatcontext>(avformat.avformat_alloc_context()) {@Override public void close(){if (null!=ptr) avformat.avformat_free_context(ptr);}};
) {
AVFormatContext fmtCtx = Objects.requireNonNull(fmtCtxPtr.ptr);
fmtCtx.setAutoWrite(false);
fmtCtx.setAutoRead(false);
fmtCtx.oformat = format; fmtCtx.writeField("oformat");
AVStream st = avformat.avformat_new_stream(fmtCtx, codec);
if (null == st)
throw new IllegalStateException();
AVCodecContext c = st.codec;
if (null == c)
throw new IllegalStateException();
st.setAutoWrite(false);
fmtCtx.readField("nb_streams");
st.id = fmtCtx.nb_streams - 1; st.writeField("id");
assert st.id >= 0;
System.out.format("New stream: id=%d%n", st.id);
if (0 != (format.flags & AvformatLibrary.AVFMT_GLOBALHEADER)) {
c.flags |= AvcodecLibrary.CODEC_FLAG_GLOBAL_HEADER;
}
c.writeField("flags");
c.bit_rate = 64000; c.writeField("bit_rate");
int bestSampleRate;
if (null == codec.supported_samplerates) {
bestSampleRate = 44100;
} else {
bestSampleRate = 0;
for (int offset = 0, sample_rate = codec.supported_samplerates.getInt(offset); sample_rate != 0; codec.supported_samplerates.getInt(++offset)) {
bestSampleRate = Math.max(bestSampleRate, sample_rate);
}
assert bestSampleRate > 0;
}
c.sample_rate = bestSampleRate; c.writeField("sample_rate");
c.channel_layout = AvutilLibrary.AV_CH_LAYOUT_STEREO; c.writeField("channel_layout");
c.channels = avutil.av_get_channel_layout_nb_channels(c.channel_layout); c.writeField("channels");
assert 2 == c.channels;
c.sample_fmt = AvutilLibrary.AVSampleFormat.AV_SAMPLE_FMT_S16; c.writeField("sample_fmt");
c.time_base.num = 1;
c.time_base.den = bestSampleRate;
c.writeField("time_base");
c.setAutoWrite(false);
AudioFormat javaSoundFormat = new AudioFormat(bestSampleRate, Short.SIZE, c.channels, true, ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
DataLine.Info javaDataLineInfo = new DataLine.Info(TargetDataLine.class, javaSoundFormat);
if (! AudioSystem.isLineSupported(javaDataLineInfo))
throw new IllegalStateException();
int err;
if ((err = avcodec.avcodec_open(c, codec)) < 0) {
throw new IllegalStateException();
}
assert c.channels != 0;
AVIOContext.ByReference[] ioCtxReference = new AVIOContext.ByReference[1];
if (0 != (err = avformat.avio_open(ioCtxReference, file_url, AvformatLibrary.AVIO_FLAG_WRITE))) {
throw new IllegalStateException("averror " + err);
}
try (
SharedPtr ioCtxPtr = new SharedPtr(ioCtxReference[0]) {@Override public void close(){if (null!=ptr) avutil.av_free(ptr.getPointer());}}
) {
AVIOContext.ByReference ioCtx = Objects.requireNonNull(ioCtxPtr.ptr);
fmtCtx.pb = ioCtx; fmtCtx.writeField("pb");
int averr = avformat.avformat_write_header(fmtCtx, null);
if (averr < 0) {
throw new IllegalStateException("" + averr);
}
st.read(); // it is modified by avformat_write_header
System.out.format("Wrote header. fmtCtx->nb_streams=%d, st->time_base=%d/%d; st->avg_frame_rate=%d/%d%n", fmtCtx.nb_streams, st.time_base.num, st.time_base.den, st.avg_frame_rate.num, st.avg_frame_rate.den);
avformat.avio_flush(ioCtx);
int frame_size = c.frame_size != 0 ? c.frame_size : 4096;
int expectedBufferSize = frame_size * c.channels * (Short.SIZE/8);
boolean supports_small_last_frame = c.frame_size == 0 ? true : 0 != (codec.capabilities & AvcodecLibrary.CODEC_CAP_SMALL_LAST_FRAME);
int bufferSize = avutil.av_samples_get_buffer_size((IntBuffer)null, c.channels, frame_size, c.sample_fmt, 1);
assert bufferSize == expectedBufferSize: String.format("expected %d; got %d", expectedBufferSize, bufferSize);
ByteBuffer samples = ByteBuffer.allocate(expectedBufferSize);
samples.order(ByteOrder.nativeOrder());
int audio_time = 0; // unit: (c.time_base) s = (1/c.sample_rate) s
int audio_sample_count = supports_small_last_frame ?
3 * c.sample_rate :
3 * c.sample_rate / frame_size * frame_size;
while (audio_time < audio_sample_count) {
int frame_audio_time = audio_time;
samples.clear();
int nb_samples_in_frame = 0;
// encode a single tone sound
for (; samples.hasRemaining() && audio_time < audio_sample_count; nb_samples_in_frame++, audio_time++) {
double x = 2*Math.PI*440/c.sample_rate * audio_time;
double y = 10000 * Math.sin(x);
samples.putShort((short) y);
samples.putShort((short) y);
}
samples.flip();
try (
SharedPtr<avframe> framePtr = new SharedPtr<avframe>(avcodec.avcodec_alloc_frame()) {@Override public void close() {if (null!=ptr) avutil.av_free(ptr.getPointer());}};
) {
AVFrame frame = Objects.requireNonNull(framePtr.ptr);
frame.setAutoRead(false); // will be an in param
frame.setAutoWrite(false);
frame.nb_samples = nb_samples_in_frame; frame.writeField("nb_samples"); // actually unused during encoding
// Presentation time, in AVStream.time_base units.
frame.pts = avutil.av_rescale_q(frame_audio_time, c.time_base, st.time_base); // i * codec_time_base / st_time_base
frame.writeField("pts");
assert c.channels > 0;
int bytesPerSample = avutil.av_get_bytes_per_sample(c.sample_fmt);
assert bytesPerSample > 0;
if (0 != (err = avcodec.avcodec_fill_audio_frame(frame, c.channels, c.sample_fmt, samples, samples.capacity(), 1))) {
throw new IllegalStateException(""+err);
}
AVPacket packet = new AVPacket(); // one of the few structs from ffmpeg with guaranteed size
avcodec.av_init_packet(packet);
packet.size = 0;
packet.data = null;
packet.stream_index = st.index; packet.writeField("stream_index");
// encode the samples
IntBuffer gotPacket = IntBuffer.allocate(1);
if (0 != (err = avcodec.avcodec_encode_audio2(c, packet, frame, gotPacket))) {
throw new IllegalStateException("" + err);
} else if (0 != gotPacket.get()) {
packet.read();
averr = avformat.av_write_frame(fmtCtx, packet);
if (averr < 0)
throw new IllegalStateException("" + averr);
}
System.out.format("encoded frame: codec time = %d; pts=%d = av_rescale_q(%d,%d/%d,%d/%d) (%.02fs) contains %d samples (%.02fs); got_packet=%d; packet.size=%d%n",
frame_audio_time,
frame.pts,
frame_audio_time, st.codec.time_base.num,st.codec.time_base.den,st.time_base.num,st.time_base.den,
1.*frame_audio_time/c.sample_rate, frame.nb_samples, 1.*frame.nb_samples/c.sample_rate, gotPacket.array()[0], packet.size);
}
}
if (0 != (err = avformat.av_write_trailer(fmtCtx))) {
throw new IllegalStateException();
}
avformat.avio_flush(ioCtx);
}
}
System.out.println("Done writing");
}
}
</avframe></avframe></avformatcontext></avformatcontext></t>I also rewrote it in C, and the C version works fine without any blips. But I can’t figure out how I am using the library differently ; all the library function calls should be identical !
//! gcc --std=c99 sin.c $(pkg-config --cflags --libs libavutil libavformat libavcodec) -o sin
// sudo apt-get install libswscale-dev
#include
#include
#include
#include
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>mathematics.h>
#include <libavformat></libavformat>avformat.h>
#include <libswscale></libswscale>swscale.h>
#include <libavcodec></libavcodec>avcodec.h>
int main(int argc, char *argv[]) {
const char *format_name = "wav", *file_url = "file:sin.wav";
avcodec_register_all();
av_register_all();
AVOutputFormat *format = NULL;
for (AVOutputFormat *formatIter = av_oformat_next(NULL); formatIter != NULL; formatIter = av_oformat_next(formatIter)) {
int hasEncoder = NULL != avcodec_find_encoder(formatIter->audio_codec);
if (0 == strcmp(format_name, formatIter->name)) {
format = formatIter;
break;
}
}
printf("Found format %s\n", format->name);
AVCodec *codec = avcodec_find_encoder(format->audio_codec);
if (! codec) {
fprintf(stderr, "Could not find codec %d\n", format->audio_codec);
exit(1);
}
AVFormatContext *fmtCtx = avformat_alloc_context();
if (! fmtCtx) {
fprintf(stderr, "error allocating AVFormatContext\n");
exit(1);
}
fmtCtx->oformat = format;
AVStream *st = avformat_new_stream(fmtCtx, codec);
if (! st) {
fprintf(stderr, "error allocating AVStream\n");
exit(1);
}
if (fmtCtx->nb_streams != 1) {
fprintf(stderr, "avformat_new_stream should have incremented nb_streams, but it's still %d\n", fmtCtx->nb_streams);
exit(1);
}
AVCodecContext *c = st->codec;
if (! c) {
fprintf(stderr, "avformat_new_stream should have allocated a AVCodecContext for my stream\n");
exit(1);
}
st->id = fmtCtx->nb_streams - 1;
printf("Created stream %d\n", st->id);
if (0 != (format->flags & AVFMT_GLOBALHEADER)) {
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
c->bit_rate = 64000;
int bestSampleRate;
if (NULL == codec->supported_samplerates) {
bestSampleRate = 44100;
printf("Setting sample rate: %d\n", bestSampleRate);
} else {
bestSampleRate = 0;
for (const int *sample_rate_iter = codec->supported_samplerates; *sample_rate_iter != 0; sample_rate_iter++) {
if (*sample_rate_iter >= bestSampleRate)
bestSampleRate = *sample_rate_iter;
}
printf("Using best supported sample rate: %d\n", bestSampleRate);
}
c->sample_rate = bestSampleRate;
c->channel_layout = AV_CH_LAYOUT_STEREO;
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
c->time_base.num = 1;
c->time_base.den = c->sample_rate;
if (c->channels != 2) {
fprintf(stderr, "av_get_channel_layout_nb_channels returned %d instead of 2\n", c->channels);
exit(1);
}
c->sample_fmt = AV_SAMPLE_FMT_S16;
int averr;
if ((averr = avcodec_open2(c, codec, NULL)) < 0) {
fprintf(stderr, "avcodec_open2 returned error %d\n", averr);
exit(1);
}
AVIOContext *ioCtx = NULL;
if (0 != (averr = avio_open(&ioCtx, file_url, AVIO_FLAG_WRITE))) {
fprintf(stderr, "avio_open returned error %d\n", averr);
exit(1);
}
if (ioCtx == NULL) {
fprintf(stderr, "AVIOContext should have been set by avio_open\n");
exit(1);
}
fmtCtx->pb = ioCtx;
if (0 != (averr = avformat_write_header(fmtCtx, NULL))) {
fprintf(stderr, "avformat_write_header returned error %d\n", averr);
exit(1);
}
printf("Wrote header. fmtCtx->nb_streams=%d, st->time_base=%d/%d; st->avg_frame_rate=%d/%d\n", fmtCtx->nb_streams, st->time_base.num, st->time_base.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
int align = 1;
int sample_size = av_get_bytes_per_sample(c->sample_fmt);
if (sample_size != sizeof(int16_t)) {
fprintf(stderr, "expected sample size=%zu but got %d\n", sizeof(int16_t), sample_size);
exit(1);
}
int frame_size = c->frame_size != 0 ? c->frame_size : 4096;
int bufferSize = av_samples_get_buffer_size(NULL, c->channels, frame_size, c->sample_fmt, align);
int expectedBufferSize = frame_size * c->channels * sample_size;
int supports_small_last_frame = c->frame_size == 0 ? 1 : 0 != (codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME);
if (bufferSize != expectedBufferSize) {
fprintf(stderr, "expected buffer size=%d but got %d\n", expectedBufferSize, bufferSize);
exit(1);
}
int16_t *samples = (int16_t*)malloc(bufferSize);
uint32_t audio_time = 0; // unit: (1/c->sample_rate) s
uint32_t audio_sample_count = supports_small_last_frame ?
3 * c->sample_rate :
3 * c->sample_rate / frame_size * frame_size;
while (audio_time < audio_sample_count) {
uint32_t frame_audio_time = audio_time; // unit: (1/c->sample_rate) s
AVFrame *frame = avcodec_alloc_frame();
if (frame == NULL) {
fprintf(stderr, "avcodec_alloc_frame failed\n");
exit(1);
}
for (uint32_t i = 0; i != frame_size && audio_time < audio_sample_count; i++, audio_time++) {
samples[2*i] = samples[2*i + 1] = 10000 * sin(2*M_PI*440/c->sample_rate * audio_time);
frame->nb_samples = i+1; // actually unused during encoding
}
// frame->format = c->sample_fmt; // unused during encoding
frame->pts = av_rescale_q(frame_audio_time, c->time_base, st->time_base);
if (0 != (averr = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, (const uint8_t*)samples, bufferSize, align))) {
fprintf(stderr, "avcodec_fill_audio_frame returned error %d\n", averr);
exit(1);
}
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
int got_packet;
if (0 != (averr = avcodec_encode_audio2(c, &packet, frame, &got_packet))) {
fprintf(stderr, "avcodec_encode_audio2 returned error %d\n", averr);
exit(1);
}
if (got_packet) {
packet.stream_index = st->index;
if (0 < (averr = av_write_frame(fmtCtx, &packet))) {
fprintf(stderr, "av_write_frame returned error %d\n", averr);
exit(1);
} else if (averr == 1) {
// end of stream wanted.
}
}
printf("encoded frame: codec time = %u; format pts=%ld = av_rescale_q(%u,%d/%d,%d/%d) (%.02fs) contains %d samples (%.02fs); got_packet=%d; packet.size=%d\n",
frame_audio_time,
frame->pts,
frame_audio_time, c->time_base.num, c->time_base.den, st->time_base.num, st->time_base.den,
1.*frame_audio_time/c->sample_rate, frame->nb_samples, 1.*frame->nb_samples/c->sample_rate, got_packet, packet.size);
av_free(frame);
}
free(samples);
cleanupFile:
if (0 != (averr = av_write_trailer(fmtCtx))) {
fprintf(stderr, "av_write_trailer returned error %d\n", averr);
exit(1);
}
avio_flush(ioCtx);
avio_close(ioCtx);
avformat_free_context(fmtCtx);
}