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/avstring.h" | ||
21 | #include "libavutil/dict.h" | ||
22 | #include "libavutil/error.h" | ||
23 | #include "libavutil/log.h" | ||
24 | #include "libavutil/pixdesc.h" | ||
25 | #include "libavutil/pixfmt.h" | ||
26 | #include "libavutil/time.h" | ||
27 | #include "libavutil/timestamp.h" | ||
28 | |||
29 | #include "libavcodec/avcodec.h" | ||
30 | #include "libavcodec/codec.h" | ||
31 | |||
32 | #include "libavfilter/buffersrc.h" | ||
33 | |||
34 | #include "ffmpeg.h" | ||
35 | #include "ffmpeg_utils.h" | ||
36 | #include "thread_queue.h" | ||
37 | |||
38 | typedef struct DecoderPriv { | ||
39 | Decoder dec; | ||
40 | |||
41 | AVCodecContext *dec_ctx; | ||
42 | |||
43 | AVFrame *frame; | ||
44 | AVPacket *pkt; | ||
45 | |||
46 | // override output video sample aspect ratio with this value | ||
47 | AVRational sar_override; | ||
48 | |||
49 | AVRational framerate_in; | ||
50 | |||
51 | // a combination of DECODER_FLAG_*, provided to dec_open() | ||
52 | int flags; | ||
53 | |||
54 | enum AVPixelFormat hwaccel_pix_fmt; | ||
55 | enum HWAccelID hwaccel_id; | ||
56 | enum AVHWDeviceType hwaccel_device_type; | ||
57 | enum AVPixelFormat hwaccel_output_format; | ||
58 | |||
59 | // pts/estimated duration of the last decoded frame | ||
60 | // * in decoder timebase for video, | ||
61 | // * in last_frame_tb (may change during decoding) for audio | ||
62 | int64_t last_frame_pts; | ||
63 | int64_t last_frame_duration_est; | ||
64 | AVRational last_frame_tb; | ||
65 | int64_t last_filter_in_rescale_delta; | ||
66 | int last_frame_sample_rate; | ||
67 | |||
68 | /* previous decoded subtitles */ | ||
69 | AVFrame *sub_prev[2]; | ||
70 | AVFrame *sub_heartbeat; | ||
71 | |||
72 | Scheduler *sch; | ||
73 | unsigned sch_idx; | ||
74 | |||
75 | // this decoder's index in decoders or -1 | ||
76 | int index; | ||
77 | void *log_parent; | ||
78 | char log_name[32]; | ||
79 | char *parent_name; | ||
80 | |||
81 | struct { | ||
82 | AVDictionary *opts; | ||
83 | const AVCodec *codec; | ||
84 | } standalone_init; | ||
85 | } DecoderPriv; | ||
86 | |||
87 | 6391 | static DecoderPriv *dp_from_dec(Decoder *d) | |
88 | { | ||
89 | 6391 | return (DecoderPriv*)d; | |
90 | } | ||
91 | |||
92 | // data that is local to the decoder thread and not visible outside of it | ||
93 | typedef struct DecThreadContext { | ||
94 | AVFrame *frame; | ||
95 | AVPacket *pkt; | ||
96 | } DecThreadContext; | ||
97 | |||
98 | 7289 | void dec_free(Decoder **pdec) | |
99 | { | ||
100 | 7289 | Decoder *dec = *pdec; | |
101 | DecoderPriv *dp; | ||
102 | |||
103 |
2/2✓ Branch 0 taken 899 times.
✓ Branch 1 taken 6390 times.
|
7289 | if (!dec) |
104 | 899 | return; | |
105 | 6390 | dp = dp_from_dec(dec); | |
106 | |||
107 | 6390 | avcodec_free_context(&dp->dec_ctx); | |
108 | |||
109 | 6390 | av_frame_free(&dp->frame); | |
110 | 6390 | av_packet_free(&dp->pkt); | |
111 | |||
112 | 6390 | av_dict_free(&dp->standalone_init.opts); | |
113 | |||
114 |
2/2✓ Branch 0 taken 12780 times.
✓ Branch 1 taken 6390 times.
|
19170 | for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) |
115 | 12780 | av_frame_free(&dp->sub_prev[i]); | |
116 | 6390 | av_frame_free(&dp->sub_heartbeat); | |
117 | |||
118 | 6390 | av_freep(&dp->parent_name); | |
119 | |||
120 | 6390 | av_freep(pdec); | |
121 | } | ||
122 | |||
123 | 558 | static const char *dec_item_name(void *obj) | |
124 | { | ||
125 | 558 | const DecoderPriv *dp = obj; | |
126 | |||
127 | 558 | return dp->log_name; | |
128 | } | ||
129 | |||
130 | static const AVClass dec_class = { | ||
131 | .class_name = "Decoder", | ||
132 | .version = LIBAVUTIL_VERSION_INT, | ||
133 | .parent_log_context_offset = offsetof(DecoderPriv, log_parent), | ||
134 | .item_name = dec_item_name, | ||
135 | }; | ||
136 | |||
137 | static int decoder_thread(void *arg); | ||
138 | |||
139 | 6390 | static int dec_alloc(DecoderPriv **pdec, Scheduler *sch, int send_end_ts) | |
140 | { | ||
141 | DecoderPriv *dp; | ||
142 | 6390 | int ret = 0; | |
143 | |||
144 | 6390 | *pdec = NULL; | |
145 | |||
146 | 6390 | dp = av_mallocz(sizeof(*dp)); | |
147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dp) |
148 | ✗ | return AVERROR(ENOMEM); | |
149 | |||
150 | 6390 | dp->frame = av_frame_alloc(); | |
151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dp->frame) |
152 | ✗ | goto fail; | |
153 | |||
154 | 6390 | dp->pkt = av_packet_alloc(); | |
155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dp->pkt) |
156 | ✗ | goto fail; | |
157 | |||
158 | 6390 | dp->index = -1; | |
159 | 6390 | dp->dec.class = &dec_class; | |
160 | 6390 | dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE; | |
161 | 6390 | dp->last_frame_pts = AV_NOPTS_VALUE; | |
162 | 6390 | dp->last_frame_tb = (AVRational){ 1, 1 }; | |
163 | 6390 | dp->hwaccel_pix_fmt = AV_PIX_FMT_NONE; | |
164 | |||
165 | 6390 | ret = sch_add_dec(sch, decoder_thread, dp, send_end_ts); | |
166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret < 0) |
167 | ✗ | goto fail; | |
168 | 6390 | dp->sch = sch; | |
169 | 6390 | dp->sch_idx = ret; | |
170 | |||
171 | 6390 | *pdec = dp; | |
172 | |||
173 | 6390 | return 0; | |
174 | ✗ | fail: | |
175 | ✗ | dec_free((Decoder**)&dp); | |
176 | ✗ | return ret >= 0 ? AVERROR(ENOMEM) : ret; | |
177 | } | ||
178 | |||
179 | 250444 | static AVRational audio_samplerate_update(DecoderPriv *dp, | |
180 | const AVFrame *frame) | ||
181 | { | ||
182 | 250444 | const int prev = dp->last_frame_tb.den; | |
183 | 250444 | const int sr = frame->sample_rate; | |
184 | |||
185 | AVRational tb_new; | ||
186 | int64_t gcd; | ||
187 | |||
188 |
2/2✓ Branch 0 taken 249248 times.
✓ Branch 1 taken 1196 times.
|
250444 | if (frame->sample_rate == dp->last_frame_sample_rate) |
189 | 249248 | goto finish; | |
190 | |||
191 | 1196 | gcd = av_gcd(prev, sr); | |
192 | |||
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1196 times.
|
1196 | if (prev / gcd >= INT_MAX / sr) { |
194 | ✗ | av_log(dp, AV_LOG_WARNING, | |
195 | "Audio timestamps cannot be represented exactly after " | ||
196 | "sample rate change: %d -> %d\n", prev, sr); | ||
197 | |||
198 | // LCM of 192000, 44100, allows to represent all common samplerates | ||
199 | ✗ | tb_new = (AVRational){ 1, 28224000 }; | |
200 | } else | ||
201 | 1196 | tb_new = (AVRational){ 1, prev / gcd * sr }; | |
202 | |||
203 | // keep the frame timebase if it is strictly better than | ||
204 | // the samplerate-defined one | ||
205 |
4/4✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 1074 times.
|
1196 | if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den && |
206 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 65 times.
|
106 | !(frame->time_base.den % tb_new.den)) |
207 | 41 | tb_new = frame->time_base; | |
208 | |||
209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1196 times.
|
1196 | if (dp->last_frame_pts != AV_NOPTS_VALUE) |
210 | ✗ | dp->last_frame_pts = av_rescale_q(dp->last_frame_pts, | |
211 | dp->last_frame_tb, tb_new); | ||
212 | 1196 | dp->last_frame_duration_est = av_rescale_q(dp->last_frame_duration_est, | |
213 | dp->last_frame_tb, tb_new); | ||
214 | |||
215 | 1196 | dp->last_frame_tb = tb_new; | |
216 | 1196 | dp->last_frame_sample_rate = frame->sample_rate; | |
217 | |||
218 | 250444 | finish: | |
219 | 250444 | return dp->last_frame_tb; | |
220 | } | ||
221 | |||
222 | 250444 | static void audio_ts_process(DecoderPriv *dp, AVFrame *frame) | |
223 | { | ||
224 | 250444 | AVRational tb_filter = (AVRational){1, frame->sample_rate}; | |
225 | AVRational tb; | ||
226 | int64_t pts_pred; | ||
227 | |||
228 | // on samplerate change, choose a new internal timebase for timestamp | ||
229 | // generation that can represent timestamps from all the samplerates | ||
230 | // seen so far | ||
231 | 250444 | tb = audio_samplerate_update(dp, frame); | |
232 |
2/2✓ Branch 0 taken 249248 times.
✓ Branch 1 taken 1196 times.
|
250444 | pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 : |
233 | 249248 | dp->last_frame_pts + dp->last_frame_duration_est; | |
234 | |||
235 |
2/2✓ Branch 0 taken 50346 times.
✓ Branch 1 taken 200098 times.
|
250444 | if (frame->pts == AV_NOPTS_VALUE) { |
236 | 50346 | frame->pts = pts_pred; | |
237 | 50346 | frame->time_base = tb; | |
238 |
2/2✓ Branch 0 taken 198915 times.
✓ Branch 1 taken 1183 times.
|
200098 | } else if (dp->last_frame_pts != AV_NOPTS_VALUE && |
239 |
2/2✓ Branch 0 taken 610 times.
✓ Branch 1 taken 198305 times.
|
198915 | frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base, |
240 | AV_ROUND_UP)) { | ||
241 | // there was a gap in timestamps, reset conversion state | ||
242 | 610 | dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE; | |
243 | } | ||
244 | |||
245 | 250444 | frame->pts = av_rescale_delta(frame->time_base, frame->pts, | |
246 | tb, frame->nb_samples, | ||
247 | &dp->last_filter_in_rescale_delta, tb); | ||
248 | |||
249 | 250444 | dp->last_frame_pts = frame->pts; | |
250 | 250444 | dp->last_frame_duration_est = av_rescale_q(frame->nb_samples, | |
251 | tb_filter, tb); | ||
252 | |||
253 | // finally convert to filtering timebase | ||
254 | 250444 | frame->pts = av_rescale_q(frame->pts, tb, tb_filter); | |
255 | 250444 | frame->duration = frame->nb_samples; | |
256 | 250444 | frame->time_base = tb_filter; | |
257 | 250444 | } | |
258 | |||
259 | 129713 | static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame) | |
260 | { | ||
261 | 129713 | const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE; | |
262 | 129713 | const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED; | |
263 | 129713 | int64_t codec_duration = 0; | |
264 | |||
265 | // XXX lavf currently makes up frame durations when they are not provided by | ||
266 | // the container. As there is no way to reliably distinguish real container | ||
267 | // durations from the fake made-up ones, we use heuristics based on whether | ||
268 | // the container has timestamps. Eventually lavf should stop making up | ||
269 | // durations, then this should be simplified. | ||
270 | |||
271 | // prefer frame duration for containers with timestamps | ||
272 |
6/6✓ Branch 0 taken 128081 times.
✓ Branch 1 taken 1632 times.
✓ Branch 2 taken 33025 times.
✓ Branch 3 taken 95056 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 33018 times.
|
129713 | if (frame->duration > 0 && (!ts_unreliable || fr_forced)) |
273 | 95063 | return frame->duration; | |
274 | |||
275 |
4/4✓ Branch 0 taken 34569 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 5354 times.
✓ Branch 3 taken 29215 times.
|
34650 | if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) { |
276 | 5354 | int fields = frame->repeat_pict + 2; | |
277 | 5354 | AVRational field_rate = av_mul_q(dp->dec_ctx->framerate, | |
278 | 5354 | (AVRational){ 2, 1 }); | |
279 | 5354 | codec_duration = av_rescale_q(fields, av_inv_q(field_rate), | |
280 | frame->time_base); | ||
281 | } | ||
282 | |||
283 | // prefer codec-layer duration for containers without timestamps | ||
284 |
4/4✓ Branch 0 taken 5354 times.
✓ Branch 1 taken 29296 times.
✓ Branch 2 taken 5333 times.
✓ Branch 3 taken 21 times.
|
34650 | if (codec_duration > 0 && ts_unreliable) |
285 | 5333 | return codec_duration; | |
286 | |||
287 | // when timestamps are available, repeat last frame's actual duration | ||
288 | // (i.e. pts difference between this and last frame) | ||
289 |
3/4✓ Branch 0 taken 29317 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28810 times.
✓ Branch 3 taken 507 times.
|
29317 | if (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE && |
290 |
1/2✓ Branch 0 taken 28810 times.
✗ Branch 1 not taken.
|
28810 | frame->pts > dp->last_frame_pts) |
291 | 28810 | return frame->pts - dp->last_frame_pts; | |
292 | |||
293 | // try frame/codec duration | ||
294 |
2/2✓ Branch 0 taken 438 times.
✓ Branch 1 taken 69 times.
|
507 | if (frame->duration > 0) |
295 | 438 | return frame->duration; | |
296 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 68 times.
|
69 | if (codec_duration > 0) |
297 | 1 | return codec_duration; | |
298 | |||
299 | // try average framerate | ||
300 |
3/4✓ Branch 0 taken 43 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
|
68 | if (dp->framerate_in.num && dp->framerate_in.den) { |
301 | 43 | int64_t d = av_rescale_q(1, av_inv_q(dp->framerate_in), | |
302 | frame->time_base); | ||
303 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | if (d > 0) |
304 | 43 | return d; | |
305 | } | ||
306 | |||
307 | // last resort is last frame's estimated duration, and 1 | ||
308 | 25 | return FFMAX(dp->last_frame_duration_est, 1); | |
309 | } | ||
310 | |||
311 | ✗ | static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input) | |
312 | { | ||
313 | ✗ | DecoderPriv *dp = avctx->opaque; | |
314 | ✗ | AVFrame *output = NULL; | |
315 | ✗ | enum AVPixelFormat output_format = dp->hwaccel_output_format; | |
316 | int err; | ||
317 | |||
318 | ✗ | if (input->format == output_format) { | |
319 | // Nothing to do. | ||
320 | ✗ | return 0; | |
321 | } | ||
322 | |||
323 | ✗ | output = av_frame_alloc(); | |
324 | ✗ | if (!output) | |
325 | ✗ | return AVERROR(ENOMEM); | |
326 | |||
327 | ✗ | output->format = output_format; | |
328 | |||
329 | ✗ | err = av_hwframe_transfer_data(output, input, 0); | |
330 | ✗ | if (err < 0) { | |
331 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to " | |
332 | "output frame: %d.\n", err); | ||
333 | ✗ | goto fail; | |
334 | } | ||
335 | |||
336 | ✗ | err = av_frame_copy_props(output, input); | |
337 | ✗ | if (err < 0) { | |
338 | ✗ | av_frame_unref(output); | |
339 | ✗ | goto fail; | |
340 | } | ||
341 | |||
342 | ✗ | av_frame_unref(input); | |
343 | ✗ | av_frame_move_ref(input, output); | |
344 | ✗ | av_frame_free(&output); | |
345 | |||
346 | ✗ | return 0; | |
347 | |||
348 | ✗ | fail: | |
349 | ✗ | av_frame_free(&output); | |
350 | ✗ | return err; | |
351 | } | ||
352 | |||
353 | 129713 | static int video_frame_process(DecoderPriv *dp, AVFrame *frame) | |
354 | { | ||
355 | #if FFMPEG_OPT_TOP | ||
356 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129713 times.
|
129713 | if (dp->flags & DECODER_FLAG_TOP_FIELD_FIRST) { |
357 | ✗ | av_log(dp, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n"); | |
358 | ✗ | frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
359 | } | ||
360 | #endif | ||
361 | |||
362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129713 times.
|
129713 | if (frame->format == dp->hwaccel_pix_fmt) { |
363 | ✗ | int err = hwaccel_retrieve_data(dp->dec_ctx, frame); | |
364 | ✗ | if (err < 0) | |
365 | ✗ | return err; | |
366 | } | ||
367 | |||
368 | 129713 | frame->pts = frame->best_effort_timestamp; | |
369 | |||
370 | // forced fixed framerate | ||
371 |
2/2✓ Branch 0 taken 317 times.
✓ Branch 1 taken 129396 times.
|
129713 | if (dp->flags & DECODER_FLAG_FRAMERATE_FORCED) { |
372 | 317 | frame->pts = AV_NOPTS_VALUE; | |
373 | 317 | frame->duration = 1; | |
374 | 317 | frame->time_base = av_inv_q(dp->framerate_in); | |
375 | } | ||
376 | |||
377 | // no timestamp available - extrapolate from previous frame duration | ||
378 |
2/2✓ Branch 0 taken 33396 times.
✓ Branch 1 taken 96317 times.
|
129713 | if (frame->pts == AV_NOPTS_VALUE) |
379 |
2/2✓ Branch 0 taken 32853 times.
✓ Branch 1 taken 543 times.
|
66249 | frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 : |
380 | 32853 | dp->last_frame_pts + dp->last_frame_duration_est; | |
381 | |||
382 | // update timestamp history | ||
383 | 129713 | dp->last_frame_duration_est = video_duration_estimate(dp, frame); | |
384 | 129713 | dp->last_frame_pts = frame->pts; | |
385 | 129713 | dp->last_frame_tb = frame->time_base; | |
386 | |||
387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129713 times.
|
129713 | if (debug_ts) { |
388 | ✗ | av_log(dp, AV_LOG_INFO, | |
389 | "decoder -> pts:%s pts_time:%s " | ||
390 | "pkt_dts:%s pkt_dts_time:%s " | ||
391 | "duration:%s duration_time:%s " | ||
392 | "keyframe:%d frame_type:%d time_base:%d/%d\n", | ||
393 | ✗ | av_ts2str(frame->pts), | |
394 | ✗ | av_ts2timestr(frame->pts, &frame->time_base), | |
395 | ✗ | av_ts2str(frame->pkt_dts), | |
396 | ✗ | av_ts2timestr(frame->pkt_dts, &frame->time_base), | |
397 | ✗ | av_ts2str(frame->duration), | |
398 | ✗ | av_ts2timestr(frame->duration, &frame->time_base), | |
399 | ✗ | !!(frame->flags & AV_FRAME_FLAG_KEY), frame->pict_type, | |
400 | frame->time_base.num, frame->time_base.den); | ||
401 | } | ||
402 | |||
403 |
2/2✓ Branch 0 taken 13503 times.
✓ Branch 1 taken 116210 times.
|
129713 | if (dp->sar_override.num) |
404 | 13503 | frame->sample_aspect_ratio = dp->sar_override; | |
405 | |||
406 | 129713 | return 0; | |
407 | } | ||
408 | |||
409 | 1 | static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src) | |
410 | { | ||
411 | 1 | int ret = AVERROR_BUG; | |
412 | 1 | AVSubtitle tmp = { | |
413 | 1 | .format = src->format, | |
414 | 1 | .start_display_time = src->start_display_time, | |
415 | 1 | .end_display_time = src->end_display_time, | |
416 | .num_rects = 0, | ||
417 | .rects = NULL, | ||
418 | 1 | .pts = src->pts | |
419 | }; | ||
420 | |||
421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!src->num_rects) |
422 | ✗ | goto success; | |
423 | |||
424 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects)))) |
425 | ✗ | return AVERROR(ENOMEM); | |
426 | |||
427 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (int i = 0; i < src->num_rects; i++) { |
428 | 1 | AVSubtitleRect *src_rect = src->rects[i]; | |
429 | AVSubtitleRect *dst_rect; | ||
430 | |||
431 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) { |
432 | ✗ | ret = AVERROR(ENOMEM); | |
433 | ✗ | goto cleanup; | |
434 | } | ||
435 | |||
436 | 1 | tmp.num_rects++; | |
437 | |||
438 | 1 | dst_rect->type = src_rect->type; | |
439 | 1 | dst_rect->flags = src_rect->flags; | |
440 | |||
441 | 1 | dst_rect->x = src_rect->x; | |
442 | 1 | dst_rect->y = src_rect->y; | |
443 | 1 | dst_rect->w = src_rect->w; | |
444 | 1 | dst_rect->h = src_rect->h; | |
445 | 1 | dst_rect->nb_colors = src_rect->nb_colors; | |
446 | |||
447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (src_rect->text) |
448 | ✗ | if (!(dst_rect->text = av_strdup(src_rect->text))) { | |
449 | ✗ | ret = AVERROR(ENOMEM); | |
450 | ✗ | goto cleanup; | |
451 | } | ||
452 | |||
453 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (src_rect->ass) |
454 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!(dst_rect->ass = av_strdup(src_rect->ass))) { |
455 | ✗ | ret = AVERROR(ENOMEM); | |
456 | ✗ | goto cleanup; | |
457 | } | ||
458 | |||
459 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (int j = 0; j < 4; j++) { |
460 | // SUBTITLE_BITMAP images are special in the sense that they | ||
461 | // are like PAL8 images. first pointer to data, second to | ||
462 | // palette. This makes the size calculation match this. | ||
463 | ✗ | size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ? | |
464 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | AVPALETTE_SIZE : |
465 | 4 | src_rect->h * src_rect->linesize[j]; | |
466 | |||
467 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!src_rect->data[j]) |
468 | 4 | continue; | |
469 | |||
470 | ✗ | if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) { | |
471 | ✗ | ret = AVERROR(ENOMEM); | |
472 | ✗ | goto cleanup; | |
473 | } | ||
474 | ✗ | dst_rect->linesize[j] = src_rect->linesize[j]; | |
475 | } | ||
476 | } | ||
477 | |||
478 | 1 | success: | |
479 | 1 | *dst = tmp; | |
480 | |||
481 | 1 | return 0; | |
482 | |||
483 | ✗ | cleanup: | |
484 | ✗ | avsubtitle_free(&tmp); | |
485 | |||
486 | ✗ | return ret; | |
487 | } | ||
488 | |||
489 | 834 | static void subtitle_free(void *opaque, uint8_t *data) | |
490 | { | ||
491 | 834 | AVSubtitle *sub = (AVSubtitle*)data; | |
492 | 834 | avsubtitle_free(sub); | |
493 | 834 | av_free(sub); | |
494 | 834 | } | |
495 | |||
496 | 834 | static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy) | |
497 | { | ||
498 | AVBufferRef *buf; | ||
499 | AVSubtitle *sub; | ||
500 | int ret; | ||
501 | |||
502 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 833 times.
|
834 | if (copy) { |
503 | 1 | sub = av_mallocz(sizeof(*sub)); | |
504 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM); |
505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
506 | ✗ | av_freep(&sub); | |
507 | ✗ | return ret; | |
508 | } | ||
509 | } else { | ||
510 | 833 | sub = av_memdup(subtitle, sizeof(*subtitle)); | |
511 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
|
833 | if (!sub) |
512 | ✗ | return AVERROR(ENOMEM); | |
513 | 833 | memset(subtitle, 0, sizeof(*subtitle)); | |
514 | } | ||
515 | |||
516 | 834 | buf = av_buffer_create((uint8_t*)sub, sizeof(*sub), | |
517 | subtitle_free, NULL, 0); | ||
518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
|
834 | if (!buf) { |
519 | ✗ | avsubtitle_free(sub); | |
520 | ✗ | av_freep(&sub); | |
521 | ✗ | return AVERROR(ENOMEM); | |
522 | } | ||
523 | |||
524 | 834 | frame->buf[0] = buf; | |
525 | |||
526 | 834 | return 0; | |
527 | } | ||
528 | |||
529 | 834 | static int process_subtitle(DecoderPriv *dp, AVFrame *frame) | |
530 | { | ||
531 | 834 | const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data; | |
532 | 834 | int ret = 0; | |
533 | |||
534 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 817 times.
|
834 | if (dp->flags & DECODER_FLAG_FIX_SUB_DURATION) { |
535 | 34 | AVSubtitle *sub_prev = dp->sub_prev[0]->buf[0] ? | |
536 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
|
17 | (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL; |
537 | 17 | int end = 1; | |
538 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
|
17 | if (sub_prev) { |
539 | 15 | end = av_rescale(subtitle->pts - sub_prev->pts, | |
540 | 1000, AV_TIME_BASE); | ||
541 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if (end < sub_prev->end_display_time) { |
542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | av_log(dp, AV_LOG_DEBUG, |
543 | "Subtitle duration reduced from %"PRId32" to %d%s\n", | ||
544 | sub_prev->end_display_time, end, | ||
545 | end <= 0 ? ", dropping it" : ""); | ||
546 | 15 | sub_prev->end_display_time = end; | |
547 | } | ||
548 | } | ||
549 | |||
550 | 17 | av_frame_unref(dp->sub_prev[1]); | |
551 | 17 | av_frame_move_ref(dp->sub_prev[1], frame); | |
552 | |||
553 | 17 | frame = dp->sub_prev[0]; | |
554 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
|
17 | subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL; |
555 | |||
556 | 17 | FFSWAP(AVFrame*, dp->sub_prev[0], dp->sub_prev[1]); | |
557 | |||
558 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (end <= 0) |
559 | ✗ | return 0; | |
560 | } | ||
561 | |||
562 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 832 times.
|
834 | if (!subtitle) |
563 | 2 | return 0; | |
564 | |||
565 | 832 | ret = sch_dec_send(dp->sch, dp->sch_idx, frame); | |
566 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
|
832 | if (ret < 0) |
567 | ✗ | av_frame_unref(frame); | |
568 | |||
569 |
1/2✓ Branch 0 taken 832 times.
✗ Branch 1 not taken.
|
832 | return ret == AVERROR_EOF ? AVERROR_EXIT : ret; |
570 | } | ||
571 | |||
572 | 4 | static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts) | |
573 | { | ||
574 | 4 | int ret = AVERROR_BUG; | |
575 | 8 | AVSubtitle *prev_subtitle = dp->sub_prev[0]->buf[0] ? | |
576 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL; |
577 | AVSubtitle *subtitle; | ||
578 | |||
579 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
4 | if (!(dp->flags & DECODER_FLAG_FIX_SUB_DURATION) || !prev_subtitle || |
580 |
3/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
|
3 | !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts) |
581 | 3 | return 0; | |
582 | |||
583 | 1 | av_frame_unref(dp->sub_heartbeat); | |
584 | 1 | ret = subtitle_wrap_frame(dp->sub_heartbeat, prev_subtitle, 1); | |
585 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
586 | ✗ | return ret; | |
587 | |||
588 | 1 | subtitle = (AVSubtitle*)dp->sub_heartbeat->buf[0]->data; | |
589 | 1 | subtitle->pts = signal_pts; | |
590 | |||
591 | 1 | return process_subtitle(dp, dp->sub_heartbeat); | |
592 | } | ||
593 | |||
594 | 2654 | static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt, | |
595 | AVFrame *frame) | ||
596 | { | ||
597 | 2654 | AVPacket *flush_pkt = NULL; | |
598 | AVSubtitle subtitle; | ||
599 | int got_output; | ||
600 | int ret; | ||
601 | |||
602 |
4/4✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 1666 times.
|
2654 | if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) { |
603 | 948 | frame->pts = pkt->pts; | |
604 | 948 | frame->time_base = pkt->time_base; | |
605 | 948 | frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT; | |
606 | |||
607 | 948 | ret = sch_dec_send(dp->sch, dp->sch_idx, frame); | |
608 |
1/2✓ Branch 0 taken 948 times.
✗ Branch 1 not taken.
|
948 | return ret == AVERROR_EOF ? AVERROR_EXIT : ret; |
609 |
4/4✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1662 times.
|
1706 | } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) { |
610 | 4 | return fix_sub_duration_heartbeat(dp, av_rescale_q(pkt->pts, pkt->time_base, | |
611 | 4 | AV_TIME_BASE_Q)); | |
612 | } | ||
613 | |||
614 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1662 times.
|
1702 | if (!pkt) { |
615 | 40 | flush_pkt = av_packet_alloc(); | |
616 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (!flush_pkt) |
617 | ✗ | return AVERROR(ENOMEM); | |
618 | } | ||
619 | |||
620 |
2/2✓ Branch 0 taken 1662 times.
✓ Branch 1 taken 40 times.
|
1702 | ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output, |
621 | pkt ? pkt : flush_pkt); | ||
622 | 1702 | av_packet_free(&flush_pkt); | |
623 | |||
624 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1623 times.
|
1702 | if (ret < 0) { |
625 | 79 | av_log(dp, AV_LOG_ERROR, "Error decoding subtitles: %s\n", | |
626 | 79 | av_err2str(ret)); | |
627 | 79 | dp->dec.decode_errors++; | |
628 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
|
79 | return exit_on_error ? ret : 0; |
629 | } | ||
630 | |||
631 |
2/2✓ Branch 0 taken 790 times.
✓ Branch 1 taken 833 times.
|
1623 | if (!got_output) |
632 |
2/2✓ Branch 0 taken 750 times.
✓ Branch 1 taken 40 times.
|
790 | return pkt ? 0 : AVERROR_EOF; |
633 | |||
634 | 833 | dp->dec.frames_decoded++; | |
635 | |||
636 | // XXX the queue for transferring data to consumers runs | ||
637 | // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that | ||
638 | // inside the frame | ||
639 | // eventually, subtitles should be switched to use AVFrames natively | ||
640 | 833 | ret = subtitle_wrap_frame(frame, &subtitle, 0); | |
641 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
|
833 | if (ret < 0) { |
642 | ✗ | avsubtitle_free(&subtitle); | |
643 | ✗ | return ret; | |
644 | } | ||
645 | |||
646 | 833 | frame->width = dp->dec_ctx->width; | |
647 | 833 | frame->height = dp->dec_ctx->height; | |
648 | |||
649 | 833 | return process_subtitle(dp, frame); | |
650 | } | ||
651 | |||
652 | 364086 | static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame) | |
653 | { | ||
654 | 364086 | AVCodecContext *dec = dp->dec_ctx; | |
655 | 364086 | const char *type_desc = av_get_media_type_string(dec->codec_type); | |
656 | int ret; | ||
657 | |||
658 |
2/2✓ Branch 0 taken 2654 times.
✓ Branch 1 taken 361432 times.
|
364086 | if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE) |
659 | 2654 | return transcode_subtitles(dp, pkt, frame); | |
660 | |||
661 | // With fate-indeo3-2, we're getting 0-sized packets before EOF for some | ||
662 | // reason. This seems like a semi-critical bug. Don't trigger EOF, and | ||
663 | // skip the packet. | ||
664 |
4/4✓ Branch 0 taken 358207 times.
✓ Branch 1 taken 3225 times.
✓ Branch 2 taken 1480 times.
✓ Branch 3 taken 356727 times.
|
361432 | if (pkt && pkt->size == 0) |
665 | 1480 | return 0; | |
666 | |||
667 |
4/4✓ Branch 0 taken 356727 times.
✓ Branch 1 taken 3225 times.
✓ Branch 2 taken 58117 times.
✓ Branch 3 taken 298610 times.
|
359952 | if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) { |
668 | 58117 | pkt->pts = AV_NOPTS_VALUE; | |
669 | 58117 | pkt->dts = AV_NOPTS_VALUE; | |
670 | } | ||
671 | |||
672 |
2/2✓ Branch 0 taken 356727 times.
✓ Branch 1 taken 3225 times.
|
359952 | if (pkt) { |
673 | 356727 | FrameData *fd = packet_data(pkt); | |
674 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 356727 times.
|
356727 | if (!fd) |
675 | ✗ | return AVERROR(ENOMEM); | |
676 | 356727 | fd->wallclock[LATENCY_PROBE_DEC_PRE] = av_gettime_relative(); | |
677 | } | ||
678 | |||
679 | 359952 | ret = avcodec_send_packet(dec, pkt); | |
680 |
3/6✓ Branch 0 taken 359527 times.
✓ Branch 1 taken 425 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 425 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
359952 | if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) { |
681 | // In particular, we don't expect AVERROR(EAGAIN), because we read all | ||
682 | // decoded frames with avcodec_receive_frame() until done. | ||
683 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
|
425 | if (ret == AVERROR(EAGAIN)) { |
684 | ✗ | av_log(dp, AV_LOG_FATAL, "A decoder returned an unexpected error code. " | |
685 | "This is a bug, please report it.\n"); | ||
686 | ✗ | return AVERROR_BUG; | |
687 | } | ||
688 |
1/2✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
|
425 | av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n", |
689 | 425 | pkt ? "packet" : "EOF", av_err2str(ret)); | |
690 | |||
691 |
1/2✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
|
425 | if (ret != AVERROR_EOF) { |
692 | 425 | dp->dec.decode_errors++; | |
693 |
1/2✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
|
425 | if (!exit_on_error) |
694 | 425 | ret = 0; | |
695 | } | ||
696 | |||
697 | 425 | return ret; | |
698 | } | ||
699 | |||
700 | 377031 | while (1) { | |
701 | FrameData *fd; | ||
702 | |||
703 | 736558 | av_frame_unref(frame); | |
704 | |||
705 | 736558 | update_benchmark(NULL); | |
706 | 736558 | ret = avcodec_receive_frame(dec, frame); | |
707 | 736558 | update_benchmark("decode_%s %s", type_desc, dp->parent_name); | |
708 | |||
709 |
2/2✓ Branch 0 taken 353171 times.
✓ Branch 1 taken 383387 times.
|
736558 | if (ret == AVERROR(EAGAIN)) { |
710 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 353171 times.
|
353171 | av_assert0(pkt); // should never happen during flushing |
711 | 353171 | return 0; | |
712 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 380163 times.
|
383387 | } else if (ret == AVERROR_EOF) { |
713 | 3224 | return ret; | |
714 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 380157 times.
|
380163 | } else if (ret < 0) { |
715 | 6 | av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret)); | |
716 | 6 | dp->dec.decode_errors++; | |
717 | |||
718 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (exit_on_error) |
719 | ✗ | return ret; | |
720 | |||
721 | 6 | continue; | |
722 | } | ||
723 | |||
724 |
3/4✓ Branch 0 taken 380120 times.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 380120 times.
|
380157 | if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) { |
725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
|
37 | av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING, |
726 | "corrupt decoded frame\n"); | ||
727 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
|
37 | if (exit_on_error) |
728 | ✗ | return AVERROR_INVALIDDATA; | |
729 | } | ||
730 | |||
731 | 380157 | fd = frame_data(frame); | |
732 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 380157 times.
|
380157 | if (!fd) { |
733 | ✗ | av_frame_unref(frame); | |
734 | ✗ | return AVERROR(ENOMEM); | |
735 | } | ||
736 | 380157 | fd->dec.pts = frame->pts; | |
737 | 380157 | fd->dec.tb = dec->pkt_timebase; | |
738 | 380157 | fd->dec.frame_num = dec->frame_num - 1; | |
739 | 380157 | fd->bits_per_raw_sample = dec->bits_per_raw_sample; | |
740 | |||
741 | 380157 | fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative(); | |
742 | |||
743 | 380157 | frame->time_base = dec->pkt_timebase; | |
744 | |||
745 |
2/2✓ Branch 0 taken 250444 times.
✓ Branch 1 taken 129713 times.
|
380157 | if (dec->codec_type == AVMEDIA_TYPE_AUDIO) { |
746 | 250444 | dp->dec.samples_decoded += frame->nb_samples; | |
747 | |||
748 | 250444 | audio_ts_process(dp, frame); | |
749 | } else { | ||
750 | 129713 | ret = video_frame_process(dp, frame); | |
751 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129713 times.
|
129713 | if (ret < 0) { |
752 | ✗ | av_log(dp, AV_LOG_FATAL, | |
753 | "Error while processing the decoded data\n"); | ||
754 | ✗ | return ret; | |
755 | } | ||
756 | } | ||
757 | |||
758 | 380157 | dp->dec.frames_decoded++; | |
759 | |||
760 | 380157 | ret = sch_dec_send(dp->sch, dp->sch_idx, frame); | |
761 |
2/2✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 377025 times.
|
380157 | if (ret < 0) { |
762 | 3132 | av_frame_unref(frame); | |
763 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3132 times.
|
3132 | return ret == AVERROR_EOF ? AVERROR_EXIT : ret; |
764 | } | ||
765 | } | ||
766 | } | ||
767 | |||
768 | static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, | ||
769 | const DecoderOpts *o, AVFrame *param_out); | ||
770 | |||
771 | 1 | static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt) | |
772 | { | ||
773 | DecoderOpts o; | ||
774 | const FrameData *fd; | ||
775 | char name[16]; | ||
776 | |||
777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!pkt->opaque_ref) |
778 | ✗ | return AVERROR_BUG; | |
779 | 1 | fd = (FrameData *)pkt->opaque_ref->data; | |
780 | |||
781 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!fd->par_enc) |
782 | ✗ | return AVERROR_BUG; | |
783 | |||
784 | 1 | memset(&o, 0, sizeof(o)); | |
785 | |||
786 | 1 | o.par = fd->par_enc; | |
787 | 1 | o.time_base = pkt->time_base; | |
788 | |||
789 | 1 | o.codec = dp->standalone_init.codec; | |
790 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!o.codec) |
791 | 1 | o.codec = avcodec_find_decoder(o.par->codec_id); | |
792 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!o.codec) { |
793 | ✗ | const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id); | |
794 | |||
795 | ✗ | av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n", | |
796 | desc ? desc->name : "?"); | ||
797 | ✗ | return AVERROR_DECODER_NOT_FOUND; | |
798 | } | ||
799 | |||
800 | 1 | snprintf(name, sizeof(name), "dec%d", dp->index); | |
801 | 1 | o.name = name; | |
802 | |||
803 | 1 | return dec_open(dp, &dp->standalone_init.opts, &o, NULL); | |
804 | } | ||
805 | |||
806 | 6390 | static void dec_thread_set_name(const DecoderPriv *dp) | |
807 | { | ||
808 | 6390 | char name[16] = "dec"; | |
809 | |||
810 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6389 times.
|
6390 | if (dp->index >= 0) |
811 | 1 | av_strlcatf(name, sizeof(name), "%d", dp->index); | |
812 |
1/2✓ Branch 0 taken 6389 times.
✗ Branch 1 not taken.
|
6389 | else if (dp->parent_name) |
813 | 6389 | av_strlcat(name, dp->parent_name, sizeof(name)); | |
814 | |||
815 |
2/2✓ Branch 0 taken 6389 times.
✓ Branch 1 taken 1 times.
|
6390 | if (dp->dec_ctx) |
816 | 6389 | av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name); | |
817 | |||
818 | 6390 | ff_thread_setname(name); | |
819 | 6390 | } | |
820 | |||
821 | 6390 | static void dec_thread_uninit(DecThreadContext *dt) | |
822 | { | ||
823 | 6390 | av_packet_free(&dt->pkt); | |
824 | 6390 | av_frame_free(&dt->frame); | |
825 | |||
826 | 6390 | memset(dt, 0, sizeof(*dt)); | |
827 | 6390 | } | |
828 | |||
829 | 6390 | static int dec_thread_init(DecThreadContext *dt) | |
830 | { | ||
831 | 6390 | memset(dt, 0, sizeof(*dt)); | |
832 | |||
833 | 6390 | dt->frame = av_frame_alloc(); | |
834 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dt->frame) |
835 | ✗ | goto fail; | |
836 | |||
837 | 6390 | dt->pkt = av_packet_alloc(); | |
838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dt->pkt) |
839 | ✗ | goto fail; | |
840 | |||
841 | 6390 | return 0; | |
842 | |||
843 | ✗ | fail: | |
844 | ✗ | dec_thread_uninit(dt); | |
845 | ✗ | return AVERROR(ENOMEM); | |
846 | } | ||
847 | |||
848 | 6390 | static int decoder_thread(void *arg) | |
849 | { | ||
850 | 6390 | DecoderPriv *dp = arg; | |
851 | DecThreadContext dt; | ||
852 | 6390 | int ret = 0, input_status = 0; | |
853 | |||
854 | 6390 | ret = dec_thread_init(&dt); | |
855 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret < 0) |
856 | ✗ | goto finish; | |
857 | |||
858 | 6390 | dec_thread_set_name(dp); | |
859 | |||
860 |
1/2✓ Branch 0 taken 364086 times.
✗ Branch 1 not taken.
|
364086 | while (!input_status) { |
861 | int flush_buffers, have_data; | ||
862 | |||
863 | 364086 | input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt); | |
864 |
2/2✓ Branch 0 taken 360827 times.
✓ Branch 1 taken 3259 times.
|
724913 | have_data = input_status >= 0 && |
865 |
3/4✓ Branch 0 taken 958 times.
✓ Branch 1 taken 359869 times.
✓ Branch 2 taken 958 times.
✗ Branch 3 not taken.
|
360827 | (dt.pkt->buf || dt.pkt->side_data_elems || |
866 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 948 times.
|
958 | (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT || |
867 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION); |
868 |
4/4✓ Branch 0 taken 360827 times.
✓ Branch 1 taken 3259 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 360821 times.
|
364086 | flush_buffers = input_status >= 0 && !have_data; |
869 |
2/2✓ Branch 0 taken 3265 times.
✓ Branch 1 taken 360821 times.
|
364086 | if (!have_data) |
870 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3259 times.
|
3265 | av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n", |
871 | flush_buffers ? "flush" : "EOF"); | ||
872 | |||
873 | // this is a standalone decoder that has not been initialized yet | ||
874 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 364085 times.
|
364086 | if (!dp->dec_ctx) { |
875 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (flush_buffers) |
876 | ✗ | continue; | |
877 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (input_status < 0) { |
878 | ✗ | av_log(dp, AV_LOG_ERROR, | |
879 | "Cannot initialize a standalone decoder\n"); | ||
880 | ✗ | ret = input_status; | |
881 | ✗ | goto finish; | |
882 | } | ||
883 | |||
884 | 1 | ret = dec_standalone_open(dp, dt.pkt); | |
885 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
886 | ✗ | goto finish; | |
887 | } | ||
888 | |||
889 |
2/2✓ Branch 0 taken 360821 times.
✓ Branch 1 taken 3265 times.
|
364086 | ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame); |
890 | |||
891 | 364086 | av_packet_unref(dt.pkt); | |
892 | 364086 | av_frame_unref(dt.frame); | |
893 | |||
894 | // AVERROR_EOF - EOF from the decoder | ||
895 | // AVERROR_EXIT - EOF from the scheduler | ||
896 | // we treat them differently when flushing | ||
897 |
2/2✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 360954 times.
|
364086 | if (ret == AVERROR_EXIT) { |
898 | 3132 | ret = AVERROR_EOF; | |
899 | 3132 | flush_buffers = 0; | |
900 | } | ||
901 | |||
902 |
2/2✓ Branch 0 taken 6396 times.
✓ Branch 1 taken 357690 times.
|
364086 | if (ret == AVERROR_EOF) { |
903 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6390 times.
|
6396 | av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n", |
904 | flush_buffers ? "resetting" : "finishing"); | ||
905 | |||
906 |
2/2✓ Branch 0 taken 6390 times.
✓ Branch 1 taken 6 times.
|
6396 | if (!flush_buffers) |
907 | 6390 | break; | |
908 | |||
909 | /* report last frame duration to the scheduler */ | ||
910 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
911 | 3 | dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est; | |
912 | 3 | dt.pkt->time_base = dp->last_frame_tb; | |
913 | } | ||
914 | |||
915 | 6 | avcodec_flush_buffers(dp->dec_ctx); | |
916 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 357690 times.
|
357690 | } else if (ret < 0) { |
917 | ✗ | av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n", | |
918 | ✗ | av_err2str(ret)); | |
919 | ✗ | break; | |
920 | } | ||
921 | } | ||
922 | |||
923 | // EOF is normal thread termination | ||
924 |
1/2✓ Branch 0 taken 6390 times.
✗ Branch 1 not taken.
|
6390 | if (ret == AVERROR_EOF) |
925 | 6390 | ret = 0; | |
926 | |||
927 | // on success send EOF timestamp to our downstreams | ||
928 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret >= 0) { |
929 | float err_rate; | ||
930 | |||
931 | 6390 | av_frame_unref(dt.frame); | |
932 | |||
933 | 6390 | dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF; | |
934 |
2/2✓ Branch 0 taken 6347 times.
✓ Branch 1 taken 43 times.
|
6390 | dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE : |
935 | 6347 | dp->last_frame_pts + dp->last_frame_duration_est; | |
936 | 6390 | dt.frame->time_base = dp->last_frame_tb; | |
937 | |||
938 | 6390 | ret = sch_dec_send(dp->sch, dp->sch_idx, dt.frame); | |
939 |
3/4✓ Branch 0 taken 3141 times.
✓ Branch 1 taken 3249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3141 times.
|
6390 | if (ret < 0 && ret != AVERROR_EOF) { |
940 | ✗ | av_log(dp, AV_LOG_FATAL, | |
941 | ✗ | "Error signalling EOF timestamp: %s\n", av_err2str(ret)); | |
942 | ✗ | goto finish; | |
943 | } | ||
944 | 6390 | ret = 0; | |
945 | |||
946 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ? |
947 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6387 times.
|
6393 | dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f; |
948 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6389 times.
|
6390 | if (err_rate > max_error_rate) { |
949 | 1 | av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n", | |
950 | err_rate, max_error_rate); | ||
951 | 1 | ret = FFMPEG_ERROR_RATE_EXCEEDED; | |
952 |
2/2✓ Branch 0 taken 6388 times.
✓ Branch 1 taken 1 times.
|
6389 | } else if (err_rate) |
953 | 1 | av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate); | |
954 | } | ||
955 | |||
956 | 6388 | finish: | |
957 | 6390 | dec_thread_uninit(&dt); | |
958 | |||
959 | 6390 | return ret; | |
960 | } | ||
961 | |||
962 | 953 | static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) | |
963 | { | ||
964 | 953 | DecoderPriv *dp = s->opaque; | |
965 | const enum AVPixelFormat *p; | ||
966 | |||
967 |
1/2✓ Branch 0 taken 2614 times.
✗ Branch 1 not taken.
|
2614 | for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { |
968 | 2614 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); | |
969 | 2614 | const AVCodecHWConfig *config = NULL; | |
970 | |||
971 |
2/2✓ Branch 0 taken 953 times.
✓ Branch 1 taken 1661 times.
|
2614 | if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) |
972 | 953 | break; | |
973 | |||
974 |
1/2✓ Branch 0 taken 1661 times.
✗ Branch 1 not taken.
|
1661 | if (dp->hwaccel_id == HWACCEL_GENERIC || |
975 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1661 times.
|
1661 | dp->hwaccel_id == HWACCEL_AUTO) { |
976 | ✗ | for (int i = 0;; i++) { | |
977 | ✗ | config = avcodec_get_hw_config(s->codec, i); | |
978 | ✗ | if (!config) | |
979 | ✗ | break; | |
980 | ✗ | if (!(config->methods & | |
981 | AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) | ||
982 | ✗ | continue; | |
983 | ✗ | if (config->pix_fmt == *p) | |
984 | ✗ | break; | |
985 | } | ||
986 | } | ||
987 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1661 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1661 | if (config && config->device_type == dp->hwaccel_device_type) { |
988 | ✗ | dp->hwaccel_pix_fmt = *p; | |
989 | ✗ | break; | |
990 | } | ||
991 | } | ||
992 | |||
993 | 953 | return *p; | |
994 | } | ||
995 | |||
996 | 6390 | static HWDevice *hw_device_match_by_codec(const AVCodec *codec) | |
997 | { | ||
998 | const AVCodecHWConfig *config; | ||
999 | HWDevice *dev; | ||
1000 | 6390 | for (int i = 0;; i++) { | |
1001 | 8043 | config = avcodec_get_hw_config(codec, i); | |
1002 |
2/2✓ Branch 0 taken 6390 times.
✓ Branch 1 taken 1653 times.
|
8043 | if (!config) |
1003 | 6390 | return NULL; | |
1004 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
|
1653 | if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) |
1005 | ✗ | continue; | |
1006 | 1653 | dev = hw_device_get_by_type(config->device_type); | |
1007 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
|
1653 | if (dev) |
1008 | ✗ | return dev; | |
1009 | } | ||
1010 | } | ||
1011 | |||
1012 | 6390 | static int hw_device_setup_for_decode(DecoderPriv *dp, | |
1013 | const AVCodec *codec, | ||
1014 | const char *hwaccel_device) | ||
1015 | { | ||
1016 | const AVCodecHWConfig *config; | ||
1017 | enum AVHWDeviceType type; | ||
1018 | 6390 | HWDevice *dev = NULL; | |
1019 | 6390 | int err, auto_device = 0; | |
1020 | |||
1021 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (hwaccel_device) { |
1022 | ✗ | dev = hw_device_get_by_name(hwaccel_device); | |
1023 | ✗ | if (!dev) { | |
1024 | ✗ | if (dp->hwaccel_id == HWACCEL_AUTO) { | |
1025 | ✗ | auto_device = 1; | |
1026 | ✗ | } else if (dp->hwaccel_id == HWACCEL_GENERIC) { | |
1027 | ✗ | type = dp->hwaccel_device_type; | |
1028 | ✗ | err = hw_device_init_from_type(type, hwaccel_device, | |
1029 | &dev); | ||
1030 | } else { | ||
1031 | // This will be dealt with by API-specific initialisation | ||
1032 | // (using hwaccel_device), so nothing further needed here. | ||
1033 | ✗ | return 0; | |
1034 | } | ||
1035 | } else { | ||
1036 | ✗ | if (dp->hwaccel_id == HWACCEL_AUTO) { | |
1037 | ✗ | dp->hwaccel_device_type = dev->type; | |
1038 | ✗ | } else if (dp->hwaccel_device_type != dev->type) { | |
1039 | ✗ | av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device " | |
1040 | "specified for decoder: device %s of type %s is not " | ||
1041 | ✗ | "usable with hwaccel %s.\n", dev->name, | |
1042 | ✗ | av_hwdevice_get_type_name(dev->type), | |
1043 | av_hwdevice_get_type_name(dp->hwaccel_device_type)); | ||
1044 | ✗ | return AVERROR(EINVAL); | |
1045 | } | ||
1046 | } | ||
1047 | } else { | ||
1048 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (dp->hwaccel_id == HWACCEL_AUTO) { |
1049 | ✗ | auto_device = 1; | |
1050 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | } else if (dp->hwaccel_id == HWACCEL_GENERIC) { |
1051 | ✗ | type = dp->hwaccel_device_type; | |
1052 | ✗ | dev = hw_device_get_by_type(type); | |
1053 | |||
1054 | // When "-qsv_device device" is used, an internal QSV device named | ||
1055 | // as "__qsv_device" is created. Another QSV device is created too | ||
1056 | // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices | ||
1057 | // if both "-qsv_device device" and "-init_hw_device qsv=name:device" | ||
1058 | // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL. | ||
1059 | // To keep back-compatibility with the removed ad-hoc libmfx setup code, | ||
1060 | // call hw_device_get_by_name("__qsv_device") to select the internal QSV | ||
1061 | // device. | ||
1062 | ✗ | if (!dev && type == AV_HWDEVICE_TYPE_QSV) | |
1063 | ✗ | dev = hw_device_get_by_name("__qsv_device"); | |
1064 | |||
1065 | ✗ | if (!dev) | |
1066 | ✗ | err = hw_device_init_from_type(type, NULL, &dev); | |
1067 | } else { | ||
1068 | 6390 | dev = hw_device_match_by_codec(codec); | |
1069 |
1/2✓ Branch 0 taken 6390 times.
✗ Branch 1 not taken.
|
6390 | if (!dev) { |
1070 | // No device for this codec, but not using generic hwaccel | ||
1071 | // and therefore may well not need one - ignore. | ||
1072 | 6390 | return 0; | |
1073 | } | ||
1074 | } | ||
1075 | } | ||
1076 | |||
1077 | ✗ | if (auto_device) { | |
1078 | ✗ | if (!avcodec_get_hw_config(codec, 0)) { | |
1079 | // Decoder does not support any hardware devices. | ||
1080 | ✗ | return 0; | |
1081 | } | ||
1082 | ✗ | for (int i = 0; !dev; i++) { | |
1083 | ✗ | config = avcodec_get_hw_config(codec, i); | |
1084 | ✗ | if (!config) | |
1085 | ✗ | break; | |
1086 | ✗ | type = config->device_type; | |
1087 | ✗ | dev = hw_device_get_by_type(type); | |
1088 | ✗ | if (dev) { | |
1089 | ✗ | av_log(dp, AV_LOG_INFO, "Using auto " | |
1090 | "hwaccel type %s with existing device %s.\n", | ||
1091 | ✗ | av_hwdevice_get_type_name(type), dev->name); | |
1092 | } | ||
1093 | } | ||
1094 | ✗ | for (int i = 0; !dev; i++) { | |
1095 | ✗ | config = avcodec_get_hw_config(codec, i); | |
1096 | ✗ | if (!config) | |
1097 | ✗ | break; | |
1098 | ✗ | type = config->device_type; | |
1099 | // Try to make a new device of this type. | ||
1100 | ✗ | err = hw_device_init_from_type(type, hwaccel_device, | |
1101 | &dev); | ||
1102 | ✗ | if (err < 0) { | |
1103 | // Can't make a device of this type. | ||
1104 | ✗ | continue; | |
1105 | } | ||
1106 | ✗ | if (hwaccel_device) { | |
1107 | ✗ | av_log(dp, AV_LOG_INFO, "Using auto " | |
1108 | "hwaccel type %s with new device created " | ||
1109 | "from %s.\n", av_hwdevice_get_type_name(type), | ||
1110 | hwaccel_device); | ||
1111 | } else { | ||
1112 | ✗ | av_log(dp, AV_LOG_INFO, "Using auto " | |
1113 | "hwaccel type %s with new default device.\n", | ||
1114 | av_hwdevice_get_type_name(type)); | ||
1115 | } | ||
1116 | } | ||
1117 | ✗ | if (dev) { | |
1118 | ✗ | dp->hwaccel_device_type = type; | |
1119 | } else { | ||
1120 | ✗ | av_log(dp, AV_LOG_INFO, "Auto hwaccel " | |
1121 | "disabled: no device found.\n"); | ||
1122 | ✗ | dp->hwaccel_id = HWACCEL_NONE; | |
1123 | ✗ | return 0; | |
1124 | } | ||
1125 | } | ||
1126 | |||
1127 | ✗ | if (!dev) { | |
1128 | ✗ | av_log(dp, AV_LOG_ERROR, "No device available " | |
1129 | "for decoder: device type %s needed for codec %s.\n", | ||
1130 | ✗ | av_hwdevice_get_type_name(type), codec->name); | |
1131 | ✗ | return err; | |
1132 | } | ||
1133 | |||
1134 | ✗ | dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); | |
1135 | ✗ | if (!dp->dec_ctx->hw_device_ctx) | |
1136 | ✗ | return AVERROR(ENOMEM); | |
1137 | |||
1138 | ✗ | return 0; | |
1139 | } | ||
1140 | |||
1141 | 6390 | static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, | |
1142 | const DecoderOpts *o, AVFrame *param_out) | ||
1143 | { | ||
1144 | 6390 | const AVCodec *codec = o->codec; | |
1145 | int ret; | ||
1146 | |||
1147 | 6390 | dp->flags = o->flags; | |
1148 | 6390 | dp->log_parent = o->log_parent; | |
1149 | |||
1150 | 6390 | dp->dec.type = codec->type; | |
1151 | 6390 | dp->framerate_in = o->framerate; | |
1152 | |||
1153 | 6390 | dp->hwaccel_id = o->hwaccel_id; | |
1154 | 6390 | dp->hwaccel_device_type = o->hwaccel_device_type; | |
1155 | 6390 | dp->hwaccel_output_format = o->hwaccel_output_format; | |
1156 | |||
1157 | 6390 | snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name); | |
1158 | |||
1159 |
1/2✓ Branch 0 taken 6390 times.
✗ Branch 1 not taken.
|
6390 | dp->parent_name = av_strdup(o->name ? o->name : ""); |
1160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dp->parent_name) |
1161 | ✗ | return AVERROR(ENOMEM); | |
1162 | |||
1163 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6350 times.
|
6390 | if (codec->type == AVMEDIA_TYPE_SUBTITLE && |
1164 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
|
40 | (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) { |
1165 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) { |
1166 | 4 | dp->sub_prev[i] = av_frame_alloc(); | |
1167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!dp->sub_prev[i]) |
1168 | ✗ | return AVERROR(ENOMEM); | |
1169 | } | ||
1170 | 2 | dp->sub_heartbeat = av_frame_alloc(); | |
1171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!dp->sub_heartbeat) |
1172 | ✗ | return AVERROR(ENOMEM); | |
1173 | } | ||
1174 | |||
1175 | 6390 | dp->sar_override = o->par->sample_aspect_ratio; | |
1176 | |||
1177 | 6390 | dp->dec_ctx = avcodec_alloc_context3(codec); | |
1178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (!dp->dec_ctx) |
1179 | ✗ | return AVERROR(ENOMEM); | |
1180 | |||
1181 | 6390 | ret = avcodec_parameters_to_context(dp->dec_ctx, o->par); | |
1182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret < 0) { |
1183 | ✗ | av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n"); | |
1184 | ✗ | return ret; | |
1185 | } | ||
1186 | |||
1187 | 6390 | dp->dec_ctx->opaque = dp; | |
1188 | 6390 | dp->dec_ctx->get_format = get_format; | |
1189 | 6390 | dp->dec_ctx->pkt_timebase = o->time_base; | |
1190 | |||
1191 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 2 taken 6333 times.
|
6390 | if (!av_dict_get(*dec_opts, "threads", NULL, 0)) |
1192 | 57 | av_dict_set(dec_opts, "threads", "auto", 0); | |
1193 | |||
1194 | 6390 | av_dict_set(dec_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY); | |
1195 | |||
1196 | 6390 | ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device); | |
1197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret < 0) { |
1198 | ✗ | av_log(dp, AV_LOG_ERROR, | |
1199 | "Hardware device setup failed for decoder: %s\n", | ||
1200 | ✗ | av_err2str(ret)); | |
1201 | ✗ | return ret; | |
1202 | } | ||
1203 | |||
1204 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6390 times.
|
6390 | if ((ret = avcodec_open2(dp->dec_ctx, codec, dec_opts)) < 0) { |
1205 | ✗ | av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n", | |
1206 | ✗ | av_err2str(ret)); | |
1207 | ✗ | return ret; | |
1208 | } | ||
1209 | |||
1210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (dp->dec_ctx->hw_device_ctx) { |
1211 | // Update decoder extra_hw_frames option to account for the | ||
1212 | // frames held in queues inside the ffmpeg utility. This is | ||
1213 | // called after avcodec_open2() because the user-set value of | ||
1214 | // extra_hw_frames becomes valid in there, and we need to add | ||
1215 | // this on top of it. | ||
1216 | ✗ | int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE; | |
1217 | ✗ | if (dp->dec_ctx->extra_hw_frames >= 0) | |
1218 | ✗ | dp->dec_ctx->extra_hw_frames += extra_frames; | |
1219 | else | ||
1220 | ✗ | dp->dec_ctx->extra_hw_frames = extra_frames; | |
1221 | } | ||
1222 | |||
1223 | 6390 | ret = check_avoptions(*dec_opts); | |
1224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
|
6390 | if (ret < 0) |
1225 | ✗ | return ret; | |
1226 | |||
1227 | 6390 | dp->dec.subtitle_header = dp->dec_ctx->subtitle_header; | |
1228 | 6390 | dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size; | |
1229 | |||
1230 |
2/2✓ Branch 0 taken 6389 times.
✓ Branch 1 taken 1 times.
|
6390 | if (param_out) { |
1231 |
2/2✓ Branch 0 taken 1199 times.
✓ Branch 1 taken 5190 times.
|
6389 | if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
1232 | 1199 | param_out->format = dp->dec_ctx->sample_fmt; | |
1233 | 1199 | param_out->sample_rate = dp->dec_ctx->sample_rate; | |
1234 | |||
1235 | 1199 | ret = av_channel_layout_copy(¶m_out->ch_layout, &dp->dec_ctx->ch_layout); | |
1236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1199 times.
|
1199 | if (ret < 0) |
1237 | ✗ | return ret; | |
1238 |
2/2✓ Branch 0 taken 5150 times.
✓ Branch 1 taken 40 times.
|
5190 | } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
1239 | 5150 | param_out->format = dp->dec_ctx->pix_fmt; | |
1240 | 5150 | param_out->width = dp->dec_ctx->width; | |
1241 | 5150 | param_out->height = dp->dec_ctx->height; | |
1242 | 5150 | param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio; | |
1243 | 5150 | param_out->colorspace = dp->dec_ctx->colorspace; | |
1244 | 5150 | param_out->color_range = dp->dec_ctx->color_range; | |
1245 | } | ||
1246 | |||
1247 | 6389 | param_out->time_base = dp->dec_ctx->pkt_timebase; | |
1248 | } | ||
1249 | |||
1250 | 6390 | return 0; | |
1251 | } | ||
1252 | |||
1253 | 6389 | int dec_init(Decoder **pdec, Scheduler *sch, | |
1254 | AVDictionary **dec_opts, const DecoderOpts *o, | ||
1255 | AVFrame *param_out) | ||
1256 | { | ||
1257 | DecoderPriv *dp; | ||
1258 | int ret; | ||
1259 | |||
1260 | 6389 | *pdec = NULL; | |
1261 | |||
1262 | 6389 | ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS)); | |
1263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6389 times.
|
6389 | if (ret < 0) |
1264 | ✗ | return ret; | |
1265 | |||
1266 | 6389 | ret = dec_open(dp, dec_opts, o, param_out); | |
1267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6389 times.
|
6389 | if (ret < 0) |
1268 | ✗ | goto fail; | |
1269 | |||
1270 | 6389 | *pdec = &dp->dec; | |
1271 | |||
1272 | 6389 | return dp->sch_idx; | |
1273 | ✗ | fail: | |
1274 | ✗ | dec_free((Decoder**)&dp); | |
1275 | ✗ | return ret; | |
1276 | } | ||
1277 | |||
1278 | 1 | int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch) | |
1279 | { | ||
1280 | DecoderPriv *dp; | ||
1281 | |||
1282 | OutputFile *of; | ||
1283 | OutputStream *ost; | ||
1284 | int of_index, ost_index; | ||
1285 | char *p; | ||
1286 | |||
1287 | unsigned enc_idx; | ||
1288 | int ret; | ||
1289 | |||
1290 | 1 | ret = dec_alloc(&dp, sch, 0); | |
1291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
1292 | ✗ | return ret; | |
1293 | |||
1294 | 1 | dp->index = nb_decoders; | |
1295 | |||
1296 | 1 | ret = GROW_ARRAY(decoders, nb_decoders); | |
1297 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
1298 | ✗ | dec_free((Decoder **)&dp); | |
1299 | ✗ | return ret; | |
1300 | } | ||
1301 | |||
1302 | 1 | decoders[nb_decoders - 1] = (Decoder *)dp; | |
1303 | |||
1304 | 1 | of_index = strtol(arg, &p, 0); | |
1305 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (of_index < 0 || of_index >= nb_output_files) { |
1306 | ✗ | av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg); | |
1307 | ✗ | return AVERROR(EINVAL); | |
1308 | } | ||
1309 | 1 | of = output_files[of_index]; | |
1310 | |||
1311 | 1 | ost_index = strtol(p + 1, NULL, 0); | |
1312 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (ost_index < 0 || ost_index >= of->nb_streams) { |
1313 | ✗ | av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg); | |
1314 | ✗ | return AVERROR(EINVAL); | |
1315 | } | ||
1316 | 1 | ost = of->streams[ost_index]; | |
1317 | |||
1318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ost->enc) { |
1319 | ✗ | av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg); | |
1320 | ✗ | return AVERROR(EINVAL); | |
1321 | } | ||
1322 | |||
1323 | 1 | dp->dec.type = ost->type; | |
1324 | |||
1325 | 1 | ret = enc_loopback(ost->enc); | |
1326 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
1327 | ✗ | return ret; | |
1328 | 1 | enc_idx = ret; | |
1329 | |||
1330 | 1 | ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC(dp->sch_idx)); | |
1331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
1332 | ✗ | return ret; | |
1333 | |||
1334 | 1 | ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0); | |
1335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
1336 | ✗ | return ret; | |
1337 | |||
1338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (o->codec_names.nb_opt) { |
1339 | ✗ | const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str; | |
1340 | ✗ | dp->standalone_init.codec = avcodec_find_decoder_by_name(name); | |
1341 | ✗ | if (!dp->standalone_init.codec) { | |
1342 | ✗ | av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name); | |
1343 | ✗ | return AVERROR_DECODER_NOT_FOUND; | |
1344 | } | ||
1345 | } | ||
1346 | |||
1347 | 1 | return 0; | |
1348 | } | ||
1349 | |||
1350 | 1 | int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts) | |
1351 | { | ||
1352 | 1 | DecoderPriv *dp = dp_from_dec(d); | |
1353 | char name[16]; | ||
1354 | |||
1355 | 1 | snprintf(name, sizeof(name), "dec%d", dp->index); | |
1356 | 1 | opts->name = av_strdup(name); | |
1357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!opts->name) |
1358 | ✗ | return AVERROR(ENOMEM); | |
1359 | |||
1360 | 1 | return dp->sch_idx; | |
1361 | } | ||
1362 |