2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/error.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/pixfmt.h"
30 #include "libavutil/stereo3d.h"
31 #include "libavutil/time.h"
32 #include "libavutil/timestamp.h"
34 #include "libavcodec/avcodec.h"
35 #include "libavcodec/codec.h"
39 typedef struct DecoderPriv
{
42 AVCodecContext
*dec_ctx
;
45 AVFrame
*frame_tmp_ref
;
48 // override output video sample aspect ratio with this value
49 AVRational sar_override
;
51 AVRational framerate_in
;
53 // a combination of DECODER_FLAG_*, provided to dec_open()
57 enum AVPixelFormat hwaccel_pix_fmt
;
58 enum HWAccelID hwaccel_id
;
59 enum AVHWDeviceType hwaccel_device_type
;
60 enum AVPixelFormat hwaccel_output_format
;
62 // pts/estimated duration of the last decoded frame
63 // * in decoder timebase for video,
64 // * in last_frame_tb (may change during decoding) for audio
65 int64_t last_frame_pts
;
66 int64_t last_frame_duration_est
;
67 AVRational last_frame_tb
;
68 int64_t last_filter_in_rescale_delta
;
69 int last_frame_sample_rate
;
71 /* previous decoded subtitles */
73 AVFrame
*sub_heartbeat
;
78 // this decoder's index in decoders or -1
84 // user specified decoder multiview options manually
85 int multiview_user_config
;
91 int nb_views_requested
;
93 /* A map of view ID to decoder outputs.
94 * MUST NOT be accessed outside of get_format()/get_buffer() */
103 const AVCodec
*codec
;
107 static DecoderPriv
*dp_from_dec(Decoder
*d
)
109 return (DecoderPriv
*)d
;
112 // data that is local to the decoder thread and not visible outside of it
113 typedef struct DecThreadContext
{
118 void dec_free(Decoder
**pdec
)
120 Decoder
*dec
= *pdec
;
125 dp
= dp_from_dec(dec
);
127 avcodec_free_context(&dp
->dec_ctx
);
129 av_frame_free(&dp
->frame
);
130 av_frame_free(&dp
->frame_tmp_ref
);
131 av_packet_free(&dp
->pkt
);
133 av_dict_free(&dp
->standalone_init
.opts
);
135 for (int i
= 0; i
< FF_ARRAY_ELEMS(dp
->sub_prev
); i
++)
136 av_frame_free(&dp
->sub_prev
[i
]);
137 av_frame_free(&dp
->sub_heartbeat
);
139 av_freep(&dp
->parent_name
);
141 av_freep(&dp
->views_requested
);
142 av_freep(&dp
->view_map
);
147 static const char *dec_item_name(void *obj
)
149 const DecoderPriv
*dp
= obj
;
154 static const AVClass dec_class
= {
155 .class_name
= "Decoder",
156 .version
= LIBAVUTIL_VERSION_INT
,
157 .parent_log_context_offset
= offsetof(DecoderPriv
, log_parent
),
158 .item_name
= dec_item_name
,
161 static int decoder_thread(void *arg
);
163 static int dec_alloc(DecoderPriv
**pdec
, Scheduler
*sch
, int send_end_ts
)
170 dp
= av_mallocz(sizeof(*dp
));
172 return AVERROR(ENOMEM
);
174 dp
->frame
= av_frame_alloc();
178 dp
->pkt
= av_packet_alloc();
183 dp
->dec
.class = &dec_class
;
184 dp
->last_filter_in_rescale_delta
= AV_NOPTS_VALUE
;
185 dp
->last_frame_pts
= AV_NOPTS_VALUE
;
186 dp
->last_frame_tb
= (AVRational
){ 1, 1 };
187 dp
->hwaccel_pix_fmt
= AV_PIX_FMT_NONE
;
189 ret
= sch_add_dec(sch
, decoder_thread
, dp
, send_end_ts
);
199 dec_free((Decoder
**)&dp
);
200 return ret
>= 0 ? AVERROR(ENOMEM
) : ret
;
203 static AVRational
audio_samplerate_update(DecoderPriv
*dp
,
204 const AVFrame
*frame
)
206 const int prev
= dp
->last_frame_tb
.den
;
207 const int sr
= frame
->sample_rate
;
212 if (frame
->sample_rate
== dp
->last_frame_sample_rate
)
215 gcd
= av_gcd(prev
, sr
);
217 if (prev
/ gcd
>= INT_MAX
/ sr
) {
218 av_log(dp
, AV_LOG_WARNING
,
219 "Audio timestamps cannot be represented exactly after "
220 "sample rate change: %d -> %d\n", prev
, sr
);
222 // LCM of 192000, 44100, allows to represent all common samplerates
223 tb_new
= (AVRational
){ 1, 28224000 };
225 tb_new
= (AVRational
){ 1, prev
/ gcd
* sr
};
227 // keep the frame timebase if it is strictly better than
228 // the samplerate-defined one
229 if (frame
->time_base
.num
== 1 && frame
->time_base
.den
> tb_new
.den
&&
230 !(frame
->time_base
.den
% tb_new
.den
))
231 tb_new
= frame
->time_base
;
233 if (dp
->last_frame_pts
!= AV_NOPTS_VALUE
)
234 dp
->last_frame_pts
= av_rescale_q(dp
->last_frame_pts
,
235 dp
->last_frame_tb
, tb_new
);
236 dp
->last_frame_duration_est
= av_rescale_q(dp
->last_frame_duration_est
,
237 dp
->last_frame_tb
, tb_new
);
239 dp
->last_frame_tb
= tb_new
;
240 dp
->last_frame_sample_rate
= frame
->sample_rate
;
243 return dp
->last_frame_tb
;
246 static void audio_ts_process(DecoderPriv
*dp
, AVFrame
*frame
)
248 AVRational tb_filter
= (AVRational
){1, frame
->sample_rate
};
252 // on samplerate change, choose a new internal timebase for timestamp
253 // generation that can represent timestamps from all the samplerates
255 tb
= audio_samplerate_update(dp
, frame
);
256 pts_pred
= dp
->last_frame_pts
== AV_NOPTS_VALUE
? 0 :
257 dp
->last_frame_pts
+ dp
->last_frame_duration_est
;
259 if (frame
->pts
== AV_NOPTS_VALUE
) {
260 frame
->pts
= pts_pred
;
261 frame
->time_base
= tb
;
262 } else if (dp
->last_frame_pts
!= AV_NOPTS_VALUE
&&
263 frame
->pts
> av_rescale_q_rnd(pts_pred
, tb
, frame
->time_base
,
265 // there was a gap in timestamps, reset conversion state
266 dp
->last_filter_in_rescale_delta
= AV_NOPTS_VALUE
;
269 frame
->pts
= av_rescale_delta(frame
->time_base
, frame
->pts
,
270 tb
, frame
->nb_samples
,
271 &dp
->last_filter_in_rescale_delta
, tb
);
273 dp
->last_frame_pts
= frame
->pts
;
274 dp
->last_frame_duration_est
= av_rescale_q(frame
->nb_samples
,
277 // finally convert to filtering timebase
278 frame
->pts
= av_rescale_q(frame
->pts
, tb
, tb_filter
);
279 frame
->duration
= frame
->nb_samples
;
280 frame
->time_base
= tb_filter
;
283 static int64_t video_duration_estimate(const DecoderPriv
*dp
, const AVFrame
*frame
)
285 const int ts_unreliable
= dp
->flags
& DECODER_FLAG_TS_UNRELIABLE
;
286 const int fr_forced
= dp
->flags
& DECODER_FLAG_FRAMERATE_FORCED
;
287 int64_t codec_duration
= 0;
288 // difference between this and last frame's timestamps
289 const int64_t ts_diff
=
290 (frame
->pts
!= AV_NOPTS_VALUE
&& dp
->last_frame_pts
!= AV_NOPTS_VALUE
) ?
291 frame
->pts
- dp
->last_frame_pts
: -1;
293 // XXX lavf currently makes up frame durations when they are not provided by
294 // the container. As there is no way to reliably distinguish real container
295 // durations from the fake made-up ones, we use heuristics based on whether
296 // the container has timestamps. Eventually lavf should stop making up
297 // durations, then this should be simplified.
299 // frame duration is unreliable (typically guessed by lavf) when it is equal
300 // to 1 and the actual duration of the last frame is more than 2x larger
301 const int duration_unreliable
= frame
->duration
== 1 && ts_diff
> 2 * frame
->duration
;
303 // prefer frame duration for containers with timestamps
305 (frame
->duration
> 0 && !ts_unreliable
&& !duration_unreliable
))
306 return frame
->duration
;
308 if (dp
->dec_ctx
->framerate
.den
&& dp
->dec_ctx
->framerate
.num
) {
309 int fields
= frame
->repeat_pict
+ 2;
310 AVRational field_rate
= av_mul_q(dp
->dec_ctx
->framerate
,
311 (AVRational
){ 2, 1 });
312 codec_duration
= av_rescale_q(fields
, av_inv_q(field_rate
),
316 // prefer codec-layer duration for containers without timestamps
317 if (codec_duration
> 0 && ts_unreliable
)
318 return codec_duration
;
320 // when timestamps are available, repeat last frame's actual duration
321 // (i.e. pts difference between this and last frame)
325 // try frame/codec duration
326 if (frame
->duration
> 0)
327 return frame
->duration
;
328 if (codec_duration
> 0)
329 return codec_duration
;
331 // try average framerate
332 if (dp
->framerate_in
.num
&& dp
->framerate_in
.den
) {
333 int64_t d
= av_rescale_q(1, av_inv_q(dp
->framerate_in
),
339 // last resort is last frame's estimated duration, and 1
340 return FFMAX(dp
->last_frame_duration_est
, 1);
343 static int hwaccel_retrieve_data(AVCodecContext
*avctx
, AVFrame
*input
)
345 DecoderPriv
*dp
= avctx
->opaque
;
346 AVFrame
*output
= NULL
;
347 enum AVPixelFormat output_format
= dp
->hwaccel_output_format
;
350 if (input
->format
== output_format
) {
355 output
= av_frame_alloc();
357 return AVERROR(ENOMEM
);
359 output
->format
= output_format
;
361 err
= av_hwframe_transfer_data(output
, input
, 0);
363 av_log(avctx
, AV_LOG_ERROR
, "Failed to transfer data to "
364 "output frame: %d.\n", err
);
368 err
= av_frame_copy_props(output
, input
);
370 av_frame_unref(output
);
374 av_frame_unref(input
);
375 av_frame_move_ref(input
, output
);
376 av_frame_free(&output
);
381 av_frame_free(&output
);
385 static int video_frame_process(DecoderPriv
*dp
, AVFrame
*frame
,
386 unsigned *outputs_mask
)
389 if (dp
->flags
& DECODER_FLAG_TOP_FIELD_FIRST
) {
390 av_log(dp
, AV_LOG_WARNING
, "-top is deprecated, use the setfield filter instead\n");
391 frame
->flags
|= AV_FRAME_FLAG_TOP_FIELD_FIRST
;
395 if (frame
->format
== dp
->hwaccel_pix_fmt
) {
396 int err
= hwaccel_retrieve_data(dp
->dec_ctx
, frame
);
401 frame
->pts
= frame
->best_effort_timestamp
;
403 // forced fixed framerate
404 if (dp
->flags
& DECODER_FLAG_FRAMERATE_FORCED
) {
405 frame
->pts
= AV_NOPTS_VALUE
;
407 frame
->time_base
= av_inv_q(dp
->framerate_in
);
410 // no timestamp available - extrapolate from previous frame duration
411 if (frame
->pts
== AV_NOPTS_VALUE
)
412 frame
->pts
= dp
->last_frame_pts
== AV_NOPTS_VALUE
? 0 :
413 dp
->last_frame_pts
+ dp
->last_frame_duration_est
;
415 // update timestamp history
416 dp
->last_frame_duration_est
= video_duration_estimate(dp
, frame
);
417 dp
->last_frame_pts
= frame
->pts
;
418 dp
->last_frame_tb
= frame
->time_base
;
421 av_log(dp
, AV_LOG_INFO
,
422 "decoder -> pts:%s pts_time:%s "
423 "pkt_dts:%s pkt_dts_time:%s "
424 "duration:%s duration_time:%s "
425 "keyframe:%d frame_type:%d time_base:%d/%d\n",
426 av_ts2str(frame
->pts
),
427 av_ts2timestr(frame
->pts
, &frame
->time_base
),
428 av_ts2str(frame
->pkt_dts
),
429 av_ts2timestr(frame
->pkt_dts
, &frame
->time_base
),
430 av_ts2str(frame
->duration
),
431 av_ts2timestr(frame
->duration
, &frame
->time_base
),
432 !!(frame
->flags
& AV_FRAME_FLAG_KEY
), frame
->pict_type
,
433 frame
->time_base
.num
, frame
->time_base
.den
);
436 if (dp
->sar_override
.num
)
437 frame
->sample_aspect_ratio
= dp
->sar_override
;
439 if (dp
->apply_cropping
) {
440 // lavfi does not require aligned frame data
441 int ret
= av_frame_apply_cropping(frame
, AV_FRAME_CROP_UNALIGNED
);
443 av_log(dp
, AV_LOG_ERROR
, "Error applying decoder cropping\n");
449 *outputs_mask
= (uintptr_t)frame
->opaque
;
454 static int copy_av_subtitle(AVSubtitle
*dst
, const AVSubtitle
*src
)
456 int ret
= AVERROR_BUG
;
458 .format
= src
->format
,
459 .start_display_time
= src
->start_display_time
,
460 .end_display_time
= src
->end_display_time
,
469 if (!(tmp
.rects
= av_calloc(src
->num_rects
, sizeof(*tmp
.rects
))))
470 return AVERROR(ENOMEM
);
472 for (int i
= 0; i
< src
->num_rects
; i
++) {
473 AVSubtitleRect
*src_rect
= src
->rects
[i
];
474 AVSubtitleRect
*dst_rect
;
476 if (!(dst_rect
= tmp
.rects
[i
] = av_mallocz(sizeof(*tmp
.rects
[0])))) {
477 ret
= AVERROR(ENOMEM
);
483 dst_rect
->type
= src_rect
->type
;
484 dst_rect
->flags
= src_rect
->flags
;
486 dst_rect
->x
= src_rect
->x
;
487 dst_rect
->y
= src_rect
->y
;
488 dst_rect
->w
= src_rect
->w
;
489 dst_rect
->h
= src_rect
->h
;
490 dst_rect
->nb_colors
= src_rect
->nb_colors
;
493 if (!(dst_rect
->text
= av_strdup(src_rect
->text
))) {
494 ret
= AVERROR(ENOMEM
);
499 if (!(dst_rect
->ass
= av_strdup(src_rect
->ass
))) {
500 ret
= AVERROR(ENOMEM
);
504 for (int j
= 0; j
< 4; j
++) {
505 // SUBTITLE_BITMAP images are special in the sense that they
506 // are like PAL8 images. first pointer to data, second to
507 // palette. This makes the size calculation match this.
508 size_t buf_size
= src_rect
->type
== SUBTITLE_BITMAP
&& j
== 1 ?
510 src_rect
->h
* src_rect
->linesize
[j
];
512 if (!src_rect
->data
[j
])
515 if (!(dst_rect
->data
[j
] = av_memdup(src_rect
->data
[j
], buf_size
))) {
516 ret
= AVERROR(ENOMEM
);
519 dst_rect
->linesize
[j
] = src_rect
->linesize
[j
];
529 avsubtitle_free(&tmp
);
534 static void subtitle_free(void *opaque
, uint8_t *data
)
536 AVSubtitle
*sub
= (AVSubtitle
*)data
;
537 avsubtitle_free(sub
);
541 static int subtitle_wrap_frame(AVFrame
*frame
, AVSubtitle
*subtitle
, int copy
)
548 sub
= av_mallocz(sizeof(*sub
));
549 ret
= sub
? copy_av_subtitle(sub
, subtitle
) : AVERROR(ENOMEM
);
555 sub
= av_memdup(subtitle
, sizeof(*subtitle
));
557 return AVERROR(ENOMEM
);
558 memset(subtitle
, 0, sizeof(*subtitle
));
561 buf
= av_buffer_create((uint8_t*)sub
, sizeof(*sub
),
562 subtitle_free
, NULL
, 0);
564 avsubtitle_free(sub
);
566 return AVERROR(ENOMEM
);
574 static int process_subtitle(DecoderPriv
*dp
, AVFrame
*frame
)
576 const AVSubtitle
*subtitle
= (AVSubtitle
*)frame
->buf
[0]->data
;
579 if (dp
->flags
& DECODER_FLAG_FIX_SUB_DURATION
) {
580 AVSubtitle
*sub_prev
= dp
->sub_prev
[0]->buf
[0] ?
581 (AVSubtitle
*)dp
->sub_prev
[0]->buf
[0]->data
: NULL
;
584 end
= av_rescale(subtitle
->pts
- sub_prev
->pts
,
586 if (end
< sub_prev
->end_display_time
) {
587 av_log(dp
, AV_LOG_DEBUG
,
588 "Subtitle duration reduced from %"PRId32
" to %d%s\n",
589 sub_prev
->end_display_time
, end
,
590 end
<= 0 ? ", dropping it" : "");
591 sub_prev
->end_display_time
= end
;
595 av_frame_unref(dp
->sub_prev
[1]);
596 av_frame_move_ref(dp
->sub_prev
[1], frame
);
598 frame
= dp
->sub_prev
[0];
599 subtitle
= frame
->buf
[0] ? (AVSubtitle
*)frame
->buf
[0]->data
: NULL
;
601 FFSWAP(AVFrame
*, dp
->sub_prev
[0], dp
->sub_prev
[1]);
610 ret
= sch_dec_send(dp
->sch
, dp
->sch_idx
, 0, frame
);
612 av_frame_unref(frame
);
614 return ret
== AVERROR_EOF
? AVERROR_EXIT
: ret
;
617 static int fix_sub_duration_heartbeat(DecoderPriv
*dp
, int64_t signal_pts
)
619 int ret
= AVERROR_BUG
;
620 AVSubtitle
*prev_subtitle
= dp
->sub_prev
[0]->buf
[0] ?
621 (AVSubtitle
*)dp
->sub_prev
[0]->buf
[0]->data
: NULL
;
622 AVSubtitle
*subtitle
;
624 if (!(dp
->flags
& DECODER_FLAG_FIX_SUB_DURATION
) || !prev_subtitle
||
625 !prev_subtitle
->num_rects
|| signal_pts
<= prev_subtitle
->pts
)
628 av_frame_unref(dp
->sub_heartbeat
);
629 ret
= subtitle_wrap_frame(dp
->sub_heartbeat
, prev_subtitle
, 1);
633 subtitle
= (AVSubtitle
*)dp
->sub_heartbeat
->buf
[0]->data
;
634 subtitle
->pts
= signal_pts
;
636 return process_subtitle(dp
, dp
->sub_heartbeat
);
639 static int transcode_subtitles(DecoderPriv
*dp
, const AVPacket
*pkt
,
642 AVPacket
*flush_pkt
= NULL
;
647 if (pkt
&& (intptr_t)pkt
->opaque
== PKT_OPAQUE_SUB_HEARTBEAT
) {
648 frame
->pts
= pkt
->pts
;
649 frame
->time_base
= pkt
->time_base
;
650 frame
->opaque
= (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT
;
652 ret
= sch_dec_send(dp
->sch
, dp
->sch_idx
, 0, frame
);
653 return ret
== AVERROR_EOF
? AVERROR_EXIT
: ret
;
654 } else if (pkt
&& (intptr_t)pkt
->opaque
== PKT_OPAQUE_FIX_SUB_DURATION
) {
655 return fix_sub_duration_heartbeat(dp
, av_rescale_q(pkt
->pts
, pkt
->time_base
,
660 flush_pkt
= av_packet_alloc();
662 return AVERROR(ENOMEM
);
665 ret
= avcodec_decode_subtitle2(dp
->dec_ctx
, &subtitle
, &got_output
,
666 pkt
? pkt
: flush_pkt
);
667 av_packet_free(&flush_pkt
);
670 av_log(dp
, AV_LOG_ERROR
, "Error decoding subtitles: %s\n",
672 dp
->dec
.decode_errors
++;
673 return exit_on_error
? ret
: 0;
677 return pkt
? 0 : AVERROR_EOF
;
679 dp
->dec
.frames_decoded
++;
681 // XXX the queue for transferring data to consumers runs
682 // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
684 // eventually, subtitles should be switched to use AVFrames natively
685 ret
= subtitle_wrap_frame(frame
, &subtitle
, 0);
687 avsubtitle_free(&subtitle
);
691 frame
->width
= dp
->dec_ctx
->width
;
692 frame
->height
= dp
->dec_ctx
->height
;
694 return process_subtitle(dp
, frame
);
697 static int packet_decode(DecoderPriv
*dp
, AVPacket
*pkt
, AVFrame
*frame
)
699 AVCodecContext
*dec
= dp
->dec_ctx
;
700 const char *type_desc
= av_get_media_type_string(dec
->codec_type
);
703 if (dec
->codec_type
== AVMEDIA_TYPE_SUBTITLE
)
704 return transcode_subtitles(dp
, pkt
, frame
);
706 // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
707 // reason. This seems like a semi-critical bug. Don't trigger EOF, and
709 if (pkt
&& pkt
->size
== 0)
712 if (pkt
&& (dp
->flags
& DECODER_FLAG_TS_UNRELIABLE
)) {
713 pkt
->pts
= AV_NOPTS_VALUE
;
714 pkt
->dts
= AV_NOPTS_VALUE
;
718 FrameData
*fd
= packet_data(pkt
);
720 return AVERROR(ENOMEM
);
721 fd
->wallclock
[LATENCY_PROBE_DEC_PRE
] = av_gettime_relative();
724 ret
= avcodec_send_packet(dec
, pkt
);
725 if (ret
< 0 && !(ret
== AVERROR_EOF
&& !pkt
)) {
726 // In particular, we don't expect AVERROR(EAGAIN), because we read all
727 // decoded frames with avcodec_receive_frame() until done.
728 if (ret
== AVERROR(EAGAIN
)) {
729 av_log(dp
, AV_LOG_FATAL
, "A decoder returned an unexpected error code. "
730 "This is a bug, please report it.\n");
733 av_log(dp
, AV_LOG_ERROR
, "Error submitting %s to decoder: %s\n",
734 pkt
? "packet" : "EOF", av_err2str(ret
));
736 if (ret
!= AVERROR_EOF
) {
737 dp
->dec
.decode_errors
++;
747 unsigned outputs_mask
= 1;
749 av_frame_unref(frame
);
751 update_benchmark(NULL
);
752 ret
= avcodec_receive_frame(dec
, frame
);
753 update_benchmark("decode_%s %s", type_desc
, dp
->parent_name
);
755 if (ret
== AVERROR(EAGAIN
)) {
756 av_assert0(pkt
); // should never happen during flushing
758 } else if (ret
== AVERROR_EOF
) {
760 } else if (ret
< 0) {
761 av_log(dp
, AV_LOG_ERROR
, "Decoding error: %s\n", av_err2str(ret
));
762 dp
->dec
.decode_errors
++;
770 if (frame
->decode_error_flags
|| (frame
->flags
& AV_FRAME_FLAG_CORRUPT
)) {
771 av_log(dp
, exit_on_error
? AV_LOG_FATAL
: AV_LOG_WARNING
,
772 "corrupt decoded frame\n");
774 return AVERROR_INVALIDDATA
;
777 fd
= frame_data(frame
);
779 av_frame_unref(frame
);
780 return AVERROR(ENOMEM
);
782 fd
->dec
.pts
= frame
->pts
;
783 fd
->dec
.tb
= dec
->pkt_timebase
;
784 fd
->dec
.frame_num
= dec
->frame_num
- 1;
785 fd
->bits_per_raw_sample
= dec
->bits_per_raw_sample
;
787 fd
->wallclock
[LATENCY_PROBE_DEC_POST
] = av_gettime_relative();
789 frame
->time_base
= dec
->pkt_timebase
;
791 if (dec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
792 dp
->dec
.samples_decoded
+= frame
->nb_samples
;
794 audio_ts_process(dp
, frame
);
796 ret
= video_frame_process(dp
, frame
, &outputs_mask
);
798 av_log(dp
, AV_LOG_FATAL
,
799 "Error while processing the decoded data\n");
804 dp
->dec
.frames_decoded
++;
806 for (int i
= 0; i
< stdc_count_ones(outputs_mask
); i
++) {
807 AVFrame
*to_send
= frame
;
810 av_assert0(outputs_mask
);
811 pos
= stdc_trailing_zeros(outputs_mask
);
812 outputs_mask
&= ~(1U << pos
);
814 // this is not the last output and sch_dec_send() consumes the frame
815 // given to it, so make a temporary reference
817 to_send
= dp
->frame_tmp_ref
;
818 ret
= av_frame_ref(to_send
, frame
);
823 ret
= sch_dec_send(dp
->sch
, dp
->sch_idx
, pos
, to_send
);
825 av_frame_unref(to_send
);
826 return ret
== AVERROR_EOF
? AVERROR_EXIT
: ret
;
832 static int dec_open(DecoderPriv
*dp
, AVDictionary
**dec_opts
,
833 const DecoderOpts
*o
, AVFrame
*param_out
);
835 static int dec_standalone_open(DecoderPriv
*dp
, const AVPacket
*pkt
)
841 if (!pkt
->opaque_ref
)
843 fd
= (FrameData
*)pkt
->opaque_ref
->data
;
848 memset(&o
, 0, sizeof(o
));
851 o
.time_base
= pkt
->time_base
;
853 o
.codec
= dp
->standalone_init
.codec
;
855 o
.codec
= avcodec_find_decoder(o
.par
->codec_id
);
857 const AVCodecDescriptor
*desc
= avcodec_descriptor_get(o
.par
->codec_id
);
859 av_log(dp
, AV_LOG_ERROR
, "Cannot find a decoder for codec ID '%s'\n",
860 desc
? desc
->name
: "?");
861 return AVERROR_DECODER_NOT_FOUND
;
864 snprintf(name
, sizeof(name
), "dec%d", dp
->index
);
867 return dec_open(dp
, &dp
->standalone_init
.opts
, &o
, NULL
);
870 static void dec_thread_set_name(const DecoderPriv
*dp
)
872 char name
[16] = "dec";
875 av_strlcatf(name
, sizeof(name
), "%d", dp
->index
);
876 else if (dp
->parent_name
)
877 av_strlcat(name
, dp
->parent_name
, sizeof(name
));
880 av_strlcatf(name
, sizeof(name
), ":%s", dp
->dec_ctx
->codec
->name
);
882 ff_thread_setname(name
);
885 static void dec_thread_uninit(DecThreadContext
*dt
)
887 av_packet_free(&dt
->pkt
);
888 av_frame_free(&dt
->frame
);
890 memset(dt
, 0, sizeof(*dt
));
893 static int dec_thread_init(DecThreadContext
*dt
)
895 memset(dt
, 0, sizeof(*dt
));
897 dt
->frame
= av_frame_alloc();
901 dt
->pkt
= av_packet_alloc();
908 dec_thread_uninit(dt
);
909 return AVERROR(ENOMEM
);
912 static int decoder_thread(void *arg
)
914 DecoderPriv
*dp
= arg
;
916 int ret
= 0, input_status
= 0;
918 ret
= dec_thread_init(&dt
);
922 dec_thread_set_name(dp
);
924 while (!input_status
) {
925 int flush_buffers
, have_data
;
927 input_status
= sch_dec_receive(dp
->sch
, dp
->sch_idx
, dt
.pkt
);
928 have_data
= input_status
>= 0 &&
929 (dt
.pkt
->buf
|| dt
.pkt
->side_data_elems
||
930 (intptr_t)dt
.pkt
->opaque
== PKT_OPAQUE_SUB_HEARTBEAT
||
931 (intptr_t)dt
.pkt
->opaque
== PKT_OPAQUE_FIX_SUB_DURATION
);
932 flush_buffers
= input_status
>= 0 && !have_data
;
934 av_log(dp
, AV_LOG_VERBOSE
, "Decoder thread received %s packet\n",
935 flush_buffers
? "flush" : "EOF");
937 // this is a standalone decoder that has not been initialized yet
941 if (input_status
< 0) {
942 av_log(dp
, AV_LOG_ERROR
,
943 "Cannot initialize a standalone decoder\n");
948 ret
= dec_standalone_open(dp
, dt
.pkt
);
953 ret
= packet_decode(dp
, have_data
? dt
.pkt
: NULL
, dt
.frame
);
955 av_packet_unref(dt
.pkt
);
956 av_frame_unref(dt
.frame
);
958 // AVERROR_EOF - EOF from the decoder
959 // AVERROR_EXIT - EOF from the scheduler
960 // we treat them differently when flushing
961 if (ret
== AVERROR_EXIT
) {
966 if (ret
== AVERROR_EOF
) {
967 av_log(dp
, AV_LOG_VERBOSE
, "Decoder returned EOF, %s\n",
968 flush_buffers
? "resetting" : "finishing");
973 /* report last frame duration to the scheduler */
974 if (dp
->dec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
975 dt
.pkt
->pts
= dp
->last_frame_pts
+ dp
->last_frame_duration_est
;
976 dt
.pkt
->time_base
= dp
->last_frame_tb
;
979 avcodec_flush_buffers(dp
->dec_ctx
);
980 } else if (ret
< 0) {
981 av_log(dp
, AV_LOG_ERROR
, "Error processing packet in decoder: %s\n",
987 // EOF is normal thread termination
988 if (ret
== AVERROR_EOF
)
991 // on success send EOF timestamp to our downstreams
995 av_frame_unref(dt
.frame
);
997 dt
.frame
->opaque
= (void*)(intptr_t)FRAME_OPAQUE_EOF
;
998 dt
.frame
->pts
= dp
->last_frame_pts
== AV_NOPTS_VALUE
? AV_NOPTS_VALUE
:
999 dp
->last_frame_pts
+ dp
->last_frame_duration_est
;
1000 dt
.frame
->time_base
= dp
->last_frame_tb
;
1002 ret
= sch_dec_send(dp
->sch
, dp
->sch_idx
, 0, dt
.frame
);
1003 if (ret
< 0 && ret
!= AVERROR_EOF
) {
1004 av_log(dp
, AV_LOG_FATAL
,
1005 "Error signalling EOF timestamp: %s\n", av_err2str(ret
));
1010 err_rate
= (dp
->dec
.frames_decoded
|| dp
->dec
.decode_errors
) ?
1011 dp
->dec
.decode_errors
/ (dp
->dec
.frames_decoded
+ dp
->dec
.decode_errors
) : 0.f
;
1012 if (err_rate
> max_error_rate
) {
1013 av_log(dp
, AV_LOG_FATAL
, "Decode error rate %g exceeds maximum %g\n",
1014 err_rate
, max_error_rate
);
1015 ret
= FFMPEG_ERROR_RATE_EXCEEDED
;
1016 } else if (err_rate
)
1017 av_log(dp
, AV_LOG_VERBOSE
, "Decode error rate %g\n", err_rate
);
1021 dec_thread_uninit(&dt
);
1026 int dec_request_view(Decoder
*d
, const ViewSpecifier
*vs
,
1029 DecoderPriv
*dp
= dp_from_dec(d
);
1030 unsigned out_idx
= 0;
1033 if (dp
->multiview_user_config
) {
1034 if (!vs
|| vs
->type
== VIEW_SPECIFIER_TYPE_NONE
) {
1035 *src
= SCH_DEC_OUT(dp
->sch_idx
, 0);
1039 av_log(dp
, AV_LOG_ERROR
,
1040 "Manually selecting views with -view_ids cannot be combined "
1041 "with view selection via stream specifiers. It is strongly "
1042 "recommended you always use stream specifiers only.\n");
1043 return AVERROR(EINVAL
);
1046 // when multiview_user_config is not set, NONE specifier is treated
1047 // as requesting the base view
1048 vs
= (vs
&& vs
->type
!= VIEW_SPECIFIER_TYPE_NONE
) ? vs
:
1049 &(ViewSpecifier
){ .type
= VIEW_SPECIFIER_TYPE_IDX
, .val
= 0 };
1051 // check if the specifier matches an already-existing one
1052 for (int i
= 0; i
< dp
->nb_views_requested
; i
++) {
1053 const ViewSpecifier
*vs1
= &dp
->views_requested
[i
].vs
;
1055 if (vs
->type
== vs1
->type
&&
1056 (vs
->type
== VIEW_SPECIFIER_TYPE_ALL
|| vs
->val
== vs1
->val
)) {
1057 *src
= SCH_DEC_OUT(dp
->sch_idx
, dp
->views_requested
[i
].out_idx
);
1062 // we use a bitmask to map view IDs to decoder outputs, which
1063 // limits the number of outputs allowed
1064 if (dp
->nb_views_requested
>= sizeof(dp
->view_map
[0].out_mask
) * 8) {
1065 av_log(dp
, AV_LOG_ERROR
, "Too many view specifiers\n");
1066 return AVERROR(ENOSYS
);
1069 ret
= GROW_ARRAY(dp
->views_requested
, dp
->nb_views_requested
);
1073 if (dp
->nb_views_requested
> 1) {
1074 ret
= sch_add_dec_output(dp
->sch
, dp
->sch_idx
);
1080 dp
->views_requested
[dp
->nb_views_requested
- 1].out_idx
= out_idx
;
1081 dp
->views_requested
[dp
->nb_views_requested
- 1].vs
= *vs
;
1083 *src
= SCH_DEC_OUT(dp
->sch_idx
,
1084 dp
->views_requested
[dp
->nb_views_requested
- 1].out_idx
);
1089 static int multiview_setup(DecoderPriv
*dp
, AVCodecContext
*dec_ctx
)
1091 unsigned views_wanted
= 0;
1093 unsigned nb_view_ids_av
, nb_view_ids
;
1094 unsigned *view_ids_av
= NULL
, *view_pos_av
= NULL
;
1095 int *view_ids
= NULL
;
1098 // no views/only base view were requested - do nothing
1099 if (!dp
->nb_views_requested
||
1100 (dp
->nb_views_requested
== 1 &&
1101 dp
->views_requested
[0].vs
.type
== VIEW_SPECIFIER_TYPE_IDX
&&
1102 dp
->views_requested
[0].vs
.val
== 0))
1105 av_freep(&dp
->view_map
);
1106 dp
->nb_view_map
= 0;
1108 // retrieve views available in current CVS
1109 ret
= av_opt_get_array_size(dec_ctx
, "view_ids_available",
1110 AV_OPT_SEARCH_CHILDREN
, &nb_view_ids_av
);
1112 av_log(dp
, AV_LOG_ERROR
,
1113 "Multiview decoding requested, but decoder '%s' does not "
1114 "support it\n", dec_ctx
->codec
->name
);
1115 return AVERROR(ENOSYS
);
1118 if (nb_view_ids_av
) {
1119 unsigned nb_view_pos_av
;
1121 if (nb_view_ids_av
>= sizeof(views_wanted
) * 8) {
1122 av_log(dp
, AV_LOG_ERROR
, "Too many views in video: %u\n", nb_view_ids_av
);
1123 ret
= AVERROR(ENOSYS
);
1127 view_ids_av
= av_calloc(nb_view_ids_av
, sizeof(*view_ids_av
));
1129 ret
= AVERROR(ENOMEM
);
1133 ret
= av_opt_get_array(dec_ctx
, "view_ids_available",
1134 AV_OPT_SEARCH_CHILDREN
, 0, nb_view_ids_av
,
1135 AV_OPT_TYPE_UINT
, view_ids_av
);
1139 ret
= av_opt_get_array_size(dec_ctx
, "view_pos_available",
1140 AV_OPT_SEARCH_CHILDREN
, &nb_view_pos_av
);
1141 if (ret
>= 0 && nb_view_pos_av
== nb_view_ids_av
) {
1142 view_pos_av
= av_calloc(nb_view_ids_av
, sizeof(*view_pos_av
));
1144 ret
= AVERROR(ENOMEM
);
1148 ret
= av_opt_get_array(dec_ctx
, "view_pos_available",
1149 AV_OPT_SEARCH_CHILDREN
, 0, nb_view_ids_av
,
1150 AV_OPT_TYPE_UINT
, view_pos_av
);
1155 // assume there is a single view with ID=0
1157 view_ids_av
= av_calloc(nb_view_ids_av
, sizeof(*view_ids_av
));
1158 view_pos_av
= av_calloc(nb_view_ids_av
, sizeof(*view_pos_av
));
1159 if (!view_ids_av
|| !view_pos_av
) {
1160 ret
= AVERROR(ENOMEM
);
1163 view_pos_av
[0] = AV_STEREO3D_VIEW_UNSPEC
;
1166 dp
->view_map
= av_calloc(nb_view_ids_av
, sizeof(*dp
->view_map
));
1167 if (!dp
->view_map
) {
1168 ret
= AVERROR(ENOMEM
);
1171 dp
->nb_view_map
= nb_view_ids_av
;
1173 for (int i
= 0; i
< dp
->nb_view_map
; i
++)
1174 dp
->view_map
[i
].id
= view_ids_av
[i
];
1176 // figure out which views should go to which output
1177 for (int i
= 0; i
< dp
->nb_views_requested
; i
++) {
1178 const ViewSpecifier
*vs
= &dp
->views_requested
[i
].vs
;
1181 case VIEW_SPECIFIER_TYPE_IDX
:
1182 if (vs
->val
>= nb_view_ids_av
) {
1183 av_log(dp
, exit_on_error
? AV_LOG_ERROR
: AV_LOG_WARNING
,
1184 "View with index %u requested, but only %u views available "
1185 "in current video sequence (more views may or may not be "
1186 "available in later sequences).\n",
1187 vs
->val
, nb_view_ids_av
);
1188 if (exit_on_error
) {
1189 ret
= AVERROR(EINVAL
);
1195 views_wanted
|= 1U << vs
->val
;
1196 dp
->view_map
[vs
->val
].out_mask
|= 1ULL << i
;
1199 case VIEW_SPECIFIER_TYPE_ID
: {
1202 for (unsigned j
= 0; j
< nb_view_ids_av
; j
++) {
1203 if (view_ids_av
[j
] == vs
->val
) {
1209 av_log(dp
, exit_on_error
? AV_LOG_ERROR
: AV_LOG_WARNING
,
1210 "View with ID %u requested, but is not available "
1211 "in the video sequence\n", vs
->val
);
1212 if (exit_on_error
) {
1213 ret
= AVERROR(EINVAL
);
1219 views_wanted
|= 1U << view_idx
;
1220 dp
->view_map
[view_idx
].out_mask
|= 1ULL << i
;
1224 case VIEW_SPECIFIER_TYPE_POS
: {
1227 for (unsigned j
= 0; view_pos_av
&& j
< nb_view_ids_av
; j
++) {
1228 if (view_pos_av
[j
] == vs
->val
) {
1234 av_log(dp
, exit_on_error
? AV_LOG_ERROR
: AV_LOG_WARNING
,
1235 "View position '%s' requested, but is not available "
1236 "in the video sequence\n", av_stereo3d_view_name(vs
->val
));
1237 if (exit_on_error
) {
1238 ret
= AVERROR(EINVAL
);
1244 views_wanted
|= 1U << view_idx
;
1245 dp
->view_map
[view_idx
].out_mask
|= 1ULL << i
;
1249 case VIEW_SPECIFIER_TYPE_ALL
:
1250 views_wanted
|= (1U << nb_view_ids_av
) - 1;
1252 for (int j
= 0; j
< dp
->nb_view_map
; j
++)
1253 dp
->view_map
[j
].out_mask
|= 1ULL << i
;
1258 if (!views_wanted
) {
1259 av_log(dp
, AV_LOG_ERROR
, "No views were selected for decoding\n");
1260 ret
= AVERROR(EINVAL
);
1264 // signal to decoder which views we want
1265 nb_view_ids
= stdc_count_ones(views_wanted
);
1266 view_ids
= av_malloc_array(nb_view_ids
, sizeof(*view_ids
));
1268 ret
= AVERROR(ENOMEM
);
1272 for (unsigned i
= 0; i
< nb_view_ids
; i
++) {
1275 av_assert0(views_wanted
);
1276 pos
= stdc_trailing_zeros(views_wanted
);
1277 views_wanted
&= ~(1U << pos
);
1279 view_ids
[i
] = view_ids_av
[pos
];
1282 // unset view_ids in case we set it earlier
1283 av_opt_set(dec_ctx
, "view_ids", NULL
, AV_OPT_SEARCH_CHILDREN
);
1285 ret
= av_opt_set_array(dec_ctx
, "view_ids", AV_OPT_SEARCH_CHILDREN
,
1286 0, nb_view_ids
, AV_OPT_TYPE_INT
, view_ids
);
1290 if (!dp
->frame_tmp_ref
) {
1291 dp
->frame_tmp_ref
= av_frame_alloc();
1292 if (!dp
->frame_tmp_ref
) {
1293 ret
= AVERROR(ENOMEM
);
1299 av_freep(&view_ids_av
);
1300 av_freep(&view_pos_av
);
1301 av_freep(&view_ids
);
1306 static void multiview_check_manual(DecoderPriv
*dp
, const AVDictionary
*dec_opts
)
1308 if (av_dict_get(dec_opts
, "view_ids", NULL
, 0)) {
1309 av_log(dp
, AV_LOG_WARNING
, "Manually selecting views with -view_ids "
1310 "is not recommended, use view specifiers instead\n");
1311 dp
->multiview_user_config
= 1;
1315 static enum AVPixelFormat
get_format(AVCodecContext
*s
, const enum AVPixelFormat
*pix_fmts
)
1317 DecoderPriv
*dp
= s
->opaque
;
1318 const enum AVPixelFormat
*p
;
1321 ret
= multiview_setup(dp
, s
);
1323 av_log(dp
, AV_LOG_ERROR
, "Error setting up multiview decoding: %s\n",
1325 return AV_PIX_FMT_NONE
;
1328 for (p
= pix_fmts
; *p
!= AV_PIX_FMT_NONE
; p
++) {
1329 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(*p
);
1330 const AVCodecHWConfig
*config
= NULL
;
1332 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
))
1335 if (dp
->hwaccel_id
== HWACCEL_GENERIC
||
1336 dp
->hwaccel_id
== HWACCEL_AUTO
) {
1337 for (int i
= 0;; i
++) {
1338 config
= avcodec_get_hw_config(s
->codec
, i
);
1341 if (!(config
->methods
&
1342 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
))
1344 if (config
->pix_fmt
== *p
)
1348 if (config
&& config
->device_type
== dp
->hwaccel_device_type
) {
1349 dp
->hwaccel_pix_fmt
= *p
;
1357 static int get_buffer(AVCodecContext
*dec_ctx
, AVFrame
*frame
, int flags
)
1359 DecoderPriv
*dp
= dec_ctx
->opaque
;
1361 // for multiview video, store the output mask in frame opaque
1362 if (dp
->nb_view_map
) {
1363 const AVFrameSideData
*sd
= av_frame_get_side_data(frame
, AV_FRAME_DATA_VIEW_ID
);
1364 int view_id
= sd
? *(int*)sd
->data
: 0;
1366 for (int i
= 0; i
< dp
->nb_view_map
; i
++) {
1367 if (dp
->view_map
[i
].id
== view_id
) {
1368 frame
->opaque
= (void*)dp
->view_map
[i
].out_mask
;
1374 return avcodec_default_get_buffer2(dec_ctx
, frame
, flags
);
1377 static HWDevice
*hw_device_match_by_codec(const AVCodec
*codec
)
1379 const AVCodecHWConfig
*config
;
1381 for (int i
= 0;; i
++) {
1382 config
= avcodec_get_hw_config(codec
, i
);
1385 if (!(config
->methods
& AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
))
1387 dev
= hw_device_get_by_type(config
->device_type
);
1393 static int hw_device_setup_for_decode(DecoderPriv
*dp
,
1394 const AVCodec
*codec
,
1395 const char *hwaccel_device
)
1397 const AVCodecHWConfig
*config
;
1398 enum AVHWDeviceType type
;
1399 HWDevice
*dev
= NULL
;
1400 int err
, auto_device
= 0;
1402 if (hwaccel_device
) {
1403 dev
= hw_device_get_by_name(hwaccel_device
);
1405 if (dp
->hwaccel_id
== HWACCEL_AUTO
) {
1407 } else if (dp
->hwaccel_id
== HWACCEL_GENERIC
) {
1408 type
= dp
->hwaccel_device_type
;
1409 err
= hw_device_init_from_type(type
, hwaccel_device
,
1412 // This will be dealt with by API-specific initialisation
1413 // (using hwaccel_device), so nothing further needed here.
1417 if (dp
->hwaccel_id
== HWACCEL_AUTO
) {
1418 dp
->hwaccel_device_type
= dev
->type
;
1419 } else if (dp
->hwaccel_device_type
!= dev
->type
) {
1420 av_log(dp
, AV_LOG_ERROR
, "Invalid hwaccel device "
1421 "specified for decoder: device %s of type %s is not "
1422 "usable with hwaccel %s.\n", dev
->name
,
1423 av_hwdevice_get_type_name(dev
->type
),
1424 av_hwdevice_get_type_name(dp
->hwaccel_device_type
));
1425 return AVERROR(EINVAL
);
1429 if (dp
->hwaccel_id
== HWACCEL_AUTO
) {
1431 } else if (dp
->hwaccel_id
== HWACCEL_GENERIC
) {
1432 type
= dp
->hwaccel_device_type
;
1433 dev
= hw_device_get_by_type(type
);
1435 // When "-qsv_device device" is used, an internal QSV device named
1436 // as "__qsv_device" is created. Another QSV device is created too
1437 // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
1438 // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
1439 // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
1440 // To keep back-compatibility with the removed ad-hoc libmfx setup code,
1441 // call hw_device_get_by_name("__qsv_device") to select the internal QSV
1443 if (!dev
&& type
== AV_HWDEVICE_TYPE_QSV
)
1444 dev
= hw_device_get_by_name("__qsv_device");
1447 err
= hw_device_init_from_type(type
, NULL
, &dev
);
1449 dev
= hw_device_match_by_codec(codec
);
1451 // No device for this codec, but not using generic hwaccel
1452 // and therefore may well not need one - ignore.
1459 if (!avcodec_get_hw_config(codec
, 0)) {
1460 // Decoder does not support any hardware devices.
1463 for (int i
= 0; !dev
; i
++) {
1464 config
= avcodec_get_hw_config(codec
, i
);
1467 type
= config
->device_type
;
1468 dev
= hw_device_get_by_type(type
);
1470 av_log(dp
, AV_LOG_INFO
, "Using auto "
1471 "hwaccel type %s with existing device %s.\n",
1472 av_hwdevice_get_type_name(type
), dev
->name
);
1475 for (int i
= 0; !dev
; i
++) {
1476 config
= avcodec_get_hw_config(codec
, i
);
1479 type
= config
->device_type
;
1480 // Try to make a new device of this type.
1481 err
= hw_device_init_from_type(type
, hwaccel_device
,
1484 // Can't make a device of this type.
1487 if (hwaccel_device
) {
1488 av_log(dp
, AV_LOG_INFO
, "Using auto "
1489 "hwaccel type %s with new device created "
1490 "from %s.\n", av_hwdevice_get_type_name(type
),
1493 av_log(dp
, AV_LOG_INFO
, "Using auto "
1494 "hwaccel type %s with new default device.\n",
1495 av_hwdevice_get_type_name(type
));
1499 dp
->hwaccel_device_type
= type
;
1501 av_log(dp
, AV_LOG_INFO
, "Auto hwaccel "
1502 "disabled: no device found.\n");
1503 dp
->hwaccel_id
= HWACCEL_NONE
;
1509 av_log(dp
, AV_LOG_ERROR
, "No device available "
1510 "for decoder: device type %s needed for codec %s.\n",
1511 av_hwdevice_get_type_name(type
), codec
->name
);
1515 dp
->dec_ctx
->hw_device_ctx
= av_buffer_ref(dev
->device_ref
);
1516 if (!dp
->dec_ctx
->hw_device_ctx
)
1517 return AVERROR(ENOMEM
);
1522 static int dec_open(DecoderPriv
*dp
, AVDictionary
**dec_opts
,
1523 const DecoderOpts
*o
, AVFrame
*param_out
)
1525 const AVCodec
*codec
= o
->codec
;
1528 dp
->flags
= o
->flags
;
1529 dp
->log_parent
= o
->log_parent
;
1531 dp
->dec
.type
= codec
->type
;
1532 dp
->framerate_in
= o
->framerate
;
1534 dp
->hwaccel_id
= o
->hwaccel_id
;
1535 dp
->hwaccel_device_type
= o
->hwaccel_device_type
;
1536 dp
->hwaccel_output_format
= o
->hwaccel_output_format
;
1538 snprintf(dp
->log_name
, sizeof(dp
->log_name
), "dec:%s", codec
->name
);
1540 dp
->parent_name
= av_strdup(o
->name
? o
->name
: "");
1541 if (!dp
->parent_name
)
1542 return AVERROR(ENOMEM
);
1544 if (codec
->type
== AVMEDIA_TYPE_SUBTITLE
&&
1545 (dp
->flags
& DECODER_FLAG_FIX_SUB_DURATION
)) {
1546 for (int i
= 0; i
< FF_ARRAY_ELEMS(dp
->sub_prev
); i
++) {
1547 dp
->sub_prev
[i
] = av_frame_alloc();
1548 if (!dp
->sub_prev
[i
])
1549 return AVERROR(ENOMEM
);
1551 dp
->sub_heartbeat
= av_frame_alloc();
1552 if (!dp
->sub_heartbeat
)
1553 return AVERROR(ENOMEM
);
1556 dp
->sar_override
= o
->par
->sample_aspect_ratio
;
1558 dp
->dec_ctx
= avcodec_alloc_context3(codec
);
1560 return AVERROR(ENOMEM
);
1562 ret
= avcodec_parameters_to_context(dp
->dec_ctx
, o
->par
);
1564 av_log(dp
, AV_LOG_ERROR
, "Error initializing the decoder context.\n");
1568 dp
->dec_ctx
->opaque
= dp
;
1569 dp
->dec_ctx
->get_format
= get_format
;
1570 dp
->dec_ctx
->get_buffer2
= get_buffer
;
1571 dp
->dec_ctx
->pkt_timebase
= o
->time_base
;
1573 if (!av_dict_get(*dec_opts
, "threads", NULL
, 0))
1574 av_dict_set(dec_opts
, "threads", "auto", 0);
1576 ret
= hw_device_setup_for_decode(dp
, codec
, o
->hwaccel_device
);
1578 av_log(dp
, AV_LOG_ERROR
,
1579 "Hardware device setup failed for decoder: %s\n",
1584 ret
= av_opt_set_dict2(dp
->dec_ctx
, dec_opts
, AV_OPT_SEARCH_CHILDREN
);
1586 av_log(dp
, AV_LOG_ERROR
, "Error applying decoder options: %s\n",
1590 ret
= check_avoptions(*dec_opts
);
1594 dp
->dec_ctx
->flags
|= AV_CODEC_FLAG_COPY_OPAQUE
;
1595 if (o
->flags
& DECODER_FLAG_BITEXACT
)
1596 dp
->dec_ctx
->flags
|= AV_CODEC_FLAG_BITEXACT
;
1598 // we apply cropping outselves
1599 dp
->apply_cropping
= dp
->dec_ctx
->apply_cropping
;
1600 dp
->dec_ctx
->apply_cropping
= 0;
1602 if ((ret
= avcodec_open2(dp
->dec_ctx
, codec
, NULL
)) < 0) {
1603 av_log(dp
, AV_LOG_ERROR
, "Error while opening decoder: %s\n",
1608 if (dp
->dec_ctx
->hw_device_ctx
) {
1609 // Update decoder extra_hw_frames option to account for the
1610 // frames held in queues inside the ffmpeg utility. This is
1611 // called after avcodec_open2() because the user-set value of
1612 // extra_hw_frames becomes valid in there, and we need to add
1613 // this on top of it.
1614 int extra_frames
= DEFAULT_FRAME_THREAD_QUEUE_SIZE
;
1615 if (dp
->dec_ctx
->extra_hw_frames
>= 0)
1616 dp
->dec_ctx
->extra_hw_frames
+= extra_frames
;
1618 dp
->dec_ctx
->extra_hw_frames
= extra_frames
;
1621 dp
->dec
.subtitle_header
= dp
->dec_ctx
->subtitle_header
;
1622 dp
->dec
.subtitle_header_size
= dp
->dec_ctx
->subtitle_header_size
;
1625 if (dp
->dec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1626 param_out
->format
= dp
->dec_ctx
->sample_fmt
;
1627 param_out
->sample_rate
= dp
->dec_ctx
->sample_rate
;
1629 ret
= av_channel_layout_copy(¶m_out
->ch_layout
, &dp
->dec_ctx
->ch_layout
);
1632 } else if (dp
->dec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1633 param_out
->format
= dp
->dec_ctx
->pix_fmt
;
1634 param_out
->width
= dp
->dec_ctx
->width
;
1635 param_out
->height
= dp
->dec_ctx
->height
;
1636 param_out
->sample_aspect_ratio
= dp
->dec_ctx
->sample_aspect_ratio
;
1637 param_out
->colorspace
= dp
->dec_ctx
->colorspace
;
1638 param_out
->color_range
= dp
->dec_ctx
->color_range
;
1641 param_out
->time_base
= dp
->dec_ctx
->pkt_timebase
;
1647 int dec_init(Decoder
**pdec
, Scheduler
*sch
,
1648 AVDictionary
**dec_opts
, const DecoderOpts
*o
,
1656 ret
= dec_alloc(&dp
, sch
, !!(o
->flags
& DECODER_FLAG_SEND_END_TS
));
1660 multiview_check_manual(dp
, *dec_opts
);
1662 ret
= dec_open(dp
, dec_opts
, o
, param_out
);
1670 dec_free((Decoder
**)&dp
);
1674 int dec_create(const OptionsContext
*o
, const char *arg
, Scheduler
*sch
)
1680 int of_index
, ost_index
;
1686 ret
= dec_alloc(&dp
, sch
, 0);
1690 dp
->index
= nb_decoders
;
1692 ret
= GROW_ARRAY(decoders
, nb_decoders
);
1694 dec_free((Decoder
**)&dp
);
1698 decoders
[nb_decoders
- 1] = (Decoder
*)dp
;
1700 of_index
= strtol(arg
, &p
, 0);
1701 if (of_index
< 0 || of_index
>= nb_output_files
) {
1702 av_log(dp
, AV_LOG_ERROR
, "Invalid output file index '%d' in %s\n", of_index
, arg
);
1703 return AVERROR(EINVAL
);
1705 of
= output_files
[of_index
];
1707 ost_index
= strtol(p
+ 1, NULL
, 0);
1708 if (ost_index
< 0 || ost_index
>= of
->nb_streams
) {
1709 av_log(dp
, AV_LOG_ERROR
, "Invalid output stream index '%d' in %s\n", ost_index
, arg
);
1710 return AVERROR(EINVAL
);
1712 ost
= of
->streams
[ost_index
];
1715 av_log(dp
, AV_LOG_ERROR
, "Output stream %s has no encoder\n", arg
);
1716 return AVERROR(EINVAL
);
1719 dp
->dec
.type
= ost
->type
;
1721 ret
= enc_loopback(ost
->enc
);
1726 ret
= sch_connect(sch
, SCH_ENC(enc_idx
), SCH_DEC_IN(dp
->sch_idx
));
1730 ret
= av_dict_copy(&dp
->standalone_init
.opts
, o
->g
->codec_opts
, 0);
1734 multiview_check_manual(dp
, dp
->standalone_init
.opts
);
1736 if (o
->codec_names
.nb_opt
) {
1737 const char *name
= o
->codec_names
.opt
[o
->codec_names
.nb_opt
- 1].u
.str
;
1738 dp
->standalone_init
.codec
= avcodec_find_decoder_by_name(name
);
1739 if (!dp
->standalone_init
.codec
) {
1740 av_log(dp
, AV_LOG_ERROR
, "No such decoder: %s\n", name
);
1741 return AVERROR_DECODER_NOT_FOUND
;
1748 int dec_filter_add(Decoder
*d
, InputFilter
*ifilter
, InputFilterOptions
*opts
,
1749 const ViewSpecifier
*vs
, SchedulerNode
*src
)
1751 DecoderPriv
*dp
= dp_from_dec(d
);
1754 snprintf(name
, sizeof(name
), "dec%d", dp
->index
);
1755 opts
->name
= av_strdup(name
);
1757 return AVERROR(ENOMEM
);
1759 return dec_request_view(d
, vs
, src
);