decavcodec: fix issues with audio that has no explicit channel_layout
authorJohn Stebbins <jstebbins.hb@gmail.com>
Mon, 4 Jun 2018 18:04:11 +0000 (11:04 -0700)
committerJohn Stebbins <jstebbins.hb@gmail.com>
Mon, 4 Jun 2018 18:04:11 +0000 (11:04 -0700)
ffmpeg doesn't set a default channel layout for audio that has no
explicit layout (e.g. pcm_216le).  So we need to guess it from the
number of channels.

Fixes "no audio" in https://github.com/HandBrake/HandBrake/issues/1387

libhb/decavcodec.c

index dd044f3a8ac4d57beb2ccd10bcdee330c90f4c13..db677db1c83243d5e3faab6c3a5bcc3ba0ef617b 100644 (file)
@@ -738,18 +738,28 @@ static int decavcodecaBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
                     info->sample_bit_depth  = context->bits_per_raw_sample;
 
                     int bps = av_get_bits_per_sample(context->codec_id);
-                    int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
-                    if (bps > 0)
+                    int channels;
+                    if (frame->channel_layout != 0)
                     {
-                        info->bitrate = bps * channels * info->rate.num;
+                        channels = av_get_channel_layout_nb_channels(
+                                                        frame->channel_layout);
                     }
-                    else if (context->bit_rate > 0)
+                    else
                     {
-                        info->bitrate = context->bit_rate;
+                        channels = frame->channels;
                     }
-                    else
+
+                    info->bitrate = bps * channels * info->rate.num;
+                    if (info->bitrate <= 0)
                     {
-                        info->bitrate = 1;
+                        if (context->bit_rate > 0)
+                        {
+                            info->bitrate = context->bit_rate;
+                        }
+                        else
+                        {
+                            info->bitrate = 1;
+                        }
                     }
 
                     if (truehd_mono)
@@ -780,6 +790,13 @@ static int decavcodecaBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
                             info->channel_layout = frame->channel_layout;
                         }
                     }
+                    if (info->channel_layout == 0)
+                    {
+                        // Channel layout was not set.  Guess a layout based
+                        // on number of channels.
+                        info->channel_layout = av_get_default_channel_layout(
+                                                            frame->channels);
+                    }
                     if (context->codec_id == AV_CODEC_ID_AC3 ||
                         context->codec_id == AV_CODEC_ID_EAC3)
                     {
@@ -2203,6 +2220,7 @@ static void decodeAudio(hb_work_private_t *pv, packet_info_t * packet_info)
         else
         {
             AVFrameSideData *side_data;
+            uint64_t         channel_layout;
             if ((side_data =
                  av_frame_get_side_data(pv->frame,
                                 AV_FRAME_DATA_DOWNMIX_INFO)) != NULL)
@@ -2227,8 +2245,13 @@ static void decodeAudio(hb_work_private_t *pv, packet_info_t * packet_info)
                                                  center_mix_level,
                                                  downmix_info->lfe_mix_level);
             }
-            hb_audio_resample_set_channel_layout(pv->resample,
-                                                 pv->frame->channel_layout);
+            channel_layout = pv->frame->channel_layout;
+            if (channel_layout == 0)
+            {
+                channel_layout = av_get_default_channel_layout(
+                                                        pv->frame->channels);
+            }
+            hb_audio_resample_set_channel_layout(pv->resample, channel_layout);
             hb_audio_resample_set_sample_fmt(pv->resample,
                                              pv->frame->format);
             if (hb_audio_resample_update(pv->resample))