public void AudioFrameNext(Format.Audio cFormatSource, Frame cFrameSource) { if (NULL == _pStreamAudio) throw new Exception("there is no audio stream in file"); if (null == cFrameSource) return; AVStream stAVStream = (AVStream)Marshal.PtrToStructure(_pStreamAudio, typeof(AVStream)); //_nPTSAudio = (double)stAVStream.pts.val * stAVStream.time_base.num / stAVStream.time_base.den; Frame cFrame; AVPacket stAVPacket = new AVPacket(); IntPtr pAVPacket; if (null == _cFrameAudio) _cFrameAudio = new Frame(_cFormatAudio, _cFormatAudio.nBufferSize); Frame[] aFrames = cFormatSource.Convert(_cFormatAudio, cFrameSource, _cFrameAudio); for (int nIndx = 0; aFrames.Length > nIndx; nIndx++) { cFrame = aFrames[nIndx]; pAVPacket = Functions.av_malloc((uint)(Marshal.SizeOf(stAVPacket))); Functions.av_init_packet(pAVPacket); stAVPacket = (AVPacket)Marshal.PtrToStructure(pAVPacket, typeof(AVPacket)); if (cFrame.nPTS != Constants.AV_NOPTS_VALUE) stAVPacket.pts = Functions.av_rescale_q(cFrame.nPTS, _cFormatAudio.stAVCodecContext.time_base, stAVStream.time_base); //stAVPacket.pts = stAVPacket.dts = Functions.av_rescale_q((long)_nPTSAudio, _cFormatVideo.stAVCodecContext.time_base, stAVStream.time_base); stAVPacket.flags |= Constants.AV_PKT_FLAG_KEY; stAVPacket.stream_index = stAVStream.index; stAVPacket.size = cFrame.nLength; stAVPacket.data = cFrame.p; Marshal.StructureToPtr(stAVPacket, pAVPacket, true); _cFormatCtx.PacketWrite(pAVPacket); Functions.av_free_packet(pAVPacket); Functions.av_freep(ref pAVPacket); cFrame.Dispose(); //_nPTSAudio++; } }
private void FrameNext(IntPtr pStream, Format cFormatSource, Frame cFrameSource, Format cFormatTarget, IntPtr pBitStreamFilter) { AVStream stAVStream = (AVStream)Marshal.PtrToStructure(pStream, typeof(AVStream)); Frame[] aFrames; if (null == cFormatSource) { if (null == cFrameSource && cFormatTarget is Format.Audio) aFrames = ((Format.Audio)cFormatTarget).Flush(); else aFrames = new Frame[] { new Frame(null, cFrameSource.aBytes.ToArray()) { nPTS = cFrameSource.nPTS, bKeyframe = cFrameSource.bKeyframe } }; } else aFrames = cFormatSource.Convert(cFormatTarget, cFrameSource); IntPtr pAVPacket; AVPacket stAVPacket; for (int nIndx = 0; aFrames.Length > nIndx; nIndx++) { pAVPacket = Functions.av_malloc((uint)(Marshal.SizeOf(typeof(AVPacket)))); Functions.av_init_packet(pAVPacket); stAVPacket = (AVPacket)Marshal.PtrToStructure(pAVPacket, typeof(AVPacket)); if (aFrames[nIndx].nPTS != Constants.AV_NOPTS_VALUE) stAVPacket.pts = Functions.av_rescale_q(aFrames[nIndx].nPTS, cFormatTarget.stAVCodecContext.time_base, stAVStream.time_base); if (aFrames[nIndx].bKeyframe) stAVPacket.flags |= Constants.AV_PKT_FLAG_KEY; stAVPacket.stream_index = stAVStream.index; stAVPacket.size = aFrames[nIndx].nLength; stAVPacket.data = aFrames[nIndx].pBytes; lock(_aFramesLocked) _aFramesLocked.Add(aFrames[nIndx]); stAVPacket.buf = Functions.av_buffer_create(stAVPacket.data, stAVPacket.size, Marshal.GetFunctionPointerForDelegate(_fFrameUnlock), aFrames[nIndx], 0); //System.IO.File.AppendAllText("packets", stAVPacket.pts + "\t" + stAVPacket.size + Environment.NewLine); if (NULL != pBitStreamFilter && 0 != Functions.av_bitstream_filter_filter(pBitStreamFilter, cFormatTarget.pAVCodecContext, NULL, ref stAVPacket.data, ref stAVPacket.size, stAVPacket.data, stAVPacket.size, aFrames[nIndx].bKeyframe)) throw new Exception("error while filter a frame"); Marshal.StructureToPtr(stAVPacket, pAVPacket, true); lock(_cFormatCtx) _cFormatCtx.PacketWrite(pAVPacket); //Functions.av_free_packet(pAVPacket); //Functions.av_freep(ref pAVPacket); } }
public void VideoFrameNext(Format.Video cFormatSource, Frame cFrameSource) { if (NULL == _pStreamVideo) throw new Exception("there is no video stream in file"); AVStream stAVStream = (AVStream)Marshal.PtrToStructure(_pStreamVideo, typeof(AVStream)); //_nPTSVideo = (double)stAVStream.pts.val * stAVStream.time_base.num / stAVStream.time_base.den; //(1 / FPS) * sample rate * frame number if (null == _cFrameVideo) _cFrameVideo = new Frame(_cFormatVideo, _cFormatVideo.nBufferSize); Frame[] aFrames = cFormatSource.Convert(_cFormatVideo, cFrameSource, _cFrameVideo); AVPacket stAVPacket = new AVPacket(); IntPtr pAVPacket; for (int nIndx = 0; aFrames.Length > nIndx; nIndx++) { cFrameSource = aFrames[nIndx]; pAVPacket = Functions.av_malloc((uint)(Marshal.SizeOf(stAVPacket))); Functions.av_init_packet(pAVPacket); stAVPacket = (AVPacket)Marshal.PtrToStructure(pAVPacket, typeof(AVPacket)); if (cFrameSource.nPTS != Constants.AV_NOPTS_VALUE) stAVPacket.pts = Functions.av_rescale_q(cFrameSource.nPTS, _cFormatVideo.stAVCodecContext.time_base, stAVStream.time_base); if (cFrameSource.bKeyframe) stAVPacket.flags |= Constants.AV_PKT_FLAG_KEY; stAVPacket.stream_index = stAVStream.index; stAVPacket.size = cFrameSource.nLength; stAVPacket.data = cFrameSource.p; Marshal.StructureToPtr(stAVPacket, pAVPacket, true); //System.IO.File.AppendAllText("packets", stAVPacket.pts + "\t" + stAVPacket.size + "\r\n"); _cFormatCtx.PacketWrite(pAVPacket); Functions.av_free_packet(pAVPacket); Functions.av_freep(ref pAVPacket); //cFrameSource.Dispose(); } }