-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.cpp
137 lines (104 loc) · 2.76 KB
/
codec.cpp
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
#include "compml_video.hpp"
#define DEBUG 0
#define IMAGE_SAVE 0
class Codec
{
private:
int ret;
public:
const AVCodec *codec;
AVCodecContext *context;
AVCodecParserContext *parser;
Codec(bool is_encoder, int bit_rate, int rc_buf)
{
if (is_encoder)
codec = avcodec_find_encoder_by_name("libx264");
else
{
codec = avcodec_find_decoder_by_name("h264");
parser = av_parser_init(codec->id);
}
context = avcodec_alloc_context3(codec);
if (is_encoder)
{
context->width = WAYMO_WIDTH;
context->height = WAYMO_HEIGHT;
context->time_base = (AVRational){1, 10};
context->framerate = (AVRational){10, 1};
context->gop_size = 10;
context->max_b_frames = 1;
context->pix_fmt = AV_PIX_FMT_YUV420P;
}
else {
context->width = WAYMO_WIDTH;
context->height = WAYMO_HEIGHT;
}
ret = avcodec_open2(context, codec, NULL);
}
~Codec()
{
avcodec_free_context(&context);
}
};
AVPacket *allocatePacket(AVPacket *packet)
{
packet = av_packet_alloc();
return packet;
}
AVFrame *allocateFrame(AVFrame *frame)
{
int ret;
frame = av_frame_alloc();
frame->format = AV_PIX_FMT_YUV420P;
frame->width = WAYMO_WIDTH;
frame->height = WAYMO_HEIGHT;
ret = av_frame_get_buffer(frame, 0);
return frame;
}
AVFrame *allocateFrame2(AVFrame *frame) {
int ret;
frame = av_frame_alloc();
frame->format = AV_PIX_FMT_YUV420P;
frame->width = WAYMO_WIDTH;
frame->height = WAYMO_HEIGHT;
ret = av_frame_get_buffer(frame, 0);
return frame;
}
void deallocateResources(AVPacket *packet, AVFrame *frame)
{
av_frame_free(&frame);
av_packet_free(&packet);
}
#ifdef IMAGE_SAVE
void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
char *filename)
{
FILE *f;
int i;
f = fopen(filename, "wb");
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
for (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
#endif
/* Implementation to save in lossless RGB */
void rgb_save(AVFrame* frame, char* filename) {
FILE* file;
struct SwsContext* sws_ctx;
AVFrame* frame_rgb = av_frame_alloc();
frame_rgb->width = frame->width;
frame_rgb->height = frame->height;
frame_rgb->format = AV_PIX_FMT_RGB24;
av_frame_get_buffer(frame_rgb, 0);
sws_ctx = sws_getContext(WAYMO_WIDTH, WAYMO_HEIGHT, AV_PIX_FMT_YUV444P, WAYMO_WIDTH, WAYMO_HEIGHT,
AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
sws_scale(sws_ctx, frame->data, frame->linesize, 0, WAYMO_HEIGHT, frame_rgb->data, frame_rgb->linesize);
file = fopen(filename, "wb");
fwrite(frame_rgb->data[0], 1, WAYMO_HEIGHT*WAYMO_WIDTH*3, file);
fclose(file);
}
void saveVideo(AVPacket *pkt, FILE *outfile)
{
fwrite(pkt->data, 1, pkt->size, outfile);
}