VideoParser.cpp
4.14 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include "VideoParser.h"
#include "VideoDecoder.h"
#include "FrameQueue.h"
#include <cstring>
#include <cassert>
VideoParser::VideoParser(VideoDecoder *pVideoDecoder, FrameQueue *pFrameQueue): hParser_(0)
{
assert(0 != pFrameQueue);
oParserData_.pFrameQueue = pFrameQueue;
assert(0 != pVideoDecoder);
oParserData_.pVideoDecoder = pVideoDecoder;
CUVIDPARSERPARAMS oVideoParserParameters;
memset(&oVideoParserParameters, 0, sizeof(CUVIDPARSERPARAMS));
oVideoParserParameters.CodecType = pVideoDecoder->codec();
oVideoParserParameters.ulMaxNumDecodeSurfaces = pVideoDecoder->maxDecodeSurfaces();
oVideoParserParameters.ulMaxDisplayDelay = 1; // this flag is needed so the parser will push frames out to the decoder as quickly as it can
oVideoParserParameters.pUserData = &oParserData_;
oVideoParserParameters.pfnSequenceCallback = HandleVideoSequence; // Called before decoding frames and/or whenever there is a format change
oVideoParserParameters.pfnDecodePicture = HandlePictureDecode; // Called when a picture is ready to be decoded (decode order)
oVideoParserParameters.pfnDisplayPicture = HandlePictureDisplay; // Called whenever a picture is ready to be displayed (display order)
CUresult oResult = cuvidCreateVideoParser(&hParser_, &oVideoParserParameters);
assert(CUDA_SUCCESS == oResult);
}
VideoParser::~VideoParser()
{
if ( hParser_ )
{
cuvidDestroyVideoParser( hParser_ );
hParser_ = NULL;
}
return;
}
int VideoParser::ParseVideoData( CUVIDSOURCEDATAPACKET * pkt )
{
CUresult rlt = CUDA_SUCCESS;
rlt = cuvidParseVideoData( hParser_, pkt );
return 0;
}
int
CUDAAPI
VideoParser::HandleVideoSequence(void *pUserData, CUVIDEOFORMAT *pFormat)
{
VideoParserData *pParserData = reinterpret_cast<VideoParserData *>(pUserData);
if ((pFormat->codec != pParserData->pVideoDecoder->codec()) // codec-type
|| (pFormat->coded_width != pParserData->pVideoDecoder->frameWidth())
|| (pFormat->coded_height != pParserData->pVideoDecoder->frameHeight())
|| (pFormat->chroma_format != pParserData->pVideoDecoder->chromaFormat()))
{
// We don't deal with dynamic changes in video format
return 0;
}
return 1;
}
int
CUDAAPI
VideoParser::HandlePictureDecode(void *pUserData, CUVIDPICPARAMS *pPicParams)
{
VideoParserData *pParserData = reinterpret_cast<VideoParserData *>(pUserData);
bool bFrameAvailable = pParserData->pFrameQueue->waitUntilFrameAvailable(pPicParams->CurrPicIdx);
if (!bFrameAvailable)
return false;
pParserData->pVideoDecoder->decodePicture(pPicParams);
return true;
}
int
CUDAAPI
VideoParser::HandlePictureDisplay(void *pUserData, CUVIDPARSERDISPINFO *pPicParams)
{
// std::cout << *pPicParams << std::endl;
VideoParserData *pParserData = reinterpret_cast<VideoParserData *>(pUserData);
pParserData->pFrameQueue->enqueue(pPicParams);
return 1;
}
std::ostream &
operator << (std::ostream &rOutputStream, const CUVIDPARSERDISPINFO &rParserDisplayInfo)
{
rOutputStream << "Picture Index: " << rParserDisplayInfo.picture_index << "\n";
rOutputStream << "Progressive frame: ";
if (rParserDisplayInfo.progressive_frame)
rOutputStream << "true\n";
else
rOutputStream << "false\n";
rOutputStream << "Top field first: ";
if (rParserDisplayInfo.top_field_first)
rOutputStream << "true\n";
else
rOutputStream << "false\n";
rOutputStream << "Repeat first field: ";
if (rParserDisplayInfo.repeat_first_field)
rOutputStream << "true\n";
else
rOutputStream << "false\n";
rOutputStream << "Time stamp: " << rParserDisplayInfo.timestamp << "\n";
return rOutputStream;
}