-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfile.c
243 lines (191 loc) · 8.42 KB
/
infile.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
#include "restream.h"
#include "guide.h"
#include "playlist.h"
#include "infile.h"
#include "reader.h"
#include "writer.h"
int infile_init(ctx_restream *restrm){
int retcd, stream_index, indx;
char errstr[128];
snprintf(restrm->function_name,1024,"%s","infile_init");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
//fprintf(stderr,"%s: Initializing : >%s< \n"
// ,restrm->guide_info->guide_displayname, restrm->in_filename);
guide_process(restrm);
snprintf(restrm->function_name,1024,"%s","infile_init 01");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
restrm->ifmt_ctx=NULL;
retcd = avformat_open_input(&restrm->ifmt_ctx, restrm->in_filename, NULL, NULL);
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
fprintf(stderr, "%s: Could not open input file '%s': %s \n"
,restrm->guide_info->guide_displayname, restrm->in_filename, errstr);
return -1;
}
snprintf(restrm->function_name,1024,"%s","infile_init 02");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
retcd = avformat_find_stream_info(restrm->ifmt_ctx, NULL);
if (retcd < 0) {
fprintf(stderr, "%s: Failed to retrieve input stream information\n"
,restrm->guide_info->guide_displayname);
return -1;
}
restrm->stream_index = 0;
restrm->stream_ctx = NULL;
restrm->stream_count = 0;
restrm->stream_count = restrm->ifmt_ctx->nb_streams;
//restrm->stream_ctx = av_mallocz_array(restrm->stream_count, sizeof(*restrm->stream_ctx));
restrm->stream_ctx = malloc(restrm->stream_count * sizeof(StreamContext));
if (!restrm->stream_ctx) {
fprintf(stderr, "%s: Failed to allocate space for streams\n"
,restrm->guide_info->guide_displayname);
return -1;
}
snprintf(restrm->function_name,1024,"%s","infile_init 03");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
stream_index = 0;
while (stream_index < restrm->ifmt_ctx->nb_streams){
restrm->stream_ctx[stream_index].dec_ctx = NULL;
restrm->stream_ctx[stream_index].enc_ctx = NULL;
stream_index++;
}
stream_index = 0;
while (stream_index < restrm->ifmt_ctx->nb_streams){
AVStream *stream = restrm->ifmt_ctx->streams[stream_index];
AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
AVCodecContext *codec_ctx;
if (!dec) {
fprintf(stderr, "%s: Failed to find decoder for stream #%d\n"
,restrm->guide_info->guide_displayname, stream_index);
return -1;
}
codec_ctx = avcodec_alloc_context3(dec);
if (!codec_ctx) {
fprintf(stderr, "%s: Failed to allocate decoder for stream #%d\n"
,restrm->guide_info->guide_displayname, stream_index);
return -1;
}
retcd = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
if (retcd < 0) {
fprintf(stderr, "%s: Failed to copy decoder parameters for stream #%d\n"
,restrm->guide_info->guide_displayname, stream_index);
return -1;
}
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){
codec_ctx->framerate = av_guess_frame_rate(restrm->ifmt_ctx, stream, NULL);
restrm->video_index = stream_index;
}
if (codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO){
restrm->audio_index = stream_index;
}
retcd = avcodec_open2(codec_ctx, dec, NULL);
if (retcd < 0) {
fprintf(stderr, "%s: Failed to open decoder for stream #%d\n"
,restrm->guide_info->guide_displayname, stream_index);
return -1;
}
}
restrm->stream_ctx[stream_index].dec_ctx = codec_ctx;
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
indx = 0;
restrm->dts_start = 0;
while (indx < 30) {
/* Read some pkts to get correct start time */
av_packet_unref(&restrm->pkt);
av_init_packet(&restrm->pkt);
restrm->pkt.data = NULL;
restrm->pkt.size = 0;
snprintf(restrm->function_name,1024,"%s","infile_init 04");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
retcd = av_read_frame(restrm->ifmt_ctx, &restrm->pkt);
if (retcd < 0){
fprintf(stderr, "%s: Failed to read first packets %d\n"
,restrm->guide_info->guide_displayname, stream_index);
return -1;
}
if (restrm->pkt.dts != AV_NOPTS_VALUE) {
restrm->dts_start = av_rescale(restrm->pkt.dts, 1000000
,restrm->ifmt_ctx->streams[restrm->pkt.stream_index]->time_base.den);
indx = 1000;
}
indx++;
}
restrm->time_start = av_gettime_relative();
restrm->dts_last = restrm->dts_start;
snprintf(restrm->function_name,1024,"%s","infile_init 05");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
}
stream_index++;
}
return 0;
}
void infile_close(ctx_restream *restrm){
int indx;
snprintf(restrm->function_name,1024,"%s","infile_close");
restrm->watchdog_playlist = av_gettime_relative();
if (restrm->ifmt_ctx == NULL) return;
indx = 0;
while (indx < restrm->stream_count){
if (restrm->stream_ctx[indx].dec_ctx != NULL){
avcodec_free_context(&restrm->stream_ctx[indx].dec_ctx);
restrm->stream_ctx[indx].dec_ctx = NULL;
}
indx++;
}
if (restrm->stream_ctx != NULL) free(restrm->stream_ctx);
avformat_close_input(&restrm->ifmt_ctx);
restrm->ifmt_ctx = NULL;
//fprintf(stderr, "%s: Input closed \n"
// ,restrm->guide_info->guide_displayname);
restrm->dts_base = restrm->dts_last + 1;
}
void infile_wait(ctx_restream *restrm){
int64_t tm_diff, dts_diff, tot_diff, dts;
snprintf(restrm->function_name,1024,"%s","infile_wait");
restrm->soft_restart = 1;
if (finish) return;
if (restrm->pkt.stream_index != restrm->video_index) return;
restrm->watchdog_playlist = av_gettime_relative();
if (restrm->pkt.dts != AV_NOPTS_VALUE) {
dts = av_rescale(restrm->pkt.dts, 1000000
,restrm->ifmt_ctx->streams[restrm->pkt.stream_index]->time_base.den);
if (dts < restrm->dts_last) return;
restrm->dts_last = dts;
tm_diff = av_gettime_relative() - restrm->connect_start;
if ((tm_diff <2000000) && (restrm->pipe_state == PIPE_IS_OPEN)) return;
/* How much time has really elapsed since the start of movie*/
tm_diff = av_gettime_relative() - restrm->time_start;
/* How much time the dts wants us to be at since the start of the movie */
dts_diff = dts - restrm->dts_start;
/* How much time we need to wait to get in sync*/
tot_diff = dts_diff - tm_diff;
if (tot_diff > 0){
if (tot_diff < 1000000){
snprintf(restrm->function_name,1024,"%s %ld","infile_wait",tot_diff);
SLEEP(0, tot_diff * 1000);
} else {
/*
if (restrm->pipe_state == PIPE_NEEDS_RESET ){
fprintf(stderr
,"%s: Excessive wait time "
" dts_last: %ld pkt.dts:%ld"
" dts_start: %ld dts_base:%ld"
" tm_diff: %ld dts_diff: %ld tot_diff: %ld\n"
, restrm->guide_info->guide_displayname
, restrm->dts_last, restrm->pkt.dts
, restrm->dts_start, restrm->dts_base
, tm_diff, dts_diff, tot_diff
);
}
*/
/* reset all our times to see if we can get in sync*/
restrm->time_start = av_gettime_relative();
restrm->dts_start = av_rescale(restrm->pkt.dts, 1000000
,restrm->ifmt_ctx->streams[restrm->pkt.stream_index]->time_base.den);
restrm->dts_last = restrm->dts_start;
}
}
}
}