3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
10 /* This module is Handbrake's interface to the ffmpeg decoder library
11 (libavcodec & small parts of libavformat). It contains four Handbrake
14 decavcodeca connects HB to an ffmpeg audio decoder
15 decavcodecv connects HB to an ffmpeg video decoder
17 (Two different routines are needed because the ffmpeg library
18 has different decoder calling conventions for audio & video.
19 These work objects are self-contained & follow all
20 of HB's conventions for a decoder module. They can be used like
23 These decoders handle 2 kinds of input. Streams that are demuxed
24 by HandBrake and streams that are demuxed by libavformat. In the
25 case of streams that are demuxed by HandBrake, there is an extra
26 parse step required that happens in decodeVideo and decavcodecaWork.
27 In the case of streams that are demuxed by libavformat, there is context
28 information that we need from the libavformat. This information is
29 propagated from hb_stream_open to these decoders through title->opaque_priv.
31 A consequence of the above is that the streams that are demuxed by HandBrake
32 *can't* use information from the AVStream because there isn't one - they
33 get their data from either the dvd reader or the mpeg reader, not the ffmpeg
34 stream reader. That means that they have to make up for deficiencies in the
35 AVCodecContext info by using stuff kept in the HB "title" struct. It
36 also means that ffmpeg codecs that randomly scatter state needed by
37 the decoder across both the AVCodecContext & the AVStream (e.g., the
38 VC1 decoder) can't easily be used by the HB mpeg stream reader.
43 #include "audio_resample.h"
51 #include "qsv_common.h"
54 static void compute_frame_duration( hb_work_private_t
*pv
);
55 static void flushDelayQueue( hb_work_private_t
*pv
);
56 static int decavcodecaInit( hb_work_object_t
*, hb_job_t
* );
57 static int decavcodecaWork( hb_work_object_t
*, hb_buffer_t
**, hb_buffer_t
** );
58 static void decavcodecClose( hb_work_object_t
* );
59 static int decavcodecaInfo( hb_work_object_t
*, hb_work_info_t
* );
60 static int decavcodecaBSInfo( hb_work_object_t
*, const hb_buffer_t
*, hb_work_info_t
* );
62 hb_work_object_t hb_decavcodeca
=
64 .id
= WORK_DECAVCODEC
,
65 .name
= "Audio decoder (libavcodec)",
66 .init
= decavcodecaInit
,
67 .work
= decavcodecaWork
,
68 .close
= decavcodecClose
,
69 .info
= decavcodecaInfo
,
70 .bsinfo
= decavcodecaBSInfo
75 // there are nheap items on the heap indexed 1..nheap (i.e., top of
76 // heap is 1). The 0th slot is unused - a marker is put there to check
77 // for overwrite errs.
78 int64_t h
[HEAP_SIZE
+1];
82 struct hb_work_private_s
86 AVCodecContext
*context
;
87 AVCodecParserContext
*parser
;
91 int video_codec_opened
;
93 double duration
; // frame duration (for video)
94 double field_duration
; // field duration (for video)
95 int frame_duration_set
; // Indicates valid timing was found in stream
96 double pts_next
; // next pts we expect to generate
97 int64_t chap_time
; // time of next chap mark (if new_chap != 0)
98 int new_chap
; // output chapter mark pending
101 uint32_t decode_errors
;
103 int brokenTS
; // video stream may contain packed b-frames
104 hb_buffer_t
* delayq
[HEAP_SIZE
];
108 struct SwsContext
*sws_context
; // if we have to rescale or convert color space
113 int wait_for_keyframe
;
115 hb_va_dxva2_t
*dxva2
;
117 hb_oclscale_t
*opencl_scale
;
119 hb_audio_resample_t
*resample
;
122 // QSV-specific settings
126 av_qsv_config config
;
127 const char *codec_name
;
128 #define USE_QSV_PTS_WORKAROUND // work around out-of-order output timestamps
129 #ifdef USE_QSV_PTS_WORKAROUND
135 hb_list_t
* list_subtitle
;
138 #ifdef USE_QSV_PTS_WORKAROUND
139 // save/restore PTS if the decoder may not attach the right PTS to the frame
140 static void hb_av_add_new_pts(hb_list_t
*list
, int64_t new_pts
)
143 int64_t *cur_item
, *new_item
;
144 if (list
!= NULL
&& new_pts
!= AV_NOPTS_VALUE
)
146 new_item
= malloc(sizeof(int64_t));
147 if (new_item
!= NULL
)
150 // sort chronologically
151 for (index
= 0; index
< hb_list_count(list
); index
++)
153 cur_item
= hb_list_item(list
, index
);
154 if (cur_item
!= NULL
)
156 if (*cur_item
== *new_item
)
162 if (*cur_item
> *new_item
)
169 hb_list_insert(list
, index
, new_item
);
173 static int64_t hb_av_pop_next_pts(hb_list_t
*list
)
175 int64_t *item
, next_pts
= AV_NOPTS_VALUE
;
176 if (list
!= NULL
&& hb_list_count(list
) > 0)
178 item
= hb_list_item(list
, 0);
182 hb_list_rem(list
, item
);
190 static void decodeAudio( hb_audio_t
* audio
, hb_work_private_t
*pv
, uint8_t *data
, int size
, int64_t pts
);
191 static hb_buffer_t
*link_buf_list( hb_work_private_t
*pv
);
194 static int64_t heap_pop( pts_heap_t
*heap
)
198 if ( heap
->nheap
<= 0 )
200 return AV_NOPTS_VALUE
;
203 // return the top of the heap then put the bottom element on top,
204 // decrease the heap size by one & rebalence the heap.
207 int64_t v
= heap
->h
[heap
->nheap
--];
209 int child
= parent
<< 1;
210 while ( child
<= heap
->nheap
)
212 // find the smallest of the two children of parent
213 if (child
< heap
->nheap
&& heap
->h
[child
] > heap
->h
[child
+1] )
216 if (v
<= heap
->h
[child
])
217 // new item is smaller than either child so it's the new parent.
220 // smallest child is smaller than new item so move it up then
221 // check its children.
222 int64_t hp
= heap
->h
[child
];
223 heap
->h
[parent
] = hp
;
231 static void heap_push( pts_heap_t
*heap
, int64_t v
)
233 if ( heap
->nheap
< HEAP_SIZE
)
238 // stick the new value on the bottom of the heap then bubble it
239 // up to its correct spot.
240 int child
= heap
->nheap
;
242 int parent
= child
>> 1;
243 if (heap
->h
[parent
] <= v
)
246 int64_t hp
= heap
->h
[parent
];
253 /***********************************************************************
254 * hb_work_decavcodec_init
255 ***********************************************************************
257 **********************************************************************/
258 static int decavcodecaInit( hb_work_object_t
* w
, hb_job_t
* job
)
262 hb_work_private_t
* pv
= calloc( 1, sizeof( hb_work_private_t
) );
263 w
->private_data
= pv
;
267 pv
->title
= job
->title
;
269 pv
->title
= w
->title
;
270 pv
->list
= hb_list_init();
272 codec
= avcodec_find_decoder(w
->codec_param
);
273 pv
->context
= avcodec_alloc_context3(codec
);
275 if (pv
->title
->opaque_priv
!= NULL
)
277 AVFormatContext
*ic
= (AVFormatContext
*)pv
->title
->opaque_priv
;
278 avcodec_copy_context(pv
->context
, ic
->streams
[w
->audio
->id
]->codec
);
279 // libav's eac3 parser toggles the codec_id in the context as
280 // it reads eac3 data between AV_CODEC_ID_AC3 and AV_CODEC_ID_EAC3.
281 // It detects an AC3 sync pattern sometimes in ac3_sync() which
282 // causes it to eventually set avctx->codec_id to AV_CODEC_ID_AC3
283 // in ff_aac_ac3_parse(). Since we are parsing some data before
284 // we get here, the codec_id may have flipped. This will cause an
285 // error in hb_avcodec_open(). So flip it back!
286 pv
->context
->codec_id
= w
->codec_param
;
290 pv
->parser
= av_parser_init(w
->codec_param
);
292 hb_ff_set_sample_fmt(pv
->context
, codec
, AV_SAMPLE_FMT_FLT
);
294 /* Downmixing & sample_fmt conversion */
295 if (!(w
->audio
->config
.out
.codec
& HB_ACODEC_PASS_FLAG
))
298 hb_audio_resample_init(AV_SAMPLE_FMT_FLT
,
299 w
->audio
->config
.out
.mixdown
,
300 w
->audio
->config
.out
.normalize_mix_level
);
301 if (pv
->resample
== NULL
)
303 hb_error("decavcodecaInit: hb_audio_resample_init() failed");
307 * Some audio decoders can downmix using embedded coefficients,
308 * or dedicated audio substreams for a specific channel layout.
310 * But some will e.g. use normalized mix coefficients unconditionally,
311 * so we need to make sure this matches what the user actually requested.
313 int avcodec_downmix
= 0;
314 switch (w
->codec_param
)
316 case AV_CODEC_ID_AC3
:
317 case AV_CODEC_ID_EAC3
:
318 avcodec_downmix
= w
->audio
->config
.out
.normalize_mix_level
!= 0;
320 case AV_CODEC_ID_DTS
:
321 avcodec_downmix
= w
->audio
->config
.out
.normalize_mix_level
== 0;
323 case AV_CODEC_ID_TRUEHD
:
324 avcodec_downmix
= (w
->audio
->config
.out
.normalize_mix_level
== 0 ||
325 w
->audio
->config
.out
.mixdown
== HB_AMIXDOWN_MONO
||
326 w
->audio
->config
.out
.mixdown
== HB_AMIXDOWN_DOLBY
||
327 w
->audio
->config
.out
.mixdown
== HB_AMIXDOWN_DOLBYPLII
);
334 switch (w
->audio
->config
.out
.mixdown
)
336 // request 5.1 before downmixing to dpl1/dpl2
337 case HB_AMIXDOWN_DOLBY
:
338 case HB_AMIXDOWN_DOLBYPLII
:
339 pv
->context
->request_channel_layout
= AV_CH_LAYOUT_5POINT1
;
341 // request the layout corresponding to the selected mixdown
343 pv
->context
->request_channel_layout
=
344 hb_ff_mixdown_xlat(w
->audio
->config
.out
.mixdown
, NULL
);
350 // libavcodec can't decode TrueHD Mono (bug #356)
351 // work around it by requesting Stereo and downmixing
352 if (w
->codec_param
== AV_CODEC_ID_TRUEHD
&&
353 w
->audio
->config
.in
.channel_layout
== AV_CH_LAYOUT_MONO
)
355 pv
->context
->request_channel_layout
= AV_CH_LAYOUT_STEREO
;
358 // Set decoder opts...
359 AVDictionary
* av_opts
= NULL
;
360 av_dict_set( &av_opts
, "refcounted_frames", "1", 0 );
362 // Dynamic Range Compression
363 if (w
->audio
->config
.out
.dynamic_range_compression
>= 0.0f
&&
364 hb_audio_can_apply_drc(w
->audio
->config
.in
.codec
,
365 w
->audio
->config
.in
.codec_param
, 0))
367 float drc_scale_max
= 1.0f
;
369 * avcodec_open will fail if the value for any of the options is out of
370 * range, so assume a conservative maximum of 1 and try to determine the
371 * option's actual upper limit.
373 if (codec
!= NULL
&& codec
->priv_class
!= NULL
)
376 opt
= av_opt_find2((void*)&codec
->priv_class
, "drc_scale", NULL
,
377 AV_OPT_FLAG_DECODING_PARAM
|AV_OPT_FLAG_AUDIO_PARAM
,
378 AV_OPT_SEARCH_FAKE_OBJ
, NULL
);
381 drc_scale_max
= opt
->max
;
384 if (w
->audio
->config
.out
.dynamic_range_compression
> drc_scale_max
)
386 hb_log("decavcodecaInit: track %d, sanitizing out-of-range DRC %.2f to %.2f",
387 w
->audio
->config
.out
.track
,
388 w
->audio
->config
.out
.dynamic_range_compression
, drc_scale_max
);
389 w
->audio
->config
.out
.dynamic_range_compression
= drc_scale_max
;
392 char drc_scale
[5]; // "?.??\n"
393 snprintf(drc_scale
, sizeof(drc_scale
), "%.2f",
394 w
->audio
->config
.out
.dynamic_range_compression
);
395 av_dict_set(&av_opts
, "drc_scale", drc_scale
, 0);
398 if (hb_avcodec_open(pv
->context
, codec
, &av_opts
, 0))
400 av_dict_free( &av_opts
);
401 hb_log("decavcodecaInit: avcodec_open failed");
404 // avcodec_open populates av_opts with the things it didn't recognize.
405 AVDictionaryEntry
*t
= NULL
;
406 while ((t
= av_dict_get(av_opts
, "", t
, AV_DICT_IGNORE_SUFFIX
)) != NULL
)
408 hb_log("decavcodecaInit: unknown option '%s'", t
->key
);
410 av_dict_free( &av_opts
);
412 pv
->frame
= av_frame_alloc();
413 if (pv
->frame
== NULL
)
415 hb_log("decavcodecaInit: av_frame_alloc failed");
422 /***********************************************************************
424 ***********************************************************************
426 **********************************************************************/
427 static void closePrivData( hb_work_private_t
** ppv
)
429 hb_work_private_t
* pv
= *ppv
;
433 flushDelayQueue( pv
);
434 hb_buffer_t
*buf
= link_buf_list( pv
);
435 hb_buffer_close( &buf
);
437 if ( pv
->job
&& pv
->context
&& pv
->context
->codec
)
439 hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
440 pv
->context
->codec
->name
, pv
->nframes
, pv
->decode_errors
,
443 av_frame_free(&pv
->frame
);
444 if ( pv
->sws_context
)
446 sws_freeContext( pv
->sws_context
);
450 av_parser_close(pv
->parser
);
452 if ( pv
->context
&& pv
->context
->codec
)
456 * FIXME: knowingly leaked.
458 * If we're using our Libav QSV wrapper, qsv_decode_end() will call
459 * MFXClose() on the QSV session. Even if decoding is complete, we
460 * still need that session for QSV filtering and/or encoding, so we
461 * we can't close the context here until we implement a proper fix.
466 hb_avcodec_close(pv
->context
);
471 av_freep( &pv
->context
->extradata
);
472 av_freep( &pv
->context
);
476 hb_list_empty( &pv
->list
);
478 hb_audio_resample_free(pv
->resample
);
481 if (pv
->opencl_scale
!= NULL
)
483 free(pv
->opencl_scale
);
485 if (pv
->dxva2
!= NULL
)
489 HB_OCL_BUF_FREE(hb_ocl
, pv
->dxva2
->cl_mem_nv12
);
491 hb_va_close(pv
->dxva2
);
495 #ifdef USE_QSV_PTS_WORKAROUND
496 if (pv
->qsv
.decode
&& pv
->qsv
.pts_list
!= NULL
)
499 while (hb_list_count(pv
->qsv
.pts_list
) > 0)
501 int64_t *item
= hb_list_item(pv
->qsv
.pts_list
, 0);
502 hb_list_rem(pv
->qsv
.pts_list
, item
);
505 hb_list_close(&pv
->qsv
.pts_list
);
514 static void decavcodecClose( hb_work_object_t
* w
)
516 hb_work_private_t
* pv
= w
->private_data
;
518 if( pv
->dst_frame
) free( pv
->dst_frame
);
522 closePrivData( &pv
);
523 w
->private_data
= NULL
;
527 /***********************************************************************
529 ***********************************************************************
531 **********************************************************************/
532 static int decavcodecaWork( hb_work_object_t
* w
, hb_buffer_t
** buf_in
,
533 hb_buffer_t
** buf_out
)
535 hb_work_private_t
* pv
= w
->private_data
;
536 hb_buffer_t
* in
= *buf_in
;
538 if (in
->s
.flags
& HB_BUF_FLAG_EOF
)
540 /* EOF on input stream - send it downstream & say that we're done */
548 if ( in
->s
.start
< 0 && pv
->pts_next
<= 0 )
550 // discard buffers that start before video time 0
555 for ( pos
= 0; pos
< in
->size
; pos
+= len
)
563 if ( pv
->parser
!= NULL
)
565 len
= av_parser_parse2( pv
->parser
, pv
->context
, &pout
, &pout_len
,
566 in
->data
+ pos
, in
->size
- pos
, cur
, cur
, 0 );
567 cur
= pv
->parser
->pts
;
572 len
= pout_len
= in
->size
;
574 if (pout
!= NULL
&& pout_len
> 0)
576 decodeAudio( w
->audio
, pv
, pout
, pout_len
, cur
);
579 *buf_out
= link_buf_list( pv
);
583 static int decavcodecaInfo( hb_work_object_t
*w
, hb_work_info_t
*info
)
585 hb_work_private_t
*pv
= w
->private_data
;
587 memset( info
, 0, sizeof(*info
) );
589 if ( pv
&& pv
->context
)
591 AVCodecContext
*context
= pv
->context
;
592 info
->bitrate
= context
->bit_rate
;
593 info
->rate
.num
= context
->time_base
.num
;
594 info
->rate
.den
= context
->time_base
.den
;
595 info
->profile
= context
->profile
;
596 info
->level
= context
->level
;
602 static int decavcodecaBSInfo( hb_work_object_t
*w
, const hb_buffer_t
*buf
,
603 hb_work_info_t
*info
)
605 hb_work_private_t
*pv
= w
->private_data
;
607 hb_audio_t
*audio
= w
->audio
;
609 memset( info
, 0, sizeof(*info
) );
611 if ( pv
&& pv
->context
)
613 return decavcodecaInfo( w
, info
);
616 AVCodec
*codec
= avcodec_find_decoder( w
->codec_param
);
619 // there's no ffmpeg codec for this audio type - give up
623 static char codec_name
[64];
624 info
->name
= strncpy( codec_name
, codec
->name
, sizeof(codec_name
)-1 );
626 AVCodecContext
*context
= avcodec_alloc_context3(codec
);
627 AVCodecParserContext
*parser
= NULL
;
629 if (w
->title
&& w
->title
->opaque_priv
!= NULL
)
631 AVFormatContext
*ic
= (AVFormatContext
*)w
->title
->opaque_priv
;
632 avcodec_copy_context(context
, ic
->streams
[audio
->id
]->codec
);
633 // libav's eac3 parser toggles the codec_id in the context as
634 // it reads eac3 data between AV_CODEC_ID_AC3 and AV_CODEC_ID_EAC3.
635 // It detects an AC3 sync pattern sometimes in ac3_sync() which
636 // causes it to eventually set avctx->codec_id to AV_CODEC_ID_AC3
637 // in ff_aac_ac3_parse(). Since we are parsing some data before
638 // we get here, the codec_id may have flipped. This will cause an
639 // error in hb_avcodec_open(). So flip it back!
640 context
->codec_id
= w
->codec_param
;
644 parser
= av_parser_init(codec
->id
);
647 hb_ff_set_sample_fmt( context
, codec
, AV_SAMPLE_FMT_FLT
);
649 AVDictionary
* av_opts
= NULL
;
650 av_dict_set( &av_opts
, "err_detect", "crccheck+explode", 0 );
651 if ( hb_avcodec_open( context
, codec
, &av_opts
, 0 ) )
653 av_dict_free( &av_opts
);
656 av_dict_free( &av_opts
);
657 unsigned char *parse_buffer
;
658 int parse_pos
, dec_pos
, parse_buffer_size
;
660 while (buf
!= NULL
&& !ret
)
663 while (parse_pos
< buf
->size
)
665 int parse_len
, truehd_mono
= 0;
669 parse_len
= av_parser_parse2(parser
, context
,
670 &parse_buffer
, &parse_buffer_size
,
671 buf
->data
+ parse_pos
, buf
->size
- parse_pos
,
672 buf
->s
.start
, buf
->s
.start
, 0);
676 parse_buffer
= buf
->data
+ parse_pos
;
677 parse_len
= parse_buffer_size
= buf
->size
- parse_pos
;
680 // libavcodec can't decode TrueHD Mono (bug #356)
681 // work around it by requesting Stereo before decoding
682 if (context
->codec_id
== AV_CODEC_ID_TRUEHD
&&
683 context
->channel_layout
== AV_CH_LAYOUT_MONO
)
686 context
->request_channel_layout
= AV_CH_LAYOUT_STEREO
;
690 context
->request_channel_layout
= 0;
694 while (dec_pos
< parse_buffer_size
)
698 AVFrame
*frame
= av_frame_alloc();
700 av_init_packet(&avp
);
701 avp
.data
= parse_buffer
+ dec_pos
;
702 avp
.size
= parse_buffer_size
- dec_pos
;
704 dec_len
= avcodec_decode_audio4(context
, frame
, &got_frame
, &avp
);
707 av_frame_free(&frame
);
710 if (dec_len
> 0 && got_frame
)
712 // libavcoded doesn't consistently set frame->sample_rate
713 if (frame
->sample_rate
!= 0)
715 info
->rate
.num
= frame
->sample_rate
;
719 info
->rate
.num
= context
->sample_rate
;
720 hb_log("decavcodecaBSInfo: warning: invalid frame sample_rate! Using context sample_rate.");
723 info
->samples_per_frame
= frame
->nb_samples
;
724 info
->sample_bit_depth
= context
->bits_per_raw_sample
;
726 int bps
= av_get_bits_per_sample(context
->codec_id
);
727 int channels
= av_get_channel_layout_nb_channels(frame
->channel_layout
);
730 info
->bitrate
= bps
* channels
* info
->rate
.num
;
732 else if (context
->bit_rate
> 0)
734 info
->bitrate
= context
->bit_rate
;
743 info
->channel_layout
= AV_CH_LAYOUT_MONO
;
744 info
->matrix_encoding
= AV_MATRIX_ENCODING_NONE
;
748 AVFrameSideData
*side_data
;
750 av_frame_get_side_data(frame
,
751 AV_FRAME_DATA_MATRIXENCODING
)) != NULL
)
753 info
->matrix_encoding
= *side_data
->data
;
757 info
->matrix_encoding
= AV_MATRIX_ENCODING_NONE
;
759 if (info
->matrix_encoding
== AV_MATRIX_ENCODING_DOLBY
||
760 info
->matrix_encoding
== AV_MATRIX_ENCODING_DPLII
)
762 info
->channel_layout
= AV_CH_LAYOUT_STEREO_DOWNMIX
;
766 info
->channel_layout
= frame
->channel_layout
;
769 if (context
->codec_id
== AV_CODEC_ID_AC3
||
770 context
->codec_id
== AV_CODEC_ID_EAC3
)
772 if (context
->audio_service_type
== AV_AUDIO_SERVICE_TYPE_KARAOKE
)
778 info
->mode
= context
->audio_service_type
;
781 else if (context
->codec_id
== AV_CODEC_ID_AAC
&&
782 context
->extradata_size
== 0)
784 // Parse ADTS AAC streams for AudioSpecificConfig.
785 // This data is required in order to write
786 // proper headers in MP4 and MKV files.
787 AVBitStreamFilterContext
* aac_adtstoasc
;
788 aac_adtstoasc
= av_bitstream_filter_init("aac_adtstoasc");
793 ret
= av_bitstream_filter_filter(aac_adtstoasc
, context
,
794 NULL
, &data
, &size
, avp
.data
, avp
.size
, 0);
796 context
->extradata_size
> 0 &&
797 audio
->priv
.config
.extradata
.length
== 0)
800 len
= MIN(context
->extradata_size
, HB_CONFIG_MAX_SIZE
);
801 memcpy(audio
->priv
.config
.extradata
.bytes
,
802 context
->extradata
, len
);
803 audio
->priv
.config
.extradata
.length
= len
;
805 av_bitstream_filter_close(aac_adtstoasc
);
810 av_frame_free(&frame
);
814 av_frame_free(&frame
);
816 parse_pos
+= parse_len
;
821 info
->profile
= context
->profile
;
822 info
->level
= context
->level
;
823 info
->channel_map
= &hb_libav_chan_map
;
825 if ( parser
!= NULL
)
826 av_parser_close( parser
);
827 hb_avcodec_close( context
);
828 av_freep( &context
->extradata
);
829 av_freep( &context
);
833 /* -------------------------------------------------------------
834 * General purpose video decoder using libavcodec
837 static uint8_t *copy_plane( uint8_t *dst
, uint8_t* src
, int dstride
, int sstride
,
840 if ( dstride
== sstride
)
842 memcpy( dst
, src
, dstride
* h
);
843 return dst
+ dstride
* h
;
845 int lbytes
= dstride
<= sstride
? dstride
: sstride
;
848 memcpy( dst
, src
, lbytes
);
855 // copy one video frame into an HB buf. If the frame isn't in our color space
856 // or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
857 // Otherwise just copy the bits.
858 static hb_buffer_t
*copy_frame( hb_work_private_t
*pv
)
860 AVCodecContext
*context
= pv
->context
;
864 // HandBrake's video pipeline uses yuv420 color. This means all
865 // dimensions must be even. So we must adjust the dimensions
866 // of incoming video if not even.
867 w
= context
->width
& ~1;
868 h
= context
->height
& ~1;
872 w
= pv
->job
->title
->geometry
.width
;
873 h
= pv
->job
->title
->geometry
.height
;
877 if (pv
->dxva2
&& pv
->job
)
882 buf
= hb_video_buffer_init( w
, h
);
888 pv
->dst_frame
= malloc( ww
* hh
* 3 / 2 );
890 if( hb_va_extract( pv
->dxva2
, pv
->dst_frame
, pv
->frame
, pv
->job
->width
, pv
->job
->height
, pv
->job
->title
->crop
, pv
->opencl_scale
, pv
->job
->use_opencl
, pv
->job
->use_decomb
, pv
->job
->use_detelecine
) == HB_WORK_ERROR
)
892 hb_log( "hb_va_Extract failed!!!!!!" );
894 w
= buf
->plane
[0].stride
;
895 h
= buf
->plane
[0].height
;
896 uint8_t *dst
= buf
->plane
[0].data
;
897 copy_plane( dst
, pv
->dst_frame
, w
, ww
, h
);
898 w
= buf
->plane
[1].stride
;
899 h
= buf
->plane
[1].height
;
900 dst
= buf
->plane
[1].data
;
901 copy_plane( dst
, pv
->dst_frame
+ ww
* hh
, w
, ww
>> 1, h
);
902 w
= buf
->plane
[2].stride
;
903 h
= buf
->plane
[2].height
;
904 dst
= buf
->plane
[2].data
;
905 copy_plane( dst
, pv
->dst_frame
+ ww
* hh
+( ( ww
* hh
) >> 2 ), w
, ww
>> 1, h
);
911 hb_buffer_t
*buf
= hb_video_buffer_init( w
, h
);
914 // no need to copy the frame data when decoding with QSV to opaque memory
915 if (pv
->qsv
.decode
&&
916 pv
->qsv
.config
.io_pattern
== MFX_IOPATTERN_OUT_OPAQUE_MEMORY
)
918 buf
->qsv_details
.qsv_atom
= pv
->frame
->data
[2];
923 uint8_t *dst
= buf
->data
;
925 if (context
->pix_fmt
!= AV_PIX_FMT_YUV420P
|| w
!= context
->width
||
926 h
!= context
->height
)
928 // have to convert to our internal color space and/or rescale
930 hb_avpicture_fill(&dstpic
, buf
);
932 if (pv
->sws_context
== NULL
||
933 pv
->sws_width
!= context
->width
||
934 pv
->sws_height
!= context
->height
||
935 pv
->sws_pix_fmt
!= context
->pix_fmt
)
937 if (pv
->sws_context
!= NULL
)
938 sws_freeContext(pv
->sws_context
);
939 pv
->sws_context
= hb_sws_get_context(context
->width
,
942 w
, h
, AV_PIX_FMT_YUV420P
,
943 SWS_LANCZOS
|SWS_ACCURATE_RND
);
944 pv
->sws_width
= context
->width
;
945 pv
->sws_height
= context
->height
;
946 pv
->sws_pix_fmt
= context
->pix_fmt
;
948 sws_scale(pv
->sws_context
,
949 (const uint8_t* const *)pv
->frame
->data
,
951 0, context
->height
, dstpic
.data
, dstpic
.linesize
);
955 w
= buf
->plane
[0].stride
;
956 h
= buf
->plane
[0].height
;
957 dst
= buf
->plane
[0].data
;
958 copy_plane( dst
, pv
->frame
->data
[0], w
, pv
->frame
->linesize
[0], h
);
959 w
= buf
->plane
[1].stride
;
960 h
= buf
->plane
[1].height
;
961 dst
= buf
->plane
[1].data
;
962 copy_plane( dst
, pv
->frame
->data
[1], w
, pv
->frame
->linesize
[1], h
);
963 w
= buf
->plane
[2].stride
;
964 h
= buf
->plane
[2].height
;
965 dst
= buf
->plane
[2].data
;
966 copy_plane( dst
, pv
->frame
->data
[2], w
, pv
->frame
->linesize
[2], h
);
974 static int get_frame_buf_hwd( AVCodecContext
*context
, AVFrame
*frame
)
977 hb_work_private_t
*pv
= (hb_work_private_t
*)context
->opaque
;
978 if ( (pv
!= NULL
) && pv
->dxva2
)
980 int result
= HB_WORK_ERROR
;
981 hb_work_private_t
*pv
= (hb_work_private_t
*)context
->opaque
;
982 result
= hb_va_get_frame_buf( pv
->dxva2
, context
, frame
);
983 if( result
== HB_WORK_ERROR
)
984 return avcodec_default_get_buffer( context
, frame
);
988 return avcodec_default_get_buffer( context
, frame
);
991 static void hb_ffmpeg_release_frame_buf( struct AVCodecContext
*p_context
, AVFrame
*frame
)
993 hb_work_private_t
*p_dec
= (hb_work_private_t
*)p_context
->opaque
;
997 hb_va_release( p_dec
->dxva2
, frame
);
999 else if( !frame
->opaque
)
1001 if( frame
->type
== FF_BUFFER_TYPE_INTERNAL
)
1002 avcodec_default_release_buffer( p_context
, frame
);
1004 for( i
= 0; i
< 4; i
++ )
1005 frame
->data
[i
] = NULL
;
1009 static void log_chapter( hb_work_private_t
*pv
, int chap_num
, int64_t pts
)
1016 c
= hb_list_item( pv
->job
->list_chapter
, chap_num
- 1 );
1017 if ( c
&& c
->title
)
1019 hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64
,
1020 pv
->context
->codec
->name
, c
->title
, chap_num
, pv
->nframes
, pts
);
1024 hb_log( "%s: Chapter %d at frame %u time %"PRId64
,
1025 pv
->context
->codec
->name
, chap_num
, pv
->nframes
, pts
);
1029 static void flushDelayQueue( hb_work_private_t
*pv
)
1032 int slot
= pv
->queue_primed
? pv
->nframes
& (HEAP_SIZE
-1) : 0;
1034 // flush all the video packets left on our timestamp-reordering delay q
1035 while ( ( buf
= pv
->delayq
[slot
] ) != NULL
)
1037 buf
->s
.start
= heap_pop( &pv
->pts_heap
);
1038 hb_list_add( pv
->list
, buf
);
1039 pv
->delayq
[slot
] = NULL
;
1040 slot
= ( slot
+ 1 ) & (HEAP_SIZE
-1);
1044 #define TOP_FIRST PIC_FLAG_TOP_FIELD_FIRST
1045 #define PROGRESSIVE PIC_FLAG_PROGRESSIVE_FRAME
1046 #define REPEAT_FIRST PIC_FLAG_REPEAT_FIRST_FIELD
1052 #define TBT_PROG 256
1054 static void checkCadence( int * cadence
, uint16_t flags
, int64_t start
)
1056 /* Rotate the cadence tracking. */
1058 for(i
=11; i
> 0; i
--)
1060 cadence
[i
] = cadence
[i
-1];
1063 if ( !(flags
& PROGRESSIVE
) && !(flags
& TOP_FIRST
) )
1065 /* Not progressive, not top first...
1066 That means it's probably bottom
1067 first, 2 fields displayed.
1069 //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
1072 else if ( !(flags
& PROGRESSIVE
) && (flags
& TOP_FIRST
) )
1074 /* Not progressive, top is first,
1075 Two fields displayed.
1077 //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
1080 else if ( (flags
& PROGRESSIVE
) && !(flags
& TOP_FIRST
) && !( flags
& REPEAT_FIRST
) )
1082 /* Progressive, but noting else.
1083 That means Bottom first,
1086 //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
1087 cadence
[0] = BT_PROG
;
1089 else if ( (flags
& PROGRESSIVE
) && !(flags
& TOP_FIRST
) && ( flags
& REPEAT_FIRST
) )
1091 /* Progressive, and repeat. .
1092 That means Bottom first,
1095 //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
1096 cadence
[0] = BTB_PROG
;
1098 else if ( (flags
& PROGRESSIVE
) && (flags
& TOP_FIRST
) && !( flags
& REPEAT_FIRST
) )
1100 /* Progressive, top first.
1101 That means top first,
1104 //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
1105 cadence
[0] = TB_PROG
;
1107 else if ( (flags
& PROGRESSIVE
) && (flags
& TOP_FIRST
) && ( flags
& REPEAT_FIRST
) )
1109 /* Progressive, top, repeat.
1110 That means top first,
1113 //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
1114 cadence
[0] = TBT_PROG
;
1117 if ( (cadence
[2] <= TB
) && (cadence
[1] <= TB
) && (cadence
[0] > TB
) && (cadence
[11]) )
1118 hb_log("%fs: Video -> Film", (float)start
/ 90000);
1119 if ( (cadence
[2] > TB
) && (cadence
[1] <= TB
) && (cadence
[0] <= TB
) && (cadence
[11]) )
1120 hb_log("%fs: Film -> Video", (float)start
/ 90000);
1123 // send cc_buf to the CC decoder(s)
1124 static void cc_send_to_decoder(hb_work_private_t
*pv
, hb_buffer_t
*buf
)
1129 // if there's more than one decoder for the captions send a copy
1130 // of the buffer to all.
1131 hb_subtitle_t
*subtitle
;
1132 int ii
= 0, n
= hb_list_count(pv
->list_subtitle
);
1135 // make a copy of the buf then forward it to the decoder
1136 hb_buffer_t
*cpy
= hb_buffer_dup(buf
);
1138 subtitle
= hb_list_item(pv
->list_subtitle
, ii
++);
1139 hb_fifo_push(subtitle
->fifo_in
, cpy
);
1141 subtitle
= hb_list_item(pv
->list_subtitle
, ii
);
1142 hb_fifo_push( subtitle
->fifo_in
, buf
);
1145 static hb_buffer_t
* cc_fill_buffer(hb_work_private_t
*pv
, uint8_t *cc
, int size
, int64_t pts
)
1147 int cc_count
[4] = {0,};
1149 hb_buffer_t
*buf
= NULL
;
1151 for (ii
= 0; ii
< size
; ii
+= 3)
1153 if ((cc
[ii
] & 0x04) == 0) // not valid
1155 if ((cc
[ii
+1] & 0x7f) == 0 && (cc
[ii
+2] & 0x7f) == 0) // stuffing
1157 int type
= cc
[ii
] & 0x03;
1161 // Only handles CC1 for now.
1162 if (cc_count
[0] > 0)
1164 buf
= hb_buffer_init(cc_count
[0] * 2);
1167 for (ii
= 0; ii
< size
; ii
+= 3)
1169 if ((cc
[ii
] & 0x04) == 0) // not valid
1171 if ((cc
[ii
+1] & 0x7f) == 0 && (cc
[ii
+2] & 0x7f) == 0) // stuffing
1173 int type
= cc
[ii
] & 0x03;
1176 buf
->data
[jj
++] = cc
[ii
+1];
1177 buf
->data
[jj
++] = cc
[ii
+2];
1184 static int get_frame_type(int type
)
1188 case AV_PICTURE_TYPE_I
:
1190 case AV_PICTURE_TYPE_B
:
1192 case AV_PICTURE_TYPE_P
:
1199 * Decodes a video frame from the specified raw packet data
1200 * ('data', 'size', 'sequence').
1201 * The output of this function is stored in 'pv->list', which contains a list
1202 * of zero or more decoded packets.
1204 * The returned packets are guaranteed to have their timestamps in the correct
1205 * order, even if the original packets decoded by libavcodec have misordered
1206 * timestamps, due to the use of 'packed B-frames'.
1208 * Internally the set of decoded packets may be buffered in 'pv->delayq'
1209 * until enough packets have been decoded so that the timestamps can be
1210 * correctly rewritten, if this is necessary.
1212 static int decodeFrame( hb_work_object_t
*w
, uint8_t *data
, int size
, int sequence
, int64_t pts
, int64_t dts
, uint8_t frametype
)
1214 hb_work_private_t
*pv
= w
->private_data
;
1215 int got_picture
, oldlevel
= 0;
1218 if ( global_verbosity_level
<= 1 )
1220 oldlevel
= av_log_get_level();
1221 av_log_set_level( AV_LOG_QUIET
);
1224 av_init_packet(&avp
);
1230 if (pv
->palette
!= NULL
)
1234 palette
= av_packet_new_side_data(&avp
, AV_PKT_DATA_PALETTE
,
1236 size
= MIN(pv
->palette
->size
, AVPALETTE_SIZE
);
1237 memcpy(palette
, pv
->palette
->data
, size
);
1238 hb_buffer_close(&pv
->palette
);
1242 * libav avcodec_decode_video2() needs AVPacket flagged with AV_PKT_FLAG_KEY
1243 * for some codecs. For example, sequence of PNG in a mov container.
1245 if ( frametype
& HB_FRAME_KEY
)
1247 avp
.flags
|= AV_PKT_FLAG_KEY
;
1250 #ifdef USE_QSV_PTS_WORKAROUND
1252 * The MediaSDK decoder will return decoded frames in the correct order,
1253 * but *sometimes* with the incorrect timestamp assigned to them.
1255 * We work around it by saving the input timestamps (in chronological order)
1256 * and restoring them after decoding.
1258 if (pv
->qsv
.decode
&& avp
.data
!= NULL
)
1260 hb_av_add_new_pts(pv
->qsv
.pts_list
, avp
.pts
);
1264 if ( avcodec_decode_video2( pv
->context
, pv
->frame
, &got_picture
, &avp
) < 0 )
1266 ++pv
->decode_errors
;
1270 if (pv
->qsv
.decode
&& pv
->job
->qsv
.ctx
== NULL
&& pv
->video_codec_opened
> 0)
1272 // this is quite late, but we can't be certain that the QSV context is
1273 // available until after we call avcodec_decode_video2() at least once
1274 pv
->job
->qsv
.ctx
= pv
->context
->priv_data
;
1278 #ifdef USE_QSV_PTS_WORKAROUND
1279 if (pv
->qsv
.decode
&& got_picture
)
1281 // we got a decoded frame, restore the lowest available PTS
1282 pv
->frame
->pkt_pts
= hb_av_pop_next_pts(pv
->qsv
.pts_list
);
1286 if ( global_verbosity_level
<= 1 )
1288 av_log_set_level( oldlevel
);
1294 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
1295 // packet had a pts we handed it to av_parser_parse (if the packet had
1296 // no pts we set it to AV_NOPTS_VALUE, but before the parse we can't
1297 // distinguish between the start of a video frame with no pts & an
1298 // intermediate packet of some frame which never has a pts). we hope
1299 // that when parse returns the frame to us the pts we originally
1300 // handed it will be in parser->pts. we put this pts into avp.pts so
1301 // that when avcodec_decode_video finally gets around to allocating an
1302 // AVFrame to hold the decoded frame, avcodec_default_get_buffer can
1303 // stuff that pts into the it. if all of these relays worked at this
1304 // point frame.pts should hold the frame's pts from the original data
1305 // stream or AV_NOPTS_VALUE if it didn't have one. in the latter case
1306 // we generate the next pts in sequence for it.
1307 if ( !pv
->frame_duration_set
)
1308 compute_frame_duration( pv
);
1311 double frame_dur
= pv
->duration
;
1312 if ( pv
->frame
->repeat_pict
)
1314 frame_dur
+= pv
->frame
->repeat_pict
* pv
->field_duration
;
1317 if( pv
->dxva2
&& pv
->dxva2
->do_job
== HB_WORK_OK
)
1321 if( pv
->dxva2
->input_pts
[0] != 0 && pv
->dxva2
->input_pts
[1] == 0 )
1322 pv
->frame
->pkt_pts
= pv
->dxva2
->input_pts
[0];
1324 pv
->frame
->pkt_pts
= pv
->dxva2
->input_pts
[0]<pv
->dxva2
->input_pts
[1] ? pv
->dxva2
->input_pts
[0] : pv
->dxva2
->input_pts
[1];
1328 // If there was no pts for this frame, assume constant frame rate
1329 // video & estimate the next frame time from the last & duration.
1330 if (pv
->frame
->pkt_pts
== AV_NOPTS_VALUE
|| hb_hwd_enabled(w
->h
))
1336 pts
= pv
->frame
->pkt_pts
;
1337 // Detect streams with broken out of order timestamps
1338 if (!pv
->brokenTS
&& pv
->frame
->pkt_pts
< pv
->prev_pts
)
1340 hb_log("Broken timestamps detected. Reordering.");
1343 pv
->prev_pts
= pv
->frame
->pkt_pts
;
1346 pv
->pts_next
= pts
+ frame_dur
;
1348 if ( pv
->frame
->top_field_first
)
1350 flags
|= PIC_FLAG_TOP_FIELD_FIRST
;
1352 if ( !pv
->frame
->interlaced_frame
)
1354 flags
|= PIC_FLAG_PROGRESSIVE_FRAME
;
1356 if ( pv
->frame
->repeat_pict
== 1 )
1358 flags
|= PIC_FLAG_REPEAT_FIRST_FIELD
;
1360 if ( pv
->frame
->repeat_pict
== 2 )
1362 flags
|= PIC_FLAG_REPEAT_FRAME
;
1364 int frametype
= get_frame_type(pv
->frame
->pict_type
);
1366 // Check for CC data
1367 AVFrameSideData
*sd
;
1368 sd
= av_frame_get_side_data(pv
->frame
, AV_FRAME_DATA_A53_CC
);
1371 if (!pv
->job
&& pv
->title
&& sd
->size
> 0)
1373 hb_subtitle_t
*subtitle
;
1376 while ((subtitle
= hb_list_item(pv
->title
->list_subtitle
, i
++)))
1379 * Let's call them 608 subs for now even if they aren't,
1380 * since they are the only types we grok.
1382 if (subtitle
->source
== CC608SUB
)
1387 if (subtitle
== NULL
)
1389 subtitle
= calloc(sizeof( hb_subtitle_t
), 1);
1390 subtitle
->track
= hb_list_count(pv
->title
->list_subtitle
);
1392 subtitle
->format
= TEXTSUB
;
1393 subtitle
->source
= CC608SUB
;
1394 subtitle
->config
.dest
= PASSTHRUSUB
;
1395 subtitle
->codec
= WORK_DECCC608
;
1397 snprintf(subtitle
->lang
, sizeof( subtitle
->lang
),
1400 * The language of the subtitles will be the same as the
1401 * first audio track, i.e. the same as the video.
1403 hb_audio_t
*audio
= hb_list_item(pv
->title
->list_audio
, 0);
1406 snprintf(subtitle
->iso639_2
, sizeof(subtitle
->iso639_2
),
1407 "%s", audio
->config
.lang
.iso639_2
);
1409 snprintf(subtitle
->iso639_2
, sizeof(subtitle
->iso639_2
),
1412 hb_list_add(pv
->title
->list_subtitle
, subtitle
);
1415 if (pv
->list_subtitle
!= NULL
&& sd
->size
> 0)
1417 hb_buffer_t
*cc_buf
;
1418 cc_buf
= cc_fill_buffer(pv
, sd
->data
, sd
->size
, pts
);
1419 cc_send_to_decoder(pv
, cc_buf
);
1425 // if we're doing a scan or this content couldn't have been broken
1426 // by Microsoft we don't worry about timestamp reordering
1427 if ( ! pv
->job
|| ! pv
->brokenTS
)
1429 buf
= copy_frame( pv
);
1430 av_frame_unref(pv
->frame
);
1432 buf
->sequence
= sequence
;
1434 buf
->s
.flags
= flags
;
1435 buf
->s
.frametype
= frametype
;
1437 if ( pv
->new_chap
&& buf
->s
.start
>= pv
->chap_time
)
1439 buf
->s
.new_chap
= pv
->new_chap
;
1440 log_chapter( pv
, pv
->new_chap
, buf
->s
.start
);
1444 else if ( pv
->nframes
== 0 && pv
->job
)
1446 log_chapter( pv
, pv
->job
->chapter_start
, buf
->s
.start
);
1448 checkCadence( pv
->cadence
, flags
, buf
->s
.start
);
1449 hb_list_add( pv
->list
, buf
);
1454 // XXX This following probably addresses a libavcodec bug but I don't
1455 // see an easy fix so we workaround it here.
1457 // The M$ 'packed B-frames' atrocity results in decoded frames with
1458 // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
1459 // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
1460 // The frames are actually delivered in the right order but with
1461 // the wrong timestamp. To get the correct timestamp attached to
1462 // each frame we have a delay queue (longer than the max number of
1463 // b-frames) & a sorting heap for the timestamps. As each frame
1464 // comes out of the decoder the oldest frame in the queue is removed
1465 // and associated with the smallest timestamp. Then the new frame is
1466 // added to the queue & its timestamp is pushed on the heap.
1467 // This does nothing if the timestamps are correct (i.e., the video
1468 // uses a codec that Micro$oft hasn't broken yet) but the frames
1469 // get timestamped correctly even when M$ has munged them.
1471 // remove the oldest picture from the frame queue (if any) &
1472 // give it the smallest timestamp from our heap. The queue size
1473 // is a power of two so we get the slot of the oldest by masking
1474 // the frame count & this will become the slot of the newest
1475 // once we've removed & processed the oldest.
1476 int slot
= pv
->nframes
& (HEAP_SIZE
-1);
1477 if ( ( buf
= pv
->delayq
[slot
] ) != NULL
)
1479 pv
->queue_primed
= 1;
1480 buf
->s
.start
= heap_pop( &pv
->pts_heap
);
1481 if ( pv
->new_chap
&& buf
->s
.start
>= pv
->chap_time
)
1483 buf
->s
.new_chap
= pv
->new_chap
;
1484 log_chapter( pv
, pv
->new_chap
, buf
->s
.start
);
1488 else if ( pv
->nframes
== 0 && pv
->job
)
1490 log_chapter( pv
, pv
->job
->chapter_start
, buf
->s
.start
);
1492 checkCadence( pv
->cadence
, buf
->s
.flags
, buf
->s
.start
);
1493 hb_list_add( pv
->list
, buf
);
1496 // add the new frame to the delayq & push its timestamp on the heap
1497 buf
= copy_frame( pv
);
1498 av_frame_unref(pv
->frame
);
1499 buf
->sequence
= sequence
;
1500 /* Store picture flags for later use by filters */
1501 buf
->s
.flags
= flags
;
1502 buf
->s
.frametype
= frametype
;
1503 pv
->delayq
[slot
] = buf
;
1504 heap_push( &pv
->pts_heap
, pts
);
1511 static void decodeVideo( hb_work_object_t
*w
, uint8_t *data
, int size
, int sequence
, int64_t pts
, int64_t dts
, uint8_t frametype
)
1513 hb_work_private_t
*pv
= w
->private_data
;
1516 * The following loop is a do..while because we need to handle both
1517 * data & the flush at the end (signaled by size=0). At the end there's
1518 * generally a frame in the parser & one or more frames in the decoder
1519 * (depending on the bframes setting).
1525 int64_t parser_pts
, parser_dts
;
1528 len
= av_parser_parse2( pv
->parser
, pv
->context
, &pout
, &pout_len
,
1529 data
+ pos
, size
- pos
, pts
, dts
, 0 );
1530 parser_pts
= pv
->parser
->pts
;
1531 parser_dts
= pv
->parser
->dts
;
1536 len
= pout_len
= size
;
1544 decodeFrame( w
, pout
, pout_len
, sequence
, parser_pts
, parser_dts
, frametype
);
1546 } while ( pos
< size
);
1548 /* the stuff above flushed the parser, now flush the decoder */
1551 while (decodeFrame(w
, NULL
, 0, sequence
, AV_NOPTS_VALUE
, AV_NOPTS_VALUE
, 0))
1558 // flush a second time
1559 while (decodeFrame(w
, NULL
, 0, sequence
, AV_NOPTS_VALUE
, AV_NOPTS_VALUE
, 0))
1565 flushDelayQueue(pv
);
1566 if (pv
->list_subtitle
!= NULL
)
1567 cc_send_to_decoder(pv
, hb_buffer_eof_init());
1572 * Removes all packets from 'pv->list', links them together into
1573 * a linked-list, and returns the first packet in the list.
1575 static hb_buffer_t
*link_buf_list( hb_work_private_t
*pv
)
1577 hb_buffer_t
*head
= hb_list_item( pv
->list
, 0 );
1581 hb_list_rem( pv
->list
, head
);
1583 hb_buffer_t
*last
= head
, *buf
;
1585 while ( ( buf
= hb_list_item( pv
->list
, 0 ) ) != NULL
)
1587 hb_list_rem( pv
->list
, buf
);
1595 static int decavcodecvInit( hb_work_object_t
* w
, hb_job_t
* job
)
1598 hb_work_private_t
*pv
= calloc( 1, sizeof( hb_work_private_t
) );
1600 w
->private_data
= pv
;
1601 pv
->wait_for_keyframe
= 60;
1604 pv
->title
= job
->title
;
1606 pv
->title
= w
->title
;
1607 pv
->list
= hb_list_init();
1610 if (hb_qsv_decode_is_enabled(job
))
1612 // determine which encoder we're using
1613 hb_qsv_info_t
*info
= hb_qsv_info_get(job
->vcodec
);
1614 pv
->qsv
.decode
= info
!= NULL
;
1617 // setup the QSV configuration
1618 pv
->qsv
.config
.io_pattern
= MFX_IOPATTERN_OUT_OPAQUE_MEMORY
;
1619 pv
->qsv
.config
.impl_requested
= info
->implementation
;
1620 pv
->qsv
.config
.async_depth
= job
->qsv
.async_depth
;
1621 pv
->qsv
.config
.sync_need
= 0;
1622 pv
->qsv
.config
.usage_threaded
= 1;
1623 pv
->qsv
.config
.additional_buffers
= 64; // FIFO_LARGE
1624 if (info
->capabilities
& HB_QSV_CAP_RATECONTROL_LA
)
1626 // more surfaces may be needed for the lookahead
1627 pv
->qsv
.config
.additional_buffers
= 160;
1629 pv
->qsv
.codec_name
= hb_qsv_decode_get_codec_name(w
->codec_param
);
1638 if( pv
->job
&& pv
->job
->title
&& !pv
->job
->title
->has_resolution_change
)
1640 pv
->threads
= HB_FFMPEG_THREADS_AUTO
;
1643 AVCodec
*codec
= NULL
;
1648 codec
= avcodec_find_decoder_by_name(pv
->qsv
.codec_name
);
1653 codec
= avcodec_find_decoder(w
->codec_param
);
1655 if ( codec
== NULL
)
1657 hb_log( "decavcodecvInit: failed to find codec for id (%d)", w
->codec_param
);
1661 if ( pv
->title
->opaque_priv
)
1663 AVFormatContext
*ic
= (AVFormatContext
*)pv
->title
->opaque_priv
;
1665 pv
->context
= avcodec_alloc_context3(codec
);
1666 avcodec_copy_context( pv
->context
, ic
->streams
[pv
->title
->video_id
]->codec
);
1667 pv
->context
->workaround_bugs
= FF_BUG_AUTODETECT
;
1668 pv
->context
->err_recognition
= AV_EF_CRCCHECK
;
1669 pv
->context
->error_concealment
= FF_EC_GUESS_MVS
|FF_EC_DEBLOCK
;
1671 // QSV decoding is faster, so prefer it to DXVA2
1672 if (pv
->job
!= NULL
&& !pv
->qsv
.decode
&& hb_hwd_enabled(pv
->job
->h
))
1674 pv
->dxva2
= hb_va_create_dxva2( pv
->dxva2
, w
->codec_param
);
1675 if( pv
->dxva2
&& pv
->dxva2
->do_job
== HB_WORK_OK
)
1677 hb_va_new_dxva2( pv
->dxva2
, pv
->context
);
1678 pv
->context
->slice_flags
|= SLICE_FLAG_ALLOW_FIELD
;
1679 pv
->context
->opaque
= pv
;
1680 pv
->context
->get_buffer
= get_frame_buf_hwd
;
1681 pv
->context
->release_buffer
= hb_ffmpeg_release_frame_buf
;
1682 pv
->context
->get_format
= hb_ffmpeg_get_format
;
1683 pv
->opencl_scale
= ( hb_oclscale_t
* )malloc( sizeof( hb_oclscale_t
) );
1684 memset( pv
->opencl_scale
, 0, sizeof( hb_oclscale_t
) );
1689 hb_log("decavcodecvInit: hb_va_create_dxva2 failed, using software decoder");
1698 #ifdef USE_QSV_PTS_WORKAROUND
1699 pv
->qsv
.pts_list
= hb_list_init();
1701 // set the QSV configuration before opening the decoder
1702 pv
->context
->hwaccel_context
= &pv
->qsv
.config
;
1706 // Set encoder opts...
1707 AVDictionary
* av_opts
= NULL
;
1708 av_dict_set( &av_opts
, "refcounted_frames", "1", 0 );
1709 if (pv
->title
->flags
& HBTF_NO_IDR
)
1711 av_dict_set( &av_opts
, "flags", "output_corrupt", 0 );
1714 if ( hb_avcodec_open( pv
->context
, codec
, &av_opts
, pv
->threads
) )
1716 av_dict_free( &av_opts
);
1717 hb_log( "decavcodecvInit: avcodec_open failed" );
1720 av_dict_free( &av_opts
);
1722 pv
->video_codec_opened
= 1;
1723 // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
1724 // b-frames abortion that messes up frame ordering and timestamps.
1725 // XXX ffmpeg knows which streams are broken but doesn't expose the
1726 // info externally. We should patch ffmpeg to add a flag to the
1727 // codec context for this but until then we mark all ffmpeg streams
1733 pv
->parser
= av_parser_init( w
->codec_param
);
1736 pv
->frame
= av_frame_alloc();
1737 if (pv
->frame
== NULL
)
1739 hb_log("decavcodecvInit: av_frame_alloc failed");
1744 * If not scanning, then are we supposed to extract Closed Captions
1745 * and send them to the decoder?
1747 if (job
!= NULL
&& hb_list_count(job
->list_subtitle
) > 0)
1749 hb_subtitle_t
*subtitle
;
1752 while ((subtitle
= hb_list_item(job
->list_subtitle
, i
++)) != NULL
)
1754 if (subtitle
->source
== CC608SUB
)
1756 if (pv
->list_subtitle
== NULL
)
1758 pv
->list_subtitle
= hb_list_init();
1760 hb_list_add(pv
->list_subtitle
, subtitle
);
1767 static int setup_extradata( hb_work_object_t
*w
, hb_buffer_t
*in
)
1769 hb_work_private_t
*pv
= w
->private_data
;
1771 // we can't call the avstream funcs but the read_header func in the
1772 // AVInputFormat may set up some state in the AVContext. In particular
1773 // vc1t_read_header allocates 'extradata' to deal with header issues
1774 // related to Microsoft's bizarre engineering notions. We alloc a chunk
1775 // of space to make vc1 work then associate the codec with the context.
1776 if (pv
->context
->extradata
== NULL
)
1778 if (pv
->parser
== NULL
|| pv
->parser
== NULL
||
1779 pv
->parser
->parser
->split
== NULL
)
1786 size
= pv
->parser
->parser
->split(pv
->context
, in
->data
, in
->size
);
1789 pv
->context
->extradata_size
= size
;
1790 pv
->context
->extradata
=
1791 av_malloc(size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
1792 if (pv
->context
->extradata
== NULL
)
1794 memcpy(pv
->context
->extradata
, in
->data
, size
);
1804 static int decavcodecvWork( hb_work_object_t
* w
, hb_buffer_t
** buf_in
,
1805 hb_buffer_t
** buf_out
)
1807 hb_work_private_t
*pv
= w
->private_data
;
1808 hb_buffer_t
*in
= *buf_in
;
1809 int64_t pts
= AV_NOPTS_VALUE
;
1815 /* if we got an empty buffer signaling end-of-stream send it downstream */
1816 if (in
->s
.flags
& HB_BUF_FLAG_EOF
)
1818 if (pv
->context
!= NULL
&& pv
->context
->codec
!= NULL
)
1820 decodeVideo(w
, in
->data
, 0, 0, pts
, dts
, 0);
1822 hb_list_add( pv
->list
, in
);
1823 *buf_out
= link_buf_list( pv
);
1824 return HB_WORK_DONE
;
1827 // if this is the first frame open the codec (we have to wait for the
1828 // first frame because of M$ VC1 braindamage).
1829 if ( !pv
->video_codec_opened
)
1832 AVCodec
*codec
= NULL
;
1836 codec
= avcodec_find_decoder_by_name(pv
->qsv
.codec_name
);
1841 codec
= avcodec_find_decoder(w
->codec_param
);
1844 if ( codec
== NULL
)
1846 hb_log( "decavcodecvWork: failed to find codec for id (%d)", w
->codec_param
);
1847 *buf_out
= hb_buffer_eof_init();
1848 return HB_WORK_DONE
;
1851 pv
->context
= avcodec_alloc_context3( codec
);
1852 pv
->context
->workaround_bugs
= FF_BUG_AUTODETECT
;
1853 pv
->context
->err_recognition
= AV_EF_CRCCHECK
;
1854 pv
->context
->error_concealment
= FF_EC_GUESS_MVS
|FF_EC_DEBLOCK
;
1856 if ( setup_extradata( w
, in
) )
1858 // we didn't find the headers needed to set up extradata.
1859 // the codec will abort if we open it so just free the buf
1860 // and hope we eventually get the info we need.
1861 hb_buffer_close( &in
);
1868 #ifdef USE_QSV_PTS_WORKAROUND
1869 pv
->qsv
.pts_list
= hb_list_init();
1871 // set the QSV configuration before opening the decoder
1872 pv
->context
->hwaccel_context
= &pv
->qsv
.config
;
1876 AVDictionary
* av_opts
= NULL
;
1877 av_dict_set( &av_opts
, "refcounted_frames", "1", 0 );
1878 if (pv
->title
->flags
& HBTF_NO_IDR
)
1880 av_dict_set( &av_opts
, "flags", "output_corrupt", 0 );
1883 // disable threaded decoding for scan, can cause crashes
1884 if ( hb_avcodec_open( pv
->context
, codec
, &av_opts
, pv
->threads
) )
1886 av_dict_free( &av_opts
);
1887 hb_log( "decavcodecvWork: avcodec_open failed" );
1888 *buf_out
= hb_buffer_eof_init();
1889 return HB_WORK_DONE
;
1891 av_dict_free( &av_opts
);
1892 pv
->video_codec_opened
= 1;
1895 if( in
->s
.start
>= 0 )
1898 dts
= in
->s
.renderOffset
;
1900 if ( in
->s
.new_chap
)
1902 pv
->new_chap
= in
->s
.new_chap
;
1903 pv
->chap_time
= pts
>= 0? pts
: pv
->pts_next
;
1906 if( pv
->dxva2
&& pv
->dxva2
->do_job
== HB_WORK_OK
)
1908 if( pv
->dxva2
->input_pts
[0] <= pv
->dxva2
->input_pts
[1] )
1909 pv
->dxva2
->input_pts
[0] = pts
;
1910 else if( pv
->dxva2
->input_pts
[0] > pv
->dxva2
->input_pts
[1] )
1911 pv
->dxva2
->input_pts
[1] = pts
;
1912 pv
->dxva2
->input_dts
= dts
;
1915 if (in
->palette
!= NULL
)
1917 pv
->palette
= in
->palette
;
1920 decodeVideo( w
, in
->data
, in
->size
, in
->sequence
, pts
, dts
, in
->s
.frametype
);
1921 hb_buffer_close( &in
);
1922 *buf_out
= link_buf_list( pv
);
1926 static void compute_frame_duration( hb_work_private_t
*pv
)
1928 double duration
= 0.;
1929 int64_t max_fps
= 64L;
1931 // context->time_base may be in fields, so set the max *fields* per second
1932 if ( pv
->context
->ticks_per_frame
> 1 )
1933 max_fps
*= pv
->context
->ticks_per_frame
;
1935 if ( pv
->title
->opaque_priv
)
1937 // If ffmpeg is demuxing for us, it collects some additional
1938 // information about framerates that is often more accurate
1939 // than context->time_base.
1940 AVFormatContext
*ic
= (AVFormatContext
*)pv
->title
->opaque_priv
;
1941 AVStream
*st
= ic
->streams
[pv
->title
->video_id
];
1942 if ( st
->nb_frames
&& st
->duration
)
1944 // compute the average frame duration from the total number
1945 // of frames & the total duration.
1946 duration
= ( (double)st
->duration
* (double)st
->time_base
.num
) /
1947 ( (double)st
->nb_frames
* (double)st
->time_base
.den
);
1949 // Raw demuxers set a default fps of 25 and do not parse
1950 // a value from the container. So use the codec time_base
1951 // for raw demuxers.
1952 else if (ic
->iformat
->raw_codec_id
== AV_CODEC_ID_NONE
)
1954 // XXX We don't have a frame count or duration so try to use the
1955 // far less reliable time base info in the stream.
1956 // Because the time bases are so screwed up, we only take values
1957 // in the range 8fps - 64fps.
1958 AVRational
*tb
= NULL
;
1959 if ( st
->avg_frame_rate
.den
* 64L > st
->avg_frame_rate
.num
&&
1960 st
->avg_frame_rate
.num
> st
->avg_frame_rate
.den
* 8L )
1962 tb
= &(st
->avg_frame_rate
);
1963 duration
= (double)tb
->den
/ (double)tb
->num
;
1965 else if ( st
->time_base
.num
* 64L > st
->time_base
.den
&&
1966 st
->time_base
.den
> st
->time_base
.num
* 8L )
1968 tb
= &(st
->time_base
);
1969 duration
= (double)tb
->num
/ (double)tb
->den
;
1973 pv
->context
->time_base
.num
* max_fps
> pv
->context
->time_base
.den
&&
1974 pv
->context
->time_base
.den
> pv
->context
->time_base
.num
* 8L )
1976 duration
= (double)pv
->context
->time_base
.num
/
1977 (double)pv
->context
->time_base
.den
;
1978 if ( pv
->context
->ticks_per_frame
> 1 )
1980 // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
1981 // field rate rather than frame rate so convert back to frames.
1982 duration
*= pv
->context
->ticks_per_frame
;
1988 if ( pv
->context
->time_base
.num
* max_fps
> pv
->context
->time_base
.den
&&
1989 pv
->context
->time_base
.den
> pv
->context
->time_base
.num
* 8L )
1991 duration
= (double)pv
->context
->time_base
.num
/
1992 (double)pv
->context
->time_base
.den
;
1993 if ( pv
->context
->ticks_per_frame
> 1 )
1995 // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
1996 // field rate rather than frame rate so convert back to frames.
1997 duration
*= pv
->context
->ticks_per_frame
;
2001 if ( duration
== 0 )
2003 // No valid timing info found in the stream, so pick some value
2004 duration
= 1001. / 24000.;
2008 pv
->frame_duration_set
= 1;
2010 pv
->duration
= duration
* 90000.;
2011 pv
->field_duration
= pv
->duration
;
2012 if ( pv
->context
->ticks_per_frame
> 1 )
2014 pv
->field_duration
/= pv
->context
->ticks_per_frame
;
2018 static int decavcodecvInfo( hb_work_object_t
*w
, hb_work_info_t
*info
)
2020 hb_work_private_t
*pv
= w
->private_data
;
2022 memset( info
, 0, sizeof(*info
) );
2024 if (pv
->context
== NULL
)
2027 info
->bitrate
= pv
->context
->bit_rate
;
2028 // HandBrake's video pipeline uses yuv420 color. This means all
2029 // dimensions must be even. So we must adjust the dimensions
2030 // of incoming video if not even.
2031 info
->geometry
.width
= pv
->context
->width
& ~1;
2032 info
->geometry
.height
= pv
->context
->height
& ~1;
2034 info
->geometry
.par
.num
= pv
->context
->sample_aspect_ratio
.num
;
2035 info
->geometry
.par
.den
= pv
->context
->sample_aspect_ratio
.den
;
2037 compute_frame_duration( pv
);
2038 info
->rate
.num
= 27000000;
2039 info
->rate
.den
= pv
->duration
* 300.;
2041 info
->profile
= pv
->context
->profile
;
2042 info
->level
= pv
->context
->level
;
2043 info
->name
= pv
->context
->codec
->name
;
2045 switch( pv
->context
->color_primaries
)
2047 case AVCOL_PRI_BT709
:
2048 info
->color_prim
= HB_COLR_PRI_BT709
;
2050 case AVCOL_PRI_BT470BG
:
2051 info
->color_prim
= HB_COLR_PRI_EBUTECH
;
2053 case AVCOL_PRI_BT470M
:
2054 case AVCOL_PRI_SMPTE170M
:
2055 case AVCOL_PRI_SMPTE240M
:
2056 info
->color_prim
= HB_COLR_PRI_SMPTEC
;
2060 if ((info
->geometry
.width
>= 1280 || info
->geometry
.height
>= 720)||
2061 (info
->geometry
.width
> 720 && info
->geometry
.height
> 576 ))
2062 // ITU BT.709 HD content
2063 info
->color_prim
= HB_COLR_PRI_BT709
;
2064 else if( info
->rate
.den
== 1080000 )
2065 // ITU BT.601 DVD or SD TV content (PAL)
2066 info
->color_prim
= HB_COLR_PRI_EBUTECH
;
2068 // ITU BT.601 DVD or SD TV content (NTSC)
2069 info
->color_prim
= HB_COLR_PRI_SMPTEC
;
2074 switch( pv
->context
->color_trc
)
2076 case AVCOL_TRC_SMPTE240M
:
2077 info
->color_transfer
= HB_COLR_TRA_SMPTE240M
;
2080 // ITU BT.601, BT.709, anything else
2081 info
->color_transfer
= HB_COLR_TRA_BT709
;
2085 switch( pv
->context
->colorspace
)
2087 case AVCOL_SPC_BT709
:
2088 info
->color_matrix
= HB_COLR_MAT_BT709
;
2091 case AVCOL_SPC_BT470BG
:
2092 case AVCOL_SPC_SMPTE170M
:
2093 case AVCOL_SPC_RGB
: // libswscale rgb2yuv
2094 info
->color_matrix
= HB_COLR_MAT_SMPTE170M
;
2096 case AVCOL_SPC_SMPTE240M
:
2097 info
->color_matrix
= HB_COLR_MAT_SMPTE240M
;
2101 if ((info
->geometry
.width
>= 1280 || info
->geometry
.height
>= 720)||
2102 (info
->geometry
.width
> 720 && info
->geometry
.height
> 576 ))
2103 // ITU BT.709 HD content
2104 info
->color_matrix
= HB_COLR_MAT_BT709
;
2106 // ITU BT.601 DVD or SD TV content (PAL)
2107 // ITU BT.601 DVD or SD TV content (NTSC)
2108 info
->color_matrix
= HB_COLR_MAT_SMPTE170M
;
2113 info
->video_decode_support
= HB_DECODE_SUPPORT_SW
;
2114 switch (pv
->context
->codec_id
)
2116 case AV_CODEC_ID_H264
:
2117 if (pv
->context
->pix_fmt
== AV_PIX_FMT_YUV420P
||
2118 pv
->context
->pix_fmt
== AV_PIX_FMT_YUVJ420P
)
2121 info
->video_decode_support
|= HB_DECODE_SUPPORT_QSV
;
2130 hb_va_dxva2_t
*dxva2
= hb_va_create_dxva2(NULL
, pv
->context
->codec_id
);
2133 if (hb_check_hwd_fmt(pv
->context
->pix_fmt
))
2135 info
->video_decode_support
|= HB_DECODE_SUPPORT_DXVA2
;
2144 static int decavcodecvBSInfo( hb_work_object_t
*w
, const hb_buffer_t
*buf
,
2145 hb_work_info_t
*info
)
2150 static void decavcodecvFlush( hb_work_object_t
*w
)
2152 hb_work_private_t
*pv
= w
->private_data
;
2154 if (pv
->context
!= NULL
&& pv
->context
->codec
!= NULL
)
2156 flushDelayQueue( pv
);
2157 hb_buffer_t
*buf
= link_buf_list( pv
);
2158 hb_buffer_close( &buf
);
2159 if ( pv
->title
->opaque_priv
== NULL
)
2161 pv
->video_codec_opened
= 0;
2162 hb_avcodec_close( pv
->context
);
2163 av_freep( &pv
->context
->extradata
);
2164 av_freep( &pv
->context
);
2167 av_parser_close(pv
->parser
);
2169 pv
->parser
= av_parser_init( w
->codec_param
);
2173 avcodec_flush_buffers( pv
->context
);
2176 pv
->wait_for_keyframe
= 60;
2179 hb_work_object_t hb_decavcodecv
=
2181 .id
= WORK_DECAVCODECV
,
2182 .name
= "Video decoder (libavcodec)",
2183 .init
= decavcodecvInit
,
2184 .work
= decavcodecvWork
,
2185 .close
= decavcodecClose
,
2186 .flush
= decavcodecvFlush
,
2187 .info
= decavcodecvInfo
,
2188 .bsinfo
= decavcodecvBSInfo
2190 static void decodeAudio(hb_audio_t
*audio
, hb_work_private_t
*pv
, uint8_t *data
,
2191 int size
, int64_t pts
)
2193 AVCodecContext
*context
= pv
->context
;
2194 int loop_limit
= 256;
2197 // If we are given a pts, use it; but don't lose partial ticks.
2198 if (pts
!= AV_NOPTS_VALUE
&& (int64_t)pv
->pts_next
!= pts
)
2205 av_init_packet(&avp
);
2206 avp
.data
= data
+ pos
;
2207 avp
.size
= size
- pos
;
2208 avp
.pts
= pv
->pts_next
;
2209 avp
.dts
= AV_NOPTS_VALUE
;
2211 int len
= avcodec_decode_audio4(context
, pv
->frame
, &got_frame
, &avp
);
2212 if ((len
< 0) || (!got_frame
&& !(loop_limit
--)))
2227 // libavcoded doesn't yet consistently set frame->sample_rate
2228 if (pv
->frame
->sample_rate
!= 0)
2230 samplerate
= pv
->frame
->sample_rate
;
2234 samplerate
= context
->sample_rate
;
2236 double duration
= (90000. * pv
->frame
->nb_samples
/
2237 (double)samplerate
);
2239 if (audio
->config
.out
.codec
& HB_ACODEC_PASS_FLAG
)
2241 // Note that even though we are doing passthru, we had to decode
2242 // so that we know the stop time and the pts of the next audio
2244 out
= hb_buffer_init(avp
.size
);
2245 memcpy(out
->data
, avp
.data
, avp
.size
);
2249 AVFrameSideData
*side_data
;
2251 av_frame_get_side_data(pv
->frame
,
2252 AV_FRAME_DATA_DOWNMIX_INFO
)) != NULL
)
2254 double surround_mix_level
, center_mix_level
;
2255 AVDownmixInfo
*downmix_info
= (AVDownmixInfo
*)side_data
->data
;
2256 if (audio
->config
.out
.mixdown
== HB_AMIXDOWN_DOLBY
||
2257 audio
->config
.out
.mixdown
== HB_AMIXDOWN_DOLBYPLII
)
2259 surround_mix_level
= downmix_info
->surround_mix_level_ltrt
;
2260 center_mix_level
= downmix_info
->center_mix_level_ltrt
;
2264 surround_mix_level
= downmix_info
->surround_mix_level
;
2265 center_mix_level
= downmix_info
->center_mix_level
;
2267 hb_audio_resample_set_mix_levels(pv
->resample
,
2270 downmix_info
->lfe_mix_level
);
2272 hb_audio_resample_set_channel_layout(pv
->resample
,
2273 pv
->frame
->channel_layout
);
2274 hb_audio_resample_set_sample_fmt(pv
->resample
,
2276 if (hb_audio_resample_update(pv
->resample
))
2278 hb_log("decavcodec: hb_audio_resample_update() failed");
2279 av_frame_unref(pv
->frame
);
2282 out
= hb_audio_resample(pv
->resample
, pv
->frame
->extended_data
,
2283 pv
->frame
->nb_samples
);
2285 av_frame_unref(pv
->frame
);
2289 out
->s
.start
= pv
->pts_next
;
2290 out
->s
.duration
= duration
;
2291 out
->s
.stop
= duration
+ pv
->pts_next
;
2292 pv
->pts_next
= duration
+ pv
->pts_next
;
2293 hb_list_add(pv
->list
, out
);