Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * This file is part of FFmpeg. | ||
3 | * | ||
4 | * FFmpeg is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * FFmpeg is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with FFmpeg; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | |||
19 | #include "libavutil/avassert.h" | ||
20 | #include "libavutil/dict.h" | ||
21 | #include "libavutil/error.h" | ||
22 | #include "libavutil/log.h" | ||
23 | #include "libavutil/pixdesc.h" | ||
24 | #include "libavutil/pixfmt.h" | ||
25 | #include "libavutil/timestamp.h" | ||
26 | |||
27 | #include "libavcodec/avcodec.h" | ||
28 | #include "libavcodec/codec.h" | ||
29 | |||
30 | #include "libavfilter/buffersrc.h" | ||
31 | |||
32 | #include "ffmpeg.h" | ||
33 | #include "ffmpeg_utils.h" | ||
34 | #include "thread_queue.h" | ||
35 | |||
36 | struct Decoder { | ||
37 | AVFrame *frame; | ||
38 | AVPacket *pkt; | ||
39 | |||
40 | enum AVPixelFormat hwaccel_pix_fmt; | ||
41 | |||
42 | // pts/estimated duration of the last decoded frame | ||
43 | // * in decoder timebase for video, | ||
44 | // * in last_frame_tb (may change during decoding) for audio | ||
45 | int64_t last_frame_pts; | ||
46 | int64_t last_frame_duration_est; | ||
47 | AVRational last_frame_tb; | ||
48 | int64_t last_filter_in_rescale_delta; | ||
49 | int last_frame_sample_rate; | ||
50 | |||
51 | /* previous decoded subtitles */ | ||
52 | AVFrame *sub_prev[2]; | ||
53 | AVFrame *sub_heartbeat; | ||
54 | |||
55 | pthread_t thread; | ||
56 | /** | ||
57 | * Queue for sending coded packets from the main thread to | ||
58 | * the decoder thread. | ||
59 | * | ||
60 | * An empty packet is sent to flush the decoder without terminating | ||
61 | * decoding. | ||
62 | */ | ||
63 | ThreadQueue *queue_in; | ||
64 | /** | ||
65 | * Queue for sending decoded frames from the decoder thread | ||
66 | * to the main thread. | ||
67 | * | ||
68 | * An empty frame is sent to signal that a single packet has been fully | ||
69 | * processed. | ||
70 | */ | ||
71 | ThreadQueue *queue_out; | ||
72 | }; | ||
73 | |||
74 | // data that is local to the decoder thread and not visible outside of it | ||
75 | typedef struct DecThreadContext { | ||
76 | AVFrame *frame; | ||
77 | AVPacket *pkt; | ||
78 | } DecThreadContext; | ||
79 | |||
80 | 12682 | static int dec_thread_stop(Decoder *d) | |
81 | { | ||
82 | void *ret; | ||
83 | |||
84 |
2/2✓ Branch 0 taken 6341 times.
✓ Branch 1 taken 6341 times.
|
12682 | if (!d->queue_in) |
85 | 6341 | return 0; | |
86 | |||
87 | 6341 | tq_send_finish(d->queue_in, 0); | |
88 | 6341 | tq_receive_finish(d->queue_out, 0); | |
89 | |||
90 | 6341 | pthread_join(d->thread, &ret); | |
91 | |||
92 | 6341 | tq_free(&d->queue_in); | |
93 | 6341 | tq_free(&d->queue_out); | |
94 | |||
95 | 6341 | return (intptr_t)ret; | |
96 | } | ||
97 | |||
98 | 7178 | void dec_free(Decoder **pdec) | |
99 | { | ||
100 | 7178 | Decoder *dec = *pdec; | |
101 | |||
102 |
2/2✓ Branch 0 taken 837 times.
✓ Branch 1 taken 6341 times.
|
7178 | if (!dec) |
103 | 837 | return; | |
104 | |||
105 | 6341 | dec_thread_stop(dec); | |
106 | |||
107 | 6341 | av_frame_free(&dec->frame); | |
108 | 6341 | av_packet_free(&dec->pkt); | |
109 | |||
110 |
2/2✓ Branch 0 taken 12682 times.
✓ Branch 1 taken 6341 times.
|
19023 | for (int i = 0; i < FF_ARRAY_ELEMS(dec->sub_prev); i++) |
111 | 12682 | av_frame_free(&dec->sub_prev[i]); | |
112 | 6341 | av_frame_free(&dec->sub_heartbeat); | |
113 | |||
114 | 6341 | av_freep(pdec); | |
115 | } | ||
116 | |||
117 | 6341 | static int dec_alloc(Decoder **pdec) | |
118 | { | ||
119 | Decoder *dec; | ||
120 | |||
121 | 6341 | *pdec = NULL; | |
122 | |||
123 | 6341 | dec = av_mallocz(sizeof(*dec)); | |
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!dec) |
125 | ✗ | return AVERROR(ENOMEM); | |
126 | |||
127 | 6341 | dec->frame = av_frame_alloc(); | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!dec->frame) |
129 | ✗ | goto fail; | |
130 | |||
131 | 6341 | dec->pkt = av_packet_alloc(); | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!dec->pkt) |
133 | ✗ | goto fail; | |
134 | |||
135 | 6341 | dec->last_filter_in_rescale_delta = AV_NOPTS_VALUE; | |
136 | 6341 | dec->last_frame_pts = AV_NOPTS_VALUE; | |
137 | 6341 | dec->last_frame_tb = (AVRational){ 1, 1 }; | |
138 | 6341 | dec->hwaccel_pix_fmt = AV_PIX_FMT_NONE; | |
139 | |||
140 | 6341 | *pdec = dec; | |
141 | |||
142 | 6341 | return 0; | |
143 | ✗ | fail: | |
144 | ✗ | dec_free(&dec); | |
145 | ✗ | return AVERROR(ENOMEM); | |
146 | } | ||
147 | |||
148 | 402823 | static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame) | |
149 | { | ||
150 | int i, ret; | ||
151 | |||
152 | av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */ | ||
153 |
2/2✓ Branch 0 taken 404655 times.
✓ Branch 1 taken 402823 times.
|
807478 | for (i = 0; i < ist->nb_filters; i++) { |
154 | 404655 | ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1); | |
155 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 404487 times.
|
404655 | if (ret == AVERROR_EOF) |
156 | 168 | ret = 0; /* ignore */ | |
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 404655 times.
|
404655 | if (ret < 0) { |
158 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
159 | ✗ | "Failed to inject frame into filter network: %s\n", av_err2str(ret)); | |
160 | ✗ | break; | |
161 | } | ||
162 | } | ||
163 | 402823 | return ret; | |
164 | } | ||
165 | |||
166 | 296586 | static AVRational audio_samplerate_update(void *logctx, Decoder *d, | |
167 | const AVFrame *frame) | ||
168 | { | ||
169 | 296586 | const int prev = d->last_frame_tb.den; | |
170 | 296586 | const int sr = frame->sample_rate; | |
171 | |||
172 | AVRational tb_new; | ||
173 | int64_t gcd; | ||
174 | |||
175 |
2/2✓ Branch 0 taken 295401 times.
✓ Branch 1 taken 1185 times.
|
296586 | if (frame->sample_rate == d->last_frame_sample_rate) |
176 | 295401 | goto finish; | |
177 | |||
178 | 1185 | gcd = av_gcd(prev, sr); | |
179 | |||
180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1185 times.
|
1185 | if (prev / gcd >= INT_MAX / sr) { |
181 | ✗ | av_log(logctx, AV_LOG_WARNING, | |
182 | "Audio timestamps cannot be represented exactly after " | ||
183 | "sample rate change: %d -> %d\n", prev, sr); | ||
184 | |||
185 | // LCM of 192000, 44100, allows to represent all common samplerates | ||
186 | ✗ | tb_new = (AVRational){ 1, 28224000 }; | |
187 | } else | ||
188 | 1185 | tb_new = (AVRational){ 1, prev / gcd * sr }; | |
189 | |||
190 | // keep the frame timebase if it is strictly better than | ||
191 | // the samplerate-defined one | ||
192 |
4/4✓ Branch 0 taken 1169 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 1061 times.
|
1185 | if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den && |
193 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 67 times.
|
108 | !(frame->time_base.den % tb_new.den)) |
194 | 41 | tb_new = frame->time_base; | |
195 | |||
196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1185 times.
|
1185 | if (d->last_frame_pts != AV_NOPTS_VALUE) |
197 | ✗ | d->last_frame_pts = av_rescale_q(d->last_frame_pts, | |
198 | d->last_frame_tb, tb_new); | ||
199 | 1185 | d->last_frame_duration_est = av_rescale_q(d->last_frame_duration_est, | |
200 | d->last_frame_tb, tb_new); | ||
201 | |||
202 | 1185 | d->last_frame_tb = tb_new; | |
203 | 1185 | d->last_frame_sample_rate = frame->sample_rate; | |
204 | |||
205 | 296586 | finish: | |
206 | 296586 | return d->last_frame_tb; | |
207 | } | ||
208 | |||
209 | 296586 | static void audio_ts_process(void *logctx, Decoder *d, AVFrame *frame) | |
210 | { | ||
211 | 296586 | AVRational tb_filter = (AVRational){1, frame->sample_rate}; | |
212 | AVRational tb; | ||
213 | int64_t pts_pred; | ||
214 | |||
215 | // on samplerate change, choose a new internal timebase for timestamp | ||
216 | // generation that can represent timestamps from all the samplerates | ||
217 | // seen so far | ||
218 | 296586 | tb = audio_samplerate_update(logctx, d, frame); | |
219 |
2/2✓ Branch 0 taken 295401 times.
✓ Branch 1 taken 1185 times.
|
296586 | pts_pred = d->last_frame_pts == AV_NOPTS_VALUE ? 0 : |
220 | 295401 | d->last_frame_pts + d->last_frame_duration_est; | |
221 | |||
222 |
2/2✓ Branch 0 taken 50472 times.
✓ Branch 1 taken 246114 times.
|
296586 | if (frame->pts == AV_NOPTS_VALUE) { |
223 | 50472 | frame->pts = pts_pred; | |
224 | 50472 | frame->time_base = tb; | |
225 |
2/2✓ Branch 0 taken 244942 times.
✓ Branch 1 taken 1172 times.
|
246114 | } else if (d->last_frame_pts != AV_NOPTS_VALUE && |
226 |
2/2✓ Branch 0 taken 604 times.
✓ Branch 1 taken 244338 times.
|
244942 | frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base, |
227 | AV_ROUND_UP)) { | ||
228 | // there was a gap in timestamps, reset conversion state | ||
229 | 604 | d->last_filter_in_rescale_delta = AV_NOPTS_VALUE; | |
230 | } | ||
231 | |||
232 | 296586 | frame->pts = av_rescale_delta(frame->time_base, frame->pts, | |
233 | tb, frame->nb_samples, | ||
234 | &d->last_filter_in_rescale_delta, tb); | ||
235 | |||
236 | 296586 | d->last_frame_pts = frame->pts; | |
237 | 296586 | d->last_frame_duration_est = av_rescale_q(frame->nb_samples, | |
238 | tb_filter, tb); | ||
239 | |||
240 | // finally convert to filtering timebase | ||
241 | 296586 | frame->pts = av_rescale_q(frame->pts, tb, tb_filter); | |
242 | 296586 | frame->duration = frame->nb_samples; | |
243 | 296586 | frame->time_base = tb_filter; | |
244 | 296586 | } | |
245 | |||
246 | 106237 | static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *frame) | |
247 | { | ||
248 | 106237 | const Decoder *d = ist->decoder; | |
249 | 106237 | const InputFile *ifile = input_files[ist->file_index]; | |
250 | 106237 | int64_t codec_duration = 0; | |
251 | |||
252 | // XXX lavf currently makes up frame durations when they are not provided by | ||
253 | // the container. As there is no way to reliably distinguish real container | ||
254 | // durations from the fake made-up ones, we use heuristics based on whether | ||
255 | // the container has timestamps. Eventually lavf should stop making up | ||
256 | // durations, then this should be simplified. | ||
257 | |||
258 | // prefer frame duration for containers with timestamps | ||
259 |
6/6✓ Branch 0 taken 104643 times.
✓ Branch 1 taken 1594 times.
✓ Branch 2 taken 32240 times.
✓ Branch 3 taken 72403 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 32233 times.
|
106237 | if (frame->duration > 0 && (!ifile->format_nots || ist->framerate.num)) |
260 | 72410 | return frame->duration; | |
261 | |||
262 |
4/4✓ Branch 0 taken 33746 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 5294 times.
✓ Branch 3 taken 28452 times.
|
33827 | if (ist->dec_ctx->framerate.den && ist->dec_ctx->framerate.num) { |
263 | 5294 | int fields = frame->repeat_pict + 2; | |
264 | 5294 | AVRational field_rate = av_mul_q(ist->dec_ctx->framerate, | |
265 | 5294 | (AVRational){ 2, 1 }); | |
266 | 5294 | codec_duration = av_rescale_q(fields, av_inv_q(field_rate), | |
267 | frame->time_base); | ||
268 | } | ||
269 | |||
270 | // prefer codec-layer duration for containers without timestamps | ||
271 |
4/4✓ Branch 0 taken 5294 times.
✓ Branch 1 taken 28533 times.
✓ Branch 2 taken 5273 times.
✓ Branch 3 taken 21 times.
|
33827 | if (codec_duration > 0 && ifile->format_nots) |
272 | 5273 | return codec_duration; | |
273 | |||
274 | // when timestamps are available, repeat last frame's actual duration | ||
275 | // (i.e. pts difference between this and last frame) | ||
276 |
3/4✓ Branch 0 taken 28554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28072 times.
✓ Branch 3 taken 482 times.
|
28554 | if (frame->pts != AV_NOPTS_VALUE && d->last_frame_pts != AV_NOPTS_VALUE && |
277 |
1/2✓ Branch 0 taken 28072 times.
✗ Branch 1 not taken.
|
28072 | frame->pts > d->last_frame_pts) |
278 | 28072 | return frame->pts - d->last_frame_pts; | |
279 | |||
280 | // try frame/codec duration | ||
281 |
2/2✓ Branch 0 taken 414 times.
✓ Branch 1 taken 68 times.
|
482 | if (frame->duration > 0) |
282 | 414 | return frame->duration; | |
283 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 67 times.
|
68 | if (codec_duration > 0) |
284 | 1 | return codec_duration; | |
285 | |||
286 | // try average framerate | ||
287 |
3/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
|
67 | if (ist->st->avg_frame_rate.num && ist->st->avg_frame_rate.den) { |
288 | 42 | int64_t d = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), | |
289 | frame->time_base); | ||
290 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (d > 0) |
291 | 42 | return d; | |
292 | } | ||
293 | |||
294 | // last resort is last frame's estimated duration, and 1 | ||
295 | 25 | return FFMAX(d->last_frame_duration_est, 1); | |
296 | } | ||
297 | |||
298 | 106237 | static int video_frame_process(InputStream *ist, AVFrame *frame) | |
299 | { | ||
300 | 106237 | Decoder *d = ist->decoder; | |
301 | |||
302 | // The following line may be required in some cases where there is no parser | ||
303 | // or the parser does not has_b_frames correctly | ||
304 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 106236 times.
|
106237 | if (ist->par->video_delay < ist->dec_ctx->has_b_frames) { |
305 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) { |
306 | 1 | ist->par->video_delay = ist->dec_ctx->has_b_frames; | |
307 | } else | ||
308 | ✗ | av_log(ist->dec_ctx, AV_LOG_WARNING, | |
309 | "video_delay is larger in decoder than demuxer %d > %d.\n" | ||
310 | "If you want to help, upload a sample " | ||
311 | "of this file to https://streams.videolan.org/upload/ " | ||
312 | "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n", | ||
313 | ✗ | ist->dec_ctx->has_b_frames, | |
314 | ✗ | ist->par->video_delay); | |
315 | } | ||
316 | |||
317 |
2/2✓ Branch 0 taken 106224 times.
✓ Branch 1 taken 13 times.
|
106237 | if (ist->dec_ctx->width != frame->width || |
318 |
2/2✓ Branch 0 taken 106222 times.
✓ Branch 1 taken 2 times.
|
106224 | ist->dec_ctx->height != frame->height || |
319 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 106218 times.
|
106222 | ist->dec_ctx->pix_fmt != frame->format) { |
320 | 19 | av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n", | |
321 | frame->width, | ||
322 | frame->height, | ||
323 | frame->format, | ||
324 | 19 | ist->dec_ctx->width, | |
325 | 19 | ist->dec_ctx->height, | |
326 | 19 | ist->dec_ctx->pix_fmt); | |
327 | } | ||
328 | |||
329 | #if FFMPEG_OPT_TOP | ||
330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106237 times.
|
106237 | if(ist->top_field_first>=0) { |
331 | ✗ | av_log(ist, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n"); | |
332 | ✗ | frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
333 | } | ||
334 | #endif | ||
335 | |||
336 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106237 times.
|
106237 | if (frame->format == d->hwaccel_pix_fmt) { |
337 | ✗ | int err = hwaccel_retrieve_data(ist->dec_ctx, frame); | |
338 | ✗ | if (err < 0) | |
339 | ✗ | return err; | |
340 | } | ||
341 | |||
342 | 106237 | frame->pts = frame->best_effort_timestamp; | |
343 | |||
344 | // forced fixed framerate | ||
345 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 106125 times.
|
106237 | if (ist->framerate.num) { |
346 | 112 | frame->pts = AV_NOPTS_VALUE; | |
347 | 112 | frame->duration = 1; | |
348 | 112 | frame->time_base = av_inv_q(ist->framerate); | |
349 | } | ||
350 | |||
351 | // no timestamp available - extrapolate from previous frame duration | ||
352 |
2/2✓ Branch 0 taken 32410 times.
✓ Branch 1 taken 73827 times.
|
106237 | if (frame->pts == AV_NOPTS_VALUE) |
353 |
2/2✓ Branch 0 taken 31892 times.
✓ Branch 1 taken 518 times.
|
64302 | frame->pts = d->last_frame_pts == AV_NOPTS_VALUE ? 0 : |
354 | 31892 | d->last_frame_pts + d->last_frame_duration_est; | |
355 | |||
356 | // update timestamp history | ||
357 | 106237 | d->last_frame_duration_est = video_duration_estimate(ist, frame); | |
358 | 106237 | d->last_frame_pts = frame->pts; | |
359 | 106237 | d->last_frame_tb = frame->time_base; | |
360 | |||
361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106237 times.
|
106237 | if (debug_ts) { |
362 | ✗ | av_log(ist, AV_LOG_INFO, | |
363 | "decoder -> pts:%s pts_time:%s " | ||
364 | "pkt_dts:%s pkt_dts_time:%s " | ||
365 | "duration:%s duration_time:%s " | ||
366 | "keyframe:%d frame_type:%d time_base:%d/%d\n", | ||
367 | ✗ | av_ts2str(frame->pts), | |
368 | ✗ | av_ts2timestr(frame->pts, &frame->time_base), | |
369 | ✗ | av_ts2str(frame->pkt_dts), | |
370 | ✗ | av_ts2timestr(frame->pkt_dts, &frame->time_base), | |
371 | ✗ | av_ts2str(frame->duration), | |
372 | ✗ | av_ts2timestr(frame->duration, &frame->time_base), | |
373 | ✗ | !!(frame->flags & AV_FRAME_FLAG_KEY), frame->pict_type, | |
374 | frame->time_base.num, frame->time_base.den); | ||
375 | } | ||
376 | |||
377 |
2/2✓ Branch 0 taken 5420 times.
✓ Branch 1 taken 100817 times.
|
106237 | if (ist->st->sample_aspect_ratio.num) |
378 | 5420 | frame->sample_aspect_ratio = ist->st->sample_aspect_ratio; | |
379 | |||
380 | 106237 | return 0; | |
381 | } | ||
382 | |||
383 | 40 | static void sub2video_flush(InputStream *ist) | |
384 | { | ||
385 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
|
44 | for (int i = 0; i < ist->nb_filters; i++) { |
386 | 4 | int ret = ifilter_sub2video(ist->filters[i], NULL); | |
387 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (ret != AVERROR_EOF && ret < 0) |
388 | ✗ | av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n"); | |
389 | } | ||
390 | 40 | } | |
391 | |||
392 | 837 | static int process_subtitle(InputStream *ist, AVFrame *frame) | |
393 | { | ||
394 | 837 | Decoder *d = ist->decoder; | |
395 | 837 | const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data; | |
396 | 837 | int ret = 0; | |
397 | |||
398 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 817 times.
|
837 | if (ist->fix_sub_duration) { |
399 | 40 | AVSubtitle *sub_prev = d->sub_prev[0]->buf[0] ? | |
400 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL; |
401 | 20 | int end = 1; | |
402 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | if (sub_prev) { |
403 | 18 | end = av_rescale(subtitle->pts - sub_prev->pts, | |
404 | 1000, AV_TIME_BASE); | ||
405 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (end < sub_prev->end_display_time) { |
406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | av_log(NULL, AV_LOG_DEBUG, |
407 | "Subtitle duration reduced from %"PRId32" to %d%s\n", | ||
408 | sub_prev->end_display_time, end, | ||
409 | end <= 0 ? ", dropping it" : ""); | ||
410 | 18 | sub_prev->end_display_time = end; | |
411 | } | ||
412 | } | ||
413 | |||
414 | 20 | av_frame_unref(d->sub_prev[1]); | |
415 | 20 | av_frame_move_ref(d->sub_prev[1], frame); | |
416 | |||
417 | 20 | frame = d->sub_prev[0]; | |
418 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL; |
419 | |||
420 | 20 | FFSWAP(AVFrame*, d->sub_prev[0], d->sub_prev[1]); | |
421 | |||
422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (end <= 0) |
423 | ✗ | return 0; | |
424 | } | ||
425 | |||
426 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 835 times.
|
837 | if (!subtitle) |
427 | 2 | return 0; | |
428 | |||
429 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 835 times.
|
923 | for (int i = 0; i < ist->nb_filters; i++) { |
430 | 88 | ret = ifilter_sub2video(ist->filters[i], frame); | |
431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (ret < 0) { |
432 | ✗ | av_log(ist, AV_LOG_ERROR, "Error sending a subtitle for filtering: %s\n", | |
433 | ✗ | av_err2str(ret)); | |
434 | ✗ | return ret; | |
435 | } | ||
436 | } | ||
437 | |||
438 | 835 | subtitle = (AVSubtitle*)frame->buf[0]->data; | |
439 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 826 times.
|
835 | if (!subtitle->num_rects) |
440 | 9 | return 0; | |
441 | |||
442 |
2/2✓ Branch 0 taken 792 times.
✓ Branch 1 taken 826 times.
|
1618 | for (int oidx = 0; oidx < ist->nb_outputs; oidx++) { |
443 | 792 | OutputStream *ost = ist->outputs[oidx]; | |
444 |
2/4✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 792 times.
|
792 | if (!ost->enc || ost->type != AVMEDIA_TYPE_SUBTITLE) |
445 | ✗ | continue; | |
446 | |||
447 | 792 | ret = enc_subtitle(output_files[ost->file_index], ost, subtitle); | |
448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
|
792 | if (ret < 0) |
449 | ✗ | return ret; | |
450 | } | ||
451 | |||
452 | 826 | return 0; | |
453 | } | ||
454 | |||
455 | 5 | int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) | |
456 | { | ||
457 | 5 | Decoder *d = ist->decoder; | |
458 | 5 | int ret = AVERROR_BUG; | |
459 | 10 | AVSubtitle *prev_subtitle = d->sub_prev[0]->buf[0] ? | |
460 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL; |
461 | AVSubtitle *subtitle; | ||
462 | |||
463 |
3/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
5 | if (!ist->fix_sub_duration || !prev_subtitle || |
464 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts) |
465 | 1 | return 0; | |
466 | |||
467 | 4 | av_frame_unref(d->sub_heartbeat); | |
468 | 4 | ret = subtitle_wrap_frame(d->sub_heartbeat, prev_subtitle, 1); | |
469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
470 | ✗ | return ret; | |
471 | |||
472 | 4 | subtitle = (AVSubtitle*)d->sub_heartbeat->buf[0]->data; | |
473 | 4 | subtitle->pts = signal_pts; | |
474 | |||
475 | 4 | return process_subtitle(ist, d->sub_heartbeat); | |
476 | } | ||
477 | |||
478 | 1702 | static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, | |
479 | AVFrame *frame) | ||
480 | { | ||
481 | 1702 | Decoder *d = ist->decoder; | |
482 | 1702 | AVPacket *flush_pkt = NULL; | |
483 | AVSubtitle subtitle; | ||
484 | int got_output; | ||
485 | int ret; | ||
486 | |||
487 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1662 times.
|
1702 | if (!pkt) { |
488 | 40 | flush_pkt = av_packet_alloc(); | |
489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (!flush_pkt) |
490 | ✗ | return AVERROR(ENOMEM); | |
491 | } | ||
492 | |||
493 |
2/2✓ Branch 0 taken 1662 times.
✓ Branch 1 taken 40 times.
|
1702 | ret = avcodec_decode_subtitle2(ist->dec_ctx, &subtitle, &got_output, |
494 | pkt ? pkt : flush_pkt); | ||
495 | 1702 | av_packet_free(&flush_pkt); | |
496 | |||
497 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1623 times.
|
1702 | if (ret < 0) { |
498 | 79 | av_log(ist, AV_LOG_ERROR, "Error decoding subtitles: %s\n", | |
499 | 79 | av_err2str(ret)); | |
500 | 79 | ist->decode_errors++; | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
|
79 | return exit_on_error ? ret : 0; |
502 | } | ||
503 | |||
504 |
2/2✓ Branch 0 taken 790 times.
✓ Branch 1 taken 833 times.
|
1623 | if (!got_output) |
505 |
2/2✓ Branch 0 taken 750 times.
✓ Branch 1 taken 40 times.
|
790 | return pkt ? 0 : AVERROR_EOF; |
506 | |||
507 | 833 | ist->frames_decoded++; | |
508 | |||
509 | // XXX the queue for transferring data back to the main thread runs | ||
510 | // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that | ||
511 | // inside the frame | ||
512 | // eventually, subtitles should be switched to use AVFrames natively | ||
513 | 833 | ret = subtitle_wrap_frame(frame, &subtitle, 0); | |
514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
|
833 | if (ret < 0) { |
515 | ✗ | avsubtitle_free(&subtitle); | |
516 | ✗ | return ret; | |
517 | } | ||
518 | |||
519 | 833 | frame->width = ist->dec_ctx->width; | |
520 | 833 | frame->height = ist->dec_ctx->height; | |
521 | |||
522 | 833 | ret = tq_send(d->queue_out, 0, frame); | |
523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
|
833 | if (ret < 0) |
524 | ✗ | av_frame_unref(frame); | |
525 | |||
526 | 833 | return ret; | |
527 | } | ||
528 | |||
529 | 6301 | static int send_filter_eof(InputStream *ist) | |
530 | { | ||
531 | 6301 | Decoder *d = ist->decoder; | |
532 | int i, ret; | ||
533 | |||
534 |
2/2✓ Branch 0 taken 6317 times.
✓ Branch 1 taken 6301 times.
|
12618 | for (i = 0; i < ist->nb_filters; i++) { |
535 |
2/2✓ Branch 0 taken 6314 times.
✓ Branch 1 taken 3 times.
|
6317 | int64_t end_pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE : |
536 | 6314 | d->last_frame_pts + d->last_frame_duration_est; | |
537 | 6317 | ret = ifilter_send_eof(ist->filters[i], end_pts, d->last_frame_tb); | |
538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6317 times.
|
6317 | if (ret < 0) |
539 | ✗ | return ret; | |
540 | } | ||
541 | 6301 | return 0; | |
542 | } | ||
543 | |||
544 | 388630 | static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame) | |
545 | { | ||
546 | 388630 | const InputFile *ifile = input_files[ist->file_index]; | |
547 | 388630 | Decoder *d = ist->decoder; | |
548 | 388630 | AVCodecContext *dec = ist->dec_ctx; | |
549 | 388630 | const char *type_desc = av_get_media_type_string(dec->codec_type); | |
550 | int ret; | ||
551 | |||
552 |
2/2✓ Branch 0 taken 1702 times.
✓ Branch 1 taken 386928 times.
|
388630 | if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE) |
553 | 1702 | return transcode_subtitles(ist, pkt, frame); | |
554 | |||
555 | // With fate-indeo3-2, we're getting 0-sized packets before EOF for some | ||
556 | // reason. This seems like a semi-critical bug. Don't trigger EOF, and | ||
557 | // skip the packet. | ||
558 |
4/4✓ Branch 0 taken 380621 times.
✓ Branch 1 taken 6307 times.
✓ Branch 2 taken 1480 times.
✓ Branch 3 taken 379141 times.
|
386928 | if (pkt && pkt->size == 0) |
559 | 1480 | return 0; | |
560 | |||
561 |
4/4✓ Branch 0 taken 379141 times.
✓ Branch 1 taken 6307 times.
✓ Branch 2 taken 57290 times.
✓ Branch 3 taken 321851 times.
|
385448 | if (pkt && ifile->format_nots) { |
562 | 57290 | pkt->pts = AV_NOPTS_VALUE; | |
563 | 57290 | pkt->dts = AV_NOPTS_VALUE; | |
564 | } | ||
565 | |||
566 | 385448 | ret = avcodec_send_packet(dec, pkt); | |
567 |
3/6✓ Branch 0 taken 385059 times.
✓ Branch 1 taken 389 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 389 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
385448 | if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) { |
568 | // In particular, we don't expect AVERROR(EAGAIN), because we read all | ||
569 | // decoded frames with avcodec_receive_frame() until done. | ||
570 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 389 times.
|
389 | if (ret == AVERROR(EAGAIN)) { |
571 | ✗ | av_log(ist, AV_LOG_FATAL, "A decoder returned an unexpected error code. " | |
572 | "This is a bug, please report it.\n"); | ||
573 | ✗ | return AVERROR_BUG; | |
574 | } | ||
575 |
1/2✓ Branch 0 taken 389 times.
✗ Branch 1 not taken.
|
389 | av_log(ist, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n", |
576 | 389 | pkt ? "packet" : "EOF", av_err2str(ret)); | |
577 | |||
578 |
1/2✓ Branch 0 taken 389 times.
✗ Branch 1 not taken.
|
389 | if (ret != AVERROR_EOF) { |
579 | 389 | ist->decode_errors++; | |
580 |
1/2✓ Branch 0 taken 389 times.
✗ Branch 1 not taken.
|
389 | if (!exit_on_error) |
581 | 389 | ret = 0; | |
582 | } | ||
583 | |||
584 | 389 | return ret; | |
585 | } | ||
586 | |||
587 | 402830 | while (1) { | |
588 | FrameData *fd; | ||
589 | |||
590 | 787889 | av_frame_unref(frame); | |
591 | |||
592 | 787889 | update_benchmark(NULL); | |
593 | 787889 | ret = avcodec_receive_frame(dec, frame); | |
594 | 787889 | update_benchmark("decode_%s %d.%d", type_desc, | |
595 | ist->file_index, ist->index); | ||
596 | |||
597 |
2/2✓ Branch 0 taken 378752 times.
✓ Branch 1 taken 409137 times.
|
787889 | if (ret == AVERROR(EAGAIN)) { |
598 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 378752 times.
|
378752 | av_assert0(pkt); // should never happen during flushing |
599 | 378752 | return 0; | |
600 |
2/2✓ Branch 0 taken 6307 times.
✓ Branch 1 taken 402830 times.
|
409137 | } else if (ret == AVERROR_EOF) { |
601 | 6307 | return ret; | |
602 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 402823 times.
|
402830 | } else if (ret < 0) { |
603 | 7 | av_log(ist, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret)); | |
604 | 7 | ist->decode_errors++; | |
605 | |||
606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (exit_on_error) |
607 | ✗ | return ret; | |
608 | |||
609 | 7 | continue; | |
610 | } | ||
611 | |||
612 |
3/4✓ Branch 0 taken 402787 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 402787 times.
|
402823 | if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) { |
613 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | av_log(ist, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING, |
614 | "corrupt decoded frame\n"); | ||
615 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (exit_on_error) |
616 | ✗ | return AVERROR_INVALIDDATA; | |
617 | } | ||
618 | |||
619 | |||
620 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 402823 times.
|
402823 | av_assert0(!frame->opaque_ref); |
621 | 402823 | fd = frame_data(frame); | |
622 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 402823 times.
|
402823 | if (!fd) { |
623 | ✗ | av_frame_unref(frame); | |
624 | ✗ | return AVERROR(ENOMEM); | |
625 | } | ||
626 | 402823 | fd->dec.pts = frame->pts; | |
627 | 402823 | fd->dec.tb = dec->pkt_timebase; | |
628 | 402823 | fd->dec.frame_num = dec->frame_num - 1; | |
629 | 402823 | fd->bits_per_raw_sample = dec->bits_per_raw_sample; | |
630 | |||
631 | 402823 | frame->time_base = dec->pkt_timebase; | |
632 | |||
633 |
2/2✓ Branch 0 taken 296586 times.
✓ Branch 1 taken 106237 times.
|
402823 | if (dec->codec_type == AVMEDIA_TYPE_AUDIO) { |
634 | 296586 | ist->samples_decoded += frame->nb_samples; | |
635 | |||
636 | 296586 | audio_ts_process(ist, ist->decoder, frame); | |
637 | } else { | ||
638 | 106237 | ret = video_frame_process(ist, frame); | |
639 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106237 times.
|
106237 | if (ret < 0) { |
640 | ✗ | av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded " | |
641 | "data for stream #%d:%d\n", ist->file_index, ist->index); | ||
642 | ✗ | return ret; | |
643 | } | ||
644 | } | ||
645 | |||
646 | 402823 | ist->frames_decoded++; | |
647 | |||
648 | 402823 | ret = tq_send(d->queue_out, 0, frame); | |
649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 402823 times.
|
402823 | if (ret < 0) |
650 | ✗ | return ret; | |
651 | } | ||
652 | } | ||
653 | |||
654 | 6341 | static void dec_thread_set_name(const InputStream *ist) | |
655 | { | ||
656 | char name[16]; | ||
657 | 6341 | snprintf(name, sizeof(name), "dec%d:%d:%s", ist->file_index, ist->index, | |
658 | 6341 | ist->dec_ctx->codec->name); | |
659 | 6341 | ff_thread_setname(name); | |
660 | 6341 | } | |
661 | |||
662 | 6341 | static void dec_thread_uninit(DecThreadContext *dt) | |
663 | { | ||
664 | 6341 | av_packet_free(&dt->pkt); | |
665 | 6341 | av_frame_free(&dt->frame); | |
666 | |||
667 | 6341 | memset(dt, 0, sizeof(*dt)); | |
668 | 6341 | } | |
669 | |||
670 | 6341 | static int dec_thread_init(DecThreadContext *dt) | |
671 | { | ||
672 | 6341 | memset(dt, 0, sizeof(*dt)); | |
673 | |||
674 | 6341 | dt->frame = av_frame_alloc(); | |
675 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!dt->frame) |
676 | ✗ | goto fail; | |
677 | |||
678 | 6341 | dt->pkt = av_packet_alloc(); | |
679 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!dt->pkt) |
680 | ✗ | goto fail; | |
681 | |||
682 | 6341 | return 0; | |
683 | |||
684 | ✗ | fail: | |
685 | ✗ | dec_thread_uninit(dt); | |
686 | ✗ | return AVERROR(ENOMEM); | |
687 | } | ||
688 | |||
689 | 6341 | static void *decoder_thread(void *arg) | |
690 | { | ||
691 | 6341 | InputStream *ist = arg; | |
692 | 6341 | InputFile *ifile = input_files[ist->file_index]; | |
693 | 6341 | Decoder *d = ist->decoder; | |
694 | DecThreadContext dt; | ||
695 | 6341 | int ret = 0, input_status = 0; | |
696 | |||
697 | 6341 | ret = dec_thread_init(&dt); | |
698 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret < 0) |
699 | ✗ | goto finish; | |
700 | |||
701 | 6341 | dec_thread_set_name(ist); | |
702 | |||
703 |
1/2✓ Branch 0 taken 388630 times.
✗ Branch 1 not taken.
|
388630 | while (!input_status) { |
704 | int dummy, flush_buffers; | ||
705 | |||
706 | 388630 | input_status = tq_receive(d->queue_in, &dummy, dt.pkt); | |
707 |
4/4✓ Branch 0 taken 382289 times.
✓ Branch 1 taken 6341 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 382283 times.
|
388630 | flush_buffers = input_status >= 0 && !dt.pkt->buf; |
708 |
2/2✓ Branch 0 taken 6347 times.
✓ Branch 1 taken 382283 times.
|
388630 | if (!dt.pkt->buf) |
709 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6341 times.
|
6347 | av_log(ist, AV_LOG_VERBOSE, "Decoder thread received %s packet\n", |
710 | flush_buffers ? "flush" : "EOF"); | ||
711 | |||
712 |
2/2✓ Branch 0 taken 382283 times.
✓ Branch 1 taken 6347 times.
|
388630 | ret = packet_decode(ist, dt.pkt->buf ? dt.pkt : NULL, dt.frame); |
713 | |||
714 | 388630 | av_packet_unref(dt.pkt); | |
715 | 388630 | av_frame_unref(dt.frame); | |
716 | |||
717 |
2/2✓ Branch 0 taken 6347 times.
✓ Branch 1 taken 382283 times.
|
388630 | if (ret == AVERROR_EOF) { |
718 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6341 times.
|
6347 | av_log(ist, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n", |
719 | flush_buffers ? "resetting" : "finishing"); | ||
720 | |||
721 |
2/2✓ Branch 0 taken 6341 times.
✓ Branch 1 taken 6 times.
|
6347 | if (!flush_buffers) |
722 | 6341 | break; | |
723 | |||
724 | /* report last frame duration to the demuxer thread */ | ||
725 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (ist->dec->type == AVMEDIA_TYPE_AUDIO) { |
726 | 3 | Timestamp ts = { .ts = d->last_frame_pts + d->last_frame_duration_est, | |
727 | .tb = d->last_frame_tb }; | ||
728 | 3 | av_thread_message_queue_send(ifile->audio_ts_queue, &ts, 0); | |
729 | } | ||
730 | |||
731 | 6 | avcodec_flush_buffers(ist->dec_ctx); | |
732 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 382283 times.
|
382283 | } else if (ret < 0) { |
733 | ✗ | av_log(ist, AV_LOG_ERROR, "Error processing packet in decoder: %s\n", | |
734 | ✗ | av_err2str(ret)); | |
735 | ✗ | break; | |
736 | } | ||
737 | |||
738 | // signal to the consumer thread that the entire packet was processed | ||
739 | 382289 | ret = tq_send(d->queue_out, 0, dt.frame); | |
740 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 382289 times.
|
382289 | if (ret < 0) { |
741 | ✗ | if (ret != AVERROR_EOF) | |
742 | ✗ | av_log(ist, AV_LOG_ERROR, "Error communicating with the main thread\n"); | |
743 | ✗ | break; | |
744 | } | ||
745 | } | ||
746 | |||
747 | // EOF is normal thread termination | ||
748 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret == AVERROR_EOF) |
749 | 6341 | ret = 0; | |
750 | |||
751 | ✗ | finish: | |
752 | 6341 | tq_receive_finish(d->queue_in, 0); | |
753 | 6341 | tq_send_finish (d->queue_out, 0); | |
754 | |||
755 | // make sure the demuxer does not get stuck waiting for audio durations | ||
756 | // that will never arrive | ||
757 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6339 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
6341 | if (ifile->audio_ts_queue && ist->dec->type == AVMEDIA_TYPE_AUDIO) |
758 | 1 | av_thread_message_queue_set_err_recv(ifile->audio_ts_queue, AVERROR_EOF); | |
759 | |||
760 | 6341 | dec_thread_uninit(&dt); | |
761 | |||
762 | 6341 | av_log(ist, AV_LOG_VERBOSE, "Terminating decoder thread\n"); | |
763 | |||
764 | 6341 | return (void*)(intptr_t)ret; | |
765 | } | ||
766 | |||
767 | 388630 | int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof) | |
768 | { | ||
769 | 388630 | Decoder *d = ist->decoder; | |
770 | 388630 | int ret = 0, thread_ret; | |
771 | |||
772 | // thread already joined | ||
773 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 388630 times.
|
388630 | if (!d->queue_in) |
774 | ✗ | return AVERROR_EOF; | |
775 | |||
776 | // send the packet/flush request/EOF to the decoder thread | ||
777 |
4/4✓ Branch 0 taken 6347 times.
✓ Branch 1 taken 382283 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6341 times.
|
388630 | if (pkt || no_eof) { |
778 | 382289 | av_packet_unref(d->pkt); | |
779 | |||
780 |
2/2✓ Branch 0 taken 382283 times.
✓ Branch 1 taken 6 times.
|
382289 | if (pkt) { |
781 | 382283 | ret = av_packet_ref(d->pkt, pkt); | |
782 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 382283 times.
|
382283 | if (ret < 0) |
783 | ✗ | goto finish; | |
784 | } | ||
785 | |||
786 | 382289 | ret = tq_send(d->queue_in, 0, d->pkt); | |
787 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 382289 times.
|
382289 | if (ret < 0) |
788 | ✗ | goto finish; | |
789 | } else | ||
790 | 6341 | tq_send_finish(d->queue_in, 0); | |
791 | |||
792 | // retrieve all decoded data for the packet | ||
793 | 403656 | while (1) { | |
794 | int dummy; | ||
795 | |||
796 | 792286 | ret = tq_receive(d->queue_out, &dummy, d->frame); | |
797 |
2/2✓ Branch 0 taken 6341 times.
✓ Branch 1 taken 785945 times.
|
792286 | if (ret < 0) |
798 | 6341 | goto finish; | |
799 | |||
800 | // packet fully processed | ||
801 |
2/2✓ Branch 0 taken 382289 times.
✓ Branch 1 taken 403656 times.
|
785945 | if (!d->frame->buf[0]) |
802 | 382289 | return 0; | |
803 | |||
804 | // process the decoded frame | ||
805 |
2/2✓ Branch 0 taken 833 times.
✓ Branch 1 taken 402823 times.
|
403656 | if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) { |
806 | 833 | ret = process_subtitle(ist, d->frame); | |
807 | } else { | ||
808 | 402823 | ret = send_frame_to_filters(ist, d->frame); | |
809 | } | ||
810 | 403656 | av_frame_unref(d->frame); | |
811 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 403656 times.
|
403656 | if (ret < 0) |
812 | ✗ | goto finish; | |
813 | } | ||
814 | |||
815 | 6341 | finish: | |
816 | 6341 | thread_ret = dec_thread_stop(d); | |
817 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (thread_ret < 0) { |
818 | ✗ | av_log(ist, AV_LOG_ERROR, "Decoder thread returned error: %s\n", | |
819 | ✗ | av_err2str(thread_ret)); | |
820 | ✗ | ret = err_merge(ret, thread_ret); | |
821 | } | ||
822 | // non-EOF errors here are all fatal | ||
823 |
2/4✓ Branch 0 taken 6341 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6341 times.
|
6341 | if (ret < 0 && ret != AVERROR_EOF) |
824 | ✗ | return ret; | |
825 | |||
826 | // signal EOF to our downstreams | ||
827 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6301 times.
|
6341 | if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) |
828 | 40 | sub2video_flush(ist); | |
829 | else { | ||
830 | 6301 | ret = send_filter_eof(ist); | |
831 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6301 times.
|
6301 | if (ret < 0) { |
832 | ✗ | av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n"); | |
833 | ✗ | return ret; | |
834 | } | ||
835 | } | ||
836 | |||
837 | 6341 | return AVERROR_EOF; | |
838 | } | ||
839 | |||
840 | 6341 | static int dec_thread_start(InputStream *ist) | |
841 | { | ||
842 | 6341 | Decoder *d = ist->decoder; | |
843 | ObjPool *op; | ||
844 | 6341 | int ret = 0; | |
845 | |||
846 | 6341 | op = objpool_alloc_packets(); | |
847 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!op) |
848 | ✗ | return AVERROR(ENOMEM); | |
849 | |||
850 | 6341 | d->queue_in = tq_alloc(1, 1, op, pkt_move); | |
851 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!d->queue_in) { |
852 | ✗ | objpool_free(&op); | |
853 | ✗ | return AVERROR(ENOMEM); | |
854 | } | ||
855 | |||
856 | 6341 | op = objpool_alloc_frames(); | |
857 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!op) |
858 | ✗ | goto fail; | |
859 | |||
860 | 6341 | d->queue_out = tq_alloc(1, 4, op, frame_move); | |
861 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!d->queue_out) { |
862 | ✗ | objpool_free(&op); | |
863 | ✗ | goto fail; | |
864 | } | ||
865 | |||
866 | 6341 | ret = pthread_create(&d->thread, NULL, decoder_thread, ist); | |
867 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret) { |
868 | ✗ | ret = AVERROR(ret); | |
869 | ✗ | av_log(ist, AV_LOG_ERROR, "pthread_create() failed: %s\n", | |
870 | ✗ | av_err2str(ret)); | |
871 | ✗ | goto fail; | |
872 | } | ||
873 | |||
874 | 6341 | return 0; | |
875 | ✗ | fail: | |
876 | ✗ | if (ret >= 0) | |
877 | ✗ | ret = AVERROR(ENOMEM); | |
878 | |||
879 | ✗ | tq_free(&d->queue_in); | |
880 | ✗ | tq_free(&d->queue_out); | |
881 | ✗ | return ret; | |
882 | } | ||
883 | |||
884 | 985 | static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) | |
885 | { | ||
886 | 985 | InputStream *ist = s->opaque; | |
887 | 985 | Decoder *d = ist->decoder; | |
888 | const enum AVPixelFormat *p; | ||
889 | |||
890 |
1/2✓ Branch 0 taken 2660 times.
✗ Branch 1 not taken.
|
2660 | for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { |
891 | 2660 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); | |
892 | 2660 | const AVCodecHWConfig *config = NULL; | |
893 | int i; | ||
894 | |||
895 |
2/2✓ Branch 0 taken 985 times.
✓ Branch 1 taken 1675 times.
|
2660 | if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) |
896 | 985 | break; | |
897 | |||
898 |
1/2✓ Branch 0 taken 1675 times.
✗ Branch 1 not taken.
|
1675 | if (ist->hwaccel_id == HWACCEL_GENERIC || |
899 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
|
1675 | ist->hwaccel_id == HWACCEL_AUTO) { |
900 | ✗ | for (i = 0;; i++) { | |
901 | ✗ | config = avcodec_get_hw_config(s->codec, i); | |
902 | ✗ | if (!config) | |
903 | ✗ | break; | |
904 | ✗ | if (!(config->methods & | |
905 | AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) | ||
906 | ✗ | continue; | |
907 | ✗ | if (config->pix_fmt == *p) | |
908 | ✗ | break; | |
909 | } | ||
910 | } | ||
911 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1675 | if (config && config->device_type == ist->hwaccel_device_type) { |
912 | ✗ | d->hwaccel_pix_fmt = *p; | |
913 | ✗ | break; | |
914 | } | ||
915 | } | ||
916 | |||
917 | 985 | return *p; | |
918 | } | ||
919 | |||
920 | 6341 | static HWDevice *hw_device_match_by_codec(const AVCodec *codec) | |
921 | { | ||
922 | const AVCodecHWConfig *config; | ||
923 | HWDevice *dev; | ||
924 | int i; | ||
925 | 6341 | for (i = 0;; i++) { | |
926 | 7992 | config = avcodec_get_hw_config(codec, i); | |
927 |
2/2✓ Branch 0 taken 6341 times.
✓ Branch 1 taken 1651 times.
|
7992 | if (!config) |
928 | 6341 | return NULL; | |
929 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1651 times.
|
1651 | if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) |
930 | ✗ | continue; | |
931 | 1651 | dev = hw_device_get_by_type(config->device_type); | |
932 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1651 times.
|
1651 | if (dev) |
933 | ✗ | return dev; | |
934 | } | ||
935 | } | ||
936 | |||
937 | 6341 | static int hw_device_setup_for_decode(InputStream *ist) | |
938 | { | ||
939 | const AVCodecHWConfig *config; | ||
940 | enum AVHWDeviceType type; | ||
941 | 6341 | HWDevice *dev = NULL; | |
942 | 6341 | int err, auto_device = 0; | |
943 | |||
944 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ist->hwaccel_device) { |
945 | ✗ | dev = hw_device_get_by_name(ist->hwaccel_device); | |
946 | ✗ | if (!dev) { | |
947 | ✗ | if (ist->hwaccel_id == HWACCEL_AUTO) { | |
948 | ✗ | auto_device = 1; | |
949 | ✗ | } else if (ist->hwaccel_id == HWACCEL_GENERIC) { | |
950 | ✗ | type = ist->hwaccel_device_type; | |
951 | ✗ | err = hw_device_init_from_type(type, ist->hwaccel_device, | |
952 | &dev); | ||
953 | } else { | ||
954 | // This will be dealt with by API-specific initialisation | ||
955 | // (using hwaccel_device), so nothing further needed here. | ||
956 | ✗ | return 0; | |
957 | } | ||
958 | } else { | ||
959 | ✗ | if (ist->hwaccel_id == HWACCEL_AUTO) { | |
960 | ✗ | ist->hwaccel_device_type = dev->type; | |
961 | ✗ | } else if (ist->hwaccel_device_type != dev->type) { | |
962 | ✗ | av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device " | |
963 | "specified for decoder: device %s of type %s is not " | ||
964 | ✗ | "usable with hwaccel %s.\n", dev->name, | |
965 | ✗ | av_hwdevice_get_type_name(dev->type), | |
966 | av_hwdevice_get_type_name(ist->hwaccel_device_type)); | ||
967 | ✗ | return AVERROR(EINVAL); | |
968 | } | ||
969 | } | ||
970 | } else { | ||
971 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ist->hwaccel_id == HWACCEL_AUTO) { |
972 | ✗ | auto_device = 1; | |
973 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | } else if (ist->hwaccel_id == HWACCEL_GENERIC) { |
974 | ✗ | type = ist->hwaccel_device_type; | |
975 | ✗ | dev = hw_device_get_by_type(type); | |
976 | |||
977 | // When "-qsv_device device" is used, an internal QSV device named | ||
978 | // as "__qsv_device" is created. Another QSV device is created too | ||
979 | // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices | ||
980 | // if both "-qsv_device device" and "-init_hw_device qsv=name:device" | ||
981 | // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL. | ||
982 | // To keep back-compatibility with the removed ad-hoc libmfx setup code, | ||
983 | // call hw_device_get_by_name("__qsv_device") to select the internal QSV | ||
984 | // device. | ||
985 | ✗ | if (!dev && type == AV_HWDEVICE_TYPE_QSV) | |
986 | ✗ | dev = hw_device_get_by_name("__qsv_device"); | |
987 | |||
988 | ✗ | if (!dev) | |
989 | ✗ | err = hw_device_init_from_type(type, NULL, &dev); | |
990 | } else { | ||
991 | 6341 | dev = hw_device_match_by_codec(ist->dec); | |
992 |
1/2✓ Branch 0 taken 6341 times.
✗ Branch 1 not taken.
|
6341 | if (!dev) { |
993 | // No device for this codec, but not using generic hwaccel | ||
994 | // and therefore may well not need one - ignore. | ||
995 | 6341 | return 0; | |
996 | } | ||
997 | } | ||
998 | } | ||
999 | |||
1000 | ✗ | if (auto_device) { | |
1001 | int i; | ||
1002 | ✗ | if (!avcodec_get_hw_config(ist->dec, 0)) { | |
1003 | // Decoder does not support any hardware devices. | ||
1004 | ✗ | return 0; | |
1005 | } | ||
1006 | ✗ | for (i = 0; !dev; i++) { | |
1007 | ✗ | config = avcodec_get_hw_config(ist->dec, i); | |
1008 | ✗ | if (!config) | |
1009 | ✗ | break; | |
1010 | ✗ | type = config->device_type; | |
1011 | ✗ | dev = hw_device_get_by_type(type); | |
1012 | ✗ | if (dev) { | |
1013 | ✗ | av_log(NULL, AV_LOG_INFO, "Using auto " | |
1014 | "hwaccel type %s with existing device %s.\n", | ||
1015 | ✗ | av_hwdevice_get_type_name(type), dev->name); | |
1016 | } | ||
1017 | } | ||
1018 | ✗ | for (i = 0; !dev; i++) { | |
1019 | ✗ | config = avcodec_get_hw_config(ist->dec, i); | |
1020 | ✗ | if (!config) | |
1021 | ✗ | break; | |
1022 | ✗ | type = config->device_type; | |
1023 | // Try to make a new device of this type. | ||
1024 | ✗ | err = hw_device_init_from_type(type, ist->hwaccel_device, | |
1025 | &dev); | ||
1026 | ✗ | if (err < 0) { | |
1027 | // Can't make a device of this type. | ||
1028 | ✗ | continue; | |
1029 | } | ||
1030 | ✗ | if (ist->hwaccel_device) { | |
1031 | ✗ | av_log(NULL, AV_LOG_INFO, "Using auto " | |
1032 | "hwaccel type %s with new device created " | ||
1033 | "from %s.\n", av_hwdevice_get_type_name(type), | ||
1034 | ist->hwaccel_device); | ||
1035 | } else { | ||
1036 | ✗ | av_log(NULL, AV_LOG_INFO, "Using auto " | |
1037 | "hwaccel type %s with new default device.\n", | ||
1038 | av_hwdevice_get_type_name(type)); | ||
1039 | } | ||
1040 | } | ||
1041 | ✗ | if (dev) { | |
1042 | ✗ | ist->hwaccel_device_type = type; | |
1043 | } else { | ||
1044 | ✗ | av_log(NULL, AV_LOG_INFO, "Auto hwaccel " | |
1045 | "disabled: no device found.\n"); | ||
1046 | ✗ | ist->hwaccel_id = HWACCEL_NONE; | |
1047 | ✗ | return 0; | |
1048 | } | ||
1049 | } | ||
1050 | |||
1051 | ✗ | if (!dev) { | |
1052 | ✗ | av_log(NULL, AV_LOG_ERROR, "No device available " | |
1053 | "for decoder: device type %s needed for codec %s.\n", | ||
1054 | ✗ | av_hwdevice_get_type_name(type), ist->dec->name); | |
1055 | ✗ | return err; | |
1056 | } | ||
1057 | |||
1058 | ✗ | ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); | |
1059 | ✗ | if (!ist->dec_ctx->hw_device_ctx) | |
1060 | ✗ | return AVERROR(ENOMEM); | |
1061 | |||
1062 | ✗ | return 0; | |
1063 | } | ||
1064 | |||
1065 | 6341 | int dec_open(InputStream *ist) | |
1066 | { | ||
1067 | Decoder *d; | ||
1068 | 6341 | const AVCodec *codec = ist->dec; | |
1069 | int ret; | ||
1070 | |||
1071 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (!codec) { |
1072 | ✗ | av_log(ist, AV_LOG_ERROR, | |
1073 | "Decoding requested, but no decoder found for: %s\n", | ||
1074 | ✗ | avcodec_get_name(ist->dec_ctx->codec_id)); | |
1075 | ✗ | return AVERROR(EINVAL); | |
1076 | } | ||
1077 | |||
1078 | 6341 | ret = dec_alloc(&ist->decoder); | |
1079 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret < 0) |
1080 | ✗ | return ret; | |
1081 | 6341 | d = ist->decoder; | |
1082 | |||
1083 |
4/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6301 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 38 times.
|
6341 | if (codec->type == AVMEDIA_TYPE_SUBTITLE && ist->fix_sub_duration) { |
1084 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int i = 0; i < FF_ARRAY_ELEMS(d->sub_prev); i++) { |
1085 | 4 | d->sub_prev[i] = av_frame_alloc(); | |
1086 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!d->sub_prev[i]) |
1087 | ✗ | return AVERROR(ENOMEM); | |
1088 | } | ||
1089 | 2 | d->sub_heartbeat = av_frame_alloc(); | |
1090 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!d->sub_heartbeat) |
1091 | ✗ | return AVERROR(ENOMEM); | |
1092 | } | ||
1093 | |||
1094 | 6341 | ist->dec_ctx->opaque = ist; | |
1095 | 6341 | ist->dec_ctx->get_format = get_format; | |
1096 | |||
1097 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6340 times.
|
6341 | if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE && |
1098 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | (ist->decoding_needed & DECODING_FOR_OST)) { |
1099 | 1 | av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE); | |
1100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ist->decoding_needed & DECODING_FOR_FILTER) |
1101 | ✗ | av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n"); | |
1102 | } | ||
1103 | |||
1104 | /* Useful for subtitles retiming by lavf (FIXME), skipping samples in | ||
1105 | * audio, and video decoders such as cuvid or mediacodec */ | ||
1106 | 6341 | ist->dec_ctx->pkt_timebase = ist->st->time_base; | |
1107 | |||
1108 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 2 taken 6284 times.
|
6341 | if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0)) |
1109 | 57 | av_dict_set(&ist->decoder_opts, "threads", "auto", 0); | |
1110 | /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */ | ||
1111 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6336 times.
|
6341 | if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC) |
1112 | 5 | av_dict_set(&ist->decoder_opts, "threads", "1", 0); | |
1113 | |||
1114 | 6341 | ret = hw_device_setup_for_decode(ist); | |
1115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret < 0) { |
1116 | ✗ | av_log(ist, AV_LOG_ERROR, | |
1117 | "Hardware device setup failed for decoder: %s\n", | ||
1118 | ✗ | av_err2str(ret)); | |
1119 | ✗ | return ret; | |
1120 | } | ||
1121 | |||
1122 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6341 times.
|
6341 | if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) { |
1123 | ✗ | av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n", | |
1124 | ✗ | av_err2str(ret)); | |
1125 | ✗ | return ret; | |
1126 | } | ||
1127 | |||
1128 | 6341 | ret = check_avoptions(ist->decoder_opts); | |
1129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret < 0) |
1130 | ✗ | return ret; | |
1131 | |||
1132 | 6341 | ret = dec_thread_start(ist); | |
1133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6341 times.
|
6341 | if (ret < 0) { |
1134 | ✗ | av_log(ist, AV_LOG_ERROR, "Error starting decoder thread: %s\n", | |
1135 | ✗ | av_err2str(ret)); | |
1136 | ✗ | return ret; | |
1137 | } | ||
1138 | |||
1139 | 6341 | return 0; | |
1140 | } | ||
1141 |