-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCodedData.cpp
More file actions
108 lines (92 loc) · 2.36 KB
/
CodedData.cpp
File metadata and controls
108 lines (92 loc) · 2.36 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
#include "CodedData.hpp"
#include <cstring>
namespace avtranscoder
{
CodedData::CodedData()
: _avStream(NULL)
{
initAVPacket();
}
CodedData::CodedData(const size_t dataSize)
: _avStream(NULL)
{
const int err = av_new_packet(&_packet, dataSize);
if(err != 0)
{
LOG_ERROR("Unable to allocate the payload of a packet and initialize its fields with default values: " << getDescriptionFromErrorCode(err))
throw std::bad_alloc();
}
}
CodedData::CodedData(const AVPacket& avPacket)
: _avStream(NULL)
{
copyAVPacket(avPacket);
}
CodedData::CodedData(const CodedData& other)
{
copyAVPacket(other.getAVPacket());
_avStream = other.getAVStream();
}
CodedData& CodedData::operator=(const CodedData& other)
{
copyAVPacket(other.getAVPacket());
_avStream = other.getAVStream();
return *this;
}
CodedData::~CodedData()
{
av_packet_unref(&_packet);
}
void CodedData::resize(const size_t newSize)
{
if((int)newSize < _packet.size)
av_shrink_packet(&_packet, newSize);
else if((int)newSize > _packet.size)
av_grow_packet(&_packet, newSize - _packet.size);
}
void CodedData::refData(unsigned char* buffer, const size_t size)
{
_packet.data = buffer;
_packet.size = size;
}
void CodedData::copyData(unsigned char* buffer, const size_t size)
{
resize(size);
if(size != 0)
memcpy(_packet.data, buffer, _packet.size);
}
void CodedData::refData(CodedData& frame)
{
_packet.data = frame.getData();
_packet.size = frame.getSize();
}
void CodedData::clear()
{
av_packet_unref(&_packet);
initAVPacket();
}
void CodedData::assign(const size_t size, const int value)
{
resize(size);
memset(_packet.data, value, size);
}
void CodedData::initAVPacket()
{
_packet = *av_packet_alloc();
_packet.data = NULL;
_packet.size = 0;
}
void CodedData::copyAVPacket(const AVPacket& avPacket)
{
#if AVTRANSCODER_FFMPEG_DEPENDENCY && LIBAVCODEC_VERSION_MAJOR > 57
av_packet_ref(&_packet, &avPacket);
#elif AVTRANSCODER_FFMPEG_DEPENDENCY && LIBAVCODEC_VERSION_INT > AV_VERSION_INT(54, 56, 0)
// Need const_cast<AVCodec*> for libav versions from 54.56. to 55.56.
av_copy_packet(&_packet, const_cast<AVPacket*>(&avPacket));
#else
// @todo: we just care about data, not side properties of AVPacket
initAVPacket();
copyData(avPacket.data, avPacket.size);
#endif
}
}