-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.c
More file actions
169 lines (133 loc) · 4.62 KB
/
encoder.c
File metadata and controls
169 lines (133 loc) · 4.62 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavutil/dict.h>
#include "common.h"
#include "encoder.h"
#include "pixfmt.h"
static bool eopen(encoder_t *this) {
avformat_alloc_output_context2(&(this->fmt_ctx), NULL, NULL, this->fname);
const AVCodec *enc_codec = avcodec_find_encoder_by_name(this->opt->lib);
if (!enc_codec) {
fprintf(stderr, "Fatal: Could not find encoder '%s'.\n", this->opt->lib);
fprintf(stderr, "Did you compile FFmpeg with this encoder enabled?\n");
return false;
}
this->stream = avformat_new_stream(this->fmt_ctx, enc_codec);
this->codec_ctx = avcodec_alloc_context3(enc_codec);
this->codec_ctx->width = (int) this->opt->width;
this->codec_ctx->height = (int) this->opt->height;
this->codec_ctx->time_base = (AVRational){1, this->opt->fps};
this->codec_ctx->framerate = (AVRational){this->opt->fps, 1};
init_pixfmt();
void *fmt = non_static_call(pixfmt, find, this->opt->fmt);
if (fmt != NULL) {
this->codec_ctx->pix_fmt = *(enum AVPixelFormat *)fmt;
} else {
fprintf(stderr, "pixel format not found '%s'\n", this->opt->fmt);
fprintf(stderr, "defaulting to YUV420P\n");
this->codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
}
AVDictionary *opts = NULL;
for (size_t t = 0; t < this->opt->n; ++t) {
av_dict_set(&opts, this->opt->K[t], this->opt->V[t], 0);
}
if (avcodec_open2(this->codec_ctx, enc_codec, &opts) < 0) {
fprintf(stderr, "could not open encoder\n");
return false;
}
av_dict_free(&opts);
avcodec_parameters_from_context(this->stream->codecpar, this->codec_ctx);
this->stream->time_base = this->codec_ctx->time_base;
if (avio_open(&(this->fmt_ctx->pb), this->fname, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "could not open output file '%s'\n", this->fname);
return false;
}
if (avformat_write_header(this->fmt_ctx, NULL) < 0) {
fprintf(stderr, "could not write header\n");
return false;
}
this->frame = av_frame_alloc();
this->frame->format = this->codec_ctx->pix_fmt;
this->frame->width = this->codec_ctx->width;
this->frame->height = this->codec_ctx->height;
av_frame_get_buffer(this->frame, 0);
this->pkt = av_packet_alloc();
this->pts = 0;
return true;
}
static bool eencode(encoder_t *this,
const uint8_t *buf) {
av_frame_make_writable(this->frame);
int W = this->opt->width;
int H = this->opt->height;
int Y_size = W * H;
int UV_size = (W / 2) * (H / 2);
for (int y = 0; y < H; y++)
memcpy(this->frame->data[0] + y * this->frame->linesize[0],
buf + y * W, W);
for (int y = 0; y < H / 2; y++)
memcpy(this->frame->data[1] + y * this->frame->linesize[1],
buf + Y_size + y * (W / 2), W / 2);
for (int y = 0; y < H / 2; y++)
memcpy(this->frame->data[2] + y * this->frame->linesize[2],
buf + Y_size + UV_size + y * (W / 2), W / 2);
this->frame->pts = this->pts++;
avcodec_send_frame(this->codec_ctx, this->frame);
while (avcodec_receive_packet(this->codec_ctx, this->pkt) == 0) {
av_packet_rescale_ts(this->pkt, this->codec_ctx->time_base, this->stream->time_base);
this->pkt->stream_index = this->stream->index;
av_interleaved_write_frame(this->fmt_ctx, this->pkt);
av_packet_unref(this->pkt);
}
return true;
}
static bool eflush(encoder_t *this) {
while (avcodec_receive_packet(this->codec_ctx, this->pkt) == 0) {
av_packet_rescale_ts(this->pkt, this->codec_ctx->time_base, this->stream->time_base);
this->pkt->stream_index = this->stream->index;
av_interleaved_write_frame(this->fmt_ctx, this->pkt);
av_packet_unref(this->pkt);
}
return true;
}
static bool eclose(encoder_t *this) {
avcodec_send_frame(this->codec_ctx, NULL);
non_static_call(this, flush);
av_write_trailer(this->fmt_ctx);
av_frame_free(&(this->frame));
av_packet_free(&(this->pkt));
avcodec_free_context(&(this->codec_ctx));
avio_closep(&(this->fmt_ctx->pb));
avformat_free_context(this->fmt_ctx);
return true;
}
encoder_t *new_encoder(
const char *fname,
encopts_t *opt) {
encoder_t *enc = (encoder_t *) malloc(sizeof(encoder_t));
if (enc == NULL) {
fprintf(stderr, "failed to malloc encoder_t\n");
return NULL;
}
enc->fname = fname;
enc->opt = opt;
enc->fmt_ctx = NULL;
enc->codec_ctx = NULL;
enc->frame = NULL;
enc->pkt = NULL;
enc->stream = NULL;
enc->open = eopen;
enc->encode = eencode;
enc->flush = eflush;
enc->close = eclose;
return enc;
}
void free_encoder(encoder_t *enc) {
if (enc == NULL) return;
if (enc->close) enc->close(enc);
free_pixfmt();
free(enc);
}