-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdecoder.cpp
More file actions
87 lines (79 loc) · 2.94 KB
/
Copy pathdecoder.cpp
File metadata and controls
87 lines (79 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "decoder.h"
#include "cuvid/Utils/NvCodecUtils.h"
#include "cuvid/Utils/FFmpegDemuxer.h"
#include "cuvid/Utils/ColorSpace.h"
#include "cuvid/AppDecUtils.h"
#include <libavcodec/avcodec.h>
#include "cuvid/Utils/Logger.h"
#include <string.h>
#include "cuvid/NvDecoder/NvDecoder.h"
#include <cuda_runtime.h>
#define DEC(handle) ((NvDecoder*)(handle->dec))
videoDecoderHandle videoDecoder_init(enum AVCodecID codec){
videoDecoderHandle handle = (videoDecoderHandle)malloc(sizeof(videoDecoder));
ck(cuInit(0));
handle->cuContext = nullptr;
createCudaContext(&(handle->cuContext), 0, 0);
handle->dec = (void*)(new NvDecoder(handle->cuContext, false, FFmpeg2NvCodecId(codec)));
return handle;
}
int videoDecoder_destroy(videoDecoderHandle handle){
delete(handle->dec);
cuCtxDestroy(handle->cuContext);
handle->cuContext = nullptr;
handle->dec = nullptr;
return 0;
}
void videoFrameList_destory(videoFrameList** list){
if(*list != NULL){
if((*list)->pFrames != NULL){
free((*list)->pFrames);
(*list)->pFrames = NULL;
}
free((*list));
*list = NULL;
}
}
videoFrameList* videoFrameList_init(int width, int height, int length){
videoFrameList* frameList = (videoFrameList*)malloc(sizeof(videoFrameList));
frameList->height = height;
frameList->width = width;
frameList->length = length;
frameList->perFrameSize = height*width*4;
frameList->pFrames = (uint8_t*)malloc(frameList->height * frameList->width * 4 * frameList->length);
return frameList;
}
videoFrameList* videoDecoder_decode(videoDecoderHandle handle, u_int8_t* in, size_t in_size, char*error){
int nFrameReturned;
int i;
int frameSize;
uint8_t *pVideo = NULL, *pFrame;
videoFrameList* frameList = NULL;
CUdeviceptr dpFrame = 0, nv12Frame = 0;
if(error!=NULL){
error[0] = NULL;
}
try{
nFrameReturned = DEC(handle)->Decode(in, in_size);
}catch(NVDECException e){
if(error != NULL){
strcpy(error, e.what());
}
return NULL;
}
for (i = 0; i < nFrameReturned; i++) {
pFrame = DEC(handle)->GetFrame();
frameSize = DEC(handle)->GetFrameSize();
if(i == 0){
frameList = videoFrameList_init(DEC(handle)->GetWidth(), DEC(handle)->GetHeight(), nFrameReturned);
ck(cuMemAlloc(&dpFrame, frameList->width * frameList->height * 4));
ck(cuMemAlloc(&nv12Frame, frameSize));
}
cudaMemcpy((void*)nv12Frame, pFrame, frameSize, cudaMemcpyHostToDevice);
Nv12ToColor32<BGRA32>((uint8_t*)nv12Frame, frameList->width, (uint8_t *)dpFrame, 4 * frameList->width, frameList->width, frameList->height);
int output_size = frameList->width * frameList->height * 4;
cudaMemcpy((void*)(frameList->pFrames+i*frameList->perFrameSize), (uint8_t*)dpFrame, output_size, cudaMemcpyDeviceToHost);
}
cuMemFree(dpFrame);
return frameList;
}