Skip to content

Commit d613b66

Browse files
author
Clement Champetier
committed
frame: split Frame and CodedData
* CodedData: describes coded data. * Frame: describes decoded (raw) audio or video data. * Need to use AVFrame instead of AVPacket in avtranscoder Frame class.
1 parent 7493be6 commit d613b66

File tree

4 files changed

+111
-95
lines changed

4 files changed

+111
-95
lines changed

src/AvTranscoder/frame/Frame.cpp renamed to src/AvTranscoder/frame/CodedData.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
#include "Frame.hpp"
1+
#include "CodedData.hpp"
22

33
#include <cstring>
44

55
namespace avtranscoder
66
{
77

8-
Frame::Frame()
8+
CodedData::CodedData()
99
: _avStream(NULL)
1010
{
1111
initAVPacket();
1212
}
1313

14-
Frame::Frame(const size_t dataSize)
14+
CodedData::CodedData(const size_t dataSize)
1515
: _avStream(NULL)
1616
{
1717
av_new_packet(&_packet, dataSize);
1818
}
1919

20-
Frame::Frame(const AVPacket& avPacket)
20+
CodedData::CodedData(const AVPacket& avPacket)
2121
: _avStream(NULL)
2222
{
2323
copyAVPacket(avPacket);
2424
}
2525

26-
Frame::Frame(const Frame& other)
26+
CodedData::CodedData(const CodedData& other)
2727
{
2828
copyAVPacket(other.getAVPacket());
2929
_avStream = other.getAVStream();
3030
}
3131

32-
Frame& Frame::operator=(const Frame& other)
32+
CodedData& CodedData::operator=(const CodedData& other)
3333
{
3434
copyAVPacket(other.getAVPacket());
3535
_avStream = other.getAVStream();
3636
return *this;
3737
}
3838

39-
Frame::~Frame()
39+
CodedData::~CodedData()
4040
{
4141
av_free_packet(&_packet);
4242
}
4343

44-
void Frame::resize(const size_t newSize)
44+
void CodedData::resize(const size_t newSize)
4545
{
4646
if((int)newSize < _packet.size)
4747
av_shrink_packet(&_packet, newSize);
4848
else if((int)newSize > _packet.size)
4949
av_grow_packet(&_packet, newSize - _packet.size);
5050
}
5151

52-
void Frame::refData(unsigned char* buffer, const size_t size)
52+
void CodedData::refData(unsigned char* buffer, const size_t size)
5353
{
5454
_packet.data = buffer;
5555
_packet.size = size;
5656
}
5757

58-
void Frame::copyData(unsigned char* buffer, const size_t size)
58+
void CodedData::copyData(unsigned char* buffer, const size_t size)
5959
{
6060
resize(size);
6161
if(size != 0)
6262
memcpy(_packet.data, buffer, _packet.size);
6363
}
6464

65-
void Frame::refData(Frame& frame)
65+
void CodedData::refData(CodedData& frame)
6666
{
6767
_packet.data = frame.getData();
6868
_packet.size = frame.getSize();
6969
}
7070

71-
void Frame::clear()
71+
void CodedData::clear()
7272
{
7373
av_free_packet(&_packet);
7474
initAVPacket();
7575
}
7676

77-
void Frame::assign(const size_t size, const int value)
77+
void CodedData::assign(const size_t size, const int value)
7878
{
7979
resize(size);
8080
memset(_packet.data, value, size);
8181
}
8282

83-
void Frame::initAVPacket()
83+
void CodedData::initAVPacket()
8484
{
8585
av_init_packet(&_packet);
8686
_packet.data = NULL;
8787
_packet.size = 0;
8888
}
8989

90-
void Frame::copyAVPacket(const AVPacket& avPacket)
90+
void CodedData::copyAVPacket(const AVPacket& avPacket)
9191
{
9292
#if AVTRANSCODER_FFMPEG_DEPENDENCY && LIBAVCODEC_VERSION_INT > AV_VERSION_INT(54, 56, 0)
9393
// Need const_cast<AVCodec*> for libav versions from 54.56. to 55.56.

src/AvTranscoder/frame/CodedData.hpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef _AV_TRANSCODER_FRAME_CODEDDATA_HPP_
2+
#define _AV_TRANSCODER_FRAME_CODEDDATA_HPP_
3+
4+
#include <AvTranscoder/common.hpp>
5+
6+
extern "C" {
7+
#include <libavcodec/avcodec.h>
8+
}
9+
10+
struct AVStream;
11+
12+
namespace avtranscoder
13+
{
14+
15+
/**
16+
* @brief This class describes coded data.
17+
*/
18+
class AvExport CodedData
19+
{
20+
public:
21+
/// Create a frame with empty buffer data
22+
CodedData();
23+
24+
/// Create a frame with a the given buffer size
25+
CodedData(const size_t dataSize);
26+
27+
#ifndef SWIG
28+
/// Create a frame from the given AVPAcket (copy data of given packet)
29+
CodedData(const AVPacket& avPacket);
30+
#endif
31+
32+
/// Override copy constructor in order to copy AVPacket data
33+
CodedData(const CodedData& other);
34+
35+
/// Override operator = in order to copy AVPacket data
36+
CodedData& operator=(const CodedData& other);
37+
38+
/// Free buffer of data
39+
~CodedData();
40+
41+
void refAVStream(const AVStream& avStream) { _avStream = &avStream; }
42+
/// Resize data buffer
43+
void resize(const size_t newSize);
44+
45+
///@{
46+
/// Ref to external data buffer
47+
void refData(CodedData& frame);
48+
void refData(unsigned char* buffer, const size_t size);
49+
///@}
50+
51+
/// Copy external data buffer
52+
void copyData(unsigned char* buffer, const size_t size);
53+
54+
/**
55+
* @brief Resize the buffer with the given size, and copy the given value
56+
* @note Use this function to check if we can modify the buffer
57+
*/
58+
void assign(const size_t size, const int value);
59+
60+
/// Clear existing data and set size to 0
61+
void clear();
62+
63+
unsigned char* getData() { return _packet.data; }
64+
size_t getSize() const { return _packet.size; }
65+
66+
#ifndef SWIG
67+
/**
68+
* @return the AVStream which contains the packet
69+
* @note may be NULL in case of generated packets
70+
*/
71+
const AVStream* getAVStream() const { return _avStream; }
72+
AVPacket& getAVPacket() { return _packet; }
73+
const AVPacket& getAVPacket() const { return _packet; }
74+
const unsigned char* getData() const { return _packet.data; }
75+
#endif
76+
77+
private:
78+
void initAVPacket();
79+
void copyAVPacket(const AVPacket& avPacket);
80+
81+
private:
82+
AVPacket _packet;
83+
84+
// Stream which contains the packet
85+
const AVStream* _avStream; //< Has link (no ownership)
86+
};
87+
}
88+
89+
#endif

src/AvTranscoder/frame/Frame.hpp

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,16 @@
11
#ifndef _AV_TRANSCODER_FRAME_FRAME_HPP_
22
#define _AV_TRANSCODER_FRAME_FRAME_HPP_
33

4-
#include <AvTranscoder/common.hpp>
5-
6-
extern "C" {
7-
#include <libavcodec/avcodec.h>
8-
}
9-
10-
struct AVStream;
4+
#include "CodedData.hpp"
115

126
namespace avtranscoder
137
{
148

15-
class AvExport Frame
16-
{
17-
public:
18-
/// Create a frame with empty buffer data
19-
Frame();
20-
21-
/// Create a frame with a the given buffer size
22-
Frame(const size_t dataSize);
23-
24-
#ifndef SWIG
25-
/// Create a frame from the given AVPAcket (copy data of given packet)
26-
Frame(const AVPacket& avPacket);
27-
#endif
28-
29-
/// Override copy constructor in order to copy AVPacket data
30-
Frame(const Frame& other);
31-
32-
/// Override operator = in order to copy AVPacket data
33-
Frame& operator=(const Frame& other);
34-
35-
/// Free buffer of data
36-
~Frame();
37-
38-
void refAVStream(const AVStream& avStream) { _avStream = &avStream; }
39-
/// Resize data buffer
40-
void resize(const size_t newSize);
41-
42-
///@{
43-
/// Ref to external data buffer
44-
void refData(Frame& frame);
45-
void refData(unsigned char* buffer, const size_t size);
46-
///@}
47-
48-
/// Copy external data buffer
49-
void copyData(unsigned char* buffer, const size_t size);
50-
51-
/**
52-
* @brief Resize the buffer with the given size, and copy the given value
53-
* @note Use this function to check if we can modify the buffer
54-
*/
55-
void assign(const size_t size, const int value);
56-
57-
/// Clear existing data and set size to 0
58-
void clear();
59-
60-
unsigned char* getData() { return _packet.data; }
61-
size_t getSize() const { return _packet.size; }
62-
63-
#ifndef SWIG
64-
/**
65-
* @return the AVStream which contains the packet
66-
* @note may be NULL in case of generated packets
67-
*/
68-
const AVStream* getAVStream() const { return _avStream; }
69-
AVPacket& getAVPacket() { return _packet; }
70-
const AVPacket& getAVPacket() const { return _packet; }
71-
const unsigned char* getData() const { return _packet.data; }
72-
#endif
73-
74-
private:
75-
void initAVPacket();
76-
void copyAVPacket(const AVPacket& avPacket);
77-
78-
private:
79-
AVPacket _packet;
80-
81-
// Stream which contains the packet
82-
const AVStream* _avStream; //< Has link (no ownership)
83-
};
9+
/**
10+
* @brief This class describes decoded (raw) audio or video data.
11+
*/
12+
typedef CodedData Frame;
8413

85-
// Typedef to represent buffer of coded data.
86-
// Example 1: in case of image, no sense to get size if coded data.
87-
// Example 2: in case of audio, no sense to get number of channels if coded data.
88-
typedef Frame CodedData;
8914
}
9015

9116
#endif

src/AvTranscoder/frame/frame.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
%apply char * { unsigned char * };
22

33
%{
4+
#include <AvTranscoder/frame/CodedData.hpp>
45
#include <AvTranscoder/frame/Frame.hpp>
56
#include <AvTranscoder/frame/VideoFrame.hpp>
67
#include <AvTranscoder/frame/AudioFrame.hpp>
78
%}
89

10+
%include <AvTranscoder/frame/CodedData.hpp>
911
%include <AvTranscoder/frame/Frame.hpp>
1012
%include <AvTranscoder/frame/VideoFrame.hpp>
1113
%include <AvTranscoder/frame/AudioFrame.hpp>

0 commit comments

Comments
 (0)