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