-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathDecoder.cpp
More file actions
58 lines (47 loc) · 1.01 KB
/
Copy pathDecoder.cpp
File metadata and controls
58 lines (47 loc) · 1.01 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
#include "Decoder.h"
Decoder::Decoder()
{
packet = av_packet_alloc();
frame = av_frame_alloc();
}
int Decoder::open(AVCodecID codecId, int width, int height)
{
AVCodec *codec = avcodec_find_decoder(codecId);
codecContext = avcodec_alloc_context3(codec);
codecContext->width = width;
codecContext->height = height;
return avcodec_open2(codecContext, codec, NULL);
}
void Decoder::close()
{
avcodec_free_context(&codecContext);
av_frame_free(&frame);
av_packet_free(&packet);
}
AVFrame* Decoder::decodeData(uint8_t *data, int size)
{
// Fill packet
packet->size = size;
packet->data = data;
char buf[1024];
int ret;
//
ret = avcodec_send_packet(codecContext, packet);
if (ret < 0)
{
fprintf(stderr, "Error sending a packet for decoding\n");
exit(1);
}
//
while (ret >= 0)
{
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return NULL;
else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
exit(1);
}
return frame;
}
}