-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.c
320 lines (246 loc) · 9.88 KB
/
writer.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "restream.h"
#include "guide.h"
#include "playlist.h"
#include "infile.h"
#include "reader.h"
#include "writer.h"
int writer_init_video(ctx_restream *restrm, int indx) {
AVCodecContext *enc_ctx, *dec_ctx;
AVCodec *encoder;
AVStream *out_stream;
int retcd;
snprintf(restrm->function_name, 1024, "%s", "writer_init_video");
restrm->watchdog_playlist = av_gettime_relative();
dec_ctx = restrm->stream_ctx[indx].dec_ctx;
out_stream = avformat_new_stream(restrm->ofmt_ctx, NULL);
if (!out_stream) {
fprintf(stderr, "%s: Failed allocating output video stream\n", restrm->guide_info->guide_displayname);
return -1;
}
encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!encoder) {
fprintf(stderr, "%s: Could not find video encoder\n", restrm->guide_info->guide_displayname);
return -1;
}
enc_ctx = avcodec_alloc_context3(encoder);
if (!enc_ctx) {
fprintf(stderr, "%s: Could not allocate video encoder\n", restrm->guide_info->guide_displayname);
return -1;
}
(void)dec_ctx;
retcd = avcodec_parameters_from_context(out_stream->codecpar, dec_ctx);
if (retcd < 0) {
fprintf(stderr, "%s: Could not copy parms from video decoder\n"
,restrm->guide_info->guide_displayname);
return -1;
}
retcd = avcodec_parameters_to_context(enc_ctx, out_stream->codecpar);
if (retcd < 0) {
fprintf(stderr, "%s: Could not copy parms to video encoder\n"
,restrm->guide_info->guide_displayname);
return -1;
}
enc_ctx->width = dec_ctx->width;
enc_ctx->height = dec_ctx->height;
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->pix_fmt = dec_ctx->pix_fmt;
retcd = avcodec_open2(enc_ctx, encoder, NULL);
if (retcd < 0) {
fprintf(stderr, "%s: Could not open video encoder\n", restrm->guide_info->guide_displayname);
fprintf(stderr, "%s: %dx%d\n"
, restrm->guide_info->guide_displayname
, enc_ctx->width, enc_ctx->height);
return -1;
}
if (restrm->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
out_stream->time_base = enc_ctx->time_base;
retcd = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
if (retcd < 0) {
fprintf(stderr, "%s: Could not copy parms from decoder\n", restrm->guide_info->guide_displayname);
return -1;
}
restrm->stream_ctx[indx].enc_ctx = enc_ctx;
return 0;
}
int writer_init_audio(ctx_restream *restrm, int indx) {
AVStream *out_stream;
AVCodecContext *dec_ctx, *enc_ctx;
AVCodec *encoder;
int retcd;
snprintf(restrm->function_name, 1024, "%s", "writer_init_audio");
restrm->watchdog_playlist = av_gettime_relative();
out_stream = avformat_new_stream(restrm->ofmt_ctx, NULL);
if (!out_stream) {
fprintf(stderr, "%s: Failed allocating output stream\n", restrm->guide_info->guide_displayname);
return -1;
}
dec_ctx = restrm->stream_ctx[indx].dec_ctx;
encoder = avcodec_find_encoder(dec_ctx->codec_id);
if (!encoder) {
fprintf(stderr, "%s: Could not find audio encoder\n", restrm->guide_info->guide_displayname);
return -1;
}
enc_ctx = avcodec_alloc_context3(encoder);
if (!enc_ctx) {
fprintf(stderr, "%s: Could not allocate audio encoder\n", restrm->guide_info->guide_displayname);
return -1;
}
retcd = avcodec_parameters_from_context(out_stream->codecpar, dec_ctx);
if (retcd < 0) {
fprintf(stderr, "%s: Could not copy parms from audio decoder\n"
,restrm->guide_info->guide_displayname);
return -1;
}
retcd = avcodec_parameters_to_context(enc_ctx, out_stream->codecpar);
if (retcd < 0) {
fprintf(stderr, "%s: Could not copy parms to audio encoder\n"
,restrm->guide_info->guide_displayname);
return -1;
}
enc_ctx->sample_rate = dec_ctx->sample_rate;
enc_ctx->channel_layout = dec_ctx->channel_layout;
enc_ctx->channels = dec_ctx->channels;
enc_ctx->sample_fmt = dec_ctx->sample_fmt;
enc_ctx->time_base = dec_ctx->time_base;
/* Third parameter can be used to pass settings to encoder */
retcd = avcodec_open2(enc_ctx, encoder, NULL);
if (retcd < 0) {
fprintf(stderr, "%s: Could not open audio encoder\n", restrm->guide_info->guide_displayname);
return -1;
}
retcd = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
if (retcd < 0) {
fprintf(stderr, "%s: Failed to copy audio encoder parameters to stream\n", restrm->guide_info->guide_displayname);
return -1;
}
if (restrm->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
out_stream->time_base = enc_ctx->time_base;
restrm->stream_ctx[indx].enc_ctx = enc_ctx;
return 0;
}
void writer_close_encoder(ctx_restream *restrm) {
int indx;
snprintf(restrm->function_name, 1024, "%s", "writer_close_encoder");
restrm->watchdog_playlist = av_gettime_relative();
indx = 0;
while (indx < restrm->stream_count) {
if (restrm->stream_ctx[indx].enc_ctx != NULL) {
avcodec_free_context(&restrm->stream_ctx[indx].enc_ctx);
restrm->stream_ctx[indx].enc_ctx = NULL;
}
indx++;
}
avformat_free_context(restrm->ofmt_ctx);
restrm->ofmt_ctx = NULL;
}
void writer_close(ctx_restream *restrm) {
snprintf(restrm->function_name, 1024, "%s", "writer_close");
restrm->watchdog_playlist = av_gettime_relative();
reader_start(restrm);
if (restrm->ofmt_ctx) {
if (restrm->ofmt_ctx->pb != NULL) {
av_write_trailer(restrm->ofmt_ctx); /* Frees some memory*/
avio_closep(&restrm->ofmt_ctx->pb);
}
writer_close_encoder(restrm);
}
restrm->ofmt_ctx = NULL;
reader_close(restrm);
restrm->pipe_state = PIPE_IS_CLOSED;
}
int writer_init(ctx_restream *restrm) {
int retcd, indx;
AVOutputFormat *out_fmt;
snprintf(restrm->function_name, 1024, "%s", "writer_init");
restrm->watchdog_playlist = av_gettime_relative();
if (restrm->ifmt_ctx == NULL) {
fprintf(stderr, "%s: no input file provided\n", restrm->guide_info->guide_displayname);
return -1;
}
out_fmt = av_guess_format("matroska", NULL, NULL);
out_fmt->flags |= ~AVFMT_GLOBALHEADER;
if (!out_fmt) {
fprintf(stderr, "%s: av_guess_format failed\n", restrm->guide_info->guide_displayname);
return -1;
}
avformat_alloc_output_context2(&restrm->ofmt_ctx, out_fmt, NULL, restrm->out_filename);
if (!restrm->ofmt_ctx) {
fprintf(stderr, "%s: Could not create output context\n", restrm->guide_info->guide_displayname);
return -1;
}
restrm->ofmt_ctx->interrupt_callback.callback = restrm_interrupt;
restrm->ofmt_ctx->interrupt_callback.opaque = restrm;
restrm->ofmt = restrm->ofmt_ctx->oformat;
for (indx = 0; indx < restrm->ifmt_ctx->nb_streams; indx++) {
if (restrm->stream_ctx[indx].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
retcd = writer_init_video(restrm, indx);
if (retcd < 0)
return -1; /* Message already reported */
} else if (restrm->stream_ctx[indx].dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
retcd = writer_init_audio(restrm, indx);
if (retcd < 0)
return -1; /* Message already reported */
}
snprintf(restrm->function_name, 1024, "%s", "writer_init");
}
av_dump_format(restrm->ofmt_ctx, 0, restrm->out_filename, 1);
return 0;
}
int writer_init_open(ctx_restream *restrm) {
int retcd;
char errstr[128];
snprintf(restrm->function_name, 1024, "%s", "writer_init_open");
restrm->watchdog_playlist = av_gettime_relative();
retcd = avio_open(&restrm->ofmt_ctx->pb, restrm->out_filename, AVIO_FLAG_WRITE);
if (retcd < 0) {
fprintf(stderr, "%s: AVIO Open 1 Failed '%s'", restrm->guide_info->guide_displayname, restrm->out_filename);
return -1;
}
retcd = avformat_write_header(restrm->ofmt_ctx, NULL);
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
fprintf(stderr, "%s: avformat_write_header error. writer_init_open %s\n"
, restrm->guide_info->guide_displayname, errstr);
writer_close(restrm);
reader_start(restrm);
retcd = writer_init(restrm);
if (retcd < 0){
fprintf(stderr,"%s: Failed to open the new connection\n"
,restrm->guide_info->guide_displayname);
}
reader_close(restrm);
restrm->pipe_state = PIPE_NEEDS_RESET;
return -1;
}
return 0;
}
void writer_packet(ctx_restream *restrm) {
int retcd, indx;
int64_t base_tmp;
if (finish) return;
snprintf(restrm->function_name, 1024, "%s", "writer_packet");
restrm->watchdog_playlist = av_gettime_relative();
base_tmp = av_rescale(restrm->dts_base
, restrm->ofmt_ctx->streams[restrm->pkt.stream_index]->time_base.den
, 1000000);
if (restrm->pkt.pts != AV_NOPTS_VALUE) restrm->pkt.pts += base_tmp;
if (restrm->pkt.dts != AV_NOPTS_VALUE) restrm->pkt.dts += base_tmp;
//retcd = av_interleaved_write_frame(restrm->ofmt_ctx, &restrm->pkt);
retcd = av_write_frame(restrm->ofmt_ctx, &restrm->pkt);
/*
if ((av_gettime_relative() - restrm->connect_start) <1000000) {
for (indx=0; indx<=5; indx++){
if (restrm->pkt.pts != AV_NOPTS_VALUE) restrm->pkt.pts++;
if (restrm->pkt.dts != AV_NOPTS_VALUE) restrm->pkt.dts++;
retcd = av_write_frame(restrm->ofmt_ctx, &restrm->pkt);
}
}
*/
if (retcd < 0){
//fprintf(stderr, "%s: write packet: reset \n", restrm->guide_info->guide_displayname);
restrm->pipe_state = PIPE_NEEDS_RESET;
}
return;
}