C〓〓SDL 2を使ってMp 4ファイルの再生音ビデオ操作を実現します。
再生音声ビデオのキー:ビデオのフォーマットはH 264で、オーディオのフォーマットはAACです。fmpeg検出ストリームの方式を使用して、音声ビデオストリームの復号再生を実現する。
データ処理ロジック:H 264->YUV AAC->PCM。
SDL 2工具類
C噫Mp 4ファイル音ビデオエンコーダ類
テストコードと効果図
SDL.SDL_UpdateTexture;
//SDL.SDL_UpdateTexture(sdtexture、IntPtr.Zero、pixels、pitch);このコードはウィンドウの緑の影を再生します。
変更後の効果:
コードを改善して、同じスレッドで音声ビデオを再生します。
データ処理ロジック:H 264->YUV AAC->PCM。
SDL 2工具類
using SDL2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CvNetVideo
{
public unsafe class SDLHelper
{
private IntPtr screen;
private IntPtr sdlrenderer;
private IntPtr sdltexture;
SDL.SDL_Rect sdlrect;
SDL.SDL_Event sdlevent;
bool isInit = false;
public SDLHelper()
{
}
public void SDL_MaximizeWindow()
{
}
public int SDL_Init(int width, int height, IntPtr intPtr)
{
lock (this)
{
if (!isInit)
{
// SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER)
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER) < 0)
{
Console.WriteLine("Could not initialize SDL - {0}
", SDL.SDL_GetError());
return -1;
}
isInit = true;
}
#region SDL
if (sdltexture != IntPtr.Zero)
{
SDL.SDL_DestroyTexture(sdltexture);
}
if (sdlrenderer != IntPtr.Zero)
{
SDL.SDL_DestroyRenderer(sdlrenderer);
}
if (screen != IntPtr.Zero)
{
SDL.SDL_DestroyWindow(screen);
SDL.SDL_RaiseWindow(screen);
SDL.SDL_RestoreWindow(screen);
}
//
screen = SDL.SDL_CreateWindowFrom(intPtr);
SDL.SDL_ShowWindow(screen);
SDL.SDL_SetWindowSize(screen, width, height);
//screen = SDL.SDL_CreateWindow("SDL EVENT TEST", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, width, height, SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE);
//screen = SDL.SDL_CreateWindow("SDL EVENT TEST", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, screen_w, screen_h, SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE);
if (screen == IntPtr.Zero)
{
Console.WriteLine("Can't creat a window:{0}
", SDL.SDL_GetError());
return -1;
}
//
sdlrenderer = SDL.SDL_CreateRenderer(screen, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
//
sdltexture = SDL.SDL_CreateTexture(sdlrenderer, SDL.SDL_PIXELFORMAT_IYUV, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, width, height);
#endregion
return 0;
}
}
public int SDL_Display(int width, int height, IntPtr pixels, int pixelsSize,
int pitch)
{
lock (this)
{
#region SDL
//
sdlrect.x = 0;
sdlrect.y = 0;
sdlrect.w = width;
sdlrect.h = height;
//SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, pixels, pitch);
//
SDL.SDL_RenderClear(sdltexture);
//SDL.SDL_Rect srcRect = sdlrect;
//SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
//
SDL.SDL_RenderPresent(sdlrenderer);
return 0;
}
#endregion
}
}
public unsafe class SDLAudio
{
class aa
{
public byte[] pcm;
public int len;
}
int lastIndex = 0;
private List<aa> data = new List<aa>();
//private List<byte> data = new List<byte>();
SDL.SDL_AudioCallback Callback;
public void PlayAudio(IntPtr pcm, int len)
{
lock (this)
{
byte[] bts = new byte[len];
Marshal.Copy(pcm, bts, 0, len);
data.Add(new aa
{
len = len,
pcm = bts
});
}
//SDL.SDL_Delay(10);
}
void SDL_AudioCallback(IntPtr userdata, IntPtr stream, int len)
{
SDL 2.0
SDL.SDL_RWFromMem(stream, 0, len);
//if (audio_len == 0)
// return;
//len = (len > audio_len ? audio_len : len);
if (data.Count == 0)
{
for (int i = 0; i < len; i++)
{
((byte*)stream)[i] = 0;
}
return;
}
for (int i = 0; i < len; i++)
{
if (data[0].len > i)
{
((byte*)stream)[i] = data[0].pcm[i];
}
else
((byte*)stream)[i] = 0;
}
data.RemoveAt(0);
}
public int SDL_Init()
{
Callback = SDL_AudioCallback;
#region SDL
SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER)
//if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER) < 0)
//{
// Console.WriteLine("Could not initialize SDL - {0}
", SDL.SDL_GetError());
// return -1;
//}
#endregion
SDL.SDL_AudioSpec wanted_spec = new SDL.SDL_AudioSpec();
wanted_spec.freq = 8000;
wanted_spec.format = SDL.AUDIO_S16;
wanted_spec.channels = 1;
wanted_spec.silence = 0;
wanted_spec.samples = 320;
wanted_spec.callback = Callback;
if (SDL.SDL_OpenAudio(ref wanted_spec, IntPtr.Zero) < 0)
{
Console.WriteLine("can't open audio.");
return -1;
}
//Play
SDL.SDL_PauseAudio(0);
return 0;
}
}
}
SDLは基本的な再生機能を実現しています。C噫Mp 4ファイル音ビデオエンコーダ類
using CV.Video.Base;
using CV.Video.Base.FFmpeg;
using FFmpeg.AutoGen;
using JX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CvNetVideo.Codec.Video
{
public unsafe class JT1078CodecForMp4
{
/// <summary>
///
/// </summary>
public bool IsRun { get; protected set; }
/// <summary>
///
/// </summary>
private Thread threadVideo;
/// <summary>
///
/// </summary>
private Thread threadAudio;
/// <summary>
///
/// </summary>
private bool exit_thread = false;
/// <summary>
///
/// </summary>
private bool pause_thread = false;
/// <summary>
/// videoindex
/// </summary>
private int videoindex = -1;
/// <summary>
/// audioindex
/// </summary>
private int audioindex = -1;
/// <summary>
/// H264 YUV SDL
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
/// <returns></returns>
public unsafe int RunVideo(string fileName,SDLHelper sdlVideo)
{
IsRun = true;
exit_thread = false;
pause_thread = false;
threadVideo = Thread.CurrentThread;
int error, frame_count = 0;
int got_picture, ret;
SwsContext* pSwsCtx = null;
AVFormatContext* ofmt_ctx = null;
IntPtr convertedFrameBufferPtr = IntPtr.Zero;
try
{
//
ffmpeg.avcodec_register_all();
//
ofmt_ctx = ffmpeg.avformat_alloc_context();
//
error = ffmpeg.avformat_open_input(&ofmt_ctx, fileName, null, null);
if (error != 0)
{
throw new ApplicationException(FFmpegBinariesHelper.GetErrorMessage(error));
}
//
for (int i = 0; i < ofmt_ctx->nb_streams; i++)
{
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
Console.WriteLine("video.............."+videoindex);
}
}
if (videoindex == -1)
{
Console.WriteLine("Couldn't find a video stream.( )");
return -1;
}
//
if (videoindex > -1)
{
//
AVCodecContext* pCodecCtx = ofmt_ctx->streams[videoindex]->codec;
// id
AVCodec* pCodec = ffmpeg.avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodecCtx, pCodec, null) < 0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a video stream.channel=" + videoindex);
//
var format = ofmt_ctx->iformat->name->ToString();
var len = (ofmt_ctx->duration) / 1000000;
var width = pCodecCtx->width;
var height = pCodecCtx->height;
Console.WriteLine("video format:" + format);
Console.WriteLine("video length:" + len);
Console.WriteLine("video width&height:width=" + width + " height=" + height);
Console.WriteLine("video codec name:" + pCodec->name->ToString());
//
//AVPacket (H264)
// ,
AVPacket* packet = (AVPacket*)ffmpeg.av_malloc((ulong)sizeof(AVPacket));
//AVFrame (YUV)
//
AVFrame* pFrame = ffmpeg.av_frame_alloc();
//YUV420
AVFrame* pFrameYUV = ffmpeg.av_frame_alloc();
// AVFrame 、
//
int out_buffer_size = ffmpeg.avpicture_get_size(AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
byte* out_buffer = (byte*)ffmpeg.av_malloc((ulong)out_buffer_size);
//
ffmpeg.avpicture_fill((AVPicture*)pFrameYUV, out_buffer, AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
// ( ) , , ,
SwsContext* sws_ctx = ffmpeg.sws_getContext(pCodecCtx->width, pCodecCtx->height, AVPixelFormat.AV_PIX_FMT_YUV420P /*pCodecCtx->pix_fmt*/, pCodecCtx->width, pCodecCtx->height, AVPixelFormat.AV_PIX_FMT_YUV420P, ffmpeg.SWS_BICUBIC, null, null, null);
while (ffmpeg.av_read_frame(ofmt_ctx, packet) >= 0)
{
//
if (exit_thread)
{
break;
}
//
if (pause_thread)
{
while (pause_thread)
{
Thread.Sleep(100);
}
}
// ( )
if (packet->stream_index == videoindex)
{
// ,
ret = ffmpeg.avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
//
if (got_picture>0)
{
frame_count++;
Console.WriteLine(" : " + frame_count + " ");
//AVFrame YUV420,
ffmpeg.sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
//SDL YUV
var data = out_buffer;
sdlVideo.SDL_Display(pCodecCtx->width, pCodecCtx->height, (IntPtr)data, out_buffer_size, pFrameYUV->linesize[0]);
}
}
//
ffmpeg.av_free_packet(packet);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
if (&ofmt_ctx != null)
{
ffmpeg.avformat_close_input(&ofmt_ctx);//
}
}
IsRun = false;
return 0;
}
/// <summary>
/// AAC PCM SDL
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlAudio"></param>
/// <returns></returns>
public unsafe int RunAudio(string fileName, SDLAudio sdlAudio)
{
IsRun = true;
exit_thread = false;
pause_thread = false;
threadAudio = Thread.CurrentThread;
int error, frame_count = 0;
int got_frame, ret;
AVFormatContext* ofmt_ctx = null;
SwsContext* pSwsCtx = null;
IntPtr convertedFrameBufferPtr = IntPtr.Zero;
try
{
//
ffmpeg.avcodec_register_all();
//
ofmt_ctx = ffmpeg.avformat_alloc_context();
//
error = ffmpeg.avformat_open_input(&ofmt_ctx, fileName, null, null);
if (error != 0)
{
throw new ApplicationException(FFmpegBinariesHelper.GetErrorMessage(error));
}
//
for (int i = 0; i < ofmt_ctx->nb_streams; i++)
{
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
{
audioindex = i;
Console.WriteLine("audio.............." + audioindex);
}
}
if (audioindex == -1)
{
Console.WriteLine("Couldn't find a audio stream.( )");
return -1;
}
//
if (audioindex > -1)
{
// ,
AVCodecContext* pCodeCtx = ofmt_ctx->streams[audioindex]->codec;
// id, id
AVCodec* pCodec = ffmpeg.avcodec_find_decoder(pCodeCtx->codec_id);
if (pCodec == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodeCtx,pCodec, null)<0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a audio stream. channel=" + audioindex);
//
AVPacket* packet = (AVPacket*)ffmpeg.av_malloc((ulong)(sizeof(AVPacket)));
//
AVFrame* frame = ffmpeg.av_frame_alloc();
//frame->16bit 44100 PCM
SwrContext* swrCtx = ffmpeg.swr_alloc();
// -----------------------------------------------------------start
//
AVSampleFormat in_sample_fmt = pCodeCtx->sample_fmt;
// 16bit PCM
AVSampleFormat out_sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16;
//
int in_sample_rate = pCodeCtx->sample_rate;
//
int out_sample_rate = 44100;
//
long in_ch_layout = (long)pCodeCtx->channel_layout;
//
int out_ch_layout = ffmpeg.AV_CH_LAYOUT_MONO;
ffmpeg.swr_alloc_set_opts(swrCtx, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, null);
ffmpeg.swr_init(swrCtx);
// -----------------------------------------------------------end
//
int out_channel_nb = ffmpeg.av_get_channel_layout_nb_channels((ulong)out_ch_layout);
// pcm
byte* out_buffer = (byte*)ffmpeg.av_malloc(2 * 44100);
// AVPacket
while (ffmpeg.av_read_frame(ofmt_ctx, packet) >= 0)
{
//
if (exit_thread)
{
break;
}
//
if (pause_thread)
{
while (pause_thread)
{
Thread.Sleep(100);
}
}
if (packet->stream_index == audioindex)
{
// AVPacket->AVFrame
ret = ffmpeg.avcodec_decode_audio4(pCodeCtx, frame, &got_frame, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
//
if (got_frame>0)
{
frame_count++;
Console.WriteLine(" : "+ frame_count + " ");
var data_ = frame->data;
ffmpeg.swr_convert(swrCtx, &out_buffer, 2 * 44100,(byte**)&data_, frame->nb_samples);
// sample size
int out_buffer_size = ffmpeg.av_samples_get_buffer_size(null, out_channel_nb, frame->nb_samples, out_sample_fmt, 1);
//
var data=out_buffer;
sdlAudio.PlayAudio((IntPtr)data, out_buffer_size);
}
}
ffmpeg.av_free_packet(packet);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
if (&ofmt_ctx != null)
{
ffmpeg.avformat_close_input(&ofmt_ctx);//
}
}
IsRun = false;
return 0;
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
public void Start(string fileName, SDLHelper sdlVideo,SDLAudio sdlAudio)
{
//
threadVideo = new Thread(() =>
{
try
{
RunVideo(fileName, sdlVideo);
}
catch (Exception ex)
{
SQ.Base.ErrorLog.WriteLog4Ex("JT1078CodecForMp4.Run Video", ex);
}
});
threadVideo.IsBackground = true;
threadVideo.Start();
//
threadAudio = new Thread(() =>
{
try
{
RunAudio(fileName, sdlAudio);
}
catch (Exception ex)
{
SQ.Base.ErrorLog.WriteLog4Ex("JT1078CodecForMp4.Run Audio", ex);
}
});
threadAudio.IsBackground = true;
threadAudio.Start();
}
/// <summary>
///
/// </summary>
public void GoOn()
{
pause_thread = false;
}
/// <summary>
///
/// </summary>
public void Pause()
{
pause_thread = true;
}
/// <summary>
///
/// </summary>
public void Stop()
{
exit_thread = true;
}
}
}
一時停止、継続、停止はここでは意味がありません。解析のスピードが速いからです。テストコードと効果図
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPlay_Click(object sender, EventArgs e)
{
//
string fileName = "test.mp4";// ${Project_home}/bin/Debug/test.mp4
//
jt1078CodecForMp4 = new JT1078CodecForMp4();
jt1078CodecForMp4.Start(fileName,sdlVideo,sdlAudio);
}
注意:ここは緑で、異常です。再生方法のデータ設定を変更します。
/// <summary>
///
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="pixels"></param>
/// <param name="pixelsSize"></param>
/// <param name="pitch"></param>
/// <returns></returns>
public int SDL_Display(int width, int height, IntPtr pixels, int pixelsSize,
int pitch)
{
lock (this)
{
while (isPause)
{
SDL.SDL_Delay(20);//
}
#region SDL
//
sdlrect.x = 0;
sdlrect.y = 0;
sdlrect.w = width;
sdlrect.h = height;
SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
//SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, pixels, pitch);//
//
SDL.SDL_RenderClear(sdltexture);
//SDL.SDL_Rect srcRect = sdlrect;
//SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
//
SDL.SDL_RenderPresent(sdlrenderer);
//SDL.SDL_Delay(40);
//SDL.SDL_PollEvent(out sdlevent);
//switch (sdlevent.type)
//{
// case SDL.SDL_EventType.SDL_QUIT:
// SDL.SDL_Quit();
// return -1;
// default:
// break;
//}
return 0;
}
//SDL.SDL_RenderClear(sdlrenderer);
//SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
//SDL.SDL_RenderPresent(sdlrenderer);
Delay 40ms
//SDL.SDL_Delay(40);
#endregion
//#region SDL
//
sdlrect.x = 0;
sdlrect.y = 0;
sdlrect.w = width;
sdlrect.h = height;
SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
//
SDL.SDL_Rect srcRect = sdlrect;
SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
//
SDL.SDL_RenderPresent(sdlrenderer);
//SDL.SDL_Delay(40);
SDL.SDL_PollEvent(out sdlevent);
switch (sdlevent.type)
{
case SDL.SDL_EventType.SDL_QUIT:
SDL.SDL_Quit();
return -1;
default:
break;
}
return 0;
//#endregion
}
}
キーコード:SDL.SDL_UpdateTexture;
//SDL.SDL_UpdateTexture(sdtexture、IntPtr.Zero、pixels、pitch);このコードはウィンドウの緑の影を再生します。
変更後の効果:
コードを改善して、同じスレッドで音声ビデオを再生します。
/// <summary>
/// MP4 ( )
/// </summary>
public unsafe class JT1078CodecToPlayMp4Two
{
/// <summary>
///
/// </summary>
public bool IsRun { get; protected set; }
/// <summary>
///
/// </summary>
private Thread thread;
/// <summary>
///
/// </summary>
private bool exit_thread = false;
/// <summary>
///
/// </summary>
private bool pause_thread = false;
/// <summary>
/// videoindex
/// </summary>
private int videoindex = -1;
/// <summary>
/// audioindex
/// </summary>
private int audioindex = -1;
private bool isInit = false;
int error;
AVFormatContext* ofmt_ctx = null;
AVPacket* packet;
AVCodecContext* pCodecCtx_Video;
AVCodec* pCodec_Video;
AVFrame* pFrame_Video;
AVFrame* pFrameYUV_Video;
SwsContext* sws_ctx_video;
SDLHelper sdlVideo;
SDLAudio sdlAudio;
int out_buffer_size_video;
byte* out_buffer_video;
int video_frame_count, audio_frame_count;
AVCodecContext* pCodeCtx_Audio;
AVCodec* pCodec_Audio;
AVFrame* frame_Audio;
SwrContext* swrCtx_Audio;
byte* out_buffer_audio;
int out_buffer_size_audio;
int out_channel_nb;
AVSampleFormat out_sample_fmt;
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
/// <param name="sdlAudio"></param>
/// <returns></returns>
public int Init(string fileName, SDLHelper sdlVideo, SDLAudio sdlAudio)
{
AVFormatContext* ofmt_ctx;
//
ffmpeg.avcodec_register_all();
//
ofmt_ctx = ffmpeg.avformat_alloc_context();
this.ofmt_ctx = ofmt_ctx;
//
error = ffmpeg.avformat_open_input(&ofmt_ctx, fileName, null, null);
if (error != 0)
{
throw new ApplicationException(FFmpegBinariesHelper.GetErrorMessage(error));
}
//
for (int i = 0; i < ofmt_ctx->nb_streams; i++)
{
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
Console.WriteLine("video.............." + videoindex);
}
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
{
audioindex = i;
Console.WriteLine("audio.............." + audioindex);
}
}
if (videoindex == -1)
{
Console.WriteLine("Couldn't find a video stream.( )");
return -1;
}
if (audioindex == -1)
{
Console.WriteLine("Couldn't find a audio stream.( )");
return -1;
}
#region
//
if (videoindex > -1)
{
//
pCodecCtx_Video = ofmt_ctx->streams[videoindex]->codec;
// id
pCodec_Video = ffmpeg.avcodec_find_decoder(pCodecCtx_Video->codec_id);
if (pCodec_Video == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodecCtx_Video, pCodec_Video, null) < 0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a video stream.channel=" + videoindex);
//
var format = ofmt_ctx->iformat->name->ToString();
var len = (ofmt_ctx->duration) / 1000000;
var width = pCodecCtx_Video->width;
var height = pCodecCtx_Video->height;
Console.WriteLine("video format:" + format);
Console.WriteLine("video length:" + len);
Console.WriteLine("video width&height:width=" + width + " height=" + height);
Console.WriteLine("video codec name:" + pCodec_Video->name->ToString());
//
//AVPacket (H264)
//AVFrame (YUV)
//
pFrame_Video = ffmpeg.av_frame_alloc();
//YUV420
pFrameYUV_Video = ffmpeg.av_frame_alloc();
// AVFrame 、
//
out_buffer_size_video = ffmpeg.avpicture_get_size(AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height);
out_buffer_video = (byte*)ffmpeg.av_malloc((ulong)out_buffer_size_video);
//
ffmpeg.avpicture_fill((AVPicture*)pFrameYUV_Video, out_buffer_video, AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height);
// ( ) , , ,
sws_ctx_video = ffmpeg.sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, AVPixelFormat.AV_PIX_FMT_YUV420P /*pCodecCtx->pix_fmt*/, pCodecCtx_Video->width, pCodecCtx_Video->height, AVPixelFormat.AV_PIX_FMT_YUV420P, ffmpeg.SWS_BICUBIC, null, null, null);
}
#endregion
#region
//
if (audioindex > -1)
{
// ,
pCodeCtx_Audio = ofmt_ctx->streams[audioindex]->codec;
// id, id
pCodec_Audio = ffmpeg.avcodec_find_decoder(pCodeCtx_Audio->codec_id);
if (pCodec_Audio == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodeCtx_Audio, pCodec_Audio, null) < 0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a audio stream. channel=" + audioindex);
//
frame_Audio = ffmpeg.av_frame_alloc();
//frame->16bit 44100 PCM
swrCtx_Audio = ffmpeg.swr_alloc();
// -----------------------------------------------------------start
//
AVSampleFormat in_sample_fmt = pCodeCtx_Audio->sample_fmt;
// 16bit PCM
out_sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16;
//
int in_sample_rate = pCodeCtx_Audio->sample_rate;
//
int out_sample_rate = 44100;
//
long in_ch_layout = (long)pCodeCtx_Audio->channel_layout;
//
int out_ch_layout = ffmpeg.AV_CH_LAYOUT_MONO;
ffmpeg.swr_alloc_set_opts(swrCtx_Audio, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, null);
ffmpeg.swr_init(swrCtx_Audio);
// -----------------------------------------------------------end
//
out_channel_nb = ffmpeg.av_get_channel_layout_nb_channels((ulong)out_ch_layout);
// pcm
out_buffer_audio = (byte*)ffmpeg.av_malloc(2 * 44100);
}
#endregion
// ,
packet = (AVPacket*)ffmpeg.av_malloc((ulong)sizeof(AVPacket));
// SDL
this.sdlVideo = sdlVideo;
this.sdlAudio = sdlAudio;
isInit = true;
return 0;
}
/// <summary>
///
/// </summary>
public unsafe int ReadAndPlay()
{
IsRun = true;
exit_thread = false;
pause_thread = false;
thread = Thread.CurrentThread;
//int error, frame_count = 0;
int got_frame, ret;
//SwsContext* pSwsCtx = null;
byte* out_audio_buffer = out_buffer_audio;
try
{
while (ffmpeg.av_read_frame(ofmt_ctx, packet) >= 0)
{
//
if (exit_thread)
{
break;
}
//
while (pause_thread)
{
Thread.Sleep(100);
}
#region H264 YUV SDL
if (packet->stream_index == videoindex)
{
// ,
ret = ffmpeg.avcodec_decode_video2(pCodecCtx_Video, pFrame_Video, &got_frame, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
//
if (got_frame > 0)
{
double pts = 0; //ffmpeg.av_frame_get_best_effort_timestamp(pFrameYUV_Video);
//VideoState* vs = null;
//vs->video_clock = pts;
//vs->video_st = ofmt_ctx->streams[videoindex];
//pts = synchronize_video(vs, pFrame_Video, pts);
//if (queue_picture(is, pFrame, pts) < 0)
//{
// break;
//}
video_frame_count++;
// PTS
//int pts = video_frame_count++ * (pCodecCtx_Video->pkt_timebase.num * 1000 / 25 /* pCodecCtx->pkt_timebase.den*/);
Console.WriteLine(" : " + video_frame_count + " ");
//AVFrame YUV420,
ffmpeg.sws_scale(sws_ctx_video, pFrame_Video->data, pFrame_Video->linesize, 0, pCodecCtx_Video->height, pFrameYUV_Video->data, pFrameYUV_Video->linesize);
Console.WriteLine(" : pts= " + packet->pts + " dts=" + packet->dts);
// SDL YUV :
sdlVideo.SDL_Display(pCodecCtx_Video->width, pCodecCtx_Video->height, (IntPtr)out_buffer_video, out_buffer_size_video, pFrameYUV_Video->linesize[0]);
//sdlVideo.SDL_Display(pCodecCtx_Video->width, pCodecCtx_Video->height, (IntPtr)pFrameYUV_Video->data[0], out_buffer_size_video, pFrameYUV_Video->linesize[0]);
//DeleyToPlay_Video(packet->pts);
}
}
#endregion
#region AAC PCM SDL
if (packet->stream_index == audioindex)
{
// AVPacket->AVFrame
ret = ffmpeg.avcodec_decode_audio4(pCodeCtx_Audio, frame_Audio, &got_frame, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
//
if (got_frame > 0)
{
audio_frame_count++;
Console.WriteLine(" : " + audio_frame_count + " ");
//
ffmpeg.swr_convert(swrCtx_Audio, &out_audio_buffer, 2 * 44100, (byte**)&frame_Audio->data, frame_Audio->nb_samples);
// sample size
out_buffer_size_audio = ffmpeg.av_samples_get_buffer_size(null, out_channel_nb, frame_Audio->nb_samples, out_sample_fmt, 1);
Console.WriteLine(" : pts= " + packet->pts + " dts=" + packet->dts);
// SDL
sdlAudio.PlayAudio((IntPtr)out_audio_buffer, out_buffer_size_audio);
//DeleyToPlay_Audio(packet->pts);
}
}
#endregion
Thread.Sleep(20);
//
ffmpeg.av_free_packet(packet);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
//if (&ofmt_ctx != null)
//{
// ffmpeg.avformat_close_input(&ofmt_ctx);//
//}
}
IsRun = false;
return 0;
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
/// <param name="sdlAudio"></param>
public void Start()
{
if (!isInit)
{
MessageBox.Show(" ");
}
thread = new Thread(() =>
{
try
{
ReadAndPlay();
}
catch (Exception ex)
{
SQ.Base.ErrorLog.WriteLog4Ex("JT1078CodecForMp4.Run Video", ex);
}
});
thread.IsBackground = true;
thread.Start();
}
/// <summary>
///
/// </summary>
public void GoOnPlay()
{
pause_thread = false;
sdlVideo.PlayVideo();
sdlAudio.PlayAudio();
}
/// <summary>
///
/// </summary>
public void Pause()
{
pause_thread = true;
sdlVideo.PauseVideo();
sdlAudio.PauseAudio();
}
/// <summary>
///
/// </summary>
public void Stop()
{
exit_thread = true;
}
long lastPts_Video = 0;
DateTime lastTS_Video;
long lastPts_Audio = 0;
DateTime lastTS_Audio;
private void DeleyToPlay_Video(long pts)
{
if (lastPts_Video > 0 && lastTS_Video != null)
{
double delay = (DateTime.Now - lastTS_Video).TotalMilliseconds;
var i = (int)(pts - lastPts_Video - delay);
if (i >= 1)
{
Thread.Sleep(i);
}
}
lastTS_Video = DateTime.Now;
lastPts_Video = pts;
}
private void DeleyToPlay_Audio(long pts)
{
if (lastPts_Audio > 0 && lastTS_Audio != null)
{
double delay = (DateTime.Now - lastTS_Audio).TotalMilliseconds;
var i = (int)(pts - lastPts_Audio - delay);
if (i >= 1)
{
Thread.Sleep(i);
}
}
lastTS_Audio = DateTime.Now;
lastPts_Audio = pts;
}
# http://dranger.com/ffmpeg/tutorial05.html
//public struct VideoState
//{
// public double video_clock; // pts of last decoded frame / predicted pts of next decoded frame
// public AVStream* video_st;// video stream
//}
//public unsafe double synchronize_video(VideoState* vs, AVFrame* src_frame, double pts)
//{
// double frame_delay;
// if (pts != 0)
// {
// /* if we have pts, set video clock to it */
// vs->video_clock = pts;
// }
// else
// {
// /* if we aren't given a pts, set it to the clock */
// pts = vs->video_clock;
// }
// /* update the video clock */
// frame_delay = av_q2d(vs->video_st->codec->time_base);
// /* if we are repeating a frame, adjust clock accordingly */
// frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
// vs->video_clock += frame_delay;
// return pts;
//}
//struct VideoPicture
//{
// double pts;
//}
//int queue_picture(VideoState* vs, AVFrame* pFrame, double pts)
//{
// if (vp->bmp)
// {
// ... convert picture ...
// vp->pts = pts;
// ... alert queue ...
// }
//}
}
ビデオの同期問題のバージョンを解決します。
using CV.Media.Utils.Filter;
using CV.Video.Base;
using CV.Video.Base.FFmpeg;
using FFmpeg.AutoGen;
using JX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CvNetVideo.UCVideo;
namespace CvNetVideo.Codec.Video
{
/// <summary>
/// MP4 ( )
/// </summary>
public unsafe class JT1078CodecToPlayMp4
{
/// <summary>
///
/// </summary>
public bool IsRun { get; protected set; }
/// <summary>
///
/// </summary>
public bool IsPause { get; protected set; }
/// <summary>
///
/// </summary>
public Thread thread;
/// <summary>
///
/// </summary>
private bool exit_thread = false;
/// <summary>
///
/// </summary>
private bool pause_thread = false;
/// <summary>
/// videoindex
/// </summary>
private int videoindex = -1;
/// <summary>
/// audioindex
/// </summary>
private int audioindex = -1;
/// <summary>
///
/// </summary>
private bool isInit = false;
int error;
AVFormatContext* ofmt_ctx = null;
AVPacket* packet;
AVCodecContext* pCodecCtx_Video;
AVCodec* pCodec_Video;
AVFrame* pFrame_Video;
AVFrame* pFrameYUV_Video;
SwsContext* sws_ctx_video;
SDLHelper sdlVideo;
SDLAudio sdlAudio;
int out_buffer_size_video;
byte* out_buffer_video;
int video_frame_count, audio_frame_count;
AVCodecContext* pCodeCtx_Audio;
AVCodec* pCodec_Audio;
AVFrame* frame_Audio;
SwrContext* swrCtx_Audio;
byte* out_buffer_audio;
int out_buffer_size_audio;
int out_channel_nb;
AVSampleFormat out_sample_fmt;
int contrast;//
int brightness;//
int contrast_last;//
int brightness_last;//
//
private VideoFiltering m_video_filtering = new VideoFiltering();
/// <summary>
///
/// </summary>
/// <param name="contrast"></param>
/// <param name="brightness"></param>
/// <returns></returns>
public void SetContrastAndBrightness(int contrast, int brightness)
{
this.contrast = contrast;
this.brightness = brightness;
}
/// <summary>
/// YUV
/// </summary>
public int YuvWidth { get; set; }
/// <summary>
/// YUV
/// </summary>
public int YuvHeight { get; set; }
/// <summary>
///
/// </summary>
List<AVVideo> list = new List<AVVideo>();
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
/// <param name="sdlAudio"></param>
/// <returns></returns>
public int Init(string fileName, SDLHelper sdlVideo, SDLAudio sdlAudio)
{
AVFormatContext* ofmt_ctx;
//
ffmpeg.avcodec_register_all();
//
ofmt_ctx = ffmpeg.avformat_alloc_context();
this.ofmt_ctx = ofmt_ctx;
//
error = ffmpeg.avformat_open_input(&ofmt_ctx, fileName, null, null);
if (error != 0)
{
throw new ApplicationException(FFmpegBinariesHelper.GetErrorMessage(error));
}
//
for (int i = 0; i < ofmt_ctx->nb_streams; i++)
{
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
Console.WriteLine("video.............." + videoindex);
}
if (ofmt_ctx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
{
audioindex = i;
Console.WriteLine("audio.............." + audioindex);
}
}
if (videoindex == -1)
{
Console.WriteLine("Couldn't find a video stream.( )");
return -1;
}
if (audioindex == -1)
{
Console.WriteLine("Couldn't find a audio stream.( )");
return -1;
}
#region
//
if (videoindex > -1)
{
//
pCodecCtx_Video = ofmt_ctx->streams[videoindex]->codec;
// id
pCodec_Video = ffmpeg.avcodec_find_decoder(pCodecCtx_Video->codec_id);
if (pCodec_Video == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodecCtx_Video, pCodec_Video, null) < 0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a video stream.channel=" + videoindex);
//
var format = ofmt_ctx->iformat->name->ToString();
var len = (ofmt_ctx->duration) / 1000000;
var width = pCodecCtx_Video->width;
var height = pCodecCtx_Video->height;
Console.WriteLine("video format:" + format);
Console.WriteLine("video length:" + len);
Console.WriteLine("video width&height:width=" + width + " height=" + height);
Console.WriteLine("video codec name:" + pCodec_Video->name->ToString());
//
//AVPacket (H264)
//AVFrame (YUV)
//
pFrame_Video = ffmpeg.av_frame_alloc();
//YUV420
pFrameYUV_Video = ffmpeg.av_frame_alloc();
// AVFrame 、
//
out_buffer_size_video = ffmpeg.avpicture_get_size(AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height);
out_buffer_video = (byte*)ffmpeg.av_malloc((ulong)out_buffer_size_video);
//
ffmpeg.avpicture_fill((AVPicture*)pFrameYUV_Video, out_buffer_video, AVPixelFormat.AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height);
// ( ) , , ,
sws_ctx_video = ffmpeg.sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, AVPixelFormat.AV_PIX_FMT_YUV420P /*pCodecCtx->pix_fmt*/, pCodecCtx_Video->width, pCodecCtx_Video->height, AVPixelFormat.AV_PIX_FMT_YUV420P, ffmpeg.SWS_BICUBIC, null, null, null);
}
#endregion
#region
//
if (audioindex > -1)
{
// ,
pCodeCtx_Audio = ofmt_ctx->streams[audioindex]->codec;
// id, id
pCodec_Audio = ffmpeg.avcodec_find_decoder(pCodeCtx_Audio->codec_id);
if (pCodec_Audio == null)
{
Console.WriteLine(" ");
return -1;
}
//
if (ffmpeg.avcodec_open2(pCodeCtx_Audio, pCodec_Audio, null) < 0)
{
Console.WriteLine(" ");
return -1;
}
Console.WriteLine("Find a audio stream. channel=" + audioindex);
//
frame_Audio = ffmpeg.av_frame_alloc();
//frame->16bit 8000 PCM
swrCtx_Audio = ffmpeg.swr_alloc();
// -----------------------------------------------------------start
//
AVSampleFormat in_sample_fmt = pCodeCtx_Audio->sample_fmt;
// 16bit PCM
out_sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16;
//
int in_sample_rate = pCodeCtx_Audio->sample_rate;
//
int out_sample_rate = 8000;
//
long in_ch_layout = (long)pCodeCtx_Audio->channel_layout;
//
int out_ch_layout = ffmpeg.AV_CH_LAYOUT_MONO;
ffmpeg.swr_alloc_set_opts(swrCtx_Audio, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, null);
ffmpeg.swr_init(swrCtx_Audio);
// -----------------------------------------------------------end
//
out_channel_nb = ffmpeg.av_get_channel_layout_nb_channels((ulong)out_ch_layout);
// pcm
out_buffer_audio = (byte*)ffmpeg.av_malloc(2 * 8000);
}
#endregion
// ,
packet = (AVPacket*)ffmpeg.av_malloc((ulong)sizeof(AVPacket));
// SDL
this.sdlVideo = sdlVideo;
this.sdlAudio = sdlAudio;
isInit = true;
return 0;
}
/// <summary>
///
/// </summary>
public unsafe int ReadAndPlay(PlayFinishedDo playFinishedDo)
{
IsRun = true;
exit_thread = false;
pause_thread = false;
thread = Thread.CurrentThread;
//int error, frame_count = 0;
int got_frame, ret;
//SwsContext* pSwsCtx = null;
byte* out_audio_buffer = out_buffer_audio;
try
{
AVStream* video_stream = ofmt_ctx->streams[videoindex];
while (ffmpeg.av_read_frame(ofmt_ctx, packet) >= 0&& !exit_thread)
{
//
while (pause_thread||isLastFrame)
{
//
if (exit_thread)
{
break;
}
Thread.Sleep(10);
}
//
if (exit_thread)
{
break;
}
//
if (firstPts == -1 && packet->stream_index == videoindex)
{
firstPts = packet->pts * 1000 / (video_stream->time_base.den / video_stream->time_base.num);
startTS = DateTime.Now;
}
// ,
if (packet->stream_index == videoindex)
{
long pts_1 = packet->pts * 1000 / (video_stream->time_base.den / video_stream->time_base.num);
DeleyToPlay(pts_1);
}
#region H264 YUV SDL
if (packet->stream_index == videoindex)
{
// ,
ret = ffmpeg.avcodec_decode_video2(pCodecCtx_Video, pFrame_Video, &got_frame, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
// , , === JT1078ToYuv -----------
int width = pCodecCtx_Video->width;
int height = pCodecCtx_Video->height;
if (contrast != contrast_last || brightness != brightness_last)
{
m_video_filtering.Reset(width, height, contrast, brightness);
contrast_last = contrast;
brightness_last = brightness;
}
// , , === JT1078ToYuv -----------
//
if (got_frame > 0)
{
video_frame_count++;
//>>>> , , === JT1078ToYuv -----------
AVFrame* frame_filter;
ret = m_video_filtering.Filter(pFrame_Video, &frame_filter);
//>>>> , , === JT1078ToYuv -----------
//AVFrame YUV420,
ffmpeg.sws_scale(sws_ctx_video, frame_filter->data, frame_filter->linesize, 0, pCodecCtx_Video->height, pFrameYUV_Video->data, pFrameYUV_Video->linesize);
// 10
AVVideo videoFrame = new AVVideo(pCodecCtx_Video->width, pCodecCtx_Video->height, (IntPtr)out_buffer_video, out_buffer_size_video, pFrameYUV_Video->linesize[0]);
list.Add(videoFrame);
if (list.Count > 10) list.RemoveAt(0);
// SDL YUV :
sdlVideo.SDL_Display(pCodecCtx_Video->width, pCodecCtx_Video->height,YuvWidth, YuvHeight, (IntPtr)out_buffer_video, out_buffer_size_video, pFrameYUV_Video->linesize[0]);
//sdlVideo.SDL_Display(pCodecCtx_Video->width, pCodecCtx_Video->height, (IntPtr)pFrameYUV_Video->data[0], out_buffer_size_video, pFrameYUV_Video->linesize[0]);
//
if (isNextFrame)
{
Pause();
isNextFrame = false;
}
//
m_video_filtering.UnrefFrame();
}
}
#endregion
#region AAC PCM SDL
if (packet->stream_index == audioindex)
{
// AVPacket->AVFrame
ret = ffmpeg.avcodec_decode_audio4(pCodeCtx_Audio, frame_Audio, &got_frame, packet);
if (ret < 0)
{
Console.WriteLine(" ");
return -1;
}
//
if (got_frame > 0)
{
audio_frame_count++;
//
ffmpeg.swr_convert(swrCtx_Audio, &out_audio_buffer, 2 * 8000, (byte**)&frame_Audio->data, frame_Audio->nb_samples);
// sample size
out_buffer_size_audio = ffmpeg.av_samples_get_buffer_size(null, out_channel_nb, frame_Audio->nb_samples, out_sample_fmt, 1);
// SDL
sdlAudio.PlayAudio((IntPtr)out_audio_buffer, out_buffer_size_audio);
}
}
#endregion
//
ffmpeg.av_free_packet(packet);
Thread.Sleep(10);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
//
ffmpeg.avformat_free_context(ofmt_ctx);
//
playFinishedDo.Invoke();
}
IsRun = false;
IsPause = false;
return 0;
}
bool isLastFrame = false;
bool isNextFrame = false;
bool playFastly = false;
bool playSlowly = false;
int play_speed = 1;
long firstPts = -1;
DateTime startTS;
/// <summary>
///
/// </summary>
/// <param name="pts"></param>
/// <param name="speed"></param>
private void DeleyToPlay(long pts)
{
int delayTime = 0;
try
{
//
double delay = (DateTime.Now - startTS).TotalMilliseconds;
var i = (int)(pts - firstPts - delay);
if (i >= 100)
{
delayTime = 40;
delayTime = ControlFastOrSlow(delayTime);
}
else if (i >= 300)
{
delayTime = 60;
delayTime = ControlFastOrSlow(delayTime);
}
else if (i >= 500)
{
delayTime = 100;
delayTime = ControlFastOrSlow(delayTime);
}
}
catch
{
Console.WriteLine("Counting delay time error ");
}
finally
{
Console.WriteLine("Counting delay time = " + delayTime+ " play_speed="+ play_speed);
if (delayTime > 0)
Thread.Sleep(delayTime);
}
}
/// <summary>
///
/// </summary>
/// <param name="delayTime"></param>
private int ControlFastOrSlow(int delayTime)
{
if (playFastly)
{
//
delayTime /= play_speed;
}
else if (playSlowly)
{
//
delayTime *= play_speed;
}
return delayTime;
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="sdlVideo"></param>
/// <param name="sdlAudio"></param>
public void Start(PlayFinishedDo playFinishedDo)
{
if (!isInit)
{
MessageBox.Show(" ");
}
thread = new Thread(() =>
{
try
{
ReadAndPlay(playFinishedDo);
}
catch (Exception ex)
{
SQ.Base.ErrorLog.WriteLog4Ex("JT1078CodecForMp4.Run Video", ex);
}
});
thread.IsBackground = true;
thread.Start();
}
/// <summary>
///
/// </summary>
public void GoOnPlay()
{
// pts,
firstPts = -1;
//
pause_thread = false;
IsPause = pause_thread;
sdlVideo.PlayVideo();
sdlAudio.PlayAudio();
}
/// <summary>
///
/// </summary>
public void Pause()
{
//
pause_thread = true;
IsPause = pause_thread;
sdlVideo.PauseVideo();
sdlAudio.PauseAudio();
}
/// <summary>
///
/// </summary>
public void Stop()
{
exit_thread = true;
if (thread != null && thread.IsAlive)
{
thread.Abort();
thread.Join();
thread = null;
}
}
/// <summary>
///
/// </summary>
public void PlayFast()
{
if (pause_thread)
{
//
GoOnPlay();
}
if (playSlowly)
{
play_speed = 1;
playSlowly = false;
}
else
{
play_speed++;
}
playFastly = true;
}
/// <summary>
///
/// </summary>
public void PlaySlow()
{
if (pause_thread)
{
//
GoOnPlay();
}
if (playFastly)
{
play_speed = 1;
playFastly = false;
}
else
{
play_speed++;
}
playSlowly = true;
}
/// <summary>
///
/// </summary>
public void PlayLastFrame()
{
//
isLastFrame = true;
//
if (list.Count>0)
{
Console.WriteLine(" :"+ list.Count);
//
GoOnPlay();
AVVideo lastFrame = list.Last();
//
sdlVideo.SDL_Display(lastFrame.width, lastFrame.height, lastFrame.pixels, lastFrame.pixelsSize, lastFrame.pitch);
//
isLastFrame = false;
//
list.Remove(lastFrame);
Thread.Sleep(10);
Pause();
}
}
/// <summary>
///
/// </summary>
public void PlayNextFrame()
{
//
Pause();
//
GoOnPlay();
//
isNextFrame = true;
}
}
class Media
{
/// <summary>
/// 0:video,1:audio
/// </summary>
public int type { get; set; }
/// <summary>
/// pts value
/// </summary>
public long pts { get; set; }
}
class AVVideo : Media
{
public int width { get; set; }
public int height { get; set; }
public IntPtr pixels { get; set; }
public int pixelsSize { get; set; }
public int pitch { get; set; }
public AVVideo(int width, int height, IntPtr pixels, int pixelsSize, int pitch)
{
this.width = width;
this.height = height;
this.pixels = pixels;
this.pixelsSize = pixelsSize;
this.pitch = pitch;
}
}
class AVAudio : Media
{
public IntPtr pcm { get; set; }
public int len { get; set; }
public AVAudio(IntPtr pcm, int len)
{
this.pcm = pcm;
this.len = len;
}
}
}
以上のC〓はSDL 2を使ってMp 4ファイルの再生音ビデオの操作を実現しました。小編は皆さんに共有した内容です。参考にしていただければと思います。どうぞよろしくお願いします。