博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用信号量控制线程执行顺序,进而控制不同视频流的解码顺序
阅读量:4224 次
发布时间:2019-05-26

本文共 9658 字,大约阅读时间需要 32 分钟。

// 使用Window下多线程API以及信号量来控制视频的解码顺序

#include 
#include "QueryPerformance.h" // time counter#include "windows.h"#include
#include
#include "cudaDecodeInterface.h"#pragma comment(lib,"cudaDecode.lib")typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC) (LPVOID lpThreadParameter);typedef unsigned *PBEGINTHREADEX_THREADID;HANDLE hand_cuda[6] = {NULL, NULL, NULL, NULL,NULL,NULL};bool cuda_decodeEnd[6] = {false, false, false, false,false,false};static int counter[6] = {0, 0, 0, 0,0,0};FILE *fp0 = fopen("../decodePic/cudadecode0.yuv", "wb");FILE *fp1 = fopen("../decodePic/cudadecode1.yuv", "wb");FILE *fp2 = fopen("../decodePic/cudadecode2.yuv", "wb");FILE *fp3 = fopen("../decodePic/cudadecode3.yuv", "wb");FILE *fp4 = fopen("../decodePic/cudadecode4.yuv", "wb");FILE *fp5 = fopen("../decodePic/cudadecode5.yuv", "wb");// front claimint CudaDecodeNotify0(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);int CudaDecodeNotify1(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);int CudaDecodeNotify2(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);int CudaDecodeNotify3(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);int CudaDecodeNotify4(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);int CudaDecodeNotify5(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);DWORD WINAPI OnCuda0();DWORD WINAPI OnCuda1();DWORD WINAPI OnCuda2();DWORD WINAPI OnCuda3();DWORD WINAPI OnCuda4();DWORD WINAPI OnCuda5();HANDLE handle_video[6] = {NULL, NULL, NULL, NULL,NULL,NULL};HANDLE hand_semaphore[6] = {NULL, NULL, NULL, NULL,NULL,NULL};int main(int argc, char* argv[]){ printf("Cuda Decoder......\n"); // Initialzie semaphore hand_semaphore[0] = CreateSemaphore(NULL, 1, 1, NULL); hand_semaphore[1] = CreateSemaphore(NULL, 0, 1, NULL); hand_semaphore[2] = CreateSemaphore(NULL, 0, 1, NULL); hand_semaphore[3] = CreateSemaphore(NULL, 0, 1, NULL); hand_semaphore[4] = CreateSemaphore(NULL, 0, 1, NULL); hand_semaphore[5] = CreateSemaphore(NULL, 0, 1, NULL); // Initialize hand // DWORD begin_time = GetTickCount64(); CStopwatch stopwatch; WaitForSingleObject(hand_semaphore[0], INFINITE); handle_video[0] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda0, NULL, 0, NULL); WaitForSingleObject(hand_semaphore[1], INFINITE); handle_video[1] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda1, NULL, 0, NULL); WaitForSingleObject(hand_semaphore[2], INFINITE); handle_video[2] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda2, NULL, 0, NULL); WaitForSingleObject(hand_semaphore[3], INFINITE); handle_video[3] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda3, NULL, 0, NULL); WaitForSingleObject(hand_semaphore[4], INFINITE); handle_video[4] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda4, NULL, 0, NULL); WaitForSingleObject(hand_semaphore[5], INFINITE); handle_video[5] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda5, NULL, 0, NULL); WaitForMultipleObjects(6, handle_video, TRUE, INFINITE); __int64 qwElapsedTime = stopwatch.Now(); printf("总用时为:%d
\n", qwElapsedTime); // DWORD end_time = GetTickCount64(); // printf("总用时为:%d
\n", end_time - begin_time); for (int i=0; i<6; i++) { CloseHandle(handle_video[i]); } fflush(stdin); system("pause"); return 0;}// OnCuda函数DWORD WINAPI OnCuda0(){ bool nstate = Cuda_SupportDecode(); hand_cuda[0] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[0], "../data/720P/video0.m2v", CudaDecodeNotify0)==1 ) { StartTime = GetTickCount(); printf("文件<1>打开成功\n"); } else printf("文件<1>打开失败\n"); while ( !cuda_decodeEnd[0] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<1>解码完成 nTime1 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[0]); return 0;}DWORD WINAPI OnCuda1(){ bool nstate = Cuda_SupportDecode(); hand_cuda[1] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[1], "../data/720P/video1.m2v", CudaDecodeNotify1)==1 ) { StartTime = GetTickCount(); printf("文件<2>打开成功\n"); } else printf("文件<2>打开失败\n"); while ( !cuda_decodeEnd[1] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<2>解码完成 nTime2 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[1]); return 0;}DWORD WINAPI OnCuda2(){ bool nstate = Cuda_SupportDecode(); hand_cuda[2] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[2], "../data/720P/video2.m2v", CudaDecodeNotify2)==1 ) { StartTime = GetTickCount(); printf("文件<3>打开成功\n"); } else printf("文件<3>打开失败\n"); while ( !cuda_decodeEnd[2] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<3>解码完成 nTime3 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[2]); return 0;}DWORD WINAPI OnCuda3(){ bool nstate = Cuda_SupportDecode(); hand_cuda[3] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[3], "../data/720P/video3.m2v", CudaDecodeNotify3)==1 ) { StartTime = GetTickCount(); printf("文件<4>打开成功\n"); } else printf("文件<4>打开失败\n"); while ( !cuda_decodeEnd[3] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<4>解码完成 nTime4 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[3]); return 0;}DWORD WINAPI OnCuda4(){ bool nstate = Cuda_SupportDecode(); hand_cuda[4] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[4], "../data/720P/video4.m2v", CudaDecodeNotify4)==1 ) { StartTime = GetTickCount(); printf("文件<1>打开成功\n"); } else printf("文件<1>打开失败\n"); while ( !cuda_decodeEnd[4] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<1>解码完成 nTime1 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[4]); return 0;}DWORD WINAPI OnCuda5(){ bool nstate = Cuda_SupportDecode(); hand_cuda[5] = Cuda_CreateDecoder(); DWORD StartTime = 0; // 起始的时间 DWORD ElapsedTime = 0; //消耗的时间 if( Cuda_OpenFile(hand_cuda[5], "../data/720P/video5.m2v", CudaDecodeNotify5)==1 ) { StartTime = GetTickCount(); printf("文件<1>打开成功\n"); } else printf("文件<1>打开失败\n"); while ( !cuda_decodeEnd[5] ) { Sleep(1); } ElapsedTime = GetTickCount() - StartTime; // printf("文件<1>解码完成 nTime1 = %d
\n", ElapsedTime); Cuda_DestroyDecoder(hand_cuda[5]); return 0;}// cudaDecodeNotify函数,拷贝解码后的数据到硬盘int CudaDecodeNotify0(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[0]++; // fwrite(pDecodeData,1,nDataSize,fp0); if(nEof) { OutputDebugString("thread 1 读包结束!!!\n"); printf("====== thread 1 读包结束!!!\n"); // fclose(fp0); cuda_decodeEnd[0] = true; }#if 0 printf(" \n------ video 1:%d ------\n", counter[0]); FILE *file = fopen("../order.txt", "at"); fprintf(file, " ------ 视频1:%d ------\n", counter[0]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[1], 1, NULL); Sleep(1); return 0;}int CudaDecodeNotify1(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[1]++; // fwrite(pDecodeData,1,nDataSize,fp1); if(nEof) { OutputDebugString("thread 2 读包结束!!!\n"); printf("====== thread 2 读包结束!!!\n"); // fclose(fp4); cuda_decodeEnd[1] = true; }#if 0 printf(" \n------ video 2 : %d ------\n", counter[1]); FILE *file = fopen("../order_new.txt", "at"); fprintf(file, " ------ 视频2:%d ------\n", counter[1]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[2], 1, NULL); Sleep(1); return 0;}int CudaDecodeNotify2(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[2]++; // fwrite(pDecodeData,1,nDataSize,fp2); if(nEof) { OutputDebugString("thread 3 读包结束!!!\n"); printf("====== thread 3 读包结束!!!\n"); // fclose(fp4); cuda_decodeEnd[2] = true; }#if 0 printf(" \n------ video 3:%d ------\n", counter[2]); FILE *file = fopen("../order.txt", "at"); fprintf(file, " ------ 视频3:%d ------\n", counter[2]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[3], 1, NULL); Sleep(1); return 0;}int CudaDecodeNotify3(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[3]++; // fwrite(pDecodeData,1,nDataSize,fp3); if(nEof) { OutputDebugString("thread 4 读包结束!!!\n"); printf("====== thread 4 读包结束!!!\n"); // fclose(fp4); cuda_decodeEnd[3] = true; }#if 0 printf(" \n------ video 4 :%d ------\n\n", counter[3]); FILE *file = fopen("../order.txt", "at"); fprintf(file, " ------ 视频4:%d ------\n", counter[3]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[4], 1, NULL); Sleep(1); return 0;}int CudaDecodeNotify4(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[4]++; // fwrite(pDecodeData,1,nDataSize,fp0); if(nEof) { OutputDebugString("thread 5 读包结束!!!\n"); printf("====== thread 5 读包结束!!!\n"); // fclose(fp0); cuda_decodeEnd[4] = true; }#if 0 printf(" \n------ video 1:%d ------\n", counter[0]); FILE *file = fopen("../order.txt", "at"); fprintf(file, " ------ 视频1:%d ------\n", counter[0]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[5], 1, NULL); Sleep(1); return 0;}int CudaDecodeNotify5(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof){ counter[5]++; // fwrite(pDecodeData,1,nDataSize,fp0); if(nEof) { OutputDebugString("thread 6 读包结束!!!\n"); printf("====== thread 6 读包结束!!!\n"); // fclose(fp0); cuda_decodeEnd[5] = true; }#if 0 printf(" \n------ video 1:%d ------\n", counter[0]); FILE *file = fopen("../order.txt", "at"); fprintf(file, " ------ 视频1:%d ------\n", counter[0]); fclose(file);#endif ReleaseSemaphore(hand_semaphore[0], 1, NULL); Sleep(1); return 0;}

转载地址:http://lwkqi.baihongyu.com/

你可能感兴趣的文章
近期活动盘点:大数据应用中日交流论坛、几位专家带你认识Flink、青年自强AI计划 CV免费公开课!(11.12-11.23)...
查看>>
清华出品 | 人脸识别最全知识图谱
查看>>
张钹院士:从少年郎到白发翁,水木清华60载,智能人生谱华章
查看>>
独家 | 利用Auto ARIMA构建高性能时间序列模型(附Python和R代码)
查看>>
医工智能 创享未来—— “医工科研数据平台”与“医工结合科研创新支持计划”发布!...
查看>>
手把手教你用Kaggle开启机器学习之旅(附资源链接)
查看>>
手把手教你用Python处理非平稳时间序列(附代码)
查看>>
爱奇艺蒙版AI:弹幕穿人过,爱豆心中坐
查看>>
大满贯!清华学生超算团队获得国际大学生超级计算机竞赛(SC18)总冠军(附现场视频)...
查看>>
RONG·基础与未来——规模化园区在数据时代如何实现内生增长?
查看>>
收藏 | 最新知识图谱论文清单(附解读、下载)
查看>>
独家 | 一文读懂R中的探索性数据分析(附R代码)
查看>>
独家 | 手把手教你用Python进行Web抓取(附代码)
查看>>
独家 | 提速20倍!3个细节优化Tableau工作簿加载过程(附实例)
查看>>
经济学人:清华大学或将迅速成为全球科研领军者
查看>>
超燃!Apache Flink 全球顶级盛会强势来袭
查看>>
资源 | 机器学习高质量数据集大合辑(附链接)
查看>>
第二届清华大学iCenter量化策略挑战赛开幕!
查看>>
在数据科学人才教育中不断前行: 《大数据系统基础A、B》课程实践项目中期答辩顺利举行...
查看>>
送你一份使用k近邻算法实现回归的实用指南(附代码、链接)
查看>>