githubのコードからのfolk
Revision | 6d1d20e364106fb397d9c131ff0fc7d01452d5ba (tree) |
---|---|
Time | 2010-10-09 05:30:53 |
Author | jstebbins <jstebbins@b64f...> |
Commiter | jstebbins |
cli: make smarter mixdown decision when doing ac3 encode fallback
also, clean up the mixdown sanitizing logic in work.c
added new functions
hb_get_default_mix(codec, layout)
hb_get_best_mix(codec, layout)
These take the output codec and the input layout as parameters.
git-svn-id: svn://localhost/HandBrake/trunk@3580 b64f7644-9d1e-0410-96f1-a4d463321fa5
@@ -201,6 +201,108 @@ int hb_get_default_audio_bitrate( uint32_t codec, int samplerate, int mixdown ) | ||
201 | 201 | return bitrate; |
202 | 202 | } |
203 | 203 | |
204 | +int hb_get_best_mixdown( uint32_t codec, int layout ) | |
205 | +{ | |
206 | + switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK) | |
207 | + { | |
208 | + // stereo input or something not handled below | |
209 | + default: | |
210 | + case HB_INPUT_CH_LAYOUT_STEREO: | |
211 | + // mono gets mixed up to stereo & more than stereo gets mixed down | |
212 | + return HB_AMIXDOWN_STEREO; | |
213 | + | |
214 | + // mono input | |
215 | + case HB_INPUT_CH_LAYOUT_MONO: | |
216 | + // everything else passes through | |
217 | + return HB_AMIXDOWN_MONO; | |
218 | + | |
219 | + // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input | |
220 | + // the A52 flags don't allow for a way to distinguish between DPL1 and | |
221 | + // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY. | |
222 | + case HB_INPUT_CH_LAYOUT_DOLBY: | |
223 | + return HB_AMIXDOWN_DOLBY; | |
224 | + | |
225 | + // 4 channel discrete | |
226 | + case HB_INPUT_CH_LAYOUT_2F2R: | |
227 | + case HB_INPUT_CH_LAYOUT_3F1R: | |
228 | + // a52dec and libdca can't upmix to 6ch, | |
229 | + // so we must downmix these. | |
230 | + return HB_AMIXDOWN_DOLBYPLII; | |
231 | + | |
232 | + // 5 or 6 channel discrete | |
233 | + case HB_INPUT_CH_LAYOUT_3F2R: | |
234 | + if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) ) | |
235 | + { | |
236 | + // we don't do 5 channel discrete so mixdown to DPLII | |
237 | + // a52dec and libdca can't upmix to 6ch, | |
238 | + // so we must downmix this. | |
239 | + return HB_AMIXDOWN_DOLBYPLII; | |
240 | + } | |
241 | + else | |
242 | + { | |
243 | + switch (codec) | |
244 | + { | |
245 | + case HB_ACODEC_LAME: | |
246 | + return HB_AMIXDOWN_DOLBYPLII; | |
247 | + | |
248 | + default: | |
249 | + return HB_AMIXDOWN_6CH; | |
250 | + } | |
251 | + } | |
252 | + } | |
253 | +} | |
254 | + | |
255 | +int hb_get_default_mixdown( uint32_t codec, int layout ) | |
256 | +{ | |
257 | + switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK) | |
258 | + { | |
259 | + // stereo input or something not handled below | |
260 | + default: | |
261 | + case HB_INPUT_CH_LAYOUT_STEREO: | |
262 | + // mono gets mixed up to stereo & more than stereo gets mixed down | |
263 | + return HB_AMIXDOWN_STEREO; | |
264 | + | |
265 | + // mono input | |
266 | + case HB_INPUT_CH_LAYOUT_MONO: | |
267 | + // everything else passes through | |
268 | + return HB_AMIXDOWN_MONO; | |
269 | + | |
270 | + // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input | |
271 | + // the A52 flags don't allow for a way to distinguish between DPL1 and | |
272 | + // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY. | |
273 | + case HB_INPUT_CH_LAYOUT_DOLBY: | |
274 | + return HB_AMIXDOWN_DOLBY; | |
275 | + | |
276 | + // 4 channel discrete | |
277 | + case HB_INPUT_CH_LAYOUT_2F2R: | |
278 | + case HB_INPUT_CH_LAYOUT_3F1R: | |
279 | + // a52dec and libdca can't upmix to 6ch, | |
280 | + // so we must downmix these. | |
281 | + return HB_AMIXDOWN_DOLBYPLII; | |
282 | + | |
283 | + // 5 or 6 channel discrete | |
284 | + case HB_INPUT_CH_LAYOUT_3F2R: | |
285 | + if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) ) | |
286 | + { | |
287 | + // we don't do 5 channel discrete so mixdown to DPLII | |
288 | + // a52dec and libdca can't upmix to 6ch, | |
289 | + // so we must downmix this. | |
290 | + return HB_AMIXDOWN_DOLBYPLII; | |
291 | + } | |
292 | + else | |
293 | + { | |
294 | + switch (codec) | |
295 | + { | |
296 | + case HB_ACODEC_AC3: | |
297 | + return HB_AMIXDOWN_6CH; | |
298 | + | |
299 | + default: | |
300 | + return HB_AMIXDOWN_DOLBYPLII; | |
301 | + } | |
302 | + } | |
303 | + } | |
304 | +} | |
305 | + | |
204 | 306 | /********************************************************************** |
205 | 307 | * hb_reduce |
206 | 308 | ********************************************************************** |
@@ -150,6 +150,8 @@ extern hb_mixdown_t hb_audio_mixdowns[]; | ||
150 | 150 | extern int hb_audio_mixdowns_count; |
151 | 151 | int hb_mixdown_get_mixdown_from_short_name( const char * short_name ); |
152 | 152 | const char * hb_mixdown_get_short_name_from_mixdown( int amixdown ); |
153 | +int hb_get_best_mixdown( uint32_t codec, int layout ); | |
154 | +int hb_get_default_mixdown( uint32_t codec, int layout ); | |
153 | 155 | int hb_find_closest_audio_bitrate(int bitrate); |
154 | 156 | void hb_get_audio_bitrate_limits(uint32_t codec, int samplerate, int mixdown, int *low, int *high); |
155 | 157 | int hb_get_best_audio_bitrate( uint32_t codec, int bitrate, int samplerate, int mixdown); |
@@ -559,100 +559,55 @@ static void do_job( hb_job_t * job, int cpu_count ) | ||
559 | 559 | } |
560 | 560 | |
561 | 561 | int requested_mixdown = 0; |
562 | + int best_mixdown = 0; | |
562 | 563 | int requested_mixdown_index = 0; |
563 | 564 | |
564 | 565 | for( i = 0; i < hb_list_count( title->list_audio ); i++ ) |
565 | 566 | { |
566 | 567 | audio = hb_list_item( title->list_audio, i ); |
567 | 568 | |
568 | - if( audio->config.out.codec != audio->config.in.codec ) | |
569 | - { | |
570 | - /* sense-check the current mixdown options */ | |
569 | + best_mixdown = hb_get_best_mixdown( audio->config.out.codec, | |
570 | + audio->config.in.channel_layout ); | |
571 | 571 | |
572 | - /* log the requested mixdown */ | |
573 | - for (j = 0; j < hb_audio_mixdowns_count; j++) { | |
574 | - if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown) { | |
575 | - requested_mixdown = audio->config.out.mixdown; | |
576 | - requested_mixdown_index = j; | |
577 | - } | |
578 | - } | |
572 | + /* sense-check the current mixdown options */ | |
579 | 573 | |
580 | - /* sense-check the requested mixdown */ | |
581 | - | |
582 | - if( audio->config.out.mixdown == 0 && | |
583 | - audio->config.out.codec != HB_ACODEC_AC3_PASS && | |
584 | - audio->config.out.codec != HB_ACODEC_DCA_PASS ) | |
585 | - { | |
586 | - /* | |
587 | - * Mixdown wasn't specified and this is not pass-through, | |
588 | - * set a default mixdown of stereo. | |
589 | - */ | |
590 | - audio->config.out.mixdown = HB_AMIXDOWN_STEREO; | |
574 | + /* log the requested mixdown */ | |
575 | + for (j = 0; j < hb_audio_mixdowns_count; j++) { | |
576 | + if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown) { | |
577 | + requested_mixdown = audio->config.out.mixdown; | |
578 | + requested_mixdown_index = j; | |
591 | 579 | } |
580 | + } | |
592 | 581 | |
593 | - // Here we try to sanitize the audio input to output mapping. | |
594 | - // Constraints are: | |
595 | - // 1. only the AC3 & DCA decoder libraries currently support mixdown | |
596 | - // 2. the lame encoder library only supports stereo. | |
597 | - // So if the encoder is lame we need the output to be stereo (or multichannel | |
598 | - // matrixed into stereo like dpl). If the decoder is not AC3 or DCA the | |
599 | - // encoder has to handle the input format since we can't do a mixdown. | |
600 | - switch (audio->config.in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK) | |
601 | - { | |
602 | - // stereo input or something not handled below | |
603 | - default: | |
604 | - case HB_INPUT_CH_LAYOUT_STEREO: | |
605 | - // mono gets mixed up to stereo & more than stereo gets mixed down | |
606 | - if ( audio->config.out.mixdown > HB_AMIXDOWN_STEREO ) | |
607 | - { | |
608 | - audio->config.out.mixdown = HB_AMIXDOWN_STEREO; | |
609 | - } | |
610 | - break; | |
611 | - | |
612 | - // mono input | |
613 | - case HB_INPUT_CH_LAYOUT_MONO: | |
614 | - // everything else passes through | |
615 | - audio->config.out.mixdown = HB_AMIXDOWN_MONO; | |
616 | - break; | |
617 | - | |
618 | - // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input | |
619 | - // the A52 flags don't allow for a way to distinguish between DPL1 and | |
620 | - // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY. | |
621 | - case HB_INPUT_CH_LAYOUT_DOLBY: | |
622 | - if ( audio->config.out.mixdown > HB_AMIXDOWN_DOLBY ) | |
623 | - { | |
624 | - audio->config.out.mixdown = HB_AMIXDOWN_DOLBY; | |
625 | - } | |
626 | - break; | |
627 | - | |
628 | - // 4 channel discrete | |
629 | - case HB_INPUT_CH_LAYOUT_2F2R: | |
630 | - case HB_INPUT_CH_LAYOUT_3F1R: | |
631 | - if ( audio->config.out.mixdown > HB_AMIXDOWN_DOLBY ) | |
632 | - { | |
633 | - audio->config.out.mixdown = HB_AMIXDOWN_DOLBY; | |
634 | - } | |
635 | - break; | |
582 | + /* sense-check the requested mixdown */ | |
583 | + if( audio->config.out.mixdown == 0 && | |
584 | + audio->config.out.codec != HB_ACODEC_AC3_PASS && | |
585 | + audio->config.out.codec != HB_ACODEC_DCA_PASS ) | |
586 | + { | |
587 | + /* | |
588 | + * Mixdown wasn't specified and this is not pass-through, | |
589 | + * set a default mixdown | |
590 | + */ | |
591 | + audio->config.out.mixdown = best_mixdown; | |
592 | + } | |
636 | 593 | |
637 | - // 5 or 6 channel discrete | |
638 | - case HB_INPUT_CH_LAYOUT_3F2R: | |
639 | - if ( ! ( audio->config.in.channel_layout & | |
640 | - HB_INPUT_CH_LAYOUT_HAS_LFE ) ) | |
641 | - { | |
642 | - // we don't do 5 channel discrete so mixdown to DPLII | |
643 | - audio->config.out.mixdown = HB_AMIXDOWN_DOLBYPLII; | |
644 | - } | |
645 | - break; | |
594 | + if ( !( audio->config.out.codec & HB_ACODEC_PASS_FLAG ) ) | |
595 | + { | |
596 | + if ( audio->config.out.mixdown > best_mixdown ) | |
597 | + { | |
598 | + audio->config.out.mixdown = best_mixdown; | |
646 | 599 | } |
600 | + } | |
647 | 601 | |
602 | + if ( audio->config.out.mixdown != requested_mixdown ) | |
603 | + { | |
648 | 604 | /* log the output mixdown */ |
649 | - for (j = 0; j < hb_audio_mixdowns_count; j++) { | |
650 | - if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown) { | |
651 | - if ( audio->config.out.mixdown != requested_mixdown ) | |
652 | - { | |
653 | - hb_log("work: sanitizing track %i mixdown %s to %s", i, hb_audio_mixdowns[requested_mixdown_index].human_readable_name, hb_audio_mixdowns[j].human_readable_name); | |
654 | - } | |
655 | - break; | |
605 | + for (j = 0; j < hb_audio_mixdowns_count; j++) | |
606 | + { | |
607 | + if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown) | |
608 | + { | |
609 | + hb_log("work: sanitizing track %i mixdown %s to %s", i, hb_audio_mixdowns[requested_mixdown_index].human_readable_name, hb_audio_mixdowns[j].human_readable_name); | |
610 | + break; | |
656 | 611 | } |
657 | 612 | } |
658 | 613 | } |
@@ -1796,26 +1796,11 @@ static int HandleEvents( hb_handle_t * h ) | ||
1796 | 1796 | ( audio->out.codec & HB_ACODEC_PASS_FLAG ) && |
1797 | 1797 | !( audio->out.codec & audio->in.codec ) ) |
1798 | 1798 | { |
1799 | - int channels; | |
1800 | 1799 | audio->out.codec = HB_ACODEC_AC3; |
1801 | - channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT(audio->in.channel_layout); | |
1802 | - // bitrate setting is a placeholder till we get | |
1803 | - // defaults and limits implemented in libhb | |
1804 | - if (channels == 1) | |
1805 | - { | |
1806 | - audio->out.mixdown = HB_AMIXDOWN_MONO; | |
1807 | - audio->out.bitrate = 96; | |
1808 | - } | |
1809 | - if (channels == 2) | |
1810 | - { | |
1811 | - audio->out.mixdown = HB_AMIXDOWN_DOLBYPLII; | |
1812 | - audio->out.bitrate = 224; | |
1813 | - } | |
1814 | - else | |
1815 | - { | |
1816 | - audio->out.mixdown = HB_AMIXDOWN_6CH; | |
1817 | - audio->out.bitrate = 640; | |
1818 | - } | |
1800 | + audio->out.mixdown = hb_get_default_mixdown( audio->out.codec, audio->in.channel_layout ); | |
1801 | + audio->out.bitrate = hb_get_default_audio_bitrate( | |
1802 | + audio->out.codec, audio->out.samplerate, | |
1803 | + audio->out.mixdown ); | |
1819 | 1804 | } |
1820 | 1805 | // fix 'copy' to select a specific codec |
1821 | 1806 | if ( audio->out.codec & HB_ACODEC_PASS_FLAG ) |